xref: /linux/tools/testing/shared/linux.c (revision c4fb7f0a79771dfd18838bfc5015650a9730e9c0)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdlib.h>
3 #include <string.h>
4 #include <malloc.h>
5 #include <pthread.h>
6 #include <unistd.h>
7 #include <assert.h>
8 
9 #include <linux/gfp.h>
10 #include <linux/poison.h>
11 #include <linux/slab.h>
12 #include <linux/radix-tree.h>
13 #include <urcu/uatomic.h>
14 
15 int nr_allocated;
16 int preempt_count;
17 int test_verbose;
18 
19 void kmem_cache_set_callback(struct kmem_cache *cachep, void (*callback)(void *))
20 {
21 	cachep->callback = callback;
22 }
23 
24 void kmem_cache_set_private(struct kmem_cache *cachep, void *private)
25 {
26 	cachep->private = private;
27 }
28 
29 void kmem_cache_set_non_kernel(struct kmem_cache *cachep, unsigned int val)
30 {
31 	cachep->non_kernel = val;
32 }
33 
34 unsigned long kmem_cache_get_alloc(struct kmem_cache *cachep)
35 {
36 	return cachep->size * cachep->nr_allocated;
37 }
38 
39 unsigned long kmem_cache_nr_allocated(struct kmem_cache *cachep)
40 {
41 	return cachep->nr_allocated;
42 }
43 
44 unsigned long kmem_cache_nr_tallocated(struct kmem_cache *cachep)
45 {
46 	return cachep->nr_tallocated;
47 }
48 
49 void kmem_cache_zero_nr_tallocated(struct kmem_cache *cachep)
50 {
51 	cachep->nr_tallocated = 0;
52 }
53 
54 void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
55 		int gfp)
56 {
57 	void *p;
58 
59 	if (cachep->exec_callback) {
60 		if (cachep->callback)
61 			cachep->callback(cachep->private);
62 		cachep->exec_callback = false;
63 	}
64 
65 	if (!(gfp & __GFP_DIRECT_RECLAIM)) {
66 		if (!cachep->non_kernel) {
67 			cachep->exec_callback = true;
68 			return NULL;
69 		}
70 
71 		cachep->non_kernel--;
72 	}
73 
74 	pthread_mutex_lock(&cachep->lock);
75 	if (cachep->nr_objs) {
76 		struct radix_tree_node *node = cachep->objs;
77 		cachep->nr_objs--;
78 		cachep->objs = node->parent;
79 		pthread_mutex_unlock(&cachep->lock);
80 		node->parent = NULL;
81 		p = node;
82 	} else {
83 		pthread_mutex_unlock(&cachep->lock);
84 		if (cachep->align) {
85 			if (posix_memalign(&p, cachep->align, cachep->size) < 0)
86 				return NULL;
87 		} else {
88 			p = malloc(cachep->size);
89 		}
90 
91 		if (cachep->ctor)
92 			cachep->ctor(p);
93 		else if (gfp & __GFP_ZERO)
94 			memset(p, 0, cachep->size);
95 	}
96 
97 	uatomic_inc(&cachep->nr_allocated);
98 	uatomic_inc(&nr_allocated);
99 	uatomic_inc(&cachep->nr_tallocated);
100 	if (kmalloc_verbose)
101 		printf("Allocating %p from slab\n", p);
102 	return p;
103 }
104 
105 void __kmem_cache_free_locked(struct kmem_cache *cachep, void *objp)
106 {
107 	assert(objp);
108 	if (cachep->nr_objs > 10 || cachep->align) {
109 		memset(objp, POISON_FREE, cachep->size);
110 		free(objp);
111 	} else {
112 		struct radix_tree_node *node = objp;
113 		cachep->nr_objs++;
114 		node->parent = cachep->objs;
115 		cachep->objs = node;
116 	}
117 }
118 
119 void kmem_cache_free_locked(struct kmem_cache *cachep, void *objp)
120 {
121 	uatomic_dec(&nr_allocated);
122 	uatomic_dec(&cachep->nr_allocated);
123 	if (kmalloc_verbose)
124 		printf("Freeing %p to slab\n", objp);
125 	__kmem_cache_free_locked(cachep, objp);
126 }
127 
128 void kmem_cache_free(struct kmem_cache *cachep, void *objp)
129 {
130 	pthread_mutex_lock(&cachep->lock);
131 	kmem_cache_free_locked(cachep, objp);
132 	pthread_mutex_unlock(&cachep->lock);
133 }
134 
135 void kmem_cache_free_bulk(struct kmem_cache *cachep, size_t size, void **list)
136 {
137 	if (kmalloc_verbose)
138 		pr_debug("Bulk free %p[0-%zu]\n", list, size - 1);
139 
140 	pthread_mutex_lock(&cachep->lock);
141 	for (int i = 0; i < size; i++)
142 		kmem_cache_free_locked(cachep, list[i]);
143 	pthread_mutex_unlock(&cachep->lock);
144 }
145 
146 void kmem_cache_shrink(struct kmem_cache *cachep)
147 {
148 }
149 
150 int kmem_cache_alloc_bulk(struct kmem_cache *cachep, gfp_t gfp, size_t size,
151 			  void **p)
152 {
153 	size_t i;
154 
155 	if (kmalloc_verbose)
156 		pr_debug("Bulk alloc %zu\n", size);
157 
158 	pthread_mutex_lock(&cachep->lock);
159 	if (cachep->nr_objs >= size) {
160 		struct radix_tree_node *node;
161 
162 		for (i = 0; i < size; i++) {
163 			if (!(gfp & __GFP_DIRECT_RECLAIM)) {
164 				if (!cachep->non_kernel)
165 					break;
166 				cachep->non_kernel--;
167 			}
168 
169 			node = cachep->objs;
170 			cachep->nr_objs--;
171 			cachep->objs = node->parent;
172 			p[i] = node;
173 			node->parent = NULL;
174 		}
175 		pthread_mutex_unlock(&cachep->lock);
176 	} else {
177 		pthread_mutex_unlock(&cachep->lock);
178 		for (i = 0; i < size; i++) {
179 			if (!(gfp & __GFP_DIRECT_RECLAIM)) {
180 				if (!cachep->non_kernel)
181 					break;
182 				cachep->non_kernel--;
183 			}
184 
185 			if (cachep->align) {
186 				if (posix_memalign(&p[i], cachep->align,
187 					       cachep->size) < 0)
188 					break;
189 			} else {
190 				p[i] = malloc(cachep->size);
191 				if (!p[i])
192 					break;
193 			}
194 			if (cachep->ctor)
195 				cachep->ctor(p[i]);
196 			else if (gfp & __GFP_ZERO)
197 				memset(p[i], 0, cachep->size);
198 		}
199 	}
200 
201 	if (i < size) {
202 		size = i;
203 		pthread_mutex_lock(&cachep->lock);
204 		for (i = 0; i < size; i++)
205 			__kmem_cache_free_locked(cachep, p[i]);
206 		pthread_mutex_unlock(&cachep->lock);
207 		return 0;
208 	}
209 
210 	for (i = 0; i < size; i++) {
211 		uatomic_inc(&nr_allocated);
212 		uatomic_inc(&cachep->nr_allocated);
213 		uatomic_inc(&cachep->nr_tallocated);
214 		if (kmalloc_verbose)
215 			printf("Allocating %p from slab\n", p[i]);
216 	}
217 
218 	return size;
219 }
220 
221 struct kmem_cache *
222 __kmem_cache_create_args(const char *name, unsigned int size,
223 			  struct kmem_cache_args *args,
224 			  unsigned int flags)
225 {
226 	struct kmem_cache *ret = malloc(sizeof(*ret));
227 
228 	pthread_mutex_init(&ret->lock, NULL);
229 	ret->size = size;
230 	ret->align = args->align;
231 	ret->sheaf_capacity = args->sheaf_capacity;
232 	ret->nr_objs = 0;
233 	ret->nr_allocated = 0;
234 	ret->nr_tallocated = 0;
235 	ret->objs = NULL;
236 	ret->ctor = args->ctor;
237 	ret->non_kernel = 0;
238 	ret->exec_callback = false;
239 	ret->callback = NULL;
240 	ret->private = NULL;
241 
242 	return ret;
243 }
244 
245 /*
246  * Test the test infrastructure for kem_cache_alloc/free and bulk counterparts.
247  */
248 void test_kmem_cache_bulk(void)
249 {
250 	int i;
251 	void *list[12];
252 	static struct kmem_cache *test_cache, *test_cache2;
253 
254 	/*
255 	 * Testing the bulk allocators without aligned kmem_cache to force the
256 	 * bulk alloc/free to reuse
257 	 */
258 	test_cache = kmem_cache_create("test_cache", 256, 0, SLAB_PANIC, NULL);
259 
260 	for (i = 0; i < 5; i++)
261 		list[i] = kmem_cache_alloc(test_cache, __GFP_DIRECT_RECLAIM);
262 
263 	for (i = 0; i < 5; i++)
264 		kmem_cache_free(test_cache, list[i]);
265 	assert(test_cache->nr_objs == 5);
266 
267 	kmem_cache_alloc_bulk(test_cache, __GFP_DIRECT_RECLAIM, 5, list);
268 	kmem_cache_free_bulk(test_cache, 5, list);
269 
270 	for (i = 0; i < 12 ; i++)
271 		list[i] = kmem_cache_alloc(test_cache, __GFP_DIRECT_RECLAIM);
272 
273 	for (i = 0; i < 12; i++)
274 		kmem_cache_free(test_cache, list[i]);
275 
276 	/* The last free will not be kept around */
277 	assert(test_cache->nr_objs == 11);
278 
279 	/* Aligned caches will immediately free */
280 	test_cache2 = kmem_cache_create("test_cache2", 128, 128, SLAB_PANIC, NULL);
281 
282 	kmem_cache_alloc_bulk(test_cache2, __GFP_DIRECT_RECLAIM, 10, list);
283 	kmem_cache_free_bulk(test_cache2, 10, list);
284 	assert(!test_cache2->nr_objs);
285 
286 
287 }
288