hugetlb: ensure hugepage access is denied if hugepages are not supported
[platform/adaptation/renesas_rcar/renesas_kernel.git] / mm / slab_common.c
index f70df3e..f149e67 100644 (file)
@@ -56,7 +56,7 @@ static int kmem_cache_sanity_check(struct mem_cgroup *memcg, const char *name,
                        continue;
                }
 
-#if !defined(CONFIG_SLUB) || !defined(CONFIG_SLUB_DEBUG_ON)
+#if !defined(CONFIG_SLUB)
                /*
                 * For simplicity, we won't check this in the list of memcg
                 * caches. We have control over memcg naming, and if there
@@ -180,6 +180,18 @@ kmem_cache_create_memcg(struct mem_cgroup *memcg, const char *name, size_t size,
        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
@@ -205,7 +217,7 @@ kmem_cache_create_memcg(struct mem_cgroup *memcg, const char *name, size_t size,
        if (!s->name)
                goto out_free_cache;
 
-       err = memcg_register_cache(memcg, s, parent_cache);
+       err = memcg_alloc_cache_params(memcg, s, parent_cache);
        if (err)
                goto out_free_cache;
 
@@ -215,13 +227,23 @@ kmem_cache_create_memcg(struct mem_cgroup *memcg, const char *name, size_t size,
 
        s->refcount = 1;
        list_add(&s->list, &slab_caches);
-       memcg_cache_list_add(memcg, s);
+       memcg_register_cache(s);
 
 out_unlock:
        mutex_unlock(&slab_mutex);
        put_online_cpus();
 
        if (err) {
+               /*
+                * There is no point in flooding logs with warnings or
+                * especially crashing the system if we fail to create a cache
+                * for a memcg. In this case we will be accounting the memcg
+                * allocation to the root cgroup until we succeed to create its
+                * own cache, but it isn't that critical.
+                */
+               if (!memcg)
+                       return NULL;
+
                if (flags & SLAB_PANIC)
                        panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
                                name, err);
@@ -235,6 +257,7 @@ out_unlock:
        return s;
 
 out_free_cache:
+       memcg_free_cache_params(s);
        kfree(s->name);
        kmem_cache_free(kmem_cache, s);
        goto out_unlock;
@@ -260,11 +283,12 @@ void kmem_cache_destroy(struct kmem_cache *s)
                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_release_cache(s);
+                       memcg_free_cache_params(s);
                        kfree(s->name);
                        kmem_cache_free(kmem_cache, s);
                } else {