Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 12 Aug 2013 09:01:54 -0700
From:      David Wolfskill <david@catwhisker.org>
To:        Davide Italiano <davide@freebsd.org>
Cc:        current@freebsd.org
Subject:   Re: Early drop to debugger with DEBUG_MEMGUARD
Message-ID:  <20130812160154.GF1570@albert.catwhisker.org>
In-Reply-To: <CACYV=-FH6KinGXFkgs6hyuegHtSsMyWusUuv9DHqqgyOQ1_mDg@mail.gmail.com>
References:  <20130812151314.GE1570@albert.catwhisker.org> <CACYV=-FH6KinGXFkgs6hyuegHtSsMyWusUuv9DHqqgyOQ1_mDg@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--XIiC+We3v3zHqZ6Z
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Mon, Aug 12, 2013 at 08:30:15AM -0700, Davide Italiano wrote:
> ...
> > Booting...
> > GDB: no debug ports present
> > KDB: debugger backends: ddb
> > KDB: current backend: ddb
> > Copyright (c) 1992-2013 The FreeBSD Project.
> > Copyright (c) 1979, 1980, 1983, 1986, 1988, 1989, 1991, 1992, 1993, 1994
> >         The Regents of the University of California. All rights reserve=
d.
> > FreeBSD is a registered trademark of The FreeBSD Foundation.
> > FreeBSD 10.0-CURRENT #0  r254245M/254246:1000042: Mon Aug 12 07:20:47 P=
DT 2013
> >     root@freebeast.catwhisker.org:/common/S3/obj/usr/src/sys/MEMGUARD i=
386
> > FreeBSD clang version 3.3 (tags/RELEASE_33/final 183502) 20130610
> > WARNING: WITNESS option enabled, expect reduced performance.
> > panic: Assertion strat =3D=3D M_BESTFIT || strat =3D=3D M_FIRSTFIT fail=
ed at /usr/src/sys/kern/subr_vmem.c:1050
> > cpuid =3D 0
> > KDB: stack backtrace:
> > db_trace_self_wrapper(c116fcdc,73752f20,72732f72,79732f63,656b2f73,...)=
 at db_trace_self_wrapper+0x2d/frame 0xc1820ba0
> > kdb_backtrace(c11c4b23,0,c0f8a835,c1820c74,c0f8a835,...) at kdb_backtra=
ce+0x30/frame 0xc1820c08
> > vpanic(c12eea08,100,c0f8a835,c1820c74,c1820c74,...) at vpanic+0x11f/fra=
me 0xc1820c44
> > kassert_panic(c0f8a835,c1172e98,c1172e39,41a,8,...) at kassert_panic+0x=
ea/frame 0xc1820c68
> > vmem_alloc(c130d680,6681000,2,c1820cc0,3b5,...) at vmem_alloc+0x53/fram=
e 0xc1820ca0
> > memguard_init(c130d680,c0a9fa50,c6800000,20281000,1000,10000,0) at memg=
uard_init+0x29/frame 0xc1820cc4
> > kmeminit(c14b9fd4,c10efc89,0,0,c1820d30,...) at kmeminit+0x171/frame 0x=
c1820cf0
> > mallocinit(0,0,2,0,c11d3728,...) at mallocinit+0x32/frame 0xc1820d30
> > mi_startup() at mi_startup+0xf7/frame 0xc1820d58
> > begin() at begin+0x2c
> > KDB: enter: panic
> > [ thread pid 0 tid 0 ]
> > Stopped at      kdb_enter+0x3d: movl    $0,kdb_why
> > db>
> >
> > As you can see, this is well before any device probes or much of
> > anything else.  Thus, I suspect that it's fairly possible that the
> > assertion may well be OK after a certain point in the boot sequence,
> > but decidedly *not* OK in this specific instance.  Or perhaps the
> > assertion just doesn't play well with DEBUG_MEMGUARD.
> ...

> vmem_alloc() KPI needs the consumer to specify exactly a strategy for
> allocation, which is one of two between: M_FIRSTFIT/M_BESTFIT (fast
> allocation vs low fragmentation), and that's the assertion that's not
> respected within the code.
>=20
> 1050	        MPASS(strat =3D=3D M_BESTFIT || strat =3D=3D M_FIRSTFIT);
>=20
> It looks like memguard_init() doesn't specify none of these two strategie=
s.
>=20
> 209	        vmem_alloc(parent, memguard_mapsize, M_WAITOK, &base);
>=20
> My guess is that you need to OR one between M_BESTFIT/M_FIRSTFIT with
> M_WAITOK to have your kernel booting. What's better between the two
> probably will need some measurements but this should at least make
> your kernel booting.

Thank you for the insight & suggestion.

My first attempt was to make the following change:

Index: sys/vm/memguard.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- sys/vm/memguard.c	(revision 254246)
+++ sys/vm/memguard.c	(working copy)
@@ -206,9 +206,9 @@
 {
 	vm_offset_t base;
=20
-	vmem_alloc(parent, memguard_mapsize, M_WAITOK, &base);
+	vmem_alloc(parent, memguard_mapsize, M_WAITOK | M_FIRSTFIT, &base);
 	memguard_map =3D vmem_create("memguard arena", base, memguard_mapsize,
-	    PAGE_SIZE, 0, M_WAITOK);
+	    PAGE_SIZE, 0, M_WAITOK | M_FIRSTFIT);
 	memguard_cursor =3D base;
 	memguard_base =3D base;
=20
This built OK; but attempting to boot yielded:

Booting...
GDB: no debug ports present
KDB: debugger backends: ddb
KDB: current backend: ddb
Copyright (c) 1992-2013 The FreeBSD Project.
Copyright (c) 1979, 1980, 1983, 1986, 1988, 1989, 1991, 1992, 1993, 1994
        The Regents of the University of California. All rights reserved.
FreeBSD is a registered trademark of The FreeBSD Foundation.
FreeBSD 10.0-CURRENT #1  r254245M/254246:1000042: Mon Aug 12 08:49:12 PDT 2=
013
    root@freebeast.catwhisker.org:/common/S3/obj/usr/src/sys/MEMGUARD i386
FreeBSD clang version 3.3 (tags/RELEASE_33/final 183502) 20130610
WARNING: WITNESS option enabled, expect reduced performance.
panic: mti_zone 195 out of range 8
cpuid =3D 0
KDB: stack backtrace:
db_trace_self_wrapper(c116fcdc,0,ffffffff,c1167d73,fffffffe,...) at db_trac=
e_self_wrapper+0x2d/frame 0xc1820b58
kdb_backtrace(c11c4b23,0,c1167d58,c1820c30,c1820c00,...) at kdb_backtrace+0=
x30/frame 0xc1820bc0
vpanic(c12eea08,100,c1167d58,c1820c30,c1820c30,...) at vpanic+0x11f/frame 0=
xc1820c00
kassert_panic(c1167d58,c3,8,c130d7e4,c130d7a8,...) at kassert_panic+0xea/fr=
ame 0xc1820c24
malloc(380,c1279778,2,0,ffffffff,...) at malloc+0x308/frame 0xc1820c70
vmem_create(c11a7530,c6800000,6681000,1000,0,...) at vmem_create+0x29/frame=
 0xc1820ca0
memguard_init(c130d680,c0a9fa50,c6800000,20281000,1000,10000,0) at memguard=
_init+0x5e/frame 0xc1820cc4
kmeminit(c14b9fd4,c10efc89,0,0,c1820d30,...) at kmeminit+0x171/frame 0xc182=
0cf0
mallocinit(0,0,2,0,c11d3728,...) at mallocinit+0x32/frame 0xc1820d30
mi_startup() at mi_startup+0xf7/frame 0xc1820d58
begin() at begin+0x2c
KDB: enter: panic
[ thread pid 0 tid 0 ]
Stopped at      kdb_enter+0x3d: movl    $0,kdb_why
db>=20


grepping through the sources indicates to me that I/we have run afoul of:

=2E/kern/kern_malloc.c-504-               if (size & KMEM_ZMASK)
=2E/kern/kern_malloc.c-505-                       size =3D (size & ~KMEM_ZM=
ASK) + KMEM_ZBASE;
=2E/kern/kern_malloc.c-506-               indx =3D kmemsize[size >> KMEM_ZS=
HIFT];
=2E/kern/kern_malloc.c:507:               KASSERT(mtip->mti_zone < numzones,
=2E/kern/kern_malloc.c:508:                   ("mti_zone %u out of range %d=
",
=2E/kern/kern_malloc.c:509:                   mtip->mti_zone, numzones));
=2E/kern/kern_malloc.c:510:               zone =3D kmemzones[indx].kz_zone[=
mtip->mti_zone];
=2E/kern/kern_malloc.c-511-#ifdef MALLOC_PROFILE
=2E/kern/kern_malloc.c-512-               krequests[size >> KMEM_ZSHIFT]++;
=2E/kern/kern_malloc.c-513-#endif

Hmm....

Peace,
david
--=20
David H. Wolfskill				david@catwhisker.org
Taliban: Evil men with guns afraid of truth from a 14-year old girl.

See http://www.catwhisker.org/~david/publickey.gpg for my public key.

--XIiC+We3v3zHqZ6Z
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.20 (FreeBSD)

iEYEARECAAYFAlIJBvEACgkQmprOCmdXAD0WqQCbBL7CdQvKDG8gWwmDjEXMqgV0
B0AAn2xZD22CxdHLChwQfr351NyGONFh
=nDTz
-----END PGP SIGNATURE-----

--XIiC+We3v3zHqZ6Z--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20130812160154.GF1570>