blob: 4738d19b6df6753fdf2ce4ac1ff2a36b361b946e [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
33#endif /* CONFIG_SLOB */
34
35#ifdef CONFIG_SLAB
36#include <linux/slab_def.h>
37#endif
38
39#ifdef CONFIG_SLUB
40#include <linux/slub_def.h>
41#endif
42
43#include <linux/memcontrol.h>
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -070044#include <linux/fault-inject.h>
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -070045#include <linux/kasan.h>
46#include <linux/kmemleak.h>
Thomas Garnier7c00fce2016-07-26 15:21:56 -070047#include <linux/random.h>
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +010048#include <linux/sched/mm.h>
Liujie Xie8bc63372021-05-11 19:05:32 +080049#include <linux/android_vendor.h>
Joonsoo Kim07f361b2014-10-09 15:26:00 -070050
Christoph Lameter97d06602012-07-06 15:25:11 -050051/*
52 * State of the slab allocator.
53 *
54 * This is used to describe the states of the allocator during bootup.
55 * Allocators use this to gradually bootstrap themselves. Most allocators
56 * have the problem that the structures used for managing slab caches are
57 * allocated from slab caches themselves.
58 */
59enum slab_state {
60 DOWN, /* No slab functionality yet */
61 PARTIAL, /* SLUB: kmem_cache_node available */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +000062 PARTIAL_NODE, /* SLAB: kmalloc size for node struct available */
Christoph Lameter97d06602012-07-06 15:25:11 -050063 UP, /* Slab caches usable but not all extras yet */
64 FULL /* Everything is working */
65};
66
67extern enum slab_state slab_state;
68
Christoph Lameter18004c52012-07-06 15:25:12 -050069/* The slab cache mutex protects the management structures during changes */
70extern struct mutex slab_mutex;
Christoph Lameter9b030cb2012-09-05 00:20:33 +000071
72/* The list of all slab caches on the system */
Christoph Lameter18004c52012-07-06 15:25:12 -050073extern struct list_head slab_caches;
74
Christoph Lameter9b030cb2012-09-05 00:20:33 +000075/* The slab cache that manages slab cache information */
76extern struct kmem_cache *kmem_cache;
77
Vlastimil Babkaaf3b5f82017-02-22 15:41:05 -080078/* A table of kmalloc cache names and sizes */
79extern const struct kmalloc_info_struct {
Pengfei Licb5d9fb2019-11-30 17:49:21 -080080 const char *name[NR_KMALLOC_TYPES];
Alexey Dobriyan55de8b92018-04-05 16:20:29 -070081 unsigned int size;
Vlastimil Babkaaf3b5f82017-02-22 15:41:05 -080082} kmalloc_info[];
83
Christoph Lameterf97d5f632013-01-10 19:12:17 +000084#ifndef CONFIG_SLOB
85/* Kmalloc array related functions */
Daniel Sanders34cc6992015-06-24 16:55:57 -070086void setup_kmalloc_cache_index_table(void);
Alexey Dobriyand50112e2017-11-15 17:32:18 -080087void create_kmalloc_caches(slab_flags_t);
Christoph Lameter2c59dd62013-01-10 19:14:19 +000088
89/* Find the kmalloc slab corresponding for a certain size */
90struct kmem_cache *kmalloc_slab(size_t, gfp_t);
Christoph Lameterf97d5f632013-01-10 19:12:17 +000091#endif
92
Long Li44405092020-08-06 23:18:28 -070093gfp_t kmalloc_fix_flags(gfp_t flags);
Christoph Lameterf97d5f632013-01-10 19:12:17 +000094
Vijayanand Jittaee8d2c72021-01-12 00:42:08 +053095#ifdef CONFIG_SLUB
96/*
97 * Tracking user of a slab.
98 */
99#define TRACK_ADDRS_COUNT 16
100struct track {
101 unsigned long addr; /* Called from address */
102#ifdef CONFIG_STACKTRACE
103 unsigned long addrs[TRACK_ADDRS_COUNT]; /* Called from address */
104#endif
105 int cpu; /* Was running on cpu */
106 int pid; /* Pid context */
107 unsigned long when; /* When did the operation occur */
Liujie Xie8bc63372021-05-11 19:05:32 +0800108#ifdef CONFIG_STACKTRACE
109 ANDROID_OEM_DATA(1);
110#endif
Vijayanand Jittaee8d2c72021-01-12 00:42:08 +0530111};
112
113enum track_item { TRACK_ALLOC, TRACK_FREE };
114#endif
115
Christoph Lameter9b030cb2012-09-05 00:20:33 +0000116/* Functions provided by the slab allocators */
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800117int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags);
Christoph Lameter97d06602012-07-06 15:25:11 -0500118
Alexey Dobriyan55de8b92018-04-05 16:20:29 -0700119struct kmem_cache *create_kmalloc_cache(const char *name, unsigned int size,
120 slab_flags_t flags, unsigned int useroffset,
121 unsigned int usersize);
Christoph Lameter45530c42012-11-28 16:23:07 +0000122extern void create_boot_cache(struct kmem_cache *, const char *name,
Alexey Dobriyan361d5752018-04-05 16:20:33 -0700123 unsigned int size, slab_flags_t flags,
124 unsigned int useroffset, unsigned int usersize);
Christoph Lameter45530c42012-11-28 16:23:07 +0000125
Joonsoo Kim423c9292014-10-09 15:26:22 -0700126int slab_unmergeable(struct kmem_cache *s);
Alexey Dobriyanf4957d52018-04-05 16:20:37 -0700127struct kmem_cache *find_mergeable(unsigned size, unsigned align,
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800128 slab_flags_t flags, const char *name, void (*ctor)(void *));
Joonsoo Kim12220de2014-10-09 15:26:24 -0700129#ifndef CONFIG_SLOB
Glauber Costa2633d7a2012-12-18 14:22:34 -0800130struct kmem_cache *
Alexey Dobriyanf4957d52018-04-05 16:20:37 -0700131__kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800132 slab_flags_t flags, void (*ctor)(void *));
Joonsoo Kim423c9292014-10-09 15:26:22 -0700133
Alexey Dobriyan0293d1f2018-04-05 16:21:24 -0700134slab_flags_t kmem_cache_flags(unsigned int object_size,
Nikolay Borisov2e95bc62021-02-24 12:00:58 -0800135 slab_flags_t flags, const char *name);
Christoph Lametercbb79692012-09-05 00:18:32 +0000136#else
Glauber Costa2633d7a2012-12-18 14:22:34 -0800137static inline struct kmem_cache *
Alexey Dobriyanf4957d52018-04-05 16:20:37 -0700138__kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
Alexey Dobriyand50112e2017-11-15 17:32:18 -0800139 slab_flags_t flags, void (*ctor)(void *))
Christoph Lametercbb79692012-09-05 00:18:32 +0000140{ return NULL; }
Joonsoo Kim423c9292014-10-09 15:26:22 -0700141
Alexey Dobriyan0293d1f2018-04-05 16:21:24 -0700142static inline slab_flags_t kmem_cache_flags(unsigned int object_size,
Nikolay Borisov2e95bc62021-02-24 12:00:58 -0800143 slab_flags_t flags, const char *name)
Joonsoo Kim423c9292014-10-09 15:26:22 -0700144{
145 return flags;
146}
Christoph Lametercbb79692012-09-05 00:18:32 +0000147#endif
148
149
Glauber Costad8843922012-10-17 15:36:51 +0400150/* Legal flag mask for kmem_cache_create(), for various configurations */
Nicolas Boichat6d6ea1e2019-03-28 20:43:42 -0700151#define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \
152 SLAB_CACHE_DMA32 | SLAB_PANIC | \
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800153 SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS )
Glauber Costad8843922012-10-17 15:36:51 +0400154
155#if defined(CONFIG_DEBUG_SLAB)
156#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER)
157#elif defined(CONFIG_SLUB_DEBUG)
158#define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
Laura Abbottbecfda62016-03-15 14:55:06 -0700159 SLAB_TRACE | SLAB_CONSISTENCY_CHECKS)
Glauber Costad8843922012-10-17 15:36:51 +0400160#else
161#define SLAB_DEBUG_FLAGS (0)
162#endif
163
164#if defined(CONFIG_SLAB)
165#define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \
Vladimir Davydov230e9fc2016-01-14 15:18:15 -0800166 SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -0800167 SLAB_ACCOUNT)
Glauber Costad8843922012-10-17 15:36:51 +0400168#elif defined(CONFIG_SLUB)
169#define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -0800170 SLAB_TEMPORARY | SLAB_ACCOUNT)
Glauber Costad8843922012-10-17 15:36:51 +0400171#else
Rustam Kovhaevb2e2fb62021-11-19 16:43:37 -0800172#define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE)
Glauber Costad8843922012-10-17 15:36:51 +0400173#endif
174
Thomas Garniere70954f2016-12-12 16:41:38 -0800175/* Common flags available with current configuration */
Glauber Costad8843922012-10-17 15:36:51 +0400176#define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS)
177
Thomas Garniere70954f2016-12-12 16:41:38 -0800178/* Common flags permitted for kmem_cache_create */
179#define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \
180 SLAB_RED_ZONE | \
181 SLAB_POISON | \
182 SLAB_STORE_USER | \
183 SLAB_TRACE | \
184 SLAB_CONSISTENCY_CHECKS | \
185 SLAB_MEM_SPREAD | \
186 SLAB_NOLEAKTRACE | \
187 SLAB_RECLAIM_ACCOUNT | \
188 SLAB_TEMPORARY | \
Thomas Garniere70954f2016-12-12 16:41:38 -0800189 SLAB_ACCOUNT)
190
Shakeel Buttf9e13c02018-04-05 16:21:57 -0700191bool __kmem_cache_empty(struct kmem_cache *);
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000192int __kmem_cache_shutdown(struct kmem_cache *);
Dmitry Safonov52b4b952016-02-17 13:11:37 -0800193void __kmem_cache_release(struct kmem_cache *);
Tejun Heoc9fc5862017-02-22 15:41:27 -0800194int __kmem_cache_shrink(struct kmem_cache *);
Christoph Lameter41a21282014-05-06 12:50:08 -0700195void slab_kmem_cache_release(struct kmem_cache *);
Christoph Lameter945cf2b2012-09-04 23:18:33 +0000196
Glauber Costab7454ad2012-10-19 18:20:25 +0400197struct seq_file;
198struct file;
Glauber Costab7454ad2012-10-19 18:20:25 +0400199
Glauber Costa0d7561c2012-10-19 18:20:27 +0400200struct slabinfo {
201 unsigned long active_objs;
202 unsigned long num_objs;
203 unsigned long active_slabs;
204 unsigned long num_slabs;
205 unsigned long shared_avail;
206 unsigned int limit;
207 unsigned int batchcount;
208 unsigned int shared;
209 unsigned int objects_per_slab;
210 unsigned int cache_order;
211};
212
213void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo);
214void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s);
Glauber Costab7454ad2012-10-19 18:20:25 +0400215ssize_t slabinfo_write(struct file *file, const char __user *buffer,
216 size_t count, loff_t *ppos);
Glauber Costaba6c4962012-12-18 14:22:27 -0800217
Christoph Lameter484748f2015-09-04 15:45:34 -0700218/*
219 * Generic implementation of bulk operations
220 * These are useful for situations in which the allocator cannot
Jesper Dangaard Brouer9f706d62016-03-15 14:54:03 -0700221 * perform optimizations. In that case segments of the object listed
Christoph Lameter484748f2015-09-04 15:45:34 -0700222 * may be allocated or freed using these operations.
223 */
224void __kmem_cache_free_bulk(struct kmem_cache *, size_t, void **);
Jesper Dangaard Brouer865762a2015-11-20 15:57:58 -0800225int __kmem_cache_alloc_bulk(struct kmem_cache *, gfp_t, size_t, void **);
Christoph Lameter484748f2015-09-04 15:45:34 -0700226
Roman Gushchin6cea1d52019-07-11 20:56:16 -0700227static inline int cache_vmstat_idx(struct kmem_cache *s)
228{
229 return (s->flags & SLAB_RECLAIM_ACCOUNT) ?
Roman Gushchind42f3242020-08-06 23:20:39 -0700230 NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B;
Roman Gushchin6cea1d52019-07-11 20:56:16 -0700231}
232
Vlastimil Babkae42f1742020-08-06 23:19:05 -0700233#ifdef CONFIG_SLUB_DEBUG
234#ifdef CONFIG_SLUB_DEBUG_ON
235DECLARE_STATIC_KEY_TRUE(slub_debug_enabled);
236#else
237DECLARE_STATIC_KEY_FALSE(slub_debug_enabled);
238#endif
239extern void print_tracking(struct kmem_cache *s, void *object);
Vijayanand Jittaee8d2c72021-01-12 00:42:08 +0530240extern unsigned long get_each_object_track(struct kmem_cache *s,
241 struct page *page, enum track_item alloc,
242 int (*fn)(const struct kmem_cache *, const void *,
243 const struct track *, void *), void *private);
Vijayanand Jitta14309282021-03-23 08:57:33 +0530244extern slab_flags_t slub_debug;
Marco Elver7acbce02021-07-13 13:34:14 +0800245static inline bool __slub_debug_enabled(void)
246{
247 return static_branch_unlikely(&slub_debug_enabled);
248}
Vlastimil Babkae42f1742020-08-06 23:19:05 -0700249#else
250static inline void print_tracking(struct kmem_cache *s, void *object)
251{
252}
Marco Elver7acbce02021-07-13 13:34:14 +0800253static inline bool __slub_debug_enabled(void)
254{
255 return false;
256}
Suren Baghdasaryan6a0a7052021-02-22 17:26:22 -0800257#ifdef CONFIG_SLUB
Vijayanand Jittaee8d2c72021-01-12 00:42:08 +0530258static inline unsigned long get_each_object_track(struct kmem_cache *s,
259 struct page *page, enum track_item alloc,
260 int (*fn)(const struct kmem_cache *, const void *,
261 const struct track *, void *), void *private)
262{
263 return 0;
264}
Vlastimil Babkae42f1742020-08-06 23:19:05 -0700265#endif
Suren Baghdasaryan6a0a7052021-02-22 17:26:22 -0800266#endif
Vlastimil Babkae42f1742020-08-06 23:19:05 -0700267
268/*
269 * Returns true if any of the specified slub_debug flags is enabled for the
270 * cache. Use only for flags parsed by setup_slub_debug() as it also enables
271 * the static key.
272 */
273static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags)
274{
Marco Elver7acbce02021-07-13 13:34:14 +0800275 if (IS_ENABLED(CONFIG_SLUB_DEBUG))
276 VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS));
277 if (__slub_debug_enabled())
Vlastimil Babkae42f1742020-08-06 23:19:05 -0700278 return s->flags & flags;
Vlastimil Babkae42f1742020-08-06 23:19:05 -0700279 return false;
280}
281
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700282#ifdef CONFIG_MEMCG_KMEM
Roman Gushchin286e04b2020-08-06 23:20:52 -0700283static inline struct obj_cgroup **page_obj_cgroups(struct page *page)
284{
285 /*
286 * page->mem_cgroup and page->obj_cgroups are sharing the same
287 * space. To distinguish between them in case we don't know for sure
288 * that the page is a slab page (e.g. page_cgroup_ino()), let's
289 * always set the lowest bit of obj_cgroups.
290 */
291 return (struct obj_cgroup **)
292 ((unsigned long)page->obj_cgroups & ~0x1UL);
293}
294
Roman Gushchin98556092020-08-06 23:21:10 -0700295static inline bool page_has_obj_cgroups(struct page *page)
Roman Gushchin4d96ba32019-07-11 20:56:31 -0700296{
Roman Gushchin98556092020-08-06 23:21:10 -0700297 return ((unsigned long)page->obj_cgroups & 0x1UL);
Roman Gushchin4d96ba32019-07-11 20:56:31 -0700298}
299
Roman Gushchin10befea2020-08-06 23:21:27 -0700300int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
301 gfp_t gfp);
Roman Gushchin286e04b2020-08-06 23:20:52 -0700302
303static inline void memcg_free_page_obj_cgroups(struct page *page)
304{
305 kfree(page_obj_cgroups(page));
306 page->obj_cgroups = NULL;
307}
308
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700309static inline size_t obj_full_size(struct kmem_cache *s)
310{
311 /*
312 * For each accounted object there is an extra space which is used
313 * to store obj_cgroup membership. Charge it too.
314 */
315 return s->size + sizeof(struct obj_cgroup *);
316}
317
Roman Gushchinbecaba62020-12-05 22:14:45 -0800318/*
319 * Returns false if the allocation should fail.
320 */
321static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
322 struct obj_cgroup **objcgp,
323 size_t objects, gfp_t flags)
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700324{
Roman Gushchin98556092020-08-06 23:21:10 -0700325 struct obj_cgroup *objcg;
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700326
Roman Gushchinbecaba62020-12-05 22:14:45 -0800327 if (!memcg_kmem_enabled())
328 return true;
329
330 if (!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT))
331 return true;
332
Roman Gushchin98556092020-08-06 23:21:10 -0700333 objcg = get_obj_cgroup_from_current();
334 if (!objcg)
Roman Gushchinbecaba62020-12-05 22:14:45 -0800335 return true;
Roman Gushchin98556092020-08-06 23:21:10 -0700336
337 if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s))) {
338 obj_cgroup_put(objcg);
Roman Gushchinbecaba62020-12-05 22:14:45 -0800339 return false;
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700340 }
341
Roman Gushchinbecaba62020-12-05 22:14:45 -0800342 *objcgp = objcg;
343 return true;
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700344}
345
346static inline void mod_objcg_state(struct obj_cgroup *objcg,
347 struct pglist_data *pgdat,
348 int idx, int nr)
349{
350 struct mem_cgroup *memcg;
351 struct lruvec *lruvec;
352
353 rcu_read_lock();
354 memcg = obj_cgroup_memcg(objcg);
355 lruvec = mem_cgroup_lruvec(memcg, pgdat);
356 mod_memcg_lruvec_state(lruvec, idx, nr);
357 rcu_read_unlock();
358}
359
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700360static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
361 struct obj_cgroup *objcg,
Roman Gushchin10befea2020-08-06 23:21:27 -0700362 gfp_t flags, size_t size,
363 void **p)
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700364{
365 struct page *page;
366 unsigned long off;
367 size_t i;
368
Roman Gushchinbecaba62020-12-05 22:14:45 -0800369 if (!memcg_kmem_enabled() || !objcg)
Roman Gushchin10befea2020-08-06 23:21:27 -0700370 return;
371
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700372 for (i = 0; i < size; i++) {
373 if (likely(p[i])) {
374 page = virt_to_head_page(p[i]);
Roman Gushchin10befea2020-08-06 23:21:27 -0700375
376 if (!page_has_obj_cgroups(page) &&
377 memcg_alloc_page_obj_cgroups(page, s, flags)) {
378 obj_cgroup_uncharge(objcg, obj_full_size(s));
379 continue;
380 }
381
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700382 off = obj_to_index(s, page, p[i]);
383 obj_cgroup_get(objcg);
384 page_obj_cgroups(page)[off] = objcg;
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700385 mod_objcg_state(objcg, page_pgdat(page),
386 cache_vmstat_idx(s), obj_full_size(s));
387 } else {
388 obj_cgroup_uncharge(objcg, obj_full_size(s));
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700389 }
390 }
391 obj_cgroup_put(objcg);
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700392}
393
Bharata B Raod1b2cf62020-10-13 16:53:09 -0700394static inline void memcg_slab_free_hook(struct kmem_cache *s_orig,
395 void **p, int objects)
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700396{
Bharata B Raod1b2cf62020-10-13 16:53:09 -0700397 struct kmem_cache *s;
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700398 struct obj_cgroup *objcg;
Bharata B Raod1b2cf62020-10-13 16:53:09 -0700399 struct page *page;
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700400 unsigned int off;
Bharata B Raod1b2cf62020-10-13 16:53:09 -0700401 int i;
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700402
Roman Gushchin10befea2020-08-06 23:21:27 -0700403 if (!memcg_kmem_enabled())
404 return;
405
Bharata B Raod1b2cf62020-10-13 16:53:09 -0700406 for (i = 0; i < objects; i++) {
407 if (unlikely(!p[i]))
408 continue;
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700409
Bharata B Raod1b2cf62020-10-13 16:53:09 -0700410 page = virt_to_head_page(p[i]);
411 if (!page_has_obj_cgroups(page))
412 continue;
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700413
Bharata B Raod1b2cf62020-10-13 16:53:09 -0700414 if (!s_orig)
415 s = page->slab_cache;
416 else
417 s = s_orig;
Roman Gushchin10befea2020-08-06 23:21:27 -0700418
Bharata B Raod1b2cf62020-10-13 16:53:09 -0700419 off = obj_to_index(s, page, p[i]);
420 objcg = page_obj_cgroups(page)[off];
421 if (!objcg)
422 continue;
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700423
Bharata B Raod1b2cf62020-10-13 16:53:09 -0700424 page_obj_cgroups(page)[off] = NULL;
425 obj_cgroup_uncharge(objcg, obj_full_size(s));
426 mod_objcg_state(objcg, page_pgdat(page), cache_vmstat_idx(s),
427 -obj_full_size(s));
428 obj_cgroup_put(objcg);
429 }
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700430}
431
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700432#else /* CONFIG_MEMCG_KMEM */
Roman Gushchin98556092020-08-06 23:21:10 -0700433static inline bool page_has_obj_cgroups(struct page *page)
434{
435 return false;
436}
437
438static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
Roman Gushchin4d96ba32019-07-11 20:56:31 -0700439{
440 return NULL;
441}
442
Roman Gushchin286e04b2020-08-06 23:20:52 -0700443static inline int memcg_alloc_page_obj_cgroups(struct page *page,
444 struct kmem_cache *s, gfp_t gfp)
445{
446 return 0;
447}
448
449static inline void memcg_free_page_obj_cgroups(struct page *page)
450{
451}
452
Roman Gushchinbecaba62020-12-05 22:14:45 -0800453static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
454 struct obj_cgroup **objcgp,
455 size_t objects, gfp_t flags)
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700456{
Roman Gushchinbecaba62020-12-05 22:14:45 -0800457 return true;
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700458}
459
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700460static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
461 struct obj_cgroup *objcg,
Roman Gushchin10befea2020-08-06 23:21:27 -0700462 gfp_t flags, size_t size,
463 void **p)
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700464{
465}
466
Bharata B Raod1b2cf62020-10-13 16:53:09 -0700467static inline void memcg_slab_free_hook(struct kmem_cache *s,
468 void **p, int objects)
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700469{
470}
Kirill Tkhai84c07d12018-08-17 15:47:25 -0700471#endif /* CONFIG_MEMCG_KMEM */
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800472
Kees Cooka64b5372019-07-11 20:53:26 -0700473static inline struct kmem_cache *virt_to_cache(const void *obj)
474{
475 struct page *page;
476
477 page = virt_to_head_page(obj);
478 if (WARN_ONCE(!PageSlab(page), "%s: Object is not a Slab page!\n",
479 __func__))
480 return NULL;
481 return page->slab_cache;
482}
483
Roman Gushchin74d555b2020-08-06 23:21:44 -0700484static __always_inline void account_slab_page(struct page *page, int order,
485 struct kmem_cache *s)
Roman Gushchin6cea1d52019-07-11 20:56:16 -0700486{
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700487 mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
488 PAGE_SIZE << order);
Roman Gushchin6cea1d52019-07-11 20:56:16 -0700489}
490
Roman Gushchin74d555b2020-08-06 23:21:44 -0700491static __always_inline void unaccount_slab_page(struct page *page, int order,
492 struct kmem_cache *s)
Roman Gushchin6cea1d52019-07-11 20:56:16 -0700493{
Roman Gushchin10befea2020-08-06 23:21:27 -0700494 if (memcg_kmem_enabled())
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700495 memcg_free_page_obj_cgroups(page);
Roman Gushchin98556092020-08-06 23:21:10 -0700496
Roman Gushchinf2fe7b02020-08-06 23:20:59 -0700497 mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
498 -(PAGE_SIZE << order));
Roman Gushchin6cea1d52019-07-11 20:56:16 -0700499}
500
Vlastimil Babkae42f1742020-08-06 23:19:05 -0700501static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
502{
503 struct kmem_cache *cachep;
504
505 if (!IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
Vlastimil Babkae42f1742020-08-06 23:19:05 -0700506 !kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS))
507 return s;
508
509 cachep = virt_to_cache(x);
Roman Gushchin10befea2020-08-06 23:21:27 -0700510 if (WARN(cachep && cachep != s,
Vlastimil Babkae42f1742020-08-06 23:19:05 -0700511 "%s: Wrong slab cache. %s but object is from %s\n",
512 __func__, s->name, cachep->name))
513 print_tracking(cachep, x);
514 return cachep;
515}
516
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700517static inline size_t slab_ksize(const struct kmem_cache *s)
518{
519#ifndef CONFIG_SLUB
520 return s->object_size;
521
522#else /* CONFIG_SLUB */
523# ifdef CONFIG_SLUB_DEBUG
524 /*
525 * Debugging requires use of the padding between object
526 * and whatever may come after it.
527 */
528 if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
529 return s->object_size;
530# endif
Alexander Potapenko80a92012016-07-28 15:49:07 -0700531 if (s->flags & SLAB_KASAN)
532 return s->object_size;
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700533 /*
534 * If we have the need to store the freelist pointer
535 * back there or track user information then we can
536 * only use the space before that information.
537 */
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800538 if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER))
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700539 return s->inuse;
540 /*
541 * Else we can use all the padding etc for the allocation
542 */
543 return s->size;
544#endif
545}
546
547static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700548 struct obj_cgroup **objcgp,
549 size_t size, gfp_t flags)
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700550{
551 flags &= gfp_allowed_mask;
Peter Zijlstrad92a8cf2017-03-03 10:13:38 +0100552
553 fs_reclaim_acquire(flags);
554 fs_reclaim_release(flags);
555
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700556 might_sleep_if(gfpflags_allow_blocking(flags));
557
Jesper Dangaard Brouerfab99632016-03-15 14:53:38 -0700558 if (should_failslab(s, flags))
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700559 return NULL;
560
Roman Gushchinbecaba62020-12-05 22:14:45 -0800561 if (!memcg_slab_pre_alloc_hook(s, objcgp, size, flags))
562 return NULL;
Vladimir Davydov45264772016-07-26 15:24:21 -0700563
564 return s;
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700565}
566
Roman Gushchin964d4bd2020-08-06 23:20:56 -0700567static inline void slab_post_alloc_hook(struct kmem_cache *s,
Andrey Konovalov5a7af112021-03-18 17:01:41 +1100568 struct obj_cgroup *objcg, gfp_t flags,
569 size_t size, void **p, bool init)
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700570{
571 size_t i;
572
573 flags &= gfp_allowed_mask;
Andrey Konovalov5a7af112021-03-18 17:01:41 +1100574
575 /*
576 * As memory initialization might be integrated into KASAN,
577 * kasan_slab_alloc and initialization memset must be
578 * kept together to avoid discrepancies in behavior.
579 *
580 * As p[i] might get tagged, memset and kmemleak hook come after KASAN.
581 */
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700582 for (i = 0; i < size; i++) {
Andrey Konovalov5a7af112021-03-18 17:01:41 +1100583 p[i] = kasan_slab_alloc(s, p[i], flags, init);
584 if (p[i] && init && !kasan_has_integrated_init())
585 memset(p[i], 0, s->object_size);
Andrey Konovalov53128242019-02-20 22:19:11 -0800586 kmemleak_alloc_recursive(p[i], s->object_size, 1,
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700587 s->flags, flags);
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700588 }
Vladimir Davydov45264772016-07-26 15:24:21 -0700589
Roman Gushchinbecaba62020-12-05 22:14:45 -0800590 memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
Jesper Dangaard Brouer11c7aec2016-03-15 14:53:35 -0700591}
592
Christoph Lameter44c53562014-08-06 16:04:07 -0700593#ifndef CONFIG_SLOB
Christoph Lameterca349562013-01-10 19:14:19 +0000594/*
595 * The slab lists for all objects.
596 */
597struct kmem_cache_node {
598 spinlock_t list_lock;
599
600#ifdef CONFIG_SLAB
601 struct list_head slabs_partial; /* partial list first, better asm code */
602 struct list_head slabs_full;
603 struct list_head slabs_free;
David Rientjesbf00bd32016-12-12 16:41:44 -0800604 unsigned long total_slabs; /* length of all slab lists */
605 unsigned long free_slabs; /* length of free slab list only */
Christoph Lameterca349562013-01-10 19:14:19 +0000606 unsigned long free_objects;
607 unsigned int free_limit;
608 unsigned int colour_next; /* Per-node cache coloring */
609 struct array_cache *shared; /* shared per node */
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700610 struct alien_cache **alien; /* on other nodes */
Christoph Lameterca349562013-01-10 19:14:19 +0000611 unsigned long next_reap; /* updated without locking */
612 int free_touched; /* updated without locking */
613#endif
614
615#ifdef CONFIG_SLUB
616 unsigned long nr_partial;
617 struct list_head partial;
618#ifdef CONFIG_SLUB_DEBUG
619 atomic_long_t nr_slabs;
620 atomic_long_t total_objects;
621 struct list_head full;
622#endif
623#endif
624
625};
Wanpeng Lie25839f2013-07-04 08:33:23 +0800626
Christoph Lameter44c53562014-08-06 16:04:07 -0700627static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
628{
629 return s->node[node];
630}
631
632/*
633 * Iterator over all nodes. The body will be executed for each node that has
634 * a kmem_cache_node structure allocated (which is true for all online nodes)
635 */
636#define for_each_kmem_cache_node(__s, __node, __n) \
Mikulas Patocka91635822014-10-09 15:26:20 -0700637 for (__node = 0; __node < nr_node_ids; __node++) \
638 if ((__n = get_node(__s, __node)))
Christoph Lameter44c53562014-08-06 16:04:07 -0700639
640#endif
641
Vladimir Davydov1df3b262014-12-10 15:42:16 -0800642void *slab_start(struct seq_file *m, loff_t *pos);
Wanpeng Li276a2432013-07-08 08:08:28 +0800643void *slab_next(struct seq_file *m, void *p, loff_t *pos);
644void slab_stop(struct seq_file *m, void *p);
Vladimir Davydovb0475012014-12-10 15:44:19 -0800645int memcg_slab_show(struct seq_file *m, void *p);
Andrey Ryabinin5240ab42014-08-06 16:04:14 -0700646
Yang Shi852d8be2017-11-15 17:32:07 -0800647#if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
648void dump_unreclaimable_slab(void);
649#else
650static inline void dump_unreclaimable_slab(void)
651{
652}
653#endif
654
Alexander Potapenko55834c52016-05-20 16:59:11 -0700655void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr);
656
Thomas Garnier7c00fce2016-07-26 15:21:56 -0700657#ifdef CONFIG_SLAB_FREELIST_RANDOM
658int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
659 gfp_t gfp);
660void cache_random_seq_destroy(struct kmem_cache *cachep);
661#else
662static inline int cache_random_seq_create(struct kmem_cache *cachep,
663 unsigned int count, gfp_t gfp)
664{
665 return 0;
666}
667static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { }
668#endif /* CONFIG_SLAB_FREELIST_RANDOM */
669
Alexander Potapenko64713842019-07-11 20:59:19 -0700670static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c)
671{
672 if (static_branch_unlikely(&init_on_alloc)) {
673 if (c->ctor)
674 return false;
675 if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))
676 return flags & __GFP_ZERO;
677 return true;
678 }
679 return flags & __GFP_ZERO;
680}
681
682static inline bool slab_want_init_on_free(struct kmem_cache *c)
683{
684 if (static_branch_unlikely(&init_on_free))
685 return !(c->ctor ||
686 (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)));
687 return false;
688}
689
Faiyaz Mohammed54e74122021-06-15 09:19:37 +0530690#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG)
691void debugfs_slab_release(struct kmem_cache *);
692#else
693static inline void debugfs_slab_release(struct kmem_cache *s) { }
694#endif
695
Andrey Ryabinin5240ab42014-08-06 16:04:14 -0700696#endif /* MM_SLAB_H */