]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blobdiff - mm/slab_common.c
sched/headers: Prepare for new header dependencies before moving code to <linux/sched...
[mirror_ubuntu-artful-kernel.git] / mm / slab_common.c
index c3885032dbce5038df4896e90048a74228ba13ee..09d0e849b07f47d82f5d9a5cda4862517d297cb2 100644 (file)
@@ -138,6 +138,9 @@ int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t nr,
 }
 
 #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
+
+LIST_HEAD(slab_root_caches);
+
 void slab_init_memcg_params(struct kmem_cache *s)
 {
        s->memcg_params.root_cache = NULL;
@@ -154,6 +157,7 @@ static int init_memcg_params(struct kmem_cache *s,
                s->memcg_params.root_cache = root_cache;
                s->memcg_params.memcg = memcg;
                INIT_LIST_HEAD(&s->memcg_params.children_node);
+               INIT_LIST_HEAD(&s->memcg_params.kmem_caches_node);
                return 0;
        }
 
@@ -182,9 +186,6 @@ static int update_memcg_params(struct kmem_cache *s, int new_array_size)
 {
        struct memcg_cache_array *old, *new;
 
-       if (!is_root_cache(s))
-               return 0;
-
        new = kzalloc(sizeof(struct memcg_cache_array) +
                      new_array_size * sizeof(void *), GFP_KERNEL);
        if (!new)
@@ -208,7 +209,7 @@ int memcg_update_all_caches(int num_memcgs)
        int ret = 0;
 
        mutex_lock(&slab_mutex);
-       list_for_each_entry(s, &slab_caches, list) {
+       list_for_each_entry(s, &slab_root_caches, root_caches_node) {
                ret = update_memcg_params(s, num_memcgs);
                /*
                 * Instead of freeing the memory, we'll just leave the caches
@@ -221,9 +222,26 @@ int memcg_update_all_caches(int num_memcgs)
        return ret;
 }
 
-static void unlink_memcg_cache(struct kmem_cache *s)
+void memcg_link_cache(struct kmem_cache *s)
 {
-       list_del(&s->memcg_params.children_node);
+       if (is_root_cache(s)) {
+               list_add(&s->root_caches_node, &slab_root_caches);
+       } else {
+               list_add(&s->memcg_params.children_node,
+                        &s->memcg_params.root_cache->memcg_params.children);
+               list_add(&s->memcg_params.kmem_caches_node,
+                        &s->memcg_params.memcg->kmem_caches);
+       }
+}
+
+static void memcg_unlink_cache(struct kmem_cache *s)
+{
+       if (is_root_cache(s)) {
+               list_del(&s->root_caches_node);
+       } else {
+               list_del(&s->memcg_params.children_node);
+               list_del(&s->memcg_params.kmem_caches_node);
+       }
 }
 #else
 static inline int init_memcg_params(struct kmem_cache *s,
@@ -236,7 +254,7 @@ static inline void destroy_memcg_params(struct kmem_cache *s)
 {
 }
 
-static inline void unlink_memcg_cache(struct kmem_cache *s)
+static inline void memcg_unlink_cache(struct kmem_cache *s)
 {
 }
 #endif /* CONFIG_MEMCG && !CONFIG_SLOB */
@@ -283,7 +301,7 @@ struct kmem_cache *find_mergeable(size_t size, size_t align,
        if (flags & SLAB_NEVER_MERGE)
                return NULL;
 
-       list_for_each_entry_reverse(s, &slab_caches, list) {
+       list_for_each_entry_reverse(s, &slab_root_caches, root_caches_node) {
                if (slab_unmergeable(s))
                        continue;
 
@@ -367,6 +385,7 @@ static struct kmem_cache *create_cache(const char *name,
 
        s->refcount = 1;
        list_add(&s->list, &slab_caches);
+       memcg_link_cache(s);
 out:
        if (err)
                return ERR_PTR(err);
@@ -509,12 +528,14 @@ static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
 
 static int shutdown_cache(struct kmem_cache *s)
 {
+       /* free asan quarantined objects */
+       kasan_cache_shutdown(s);
+
        if (__kmem_cache_shutdown(s) != 0)
                return -EBUSY;
 
+       memcg_unlink_cache(s);
        list_del(&s->list);
-       if (!is_root_cache(s))
-               unlink_memcg_cache(s);
 
        if (s->flags & SLAB_DESTROY_BY_RCU) {
                list_add_tail(&s->list, &slab_caches_to_rcu_destroy);
@@ -594,9 +615,6 @@ void memcg_create_kmem_cache(struct mem_cgroup *memcg,
                goto out_unlock;
        }
 
-       list_add(&s->memcg_params.children_node,
-                &root_cache->memcg_params.children);
-
        /*
         * Since readers won't lock (see cache_from_memcg_idx()), we need a
         * barrier here to ensure nobody will see the kmem_cache partially
@@ -612,6 +630,66 @@ out_unlock:
        put_online_cpus();
 }
 
+static void kmemcg_deactivate_workfn(struct work_struct *work)
+{
+       struct kmem_cache *s = container_of(work, struct kmem_cache,
+                                           memcg_params.deact_work);
+
+       get_online_cpus();
+       get_online_mems();
+
+       mutex_lock(&slab_mutex);
+
+       s->memcg_params.deact_fn(s);
+
+       mutex_unlock(&slab_mutex);
+
+       put_online_mems();
+       put_online_cpus();
+
+       /* done, put the ref from slab_deactivate_memcg_cache_rcu_sched() */
+       css_put(&s->memcg_params.memcg->css);
+}
+
+static void kmemcg_deactivate_rcufn(struct rcu_head *head)
+{
+       struct kmem_cache *s = container_of(head, struct kmem_cache,
+                                           memcg_params.deact_rcu_head);
+
+       /*
+        * We need to grab blocking locks.  Bounce to ->deact_work.  The
+        * work item shares the space with the RCU head and can't be
+        * initialized eariler.
+        */
+       INIT_WORK(&s->memcg_params.deact_work, kmemcg_deactivate_workfn);
+       queue_work(memcg_kmem_cache_wq, &s->memcg_params.deact_work);
+}
+
+/**
+ * slab_deactivate_memcg_cache_rcu_sched - schedule deactivation after a
+ *                                        sched RCU grace period
+ * @s: target kmem_cache
+ * @deact_fn: deactivation function to call
+ *
+ * Schedule @deact_fn to be invoked with online cpus, mems and slab_mutex
+ * held after a sched RCU grace period.  The slab is guaranteed to stay
+ * alive until @deact_fn is finished.  This is to be used from
+ * __kmemcg_cache_deactivate().
+ */
+void slab_deactivate_memcg_cache_rcu_sched(struct kmem_cache *s,
+                                          void (*deact_fn)(struct kmem_cache *))
+{
+       if (WARN_ON_ONCE(is_root_cache(s)) ||
+           WARN_ON_ONCE(s->memcg_params.deact_fn))
+               return;
+
+       /* pin memcg so that @s doesn't get destroyed in the middle */
+       css_get(&s->memcg_params.memcg->css);
+
+       s->memcg_params.deact_fn = deact_fn;
+       call_rcu_sched(&s->memcg_params.deact_rcu_head, kmemcg_deactivate_rcufn);
+}
+
 void memcg_deactivate_kmem_caches(struct mem_cgroup *memcg)
 {
        int idx;
@@ -624,17 +702,14 @@ void memcg_deactivate_kmem_caches(struct mem_cgroup *memcg)
        get_online_mems();
 
        mutex_lock(&slab_mutex);
-       list_for_each_entry(s, &slab_caches, list) {
-               if (!is_root_cache(s))
-                       continue;
-
+       list_for_each_entry(s, &slab_root_caches, root_caches_node) {
                arr = rcu_dereference_protected(s->memcg_params.memcg_caches,
                                                lockdep_is_held(&slab_mutex));
                c = arr->entries[idx];
                if (!c)
                        continue;
 
-               __kmem_cache_shrink(c, true);
+               __kmemcg_cache_deactivate(c);
                arr->entries[idx] = NULL;
        }
        mutex_unlock(&slab_mutex);
@@ -651,9 +726,8 @@ void memcg_destroy_kmem_caches(struct mem_cgroup *memcg)
        get_online_mems();
 
        mutex_lock(&slab_mutex);
-       list_for_each_entry_safe(s, s2, &slab_caches, list) {
-               if (is_root_cache(s) || s->memcg_params.memcg != memcg)
-                       continue;
+       list_for_each_entry_safe(s, s2, &memcg->kmem_caches,
+                                memcg_params.kmem_caches_node) {
                /*
                 * The cgroup is about to be freed and therefore has no charges
                 * left. Hence, all its caches must be empty by now.
@@ -745,7 +819,6 @@ void kmem_cache_destroy(struct kmem_cache *s)
        get_online_cpus();
        get_online_mems();
 
-       kasan_cache_destroy(s);
        mutex_lock(&slab_mutex);
 
        s->refcount--;
@@ -783,7 +856,7 @@ int kmem_cache_shrink(struct kmem_cache *cachep)
        get_online_cpus();
        get_online_mems();
        kasan_cache_shrink(cachep);
-       ret = __kmem_cache_shrink(cachep, false);
+       ret = __kmem_cache_shrink(cachep);
        put_online_mems();
        put_online_cpus();
        return ret;
@@ -827,6 +900,7 @@ struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size,
 
        create_boot_cache(s, name, size, flags);
        list_add(&s->list, &slab_caches);
+       memcg_link_cache(s);
        s->refcount = 1;
        return s;
 }
@@ -1134,12 +1208,12 @@ static void print_slabinfo_header(struct seq_file *m)
 void *slab_start(struct seq_file *m, loff_t *pos)
 {
        mutex_lock(&slab_mutex);
-       return seq_list_start(&slab_caches, *pos);
+       return seq_list_start(&slab_root_caches, *pos);
 }
 
 void *slab_next(struct seq_file *m, void *p, loff_t *pos)
 {
-       return seq_list_next(p, &slab_caches, pos);
+       return seq_list_next(p, &slab_root_caches, pos);
 }
 
 void slab_stop(struct seq_file *m, void *p)
@@ -1191,25 +1265,44 @@ static void cache_show(struct kmem_cache *s, struct seq_file *m)
 
 static int slab_show(struct seq_file *m, void *p)
 {
-       struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
+       struct kmem_cache *s = list_entry(p, struct kmem_cache, root_caches_node);
 
-       if (p == slab_caches.next)
+       if (p == slab_root_caches.next)
                print_slabinfo_header(m);
-       if (is_root_cache(s))
-               cache_show(s, m);
+       cache_show(s, m);
        return 0;
 }
 
 #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB)
+void *memcg_slab_start(struct seq_file *m, loff_t *pos)
+{
+       struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
+
+       mutex_lock(&slab_mutex);
+       return seq_list_start(&memcg->kmem_caches, *pos);
+}
+
+void *memcg_slab_next(struct seq_file *m, void *p, loff_t *pos)
+{
+       struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
+
+       return seq_list_next(p, &memcg->kmem_caches, pos);
+}
+
+void memcg_slab_stop(struct seq_file *m, void *p)
+{
+       mutex_unlock(&slab_mutex);
+}
+
 int memcg_slab_show(struct seq_file *m, void *p)
 {
-       struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
+       struct kmem_cache *s = list_entry(p, struct kmem_cache,
+                                         memcg_params.kmem_caches_node);
        struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m));
 
-       if (p == slab_caches.next)
+       if (p == memcg->kmem_caches.next)
                print_slabinfo_header(m);
-       if (!is_root_cache(s) && s->memcg_params.memcg == memcg)
-               cache_show(s, m);
+       cache_show(s, m);
        return 0;
 }
 #endif