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