if (is_root_cache(s))
                return;
 
+       /*
+        * Holding the slab_mutex assures nobody will touch the memcg_caches
+        * array while we are modifying it.
+        */
+       lockdep_assert_held(&slab_mutex);
+
        root = s->memcg_params->root_cache;
        memcg = s->memcg_params->memcg;
        id = memcg_cache_id(memcg);
         * before adding it to the memcg_slab_caches list, otherwise we can
         * fail to convert memcg_params_to_cache() while traversing the list.
         */
+       VM_BUG_ON(root->memcg_params->memcg_caches[id]);
        root->memcg_params->memcg_caches[id] = s;
 
        mutex_lock(&memcg->slab_caches_mutex);
        if (is_root_cache(s))
                return;
 
+       /*
+        * Holding the slab_mutex assures nobody will touch the memcg_caches
+        * array while we are modifying it.
+        */
+       lockdep_assert_held(&slab_mutex);
+
        root = s->memcg_params->root_cache;
        memcg = s->memcg_params->memcg;
        id = memcg_cache_id(memcg);
         * after removing it from the memcg_slab_caches list, otherwise we can
         * fail to convert memcg_params_to_cache() while traversing the list.
         */
+       VM_BUG_ON(!root->memcg_params->memcg_caches[id]);
        root->memcg_params->memcg_caches[id] = NULL;
 
        css_put(&memcg->css);
                                                  struct kmem_cache *cachep)
 {
        struct kmem_cache *new_cachep;
-       int idx;
 
        BUG_ON(!memcg_can_account_kmem(memcg));
 
-       idx = memcg_cache_id(memcg);
-
        mutex_lock(&memcg_cache_mutex);
-       new_cachep = cache_from_memcg_idx(cachep, idx);
-       if (new_cachep)
-               goto out;
-
        new_cachep = kmem_cache_dup(memcg, cachep);
        if (new_cachep == NULL)
                new_cachep = cachep;
-
-out:
        mutex_unlock(&memcg_cache_mutex);
        return new_cachep;
 }
 
        if (err)
                goto out_unlock;
 
+       if (memcg) {
+               /*
+                * Since per-memcg caches are created asynchronously on first
+                * allocation (see memcg_kmem_get_cache()), several threads can
+                * try to create the same cache, but only one of them may
+                * succeed. Therefore if we get here and see the cache has
+                * already been created, we silently return NULL.
+                */
+               if (cache_from_memcg_idx(parent_cache, memcg_cache_id(memcg)))
+                       goto out_unlock;
+       }
+
        /*
         * Some allocators will constraint the set of valid flags to a subset
         * of all flags. We expect them to define CACHE_CREATE_MASK in this
                list_del(&s->list);
 
                if (!__kmem_cache_shutdown(s)) {
+                       memcg_unregister_cache(s);
                        mutex_unlock(&slab_mutex);
                        if (s->flags & SLAB_DESTROY_BY_RCU)
                                rcu_barrier();
 
-                       memcg_unregister_cache(s);
                        memcg_free_cache_params(s);
                        kfree(s->name);
                        kmem_cache_free(kmem_cache, s);