Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 2 | #ifndef MM_SLAB_H |
| 3 | #define MM_SLAB_H |
| 4 | /* |
| 5 | * Internal slab definitions |
| 6 | */ |
| 7 | |
Joonsoo Kim | 07f361b | 2014-10-09 15:26:00 -0700 | [diff] [blame] | 8 | #ifdef CONFIG_SLOB |
| 9 | /* |
| 10 | * Common fields provided in kmem_cache by all slab allocators |
| 11 | * This struct is either used directly by the allocator (SLOB) |
| 12 | * or the allocator must include definitions for all fields |
| 13 | * provided in kmem_cache_common in their definition of kmem_cache. |
| 14 | * |
| 15 | * Once we can do anonymous structs (C11 standard) we could put a |
| 16 | * anonymous struct definition in these allocators so that the |
| 17 | * separate allocations in the kmem_cache structure of SLAB and |
| 18 | * SLUB is no longer needed. |
| 19 | */ |
| 20 | struct kmem_cache { |
| 21 | unsigned int object_size;/* The original size of the object */ |
| 22 | unsigned int size; /* The aligned/padded/added on size */ |
| 23 | unsigned int align; /* Alignment as calculated */ |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 24 | slab_flags_t flags; /* Active flags on the slab */ |
Alexey Dobriyan | 7bbdb81 | 2018-04-05 16:21:31 -0700 | [diff] [blame] | 25 | unsigned int useroffset;/* Usercopy region offset */ |
| 26 | unsigned int usersize; /* Usercopy region size */ |
Joonsoo Kim | 07f361b | 2014-10-09 15:26:00 -0700 | [diff] [blame] | 27 | const char *name; /* Slab name for sysfs */ |
| 28 | int refcount; /* Use counter */ |
| 29 | void (*ctor)(void *); /* Called on object slot creation */ |
| 30 | struct list_head list; /* List of all slab caches on the system */ |
| 31 | }; |
| 32 | |
Waiman Long | 9adeaa2 | 2019-09-23 15:33:49 -0700 | [diff] [blame] | 33 | #else /* !CONFIG_SLOB */ |
| 34 | |
Waiman Long | 9adeaa2 | 2019-09-23 15:33:49 -0700 | [diff] [blame] | 35 | /* |
| 36 | * This is the main placeholder for memcg-related information in kmem caches. |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 37 | * 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 Long | 9adeaa2 | 2019-09-23 15:33:49 -0700 | [diff] [blame] | 39 | * |
| 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 Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 45 | * @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 Long | 9adeaa2 | 2019-09-23 15:33:49 -0700 | [diff] [blame] | 48 | */ |
| 49 | struct memcg_cache_params { |
| 50 | struct kmem_cache *root_cache; |
Waiman Long | 9adeaa2 | 2019-09-23 15:33:49 -0700 | [diff] [blame] | 51 | |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 52 | struct kmem_cache *memcg_cache; |
| 53 | struct list_head __root_caches_node; |
Waiman Long | 9adeaa2 | 2019-09-23 15:33:49 -0700 | [diff] [blame] | 54 | }; |
Joonsoo Kim | 07f361b | 2014-10-09 15:26:00 -0700 | [diff] [blame] | 55 | #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 Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 66 | #include <linux/fault-inject.h> |
Jesper Dangaard Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 67 | #include <linux/kasan.h> |
| 68 | #include <linux/kmemleak.h> |
Thomas Garnier | 7c00fce | 2016-07-26 15:21:56 -0700 | [diff] [blame] | 69 | #include <linux/random.h> |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 70 | #include <linux/sched/mm.h> |
Roman Gushchin | 286e04b | 2020-08-06 23:20:52 -0700 | [diff] [blame] | 71 | #include <linux/kmemleak.h> |
Joonsoo Kim | 07f361b | 2014-10-09 15:26:00 -0700 | [diff] [blame] | 72 | |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 73 | /* |
| 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 | */ |
| 81 | enum slab_state { |
| 82 | DOWN, /* No slab functionality yet */ |
| 83 | PARTIAL, /* SLUB: kmem_cache_node available */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 84 | PARTIAL_NODE, /* SLAB: kmalloc size for node struct available */ |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 85 | UP, /* Slab caches usable but not all extras yet */ |
| 86 | FULL /* Everything is working */ |
| 87 | }; |
| 88 | |
| 89 | extern enum slab_state slab_state; |
| 90 | |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 91 | /* The slab cache mutex protects the management structures during changes */ |
| 92 | extern struct mutex slab_mutex; |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 93 | |
| 94 | /* The list of all slab caches on the system */ |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 95 | extern struct list_head slab_caches; |
| 96 | |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 97 | /* The slab cache that manages slab cache information */ |
| 98 | extern struct kmem_cache *kmem_cache; |
| 99 | |
Vlastimil Babka | af3b5f8 | 2017-02-22 15:41:05 -0800 | [diff] [blame] | 100 | /* A table of kmalloc cache names and sizes */ |
| 101 | extern const struct kmalloc_info_struct { |
Pengfei Li | cb5d9fb | 2019-11-30 17:49:21 -0800 | [diff] [blame] | 102 | const char *name[NR_KMALLOC_TYPES]; |
Alexey Dobriyan | 55de8b9 | 2018-04-05 16:20:29 -0700 | [diff] [blame] | 103 | unsigned int size; |
Vlastimil Babka | af3b5f8 | 2017-02-22 15:41:05 -0800 | [diff] [blame] | 104 | } kmalloc_info[]; |
| 105 | |
Christoph Lameter | f97d5f6 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 106 | #ifndef CONFIG_SLOB |
| 107 | /* Kmalloc array related functions */ |
Daniel Sanders | 34cc699 | 2015-06-24 16:55:57 -0700 | [diff] [blame] | 108 | void setup_kmalloc_cache_index_table(void); |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 109 | void create_kmalloc_caches(slab_flags_t); |
Christoph Lameter | 2c59dd6 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 110 | |
| 111 | /* Find the kmalloc slab corresponding for a certain size */ |
| 112 | struct kmem_cache *kmalloc_slab(size_t, gfp_t); |
Christoph Lameter | f97d5f6 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 113 | #endif |
| 114 | |
Long Li | 4440509 | 2020-08-06 23:18:28 -0700 | [diff] [blame] | 115 | gfp_t kmalloc_fix_flags(gfp_t flags); |
Christoph Lameter | f97d5f6 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 116 | |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 117 | /* Functions provided by the slab allocators */ |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 118 | int __kmem_cache_create(struct kmem_cache *, slab_flags_t flags); |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 119 | |
Alexey Dobriyan | 55de8b9 | 2018-04-05 16:20:29 -0700 | [diff] [blame] | 120 | struct kmem_cache *create_kmalloc_cache(const char *name, unsigned int size, |
| 121 | slab_flags_t flags, unsigned int useroffset, |
| 122 | unsigned int usersize); |
Christoph Lameter | 45530c4 | 2012-11-28 16:23:07 +0000 | [diff] [blame] | 123 | extern void create_boot_cache(struct kmem_cache *, const char *name, |
Alexey Dobriyan | 361d575 | 2018-04-05 16:20:33 -0700 | [diff] [blame] | 124 | unsigned int size, slab_flags_t flags, |
| 125 | unsigned int useroffset, unsigned int usersize); |
Christoph Lameter | 45530c4 | 2012-11-28 16:23:07 +0000 | [diff] [blame] | 126 | |
Joonsoo Kim | 423c929 | 2014-10-09 15:26:22 -0700 | [diff] [blame] | 127 | int slab_unmergeable(struct kmem_cache *s); |
Alexey Dobriyan | f4957d5 | 2018-04-05 16:20:37 -0700 | [diff] [blame] | 128 | struct kmem_cache *find_mergeable(unsigned size, unsigned align, |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 129 | slab_flags_t flags, const char *name, void (*ctor)(void *)); |
Joonsoo Kim | 12220de | 2014-10-09 15:26:24 -0700 | [diff] [blame] | 130 | #ifndef CONFIG_SLOB |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 131 | struct kmem_cache * |
Alexey Dobriyan | f4957d5 | 2018-04-05 16:20:37 -0700 | [diff] [blame] | 132 | __kmem_cache_alias(const char *name, unsigned int size, unsigned int align, |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 133 | slab_flags_t flags, void (*ctor)(void *)); |
Joonsoo Kim | 423c929 | 2014-10-09 15:26:22 -0700 | [diff] [blame] | 134 | |
Alexey Dobriyan | 0293d1f | 2018-04-05 16:21:24 -0700 | [diff] [blame] | 135 | slab_flags_t kmem_cache_flags(unsigned int object_size, |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 136 | slab_flags_t flags, const char *name, |
Joonsoo Kim | 423c929 | 2014-10-09 15:26:22 -0700 | [diff] [blame] | 137 | void (*ctor)(void *)); |
Christoph Lameter | cbb7969 | 2012-09-05 00:18:32 +0000 | [diff] [blame] | 138 | #else |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 139 | static inline struct kmem_cache * |
Alexey Dobriyan | f4957d5 | 2018-04-05 16:20:37 -0700 | [diff] [blame] | 140 | __kmem_cache_alias(const char *name, unsigned int size, unsigned int align, |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 141 | slab_flags_t flags, void (*ctor)(void *)) |
Christoph Lameter | cbb7969 | 2012-09-05 00:18:32 +0000 | [diff] [blame] | 142 | { return NULL; } |
Joonsoo Kim | 423c929 | 2014-10-09 15:26:22 -0700 | [diff] [blame] | 143 | |
Alexey Dobriyan | 0293d1f | 2018-04-05 16:21:24 -0700 | [diff] [blame] | 144 | static inline slab_flags_t kmem_cache_flags(unsigned int object_size, |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 145 | slab_flags_t flags, const char *name, |
Joonsoo Kim | 423c929 | 2014-10-09 15:26:22 -0700 | [diff] [blame] | 146 | void (*ctor)(void *)) |
| 147 | { |
| 148 | return flags; |
| 149 | } |
Christoph Lameter | cbb7969 | 2012-09-05 00:18:32 +0000 | [diff] [blame] | 150 | #endif |
| 151 | |
| 152 | |
Glauber Costa | d884392 | 2012-10-17 15:36:51 +0400 | [diff] [blame] | 153 | /* Legal flag mask for kmem_cache_create(), for various configurations */ |
Nicolas Boichat | 6d6ea1e | 2019-03-28 20:43:42 -0700 | [diff] [blame] | 154 | #define SLAB_CORE_FLAGS (SLAB_HWCACHE_ALIGN | SLAB_CACHE_DMA | \ |
| 155 | SLAB_CACHE_DMA32 | SLAB_PANIC | \ |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 156 | SLAB_TYPESAFE_BY_RCU | SLAB_DEBUG_OBJECTS ) |
Glauber Costa | d884392 | 2012-10-17 15:36:51 +0400 | [diff] [blame] | 157 | |
| 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 Abbott | becfda6 | 2016-03-15 14:55:06 -0700 | [diff] [blame] | 162 | SLAB_TRACE | SLAB_CONSISTENCY_CHECKS) |
Glauber Costa | d884392 | 2012-10-17 15:36:51 +0400 | [diff] [blame] | 163 | #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 Davydov | 230e9fc | 2016-01-14 15:18:15 -0800 | [diff] [blame] | 169 | SLAB_RECLAIM_ACCOUNT | SLAB_TEMPORARY | \ |
Levin, Alexander (Sasha Levin) | 75f296d | 2017-11-15 17:35:54 -0800 | [diff] [blame] | 170 | SLAB_ACCOUNT) |
Glauber Costa | d884392 | 2012-10-17 15:36:51 +0400 | [diff] [blame] | 171 | #elif defined(CONFIG_SLUB) |
| 172 | #define SLAB_CACHE_FLAGS (SLAB_NOLEAKTRACE | SLAB_RECLAIM_ACCOUNT | \ |
Levin, Alexander (Sasha Levin) | 75f296d | 2017-11-15 17:35:54 -0800 | [diff] [blame] | 173 | SLAB_TEMPORARY | SLAB_ACCOUNT) |
Glauber Costa | d884392 | 2012-10-17 15:36:51 +0400 | [diff] [blame] | 174 | #else |
| 175 | #define SLAB_CACHE_FLAGS (0) |
| 176 | #endif |
| 177 | |
Thomas Garnier | e70954f | 2016-12-12 16:41:38 -0800 | [diff] [blame] | 178 | /* Common flags available with current configuration */ |
Glauber Costa | d884392 | 2012-10-17 15:36:51 +0400 | [diff] [blame] | 179 | #define CACHE_CREATE_MASK (SLAB_CORE_FLAGS | SLAB_DEBUG_FLAGS | SLAB_CACHE_FLAGS) |
| 180 | |
Thomas Garnier | e70954f | 2016-12-12 16:41:38 -0800 | [diff] [blame] | 181 | /* 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 Garnier | e70954f | 2016-12-12 16:41:38 -0800 | [diff] [blame] | 192 | SLAB_ACCOUNT) |
| 193 | |
Shakeel Butt | f9e13c0 | 2018-04-05 16:21:57 -0700 | [diff] [blame] | 194 | bool __kmem_cache_empty(struct kmem_cache *); |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 195 | int __kmem_cache_shutdown(struct kmem_cache *); |
Dmitry Safonov | 52b4b95 | 2016-02-17 13:11:37 -0800 | [diff] [blame] | 196 | void __kmem_cache_release(struct kmem_cache *); |
Tejun Heo | c9fc586 | 2017-02-22 15:41:27 -0800 | [diff] [blame] | 197 | int __kmem_cache_shrink(struct kmem_cache *); |
Christoph Lameter | 41a2128 | 2014-05-06 12:50:08 -0700 | [diff] [blame] | 198 | void slab_kmem_cache_release(struct kmem_cache *); |
Waiman Long | 04f768a | 2019-09-23 15:33:46 -0700 | [diff] [blame] | 199 | void kmem_cache_shrink_all(struct kmem_cache *s); |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 200 | |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 201 | struct seq_file; |
| 202 | struct file; |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 203 | |
Glauber Costa | 0d7561c | 2012-10-19 18:20:27 +0400 | [diff] [blame] | 204 | struct 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 | |
| 217 | void get_slabinfo(struct kmem_cache *s, struct slabinfo *sinfo); |
| 218 | void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *s); |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 219 | ssize_t slabinfo_write(struct file *file, const char __user *buffer, |
| 220 | size_t count, loff_t *ppos); |
Glauber Costa | ba6c496 | 2012-12-18 14:22:27 -0800 | [diff] [blame] | 221 | |
Christoph Lameter | 484748f | 2015-09-04 15:45:34 -0700 | [diff] [blame] | 222 | /* |
| 223 | * Generic implementation of bulk operations |
| 224 | * These are useful for situations in which the allocator cannot |
Jesper Dangaard Brouer | 9f706d6 | 2016-03-15 14:54:03 -0700 | [diff] [blame] | 225 | * perform optimizations. In that case segments of the object listed |
Christoph Lameter | 484748f | 2015-09-04 15:45:34 -0700 | [diff] [blame] | 226 | * may be allocated or freed using these operations. |
| 227 | */ |
| 228 | void __kmem_cache_free_bulk(struct kmem_cache *, size_t, void **); |
Jesper Dangaard Brouer | 865762a | 2015-11-20 15:57:58 -0800 | [diff] [blame] | 229 | int __kmem_cache_alloc_bulk(struct kmem_cache *, gfp_t, size_t, void **); |
Christoph Lameter | 484748f | 2015-09-04 15:45:34 -0700 | [diff] [blame] | 230 | |
Roman Gushchin | 6cea1d5 | 2019-07-11 20:56:16 -0700 | [diff] [blame] | 231 | static inline int cache_vmstat_idx(struct kmem_cache *s) |
| 232 | { |
| 233 | return (s->flags & SLAB_RECLAIM_ACCOUNT) ? |
Roman Gushchin | d42f324 | 2020-08-06 23:20:39 -0700 | [diff] [blame] | 234 | NR_SLAB_RECLAIMABLE_B : NR_SLAB_UNRECLAIMABLE_B; |
Roman Gushchin | 6cea1d5 | 2019-07-11 20:56:16 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Vlastimil Babka | e42f174 | 2020-08-06 23:19:05 -0700 | [diff] [blame] | 237 | #ifdef CONFIG_SLUB_DEBUG |
| 238 | #ifdef CONFIG_SLUB_DEBUG_ON |
| 239 | DECLARE_STATIC_KEY_TRUE(slub_debug_enabled); |
| 240 | #else |
| 241 | DECLARE_STATIC_KEY_FALSE(slub_debug_enabled); |
| 242 | #endif |
| 243 | extern void print_tracking(struct kmem_cache *s, void *object); |
| 244 | #else |
| 245 | static 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 | */ |
| 255 | static 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 Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 265 | #ifdef CONFIG_MEMCG_KMEM |
Tejun Heo | 510ded3 | 2017-02-22 15:41:24 -0800 | [diff] [blame] | 266 | |
| 267 | /* List of all root caches. */ |
| 268 | extern struct list_head slab_root_caches; |
| 269 | #define root_caches_node memcg_params.__root_caches_node |
| 270 | |
Glauber Costa | ba6c496 | 2012-12-18 14:22:27 -0800 | [diff] [blame] | 271 | static inline bool is_root_cache(struct kmem_cache *s) |
| 272 | { |
Tejun Heo | 9eeadc8 | 2017-02-22 15:41:17 -0800 | [diff] [blame] | 273 | return !s->memcg_params.root_cache; |
Glauber Costa | ba6c496 | 2012-12-18 14:22:27 -0800 | [diff] [blame] | 274 | } |
Glauber Costa | 2633d7a | 2012-12-18 14:22:34 -0800 | [diff] [blame] | 275 | |
Glauber Costa | b9ce5ef | 2012-12-18 14:22:46 -0800 | [diff] [blame] | 276 | static inline bool slab_equal_or_root(struct kmem_cache *s, |
Vladimir Davydov | f7ce319 | 2015-02-12 14:59:20 -0800 | [diff] [blame] | 277 | struct kmem_cache *p) |
Glauber Costa | b9ce5ef | 2012-12-18 14:22:46 -0800 | [diff] [blame] | 278 | { |
Vladimir Davydov | f7ce319 | 2015-02-12 14:59:20 -0800 | [diff] [blame] | 279 | return p == s || p == s->memcg_params.root_cache; |
Glauber Costa | b9ce5ef | 2012-12-18 14:22:46 -0800 | [diff] [blame] | 280 | } |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 281 | |
| 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 | */ |
| 287 | static inline const char *cache_name(struct kmem_cache *s) |
| 288 | { |
| 289 | if (!is_root_cache(s)) |
Vladimir Davydov | f7ce319 | 2015-02-12 14:59:20 -0800 | [diff] [blame] | 290 | s = s->memcg_params.root_cache; |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 291 | return s->name; |
| 292 | } |
| 293 | |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 294 | static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s) |
| 295 | { |
| 296 | if (is_root_cache(s)) |
| 297 | return s; |
Vladimir Davydov | f7ce319 | 2015-02-12 14:59:20 -0800 | [diff] [blame] | 298 | return s->memcg_params.root_cache; |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 299 | } |
Vladimir Davydov | 5dfb417 | 2014-06-04 16:06:38 -0700 | [diff] [blame] | 300 | |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 301 | static 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 Gushchin | 286e04b | 2020-08-06 23:20:52 -0700 | [diff] [blame] | 308 | static 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 Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 320 | static inline bool page_has_obj_cgroups(struct page *page) |
Roman Gushchin | 4d96ba3 | 2019-07-11 20:56:31 -0700 | [diff] [blame] | 321 | { |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 322 | return ((unsigned long)page->obj_cgroups & 0x1UL); |
Roman Gushchin | 4d96ba3 | 2019-07-11 20:56:31 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Roman Gushchin | 286e04b | 2020-08-06 23:20:52 -0700 | [diff] [blame] | 325 | static 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 | |
| 341 | static 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 Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 347 | static 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 | |
| 356 | static 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 Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 361 | struct obj_cgroup *objcg; |
Roman Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 362 | |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 363 | if (memcg_kmem_bypass()) |
| 364 | return s; |
| 365 | |
| 366 | cachep = memcg_kmem_get_cache(s); |
Roman Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 367 | if (is_root_cache(cachep)) |
| 368 | return s; |
| 369 | |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 370 | 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 Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 376 | cachep = NULL; |
| 377 | } |
| 378 | |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 379 | *objcgp = objcg; |
Roman Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 380 | return cachep; |
| 381 | } |
| 382 | |
| 383 | static 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 Gushchin | 964d4bd | 2020-08-06 23:20:56 -0700 | [diff] [blame] | 397 | static 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 Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 411 | 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 Gushchin | 964d4bd | 2020-08-06 23:20:56 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | obj_cgroup_put(objcg); |
Roman Gushchin | 964d4bd | 2020-08-06 23:20:56 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static 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 Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 432 | |
| 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 Gushchin | 964d4bd | 2020-08-06 23:20:56 -0700 | [diff] [blame] | 437 | obj_cgroup_put(objcg); |
| 438 | } |
| 439 | |
Vladimir Davydov | f7ce319 | 2015-02-12 14:59:20 -0800 | [diff] [blame] | 440 | extern void slab_init_memcg_params(struct kmem_cache *); |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 441 | extern void memcg_link_cache(struct kmem_cache *s); |
Vladimir Davydov | f7ce319 | 2015-02-12 14:59:20 -0800 | [diff] [blame] | 442 | |
Kirill Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 443 | #else /* CONFIG_MEMCG_KMEM */ |
Vladimir Davydov | f7ce319 | 2015-02-12 14:59:20 -0800 | [diff] [blame] | 444 | |
Tejun Heo | 510ded3 | 2017-02-22 15:41:24 -0800 | [diff] [blame] | 445 | /* If !memcg, all caches are root. */ |
| 446 | #define slab_root_caches slab_caches |
| 447 | #define root_caches_node list |
| 448 | |
Glauber Costa | ba6c496 | 2012-12-18 14:22:27 -0800 | [diff] [blame] | 449 | static inline bool is_root_cache(struct kmem_cache *s) |
| 450 | { |
| 451 | return true; |
| 452 | } |
| 453 | |
Glauber Costa | b9ce5ef | 2012-12-18 14:22:46 -0800 | [diff] [blame] | 454 | static inline bool slab_equal_or_root(struct kmem_cache *s, |
| 455 | struct kmem_cache *p) |
| 456 | { |
Kees Cook | 598a071 | 2019-07-11 20:53:23 -0700 | [diff] [blame] | 457 | return s == p; |
Glauber Costa | b9ce5ef | 2012-12-18 14:22:46 -0800 | [diff] [blame] | 458 | } |
Glauber Costa | 749c541 | 2012-12-18 14:23:01 -0800 | [diff] [blame] | 459 | |
| 460 | static inline const char *cache_name(struct kmem_cache *s) |
| 461 | { |
| 462 | return s->name; |
| 463 | } |
| 464 | |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 465 | static inline struct kmem_cache *memcg_root_cache(struct kmem_cache *s) |
| 466 | { |
| 467 | return s; |
| 468 | } |
Vladimir Davydov | 5dfb417 | 2014-06-04 16:06:38 -0700 | [diff] [blame] | 469 | |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 470 | static inline struct kmem_cache *memcg_cache(struct kmem_cache *s) |
| 471 | { |
| 472 | return NULL; |
| 473 | } |
| 474 | |
| 475 | static inline bool page_has_obj_cgroups(struct page *page) |
| 476 | { |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr) |
Roman Gushchin | 4d96ba3 | 2019-07-11 20:56:31 -0700 | [diff] [blame] | 481 | { |
| 482 | return NULL; |
| 483 | } |
| 484 | |
Roman Gushchin | 286e04b | 2020-08-06 23:20:52 -0700 | [diff] [blame] | 485 | static inline int memcg_alloc_page_obj_cgroups(struct page *page, |
| 486 | struct kmem_cache *s, gfp_t gfp) |
| 487 | { |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static inline void memcg_free_page_obj_cgroups(struct page *page) |
| 492 | { |
| 493 | } |
| 494 | |
Roman Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 495 | static 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 Gushchin | 964d4bd | 2020-08-06 23:20:56 -0700 | [diff] [blame] | 502 | static 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 | |
| 508 | static inline void memcg_slab_free_hook(struct kmem_cache *s, struct page *page, |
| 509 | void *p) |
| 510 | { |
| 511 | } |
| 512 | |
Vladimir Davydov | f7ce319 | 2015-02-12 14:59:20 -0800 | [diff] [blame] | 513 | static inline void slab_init_memcg_params(struct kmem_cache *s) |
| 514 | { |
| 515 | } |
Tejun Heo | 510ded3 | 2017-02-22 15:41:24 -0800 | [diff] [blame] | 516 | |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 517 | static inline void memcg_link_cache(struct kmem_cache *s) |
Tejun Heo | 510ded3 | 2017-02-22 15:41:24 -0800 | [diff] [blame] | 518 | { |
| 519 | } |
| 520 | |
Kirill Tkhai | 84c07d1 | 2018-08-17 15:47:25 -0700 | [diff] [blame] | 521 | #endif /* CONFIG_MEMCG_KMEM */ |
Glauber Costa | b9ce5ef | 2012-12-18 14:22:46 -0800 | [diff] [blame] | 522 | |
Kees Cook | a64b537 | 2019-07-11 20:53:26 -0700 | [diff] [blame] | 523 | static 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 Gushchin | 6cea1d5 | 2019-07-11 20:56:16 -0700 | [diff] [blame] | 534 | static __always_inline int charge_slab_page(struct page *page, |
| 535 | gfp_t gfp, int order, |
| 536 | struct kmem_cache *s) |
| 537 | { |
Roman Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 538 | if (memcg_kmem_enabled() && !is_root_cache(s)) { |
| 539 | int ret; |
Roman Gushchin | 286e04b | 2020-08-06 23:20:52 -0700 | [diff] [blame] | 540 | |
Roman Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 541 | ret = memcg_alloc_page_obj_cgroups(page, s, gfp); |
| 542 | if (ret) |
| 543 | return ret; |
Roman Gushchin | 4d96ba3 | 2019-07-11 20:56:31 -0700 | [diff] [blame] | 544 | } |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 545 | |
Roman Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 546 | mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), |
| 547 | PAGE_SIZE << order); |
| 548 | return 0; |
Roman Gushchin | 6cea1d5 | 2019-07-11 20:56:16 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | static __always_inline void uncharge_slab_page(struct page *page, int order, |
| 552 | struct kmem_cache *s) |
| 553 | { |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 554 | if (memcg_kmem_enabled() && !is_root_cache(s)) |
Roman Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 555 | memcg_free_page_obj_cgroups(page); |
Roman Gushchin | 9855609 | 2020-08-06 23:21:10 -0700 | [diff] [blame^] | 556 | |
Roman Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 557 | mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s), |
| 558 | -(PAGE_SIZE << order)); |
Roman Gushchin | 6cea1d5 | 2019-07-11 20:56:16 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Vlastimil Babka | e42f174 | 2020-08-06 23:19:05 -0700 | [diff] [blame] | 561 | static 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 Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 578 | static 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 Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 592 | if (s->flags & SLAB_KASAN) |
| 593 | return s->object_size; |
Jesper Dangaard Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 594 | /* |
| 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. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 599 | if (s->flags & (SLAB_TYPESAFE_BY_RCU | SLAB_STORE_USER)) |
Jesper Dangaard Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 600 | 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 | |
| 608 | static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s, |
Roman Gushchin | 964d4bd | 2020-08-06 23:20:56 -0700 | [diff] [blame] | 609 | struct obj_cgroup **objcgp, |
| 610 | size_t size, gfp_t flags) |
Jesper Dangaard Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 611 | { |
| 612 | flags &= gfp_allowed_mask; |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 613 | |
| 614 | fs_reclaim_acquire(flags); |
| 615 | fs_reclaim_release(flags); |
| 616 | |
Jesper Dangaard Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 617 | might_sleep_if(gfpflags_allow_blocking(flags)); |
| 618 | |
Jesper Dangaard Brouer | fab9963 | 2016-03-15 14:53:38 -0700 | [diff] [blame] | 619 | if (should_failslab(s, flags)) |
Jesper Dangaard Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 620 | return NULL; |
| 621 | |
Vladimir Davydov | 4526477 | 2016-07-26 15:24:21 -0700 | [diff] [blame] | 622 | if (memcg_kmem_enabled() && |
| 623 | ((flags & __GFP_ACCOUNT) || (s->flags & SLAB_ACCOUNT))) |
Roman Gushchin | f2fe7b0 | 2020-08-06 23:20:59 -0700 | [diff] [blame] | 624 | return memcg_slab_pre_alloc_hook(s, objcgp, size, flags); |
Vladimir Davydov | 4526477 | 2016-07-26 15:24:21 -0700 | [diff] [blame] | 625 | |
| 626 | return s; |
Jesper Dangaard Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Roman Gushchin | 964d4bd | 2020-08-06 23:20:56 -0700 | [diff] [blame] | 629 | static 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 Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 632 | { |
| 633 | size_t i; |
| 634 | |
| 635 | flags &= gfp_allowed_mask; |
| 636 | for (i = 0; i < size; i++) { |
Andrey Konovalov | 5312824 | 2019-02-20 22:19:11 -0800 | [diff] [blame] | 637 | p[i] = kasan_slab_alloc(s, p[i], flags); |
Andrey Konovalov | a2f7757 | 2019-02-20 22:19:16 -0800 | [diff] [blame] | 638 | /* As p[i] might get tagged, call kmemleak hook after KASAN. */ |
Andrey Konovalov | 5312824 | 2019-02-20 22:19:11 -0800 | [diff] [blame] | 639 | kmemleak_alloc_recursive(p[i], s->object_size, 1, |
Jesper Dangaard Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 640 | s->flags, flags); |
Jesper Dangaard Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 641 | } |
Vladimir Davydov | 4526477 | 2016-07-26 15:24:21 -0700 | [diff] [blame] | 642 | |
Roman Gushchin | 964d4bd | 2020-08-06 23:20:56 -0700 | [diff] [blame] | 643 | if (memcg_kmem_enabled() && !is_root_cache(s)) |
| 644 | memcg_slab_post_alloc_hook(s, objcg, size, p); |
Jesper Dangaard Brouer | 11c7aec | 2016-03-15 14:53:35 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Christoph Lameter | 44c5356 | 2014-08-06 16:04:07 -0700 | [diff] [blame] | 647 | #ifndef CONFIG_SLOB |
Christoph Lameter | ca34956 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 648 | /* |
| 649 | * The slab lists for all objects. |
| 650 | */ |
| 651 | struct 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 Rientjes | bf00bd3 | 2016-12-12 16:41:44 -0800 | [diff] [blame] | 658 | unsigned long total_slabs; /* length of all slab lists */ |
| 659 | unsigned long free_slabs; /* length of free slab list only */ |
Christoph Lameter | ca34956 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 660 | 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 Kim | c8522a3 | 2014-08-06 16:04:29 -0700 | [diff] [blame] | 664 | struct alien_cache **alien; /* on other nodes */ |
Christoph Lameter | ca34956 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 665 | 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 Li | e25839f | 2013-07-04 08:33:23 +0800 | [diff] [blame] | 680 | |
Christoph Lameter | 44c5356 | 2014-08-06 16:04:07 -0700 | [diff] [blame] | 681 | static 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 Patocka | 9163582 | 2014-10-09 15:26:20 -0700 | [diff] [blame] | 691 | for (__node = 0; __node < nr_node_ids; __node++) \ |
| 692 | if ((__n = get_node(__s, __node))) |
Christoph Lameter | 44c5356 | 2014-08-06 16:04:07 -0700 | [diff] [blame] | 693 | |
| 694 | #endif |
| 695 | |
Vladimir Davydov | 1df3b26 | 2014-12-10 15:42:16 -0800 | [diff] [blame] | 696 | void *slab_start(struct seq_file *m, loff_t *pos); |
Wanpeng Li | 276a243 | 2013-07-08 08:08:28 +0800 | [diff] [blame] | 697 | void *slab_next(struct seq_file *m, void *p, loff_t *pos); |
| 698 | void slab_stop(struct seq_file *m, void *p); |
Vladimir Davydov | b047501 | 2014-12-10 15:44:19 -0800 | [diff] [blame] | 699 | int memcg_slab_show(struct seq_file *m, void *p); |
Andrey Ryabinin | 5240ab4 | 2014-08-06 16:04:14 -0700 | [diff] [blame] | 700 | |
Yang Shi | 852d8be | 2017-11-15 17:32:07 -0800 | [diff] [blame] | 701 | #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG) |
| 702 | void dump_unreclaimable_slab(void); |
| 703 | #else |
| 704 | static inline void dump_unreclaimable_slab(void) |
| 705 | { |
| 706 | } |
| 707 | #endif |
| 708 | |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 709 | void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr); |
| 710 | |
Thomas Garnier | 7c00fce | 2016-07-26 15:21:56 -0700 | [diff] [blame] | 711 | #ifdef CONFIG_SLAB_FREELIST_RANDOM |
| 712 | int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count, |
| 713 | gfp_t gfp); |
| 714 | void cache_random_seq_destroy(struct kmem_cache *cachep); |
| 715 | #else |
| 716 | static inline int cache_random_seq_create(struct kmem_cache *cachep, |
| 717 | unsigned int count, gfp_t gfp) |
| 718 | { |
| 719 | return 0; |
| 720 | } |
| 721 | static inline void cache_random_seq_destroy(struct kmem_cache *cachep) { } |
| 722 | #endif /* CONFIG_SLAB_FREELIST_RANDOM */ |
| 723 | |
Alexander Potapenko | 6471384 | 2019-07-11 20:59:19 -0700 | [diff] [blame] | 724 | static 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 | |
| 736 | static 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 Ryabinin | 5240ab4 | 2014-08-06 16:04:14 -0700 | [diff] [blame] | 744 | #endif /* MM_SLAB_H */ |