blob: e716b80befc2fd8660fcaa04b43922764169490d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Christoph Lameter97d06602012-07-06 15:25:11 -05002#ifndef MM_SLAB_H
3#define MM_SLAB_H
4/*
5 * Internal slab definitions
6 */
7
Joonsoo Kim07f361b2014-10-09 15:26:00 -07008#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 */
20struct 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 */
Alexey Dobriyand50112e2017-11-15 17:32:18 -080024 slab_flags_t flags; /* Active flags on the slab */
Alexey Dobriyan7bbdb812018-04-05 16:21:31 -070025 unsigned int useroffset;/* Usercopy region offset */
26 unsigned int usersize; /* Usercopy region size */
Joonsoo Kim07f361b2014-10-09 15:26:00 -070027 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
Waiman Long9adeaa22019-09-23 15:33:49 -070033#else /* !CONFIG_SLOB */
34
Waiman Long9adeaa22019-09-23 15:33:49 -070035/*
36 * This is the main placeholder for memcg-related information in kmem caches.
Roman Gushchin98556092020-08-06 23:21:10 -070037 * Both the root cache and the child cache will have it. Some fields are used
38 * in both cases, other are specific to root caches.
Waiman Long9adeaa22019-09-23 15:33:49 -070039 *
40 * @root_cache: Common to root and child caches. NULL for root, pointer to
41 * the root cache for children.
42 *
43 * The following fields are specific to root caches.
44 *
Roman Gushchin98556092020-08-06 23:21:10 -070045 * @memcg_cache: pointer to memcg kmem cache, used by all non-root memory
46 * cgroups.
47 * @root_caches_node: list node for slab_root_caches list.
Waiman Long9adeaa22019-09-23 15:33:49 -070048 */
49struct memcg_cache_params {
50 struct kmem_cache *root_cache;
Waiman Long9adeaa22019-09-23 15:33:49 -070051
Roman Gushchin98556092020-08-06 23:21:10 -070052 struct kmem_cache *memcg_cache;
53 struct list_head __root_caches_node;
Waiman Long9adeaa22019-09-23 15:33:49 -070054};
Joonsoo Kim07f361b2014-10-09 15:26:00 -070055#endif /* CONFIG_SLOB */
56
57#ifdef CONFIG_SLAB
58#include <linux/slab_def.h>
59#endif
60
61#ifdef CONFIG_SLUB
62#include <linux/slub_def.h>
63#endif
64
65#include <linux/memcontrol.h>
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -070066#include <linux/fault-inject.h>
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -070067#include <linux/kasan.h>
68#include <linux/kmemleak.h>
Thomas Garnier7c00fce2016-07-26 15:21:56 -070069#include <linux/random.h>
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +010070#include <linux/sched/mm.h>
Roman Gushchin286e04b2020-08-06 23:20:52 -070071#include <linux/kmemleak.h>
Joonsoo Kim07f361b2014-10-09 15:26:00 -070072
Christoph Lameter97d06602012-07-06 15:25:11 -050073/*
74 * State of the slab allocator.
75 *
76 * This is used to describe the states of the allocator during bootup.
77 * Allocators use this to gradually bootstrap themselves. Most allocators
78 * have the problem that the structures used for managing slab caches are
79 * allocated from slab caches themselves.
80 */
81enum slab_state {
82 DOWN, /* No slab functionality yet */
83 PARTIAL, /* SLUB: kmem_cache_node available */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +000084 PARTIAL_NODE, /* SLAB: kmalloc size for node struct available */
Christoph Lameter97d06602012-07-06 15:25:11 -050085 UP, /* Slab caches usable but not all extras yet */
86 FULL /* Everything is working */
87};
88
89extern enum slab_state slab_state;
90
Christoph Lameter18004c52012-07-06 15:25:12 -050091/* The slab cache mutex protects the management structures during changes */
92extern struct mutex slab_mutex;
Christoph Lameter9b030cb2012-09-05 00:20:33 +000093
94/* The list of all slab caches on the system */
Christoph Lameter18004c52012-07-06 15:25:12 -050095extern struct list_head slab_caches;
96
Christoph Lameter9b030cb2012-09-05 00:20:33 +000097/* The slab cache that manages slab cache information */
98extern struct kmem_cache *kmem_cache;
99
Vlastimil Babkaaf3b5f82017-02-22 15:41:05 -0800100/* A table of kmalloc cache names and sizes */
101extern const struct kmalloc_info_struct {
Pengfei Licb5d9fb2019-11-30 17:49:21 -0800102 const char *name[NR_KMALLOC_TYPES];
Alexey Dobriyan55de8b92018-04-05 16:20:29 -0700103 unsigned int size;
Vlastimil Babkaaf3b5f82017-02-22 15:41:05 -0800104} kmalloc_info[];
105
Christoph Lameterf97d5f62013-01-10 19:12:17 +0000106#ifndef CONFIG_SLOB
107/* Kmalloc array related functions */
Daniel Sanders34cc6992015-06-24 16:55:57 -0700108void setup_kmalloc_cache_index_table(void);
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800109void create_kmalloc_caches(slab_flags_t);
Christoph Lameter2c59dd62013-01-10 19:14:19 +0000110
111/* Find the kmalloc slab corresponding for a certain size */
112struct kmem_cache *kmalloc_slab(size_t, gfp_t);
Christoph Lameterf97d5f62013-01-10 19:12:17 +0000113#endif
114
Long Li44405092020-08-06 23:18:28 -0700115gfp_t kmalloc_fix_flags(gfp_t flags);
Christoph Lameterf97d5f62013-01-10 19:12:17 +0000116
Christoph Lameter9b030cb2012-09-05 00:20:33 +0000117/* Functions provided by the slab allocators */
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800118int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags);
Christoph Lameter97d06602012-07-06 15:25:11 -0500119
Alexey Dobriyan55de8b92018-04-05 16:20:29 -0700120struct kmem_cache *create_kmalloc_cache(const char *name, unsigned int size,
121 slab_flags_t flags, unsigned int useroffset,
122 unsigned int usersize);
Christoph Lameter45530c42012-11-28 16:23:07 +0000123extern void create_boot_cache(struct kmem_cache *, const char *name,
Alexey Dobriyan361d5752018-04-05 16:20:33 -0700124 unsigned int size, slab_flags_t flags,
125 unsigned int useroffset, unsigned int usersize);
Christoph Lameter45530c42012-11-28 16:23:07 +0000126
Joonsoo Kim423c9292014-10-09 15:26:22 -0700127int slab_unmergeable(struct kmem_cache *s);
Alexey Dobriyanf4957d52018-04-05 16:20:37 -0700128struct kmem_cache *find_mergeable(unsigned size, unsigned align,
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800129 slab_flags_t flags, const char *name, void (*ctor)(void *));
Joonsoo Kim12220de2014-10-09 15:26:24 -0700130#ifndef CONFIG_SLOB
Glauber Costa2633d7a2012-12-18 14:22:34 -0800131struct kmem_cache *
Alexey Dobriyanf4957d52018-04-05 16:20:37 -0700132__kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800133 slab_flags_t flags, void (*ctor)(void *));
Joonsoo Kim423c9292014-10-09 15:26:22 -0700134
Alexey Dobriyan0293d1f2018-04-05 16:21:24 -0700135slab_flags_t kmem_cache_flags(unsigned int object_size,
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800136 slab_flags_t flags, const char *name,
Joonsoo Kim423c9292014-10-09 15:26:22 -0700137 void (*ctor)(void *));
Christoph Lametercbb79692012-09-05 00:18:32 +0000138#else
Glauber Costa2633d7a2012-12-18 14:22:34 -0800139static inline struct kmem_cache *
Alexey Dobriyanf4957d52018-04-05 16:20:37 -0700140__kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800141 slab_flags_t flags, void (*ctor)(void *))
Christoph Lametercbb79692012-09-05 00:18:32 +0000142{ return NULL; }
Joonsoo Kim423c9292014-10-09 15:26:22 -0700143
Alexey Dobriyan0293d1f2018-04-05 16:21:24 -0700144static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800145 slab_flags_t flags, const char *name,
Joonsoo Kim423c9292014-10-09 15:26:22 -0700146 void (*ctor)(void *))
147{
148 return flags;
149}
Christoph Lametercbb79692012-09-05 00:18:32 +0000150#endif
151
152
Glauber Costad8843922012-10-17 15:36:51 +0400153/* Legal flag mask for kmem_cache_create(), for various configurations */
Nicolas Boichat6d6ea1e2019-03-28 20:43:42 -0700154#define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
155 SLAB_CACHE_DMA32 | SLAB_PANIC | \
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800156 SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS )
Glauber Costad8843922012-10-17 15:36:51 +0400157
158#if defined(CONFIG_DEBUG_SLAB)
159#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
160#elif defined(CONFIG_SLUB_DEBUG)
161#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
Laura Abbottbecfda62016-03-15 14:55:06 -0700162 SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
Glauber Costad8843922012-10-17 15:36:51 +0400163#else
164#define SLAB_DEBUG_FLAGS (0)
165#endif
166
167#if defined(CONFIG_SLAB)
168#define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
Vladimir Davydov230e9fc2016-01-14 15:18:15 -0800169 SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -0800170 SLAB_ACCOUNT)
Glauber Costad8843922012-10-17 15:36:51 +0400171#elif defined(CONFIG_SLUB)
172#define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -0800173 SLAB_TEMPORARY | SLAB_ACCOUNT)
Glauber Costad8843922012-10-17 15:36:51 +0400174#else
175#define SLAB_CACHE_FLAGS (0)
176#endif
177
Thomas Garniere70954f2016-12-12 16:41:38 -0800178/* Common flags available with current configuration */
Glauber Costad8843922012-10-17 15:36:51 +0400179#define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
180
Thomas Garniere70954f2016-12-12 16:41:38 -0800181/* Common flags permitted for kmem_cache_create */
182#define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
183 SLAB_RED_ZONE | \
184 SLAB_POISON | \
185 SLAB_STORE_USER | \
186 SLAB_TRACE | \
187 SLAB_CONSISTENCY_CHECKS | \
188 SLAB_MEM_SPREAD | \
189 SLAB_NOLEAKTRACE | \
190 SLAB_RECLAIM_ACCOUNT | \
191 SLAB_TEMPORARY | \
Thomas Garniere70954f2016-12-12 16:41:38 -0800192 SLAB_ACCOUNT)
193
Shakeel Buttf9e13c02018-04-05 16:21:57 -0700194bool __kmem_cache_empty(struct kmem_cache *);
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000195int __kmem_cache_shutdown(struct kmem_cache *);
Dmitry Safonov52b4b952016-02-17 13:11:37 -0800196void __kmem_cache_release(struct kmem_cache *);
Tejun Heoc9fc5862017-02-22 15:41:27 -0800197int __kmem_cache_shrink(struct kmem_cache *);
Christoph Lameter41a21282014-05-06 12:50:08 -0700198void slab_kmem_cache_release(struct kmem_cache *);
Waiman Long04f768a2019-09-23 15:33:46 -0700199void kmem_cache_shrink_all(struct kmem_cache *s);
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000200
Glauber Costab7454ad2012-10-19 18:20:25 +0400201struct seq_file;
202struct file;
Glauber Costab7454ad2012-10-19 18:20:25 +0400203
Glauber Costa0d7561c2012-10-19 18:20:27 +0400204struct slabinfo {
205 unsigned long active_objs;
206 unsigned long num_objs;
207 unsigned long active_slabs;
208 unsigned long num_slabs;
209 unsigned long shared_avail;
210 unsigned int limit;
211 unsigned int batchcount;
212 unsigned int shared;
213 unsigned int objects_per_slab;
214 unsigned int cache_order;
215};
216
217void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
218void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
Glauber Costab7454ad2012-10-19 18:20:25 +0400219ssize_t slabinfo_write(struct file *file, const char __user *buffer,
220 size_t count, loff_t *ppos);
Glauber Costaba6c4962012-12-18 14:22:27 -0800221
Christoph Lameter484748f2015-09-04 15:45:34 -0700222/*
223 * Generic implementation of bulk operations
224 * These are useful for situations in which the allocator cannot
Jesper Dangaard Brouer9f706d62016-03-15 14:54:03 -0700225 * perform optimizations. In that case segments of the object listed
Christoph Lameter484748f2015-09-04 15:45:34 -0700226 * may be allocated or freed using these operations.
227 */
228void __kmem_cache_free_bulk(struct kmem_cache *, size_t, void **);
Jesper Dangaard Brouer865762a2015-11-20 15:57:58 -0800229int __kmem_cache_alloc_bulk(struct kmem_cache *, gfp_t, size_t, void **);
Christoph Lameter484748f2015-09-04 15:45:34 -0700230
Roman Gushchin6cea1d52019-07-11 20:56:16 -0700231static inline int cache_vmstat_idx(struct kmem_cache *s)
232{
233 return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
Roman Gushchind42f3242020-08-06 23:20:39 -0700234 NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
Roman Gushchin6cea1d52019-07-11 20:56:16 -0700235}
236
Vlastimil Babkae42f1742020-08-06 23:19:05 -0700237#ifdef CONFIG_SLUB_DEBUG
238#ifdef CONFIG_SLUB_DEBUG_ON
239DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
240#else
241DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
242#endif
243extern void print_tracking(struct kmem_cache *s, void *object);
244#else
245static inline void print_tracking(struct kmem_cache *s, void *object)
246{
247}
248#endif
249
250/*
251 * Returns true if any of the specified slub_debug flags is enabled for the
252 * cache. Use only for flags parsed by setup_slub_debug() as it also enables
253 * the static key.
254 */
255static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
256{
257#ifdef CONFIG_SLUB_DEBUG
258 VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
259 if (static_branch_unlikely(&slub_debug_enabled))
260 return s->flags & flags;
261#endif
262 return false;
263}
264
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700265#ifdef CONFIG_MEMCG_KMEM
Tejun Heo510ded32017-02-22 15:41:24 -0800266
267/* List of all root caches. */
268extern struct list_head slab_root_caches;
269#define root_caches_node memcg_params.__root_caches_node
270
Glauber Costaba6c4962012-12-18 14:22:27 -0800271static inline bool is_root_cache(struct kmem_cache *s)
272{
Tejun Heo9eeadc82017-02-22 15:41:17 -0800273 return !s->memcg_params.root_cache;
Glauber Costaba6c4962012-12-18 14:22:27 -0800274}
Glauber Costa2633d7a2012-12-18 14:22:34 -0800275
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800276static inline bool slab_equal_or_root(struct kmem_cache *s,
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800277 struct kmem_cache *p)
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800278{
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800279 return p == s || p == s->memcg_params.root_cache;
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800280}
Glauber Costa749c5412012-12-18 14:23:01 -0800281
282/*
283 * We use suffixes to the name in memcg because we can't have caches
284 * created in the system with the same name. But when we print them
285 * locally, better refer to them with the base name
286 */
287static inline const char *cache_name(struct kmem_cache *s)
288{
289 if (!is_root_cache(s))
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800290 s = s->memcg_params.root_cache;
Glauber Costa749c5412012-12-18 14:23:01 -0800291 return s->name;
292}
293
Glauber Costa943a4512012-12-18 14:23:03 -0800294static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s)
295{
296 if (is_root_cache(s))
297 return s;
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800298 return s->memcg_params.root_cache;
Glauber Costa943a4512012-12-18 14:23:03 -0800299}
Vladimir Davydov5dfb4172014-06-04 16:06:38 -0700300
Roman Gushchin98556092020-08-06 23:21:10 -0700301static inline struct kmem_cache *memcg_cache(struct kmem_cache *s)
302{
303 if (is_root_cache(s))
304 return s->memcg_params.memcg_cache;
305 return NULL;
306}
307
Roman Gushchin286e04b2020-08-06 23:20:52 -0700308static inline struct obj_cgroup **page_obj_cgroups(struct page *page)
309{
310 /*
311 * page->mem_cgroup and page->obj_cgroups are sharing the same
312 * space. To distinguish between them in case we don't know for sure
313 * that the page is a slab page (e.g. page_cgroup_ino()), let's
314 * always set the lowest bit of obj_cgroups.
315 */
316 return (struct obj_cgroup **)
317 ((unsigned long)page->obj_cgroups & ~0x1UL);
318}
319
Roman Gushchin98556092020-08-06 23:21:10 -0700320static inline bool page_has_obj_cgroups(struct page *page)
Roman Gushchin4d96ba32019-07-11 20:56:31 -0700321{
Roman Gushchin98556092020-08-06 23:21:10 -0700322 return ((unsigned long)page->obj_cgroups & 0x1UL);
Roman Gushchin4d96ba32019-07-11 20:56:31 -0700323}
324
Roman Gushchin286e04b2020-08-06 23:20:52 -0700325static inline int memcg_alloc_page_obj_cgroups(struct page *page,
326 struct kmem_cache *s, gfp_t gfp)
327{
328 unsigned int objects = objs_per_slab_page(s, page);
329 void *vec;
330
331 vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp,
332 page_to_nid(page));
333 if (!vec)
334 return -ENOMEM;
335
336 kmemleak_not_leak(vec);
337 page->obj_cgroups = (struct obj_cgroup **) ((unsigned long)vec | 0x1UL);
338 return 0;
339}
340
341static inline void memcg_free_page_obj_cgroups(struct page *page)
342{
343 kfree(page_obj_cgroups(page));
344 page->obj_cgroups = NULL;
345}
346
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700347static inline size_t obj_full_size(struct kmem_cache *s)
348{
349 /*
350 * For each accounted object there is an extra space which is used
351 * to store obj_cgroup membership. Charge it too.
352 */
353 return s->size + sizeof(struct obj_cgroup *);
354}
355
356static inline struct kmem_cache *memcg_slab_pre_alloc_hook(struct kmem_cache *s,
357 struct obj_cgroup **objcgp,
358 size_t objects, gfp_t flags)
359{
360 struct kmem_cache *cachep;
Roman Gushchin98556092020-08-06 23:21:10 -0700361 struct obj_cgroup *objcg;
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700362
Roman Gushchin98556092020-08-06 23:21:10 -0700363 if (memcg_kmem_bypass())
364 return s;
365
366 cachep = memcg_kmem_get_cache(s);
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700367 if (is_root_cache(cachep))
368 return s;
369
Roman Gushchin98556092020-08-06 23:21:10 -0700370 objcg = get_obj_cgroup_from_current();
371 if (!objcg)
372 return s;
373
374 if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s))) {
375 obj_cgroup_put(objcg);
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700376 cachep = NULL;
377 }
378
Roman Gushchin98556092020-08-06 23:21:10 -0700379 *objcgp = objcg;
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700380 return cachep;
381}
382
383static inline void mod_objcg_state(struct obj_cgroup *objcg,
384 struct pglist_data *pgdat,
385 int idx, int nr)
386{
387 struct mem_cgroup *memcg;
388 struct lruvec *lruvec;
389
390 rcu_read_lock();
391 memcg = obj_cgroup_memcg(objcg);
392 lruvec = mem_cgroup_lruvec(memcg, pgdat);
393 mod_memcg_lruvec_state(lruvec, idx, nr);
394 rcu_read_unlock();
395}
396
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700397static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
398 struct obj_cgroup *objcg,
399 size_t size, void **p)
400{
401 struct page *page;
402 unsigned long off;
403 size_t i;
404
405 for (i = 0; i < size; i++) {
406 if (likely(p[i])) {
407 page = virt_to_head_page(p[i]);
408 off = obj_to_index(s, page, p[i]);
409 obj_cgroup_get(objcg);
410 page_obj_cgroups(page)[off] = objcg;
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700411 mod_objcg_state(objcg, page_pgdat(page),
412 cache_vmstat_idx(s), obj_full_size(s));
413 } else {
414 obj_cgroup_uncharge(objcg, obj_full_size(s));
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700415 }
416 }
417 obj_cgroup_put(objcg);
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700418}
419
420static inline void memcg_slab_free_hook(struct kmem_cache *s, struct page *page,
421 void *p)
422{
423 struct obj_cgroup *objcg;
424 unsigned int off;
425
426 if (!memcg_kmem_enabled() || is_root_cache(s))
427 return;
428
429 off = obj_to_index(s, page, p);
430 objcg = page_obj_cgroups(page)[off];
431 page_obj_cgroups(page)[off] = NULL;
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700432
433 obj_cgroup_uncharge(objcg, obj_full_size(s));
434 mod_objcg_state(objcg, page_pgdat(page), cache_vmstat_idx(s),
435 -obj_full_size(s));
436
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700437 obj_cgroup_put(objcg);
438}
439
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800440extern void slab_init_memcg_params(struct kmem_cache *);
Roman Gushchin98556092020-08-06 23:21:10 -0700441extern void memcg_link_cache(struct kmem_cache *s);
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800442
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700443#else /* CONFIG_MEMCG_KMEM */
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800444
Tejun Heo510ded32017-02-22 15:41:24 -0800445/* If !memcg, all caches are root. */
446#define slab_root_caches slab_caches
447#define root_caches_node list
448
Glauber Costaba6c4962012-12-18 14:22:27 -0800449static inline bool is_root_cache(struct kmem_cache *s)
450{
451 return true;
452}
453
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800454static inline bool slab_equal_or_root(struct kmem_cache *s,
455 struct kmem_cache *p)
456{
Kees Cook598a0712019-07-11 20:53:23 -0700457 return s == p;
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800458}
Glauber Costa749c5412012-12-18 14:23:01 -0800459
460static inline const char *cache_name(struct kmem_cache *s)
461{
462 return s->name;
463}
464
Glauber Costa943a4512012-12-18 14:23:03 -0800465static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s)
466{
467 return s;
468}
Vladimir Davydov5dfb4172014-06-04 16:06:38 -0700469
Roman Gushchin98556092020-08-06 23:21:10 -0700470static inline struct kmem_cache *memcg_cache(struct kmem_cache *s)
471{
472 return NULL;
473}
474
475static inline bool page_has_obj_cgroups(struct page *page)
476{
477 return false;
478}
479
480static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
Roman Gushchin4d96ba32019-07-11 20:56:31 -0700481{
482 return NULL;
483}
484
Roman Gushchin286e04b2020-08-06 23:20:52 -0700485static inline int memcg_alloc_page_obj_cgroups(struct page *page,
486 struct kmem_cache *s, gfp_t gfp)
487{
488 return 0;
489}
490
491static inline void memcg_free_page_obj_cgroups(struct page *page)
492{
493}
494
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700495static inline struct kmem_cache *memcg_slab_pre_alloc_hook(struct kmem_cache *s,
496 struct obj_cgroup **objcgp,
497 size_t objects, gfp_t flags)
498{
499 return NULL;
500}
501
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700502static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
503 struct obj_cgroup *objcg,
504 size_t size, void **p)
505{
506}
507
508static inline void memcg_slab_free_hook(struct kmem_cache *s, struct page *page,
509 void *p)
510{
511}
512
Vladimir Davydovf7ce3192015-02-12 14:59:20 -0800513static inline void slab_init_memcg_params(struct kmem_cache *s)
514{
515}
Tejun Heo510ded32017-02-22 15:41:24 -0800516
Roman Gushchin98556092020-08-06 23:21:10 -0700517static inline void memcg_link_cache(struct kmem_cache *s)
Tejun Heo510ded32017-02-22 15:41:24 -0800518{
519}
520
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700521#endif /* CONFIG_MEMCG_KMEM */
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800522
Kees Cooka64b5372019-07-11 20:53:26 -0700523static inline struct kmem_cache *virt_to_cache(const void *obj)
524{
525 struct page *page;
526
527 page = virt_to_head_page(obj);
528 if (WARN_ONCE(!PageSlab(page), "%s: Object is not a Slab page!\n",
529 __func__))
530 return NULL;
531 return page->slab_cache;
532}
533
Roman Gushchin6cea1d52019-07-11 20:56:16 -0700534static __always_inline int charge_slab_page(struct page *page,
535 gfp_t gfp, int order,
536 struct kmem_cache *s)
537{
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700538 if (memcg_kmem_enabled() && !is_root_cache(s)) {
539 int ret;
Roman Gushchin286e04b2020-08-06 23:20:52 -0700540
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700541 ret = memcg_alloc_page_obj_cgroups(page, s, gfp);
542 if (ret)
543 return ret;
Roman Gushchin4d96ba32019-07-11 20:56:31 -0700544 }
Roman Gushchin98556092020-08-06 23:21:10 -0700545
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700546 mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
547 PAGE_SIZE << order);
548 return 0;
Roman Gushchin6cea1d52019-07-11 20:56:16 -0700549}
550
551static __always_inline void uncharge_slab_page(struct page *page, int order,
552 struct kmem_cache *s)
553{
Roman Gushchin98556092020-08-06 23:21:10 -0700554 if (memcg_kmem_enabled() && !is_root_cache(s))
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700555 memcg_free_page_obj_cgroups(page);
Roman Gushchin98556092020-08-06 23:21:10 -0700556
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700557 mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
558 -(PAGE_SIZE << order));
Roman Gushchin6cea1d52019-07-11 20:56:16 -0700559}
560
Vlastimil Babkae42f1742020-08-06 23:19:05 -0700561static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
562{
563 struct kmem_cache *cachep;
564
565 if (!IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
566 !memcg_kmem_enabled() &&
567 !kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS))
568 return s;
569
570 cachep = virt_to_cache(x);
571 if (WARN(cachep && !slab_equal_or_root(cachep, s),
572 "%s: Wrong slab cache. %s but object is from %s\n",
573 __func__, s->name, cachep->name))
574 print_tracking(cachep, x);
575 return cachep;
576}
577
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700578static inline size_t slab_ksize(const struct kmem_cache *s)
579{
580#ifndef CONFIG_SLUB
581 return s->object_size;
582
583#else /* CONFIG_SLUB */
584# ifdef CONFIG_SLUB_DEBUG
585 /*
586 * Debugging requires use of the padding between object
587 * and whatever may come after it.
588 */
589 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
590 return s->object_size;
591# endif
Alexander Potapenko80a92012016-07-28 15:49:07 -0700592 if (s->flags & SLAB_KASAN)
593 return s->object_size;
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700594 /*
595 * If we have the need to store the freelist pointer
596 * back there or track user information then we can
597 * only use the space before that information.
598 */
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800599 if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700600 return s->inuse;
601 /*
602 * Else we can use all the padding etc for the allocation
603 */
604 return s->size;
605#endif
606}
607
608static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700609 struct obj_cgroup **objcgp,
610 size_t size, gfp_t flags)
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700611{
612 flags &= gfp_allowed_mask;
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +0100613
614 fs_reclaim_acquire(flags);
615 fs_reclaim_release(flags);
616
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700617 might_sleep_if(gfpflags_allow_blocking(flags));
618
Jesper Dangaard Brouerfab99632016-03-15 14:53:38 -0700619 if (should_failslab(s, flags))
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700620 return NULL;
621
Vladimir Davydov45264772016-07-26 15:24:21 -0700622 if (memcg_kmem_enabled() &&
623 ((flags & __GFP_ACCOUNT) || (s->flags & SLAB_ACCOUNT)))
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700624 return memcg_slab_pre_alloc_hook(s, objcgp, size, flags);
Vladimir Davydov45264772016-07-26 15:24:21 -0700625
626 return s;
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700627}
628
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700629static inline void slab_post_alloc_hook(struct kmem_cache *s,
630 struct obj_cgroup *objcg,
631 gfp_t flags, size_t size, void **p)
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700632{
633 size_t i;
634
635 flags &= gfp_allowed_mask;
636 for (i = 0; i < size; i++) {
Andrey Konovalov53128242019-02-20 22:19:11 -0800637 p[i] = kasan_slab_alloc(s, p[i], flags);
Andrey Konovalova2f77572019-02-20 22:19:16 -0800638 /* As p[i] might get tagged, call kmemleak hook after KASAN. */
Andrey Konovalov53128242019-02-20 22:19:11 -0800639 kmemleak_alloc_recursive(p[i], s->object_size, 1,
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700640 s->flags, flags);
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700641 }
Vladimir Davydov45264772016-07-26 15:24:21 -0700642
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700643 if (memcg_kmem_enabled() && !is_root_cache(s))
644 memcg_slab_post_alloc_hook(s, objcg, size, p);
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700645}
646
Christoph Lameter44c53562014-08-06 16:04:07 -0700647#ifndef CONFIG_SLOB
Christoph Lameterca349562013-01-10 19:14:19 +0000648/*
649 * The slab lists for all objects.
650 */
651struct kmem_cache_node {
652 spinlock_t list_lock;
653
654#ifdef CONFIG_SLAB
655 struct list_head slabs_partial; /* partial list first, better asm code */
656 struct list_head slabs_full;
657 struct list_head slabs_free;
David Rientjesbf00bd32016-12-12 16:41:44 -0800658 unsigned long total_slabs; /* length of all slab lists */
659 unsigned long free_slabs; /* length of free slab list only */
Christoph Lameterca349562013-01-10 19:14:19 +0000660 unsigned long free_objects;
661 unsigned int free_limit;
662 unsigned int colour_next; /* Per-node cache coloring */
663 struct array_cache *shared; /* shared per node */
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700664 struct alien_cache **alien; /* on other nodes */
Christoph Lameterca349562013-01-10 19:14:19 +0000665 unsigned long next_reap; /* updated without locking */
666 int free_touched; /* updated without locking */
667#endif
668
669#ifdef CONFIG_SLUB
670 unsigned long nr_partial;
671 struct list_head partial;
672#ifdef CONFIG_SLUB_DEBUG
673 atomic_long_t nr_slabs;
674 atomic_long_t total_objects;
675 struct list_head full;
676#endif
677#endif
678
679};
Wanpeng Lie25839f2013-07-04 08:33:23 +0800680
Christoph Lameter44c53562014-08-06 16:04:07 -0700681static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
682{
683 return s->node[node];
684}
685
686/*
687 * Iterator over all nodes. The body will be executed for each node that has
688 * a kmem_cache_node structure allocated (which is true for all online nodes)
689 */
690#define for_each_kmem_cache_node(__s, __node, __n) \
Mikulas Patocka91635822014-10-09 15:26:20 -0700691 for (__node = 0; __node < nr_node_ids; __node++) \
692 if ((__n = get_node(__s, __node)))
Christoph Lameter44c53562014-08-06 16:04:07 -0700693
694#endif
695
Vladimir Davydov1df3b262014-12-10 15:42:16 -0800696void *slab_start(struct seq_file *m, loff_t *pos);
Wanpeng Li276a2432013-07-08 08:08:28 +0800697void *slab_next(struct seq_file *m, void *p, loff_t *pos);
698void slab_stop(struct seq_file *m, void *p);
Vladimir Davydovb0475012014-12-10 15:44:19 -0800699int memcg_slab_show(struct seq_file *m, void *p);
Andrey Ryabinin5240ab42014-08-06 16:04:14 -0700700
Yang Shi852d8be2017-11-15 17:32:07 -0800701#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
702void dump_unreclaimable_slab(void);
703#else
704static inline void dump_unreclaimable_slab(void)
705{
706}
707#endif
708
Alexander Potapenko55834c52016-05-20 16:59:11 -0700709void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
710
Thomas Garnier7c00fce2016-07-26 15:21:56 -0700711#ifdef CONFIG_SLAB_FREELIST_RANDOM
712int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
713 gfp_t gfp);
714void cache_random_seq_destroy(struct kmem_cache *cachep);
715#else
716static inline int cache_random_seq_create(struct kmem_cache *cachep,
717 unsigned int count, gfp_t gfp)
718{
719 return 0;
720}
721static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
722#endif /* CONFIG_SLAB_FREELIST_RANDOM */
723
Alexander Potapenko64713842019-07-11 20:59:19 -0700724static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
725{
726 if (static_branch_unlikely(&init_on_alloc)) {
727 if (c->ctor)
728 return false;
729 if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
730 return flags & __GFP_ZERO;
731 return true;
732 }
733 return flags & __GFP_ZERO;
734}
735
736static inline bool slab_want_init_on_free(struct kmem_cache *c)
737{
738 if (static_branch_unlikely(&init_on_free))
739 return !(c->ctor ||
740 (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
741 return false;
742}
743
Andrey Ryabinin5240ab42014-08-06 16:04:14 -0700744#endif /* MM_SLAB_H */