Merge tag 'backport/v3.14.24-ltsi-rc1/mach-shmobile-20141211' into backport/v3.14...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / mm / slab.h
index 0859c42..8184a7c 100644 (file)
--- a/mm/slab.h
+++ b/mm/slab.h
@@ -160,12 +160,36 @@ static inline const char *cache_name(struct kmem_cache *s)
        return s->name;
 }
 
+/*
+ * Note, we protect with RCU only the memcg_caches array, not per-memcg caches.
+ * That said the caller must assure the memcg's cache won't go away. Since once
+ * created a memcg's cache is destroyed only along with the root cache, it is
+ * true if we are going to allocate from the cache or hold a reference to the
+ * root cache by other means. Otherwise, we should hold either the slab_mutex
+ * or the memcg's slab_caches_mutex while calling this function and accessing
+ * the returned value.
+ */
 static inline struct kmem_cache *
 cache_from_memcg_idx(struct kmem_cache *s, int idx)
 {
+       struct kmem_cache *cachep;
+       struct memcg_cache_params *params;
+
        if (!s->memcg_params)
                return NULL;
-       return s->memcg_params->memcg_caches[idx];
+
+       rcu_read_lock();
+       params = rcu_dereference(s->memcg_params);
+       cachep = params->memcg_caches[idx];
+       rcu_read_unlock();
+
+       /*
+        * Make sure we will access the up-to-date value. The code updating
+        * memcg_caches issues a write barrier to match this (see
+        * memcg_register_cache()).
+        */
+       smp_read_barrier_depends();
+       return cachep;
 }
 
 static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s)