Skip to content

Commit 421af24

Browse files
Vladimir Davydovtorvalds
authored andcommitted
slub: do not drop slab_mutex for sysfs_slab_add
We release the slab_mutex while calling sysfs_slab_add from __kmem_cache_create since commit 66c4c35 ("slub: Do not hold slub_lock when calling sysfs_slab_add()"), because kobject_uevent called by sysfs_slab_add might block waiting for the usermode helper to exec, which would result in a deadlock if we took the slab_mutex while executing it. However, apart from complicating synchronization rules, releasing the slab_mutex on kmem cache creation can result in a kmemcg-related race. The point is that we check if the memcg cache exists before going to __kmem_cache_create, but register the new cache in memcg subsys after it. Since we can drop the mutex there, several threads can see that the memcg cache does not exist and proceed to creating it, which is wrong. Fortunately, recently kobject_uevent was patched to call the usermode helper with the UMH_NO_WAIT flag, making the deadlock impossible. Therefore there is no point in releasing the slab_mutex while calling sysfs_slab_add, so let's simplify kmem_cache_create synchronization and fix the kmemcg-race mentioned above by holding the slab_mutex during the whole cache creation path. Signed-off-by: Vladimir Davydov <vdavydov@parallels.com> Acked-by: Christoph Lameter <cl@linux.com> Cc: Greg KH <greg@kroah.com> Cc: Pekka Enberg <penberg@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent bcccff9 commit 421af24

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

‎mm/slub.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3237,8 +3237,9 @@ int __kmem_cache_shutdown(struct kmem_cache *s)
32373237

32383238
if (!rc) {
32393239
/*
3240-
* We do the same lock strategy around sysfs_slab_add, see
3241-
* __kmem_cache_create. Because this is pretty much the last
3240+
* Since slab_attr_store may take the slab_mutex, we should
3241+
* release the lock while removing the sysfs entry in order to
3242+
* avoid a deadlock. Because this is pretty much the last
32423243
* operation we do and the lock will be released shortly after
32433244
* that in slab_common.c, we could just move sysfs_slab_remove
32443245
* to a later point in common code. We should do that when we
@@ -3778,10 +3779,7 @@ int __kmem_cache_create(struct kmem_cache *s, unsigned long flags)
37783779
return 0;
37793780

37803781
memcg_propagate_slab_attrs(s);
3781-
mutex_unlock(&slab_mutex);
37823782
err = sysfs_slab_add(s);
3783-
mutex_lock(&slab_mutex);
3784-
37853783
if (err)
37863784
kmem_cache_close(s);
37873785

0 commit comments

Comments
 (0)