Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_SLUB_DEF_H |
| 3 | #define _LINUX_SLUB_DEF_H |
| 4 | |
| 5 | /* |
| 6 | * SLUB : A Slab allocator without object queues. |
| 7 | * |
Christoph Lameter | cde5353 | 2008-07-04 09:59:22 -0700 | [diff] [blame] | 8 | * (C) 2007 SGI, Christoph Lameter |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 9 | */ |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 10 | #include <linux/kobject.h> |
| 11 | |
Christoph Lameter | 8ff12cf | 2008-02-07 17:47:41 -0800 | [diff] [blame] | 12 | enum stat_item { |
| 13 | ALLOC_FASTPATH, /* Allocation from cpu slab */ |
| 14 | ALLOC_SLOWPATH, /* Allocation by getting a new cpu slab */ |
Zhi Yong Wu | a941f83 | 2013-11-08 20:47:36 +0800 | [diff] [blame] | 15 | FREE_FASTPATH, /* Free to cpu slab */ |
Christoph Lameter | 8ff12cf | 2008-02-07 17:47:41 -0800 | [diff] [blame] | 16 | FREE_SLOWPATH, /* Freeing not to cpu slab */ |
| 17 | FREE_FROZEN, /* Freeing to frozen slab */ |
| 18 | FREE_ADD_PARTIAL, /* Freeing moves slab to partial list */ |
| 19 | FREE_REMOVE_PARTIAL, /* Freeing removes last object */ |
Alex Shi | 8028dce | 2012-02-03 23:34:56 +0800 | [diff] [blame] | 20 | ALLOC_FROM_PARTIAL, /* Cpu slab acquired from node partial list */ |
Christoph Lameter | 8ff12cf | 2008-02-07 17:47:41 -0800 | [diff] [blame] | 21 | ALLOC_SLAB, /* Cpu slab acquired from page allocator */ |
| 22 | ALLOC_REFILL, /* Refill cpu slab from slab freelist */ |
Christoph Lameter | e36a265 | 2011-06-01 12:25:57 -0500 | [diff] [blame] | 23 | ALLOC_NODE_MISMATCH, /* Switching cpu slab */ |
Christoph Lameter | 8ff12cf | 2008-02-07 17:47:41 -0800 | [diff] [blame] | 24 | FREE_SLAB, /* Slab freed to the page allocator */ |
| 25 | CPUSLAB_FLUSH, /* Abandoning of the cpu slab */ |
| 26 | DEACTIVATE_FULL, /* Cpu slab was full when deactivated */ |
| 27 | DEACTIVATE_EMPTY, /* Cpu slab was empty when deactivated */ |
| 28 | DEACTIVATE_TO_HEAD, /* Cpu slab was moved to the head of partials */ |
| 29 | DEACTIVATE_TO_TAIL, /* Cpu slab was moved to the tail of partials */ |
| 30 | DEACTIVATE_REMOTE_FREES,/* Slab contained remotely freed objects */ |
Christoph Lameter | 03e404a | 2011-06-01 12:25:58 -0500 | [diff] [blame] | 31 | DEACTIVATE_BYPASS, /* Implicit deactivation */ |
Christoph Lameter | 65c3376 | 2008-04-14 19:11:40 +0300 | [diff] [blame] | 32 | ORDER_FALLBACK, /* Number of times fallback was necessary */ |
Christoph Lameter | 4fdccdf | 2011-03-22 13:35:00 -0500 | [diff] [blame] | 33 | CMPXCHG_DOUBLE_CPU_FAIL,/* Failure of this_cpu_cmpxchg_double */ |
Christoph Lameter | b789ef5 | 2011-06-01 12:25:49 -0500 | [diff] [blame] | 34 | CMPXCHG_DOUBLE_FAIL, /* Number of times that cmpxchg double did not match */ |
Christoph Lameter | 49e2258 | 2011-08-09 16:12:27 -0500 | [diff] [blame] | 35 | CPU_PARTIAL_ALLOC, /* Used cpu partial on alloc */ |
Alex Shi | 8028dce | 2012-02-03 23:34:56 +0800 | [diff] [blame] | 36 | CPU_PARTIAL_FREE, /* Refill cpu partial on free */ |
| 37 | CPU_PARTIAL_NODE, /* Refill cpu partial from node partial */ |
| 38 | CPU_PARTIAL_DRAIN, /* Drain cpu partial to node partial */ |
Christoph Lameter | 8ff12cf | 2008-02-07 17:47:41 -0800 | [diff] [blame] | 39 | NR_SLUB_STAT_ITEMS }; |
| 40 | |
Christoph Lameter | dfb4f09 | 2007-10-16 01:26:05 -0700 | [diff] [blame] | 41 | struct kmem_cache_cpu { |
Christoph Lameter | 8a5ec0b | 2011-02-25 11:38:54 -0600 | [diff] [blame] | 42 | void **freelist; /* Pointer to next available object */ |
Christoph Lameter | 8a5ec0b | 2011-02-25 11:38:54 -0600 | [diff] [blame] | 43 | unsigned long tid; /* Globally unique transaction id */ |
Christoph Lameter | da89b79 | 2008-01-07 23:20:31 -0800 | [diff] [blame] | 44 | struct page *page; /* The slab from which we are allocating */ |
Wei Yang | a93cf07 | 2017-07-06 15:36:31 -0700 | [diff] [blame] | 45 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
Christoph Lameter | 49e2258 | 2011-08-09 16:12:27 -0500 | [diff] [blame] | 46 | struct page *partial; /* Partially allocated frozen slabs */ |
Wei Yang | a93cf07 | 2017-07-06 15:36:31 -0700 | [diff] [blame] | 47 | #endif |
Christoph Lameter | 8ff12cf | 2008-02-07 17:47:41 -0800 | [diff] [blame] | 48 | #ifdef CONFIG_SLUB_STATS |
| 49 | unsigned stat[NR_SLUB_STAT_ITEMS]; |
| 50 | #endif |
Christoph Lameter | 4c93c355 | 2007-10-16 01:26:08 -0700 | [diff] [blame] | 51 | }; |
Christoph Lameter | dfb4f09 | 2007-10-16 01:26:05 -0700 | [diff] [blame] | 52 | |
Wei Yang | a93cf07 | 2017-07-06 15:36:31 -0700 | [diff] [blame] | 53 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
| 54 | #define slub_percpu_partial(c) ((c)->partial) |
| 55 | |
| 56 | #define slub_set_percpu_partial(c, p) \ |
| 57 | ({ \ |
| 58 | slub_percpu_partial(c) = (p)->next; \ |
| 59 | }) |
| 60 | |
| 61 | #define slub_percpu_partial_read_once(c) READ_ONCE(slub_percpu_partial(c)) |
| 62 | #else |
| 63 | #define slub_percpu_partial(c) NULL |
| 64 | |
| 65 | #define slub_set_percpu_partial(c, p) |
| 66 | |
| 67 | #define slub_percpu_partial_read_once(c) NULL |
| 68 | #endif // CONFIG_SLUB_CPU_PARTIAL |
| 69 | |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 70 | /* |
Christoph Lameter | 834f3d1 | 2008-04-14 19:11:31 +0300 | [diff] [blame] | 71 | * Word size structure that can be atomically updated or read and that |
| 72 | * contains both the order and the number of objects that a slab of the |
| 73 | * given order would contain. |
| 74 | */ |
| 75 | struct kmem_cache_order_objects { |
Alexey Dobriyan | 19af27a | 2018-04-05 16:21:39 -0700 | [diff] [blame] | 76 | unsigned int x; |
Christoph Lameter | 834f3d1 | 2008-04-14 19:11:31 +0300 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | /* |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 80 | * Slab cache management. |
| 81 | */ |
| 82 | struct kmem_cache { |
Namhyung Kim | 1b5ad24 | 2010-08-07 14:29:22 +0200 | [diff] [blame] | 83 | struct kmem_cache_cpu __percpu *cpu_slab; |
Tobin C. Harding | de810f4 | 2019-03-05 15:42:07 -0800 | [diff] [blame] | 84 | /* Used for retrieving partial slabs, etc. */ |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 85 | slab_flags_t flags; |
Christoph Lameter | 1a757fe | 2011-02-25 11:38:51 -0600 | [diff] [blame] | 86 | unsigned long min_partial; |
Tobin C. Harding | de810f4 | 2019-03-05 15:42:07 -0800 | [diff] [blame] | 87 | unsigned int size; /* The size of an object including metadata */ |
| 88 | unsigned int object_size;/* The size of an object without metadata */ |
| 89 | unsigned int offset; /* Free pointer offset */ |
Wei Yang | e6d0e1d | 2017-07-06 15:36:34 -0700 | [diff] [blame] | 90 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
Alexey Dobriyan | e5d9998 | 2018-04-05 16:21:10 -0700 | [diff] [blame] | 91 | /* Number of per cpu partial objects to keep around */ |
| 92 | unsigned int cpu_partial; |
Wei Yang | e6d0e1d | 2017-07-06 15:36:34 -0700 | [diff] [blame] | 93 | #endif |
Christoph Lameter | 834f3d1 | 2008-04-14 19:11:31 +0300 | [diff] [blame] | 94 | struct kmem_cache_order_objects oo; |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 95 | |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 96 | /* Allocation and freeing of slabs */ |
Christoph Lameter | 205ab99 | 2008-04-14 19:11:40 +0300 | [diff] [blame] | 97 | struct kmem_cache_order_objects max; |
Christoph Lameter | 65c3376 | 2008-04-14 19:11:40 +0300 | [diff] [blame] | 98 | struct kmem_cache_order_objects min; |
Christoph Lameter | b7a49f0 | 2008-02-14 14:21:32 -0800 | [diff] [blame] | 99 | gfp_t allocflags; /* gfp flags to use on each alloc */ |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 100 | int refcount; /* Refcount for slab cache destroy */ |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 101 | void (*ctor)(void *); |
Alexey Dobriyan | 52ee6d7 | 2018-04-05 16:21:06 -0700 | [diff] [blame] | 102 | unsigned int inuse; /* Offset to metadata */ |
Alexey Dobriyan | 3a3791e | 2018-04-05 16:21:02 -0700 | [diff] [blame] | 103 | unsigned int align; /* Alignment */ |
Alexey Dobriyan | 2ca6d39 | 2018-04-05 16:20:55 -0700 | [diff] [blame] | 104 | unsigned int red_left_pad; /* Left redzone padding size */ |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 105 | const char *name; /* Name (only for display!) */ |
| 106 | struct list_head list; /* List of slab caches */ |
Christoph Lameter | ab4d5ed | 2010-10-05 13:57:26 -0500 | [diff] [blame] | 107 | #ifdef CONFIG_SYSFS |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 108 | struct kobject kobj; /* For sysfs */ |
Tejun Heo | 3b7b314 | 2017-06-23 15:08:52 -0700 | [diff] [blame] | 109 | struct work_struct kobj_remove_work; |
Christoph Lameter | 0c71001 | 2007-07-17 04:03:24 -0700 | [diff] [blame] | 110 | #endif |
Johannes Weiner | 127424c | 2016-01-20 15:02:32 -0800 | [diff] [blame] | 111 | #ifdef CONFIG_MEMCG |
Vladimir Davydov | f7ce319 | 2015-02-12 14:59:20 -0800 | [diff] [blame] | 112 | struct memcg_cache_params memcg_params; |
Tobin C. Harding | de810f4 | 2019-03-05 15:42:07 -0800 | [diff] [blame] | 113 | /* For propagation, maximum size of a stored attr */ |
Alexey Dobriyan | 56d8cee | 2018-04-05 16:20:51 -0700 | [diff] [blame] | 114 | unsigned int max_attr_size; |
Vladimir Davydov | 9a41707 | 2014-04-07 15:39:31 -0700 | [diff] [blame] | 115 | #ifdef CONFIG_SYSFS |
| 116 | struct kset *memcg_kset; |
| 117 | #endif |
Glauber Costa | ba6c496 | 2012-12-18 14:22:27 -0800 | [diff] [blame] | 118 | #endif |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 119 | |
Kees Cook | 2482ddec | 2017-09-06 16:19:18 -0700 | [diff] [blame] | 120 | #ifdef CONFIG_SLAB_FREELIST_HARDENED |
| 121 | unsigned long random; |
| 122 | #endif |
| 123 | |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 124 | #ifdef CONFIG_NUMA |
Christoph Lameter | 9824601 | 2008-01-07 23:20:26 -0800 | [diff] [blame] | 125 | /* |
| 126 | * Defragmentation by allocating from a remote node. |
| 127 | */ |
Alexey Dobriyan | eb7235e | 2018-04-05 16:20:48 -0700 | [diff] [blame] | 128 | unsigned int remote_node_defrag_ratio; |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 129 | #endif |
Thomas Garnier | 210e7a4 | 2016-07-26 15:21:59 -0700 | [diff] [blame] | 130 | |
| 131 | #ifdef CONFIG_SLAB_FREELIST_RANDOM |
| 132 | unsigned int *random_seq; |
| 133 | #endif |
| 134 | |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 135 | #ifdef CONFIG_KASAN |
| 136 | struct kasan_cache kasan_info; |
| 137 | #endif |
| 138 | |
Alexey Dobriyan | 7bbdb81 | 2018-04-05 16:21:31 -0700 | [diff] [blame] | 139 | unsigned int useroffset; /* Usercopy region offset */ |
| 140 | unsigned int usersize; /* Usercopy region size */ |
David Windsor | 8eb8284 | 2017-06-10 22:50:28 -0400 | [diff] [blame] | 141 | |
Christoph Lameter | 7340cc8 | 2010-09-28 08:10:26 -0500 | [diff] [blame] | 142 | struct kmem_cache_node *node[MAX_NUMNODES]; |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
Wei Yang | e6d0e1d | 2017-07-06 15:36:34 -0700 | [diff] [blame] | 145 | #ifdef CONFIG_SLUB_CPU_PARTIAL |
| 146 | #define slub_cpu_partial(s) ((s)->cpu_partial) |
| 147 | #define slub_set_cpu_partial(s, n) \ |
| 148 | ({ \ |
| 149 | slub_cpu_partial(s) = (n); \ |
| 150 | }) |
| 151 | #else |
| 152 | #define slub_cpu_partial(s) (0) |
| 153 | #define slub_set_cpu_partial(s, n) |
Tobin C. Harding | de810f4 | 2019-03-05 15:42:07 -0800 | [diff] [blame] | 154 | #endif /* CONFIG_SLUB_CPU_PARTIAL */ |
Wei Yang | e6d0e1d | 2017-07-06 15:36:34 -0700 | [diff] [blame] | 155 | |
Christoph Lameter | 41a2128 | 2014-05-06 12:50:08 -0700 | [diff] [blame] | 156 | #ifdef CONFIG_SYSFS |
| 157 | #define SLAB_SUPPORTS_SYSFS |
Mikulas Patocka | d50d82f | 2018-06-27 23:26:09 -0700 | [diff] [blame] | 158 | void sysfs_slab_unlink(struct kmem_cache *); |
Tejun Heo | bf5eb3d | 2017-02-22 15:41:11 -0800 | [diff] [blame] | 159 | void sysfs_slab_release(struct kmem_cache *); |
Christoph Lameter | 41a2128 | 2014-05-06 12:50:08 -0700 | [diff] [blame] | 160 | #else |
Mikulas Patocka | d50d82f | 2018-06-27 23:26:09 -0700 | [diff] [blame] | 161 | static inline void sysfs_slab_unlink(struct kmem_cache *s) |
| 162 | { |
| 163 | } |
Tejun Heo | bf5eb3d | 2017-02-22 15:41:11 -0800 | [diff] [blame] | 164 | static inline void sysfs_slab_release(struct kmem_cache *s) |
Christoph Lameter | 41a2128 | 2014-05-06 12:50:08 -0700 | [diff] [blame] | 165 | { |
| 166 | } |
| 167 | #endif |
| 168 | |
Andrey Ryabinin | 75c66de | 2015-02-13 14:39:35 -0800 | [diff] [blame] | 169 | void object_err(struct kmem_cache *s, struct page *page, |
| 170 | u8 *object, char *reason); |
| 171 | |
Alexander Potapenko | c146a2b | 2016-07-28 15:49:04 -0700 | [diff] [blame] | 172 | void *fixup_red_left(struct kmem_cache *s, void *p); |
| 173 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 174 | static inline void *nearest_obj(struct kmem_cache *cache, struct page *page, |
| 175 | void *x) { |
| 176 | void *object = x - (x - page_address(page)) % cache->size; |
| 177 | void *last_object = page_address(page) + |
| 178 | (page->objects - 1) * cache->size; |
Alexander Potapenko | c146a2b | 2016-07-28 15:49:04 -0700 | [diff] [blame] | 179 | void *result = (unlikely(object > last_object)) ? last_object : object; |
| 180 | |
| 181 | result = fixup_red_left(cache, result); |
| 182 | return result; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Christoph Lameter | 81819f0 | 2007-05-06 14:49:36 -0700 | [diff] [blame] | 185 | #endif /* _LINUX_SLUB_DEF_H */ |