These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / mm / slab_common.c
index 999bb34..3c6a86b 100644 (file)
@@ -37,8 +37,7 @@ struct kmem_cache *kmem_cache;
                SLAB_TRACE | SLAB_DESTROY_BY_RCU | SLAB_NOLEAKTRACE | \
                SLAB_FAILSLAB)
 
-#define SLAB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
-               SLAB_CACHE_DMA | SLAB_NOTRACK)
+#define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | SLAB_NOTRACK)
 
 /*
  * Merge control. If this is set then no merging of slab caches will occur.
@@ -105,6 +104,29 @@ static inline int kmem_cache_sanity_check(const char *name, size_t size)
 }
 #endif
 
+void __kmem_cache_free_bulk(struct kmem_cache *s, size_t nr, void **p)
+{
+       size_t i;
+
+       for (i = 0; i < nr; i++)
+               kmem_cache_free(s, p[i]);
+}
+
+int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t nr,
+                                                               void **p)
+{
+       size_t i;
+
+       for (i = 0; i < nr; i++) {
+               void *x = p[i] = kmem_cache_alloc(s, flags);
+               if (!x) {
+                       __kmem_cache_free_bulk(s, i, p);
+                       return 0;
+               }
+       }
+       return i;
+}
+
 #ifdef CONFIG_MEMCG_KMEM
 void slab_init_memcg_params(struct kmem_cache *s)
 {
@@ -294,10 +316,10 @@ unsigned long calculate_alignment(unsigned long flags,
        return ALIGN(align, sizeof(void *));
 }
 
-static struct kmem_cache *
-do_kmem_cache_create(const char *name, size_t object_size, size_t size,
-                    size_t align, unsigned long flags, void (*ctor)(void *),
-                    struct mem_cgroup *memcg, struct kmem_cache *root_cache)
+static struct kmem_cache *create_cache(const char *name,
+               size_t object_size, size_t size, size_t align,
+               unsigned long flags, void (*ctor)(void *),
+               struct mem_cgroup *memcg, struct kmem_cache *root_cache)
 {
        struct kmem_cache *s;
        int err;
@@ -362,7 +384,7 @@ struct kmem_cache *
 kmem_cache_create(const char *name, size_t size, size_t align,
                  unsigned long flags, void (*ctor)(void *))
 {
-       struct kmem_cache *s;
+       struct kmem_cache *s = NULL;
        const char *cache_name;
        int err;
 
@@ -374,7 +396,6 @@ kmem_cache_create(const char *name, size_t size, size_t align,
 
        err = kmem_cache_sanity_check(name, size);
        if (err) {
-               s = NULL;       /* suppress uninit var warning */
                goto out_unlock;
        }
 
@@ -396,9 +417,9 @@ kmem_cache_create(const char *name, size_t size, size_t align,
                goto out_unlock;
        }
 
-       s = do_kmem_cache_create(cache_name, size, size,
-                                calculate_alignment(flags, align, size),
-                                flags, ctor, NULL, NULL);
+       s = create_cache(cache_name, size, size,
+                        calculate_alignment(flags, align, size),
+                        flags, ctor, NULL, NULL);
        if (IS_ERR(s)) {
                err = PTR_ERR(s);
                kfree_const(cache_name);
@@ -426,29 +447,20 @@ out_unlock:
 }
 EXPORT_SYMBOL(kmem_cache_create);
 
-static int do_kmem_cache_shutdown(struct kmem_cache *s,
+static int shutdown_cache(struct kmem_cache *s,
                struct list_head *release, bool *need_rcu_barrier)
 {
-       if (__kmem_cache_shutdown(s) != 0) {
-               printk(KERN_ERR "kmem_cache_destroy %s: "
-                      "Slab cache still has objects\n", s->name);
-               dump_stack();
+       if (__kmem_cache_shutdown(s) != 0)
                return -EBUSY;
-       }
 
        if (s->flags & SLAB_DESTROY_BY_RCU)
                *need_rcu_barrier = true;
 
-#ifdef CONFIG_MEMCG_KMEM
-       if (!is_root_cache(s))
-               list_del(&s->memcg_params.list);
-#endif
        list_move(&s->list, release);
        return 0;
 }
 
-static void do_kmem_cache_release(struct list_head *release,
-                                 bool need_rcu_barrier)
+static void release_caches(struct list_head *release, bool need_rcu_barrier)
 {
        struct kmem_cache *s, *s2;
 
@@ -478,7 +490,7 @@ void memcg_create_kmem_cache(struct mem_cgroup *memcg,
                             struct kmem_cache *root_cache)
 {
        static char memcg_name_buf[NAME_MAX + 1]; /* protected by slab_mutex */
-       struct cgroup_subsys_state *css = mem_cgroup_css(memcg);
+       struct cgroup_subsys_state *css = &memcg->css;
        struct memcg_cache_array *arr;
        struct kmem_cache *s = NULL;
        char *cache_name;
@@ -514,10 +526,10 @@ void memcg_create_kmem_cache(struct mem_cgroup *memcg,
        if (!cache_name)
                goto out_unlock;
 
-       s = do_kmem_cache_create(cache_name, root_cache->object_size,
-                                root_cache->size, root_cache->align,
-                                root_cache->flags, root_cache->ctor,
-                                memcg, root_cache);
+       s = create_cache(cache_name, root_cache->object_size,
+                        root_cache->size, root_cache->align,
+                        root_cache->flags, root_cache->ctor,
+                        memcg, root_cache);
        /*
         * If we could not create a memcg cache, do not complain, because
         * that's not critical at all as we can always proceed with the root
@@ -576,6 +588,18 @@ void memcg_deactivate_kmem_caches(struct mem_cgroup *memcg)
        put_online_cpus();
 }
 
+static int __shutdown_memcg_cache(struct kmem_cache *s,
+               struct list_head *release, bool *need_rcu_barrier)
+{
+       BUG_ON(is_root_cache(s));
+
+       if (shutdown_cache(s, release, need_rcu_barrier))
+               return -EBUSY;
+
+       list_del(&s->memcg_params.list);
+       return 0;
+}
+
 void memcg_destroy_kmem_caches(struct mem_cgroup *memcg)
 {
        LIST_HEAD(release);
@@ -593,14 +617,76 @@ void memcg_destroy_kmem_caches(struct mem_cgroup *memcg)
                 * The cgroup is about to be freed and therefore has no charges
                 * left. Hence, all its caches must be empty by now.
                 */
-               BUG_ON(do_kmem_cache_shutdown(s, &release, &need_rcu_barrier));
+               BUG_ON(__shutdown_memcg_cache(s, &release, &need_rcu_barrier));
        }
        mutex_unlock(&slab_mutex);
 
        put_online_mems();
        put_online_cpus();
 
-       do_kmem_cache_release(&release, need_rcu_barrier);
+       release_caches(&release, need_rcu_barrier);
+}
+
+static int shutdown_memcg_caches(struct kmem_cache *s,
+               struct list_head *release, bool *need_rcu_barrier)
+{
+       struct memcg_cache_array *arr;
+       struct kmem_cache *c, *c2;
+       LIST_HEAD(busy);
+       int i;
+
+       BUG_ON(!is_root_cache(s));
+
+       /*
+        * First, shutdown active caches, i.e. caches that belong to online
+        * memory cgroups.
+        */
+       arr = rcu_dereference_protected(s->memcg_params.memcg_caches,
+                                       lockdep_is_held(&slab_mutex));
+       for_each_memcg_cache_index(i) {
+               c = arr->entries[i];
+               if (!c)
+                       continue;
+               if (__shutdown_memcg_cache(c, release, need_rcu_barrier))
+                       /*
+                        * The cache still has objects. Move it to a temporary
+                        * list so as not to try to destroy it for a second
+                        * time while iterating over inactive caches below.
+                        */
+                       list_move(&c->memcg_params.list, &busy);
+               else
+                       /*
+                        * The cache is empty and will be destroyed soon. Clear
+                        * the pointer to it in the memcg_caches array so that
+                        * it will never be accessed even if the root cache
+                        * stays alive.
+                        */
+                       arr->entries[i] = NULL;
+       }
+
+       /*
+        * Second, shutdown all caches left from memory cgroups that are now
+        * offline.
+        */
+       list_for_each_entry_safe(c, c2, &s->memcg_params.list,
+                                memcg_params.list)
+               __shutdown_memcg_cache(c, release, need_rcu_barrier);
+
+       list_splice(&busy, &s->memcg_params.list);
+
+       /*
+        * A cache being destroyed must be empty. In particular, this means
+        * that all per memcg caches attached to it must be empty too.
+        */
+       if (!list_empty(&s->memcg_params.list))
+               return -EBUSY;
+       return 0;
+}
+#else
+static inline int shutdown_memcg_caches(struct kmem_cache *s,
+               struct list_head *release, bool *need_rcu_barrier)
+{
+       return 0;
 }
 #endif /* CONFIG_MEMCG_KMEM */
 
@@ -613,12 +699,12 @@ void slab_kmem_cache_release(struct kmem_cache *s)
 
 void kmem_cache_destroy(struct kmem_cache *s)
 {
-       struct kmem_cache *c, *c2;
        LIST_HEAD(release);
        bool need_rcu_barrier = false;
-       bool busy = false;
+       int err;
 
-       BUG_ON(!is_root_cache(s));
+       if (unlikely(!s))
+               return;
 
        get_online_cpus();
        get_online_mems();
@@ -629,21 +715,22 @@ void kmem_cache_destroy(struct kmem_cache *s)
        if (s->refcount)
                goto out_unlock;
 
-       for_each_memcg_cache_safe(c, c2, s) {
-               if (do_kmem_cache_shutdown(c, &release, &need_rcu_barrier))
-                       busy = true;
-       }
-
-       if (!busy)
-               do_kmem_cache_shutdown(s, &release, &need_rcu_barrier);
+       err = shutdown_memcg_caches(s, &release, &need_rcu_barrier);
+       if (!err)
+               err = shutdown_cache(s, &release, &need_rcu_barrier);
 
+       if (err) {
+               pr_err("kmem_cache_destroy %s: "
+                      "Slab cache still has objects\n", s->name);
+               dump_stack();
+       }
 out_unlock:
        mutex_unlock(&slab_mutex);
 
        put_online_mems();
        put_online_cpus();
 
-       do_kmem_cache_release(&release, need_rcu_barrier);
+       release_caches(&release, need_rcu_barrier);
 }
 EXPORT_SYMBOL(kmem_cache_destroy);
 
@@ -667,7 +754,7 @@ int kmem_cache_shrink(struct kmem_cache *cachep)
 }
 EXPORT_SYMBOL(kmem_cache_shrink);
 
-int slab_is_available(void)
+bool slab_is_available(void)
 {
        return slab_state >= UP;
 }
@@ -784,25 +871,45 @@ struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
 }
 
 /*
- * Create the kmalloc array. Some of the regular kmalloc arrays
- * may already have been created because they were needed to
- * enable allocations for slab creation.
+ * kmalloc_info[] is to make slub_debug=,kmalloc-xx option work at boot time.
+ * kmalloc_index() supports up to 2^26=64MB, so the final entry of the table is
+ * kmalloc-67108864.
  */
-void __init create_kmalloc_caches(unsigned long flags)
+static struct {
+       const char *name;
+       unsigned long size;
+} const kmalloc_info[] __initconst = {
+       {NULL,                      0},         {"kmalloc-96",             96},
+       {"kmalloc-192",           192},         {"kmalloc-8",               8},
+       {"kmalloc-16",             16},         {"kmalloc-32",             32},
+       {"kmalloc-64",             64},         {"kmalloc-128",           128},
+       {"kmalloc-256",           256},         {"kmalloc-512",           512},
+       {"kmalloc-1024",         1024},         {"kmalloc-2048",         2048},
+       {"kmalloc-4096",         4096},         {"kmalloc-8192",         8192},
+       {"kmalloc-16384",       16384},         {"kmalloc-32768",       32768},
+       {"kmalloc-65536",       65536},         {"kmalloc-131072",     131072},
+       {"kmalloc-262144",     262144},         {"kmalloc-524288",     524288},
+       {"kmalloc-1048576",   1048576},         {"kmalloc-2097152",   2097152},
+       {"kmalloc-4194304",   4194304},         {"kmalloc-8388608",   8388608},
+       {"kmalloc-16777216", 16777216},         {"kmalloc-33554432", 33554432},
+       {"kmalloc-67108864", 67108864}
+};
+
+/*
+ * Patch up the size_index table if we have strange large alignment
+ * requirements for the kmalloc array. This is only the case for
+ * MIPS it seems. The standard arches will not generate any code here.
+ *
+ * Largest permitted alignment is 256 bytes due to the way we
+ * handle the index determination for the smaller caches.
+ *
+ * Make sure that nothing crazy happens if someone starts tinkering
+ * around with ARCH_KMALLOC_MINALIGN
+ */
+void __init setup_kmalloc_cache_index_table(void)
 {
        int i;
 
-       /*
-        * Patch up the size_index table if we have strange large alignment
-        * requirements for the kmalloc array. This is only the case for
-        * MIPS it seems. The standard arches will not generate any code here.
-        *
-        * Largest permitted alignment is 256 bytes due to the way we
-        * handle the index determination for the smaller caches.
-        *
-        * Make sure that nothing crazy happens if someone starts tinkering
-        * around with ARCH_KMALLOC_MINALIGN
-        */
        BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
                (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
 
@@ -833,11 +940,26 @@ void __init create_kmalloc_caches(unsigned long flags)
                for (i = 128 + 8; i <= 192; i += 8)
                        size_index[size_index_elem(i)] = 8;
        }
+}
+
+static void __init new_kmalloc_cache(int idx, unsigned long flags)
+{
+       kmalloc_caches[idx] = create_kmalloc_cache(kmalloc_info[idx].name,
+                                       kmalloc_info[idx].size, flags);
+}
+
+/*
+ * Create the kmalloc array. Some of the regular kmalloc arrays
+ * may already have been created because they were needed to
+ * enable allocations for slab creation.
+ */
+void __init create_kmalloc_caches(unsigned long flags)
+{
+       int i;
+
        for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
-               if (!kmalloc_caches[i]) {
-                       kmalloc_caches[i] = create_kmalloc_cache(NULL,
-                                                       1 << i, flags);
-               }
+               if (!kmalloc_caches[i])
+                       new_kmalloc_cache(i, flags);
 
                /*
                 * Caches that are not of the two-to-the-power-of size.
@@ -845,27 +967,14 @@ void __init create_kmalloc_caches(unsigned long flags)
                 * earlier power of two caches
                 */
                if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6)
-                       kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags);
-
+                       new_kmalloc_cache(1, flags);
                if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7)
-                       kmalloc_caches[2] = create_kmalloc_cache(NULL, 192, flags);
+                       new_kmalloc_cache(2, flags);
        }
 
        /* Kmalloc array is now usable */
        slab_state = UP;
 
-       for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
-               struct kmem_cache *s = kmalloc_caches[i];
-               char *n;
-
-               if (s) {
-                       n = kasprintf(GFP_NOWAIT, "kmalloc-%d", kmalloc_size(i));
-
-                       BUG_ON(!n);
-                       s->name = n;
-               }
-       }
-
 #ifdef CONFIG_ZONE_DMA
        for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
                struct kmem_cache *s = kmalloc_caches[i];