Skip to content

Commit 499ee6a

Browse files
committed
Linux: Never sleep in kmem_cache_alloc(..., KM_NOSLEEP)
When a kmem cache is exhausted and needs 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 <[email protected]>
1 parent 4f583a8 commit 499ee6a

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

module/os/linux/spl/spl-kmem-cache.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,14 @@ spl_cache_grow(spl_kmem_cache_t *skc, int flags, void **obj)
10181018
might_sleep();
10191019
*obj = NULL;
10201020

1021+
/*
1022+
* Since we can't sleep attempt an emergency allocation to satisfy
1023+
* the request. The only alterative is to fail the allocation but
1024+
* it's preferable try. The use of KM_NOSLEEP is expected to be rare.
1025+
*/
1026+
if (flags & KM_NOSLEEP)
1027+
return (spl_emergency_alloc(skc, flags, obj));
1028+
10211029
/*
10221030
* Before allocating a new slab wait for any reaping to complete and
10231031
* then return so the local magazine can be rechecked for new objects.

0 commit comments

Comments
 (0)