Date: Tue, 2 Apr 2002 11:05:24 -0800 (PST) From: Matthew Dillon <dillon@apollo.backplane.com> To: hackers@freebsd.org Cc: Jeff Roberson <jroberson@chesapeake.net> Subject: adding allocation failure detection / automatic panic Message-ID: <200204021905.g32J5Oa17043@apollo.backplane.com> References: <20020327193305.D16840-100000@mail.chesapeake.net>
next in thread | previous in thread | raw e-mail | index | archive | help
While working on PR 36504 I noted that there are situations where the system cannot continue if zalloc() fails. Rather then force the caller of zalloc() to check for NULL in these cases, I think it makes sense to add another flag, ZONE_PANICFAIL, allowing a zone to automatically panic if the allocation fails. Normal zone allocations fail only when the system has run out of KVM or the zone has reached its size limit. The proposed patch is shown below. I thought about putting the check in _zget() but I think it is safer to panic() after the mutex is released. I intend to use the flag for the zone we allocate vm object's out of. I'm sure there are other zones that could use it as well. This way we get an obvious panic message rather then a kernel trap / panic. I'm CCing Jeff as I believe it would be beneficial for him to add a similar flag to his UMA code (if he hasn't already), and eventually this stuff will have to translate over anyway. -Matt Index: vm/vm_zone.c =================================================================== RCS file: /home/ncvs/src/sys/vm/vm_zone.c,v retrieving revision 1.54 diff -u -r1.54 vm_zone.c --- vm/vm_zone.c 18 Mar 2002 15:08:09 -0000 1.54 +++ vm/vm_zone.c 2 Apr 2002 18:59:06 -0000 @@ -108,6 +108,8 @@ * size size of zone entries. * nentries number of zone entries allocated (only ZONE_INTERRUPT.) * flags ZONE_INTERRUPT -- items can be allocated at interrupt time. + * ZONE_PANICFAIL -- panic on failure (allows caller to + * assume that the zalloc() always succeeds) * zalloc number of pages allocated when memory is needed. * * Note that when using ZONE_INTERRUPT, the size of the zone is limited @@ -482,6 +484,7 @@ item = z->zitems; z->zitems = ((void **) item)[0]; #ifdef INVARIANTS + KASSERT(item == NULL, ("zitems unexpectedly NULL")); KASSERT(((void **) item)[1] == ZENTRY_FREE, ("item is not free")); ((void **) item)[1] = 0; @@ -492,6 +495,14 @@ out: mtx_unlock(&z->zmtx); + + /* + * PANICFAIL allows the caller to assume that the zalloc() will + * always suceed. If it doesn't, we panic here after we release + * the mutex. + */ + if (item == NULL && (z->zflags & ZONE_PANICFAIL)) + panic("zalloc(%s) failed", z->zname); return item; } Index: vm/vm_zone.h =================================================================== RCS file: /home/ncvs/src/sys/vm/vm_zone.h,v retrieving revision 1.20 diff -u -r1.20 vm_zone.h --- vm/vm_zone.h 19 Mar 2002 09:11:49 -0000 1.20 +++ vm/vm_zone.h 2 Apr 2002 18:57:18 -0000 @@ -18,8 +18,9 @@ #define _SYS_ZONE_H -#define ZONE_INTERRUPT 1 /* Use this if you need to allocate at int time */ -#define ZONE_BOOT 16 /* This is an internal flag used by zbootinit */ +#define ZONE_INTERRUPT 0x0001 /* If you need to allocate at int time */ +#define ZONE_PANICFAIL 0x0002 /* panic if the zalloc fails */ +#define ZONE_BOOT 0x0010 /* Internal flag used by zbootinit */ #include <sys/_lock.h> #include <sys/_mutex.h> To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200204021905.g32J5Oa17043>