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
);
72 buf
= page_address(page
);
73 fill_with_garbage(buf
, size
);
74 __free_pages(page
, order
);
76 page
= alloc_pages(GFP_KERNEL
, order
);
79 buf
= page_address(page
);
80 if (count_nonzero_bytes(buf
, size
))
82 fill_with_garbage(buf
, size
);
83 __free_pages(page
, order
);
90 /* Test the page allocator by calling alloc_pages with different orders. */
91 static int __init
test_pages(int *total_failures
)
93 int failures
= 0, num_tests
= 0;
96 for (i
= 0; i
< NR_PAGE_ORDERS
; i
++)
97 num_tests
+= do_alloc_pages_order(i
, &failures
);
99 REPORT_FAILURES_IN_FN();
100 *total_failures
+= failures
;
104 /* Test kmalloc() with given parameters. */
105 static int __init
do_kmalloc_size(size_t size
, int *total_failures
)
109 buf
= kmalloc(size
, GFP_KERNEL
);
112 fill_with_garbage(buf
, size
);
115 buf
= kmalloc(size
, GFP_KERNEL
);
118 if (count_nonzero_bytes(buf
, size
))
120 fill_with_garbage(buf
, size
);
128 /* Test vmalloc() with given parameters. */
129 static int __init
do_vmalloc_size(size_t size
, int *total_failures
)
136 fill_with_garbage(buf
, size
);
142 if (count_nonzero_bytes(buf
, size
))
144 fill_with_garbage(buf
, size
);
152 /* Test kmalloc()/vmalloc() by allocating objects of different sizes. */
153 static int __init
test_kvmalloc(int *total_failures
)
155 int failures
= 0, num_tests
= 0;
158 for (i
= 0; i
< 20; i
++) {
160 num_tests
+= do_kmalloc_size(size
, &failures
);
161 num_tests
+= do_vmalloc_size(size
, &failures
);
164 REPORT_FAILURES_IN_FN();
165 *total_failures
+= failures
;
169 #define CTOR_BYTES (sizeof(unsigned int))
170 #define CTOR_PATTERN (0x41414141)
171 /* Initialize the first 4 bytes of the object. */
172 static void test_ctor(void *obj
)
174 *(unsigned int *)obj
= CTOR_PATTERN
;
178 * Check the invariants for the buffer allocated from a slab cache.
179 * If the cache has a test constructor, the first 4 bytes of the object must
180 * always remain equal to CTOR_PATTERN.
181 * If the cache isn't an RCU-typesafe one, or if the allocation is done with
182 * __GFP_ZERO, then the object contents must be zeroed after allocation.
183 * If the cache is an RCU-typesafe one, the object contents must never be
184 * zeroed after the first use. This is checked by memcmp() in
185 * do_kmem_cache_size().
187 static bool __init
check_buf(void *buf
, int size
, bool want_ctor
,
188 bool want_rcu
, bool want_zero
)
193 bytes
= count_nonzero_bytes(buf
, size
);
194 WARN_ON(want_ctor
&& want_zero
);
198 if (*(unsigned int *)buf
!= CTOR_PATTERN
)
207 #define BULK_SIZE 100
208 static void *bulk_array
[BULK_SIZE
];
211 * Test kmem_cache with given parameters:
212 * want_ctor - use a constructor;
213 * want_rcu - use SLAB_TYPESAFE_BY_RCU;
214 * want_zero - use __GFP_ZERO.
216 static int __init
do_kmem_cache_size(size_t size
, bool want_ctor
,
217 bool want_rcu
, bool want_zero
,
220 struct kmem_cache
*c
;
223 gfp_t alloc_mask
= GFP_KERNEL
| (want_zero
? __GFP_ZERO
: 0);
224 void *buf
, *buf_copy
;
226 c
= kmem_cache_create("test_cache", size
, 1,
227 want_rcu
? SLAB_TYPESAFE_BY_RCU
: 0,
228 want_ctor
? test_ctor
: NULL
);
229 for (iter
= 0; iter
< 10; iter
++) {
230 /* Do a test of bulk allocations */
231 if (!want_rcu
&& !want_ctor
) {
234 ret
= kmem_cache_alloc_bulk(c
, alloc_mask
, BULK_SIZE
, bulk_array
);
239 for (i
= 0; i
< ret
; i
++)
240 fail
|= check_buf(bulk_array
[i
], size
, want_ctor
, want_rcu
, want_zero
);
241 kmem_cache_free_bulk(c
, ret
, bulk_array
);
245 buf
= kmem_cache_alloc(c
, alloc_mask
);
246 /* Check that buf is zeroed, if it must be. */
247 fail
|= check_buf(buf
, size
, want_ctor
, want_rcu
, want_zero
);
248 fill_with_garbage_skip(buf
, size
, want_ctor
? CTOR_BYTES
: 0);
251 kmem_cache_free(c
, buf
);
256 * If this is an RCU cache, use a critical section to ensure we
257 * can touch objects after they're freed.
261 * Copy the buffer to check that it's not wiped on
264 buf_copy
= kmalloc(size
, GFP_ATOMIC
);
266 memcpy(buf_copy
, buf
, size
);
268 kmem_cache_free(c
, buf
);
270 * Check that |buf| is intact after kmem_cache_free().
271 * |want_zero| is false, because we wrote garbage to
272 * the buffer already.
274 fail
|= check_buf(buf
, size
, want_ctor
, want_rcu
,
277 fail
|= (bool)memcmp(buf
, buf_copy
, size
);
282 kmem_cache_destroy(c
);
284 *total_failures
+= fail
;
289 * Check that the data written to an RCU-allocated object survives
292 static int __init
do_kmem_cache_rcu_persistent(int size
, int *total_failures
)
294 struct kmem_cache
*c
;
295 void *buf
, *buf_contents
, *saved_ptr
;
297 int i
, iter
, maxiter
= 1024;
300 c
= kmem_cache_create("test_cache", size
, size
, SLAB_TYPESAFE_BY_RCU
,
302 buf
= kmem_cache_alloc(c
, GFP_KERNEL
);
306 fill_with_garbage(buf
, size
);
307 buf_contents
= kmalloc(size
, GFP_KERNEL
);
309 kmem_cache_free(c
, buf
);
312 used_objects
= kmalloc_array(maxiter
, sizeof(void *), GFP_KERNEL
);
314 kmem_cache_free(c
, buf
);
318 memcpy(buf_contents
, buf
, size
);
319 kmem_cache_free(c
, buf
);
321 * Run for a fixed number of iterations. If we never hit saved_ptr,
322 * assume the test passes.
324 for (iter
= 0; iter
< maxiter
; iter
++) {
325 buf
= kmem_cache_alloc(c
, GFP_KERNEL
);
326 used_objects
[iter
] = buf
;
327 if (buf
== saved_ptr
) {
328 fail
= memcmp(buf_contents
, buf
, size
);
329 for (i
= 0; i
<= iter
; i
++)
330 kmem_cache_free(c
, used_objects
[i
]);
335 for (iter
= 0; iter
< maxiter
; iter
++)
336 kmem_cache_free(c
, used_objects
[iter
]);
342 kmem_cache_destroy(c
);
343 *total_failures
+= fail
;
347 static int __init
do_kmem_cache_size_bulk(int size
, int *total_failures
)
349 struct kmem_cache
*c
;
350 int i
, iter
, maxiter
= 1024;
355 c
= kmem_cache_create("test_cache", size
, size
, 0, NULL
);
356 for (iter
= 0; (iter
< maxiter
) && !fail
; iter
++) {
357 num
= kmem_cache_alloc_bulk(c
, GFP_KERNEL
, ARRAY_SIZE(objects
),
359 for (i
= 0; i
< num
; i
++) {
360 bytes
= count_nonzero_bytes(objects
[i
], size
);
363 fill_with_garbage(objects
[i
], size
);
367 kmem_cache_free_bulk(c
, num
, objects
);
369 kmem_cache_destroy(c
);
370 *total_failures
+= fail
;
375 * Test kmem_cache allocation by creating caches of different sizes, with and
376 * without constructors, with and without SLAB_TYPESAFE_BY_RCU.
378 static int __init
test_kmemcache(int *total_failures
)
380 int failures
= 0, num_tests
= 0;
382 bool ctor
, rcu
, zero
;
384 for (i
= 0; i
< 10; i
++) {
386 for (flags
= 0; flags
< 8; flags
++) {
392 num_tests
+= do_kmem_cache_size(size
, ctor
, rcu
, zero
,
395 num_tests
+= do_kmem_cache_size_bulk(size
, &failures
);
397 REPORT_FAILURES_IN_FN();
398 *total_failures
+= failures
;
402 /* Test the behavior of SLAB_TYPESAFE_BY_RCU caches of different sizes. */
403 static int __init
test_rcu_persistent(int *total_failures
)
405 int failures
= 0, num_tests
= 0;
408 for (i
= 0; i
< 10; i
++) {
410 num_tests
+= do_kmem_cache_rcu_persistent(size
, &failures
);
412 REPORT_FAILURES_IN_FN();
413 *total_failures
+= failures
;
418 * Run the tests. Each test function returns the number of executed tests and
419 * updates |failures| with the number of failed tests.
421 static int __init
test_meminit_init(void)
423 int failures
= 0, num_tests
= 0;
425 num_tests
+= test_pages(&failures
);
426 num_tests
+= test_kvmalloc(&failures
);
427 num_tests
+= test_kmemcache(&failures
);
428 num_tests
+= test_rcu_persistent(&failures
);
431 pr_info("all %d tests passed!\n", num_tests
);
433 pr_info("failures: %d out of %d\n", failures
, num_tests
);
435 return failures
? -EINVAL
: 0;
437 module_init(test_meminit_init
);
439 MODULE_DESCRIPTION("Test cases for SL[AOU]B/page initialization at alloc/free time");
440 MODULE_LICENSE("GPL");