mm: memcg/slab: save obj_cgroup for non-root slab objects
[linux-2.6-microblaze.git] / mm / slab.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef MM_SLAB_H
3 #define MM_SLAB_H
4 /*
5  * Internal slab definitions
6  */
7
8 #ifdef CONFIG_SLOB
9 /*
10  * Common fields provided in kmem_cache by all slab allocators
11  * This struct is either used directly by the allocator (SLOB)
12  * or the allocator must include definitions for all fields
13  * provided in kmem_cache_common in their definition of kmem_cache.
14  *
15  * Once we can do anonymous structs (C11 standard) we could put a
16  * anonymous struct definition in these allocators so that the
17  * separate allocations in the kmem_cache structure of SLAB and
18  * SLUB is no longer needed.
19  */
20 struct kmem_cache {
21         unsigned int object_size;/* The original size of the object */
22         unsigned int size;      /* The aligned/padded/added on size  */
23         unsigned int align;     /* Alignment as calculated */
24         slab_flags_t flags;     /* Active flags on the slab */
25         unsigned int useroffset;/* Usercopy region offset */
26         unsigned int usersize;  /* Usercopy region size */
27         const char *name;       /* Slab name for sysfs */
28         int refcount;           /* Use counter */
29         void (*ctor)(void *);   /* Called on object slot creation */
30         struct list_head list;  /* List of all slab caches on the system */
31 };
32
33 #else /* !CONFIG_SLOB */
34
35 struct memcg_cache_array {
36         struct rcu_head rcu;
37         struct kmem_cache *entries[0];
38 };
39
40 /*
41  * This is the main placeholder for memcg-related information in kmem caches.
42  * Both the root cache and the child caches will have it. For the root cache,
43  * this will hold a dynamically allocated array large enough to hold
44  * information about the currently limited memcgs in the system. To allow the
45  * array to be accessed without taking any locks, on relocation we free the old
46  * version only after a grace period.
47  *
48  * Root and child caches hold different metadata.
49  *
50  * @root_cache: Common to root and child caches.  NULL for root, pointer to
51  *              the root cache for children.
52  *
53  * The following fields are specific to root caches.
54  *
55  * @memcg_caches: kmemcg ID indexed table of child caches.  This table is
56  *              used to index child cachces during allocation and cleared
57  *              early during shutdown.
58  *
59  * @root_caches_node: List node for slab_root_caches list.
60  *
61  * @children:   List of all child caches.  While the child caches are also
62  *              reachable through @memcg_caches, a child cache remains on
63  *              this list until it is actually destroyed.
64  *
65  * The following fields are specific to child caches.
66  *
67  * @memcg:      Pointer to the memcg this cache belongs to.
68  *
69  * @children_node: List node for @root_cache->children list.
70  *
71  * @kmem_caches_node: List node for @memcg->kmem_caches list.
72  */
73 struct memcg_cache_params {
74         struct kmem_cache *root_cache;
75         union {
76                 struct {
77                         struct memcg_cache_array __rcu *memcg_caches;
78                         struct list_head __root_caches_node;
79                         struct list_head children;
80                         bool dying;
81                 };
82                 struct {
83                         struct mem_cgroup *memcg;
84                         struct list_head children_node;
85                         struct list_head kmem_caches_node;
86                         struct percpu_ref refcnt;
87
88                         void (*work_fn)(struct kmem_cache *);
89                         union {
90                                 struct rcu_head rcu_head;
91                                 struct work_struct work;
92                         };
93                 };
94         };
95 };
96 #endif /* CONFIG_SLOB */
97
98 #ifdef CONFIG_SLAB
99 #include <linux/slab_def.h>
100 #endif
101
102 #ifdef CONFIG_SLUB
103 #include <linux/slub_def.h>
104 #endif
105
106 #include <linux/memcontrol.h>
107 #include <linux/fault-inject.h>
108 #include <linux/kasan.h>
109 #include <linux/kmemleak.h>
110 #include <linux/random.h>
111 #include <linux/sched/mm.h>
112 #include <linux/kmemleak.h>
113
114 /*
115  * State of the slab allocator.
116  *
117  * This is used to describe the states of the allocator during bootup.
118  * Allocators use this to gradually bootstrap themselves. Most allocators
119  * have the problem that the structures used for managing slab caches are
120  * allocated from slab caches themselves.
121  */
122 enum slab_state {
123         DOWN,                   /* No slab functionality yet */
124         PARTIAL,                /* SLUB: kmem_cache_node available */
125         PARTIAL_NODE,           /* SLAB: kmalloc size for node struct available */
126         UP,                     /* Slab caches usable but not all extras yet */
127         FULL                    /* Everything is working */
128 };
129
130 extern enum slab_state slab_state;
131
132 /* The slab cache mutex protects the management structures during changes */
133 extern struct mutex slab_mutex;
134
135 /* The list of all slab caches on the system */
136 extern struct list_head slab_caches;
137
138 /* The slab cache that manages slab cache information */
139 extern struct kmem_cache *kmem_cache;
140
141 /* A table of kmalloc cache names and sizes */
142 extern const struct kmalloc_info_struct {
143         const char *name[NR_KMALLOC_TYPES];
144         unsigned int size;
145 } kmalloc_info[];
146
147 #ifndef CONFIG_SLOB
148 /* Kmalloc array related functions */
149 void setup_kmalloc_cache_index_table(void);
150 void create_kmalloc_caches(slab_flags_t);
151
152 /* Find the kmalloc slab corresponding for a certain size */
153 struct kmem_cache *kmalloc_slab(size_t, gfp_t);
154 #endif
155
156 gfp_t kmalloc_fix_flags(gfp_t flags);
157
158 /* Functions provided by the slab allocators */
159 int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags);
160
161 struct kmem_cache *create_kmalloc_cache(const char *name, unsigned int size,
162                         slab_flags_t flags, unsigned int useroffset,
163                         unsigned int usersize);
164 extern void create_boot_cache(struct kmem_cache *, const char *name,
165                         unsigned int size, slab_flags_t flags,
166                         unsigned int useroffset, unsigned int usersize);
167
168 int slab_unmergeable(struct kmem_cache *s);
169 struct kmem_cache *find_mergeable(unsigned size, unsigned align,
170                 slab_flags_t flags, const char *name, void (*ctor)(void *));
171 #ifndef CONFIG_SLOB
172 struct kmem_cache *
173 __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
174                    slab_flags_t flags, void (*ctor)(void *));
175
176 slab_flags_t kmem_cache_flags(unsigned int object_size,
177         slab_flags_t flags, const char *name,
178         void (*ctor)(void *));
179 #else
180 static inline struct kmem_cache *
181 __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
182                    slab_flags_t flags, void (*ctor)(void *))
183 { return NULL; }
184
185 static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
186         slab_flags_t flags, const char *name,
187         void (*ctor)(void *))
188 {
189         return flags;
190 }
191 #endif
192
193
194 /* Legal flag mask for kmem_cache_create(), for various configurations */
195 #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
196                          SLAB_CACHE_DMA32 | SLAB_PANIC | \
197                          SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS )
198
199 #if defined(CONFIG_DEBUG_SLAB)
200 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
201 #elif defined(CONFIG_SLUB_DEBUG)
202 #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
203                           SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
204 #else
205 #define SLAB_DEBUG_FLAGS (0)
206 #endif
207
208 #if defined(CONFIG_SLAB)
209 #define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
210                           SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
211                           SLAB_ACCOUNT)
212 #elif defined(CONFIG_SLUB)
213 #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
214                           SLAB_TEMPORARY | SLAB_ACCOUNT)
215 #else
216 #define SLAB_CACHE_FLAGS (0)
217 #endif
218
219 /* Common flags available with current configuration */
220 #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
221
222 /* Common flags permitted for kmem_cache_create */
223 #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
224                               SLAB_RED_ZONE | \
225                               SLAB_POISON | \
226                               SLAB_STORE_USER | \
227                               SLAB_TRACE | \
228                               SLAB_CONSISTENCY_CHECKS | \
229                               SLAB_MEM_SPREAD | \
230                               SLAB_NOLEAKTRACE | \
231                               SLAB_RECLAIM_ACCOUNT | \
232                               SLAB_TEMPORARY | \
233                               SLAB_ACCOUNT)
234
235 bool __kmem_cache_empty(struct kmem_cache *);
236 int __kmem_cache_shutdown(struct kmem_cache *);
237 void __kmem_cache_release(struct kmem_cache *);
238 int __kmem_cache_shrink(struct kmem_cache *);
239 void __kmemcg_cache_deactivate(struct kmem_cache *s);
240 void __kmemcg_cache_deactivate_after_rcu(struct kmem_cache *s);
241 void slab_kmem_cache_release(struct kmem_cache *);
242 void kmem_cache_shrink_all(struct kmem_cache *s);
243
244 struct seq_file;
245 struct file;
246
247 struct slabinfo {
248         unsigned long active_objs;
249         unsigned long num_objs;
250         unsigned long active_slabs;
251         unsigned long num_slabs;
252         unsigned long shared_avail;
253         unsigned int limit;
254         unsigned int batchcount;
255         unsigned int shared;
256         unsigned int objects_per_slab;
257         unsigned int cache_order;
258 };
259
260 void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
261 void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
262 ssize_t slabinfo_write(struct file *file, const char __user *buffer,
263                        size_t count, loff_t *ppos);
264
265 /*
266  * Generic implementation of bulk operations
267  * These are useful for situations in which the allocator cannot
268  * perform optimizations. In that case segments of the object listed
269  * may be allocated or freed using these operations.
270  */
271 void __kmem_cache_free_bulk(struct kmem_cache *, size_t, void **);
272 int __kmem_cache_alloc_bulk(struct kmem_cache *, gfp_t, size_t, void **);
273
274 static inline int cache_vmstat_idx(struct kmem_cache *s)
275 {
276         return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
277                 NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
278 }
279
280 #ifdef CONFIG_SLUB_DEBUG
281 #ifdef CONFIG_SLUB_DEBUG_ON
282 DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
283 #else
284 DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
285 #endif
286 extern void print_tracking(struct kmem_cache *s, void *object);
287 #else
288 static inline void print_tracking(struct kmem_cache *s, void *object)
289 {
290 }
291 #endif
292
293 /*
294  * Returns true if any of the specified slub_debug flags is enabled for the
295  * cache. Use only for flags parsed by setup_slub_debug() as it also enables
296  * the static key.
297  */
298 static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
299 {
300 #ifdef CONFIG_SLUB_DEBUG
301         VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
302         if (static_branch_unlikely(&slub_debug_enabled))
303                 return s->flags & flags;
304 #endif
305         return false;
306 }
307
308 #ifdef CONFIG_MEMCG_KMEM
309
310 /* List of all root caches. */
311 extern struct list_head         slab_root_caches;
312 #define root_caches_node        memcg_params.__root_caches_node
313
314 /*
315  * Iterate over all memcg caches of the given root cache. The caller must hold
316  * slab_mutex.
317  */
318 #define for_each_memcg_cache(iter, root) \
319         list_for_each_entry(iter, &(root)->memcg_params.children, \
320                             memcg_params.children_node)
321
322 static inline bool is_root_cache(struct kmem_cache *s)
323 {
324         return !s->memcg_params.root_cache;
325 }
326
327 static inline bool slab_equal_or_root(struct kmem_cache *s,
328                                       struct kmem_cache *p)
329 {
330         return p == s || p == s->memcg_params.root_cache;
331 }
332
333 /*
334  * We use suffixes to the name in memcg because we can't have caches
335  * created in the system with the same name. But when we print them
336  * locally, better refer to them with the base name
337  */
338 static inline const char *cache_name(struct kmem_cache *s)
339 {
340         if (!is_root_cache(s))
341                 s = s->memcg_params.root_cache;
342         return s->name;
343 }
344
345 static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s)
346 {
347         if (is_root_cache(s))
348                 return s;
349         return s->memcg_params.root_cache;
350 }
351
352 static inline struct obj_cgroup **page_obj_cgroups(struct page *page)
353 {
354         /*
355          * page->mem_cgroup and page->obj_cgroups are sharing the same
356          * space. To distinguish between them in case we don't know for sure
357          * that the page is a slab page (e.g. page_cgroup_ino()), let's
358          * always set the lowest bit of obj_cgroups.
359          */
360         return (struct obj_cgroup **)
361                 ((unsigned long)page->obj_cgroups & ~0x1UL);
362 }
363
364 /*
365  * Expects a pointer to a slab page. Please note, that PageSlab() check
366  * isn't sufficient, as it returns true also for tail compound slab pages,
367  * which do not have slab_cache pointer set.
368  * So this function assumes that the page can pass PageSlab() && !PageTail()
369  * check.
370  *
371  * The kmem_cache can be reparented asynchronously. The caller must ensure
372  * the memcg lifetime, e.g. by taking rcu_read_lock() or cgroup_mutex.
373  */
374 static inline struct mem_cgroup *memcg_from_slab_page(struct page *page)
375 {
376         struct kmem_cache *s;
377
378         s = READ_ONCE(page->slab_cache);
379         if (s && !is_root_cache(s))
380                 return READ_ONCE(s->memcg_params.memcg);
381
382         return NULL;
383 }
384
385 /*
386  * Charge the slab page belonging to the non-root kmem_cache.
387  * Can be called for non-root kmem_caches only.
388  */
389 static __always_inline int memcg_charge_slab(struct page *page,
390                                              gfp_t gfp, int order,
391                                              struct kmem_cache *s)
392 {
393         int nr_pages = 1 << order;
394         struct mem_cgroup *memcg;
395         struct lruvec *lruvec;
396         int ret;
397
398         rcu_read_lock();
399         memcg = READ_ONCE(s->memcg_params.memcg);
400         while (memcg && !css_tryget_online(&memcg->css))
401                 memcg = parent_mem_cgroup(memcg);
402         rcu_read_unlock();
403
404         if (unlikely(!memcg || mem_cgroup_is_root(memcg))) {
405                 mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
406                                     nr_pages << PAGE_SHIFT);
407                 percpu_ref_get_many(&s->memcg_params.refcnt, nr_pages);
408                 return 0;
409         }
410
411         ret = memcg_kmem_charge(memcg, gfp, nr_pages);
412         if (ret)
413                 goto out;
414
415         lruvec = mem_cgroup_lruvec(memcg, page_pgdat(page));
416         mod_lruvec_state(lruvec, cache_vmstat_idx(s), nr_pages << PAGE_SHIFT);
417
418         percpu_ref_get_many(&s->memcg_params.refcnt, nr_pages);
419 out:
420         css_put(&memcg->css);
421         return ret;
422 }
423
424 /*
425  * Uncharge a slab page belonging to a non-root kmem_cache.
426  * Can be called for non-root kmem_caches only.
427  */
428 static __always_inline void memcg_uncharge_slab(struct page *page, int order,
429                                                 struct kmem_cache *s)
430 {
431         int nr_pages = 1 << order;
432         struct mem_cgroup *memcg;
433         struct lruvec *lruvec;
434
435         rcu_read_lock();
436         memcg = READ_ONCE(s->memcg_params.memcg);
437         if (likely(!mem_cgroup_is_root(memcg))) {
438                 lruvec = mem_cgroup_lruvec(memcg, page_pgdat(page));
439                 mod_lruvec_state(lruvec, cache_vmstat_idx(s),
440                                  -(nr_pages << PAGE_SHIFT));
441                 memcg_kmem_uncharge(memcg, nr_pages);
442         } else {
443                 mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
444                                     -(nr_pages << PAGE_SHIFT));
445         }
446         rcu_read_unlock();
447
448         percpu_ref_put_many(&s->memcg_params.refcnt, nr_pages);
449 }
450
451 static inline int memcg_alloc_page_obj_cgroups(struct page *page,
452                                                struct kmem_cache *s, gfp_t gfp)
453 {
454         unsigned int objects = objs_per_slab_page(s, page);
455         void *vec;
456
457         vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp,
458                            page_to_nid(page));
459         if (!vec)
460                 return -ENOMEM;
461
462         kmemleak_not_leak(vec);
463         page->obj_cgroups = (struct obj_cgroup **) ((unsigned long)vec | 0x1UL);
464         return 0;
465 }
466
467 static inline void memcg_free_page_obj_cgroups(struct page *page)
468 {
469         kfree(page_obj_cgroups(page));
470         page->obj_cgroups = NULL;
471 }
472
473 static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
474                                               struct obj_cgroup *objcg,
475                                               size_t size, void **p)
476 {
477         struct page *page;
478         unsigned long off;
479         size_t i;
480
481         for (i = 0; i < size; i++) {
482                 if (likely(p[i])) {
483                         page = virt_to_head_page(p[i]);
484                         off = obj_to_index(s, page, p[i]);
485                         obj_cgroup_get(objcg);
486                         page_obj_cgroups(page)[off] = objcg;
487                 }
488         }
489         obj_cgroup_put(objcg);
490         memcg_kmem_put_cache(s);
491 }
492
493 static inline void memcg_slab_free_hook(struct kmem_cache *s, struct page *page,
494                                         void *p)
495 {
496         struct obj_cgroup *objcg;
497         unsigned int off;
498
499         if (!memcg_kmem_enabled() || is_root_cache(s))
500                 return;
501
502         off = obj_to_index(s, page, p);
503         objcg = page_obj_cgroups(page)[off];
504         page_obj_cgroups(page)[off] = NULL;
505         obj_cgroup_put(objcg);
506 }
507
508 extern void slab_init_memcg_params(struct kmem_cache *);
509 extern void memcg_link_cache(struct kmem_cache *s, struct mem_cgroup *memcg);
510
511 #else /* CONFIG_MEMCG_KMEM */
512
513 /* If !memcg, all caches are root. */
514 #define slab_root_caches        slab_caches
515 #define root_caches_node        list
516
517 #define for_each_memcg_cache(iter, root) \
518         for ((void)(iter), (void)(root); 0; )
519
520 static inline bool is_root_cache(struct kmem_cache *s)
521 {
522         return true;
523 }
524
525 static inline bool slab_equal_or_root(struct kmem_cache *s,
526                                       struct kmem_cache *p)
527 {
528         return s == p;
529 }
530
531 static inline const char *cache_name(struct kmem_cache *s)
532 {
533         return s->name;
534 }
535
536 static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s)
537 {
538         return s;
539 }
540
541 static inline struct mem_cgroup *memcg_from_slab_page(struct page *page)
542 {
543         return NULL;
544 }
545
546 static inline int memcg_charge_slab(struct page *page, gfp_t gfp, int order,
547                                     struct kmem_cache *s)
548 {
549         return 0;
550 }
551
552 static inline void memcg_uncharge_slab(struct page *page, int order,
553                                        struct kmem_cache *s)
554 {
555 }
556
557 static inline int memcg_alloc_page_obj_cgroups(struct page *page,
558                                                struct kmem_cache *s, gfp_t gfp)
559 {
560         return 0;
561 }
562
563 static inline void memcg_free_page_obj_cgroups(struct page *page)
564 {
565 }
566
567 static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
568                                               struct obj_cgroup *objcg,
569                                               size_t size, void **p)
570 {
571 }
572
573 static inline void memcg_slab_free_hook(struct kmem_cache *s, struct page *page,
574                                         void *p)
575 {
576 }
577
578 static inline void slab_init_memcg_params(struct kmem_cache *s)
579 {
580 }
581
582 static inline void memcg_link_cache(struct kmem_cache *s,
583                                     struct mem_cgroup *memcg)
584 {
585 }
586
587 #endif /* CONFIG_MEMCG_KMEM */
588
589 static inline struct kmem_cache *virt_to_cache(const void *obj)
590 {
591         struct page *page;
592
593         page = virt_to_head_page(obj);
594         if (WARN_ONCE(!PageSlab(page), "%s: Object is not a Slab page!\n",
595                                         __func__))
596                 return NULL;
597         return page->slab_cache;
598 }
599
600 static __always_inline int charge_slab_page(struct page *page,
601                                             gfp_t gfp, int order,
602                                             struct kmem_cache *s)
603 {
604         int ret;
605
606         if (is_root_cache(s)) {
607                 mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
608                                     PAGE_SIZE << order);
609                 return 0;
610         }
611
612         ret = memcg_alloc_page_obj_cgroups(page, s, gfp);
613         if (ret)
614                 return ret;
615
616         return memcg_charge_slab(page, gfp, order, s);
617 }
618
619 static __always_inline void uncharge_slab_page(struct page *page, int order,
620                                                struct kmem_cache *s)
621 {
622         if (is_root_cache(s)) {
623                 mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
624                                     -(PAGE_SIZE << order));
625                 return;
626         }
627
628         memcg_free_page_obj_cgroups(page);
629         memcg_uncharge_slab(page, order, s);
630 }
631
632 static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
633 {
634         struct kmem_cache *cachep;
635
636         if (!IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
637             !memcg_kmem_enabled() &&
638             !kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS))
639                 return s;
640
641         cachep = virt_to_cache(x);
642         if (WARN(cachep && !slab_equal_or_root(cachep, s),
643                   "%s: Wrong slab cache. %s but object is from %s\n",
644                   __func__, s->name, cachep->name))
645                 print_tracking(cachep, x);
646         return cachep;
647 }
648
649 static inline size_t slab_ksize(const struct kmem_cache *s)
650 {
651 #ifndef CONFIG_SLUB
652         return s->object_size;
653
654 #else /* CONFIG_SLUB */
655 # ifdef CONFIG_SLUB_DEBUG
656         /*
657          * Debugging requires use of the padding between object
658          * and whatever may come after it.
659          */
660         if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
661                 return s->object_size;
662 # endif
663         if (s->flags & SLAB_KASAN)
664                 return s->object_size;
665         /*
666          * If we have the need to store the freelist pointer
667          * back there or track user information then we can
668          * only use the space before that information.
669          */
670         if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
671                 return s->inuse;
672         /*
673          * Else we can use all the padding etc for the allocation
674          */
675         return s->size;
676 #endif
677 }
678
679 static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
680                                                      struct obj_cgroup **objcgp,
681                                                      size_t size, gfp_t flags)
682 {
683         flags &= gfp_allowed_mask;
684
685         fs_reclaim_acquire(flags);
686         fs_reclaim_release(flags);
687
688         might_sleep_if(gfpflags_allow_blocking(flags));
689
690         if (should_failslab(s, flags))
691                 return NULL;
692
693         if (memcg_kmem_enabled() &&
694             ((flags & __GFP_ACCOUNT) || (s->flags & SLAB_ACCOUNT)))
695                 return memcg_kmem_get_cache(s, objcgp);
696
697         return s;
698 }
699
700 static inline void slab_post_alloc_hook(struct kmem_cache *s,
701                                         struct obj_cgroup *objcg,
702                                         gfp_t flags, size_t size, void **p)
703 {
704         size_t i;
705
706         flags &= gfp_allowed_mask;
707         for (i = 0; i < size; i++) {
708                 p[i] = kasan_slab_alloc(s, p[i], flags);
709                 /* As p[i] might get tagged, call kmemleak hook after KASAN. */
710                 kmemleak_alloc_recursive(p[i], s->object_size, 1,
711                                          s->flags, flags);
712         }
713
714         if (memcg_kmem_enabled() && !is_root_cache(s))
715                 memcg_slab_post_alloc_hook(s, objcg, size, p);
716 }
717
718 #ifndef CONFIG_SLOB
719 /*
720  * The slab lists for all objects.
721  */
722 struct kmem_cache_node {
723         spinlock_t list_lock;
724
725 #ifdef CONFIG_SLAB
726         struct list_head slabs_partial; /* partial list first, better asm code */
727         struct list_head slabs_full;
728         struct list_head slabs_free;
729         unsigned long total_slabs;      /* length of all slab lists */
730         unsigned long free_slabs;       /* length of free slab list only */
731         unsigned long free_objects;
732         unsigned int free_limit;
733         unsigned int colour_next;       /* Per-node cache coloring */
734         struct array_cache *shared;     /* shared per node */
735         struct alien_cache **alien;     /* on other nodes */
736         unsigned long next_reap;        /* updated without locking */
737         int free_touched;               /* updated without locking */
738 #endif
739
740 #ifdef CONFIG_SLUB
741         unsigned long nr_partial;
742         struct list_head partial;
743 #ifdef CONFIG_SLUB_DEBUG
744         atomic_long_t nr_slabs;
745         atomic_long_t total_objects;
746         struct list_head full;
747 #endif
748 #endif
749
750 };
751
752 static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
753 {
754         return s->node[node];
755 }
756
757 /*
758  * Iterator over all nodes. The body will be executed for each node that has
759  * a kmem_cache_node structure allocated (which is true for all online nodes)
760  */
761 #define for_each_kmem_cache_node(__s, __node, __n) \
762         for (__node = 0; __node < nr_node_ids; __node++) \
763                  if ((__n = get_node(__s, __node)))
764
765 #endif
766
767 void *slab_start(struct seq_file *m, loff_t *pos);
768 void *slab_next(struct seq_file *m, void *p, loff_t *pos);
769 void slab_stop(struct seq_file *m, void *p);
770 void *memcg_slab_start(struct seq_file *m, loff_t *pos);
771 void *memcg_slab_next(struct seq_file *m, void *p, loff_t *pos);
772 void memcg_slab_stop(struct seq_file *m, void *p);
773 int memcg_slab_show(struct seq_file *m, void *p);
774
775 #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
776 void dump_unreclaimable_slab(void);
777 #else
778 static inline void dump_unreclaimable_slab(void)
779 {
780 }
781 #endif
782
783 void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
784
785 #ifdef CONFIG_SLAB_FREELIST_RANDOM
786 int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
787                         gfp_t gfp);
788 void cache_random_seq_destroy(struct kmem_cache *cachep);
789 #else
790 static inline int cache_random_seq_create(struct kmem_cache *cachep,
791                                         unsigned int count, gfp_t gfp)
792 {
793         return 0;
794 }
795 static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
796 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
797
798 static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
799 {
800         if (static_branch_unlikely(&init_on_alloc)) {
801                 if (c->ctor)
802                         return false;
803                 if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
804                         return flags & __GFP_ZERO;
805                 return true;
806         }
807         return flags & __GFP_ZERO;
808 }
809
810 static inline bool slab_want_init_on_free(struct kmem_cache *c)
811 {
812         if (static_branch_unlikely(&init_on_free))
813                 return !(c->ctor ||
814                          (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
815         return false;
816 }
817
818 #endif /* MM_SLAB_H */