blob: d9228e4d0320569f51f7f581722931a2982c1416 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Christoph Lameter2e892f42006-12-13 00:34:23 -08002#ifndef _LINUX_SLAB_DEF_H
3#define _LINUX_SLAB_DEF_H
4
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01005#include <linux/reciprocal_div.h>
6
Christoph Lameter2e892f42006-12-13 00:34:23 -08007/*
8 * Definitions unique to the original Linux SLAB allocator.
Pekka Enberg8eae9852008-05-09 20:32:44 +02009 */
10
11struct kmem_cache {
Joonsoo Kimbf0dea22014-10-09 15:26:27 -070012 struct array_cache __percpu *cpu_cache;
13
Fam Zheng24755e22014-01-21 09:12:42 +080014/* 1) Cache tunables. Protected by slab_mutex */
Pekka Enberg8eae9852008-05-09 20:32:44 +020015 unsigned int batchcount;
16 unsigned int limit;
17 unsigned int shared;
18
Christoph Lameter3b0efdf2012-06-13 10:24:57 -050019 unsigned int size;
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +010020 struct reciprocal_value reciprocal_buffer_size;
Eric Dumazetb56efcf2011-07-20 19:04:23 +020021/* 2) touched by every alloc & free from the backend */
Pekka Enberg8eae9852008-05-09 20:32:44 +020022
Alexey Dobriyand50112e2017-11-15 17:32:18 -080023 slab_flags_t flags; /* constant flags */
Pekka Enberg8eae9852008-05-09 20:32:44 +020024 unsigned int num; /* # of objs per slab */
25
Eric Dumazetb56efcf2011-07-20 19:04:23 +020026/* 3) cache_grow/shrink */
Pekka Enberg8eae9852008-05-09 20:32:44 +020027 /* order of pgs per slab (2^n) */
28 unsigned int gfporder;
29
30 /* force GFP flags, e.g. GFP_DMA */
Glauber Costaa618e892012-06-14 16:17:21 +040031 gfp_t allocflags;
Pekka Enberg8eae9852008-05-09 20:32:44 +020032
33 size_t colour; /* cache colouring range */
34 unsigned int colour_off; /* colour offset */
Joonsoo Kim8456a642013-10-24 10:07:49 +090035 struct kmem_cache *freelist_cache;
36 unsigned int freelist_size;
Pekka Enberg8eae9852008-05-09 20:32:44 +020037
38 /* constructor func */
39 void (*ctor)(void *obj);
40
Eric Dumazetb56efcf2011-07-20 19:04:23 +020041/* 4) cache creation/removal */
Pekka Enberg8eae9852008-05-09 20:32:44 +020042 const char *name;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -050043 struct list_head list;
44 int refcount;
45 int object_size;
46 int align;
Pekka Enberg8eae9852008-05-09 20:32:44 +020047
Eric Dumazetb56efcf2011-07-20 19:04:23 +020048/* 5) statistics */
Pekka Enberg8eae9852008-05-09 20:32:44 +020049#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;
Joonsoo Kimd31676d2016-03-15 14:54:24 -070064#ifdef CONFIG_DEBUG_SLAB_LEAK
65 atomic_t store_user_clean;
66#endif
Pekka Enberg8eae9852008-05-09 20:32:44 +020067
68 /*
69 * If debugging is enabled, then the allocator can add additional
Christoph Lameter3b0efdf2012-06-13 10:24:57 -050070 * fields and/or padding to every object. size contains the total
Pekka Enberg8eae9852008-05-09 20:32:44 +020071 * object size including these internal fields, the following two
72 * variables contain the offset to the user object and its size.
73 */
74 int obj_offset;
Pekka Enberg8eae9852008-05-09 20:32:44 +020075#endif /* CONFIG_DEBUG_SLAB */
Johannes Weiner127424c2016-01-20 15:02:32 -080076
77#ifdef CONFIG_MEMCG
Vladimir Davydovf7ce3192015-02-12 14:59:20 -080078 struct memcg_cache_params memcg_params;
Glauber Costaba6c4962012-12-18 14:22:27 -080079#endif
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070080#ifdef CONFIG_KASAN
81 struct kasan_cache kasan_info;
82#endif
Pekka Enberg8eae9852008-05-09 20:32:44 +020083
Thomas Garnierc7ce4f602016-05-19 17:10:37 -070084#ifdef CONFIG_SLAB_FREELIST_RANDOM
Thomas Garnier7c00fce2016-07-26 15:21:56 -070085 unsigned int *random_seq;
Thomas Garnierc7ce4f602016-05-19 17:10:37 -070086#endif
87
Alexey Dobriyan7bbdb812018-04-05 16:21:31 -070088 unsigned int useroffset; /* Usercopy region offset */
89 unsigned int usersize; /* Usercopy region size */
David Windsor8eb82842017-06-10 22:50:28 -040090
Joonsoo Kimbf0dea22014-10-09 15:26:27 -070091 struct kmem_cache_node *node[MAX_NUMNODES];
Pekka Enberg8eae9852008-05-09 20:32:44 +020092};
93
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070094static inline void *nearest_obj(struct kmem_cache *cache, struct page *page,
Alexander Potapenko80a92012016-07-28 15:49:07 -070095 void *x)
96{
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070097 void *object = x - (x - page->s_mem) % cache->size;
98 void *last_object = page->s_mem + (cache->num - 1) * cache->size;
99
100 if (unlikely(object > last_object))
101 return last_object;
102 else
103 return object;
104}
105
Christoph Lameter2e892f42006-12-13 00:34:23 -0800106#endif /* _LINUX_SLAB_DEF_H */