Merge tag 'arm64-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/cmarinas...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / mm / memcontrol.c
index 270a367..09255ec 100644 (file)
@@ -572,7 +572,8 @@ static void disarm_sock_keys(struct mem_cgroup *memcg)
  * increase it.
  */
 static DEFINE_IDA(kmem_limited_groups);
-static int memcg_limited_groups_array_size;
+int memcg_limited_groups_array_size;
+
 /*
  * MIN_SIZE is different than 1, because we would like to avoid going through
  * the alloc/free process all the time. In a small machine, 4 kmem-limited
@@ -2772,6 +2773,8 @@ static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
        memcg_check_events(memcg, page);
 }
 
+static DEFINE_MUTEX(set_limit_mutex);
+
 #ifdef CONFIG_MEMCG_KMEM
 static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
 {
@@ -2792,6 +2795,27 @@ static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
        return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
 }
 
+#ifdef CONFIG_SLABINFO
+static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft,
+                                       struct seq_file *m)
+{
+       struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
+       struct memcg_cache_params *params;
+
+       if (!memcg_can_account_kmem(memcg))
+               return -EIO;
+
+       print_slabinfo_header(m);
+
+       mutex_lock(&memcg->slab_caches_mutex);
+       list_for_each_entry(params, &memcg->memcg_slab_caches, list)
+               cache_show(memcg_params_to_cache(params), m);
+       mutex_unlock(&memcg->slab_caches_mutex);
+
+       return 0;
+}
+#endif
+
 static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
 {
        struct res_counter *fail_res;
@@ -2988,7 +3012,8 @@ int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
        return 0;
 }
 
-int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s)
+int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
+                        struct kmem_cache *root_cache)
 {
        size_t size = sizeof(struct memcg_cache_params);
 
@@ -3002,8 +3027,10 @@ int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s)
        if (!s->memcg_params)
                return -ENOMEM;
 
-       if (memcg)
+       if (memcg) {
                s->memcg_params->memcg = memcg;
+               s->memcg_params->root_cache = root_cache;
+       }
        return 0;
 }
 
@@ -3078,7 +3105,27 @@ static void kmem_cache_destroy_work_func(struct work_struct *w)
 
        cachep = memcg_params_to_cache(p);
 
-       if (!atomic_read(&cachep->memcg_params->nr_pages))
+       /*
+        * If we get down to 0 after shrink, we could delete right away.
+        * However, memcg_release_pages() already puts us back in the workqueue
+        * in that case. If we proceed deleting, we'll get a dangling
+        * reference, and removing the object from the workqueue in that case
+        * is unnecessary complication. We are not a fast path.
+        *
+        * Note that this case is fundamentally different from racing with
+        * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
+        * kmem_cache_shrink, not only we would be reinserting a dead cache
+        * into the queue, but doing so from inside the worker racing to
+        * destroy it.
+        *
+        * So if we aren't down to zero, we'll just schedule a worker and try
+        * again
+        */
+       if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
+               kmem_cache_shrink(cachep);
+               if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
+                       return;
+       } else
                kmem_cache_destroy(cachep);
 }
 
@@ -3088,6 +3135,26 @@ void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
                return;
 
        /*
+        * There are many ways in which we can get here.
+        *
+        * We can get to a memory-pressure situation while the delayed work is
+        * still pending to run. The vmscan shrinkers can then release all
+        * cache memory and get us to destruction. If this is the case, we'll
+        * be executed twice, which is a bug (the second time will execute over
+        * bogus data). In this case, cancelling the work should be fine.
+        *
+        * But we can also get here from the worker itself, if
+        * kmem_cache_shrink is enough to shake all the remaining objects and
+        * get the page count to 0. In this case, we'll deadlock if we try to
+        * cancel the work (the worker runs with an internal lock held, which
+        * is the same lock we would hold for cancel_work_sync().)
+        *
+        * Since we can't possibly know who got us here, just refrain from
+        * running if there is already work pending
+        */
+       if (work_pending(&cachep->memcg_params->destroy))
+               return;
+       /*
         * We have to defer the actual destroying to a workqueue, because
         * we might currently be in a context that cannot sleep.
         */
@@ -3122,7 +3189,7 @@ static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
                return NULL;
 
        new = kmem_cache_create_memcg(memcg, name, s->object_size, s->align,
-                                     (s->flags & ~SLAB_PANIC), s->ctor);
+                                     (s->flags & ~SLAB_PANIC), s->ctor, s);
 
        if (new)
                new->allocflags |= __GFP_KMEMCG;
@@ -3162,7 +3229,6 @@ static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
        }
 
        mem_cgroup_get(memcg);
-       new_cachep->memcg_params->root_cache = cachep;
        atomic_set(&new_cachep->memcg_params->nr_pages , 0);
 
        cachep->memcg_params->memcg_caches[idx] = new_cachep;
@@ -3176,6 +3242,51 @@ out:
        return new_cachep;
 }
 
+void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
+{
+       struct kmem_cache *c;
+       int i;
+
+       if (!s->memcg_params)
+               return;
+       if (!s->memcg_params->is_root_cache)
+               return;
+
+       /*
+        * If the cache is being destroyed, we trust that there is no one else
+        * requesting objects from it. Even if there are, the sanity checks in
+        * kmem_cache_destroy should caught this ill-case.
+        *
+        * Still, we don't want anyone else freeing memcg_caches under our
+        * noses, which can happen if a new memcg comes to life. As usual,
+        * we'll take the set_limit_mutex to protect ourselves against this.
+        */
+       mutex_lock(&set_limit_mutex);
+       for (i = 0; i < memcg_limited_groups_array_size; i++) {
+               c = s->memcg_params->memcg_caches[i];
+               if (!c)
+                       continue;
+
+               /*
+                * We will now manually delete the caches, so to avoid races
+                * we need to cancel all pending destruction workers and
+                * proceed with destruction ourselves.
+                *
+                * kmem_cache_destroy() will call kmem_cache_shrink internally,
+                * and that could spawn the workers again: it is likely that
+                * the cache still have active pages until this very moment.
+                * This would lead us back to mem_cgroup_destroy_cache.
+                *
+                * But that will not execute at all if the "dead" flag is not
+                * set, so flip it down to guarantee we are in control.
+                */
+               c->memcg_params->dead = false;
+               cancel_work_sync(&c->memcg_params->destroy);
+               kmem_cache_destroy(c);
+       }
+       mutex_unlock(&set_limit_mutex);
+}
+
 struct create_work {
        struct mem_cgroup *memcg;
        struct kmem_cache *cachep;
@@ -3195,7 +3306,7 @@ static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
                cachep = memcg_params_to_cache(params);
                cachep->memcg_params->dead = true;
                INIT_WORK(&cachep->memcg_params->destroy,
-                         kmem_cache_destroy_work_func);
+                                 kmem_cache_destroy_work_func);
                schedule_work(&cachep->memcg_params->destroy);
        }
        mutex_unlock(&memcg->slab_caches_mutex);
@@ -4284,8 +4395,6 @@ void mem_cgroup_print_bad_page(struct page *page)
 }
 #endif
 
-static DEFINE_MUTEX(set_limit_mutex);
-
 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
                                unsigned long long val)
 {
@@ -5737,6 +5846,12 @@ static struct cftype mem_cgroup_files[] = {
                .trigger = mem_cgroup_reset,
                .read = mem_cgroup_read,
        },
+#ifdef CONFIG_SLABINFO
+       {
+               .name = "kmem.slabinfo",
+               .read_seq_string = mem_cgroup_slabinfo_read,
+       },
+#endif
 #endif
        { },    /* terminate */
 };
@@ -5975,7 +6090,6 @@ mem_cgroup_css_alloc(struct cgroup *cont)
                                                &per_cpu(memcg_stock, cpu);
                        INIT_WORK(&stock->work, drain_local_stock);
                }
-               hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
        } else {
                parent = mem_cgroup_from_cont(cont->parent);
                memcg->use_hierarchy = parent->use_hierarchy;
@@ -6641,6 +6755,19 @@ struct cgroup_subsys mem_cgroup_subsys = {
        .use_id = 1,
 };
 
+/*
+ * The rest of init is performed during ->css_alloc() for root css which
+ * happens before initcalls.  hotcpu_notifier() can't be done together as
+ * it would introduce circular locking by adding cgroup_lock -> cpu hotplug
+ * dependency.  Do it from a subsys_initcall().
+ */
+static int __init mem_cgroup_init(void)
+{
+       hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
+       return 0;
+}
+subsys_initcall(mem_cgroup_init);
+
 #ifdef CONFIG_MEMCG_SWAP
 static int __init enable_swap_account(char *s)
 {