diff --git a/share/man/man9/malloc.9 b/share/man/man9/malloc.9 index a2978af3a802..eec2c8691029 100644 --- a/share/man/man9/malloc.9 +++ b/share/man/man9/malloc.9 @@ -29,7 +29,7 @@ .\" $NetBSD: malloc.9,v 1.3 1996/11/11 00:05:11 lukem Exp $ .\" $FreeBSD$ .\" -.Dd January 16, 2014 +.Dd November 19, 2015 .Dt MALLOC 9 .Os .Sh NAME @@ -227,6 +227,13 @@ may sleep when called with .Dv M_WAITOK . .Fn free never sleeps. +However, +.Fn malloc , +.Fn realloc, +.Fn reallocf +and +.Fn free +may not be called in a critical section or while holding a spin lock. .Pp Any calls to .Fn malloc diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c index ff5b106e385e..01aff78a37e6 100644 --- a/sys/kern/kern_malloc.c +++ b/sys/kern/kern_malloc.c @@ -476,6 +476,9 @@ malloc(unsigned long size, struct malloc_type *mtp, int flags) KASSERT(curthread->td_intr_nesting_level == 0, ("malloc(M_WAITOK) in interrupt context")); + KASSERT(curthread->td_critnest == 0, + ("malloc: called with spinlock or critical section held")); + #ifdef DEBUG_MEMGUARD if (memguard_cmp_mtp(mtp, size)) { va = memguard_alloc(size, flags); @@ -542,6 +545,9 @@ free(void *addr, struct malloc_type *mtp) KASSERT(mtp->ks_magic == M_MAGIC, ("free: bad malloc type magic")); + KASSERT(curthread->td_critnest == 0, + ("free: called with spinlock or critical section held")); + /* free(NULL, ...) does nothing */ if (addr == NULL) return; @@ -605,6 +611,9 @@ realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags) KASSERT(mtp->ks_magic == M_MAGIC, ("realloc: bad malloc type magic")); + KASSERT(curthread->td_critnest == 0, + ("realloc: called with spinlock or critical section held")); + /* realloc(NULL, ...) is equivalent to malloc(...) */ if (addr == NULL) return (malloc(size, mtp, flags)); diff --git a/sys/vm/uma_core.c b/sys/vm/uma_core.c index 8169256994dc..1f57dfff898c 100644 --- a/sys/vm/uma_core.c +++ b/sys/vm/uma_core.c @@ -2149,6 +2149,10 @@ uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "uma_zalloc_arg: zone \"%s\"", zone->uz_name); } + + KASSERT(curthread->td_critnest == 0, + ("uma_zalloc_arg: called with spinlock or critical section held")); + #ifdef DEBUG_MEMGUARD if (memguard_cmp_zone(zone)) { item = memguard_alloc(zone->uz_size, flags); @@ -2686,6 +2690,9 @@ uma_zfree_arg(uma_zone_t zone, void *item, void *udata) CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread, zone->uz_name); + KASSERT(curthread->td_critnest == 0, + ("uma_zfree_arg: called with spinlock or critical section held")); + /* uma_zfree(..., NULL) does nothing, to match free(9). */ if (item == NULL) return;