Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 2 | #ifndef _LINUX_SLAB_DEF_H |
| 3 | #define _LINUX_SLAB_DEF_H |
| 4 | |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 5 | #include <linux/reciprocal_div.h> |
| 6 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 7 | /* |
| 8 | * Definitions unique to the original Linux SLAB allocator. |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | struct kmem_cache { |
Joonsoo Kim | bf0dea2 | 2014-10-09 15:26:27 -0700 | [diff] [blame] | 12 | struct array_cache __percpu *cpu_cache; |
| 13 | |
Fam Zheng | 24755e2 | 2014-01-21 09:12:42 +0800 | [diff] [blame] | 14 | /* 1) Cache tunables. Protected by slab_mutex */ |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 15 | unsigned int batchcount; |
| 16 | unsigned int limit; |
| 17 | unsigned int shared; |
| 18 | |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 19 | unsigned int size; |
Hannes Frederic Sowa | 809fa97 | 2014-01-22 02:29:41 +0100 | [diff] [blame] | 20 | struct reciprocal_value reciprocal_buffer_size; |
Eric Dumazet | b56efcf | 2011-07-20 19:04:23 +0200 | [diff] [blame] | 21 | /* 2) touched by every alloc & free from the backend */ |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 22 | |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 23 | slab_flags_t flags; /* constant flags */ |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 24 | unsigned int num; /* # of objs per slab */ |
| 25 | |
Eric Dumazet | b56efcf | 2011-07-20 19:04:23 +0200 | [diff] [blame] | 26 | /* 3) cache_grow/shrink */ |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 27 | /* order of pgs per slab (2^n) */ |
| 28 | unsigned int gfporder; |
| 29 | |
| 30 | /* force GFP flags, e.g. GFP_DMA */ |
Glauber Costa | a618e89 | 2012-06-14 16:17:21 +0400 | [diff] [blame] | 31 | gfp_t allocflags; |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 32 | |
| 33 | size_t colour; /* cache colouring range */ |
| 34 | unsigned int colour_off; /* colour offset */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 35 | struct kmem_cache *freelist_cache; |
| 36 | unsigned int freelist_size; |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 37 | |
| 38 | /* constructor func */ |
| 39 | void (*ctor)(void *obj); |
| 40 | |
Eric Dumazet | b56efcf | 2011-07-20 19:04:23 +0200 | [diff] [blame] | 41 | /* 4) cache creation/removal */ |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 42 | const char *name; |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 43 | struct list_head list; |
| 44 | int refcount; |
| 45 | int object_size; |
| 46 | int align; |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 47 | |
Eric Dumazet | b56efcf | 2011-07-20 19:04:23 +0200 | [diff] [blame] | 48 | /* 5) statistics */ |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 49 | #ifdef CONFIG_DEBUG_SLAB |
| 50 | unsigned long num_active; |
| 51 | unsigned long num_allocations; |
| 52 | unsigned long high_mark; |
| 53 | unsigned long grown; |
| 54 | unsigned long reaped; |
| 55 | unsigned long errors; |
| 56 | unsigned long max_freeable; |
| 57 | unsigned long node_allocs; |
| 58 | unsigned long node_frees; |
| 59 | unsigned long node_overflow; |
| 60 | atomic_t allochit; |
| 61 | atomic_t allocmiss; |
| 62 | atomic_t freehit; |
| 63 | atomic_t freemiss; |
| 64 | |
| 65 | /* |
| 66 | * If debugging is enabled, then the allocator can add additional |
Baoquan He | 05fec35 | 2018-06-07 17:05:24 -0700 | [diff] [blame] | 67 | * fields and/or padding to every object. 'size' contains the total |
| 68 | * object size including these internal fields, while 'obj_offset' |
| 69 | * and 'object_size' contain the offset to the user object and its |
| 70 | * size. |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 71 | */ |
| 72 | int obj_offset; |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 73 | #endif /* CONFIG_DEBUG_SLAB */ |
Johannes Weiner | 127424c | 2016-01-20 15:02:32 -0800 | [diff] [blame] | 74 | |
| 75 | #ifdef CONFIG_MEMCG |
Vladimir Davydov | f7ce319 | 2015-02-12 14:59:20 -0800 | [diff] [blame] | 76 | struct memcg_cache_params memcg_params; |
Glauber Costa | ba6c496 | 2012-12-18 14:22:27 -0800 | [diff] [blame] | 77 | #endif |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 78 | #ifdef CONFIG_KASAN |
| 79 | struct kasan_cache kasan_info; |
| 80 | #endif |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 81 | |
Thomas Garnier | c7ce4f60 | 2016-05-19 17:10:37 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_SLAB_FREELIST_RANDOM |
Thomas Garnier | 7c00fce | 2016-07-26 15:21:56 -0700 | [diff] [blame] | 83 | unsigned int *random_seq; |
Thomas Garnier | c7ce4f60 | 2016-05-19 17:10:37 -0700 | [diff] [blame] | 84 | #endif |
| 85 | |
Alexey Dobriyan | 7bbdb81 | 2018-04-05 16:21:31 -0700 | [diff] [blame] | 86 | unsigned int useroffset; /* Usercopy region offset */ |
| 87 | unsigned int usersize; /* Usercopy region size */ |
David Windsor | 8eb8284 | 2017-06-10 22:50:28 -0400 | [diff] [blame] | 88 | |
Joonsoo Kim | bf0dea2 | 2014-10-09 15:26:27 -0700 | [diff] [blame] | 89 | struct kmem_cache_node *node[MAX_NUMNODES]; |
Pekka Enberg | 8eae985 | 2008-05-09 20:32:44 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 92 | static inline void *nearest_obj(struct kmem_cache *cache, struct page *page, |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 93 | void *x) |
| 94 | { |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 95 | void *object = x - (x - page->s_mem) % cache->size; |
| 96 | void *last_object = page->s_mem + (cache->num - 1) * cache->size; |
| 97 | |
| 98 | if (unlikely(object > last_object)) |
| 99 | return last_object; |
| 100 | else |
| 101 | return object; |
| 102 | } |
| 103 | |
Andrey Konovalov | 5b7c414 | 2018-12-28 00:30:46 -0800 | [diff] [blame] | 104 | /* |
| 105 | * We want to avoid an expensive divide : (offset / cache->size) |
| 106 | * Using the fact that size is a constant for a particular cache, |
| 107 | * we can replace (offset / cache->size) by |
| 108 | * reciprocal_divide(offset, cache->reciprocal_buffer_size) |
| 109 | */ |
| 110 | static inline unsigned int obj_to_index(const struct kmem_cache *cache, |
| 111 | const struct page *page, void *obj) |
| 112 | { |
| 113 | u32 offset = (obj - page->s_mem); |
| 114 | return reciprocal_divide(offset, cache->reciprocal_buffer_size); |
| 115 | } |
| 116 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 117 | #endif /* _LINUX_SLAB_DEF_H */ |