blob: abc7de77b9881a0a3ab81c50f103f0b1b782fc68 [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;
64
65 /*
66 * If debugging is enabled, then the allocator can add additional
Baoquan He05fec352018-06-07 17:05:24 -070067 * 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 Enberg8eae9852008-05-09 20:32:44 +020071 */
72 int obj_offset;
Pekka Enberg8eae9852008-05-09 20:32:44 +020073#endif /* CONFIG_DEBUG_SLAB */
Johannes Weiner127424c2016-01-20 15:02:32 -080074
75#ifdef CONFIG_MEMCG
Vladimir Davydovf7ce3192015-02-12 14:59:20 -080076 struct memcg_cache_params memcg_params;
Glauber Costaba6c4962012-12-18 14:22:27 -080077#endif
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070078#ifdef CONFIG_KASAN
79 struct kasan_cache kasan_info;
80#endif
Pekka Enberg8eae9852008-05-09 20:32:44 +020081
Thomas Garnierc7ce4f602016-05-19 17:10:37 -070082#ifdef CONFIG_SLAB_FREELIST_RANDOM
Thomas Garnier7c00fce2016-07-26 15:21:56 -070083 unsigned int *random_seq;
Thomas Garnierc7ce4f602016-05-19 17:10:37 -070084#endif
85
Alexey Dobriyan7bbdb812018-04-05 16:21:31 -070086 unsigned int useroffset; /* Usercopy region offset */
87 unsigned int usersize; /* Usercopy region size */
David Windsor8eb82842017-06-10 22:50:28 -040088
Joonsoo Kimbf0dea22014-10-09 15:26:27 -070089 struct kmem_cache_node *node[MAX_NUMNODES];
Pekka Enberg8eae9852008-05-09 20:32:44 +020090};
91
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070092static inline void *nearest_obj(struct kmem_cache *cache, struct page *page,
Alexander Potapenko80a92012016-07-28 15:49:07 -070093 void *x)
94{
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -070095 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 Konovalov5b7c4142018-12-28 00:30:46 -0800104/*
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 */
110static 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 Lameter2e892f42006-12-13 00:34:23 -0800117#endif /* _LINUX_SLAB_DEF_H */