From 1921bb7b687c27b011bdfb07d6b1d4bc6493d539 Mon Sep 17 00:00:00 2001 From: Gleb Smirnoff Date: Mon, 17 Aug 2020 15:37:08 +0000 Subject: [PATCH] With INVARIANTS panic immediately if M_WAITOK is requested in a non-sleepable context. Previously only _sleep() would panic. This will catch misuse of M_WAITOK at development stage rather than at stress load stage. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D26027 --- sys/kern/kern_malloc.c | 3 +++ sys/vm/uma_core.c | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c index 300a1692e7d..46eb1c4347c 100644 --- a/sys/kern/kern_malloc.c +++ b/sys/kern/kern_malloc.c @@ -618,6 +618,9 @@ void * unsigned long osize = size; #endif + KASSERT((flags & M_WAITOK) == 0 || THREAD_CAN_SLEEP(), + ("malloc(M_WAITOK) in non-sleepable context")); + #ifdef MALLOC_DEBUG va = NULL; if (malloc_dbg(&va, &size, mtp, flags) != 0) diff --git a/sys/vm/uma_core.c b/sys/vm/uma_core.c index 9d0786c1d75..37d78354200 100644 --- a/sys/vm/uma_core.c +++ b/sys/vm/uma_core.c @@ -3328,6 +3328,9 @@ uma_zalloc_smr(uma_zone_t zone, int flags) uma_cache_bucket_t bucket; uma_cache_t cache; + KASSERT((flags & M_WAITOK) == 0 || THREAD_CAN_SLEEP(), + ("uma_zalloc_smr(M_WAITOK) in non-sleepable context")); + #ifdef UMA_ZALLOC_DEBUG void *item; @@ -3352,6 +3355,9 @@ uma_zalloc_arg(uma_zone_t zone, void *udata, int flags) uma_cache_bucket_t bucket; uma_cache_t cache; + KASSERT((flags & M_WAITOK) == 0 || THREAD_CAN_SLEEP(), + ("uma_zalloc(M_WAITOK) in non-sleepable context")); + /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */ random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);