From d291f06b5ac1e9cbbf9c9142310a5bc1cc216cdd Mon Sep 17 00:00:00 2001 From: Brian Behlendorf Date: Thu, 1 Jun 2023 17:18:30 -0700 Subject: [PATCH] Linux: Never sleep in kmem_cache_alloc(..., KM_NOSLEEP) When a kmem cache is exhausted and needed to be expanded a new slab is allocated. KM_SLEEP callers can block and wait for the allocation, but KM_NOSLEEP callers were incorrectly allowed to block as well. Resolve this by attempting an emergency allocation as a best effort. This may fail but that's fine since any KM_NOSLEEP consumer is required to handle an allocation failure. Signed-off-by: Brian Behlendorf --- module/os/linux/spl/spl-kmem-cache.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/module/os/linux/spl/spl-kmem-cache.c b/module/os/linux/spl/spl-kmem-cache.c index 745d03012f9d..21134f476b3c 100644 --- a/module/os/linux/spl/spl-kmem-cache.c +++ b/module/os/linux/spl/spl-kmem-cache.c @@ -1018,6 +1018,14 @@ spl_cache_grow(spl_kmem_cache_t *skc, int flags, void **obj) might_sleep(); *obj = NULL; + /* + * Since we can't sleep attempt an emergency allocation to satisfy + * the request. The only alterative is to fail the allocation but + * its preferable try. The use of KM_NOSLEEP is expected to be rare. + */ + if (flags & KM_NOSLEEP) + return (spl_emergency_alloc(skc, flags, obj)); + /* * Before allocating a new slab wait for any reaping to complete and * then return so the local magazine can be rechecked for new objects.