Skip to content

Commit 837e426

Browse files
behlendorftonyhutter
authored andcommitted
Linux: Never sleep in kmem_cache_alloc(..., KM_NOSLEEP) (#14926)
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]> Reviewed-by: Adam Moss <[email protected]> Reviewed-by: Brian Atkinson <[email protected]> Reviewed-by: Richard Yao <[email protected]> Reviewed-by: Tony Hutter <[email protected]>
1 parent 426d07d commit 837e426

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,9 +1020,19 @@ spl_cache_grow(spl_kmem_cache_t *skc, int flags, void **obj)
10201020
ASSERT0(flags & ~KM_PUBLIC_MASK);
10211021
ASSERT(skc->skc_magic == SKC_MAGIC);
10221022
ASSERT((skc->skc_flags & KMC_SLAB) == 0);
1023-
might_sleep();
1023+
10241024
*obj = NULL;
10251025

1026+
/*
1027+
* Since we can't sleep attempt an emergency allocation to satisfy
1028+
* the request. The only alterative is to fail the allocation but
1029+
* it's preferable try. The use of KM_NOSLEEP is expected to be rare.
1030+
*/
1031+
if (flags & KM_NOSLEEP)
1032+
return (spl_emergency_alloc(skc, flags, obj));
1033+
1034+
might_sleep();
1035+
10261036
/*
10271037
* Before allocating a new slab wait for any reaping to complete and
10281038
* then return so the local magazine can be rechecked for new objects.

0 commit comments

Comments
 (0)