1 // SPDX-License-Identifier: GPL-2.0
3 * Test cases for SL[AOU]B/page initialization at alloc/free time.
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/init.h>
8 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/string.h>
13 #include <linux/vmalloc.h>
15 #define GARBAGE_INT (0x09A7BA9E)
16 #define GARBAGE_BYTE (0x9E)
18 #define REPORT_FAILURES_IN_FN() \
21 pr_info("%s failed %d out of %d times\n", \
22 __func__, failures, num_tests); \
24 pr_info("all %d tests in %s passed\n", \
25 num_tests, __func__); \
28 /* Calculate the number of uninitialized bytes in the buffer. */
29 static int __init
count_nonzero_bytes(void *ptr
, size_t size
)
32 unsigned char *p
= (unsigned char *)ptr
;
34 for (i
= 0; i
< size
; i
++)
40 /* Fill a buffer with garbage, skipping |skip| first bytes. */
41 static void __init
fill_with_garbage_skip(void *ptr
, int size
, size_t skip
)
43 unsigned int *p
= (unsigned int *)((char *)ptr
+ skip
);
49 while (size
>= sizeof(*p
)) {
55 memset(&p
[i
], GARBAGE_BYTE
, size
);
58 static void __init
fill_with_garbage(void *ptr
, size_t size
)
60 fill_with_garbage_skip(ptr
, size
, 0);
63 static int __init
do_alloc_pages_order(int order
, int *total_failures
)
67 size_t size
= PAGE_SIZE
<< order
;
69 page
= alloc_pages(GFP_KERNEL
, order
);
70 buf
= page_address(page
);
71 fill_with_garbage(buf
, size
);
72 __free_pages(page
, order
);
74 page
= alloc_pages(GFP_KERNEL
, order
);
75 buf
= page_address(page
);
76 if (count_nonzero_bytes(buf
, size
))
78 fill_with_garbage(buf
, size
);
79 __free_pages(page
, order
);
83 /* Test the page allocator by calling alloc_pages with different orders. */
84 static int __init
test_pages(int *total_failures
)
86 int failures
= 0, num_tests
= 0;
89 for (i
= 0; i
< 10; i
++)
90 num_tests
+= do_alloc_pages_order(i
, &failures
);
92 REPORT_FAILURES_IN_FN();
93 *total_failures
+= failures
;
97 /* Test kmalloc() with given parameters. */
98 static int __init
do_kmalloc_size(size_t size
, int *total_failures
)
102 buf
= kmalloc(size
, GFP_KERNEL
);
103 fill_with_garbage(buf
, size
);
106 buf
= kmalloc(size
, GFP_KERNEL
);
107 if (count_nonzero_bytes(buf
, size
))
109 fill_with_garbage(buf
, size
);
114 /* Test vmalloc() with given parameters. */
115 static int __init
do_vmalloc_size(size_t size
, int *total_failures
)
120 fill_with_garbage(buf
, size
);
124 if (count_nonzero_bytes(buf
, size
))
126 fill_with_garbage(buf
, size
);
131 /* Test kmalloc()/vmalloc() by allocating objects of different sizes. */
132 static int __init
test_kvmalloc(int *total_failures
)
134 int failures
= 0, num_tests
= 0;
137 for (i
= 0; i
< 20; i
++) {
139 num_tests
+= do_kmalloc_size(size
, &failures
);
140 num_tests
+= do_vmalloc_size(size
, &failures
);
143 REPORT_FAILURES_IN_FN();
144 *total_failures
+= failures
;
148 #define CTOR_BYTES (sizeof(unsigned int))
149 #define CTOR_PATTERN (0x41414141)
150 /* Initialize the first 4 bytes of the object. */
151 static void test_ctor(void *obj
)
153 *(unsigned int *)obj
= CTOR_PATTERN
;
157 * Check the invariants for the buffer allocated from a slab cache.
158 * If the cache has a test constructor, the first 4 bytes of the object must
159 * always remain equal to CTOR_PATTERN.
160 * If the cache isn't an RCU-typesafe one, or if the allocation is done with
161 * __GFP_ZERO, then the object contents must be zeroed after allocation.
162 * If the cache is an RCU-typesafe one, the object contents must never be
163 * zeroed after the first use. This is checked by memcmp() in
164 * do_kmem_cache_size().
166 static bool __init
check_buf(void *buf
, int size
, bool want_ctor
,
167 bool want_rcu
, bool want_zero
)
172 bytes
= count_nonzero_bytes(buf
, size
);
173 WARN_ON(want_ctor
&& want_zero
);
177 if (*(unsigned int *)buf
!= CTOR_PATTERN
)
187 * Test kmem_cache with given parameters:
188 * want_ctor - use a constructor;
189 * want_rcu - use SLAB_TYPESAFE_BY_RCU;
190 * want_zero - use __GFP_ZERO.
192 static int __init
do_kmem_cache_size(size_t size
, bool want_ctor
,
193 bool want_rcu
, bool want_zero
,
196 struct kmem_cache
*c
;
199 gfp_t alloc_mask
= GFP_KERNEL
| (want_zero
? __GFP_ZERO
: 0);
200 void *buf
, *buf_copy
;
202 c
= kmem_cache_create("test_cache", size
, 1,
203 want_rcu
? SLAB_TYPESAFE_BY_RCU
: 0,
204 want_ctor
? test_ctor
: NULL
);
205 for (iter
= 0; iter
< 10; iter
++) {
206 buf
= kmem_cache_alloc(c
, alloc_mask
);
207 /* Check that buf is zeroed, if it must be. */
208 fail
= check_buf(buf
, size
, want_ctor
, want_rcu
, want_zero
);
209 fill_with_garbage_skip(buf
, size
, want_ctor
? CTOR_BYTES
: 0);
212 kmem_cache_free(c
, buf
);
217 * If this is an RCU cache, use a critical section to ensure we
218 * can touch objects after they're freed.
222 * Copy the buffer to check that it's not wiped on
225 buf_copy
= kmalloc(size
, GFP_ATOMIC
);
227 memcpy(buf_copy
, buf
, size
);
229 kmem_cache_free(c
, buf
);
231 * Check that |buf| is intact after kmem_cache_free().
232 * |want_zero| is false, because we wrote garbage to
233 * the buffer already.
235 fail
|= check_buf(buf
, size
, want_ctor
, want_rcu
,
238 fail
|= (bool)memcmp(buf
, buf_copy
, size
);
243 kmem_cache_destroy(c
);
245 *total_failures
+= fail
;
250 * Check that the data written to an RCU-allocated object survives
253 static int __init
do_kmem_cache_rcu_persistent(int size
, int *total_failures
)
255 struct kmem_cache
*c
;
256 void *buf
, *buf_contents
, *saved_ptr
;
258 int i
, iter
, maxiter
= 1024;
261 c
= kmem_cache_create("test_cache", size
, size
, SLAB_TYPESAFE_BY_RCU
,
263 buf
= kmem_cache_alloc(c
, GFP_KERNEL
);
265 fill_with_garbage(buf
, size
);
266 buf_contents
= kmalloc(size
, GFP_KERNEL
);
269 used_objects
= kmalloc_array(maxiter
, sizeof(void *), GFP_KERNEL
);
274 memcpy(buf_contents
, buf
, size
);
275 kmem_cache_free(c
, buf
);
277 * Run for a fixed number of iterations. If we never hit saved_ptr,
278 * assume the test passes.
280 for (iter
= 0; iter
< maxiter
; iter
++) {
281 buf
= kmem_cache_alloc(c
, GFP_KERNEL
);
282 used_objects
[iter
] = buf
;
283 if (buf
== saved_ptr
) {
284 fail
= memcmp(buf_contents
, buf
, size
);
285 for (i
= 0; i
<= iter
; i
++)
286 kmem_cache_free(c
, used_objects
[i
]);
292 kmem_cache_destroy(c
);
296 *total_failures
+= fail
;
300 static int __init
do_kmem_cache_size_bulk(int size
, int *total_failures
)
302 struct kmem_cache
*c
;
303 int i
, iter
, maxiter
= 1024;
308 c
= kmem_cache_create("test_cache", size
, size
, 0, NULL
);
309 for (iter
= 0; (iter
< maxiter
) && !fail
; iter
++) {
310 num
= kmem_cache_alloc_bulk(c
, GFP_KERNEL
, ARRAY_SIZE(objects
),
312 for (i
= 0; i
< num
; i
++) {
313 bytes
= count_nonzero_bytes(objects
[i
], size
);
316 fill_with_garbage(objects
[i
], size
);
320 kmem_cache_free_bulk(c
, num
, objects
);
322 *total_failures
+= fail
;
327 * Test kmem_cache allocation by creating caches of different sizes, with and
328 * without constructors, with and without SLAB_TYPESAFE_BY_RCU.
330 static int __init
test_kmemcache(int *total_failures
)
332 int failures
= 0, num_tests
= 0;
334 bool ctor
, rcu
, zero
;
336 for (i
= 0; i
< 10; i
++) {
338 for (flags
= 0; flags
< 8; flags
++) {
344 num_tests
+= do_kmem_cache_size(size
, ctor
, rcu
, zero
,
347 num_tests
+= do_kmem_cache_size_bulk(size
, &failures
);
349 REPORT_FAILURES_IN_FN();
350 *total_failures
+= failures
;
354 /* Test the behavior of SLAB_TYPESAFE_BY_RCU caches of different sizes. */
355 static int __init
test_rcu_persistent(int *total_failures
)
357 int failures
= 0, num_tests
= 0;
360 for (i
= 0; i
< 10; i
++) {
362 num_tests
+= do_kmem_cache_rcu_persistent(size
, &failures
);
364 REPORT_FAILURES_IN_FN();
365 *total_failures
+= failures
;
370 * Run the tests. Each test function returns the number of executed tests and
371 * updates |failures| with the number of failed tests.
373 static int __init
test_meminit_init(void)
375 int failures
= 0, num_tests
= 0;
377 num_tests
+= test_pages(&failures
);
378 num_tests
+= test_kvmalloc(&failures
);
379 num_tests
+= test_kmemcache(&failures
);
380 num_tests
+= test_rcu_persistent(&failures
);
383 pr_info("all %d tests passed!\n", num_tests
);
385 pr_info("failures: %d out of %d\n", failures
, num_tests
);
387 return failures
? -EINVAL
: 0;
389 module_init(test_meminit_init
);
391 MODULE_LICENSE("GPL");