css_get(&memcg->css);
 
-       mutex_lock(&memcg->slab_caches_mutex);
-       list_add(&s->memcg_params->list, &memcg->memcg_slab_caches);
-       mutex_unlock(&memcg->slab_caches_mutex);
 
        /*
         * Since readers won't lock (see cache_from_memcg_idx()), we need a
         */
        smp_wmb();
 
+       /*
+        * Initialize the pointer to this cache in its parent's memcg_params
+        * before adding it to the memcg_slab_caches list, otherwise we can
+        * fail to convert memcg_params_to_cache() while traversing the list.
+        */
        root->memcg_params->memcg_caches[id] = s;
+
+       mutex_lock(&memcg->slab_caches_mutex);
+       list_add(&s->memcg_params->list, &memcg->memcg_slab_caches);
+       mutex_unlock(&memcg->slab_caches_mutex);
 }
 
 void memcg_unregister_cache(struct kmem_cache *s)
        if (is_root_cache(s))
                return;
 
-       memcg = s->memcg_params->memcg;
-       id  = memcg_cache_id(memcg);
-
        root = s->memcg_params->root_cache;
-       root->memcg_params->memcg_caches[id] = NULL;
+       memcg = s->memcg_params->memcg;
+       id = memcg_cache_id(memcg);
 
        mutex_lock(&memcg->slab_caches_mutex);
        list_del(&s->memcg_params->list);
        mutex_unlock(&memcg->slab_caches_mutex);
 
+       /*
+        * Clear the pointer to this cache in its parent's memcg_params only
+        * after removing it from the memcg_slab_caches list, otherwise we can
+        * fail to convert memcg_params_to_cache() while traversing the list.
+        */
+       root->memcg_params->memcg_caches[id] = NULL;
+
        css_put(&memcg->css);
 }