Skip to content

Commit 4ec1026

Browse files
committed
mm, slab: unlink slabinfo, sysfs and debugfs immediately
kmem_cache_destroy() includes removing the associated sysfs and debugfs directories, and the cache from the list of caches that appears in /proc/slabinfo. Currently this might not happen immediately when: - the cache is SLAB_TYPESAFE_BY_RCU and the cleanup is delayed, including the directores removal - __kmem_cache_shutdown() fails due to outstanding objects - the directories remain indefinitely When a cache is recreated with the same name, such as due to module unload followed by a load, the directories will fail to be recreated for the new instance of the cache due to the old directories being present. The cache will also appear twice in /proc/slabinfo. While we want to convert the SLAB_TYPESAFE_BY_RCU cleanup to be synchronous again, the second point remains. So let's fix this first and have the directories and slabinfo removed immediately in kmem_cache_destroy() and regardless of __kmem_cache_shutdown() success. This should not make debugging harder if __kmem_cache_shutdown() fails, because a detailed report of outstanding objects is printed into dmesg already due to the failure. Also simplify kmem_cache_release() sysfs handling by using __is_defined(SLAB_SUPPORTS_SYSFS). Note the resulting code in kmem_cache_destroy() is a bit ugly but will be further simplified - this is in order to make small bisectable steps. Reviewed-by: Jann Horn <[email protected]> Signed-off-by: Vlastimil Babka <[email protected]>
1 parent b595978 commit 4ec1026

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

mm/slab_common.c

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -484,31 +484,19 @@ kmem_buckets *kmem_buckets_create(const char *name, slab_flags_t flags,
484484
}
485485
EXPORT_SYMBOL(kmem_buckets_create);
486486

487-
#ifdef SLAB_SUPPORTS_SYSFS
488487
/*
489488
* For a given kmem_cache, kmem_cache_destroy() should only be called
490489
* once or there will be a use-after-free problem. The actual deletion
491490
* and release of the kobject does not need slab_mutex or cpu_hotplug_lock
492491
* protection. So they are now done without holding those locks.
493-
*
494-
* Note that there will be a slight delay in the deletion of sysfs files
495-
* if kmem_cache_release() is called indrectly from a work function.
496492
*/
497493
static void kmem_cache_release(struct kmem_cache *s)
498494
{
499-
if (slab_state >= FULL) {
500-
sysfs_slab_unlink(s);
495+
if (__is_defined(SLAB_SUPPORTS_SYSFS) && slab_state >= FULL)
501496
sysfs_slab_release(s);
502-
} else {
497+
else
503498
slab_kmem_cache_release(s);
504-
}
505499
}
506-
#else
507-
static void kmem_cache_release(struct kmem_cache *s)
508-
{
509-
slab_kmem_cache_release(s);
510-
}
511-
#endif
512500

513501
static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
514502
{
@@ -534,7 +522,6 @@ static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
534522
rcu_barrier();
535523

536524
list_for_each_entry_safe(s, s2, &to_destroy, list) {
537-
debugfs_slab_release(s);
538525
kfence_shutdown_cache(s);
539526
kmem_cache_release(s);
540527
}
@@ -549,20 +536,23 @@ void slab_kmem_cache_release(struct kmem_cache *s)
549536

550537
void kmem_cache_destroy(struct kmem_cache *s)
551538
{
552-
int err = -EBUSY;
553539
bool rcu_set;
540+
int err;
554541

555542
if (unlikely(!s) || !kasan_check_byte(s))
556543
return;
557544

558545
cpus_read_lock();
559546
mutex_lock(&slab_mutex);
560547

561-
rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;
562-
563548
s->refcount--;
564-
if (s->refcount)
565-
goto out_unlock;
549+
if (s->refcount) {
550+
mutex_unlock(&slab_mutex);
551+
cpus_read_unlock();
552+
return;
553+
}
554+
555+
rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU;
566556

567557
/* free asan quarantined objects */
568558
kasan_cache_shutdown(s);
@@ -571,24 +561,29 @@ void kmem_cache_destroy(struct kmem_cache *s)
571561
WARN(err, "%s %s: Slab cache still has objects when called from %pS",
572562
__func__, s->name, (void *)_RET_IP_);
573563

574-
if (err)
575-
goto out_unlock;
576-
577564
list_del(&s->list);
578565

579-
if (rcu_set) {
580-
list_add_tail(&s->list, &slab_caches_to_rcu_destroy);
581-
schedule_work(&slab_caches_to_rcu_destroy_work);
582-
} else {
566+
if (!err && !rcu_set)
583567
kfence_shutdown_cache(s);
584-
debugfs_slab_release(s);
585-
}
586568

587-
out_unlock:
588569
mutex_unlock(&slab_mutex);
589570
cpus_read_unlock();
590-
if (!err && !rcu_set)
571+
572+
if (slab_state >= FULL)
573+
sysfs_slab_unlink(s);
574+
debugfs_slab_release(s);
575+
576+
if (err)
577+
return;
578+
579+
if (rcu_set) {
580+
mutex_lock(&slab_mutex);
581+
list_add_tail(&s->list, &slab_caches_to_rcu_destroy);
582+
schedule_work(&slab_caches_to_rcu_destroy_work);
583+
mutex_unlock(&slab_mutex);
584+
} else {
591585
kmem_cache_release(s);
586+
}
592587
}
593588
EXPORT_SYMBOL(kmem_cache_destroy);
594589

0 commit comments

Comments
 (0)