Merge tag 'usb-3.15-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[sfrench/cifs-2.6.git] / mm / memcontrol.c
index 038b037f8d673f05414888ab81d4c5d0f30bef7e..29501f04056887297be694c315c7caf3adf666f5 100644 (file)
@@ -3094,6 +3094,29 @@ int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
        return 0;
 }
 
+char *memcg_create_cache_name(struct mem_cgroup *memcg,
+                             struct kmem_cache *root_cache)
+{
+       static char *buf = NULL;
+
+       /*
+        * We need a mutex here to protect the shared buffer. Since this is
+        * expected to be called only on cache creation, we can employ the
+        * slab_mutex for that purpose.
+        */
+       lockdep_assert_held(&slab_mutex);
+
+       if (!buf) {
+               buf = kmalloc(NAME_MAX + 1, GFP_KERNEL);
+               if (!buf)
+                       return NULL;
+       }
+
+       cgroup_name(memcg->css.cgroup, buf, NAME_MAX + 1);
+       return kasprintf(GFP_KERNEL, "%s(%d:%s)", root_cache->name,
+                        memcg_cache_id(memcg), buf);
+}
+
 int memcg_alloc_cache_params(struct mem_cgroup *memcg, struct kmem_cache *s,
                             struct kmem_cache *root_cache)
 {
@@ -3117,6 +3140,7 @@ int memcg_alloc_cache_params(struct mem_cgroup *memcg, struct kmem_cache *s,
                s->memcg_params->root_cache = root_cache;
                INIT_WORK(&s->memcg_params->destroy,
                                kmem_cache_destroy_work_func);
+               css_get(&memcg->css);
        } else
                s->memcg_params->is_root_cache = true;
 
@@ -3125,6 +3149,10 @@ int memcg_alloc_cache_params(struct mem_cgroup *memcg, struct kmem_cache *s,
 
 void memcg_free_cache_params(struct kmem_cache *s)
 {
+       if (!s->memcg_params)
+               return;
+       if (!s->memcg_params->is_root_cache)
+               css_put(&s->memcg_params->memcg->css);
        kfree(s->memcg_params);
 }
 
@@ -3147,9 +3175,6 @@ void memcg_register_cache(struct kmem_cache *s)
        memcg = s->memcg_params->memcg;
        id = memcg_cache_id(memcg);
 
-       css_get(&memcg->css);
-
-
        /*
         * Since readers won't lock (see cache_from_memcg_idx()), we need a
         * barrier here to ensure nobody will see the kmem_cache partially
@@ -3198,10 +3223,8 @@ void memcg_unregister_cache(struct kmem_cache *s)
         * after removing it from the memcg_slab_caches list, otherwise we can
         * fail to convert memcg_params_to_cache() while traversing the list.
         */
-       VM_BUG_ON(!root->memcg_params->memcg_caches[id]);
+       VM_BUG_ON(root->memcg_params->memcg_caches[id] != s);
        root->memcg_params->memcg_caches[id] = NULL;
-
-       css_put(&memcg->css);
 }
 
 /*
@@ -3298,55 +3321,10 @@ void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
        schedule_work(&cachep->memcg_params->destroy);
 }
 
-static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
-                                                 struct kmem_cache *s)
-{
-       struct kmem_cache *new = NULL;
-       static char *tmp_path = NULL, *tmp_name = NULL;
-       static DEFINE_MUTEX(mutex);     /* protects tmp_name */
-
-       BUG_ON(!memcg_can_account_kmem(memcg));
-
-       mutex_lock(&mutex);
-       /*
-        * kmem_cache_create_memcg duplicates the given name and
-        * cgroup_name for this name requires RCU context.
-        * This static temporary buffer is used to prevent from
-        * pointless shortliving allocation.
-        */
-       if (!tmp_path || !tmp_name) {
-               if (!tmp_path)
-                       tmp_path = kmalloc(PATH_MAX, GFP_KERNEL);
-               if (!tmp_name)
-                       tmp_name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
-               if (!tmp_path || !tmp_name)
-                       goto out;
-       }
-
-       cgroup_name(memcg->css.cgroup, tmp_name, NAME_MAX + 1);
-       snprintf(tmp_path, PATH_MAX, "%s(%d:%s)", s->name,
-                memcg_cache_id(memcg), tmp_name);
-
-       new = kmem_cache_create_memcg(memcg, tmp_path, s->object_size, s->align,
-                                     (s->flags & ~SLAB_PANIC), s->ctor, s);
-       if (new)
-               new->allocflags |= __GFP_KMEMCG;
-       else
-               new = s;
-out:
-       mutex_unlock(&mutex);
-       return new;
-}
-
-void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
+int __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;
+       int i, failed = 0;
 
        /*
         * If the cache is being destroyed, we trust that there is no one else
@@ -3380,16 +3358,14 @@ void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
                c->memcg_params->dead = false;
                cancel_work_sync(&c->memcg_params->destroy);
                kmem_cache_destroy(c);
+
+               if (cache_from_memcg_idx(s, i))
+                       failed++;
        }
        mutex_unlock(&activate_kmem_mutex);
+       return failed;
 }
 
-struct create_work {
-       struct mem_cgroup *memcg;
-       struct kmem_cache *cachep;
-       struct work_struct work;
-};
-
 static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
 {
        struct kmem_cache *cachep;
@@ -3407,13 +3383,20 @@ static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
        mutex_unlock(&memcg->slab_caches_mutex);
 }
 
+struct create_work {
+       struct mem_cgroup *memcg;
+       struct kmem_cache *cachep;
+       struct work_struct work;
+};
+
 static void memcg_create_cache_work_func(struct work_struct *w)
 {
-       struct create_work *cw;
+       struct create_work *cw = container_of(w, struct create_work, work);
+       struct mem_cgroup *memcg = cw->memcg;
+       struct kmem_cache *cachep = cw->cachep;
 
-       cw = container_of(w, struct create_work, work);
-       memcg_create_kmem_cache(cw->memcg, cw->cachep);
-       css_put(&cw->memcg->css);
+       kmem_cache_create_memcg(memcg, cachep);
+       css_put(&memcg->css);
        kfree(cw);
 }
 
@@ -3818,7 +3801,7 @@ out:
        return ret;
 }
 
-int mem_cgroup_newpage_charge(struct page *page,
+int mem_cgroup_charge_anon(struct page *page,
                              struct mm_struct *mm, gfp_t gfp_mask)
 {
        unsigned int nr_pages = 1;
@@ -3954,7 +3937,7 @@ void mem_cgroup_commit_charge_swapin(struct page *page,
                                          MEM_CGROUP_CHARGE_TYPE_ANON);
 }
 
-int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
+int mem_cgroup_charge_file(struct page *page, struct mm_struct *mm,
                                gfp_t gfp_mask)
 {
        enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;