Skip to content

Commit ec66e0d

Browse files
committed
slab: add sheaf support for batching kfree_rcu() operations
Extend the sheaf infrastructure for more efficient kfree_rcu() handling. For caches with sheaves, on each cpu maintain a rcu_free sheaf in addition to main and spare sheaves. kfree_rcu() operations will try to put objects on this sheaf. Once full, the sheaf is detached and submitted to call_rcu() with a handler that will try to put it in the barn, or flush to slab pages using bulk free, when the barn is full. Then a new empty sheaf must be obtained to put more objects there. It's possible that no free sheaves are available to use for a new rcu_free sheaf, and the allocation in kfree_rcu() context can only use GFP_NOWAIT and thus may fail. In that case, fall back to the existing kfree_rcu() implementation. Expected advantages: - batching the kfree_rcu() operations, that could eventually replace the existing batching - sheaves can be reused for allocations via barn instead of being flushed to slabs, which is more efficient - this includes cases where only some cpus are allowed to process rcu callbacks (CONFIG_RCU_NOCB_CPU) Possible disadvantage: - objects might be waiting for more than their grace period (it is determined by the last object freed into the sheaf), increasing memory usage - but the existing batching does that too. Only implement this for CONFIG_KVFREE_RCU_BATCHED as the tiny implementation favors smaller memory footprint over performance. Also for now skip the usage of rcu sheaf for CONFIG_PREEMPT_RT as the contexts where kfree_rcu() is called might not be compatible with taking a barn spinlock or a GFP_NOWAIT allocation of a new sheaf taking a spinlock - the current kfree_rcu() implementation avoids doing that. Teach kvfree_rcu_barrier() to flush all rcu_free sheaves from all caches that have them. This is not a cheap operation, but the barrier usage is rare - currently kmem_cache_destroy() or on module unload. Add CONFIG_SLUB_STATS counters free_rcu_sheaf and free_rcu_sheaf_fail to count how many kfree_rcu() used the rcu_free sheaf successfully and how many had to fall back to the existing implementation. Reviewed-by: Harry Yoo <harry.yoo@oracle.com> Reviewed-by: Suren Baghdasaryan <surenb@google.com> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
1 parent 2d517aa commit ec66e0d

3 files changed

Lines changed: 295 additions & 2 deletions

File tree

mm/slab.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,9 @@ static inline bool is_kmalloc_normal(struct kmem_cache *s)
435435
return !(s->flags & (SLAB_CACHE_DMA|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT));
436436
}
437437

438+
bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj);
439+
void flush_all_rcu_sheaves(void);
440+
438441
#define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
439442
SLAB_CACHE_DMA32 | SLAB_PANIC | \
440443
SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS | \

mm/slab_common.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,27 @@ static void kfree_rcu_work(struct work_struct *work)
16081608
kvfree_rcu_list(head);
16091609
}
16101610

1611+
static bool kfree_rcu_sheaf(void *obj)
1612+
{
1613+
struct kmem_cache *s;
1614+
struct folio *folio;
1615+
struct slab *slab;
1616+
1617+
if (is_vmalloc_addr(obj))
1618+
return false;
1619+
1620+
folio = virt_to_folio(obj);
1621+
if (unlikely(!folio_test_slab(folio)))
1622+
return false;
1623+
1624+
slab = folio_slab(folio);
1625+
s = slab->slab_cache;
1626+
if (s->cpu_sheaves)
1627+
return __kfree_rcu_sheaf(s, obj);
1628+
1629+
return false;
1630+
}
1631+
16111632
static bool
16121633
need_offload_krc(struct kfree_rcu_cpu *krcp)
16131634
{
@@ -1952,6 +1973,9 @@ void kvfree_call_rcu(struct rcu_head *head, void *ptr)
19521973
if (!head)
19531974
might_sleep();
19541975

1976+
if (!IS_ENABLED(CONFIG_PREEMPT_RT) && kfree_rcu_sheaf(ptr))
1977+
return;
1978+
19551979
// Queue the object but don't yet schedule the batch.
19561980
if (debug_rcu_head_queue(ptr)) {
19571981
// Probable double kfree_rcu(), just leak.
@@ -2026,6 +2050,8 @@ void kvfree_rcu_barrier(void)
20262050
bool queued;
20272051
int i, cpu;
20282052

2053+
flush_all_rcu_sheaves();
2054+
20292055
/*
20302056
* Firstly we detach objects and queue them over an RCU-batch
20312057
* for all CPUs. Finally queued works are flushed for each CPU.

0 commit comments

Comments
 (0)