Skip to content

Commit

Permalink
Linux: Never sleep in kmem_cache_alloc(..., KM_NOSLEEP)
Browse files Browse the repository at this point in the history
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 <behlendorf1@llnl.gov>
  • Loading branch information
behlendorf committed Jun 2, 2023
1 parent 4f583a8 commit d291f06
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions module/os/linux/spl/spl-kmem-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit d291f06

Please sign in to comment.