]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
97d06609 CL |
2 | #ifndef MM_SLAB_H |
3 | #define MM_SLAB_H | |
4 | /* | |
5 | * Internal slab definitions | |
6 | */ | |
7 | ||
d122019b MWO |
8 | /* Reuses the bits in struct page */ |
9 | struct slab { | |
10 | unsigned long __page_flags; | |
401fb12c VB |
11 | |
12 | #if defined(CONFIG_SLAB) | |
13 | ||
130d4df5 | 14 | struct kmem_cache *slab_cache; |
d122019b | 15 | union { |
130d4df5 VB |
16 | struct { |
17 | struct list_head slab_list; | |
18 | void *freelist; /* array of free object indexes */ | |
19 | void *s_mem; /* first object */ | |
20 | }; | |
401fb12c VB |
21 | struct rcu_head rcu_head; |
22 | }; | |
401fb12c VB |
23 | unsigned int active; |
24 | ||
25 | #elif defined(CONFIG_SLUB) | |
26 | ||
401fb12c | 27 | struct kmem_cache *slab_cache; |
d122019b | 28 | union { |
401fb12c | 29 | struct { |
130d4df5 VB |
30 | union { |
31 | struct list_head slab_list; | |
32 | #ifdef CONFIG_SLUB_CPU_PARTIAL | |
33 | struct { | |
34 | struct slab *next; | |
35 | int slabs; /* Nr of slabs left */ | |
36 | }; | |
37 | #endif | |
38 | }; | |
39 | /* Double-word boundary */ | |
40 | void *freelist; /* first free object */ | |
41 | union { | |
42 | unsigned long counters; | |
43 | struct { | |
44 | unsigned inuse:16; | |
45 | unsigned objects:15; | |
46 | unsigned frozen:1; | |
47 | }; | |
48 | }; | |
d122019b | 49 | }; |
130d4df5 | 50 | struct rcu_head rcu_head; |
d122019b | 51 | }; |
401fb12c VB |
52 | unsigned int __unused; |
53 | ||
401fb12c VB |
54 | #else |
55 | #error "Unexpected slab allocator configured" | |
56 | #endif | |
d122019b | 57 | |
d122019b MWO |
58 | atomic_t __page_refcount; |
59 | #ifdef CONFIG_MEMCG | |
60 | unsigned long memcg_data; | |
61 | #endif | |
62 | }; | |
63 | ||
64 | #define SLAB_MATCH(pg, sl) \ | |
65 | static_assert(offsetof(struct page, pg) == offsetof(struct slab, sl)) | |
66 | SLAB_MATCH(flags, __page_flags); | |
130d4df5 | 67 | SLAB_MATCH(compound_head, slab_cache); /* Ensure bit 0 is clear */ |
d122019b MWO |
68 | SLAB_MATCH(_refcount, __page_refcount); |
69 | #ifdef CONFIG_MEMCG | |
70 | SLAB_MATCH(memcg_data, memcg_data); | |
71 | #endif | |
72 | #undef SLAB_MATCH | |
73 | static_assert(sizeof(struct slab) <= sizeof(struct page)); | |
130d4df5 VB |
74 | #if defined(CONFIG_HAVE_CMPXCHG_DOUBLE) && defined(CONFIG_SLUB) |
75 | static_assert(IS_ALIGNED(offsetof(struct slab, freelist), 2*sizeof(void *))); | |
76 | #endif | |
d122019b MWO |
77 | |
78 | /** | |
79 | * folio_slab - Converts from folio to slab. | |
80 | * @folio: The folio. | |
81 | * | |
82 | * Currently struct slab is a different representation of a folio where | |
83 | * folio_test_slab() is true. | |
84 | * | |
85 | * Return: The slab which contains this folio. | |
86 | */ | |
87 | #define folio_slab(folio) (_Generic((folio), \ | |
88 | const struct folio *: (const struct slab *)(folio), \ | |
89 | struct folio *: (struct slab *)(folio))) | |
90 | ||
91 | /** | |
92 | * slab_folio - The folio allocated for a slab | |
93 | * @slab: The slab. | |
94 | * | |
95 | * Slabs are allocated as folios that contain the individual objects and are | |
96 | * using some fields in the first struct page of the folio - those fields are | |
97 | * now accessed by struct slab. It is occasionally necessary to convert back to | |
98 | * a folio in order to communicate with the rest of the mm. Please use this | |
99 | * helper function instead of casting yourself, as the implementation may change | |
100 | * in the future. | |
101 | */ | |
102 | #define slab_folio(s) (_Generic((s), \ | |
103 | const struct slab *: (const struct folio *)s, \ | |
104 | struct slab *: (struct folio *)s)) | |
105 | ||
106 | /** | |
107 | * page_slab - Converts from first struct page to slab. | |
108 | * @p: The first (either head of compound or single) page of slab. | |
109 | * | |
110 | * A temporary wrapper to convert struct page to struct slab in situations where | |
111 | * we know the page is the compound head, or single order-0 page. | |
112 | * | |
113 | * Long-term ideally everything would work with struct slab directly or go | |
114 | * through folio to struct slab. | |
115 | * | |
116 | * Return: The slab which contains this page | |
117 | */ | |
118 | #define page_slab(p) (_Generic((p), \ | |
119 | const struct page *: (const struct slab *)(p), \ | |
120 | struct page *: (struct slab *)(p))) | |
121 | ||
122 | /** | |
123 | * slab_page - The first struct page allocated for a slab | |
124 | * @slab: The slab. | |
125 | * | |
126 | * A convenience wrapper for converting slab to the first struct page of the | |
127 | * underlying folio, to communicate with code not yet converted to folio or | |
128 | * struct slab. | |
129 | */ | |
130 | #define slab_page(s) folio_page(slab_folio(s), 0) | |
131 | ||
132 | /* | |
133 | * If network-based swap is enabled, sl*b must keep track of whether pages | |
134 | * were allocated from pfmemalloc reserves. | |
135 | */ | |
136 | static inline bool slab_test_pfmemalloc(const struct slab *slab) | |
137 | { | |
138 | return folio_test_active((struct folio *)slab_folio(slab)); | |
139 | } | |
140 | ||
141 | static inline void slab_set_pfmemalloc(struct slab *slab) | |
142 | { | |
143 | folio_set_active(slab_folio(slab)); | |
144 | } | |
145 | ||
146 | static inline void slab_clear_pfmemalloc(struct slab *slab) | |
147 | { | |
148 | folio_clear_active(slab_folio(slab)); | |
149 | } | |
150 | ||
151 | static inline void __slab_clear_pfmemalloc(struct slab *slab) | |
152 | { | |
153 | __folio_clear_active(slab_folio(slab)); | |
154 | } | |
155 | ||
156 | static inline void *slab_address(const struct slab *slab) | |
157 | { | |
158 | return folio_address(slab_folio(slab)); | |
159 | } | |
160 | ||
161 | static inline int slab_nid(const struct slab *slab) | |
162 | { | |
163 | return folio_nid(slab_folio(slab)); | |
164 | } | |
165 | ||
166 | static inline pg_data_t *slab_pgdat(const struct slab *slab) | |
167 | { | |
168 | return folio_pgdat(slab_folio(slab)); | |
169 | } | |
170 | ||
171 | static inline struct slab *virt_to_slab(const void *addr) | |
172 | { | |
173 | struct folio *folio = virt_to_folio(addr); | |
174 | ||
175 | if (!folio_test_slab(folio)) | |
176 | return NULL; | |
177 | ||
178 | return folio_slab(folio); | |
179 | } | |
180 | ||
181 | static inline int slab_order(const struct slab *slab) | |
182 | { | |
183 | return folio_order((struct folio *)slab_folio(slab)); | |
184 | } | |
185 | ||
186 | static inline size_t slab_size(const struct slab *slab) | |
187 | { | |
188 | return PAGE_SIZE << slab_order(slab); | |
189 | } | |
190 | ||
07f361b2 JK |
191 | #ifdef CONFIG_SLAB |
192 | #include <linux/slab_def.h> | |
193 | #endif | |
194 | ||
195 | #ifdef CONFIG_SLUB | |
196 | #include <linux/slub_def.h> | |
197 | #endif | |
198 | ||
199 | #include <linux/memcontrol.h> | |
11c7aec2 | 200 | #include <linux/fault-inject.h> |
11c7aec2 JDB |
201 | #include <linux/kasan.h> |
202 | #include <linux/kmemleak.h> | |
7c00fce9 | 203 | #include <linux/random.h> |
d92a8cfc | 204 | #include <linux/sched/mm.h> |
88f2ef73 | 205 | #include <linux/list_lru.h> |
07f361b2 | 206 | |
97d06609 CL |
207 | /* |
208 | * State of the slab allocator. | |
209 | * | |
210 | * This is used to describe the states of the allocator during bootup. | |
211 | * Allocators use this to gradually bootstrap themselves. Most allocators | |
212 | * have the problem that the structures used for managing slab caches are | |
213 | * allocated from slab caches themselves. | |
214 | */ | |
215 | enum slab_state { | |
216 | DOWN, /* No slab functionality yet */ | |
217 | PARTIAL, /* SLUB: kmem_cache_node available */ | |
ce8eb6c4 | 218 | PARTIAL_NODE, /* SLAB: kmalloc size for node struct available */ |
97d06609 CL |
219 | UP, /* Slab caches usable but not all extras yet */ |
220 | FULL /* Everything is working */ | |
221 | }; | |
222 | ||
223 | extern enum slab_state slab_state; | |
224 | ||
18004c5d CL |
225 | /* The slab cache mutex protects the management structures during changes */ |
226 | extern struct mutex slab_mutex; | |
9b030cb8 CL |
227 | |
228 | /* The list of all slab caches on the system */ | |
18004c5d CL |
229 | extern struct list_head slab_caches; |
230 | ||
9b030cb8 CL |
231 | /* The slab cache that manages slab cache information */ |
232 | extern struct kmem_cache *kmem_cache; | |
233 | ||
af3b5f87 VB |
234 | /* A table of kmalloc cache names and sizes */ |
235 | extern const struct kmalloc_info_struct { | |
cb5d9fb3 | 236 | const char *name[NR_KMALLOC_TYPES]; |
55de8b9c | 237 | unsigned int size; |
af3b5f87 VB |
238 | } kmalloc_info[]; |
239 | ||
f97d5f63 | 240 | /* Kmalloc array related functions */ |
34cc6990 | 241 | void setup_kmalloc_cache_index_table(void); |
d50112ed | 242 | void create_kmalloc_caches(slab_flags_t); |
2c59dd65 CL |
243 | |
244 | /* Find the kmalloc slab corresponding for a certain size */ | |
245 | struct kmem_cache *kmalloc_slab(size_t, gfp_t); | |
ed4cd17e HY |
246 | |
247 | void *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, | |
248 | int node, size_t orig_size, | |
249 | unsigned long caller); | |
250 | void __kmem_cache_free(struct kmem_cache *s, void *x, unsigned long caller); | |
f97d5f63 | 251 | |
44405099 | 252 | gfp_t kmalloc_fix_flags(gfp_t flags); |
f97d5f63 | 253 | |
9b030cb8 | 254 | /* Functions provided by the slab allocators */ |
d50112ed | 255 | int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags); |
97d06609 | 256 | |
55de8b9c AD |
257 | struct kmem_cache *create_kmalloc_cache(const char *name, unsigned int size, |
258 | slab_flags_t flags, unsigned int useroffset, | |
259 | unsigned int usersize); | |
45530c44 | 260 | extern void create_boot_cache(struct kmem_cache *, const char *name, |
361d575e AD |
261 | unsigned int size, slab_flags_t flags, |
262 | unsigned int useroffset, unsigned int usersize); | |
45530c44 | 263 | |
423c929c | 264 | int slab_unmergeable(struct kmem_cache *s); |
f4957d5b | 265 | struct kmem_cache *find_mergeable(unsigned size, unsigned align, |
d50112ed | 266 | slab_flags_t flags, const char *name, void (*ctor)(void *)); |
2633d7a0 | 267 | struct kmem_cache * |
f4957d5b | 268 | __kmem_cache_alias(const char *name, unsigned int size, unsigned int align, |
d50112ed | 269 | slab_flags_t flags, void (*ctor)(void *)); |
423c929c | 270 | |
0293d1fd | 271 | slab_flags_t kmem_cache_flags(unsigned int object_size, |
37540008 | 272 | slab_flags_t flags, const char *name); |
cbb79694 | 273 | |
bb944290 FT |
274 | static inline bool is_kmalloc_cache(struct kmem_cache *s) |
275 | { | |
bb944290 | 276 | return (s->flags & SLAB_KMALLOC); |
bb944290 | 277 | } |
cbb79694 | 278 | |
d8843922 | 279 | /* Legal flag mask for kmem_cache_create(), for various configurations */ |
6d6ea1e9 NB |
280 | #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \ |
281 | SLAB_CACHE_DMA32 | SLAB_PANIC | \ | |
5f0d5a3a | 282 | SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS ) |
d8843922 GC |
283 | |
284 | #if defined(CONFIG_DEBUG_SLAB) | |
285 | #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER) | |
286 | #elif defined(CONFIG_SLUB_DEBUG) | |
287 | #define SLAB_DEBUG_FLAGS (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ | |
becfda68 | 288 | SLAB_TRACE | SLAB_CONSISTENCY_CHECKS) |
d8843922 GC |
289 | #else |
290 | #define SLAB_DEBUG_FLAGS (0) | |
291 | #endif | |
292 | ||
293 | #if defined(CONFIG_SLAB) | |
294 | #define SLAB_CACHE_FLAGS (SLAB_MEM_SPREAD | SLAB_NOLEAKTRACE | \ | |
230e9fc2 | 295 | SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \ |
75f296d9 | 296 | SLAB_ACCOUNT) |
d8843922 GC |
297 | #elif defined(CONFIG_SLUB) |
298 | #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \ | |
6cd6d33c FT |
299 | SLAB_TEMPORARY | SLAB_ACCOUNT | \ |
300 | SLAB_NO_USER_FLAGS | SLAB_KMALLOC) | |
d8843922 | 301 | #else |
34dbc3aa | 302 | #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE) |
d8843922 GC |
303 | #endif |
304 | ||
e70954fd | 305 | /* Common flags available with current configuration */ |
d8843922 GC |
306 | #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS) |
307 | ||
e70954fd TG |
308 | /* Common flags permitted for kmem_cache_create */ |
309 | #define SLAB_FLAGS_PERMITTED (SLAB_CORE_FLAGS | \ | |
310 | SLAB_RED_ZONE | \ | |
311 | SLAB_POISON | \ | |
312 | SLAB_STORE_USER | \ | |
313 | SLAB_TRACE | \ | |
314 | SLAB_CONSISTENCY_CHECKS | \ | |
315 | SLAB_MEM_SPREAD | \ | |
316 | SLAB_NOLEAKTRACE | \ | |
317 | SLAB_RECLAIM_ACCOUNT | \ | |
318 | SLAB_TEMPORARY | \ | |
a285909f | 319 | SLAB_ACCOUNT | \ |
6cd6d33c | 320 | SLAB_KMALLOC | \ |
a285909f | 321 | SLAB_NO_USER_FLAGS) |
e70954fd | 322 | |
f9e13c0a | 323 | bool __kmem_cache_empty(struct kmem_cache *); |
945cf2b6 | 324 | int __kmem_cache_shutdown(struct kmem_cache *); |
52b4b950 | 325 | void __kmem_cache_release(struct kmem_cache *); |
c9fc5864 | 326 | int __kmem_cache_shrink(struct kmem_cache *); |
41a21285 | 327 | void slab_kmem_cache_release(struct kmem_cache *); |
945cf2b6 | 328 | |
b7454ad3 GC |
329 | struct seq_file; |
330 | struct file; | |
b7454ad3 | 331 | |
0d7561c6 GC |
332 | struct slabinfo { |
333 | unsigned long active_objs; | |
334 | unsigned long num_objs; | |
335 | unsigned long active_slabs; | |
336 | unsigned long num_slabs; | |
337 | unsigned long shared_avail; | |
338 | unsigned int limit; | |
339 | unsigned int batchcount; | |
340 | unsigned int shared; | |
341 | unsigned int objects_per_slab; | |
342 | unsigned int cache_order; | |
343 | }; | |
344 | ||
345 | void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo); | |
346 | void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s); | |
b7454ad3 GC |
347 | ssize_t slabinfo_write(struct file *file, const char __user *buffer, |
348 | size_t count, loff_t *ppos); | |
ba6c496e | 349 | |
1a984c4e | 350 | static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s) |
6cea1d56 RG |
351 | { |
352 | return (s->flags & SLAB_RECLAIM_ACCOUNT) ? | |
d42f3245 | 353 | NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B; |
6cea1d56 RG |
354 | } |
355 | ||
e42f174e VB |
356 | #ifdef CONFIG_SLUB_DEBUG |
357 | #ifdef CONFIG_SLUB_DEBUG_ON | |
358 | DECLARE_STATIC_KEY_TRUE(slub_debug_enabled); | |
359 | #else | |
360 | DECLARE_STATIC_KEY_FALSE(slub_debug_enabled); | |
361 | #endif | |
362 | extern void print_tracking(struct kmem_cache *s, void *object); | |
1f9f78b1 | 363 | long validate_slab_cache(struct kmem_cache *s); |
0d4a062a ME |
364 | static inline bool __slub_debug_enabled(void) |
365 | { | |
366 | return static_branch_unlikely(&slub_debug_enabled); | |
367 | } | |
e42f174e VB |
368 | #else |
369 | static inline void print_tracking(struct kmem_cache *s, void *object) | |
370 | { | |
371 | } | |
0d4a062a ME |
372 | static inline bool __slub_debug_enabled(void) |
373 | { | |
374 | return false; | |
375 | } | |
e42f174e VB |
376 | #endif |
377 | ||
378 | /* | |
379 | * Returns true if any of the specified slub_debug flags is enabled for the | |
380 | * cache. Use only for flags parsed by setup_slub_debug() as it also enables | |
381 | * the static key. | |
382 | */ | |
383 | static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t flags) | |
384 | { | |
0d4a062a ME |
385 | if (IS_ENABLED(CONFIG_SLUB_DEBUG)) |
386 | VM_WARN_ON_ONCE(!(flags & SLAB_DEBUG_FLAGS)); | |
387 | if (__slub_debug_enabled()) | |
e42f174e | 388 | return s->flags & flags; |
e42f174e VB |
389 | return false; |
390 | } | |
391 | ||
84c07d11 | 392 | #ifdef CONFIG_MEMCG_KMEM |
4b5f8d9a VB |
393 | /* |
394 | * slab_objcgs - get the object cgroups vector associated with a slab | |
395 | * @slab: a pointer to the slab struct | |
396 | * | |
397 | * Returns a pointer to the object cgroups vector associated with the slab, | |
398 | * or NULL if no such vector has been associated yet. | |
399 | */ | |
400 | static inline struct obj_cgroup **slab_objcgs(struct slab *slab) | |
401 | { | |
402 | unsigned long memcg_data = READ_ONCE(slab->memcg_data); | |
403 | ||
404 | VM_BUG_ON_PAGE(memcg_data && !(memcg_data & MEMCG_DATA_OBJCGS), | |
405 | slab_page(slab)); | |
406 | VM_BUG_ON_PAGE(memcg_data & MEMCG_DATA_KMEM, slab_page(slab)); | |
407 | ||
408 | return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK); | |
409 | } | |
410 | ||
411 | int memcg_alloc_slab_cgroups(struct slab *slab, struct kmem_cache *s, | |
412 | gfp_t gfp, bool new_slab); | |
fdbcb2a6 WL |
413 | void mod_objcg_state(struct obj_cgroup *objcg, struct pglist_data *pgdat, |
414 | enum node_stat_item idx, int nr); | |
286e04b8 | 415 | |
4b5f8d9a | 416 | static inline void memcg_free_slab_cgroups(struct slab *slab) |
286e04b8 | 417 | { |
4b5f8d9a VB |
418 | kfree(slab_objcgs(slab)); |
419 | slab->memcg_data = 0; | |
286e04b8 RG |
420 | } |
421 | ||
f2fe7b09 RG |
422 | static inline size_t obj_full_size(struct kmem_cache *s) |
423 | { | |
424 | /* | |
425 | * For each accounted object there is an extra space which is used | |
426 | * to store obj_cgroup membership. Charge it too. | |
427 | */ | |
428 | return s->size + sizeof(struct obj_cgroup *); | |
429 | } | |
430 | ||
becaba65 RG |
431 | /* |
432 | * Returns false if the allocation should fail. | |
433 | */ | |
434 | static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s, | |
88f2ef73 | 435 | struct list_lru *lru, |
becaba65 RG |
436 | struct obj_cgroup **objcgp, |
437 | size_t objects, gfp_t flags) | |
f2fe7b09 | 438 | { |
9855609b RG |
439 | struct obj_cgroup *objcg; |
440 | ||
f7a449f7 | 441 | if (!memcg_kmem_online()) |
becaba65 RG |
442 | return true; |
443 | ||
444 | if (!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT)) | |
445 | return true; | |
446 | ||
9855609b RG |
447 | objcg = get_obj_cgroup_from_current(); |
448 | if (!objcg) | |
becaba65 | 449 | return true; |
9855609b | 450 | |
88f2ef73 MS |
451 | if (lru) { |
452 | int ret; | |
453 | struct mem_cgroup *memcg; | |
454 | ||
455 | memcg = get_mem_cgroup_from_objcg(objcg); | |
456 | ret = memcg_list_lru_alloc(memcg, lru, flags); | |
457 | css_put(&memcg->css); | |
458 | ||
459 | if (ret) | |
460 | goto out; | |
f2fe7b09 RG |
461 | } |
462 | ||
88f2ef73 MS |
463 | if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s))) |
464 | goto out; | |
465 | ||
becaba65 RG |
466 | *objcgp = objcg; |
467 | return true; | |
88f2ef73 MS |
468 | out: |
469 | obj_cgroup_put(objcg); | |
470 | return false; | |
f2fe7b09 RG |
471 | } |
472 | ||
964d4bd3 RG |
473 | static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s, |
474 | struct obj_cgroup *objcg, | |
10befea9 RG |
475 | gfp_t flags, size_t size, |
476 | void **p) | |
964d4bd3 | 477 | { |
4b5f8d9a | 478 | struct slab *slab; |
964d4bd3 RG |
479 | unsigned long off; |
480 | size_t i; | |
481 | ||
f7a449f7 | 482 | if (!memcg_kmem_online() || !objcg) |
10befea9 RG |
483 | return; |
484 | ||
964d4bd3 RG |
485 | for (i = 0; i < size; i++) { |
486 | if (likely(p[i])) { | |
4b5f8d9a | 487 | slab = virt_to_slab(p[i]); |
10befea9 | 488 | |
4b5f8d9a VB |
489 | if (!slab_objcgs(slab) && |
490 | memcg_alloc_slab_cgroups(slab, s, flags, | |
2e9bd483 | 491 | false)) { |
10befea9 RG |
492 | obj_cgroup_uncharge(objcg, obj_full_size(s)); |
493 | continue; | |
494 | } | |
495 | ||
4b5f8d9a | 496 | off = obj_to_index(s, slab, p[i]); |
964d4bd3 | 497 | obj_cgroup_get(objcg); |
4b5f8d9a VB |
498 | slab_objcgs(slab)[off] = objcg; |
499 | mod_objcg_state(objcg, slab_pgdat(slab), | |
f2fe7b09 RG |
500 | cache_vmstat_idx(s), obj_full_size(s)); |
501 | } else { | |
502 | obj_cgroup_uncharge(objcg, obj_full_size(s)); | |
964d4bd3 RG |
503 | } |
504 | } | |
505 | obj_cgroup_put(objcg); | |
964d4bd3 RG |
506 | } |
507 | ||
b77d5b1b | 508 | static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, |
d1b2cf6c | 509 | void **p, int objects) |
964d4bd3 | 510 | { |
270c6a71 | 511 | struct obj_cgroup **objcgs; |
d1b2cf6c | 512 | int i; |
964d4bd3 | 513 | |
f7a449f7 | 514 | if (!memcg_kmem_online()) |
10befea9 RG |
515 | return; |
516 | ||
b77d5b1b MS |
517 | objcgs = slab_objcgs(slab); |
518 | if (!objcgs) | |
519 | return; | |
f2fe7b09 | 520 | |
b77d5b1b MS |
521 | for (i = 0; i < objects; i++) { |
522 | struct obj_cgroup *objcg; | |
523 | unsigned int off; | |
10befea9 | 524 | |
4b5f8d9a | 525 | off = obj_to_index(s, slab, p[i]); |
270c6a71 | 526 | objcg = objcgs[off]; |
d1b2cf6c BR |
527 | if (!objcg) |
528 | continue; | |
f2fe7b09 | 529 | |
270c6a71 | 530 | objcgs[off] = NULL; |
d1b2cf6c | 531 | obj_cgroup_uncharge(objcg, obj_full_size(s)); |
4b5f8d9a | 532 | mod_objcg_state(objcg, slab_pgdat(slab), cache_vmstat_idx(s), |
d1b2cf6c BR |
533 | -obj_full_size(s)); |
534 | obj_cgroup_put(objcg); | |
535 | } | |
964d4bd3 RG |
536 | } |
537 | ||
84c07d11 | 538 | #else /* CONFIG_MEMCG_KMEM */ |
4b5f8d9a VB |
539 | static inline struct obj_cgroup **slab_objcgs(struct slab *slab) |
540 | { | |
541 | return NULL; | |
542 | } | |
543 | ||
9855609b | 544 | static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr) |
4d96ba35 RG |
545 | { |
546 | return NULL; | |
547 | } | |
548 | ||
4b5f8d9a | 549 | static inline int memcg_alloc_slab_cgroups(struct slab *slab, |
2e9bd483 | 550 | struct kmem_cache *s, gfp_t gfp, |
4b5f8d9a | 551 | bool new_slab) |
286e04b8 RG |
552 | { |
553 | return 0; | |
554 | } | |
555 | ||
4b5f8d9a | 556 | static inline void memcg_free_slab_cgroups(struct slab *slab) |
286e04b8 RG |
557 | { |
558 | } | |
559 | ||
becaba65 | 560 | static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s, |
88f2ef73 | 561 | struct list_lru *lru, |
becaba65 RG |
562 | struct obj_cgroup **objcgp, |
563 | size_t objects, gfp_t flags) | |
f2fe7b09 | 564 | { |
becaba65 | 565 | return true; |
f2fe7b09 RG |
566 | } |
567 | ||
964d4bd3 RG |
568 | static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s, |
569 | struct obj_cgroup *objcg, | |
10befea9 RG |
570 | gfp_t flags, size_t size, |
571 | void **p) | |
964d4bd3 RG |
572 | { |
573 | } | |
574 | ||
b77d5b1b | 575 | static inline void memcg_slab_free_hook(struct kmem_cache *s, struct slab *slab, |
d1b2cf6c | 576 | void **p, int objects) |
964d4bd3 RG |
577 | { |
578 | } | |
84c07d11 | 579 | #endif /* CONFIG_MEMCG_KMEM */ |
b9ce5ef4 | 580 | |
a64b5378 KC |
581 | static inline struct kmem_cache *virt_to_cache(const void *obj) |
582 | { | |
82c1775d | 583 | struct slab *slab; |
a64b5378 | 584 | |
82c1775d MWO |
585 | slab = virt_to_slab(obj); |
586 | if (WARN_ONCE(!slab, "%s: Object is not a Slab page!\n", | |
a64b5378 KC |
587 | __func__)) |
588 | return NULL; | |
82c1775d | 589 | return slab->slab_cache; |
a64b5378 KC |
590 | } |
591 | ||
b918653b MWO |
592 | static __always_inline void account_slab(struct slab *slab, int order, |
593 | struct kmem_cache *s, gfp_t gfp) | |
6cea1d56 | 594 | { |
f7a449f7 | 595 | if (memcg_kmem_online() && (s->flags & SLAB_ACCOUNT)) |
4b5f8d9a | 596 | memcg_alloc_slab_cgroups(slab, s, gfp, true); |
2e9bd483 | 597 | |
b918653b | 598 | mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s), |
f2fe7b09 | 599 | PAGE_SIZE << order); |
6cea1d56 RG |
600 | } |
601 | ||
b918653b MWO |
602 | static __always_inline void unaccount_slab(struct slab *slab, int order, |
603 | struct kmem_cache *s) | |
6cea1d56 | 604 | { |
f7a449f7 | 605 | if (memcg_kmem_online()) |
4b5f8d9a | 606 | memcg_free_slab_cgroups(slab); |
9855609b | 607 | |
b918653b | 608 | mod_node_page_state(slab_pgdat(slab), cache_vmstat_idx(s), |
f2fe7b09 | 609 | -(PAGE_SIZE << order)); |
6cea1d56 RG |
610 | } |
611 | ||
e42f174e VB |
612 | static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x) |
613 | { | |
614 | struct kmem_cache *cachep; | |
615 | ||
616 | if (!IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) && | |
e42f174e VB |
617 | !kmem_cache_debug_flags(s, SLAB_CONSISTENCY_CHECKS)) |
618 | return s; | |
619 | ||
620 | cachep = virt_to_cache(x); | |
10befea9 | 621 | if (WARN(cachep && cachep != s, |
e42f174e VB |
622 | "%s: Wrong slab cache. %s but object is from %s\n", |
623 | __func__, s->name, cachep->name)) | |
624 | print_tracking(cachep, x); | |
625 | return cachep; | |
626 | } | |
d6a71648 HY |
627 | |
628 | void free_large_kmalloc(struct folio *folio, void *object); | |
629 | ||
8dfa9d55 HY |
630 | size_t __ksize(const void *objp); |
631 | ||
11c7aec2 JDB |
632 | static inline size_t slab_ksize(const struct kmem_cache *s) |
633 | { | |
634 | #ifndef CONFIG_SLUB | |
635 | return s->object_size; | |
636 | ||
637 | #else /* CONFIG_SLUB */ | |
638 | # ifdef CONFIG_SLUB_DEBUG | |
639 | /* | |
640 | * Debugging requires use of the padding between object | |
641 | * and whatever may come after it. | |
642 | */ | |
643 | if (s->flags & (SLAB_RED_ZONE | SLAB_POISON)) | |
644 | return s->object_size; | |
645 | # endif | |
80a9201a AP |
646 | if (s->flags & SLAB_KASAN) |
647 | return s->object_size; | |
11c7aec2 JDB |
648 | /* |
649 | * If we have the need to store the freelist pointer | |
650 | * back there or track user information then we can | |
651 | * only use the space before that information. | |
652 | */ | |
5f0d5a3a | 653 | if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER)) |
11c7aec2 JDB |
654 | return s->inuse; |
655 | /* | |
656 | * Else we can use all the padding etc for the allocation | |
657 | */ | |
658 | return s->size; | |
659 | #endif | |
660 | } | |
661 | ||
662 | static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s, | |
88f2ef73 | 663 | struct list_lru *lru, |
964d4bd3 RG |
664 | struct obj_cgroup **objcgp, |
665 | size_t size, gfp_t flags) | |
11c7aec2 JDB |
666 | { |
667 | flags &= gfp_allowed_mask; | |
d92a8cfc | 668 | |
95d6c701 | 669 | might_alloc(flags); |
11c7aec2 | 670 | |
fab9963a | 671 | if (should_failslab(s, flags)) |
11c7aec2 JDB |
672 | return NULL; |
673 | ||
88f2ef73 | 674 | if (!memcg_slab_pre_alloc_hook(s, lru, objcgp, size, flags)) |
becaba65 | 675 | return NULL; |
45264778 VD |
676 | |
677 | return s; | |
11c7aec2 JDB |
678 | } |
679 | ||
964d4bd3 | 680 | static inline void slab_post_alloc_hook(struct kmem_cache *s, |
da844b78 | 681 | struct obj_cgroup *objcg, gfp_t flags, |
9ce67395 FT |
682 | size_t size, void **p, bool init, |
683 | unsigned int orig_size) | |
11c7aec2 | 684 | { |
9ce67395 | 685 | unsigned int zero_size = s->object_size; |
11c7aec2 JDB |
686 | size_t i; |
687 | ||
688 | flags &= gfp_allowed_mask; | |
da844b78 | 689 | |
9ce67395 FT |
690 | /* |
691 | * For kmalloc object, the allocated memory size(object_size) is likely | |
692 | * larger than the requested size(orig_size). If redzone check is | |
693 | * enabled for the extra space, don't zero it, as it will be redzoned | |
694 | * soon. The redzone operation for this extra space could be seen as a | |
695 | * replacement of current poisoning under certain debug option, and | |
696 | * won't break other sanity checks. | |
697 | */ | |
698 | if (kmem_cache_debug_flags(s, SLAB_STORE_USER | SLAB_RED_ZONE) && | |
699 | (s->flags & SLAB_KMALLOC)) | |
700 | zero_size = orig_size; | |
701 | ||
da844b78 AK |
702 | /* |
703 | * As memory initialization might be integrated into KASAN, | |
704 | * kasan_slab_alloc and initialization memset must be | |
705 | * kept together to avoid discrepancies in behavior. | |
706 | * | |
707 | * As p[i] might get tagged, memset and kmemleak hook come after KASAN. | |
708 | */ | |
11c7aec2 | 709 | for (i = 0; i < size; i++) { |
da844b78 AK |
710 | p[i] = kasan_slab_alloc(s, p[i], flags, init); |
711 | if (p[i] && init && !kasan_has_integrated_init()) | |
9ce67395 | 712 | memset(p[i], 0, zero_size); |
53128245 | 713 | kmemleak_alloc_recursive(p[i], s->object_size, 1, |
11c7aec2 | 714 | s->flags, flags); |
68ef169a | 715 | kmsan_slab_alloc(s, p[i], flags); |
11c7aec2 | 716 | } |
45264778 | 717 | |
becaba65 | 718 | memcg_slab_post_alloc_hook(s, objcg, flags, size, p); |
11c7aec2 JDB |
719 | } |
720 | ||
ca34956b CL |
721 | /* |
722 | * The slab lists for all objects. | |
723 | */ | |
724 | struct kmem_cache_node { | |
ca34956b | 725 | #ifdef CONFIG_SLAB |
b539ce9f | 726 | raw_spinlock_t list_lock; |
ca34956b CL |
727 | struct list_head slabs_partial; /* partial list first, better asm code */ |
728 | struct list_head slabs_full; | |
729 | struct list_head slabs_free; | |
bf00bd34 DR |
730 | unsigned long total_slabs; /* length of all slab lists */ |
731 | unsigned long free_slabs; /* length of free slab list only */ | |
ca34956b CL |
732 | unsigned long free_objects; |
733 | unsigned int free_limit; | |
734 | unsigned int colour_next; /* Per-node cache coloring */ | |
735 | struct array_cache *shared; /* shared per node */ | |
c8522a3a | 736 | struct alien_cache **alien; /* on other nodes */ |
ca34956b CL |
737 | unsigned long next_reap; /* updated without locking */ |
738 | int free_touched; /* updated without locking */ | |
739 | #endif | |
740 | ||
741 | #ifdef CONFIG_SLUB | |
b539ce9f | 742 | spinlock_t list_lock; |
ca34956b CL |
743 | unsigned long nr_partial; |
744 | struct list_head partial; | |
745 | #ifdef CONFIG_SLUB_DEBUG | |
746 | atomic_long_t nr_slabs; | |
747 | atomic_long_t total_objects; | |
748 | struct list_head full; | |
749 | #endif | |
750 | #endif | |
751 | ||
752 | }; | |
e25839f6 | 753 | |
44c5356f CL |
754 | static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node) |
755 | { | |
756 | return s->node[node]; | |
757 | } | |
758 | ||
759 | /* | |
760 | * Iterator over all nodes. The body will be executed for each node that has | |
761 | * a kmem_cache_node structure allocated (which is true for all online nodes) | |
762 | */ | |
763 | #define for_each_kmem_cache_node(__s, __node, __n) \ | |
9163582c MP |
764 | for (__node = 0; __node < nr_node_ids; __node++) \ |
765 | if ((__n = get_node(__s, __node))) | |
44c5356f | 766 | |
44c5356f | 767 | |
852d8be0 YS |
768 | #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG) |
769 | void dump_unreclaimable_slab(void); | |
770 | #else | |
771 | static inline void dump_unreclaimable_slab(void) | |
772 | { | |
773 | } | |
774 | #endif | |
775 | ||
55834c59 AP |
776 | void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr); |
777 | ||
7c00fce9 TG |
778 | #ifdef CONFIG_SLAB_FREELIST_RANDOM |
779 | int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count, | |
780 | gfp_t gfp); | |
781 | void cache_random_seq_destroy(struct kmem_cache *cachep); | |
782 | #else | |
783 | static inline int cache_random_seq_create(struct kmem_cache *cachep, | |
784 | unsigned int count, gfp_t gfp) | |
785 | { | |
786 | return 0; | |
787 | } | |
788 | static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { } | |
789 | #endif /* CONFIG_SLAB_FREELIST_RANDOM */ | |
790 | ||
6471384a AP |
791 | static inline bool slab_want_init_on_alloc(gfp_t flags, struct kmem_cache *c) |
792 | { | |
51cba1eb KC |
793 | if (static_branch_maybe(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, |
794 | &init_on_alloc)) { | |
6471384a AP |
795 | if (c->ctor) |
796 | return false; | |
797 | if (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON)) | |
798 | return flags & __GFP_ZERO; | |
799 | return true; | |
800 | } | |
801 | return flags & __GFP_ZERO; | |
802 | } | |
803 | ||
804 | static inline bool slab_want_init_on_free(struct kmem_cache *c) | |
805 | { | |
51cba1eb KC |
806 | if (static_branch_maybe(CONFIG_INIT_ON_FREE_DEFAULT_ON, |
807 | &init_on_free)) | |
6471384a AP |
808 | return !(c->ctor || |
809 | (c->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_POISON))); | |
810 | return false; | |
811 | } | |
812 | ||
64dd6849 FM |
813 | #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_SLUB_DEBUG) |
814 | void debugfs_slab_release(struct kmem_cache *); | |
815 | #else | |
816 | static inline void debugfs_slab_release(struct kmem_cache *s) { } | |
817 | #endif | |
818 | ||
5bb1bb35 | 819 | #ifdef CONFIG_PRINTK |
8e7f37f2 PM |
820 | #define KS_ADDRS_COUNT 16 |
821 | struct kmem_obj_info { | |
822 | void *kp_ptr; | |
7213230a | 823 | struct slab *kp_slab; |
8e7f37f2 PM |
824 | void *kp_objp; |
825 | unsigned long kp_data_offset; | |
826 | struct kmem_cache *kp_slab_cache; | |
827 | void *kp_ret; | |
828 | void *kp_stack[KS_ADDRS_COUNT]; | |
e548eaa1 | 829 | void *kp_free_stack[KS_ADDRS_COUNT]; |
8e7f37f2 | 830 | }; |
2dfe63e6 | 831 | void __kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab); |
5bb1bb35 | 832 | #endif |
8e7f37f2 | 833 | |
0b3eb091 MWO |
834 | #ifdef CONFIG_HAVE_HARDENED_USERCOPY_ALLOCATOR |
835 | void __check_heap_object(const void *ptr, unsigned long n, | |
836 | const struct slab *slab, bool to_user); | |
837 | #else | |
838 | static inline | |
839 | void __check_heap_object(const void *ptr, unsigned long n, | |
840 | const struct slab *slab, bool to_user) | |
841 | { | |
842 | } | |
843 | #endif | |
844 | ||
946fa0db FT |
845 | #ifdef CONFIG_SLUB_DEBUG |
846 | void skip_orig_size_check(struct kmem_cache *s, const void *object); | |
847 | #endif | |
848 | ||
5240ab40 | 849 | #endif /* MM_SLAB_H */ |