xref: /kernel/linux/linux-5.10/lib/test_meminit.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Test cases for SL[AOU]B/page initialization at alloc/free time.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/init.h>
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/mm.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/string.h>
138c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define GARBAGE_INT (0x09A7BA9E)
168c2ecf20Sopenharmony_ci#define GARBAGE_BYTE (0x9E)
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define REPORT_FAILURES_IN_FN() \
198c2ecf20Sopenharmony_ci	do {	\
208c2ecf20Sopenharmony_ci		if (failures)	\
218c2ecf20Sopenharmony_ci			pr_info("%s failed %d out of %d times\n",	\
228c2ecf20Sopenharmony_ci				__func__, failures, num_tests);		\
238c2ecf20Sopenharmony_ci		else		\
248c2ecf20Sopenharmony_ci			pr_info("all %d tests in %s passed\n",		\
258c2ecf20Sopenharmony_ci				num_tests, __func__);			\
268c2ecf20Sopenharmony_ci	} while (0)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* Calculate the number of uninitialized bytes in the buffer. */
298c2ecf20Sopenharmony_cistatic int __init count_nonzero_bytes(void *ptr, size_t size)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	int i, ret = 0;
328c2ecf20Sopenharmony_ci	unsigned char *p = (unsigned char *)ptr;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	for (i = 0; i < size; i++)
358c2ecf20Sopenharmony_ci		if (p[i])
368c2ecf20Sopenharmony_ci			ret++;
378c2ecf20Sopenharmony_ci	return ret;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* Fill a buffer with garbage, skipping |skip| first bytes. */
418c2ecf20Sopenharmony_cistatic void __init fill_with_garbage_skip(void *ptr, int size, size_t skip)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	unsigned int *p = (unsigned int *)((char *)ptr + skip);
448c2ecf20Sopenharmony_ci	int i = 0;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	WARN_ON(skip > size);
478c2ecf20Sopenharmony_ci	size -= skip;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	while (size >= sizeof(*p)) {
508c2ecf20Sopenharmony_ci		p[i] = GARBAGE_INT;
518c2ecf20Sopenharmony_ci		i++;
528c2ecf20Sopenharmony_ci		size -= sizeof(*p);
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci	if (size)
558c2ecf20Sopenharmony_ci		memset(&p[i], GARBAGE_BYTE, size);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic void __init fill_with_garbage(void *ptr, size_t size)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	fill_with_garbage_skip(ptr, size, 0);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic int __init do_alloc_pages_order(int order, int *total_failures)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct page *page;
668c2ecf20Sopenharmony_ci	void *buf;
678c2ecf20Sopenharmony_ci	size_t size = PAGE_SIZE << order;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	page = alloc_pages(GFP_KERNEL, order);
708c2ecf20Sopenharmony_ci	buf = page_address(page);
718c2ecf20Sopenharmony_ci	fill_with_garbage(buf, size);
728c2ecf20Sopenharmony_ci	__free_pages(page, order);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	page = alloc_pages(GFP_KERNEL, order);
758c2ecf20Sopenharmony_ci	buf = page_address(page);
768c2ecf20Sopenharmony_ci	if (count_nonzero_bytes(buf, size))
778c2ecf20Sopenharmony_ci		(*total_failures)++;
788c2ecf20Sopenharmony_ci	fill_with_garbage(buf, size);
798c2ecf20Sopenharmony_ci	__free_pages(page, order);
808c2ecf20Sopenharmony_ci	return 1;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/* Test the page allocator by calling alloc_pages with different orders. */
848c2ecf20Sopenharmony_cistatic int __init test_pages(int *total_failures)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	int failures = 0, num_tests = 0;
878c2ecf20Sopenharmony_ci	int i;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_ORDER; i++)
908c2ecf20Sopenharmony_ci		num_tests += do_alloc_pages_order(i, &failures);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	REPORT_FAILURES_IN_FN();
938c2ecf20Sopenharmony_ci	*total_failures += failures;
948c2ecf20Sopenharmony_ci	return num_tests;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/* Test kmalloc() with given parameters. */
988c2ecf20Sopenharmony_cistatic int __init do_kmalloc_size(size_t size, int *total_failures)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	void *buf;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	buf = kmalloc(size, GFP_KERNEL);
1038c2ecf20Sopenharmony_ci	fill_with_garbage(buf, size);
1048c2ecf20Sopenharmony_ci	kfree(buf);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	buf = kmalloc(size, GFP_KERNEL);
1078c2ecf20Sopenharmony_ci	if (count_nonzero_bytes(buf, size))
1088c2ecf20Sopenharmony_ci		(*total_failures)++;
1098c2ecf20Sopenharmony_ci	fill_with_garbage(buf, size);
1108c2ecf20Sopenharmony_ci	kfree(buf);
1118c2ecf20Sopenharmony_ci	return 1;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/* Test vmalloc() with given parameters. */
1158c2ecf20Sopenharmony_cistatic int __init do_vmalloc_size(size_t size, int *total_failures)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	void *buf;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	buf = vmalloc(size);
1208c2ecf20Sopenharmony_ci	fill_with_garbage(buf, size);
1218c2ecf20Sopenharmony_ci	vfree(buf);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	buf = vmalloc(size);
1248c2ecf20Sopenharmony_ci	if (count_nonzero_bytes(buf, size))
1258c2ecf20Sopenharmony_ci		(*total_failures)++;
1268c2ecf20Sopenharmony_ci	fill_with_garbage(buf, size);
1278c2ecf20Sopenharmony_ci	vfree(buf);
1288c2ecf20Sopenharmony_ci	return 1;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/* Test kmalloc()/vmalloc() by allocating objects of different sizes. */
1328c2ecf20Sopenharmony_cistatic int __init test_kvmalloc(int *total_failures)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	int failures = 0, num_tests = 0;
1358c2ecf20Sopenharmony_ci	int i, size;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	for (i = 0; i < 20; i++) {
1388c2ecf20Sopenharmony_ci		size = 1 << i;
1398c2ecf20Sopenharmony_ci		num_tests += do_kmalloc_size(size, &failures);
1408c2ecf20Sopenharmony_ci		num_tests += do_vmalloc_size(size, &failures);
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	REPORT_FAILURES_IN_FN();
1448c2ecf20Sopenharmony_ci	*total_failures += failures;
1458c2ecf20Sopenharmony_ci	return num_tests;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#define CTOR_BYTES (sizeof(unsigned int))
1498c2ecf20Sopenharmony_ci#define CTOR_PATTERN (0x41414141)
1508c2ecf20Sopenharmony_ci/* Initialize the first 4 bytes of the object. */
1518c2ecf20Sopenharmony_cistatic void test_ctor(void *obj)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	*(unsigned int *)obj = CTOR_PATTERN;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci/*
1578c2ecf20Sopenharmony_ci * Check the invariants for the buffer allocated from a slab cache.
1588c2ecf20Sopenharmony_ci * If the cache has a test constructor, the first 4 bytes of the object must
1598c2ecf20Sopenharmony_ci * always remain equal to CTOR_PATTERN.
1608c2ecf20Sopenharmony_ci * If the cache isn't an RCU-typesafe one, or if the allocation is done with
1618c2ecf20Sopenharmony_ci * __GFP_ZERO, then the object contents must be zeroed after allocation.
1628c2ecf20Sopenharmony_ci * If the cache is an RCU-typesafe one, the object contents must never be
1638c2ecf20Sopenharmony_ci * zeroed after the first use. This is checked by memcmp() in
1648c2ecf20Sopenharmony_ci * do_kmem_cache_size().
1658c2ecf20Sopenharmony_ci */
1668c2ecf20Sopenharmony_cistatic bool __init check_buf(void *buf, int size, bool want_ctor,
1678c2ecf20Sopenharmony_ci			     bool want_rcu, bool want_zero)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	int bytes;
1708c2ecf20Sopenharmony_ci	bool fail = false;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	bytes = count_nonzero_bytes(buf, size);
1738c2ecf20Sopenharmony_ci	WARN_ON(want_ctor && want_zero);
1748c2ecf20Sopenharmony_ci	if (want_zero)
1758c2ecf20Sopenharmony_ci		return bytes;
1768c2ecf20Sopenharmony_ci	if (want_ctor) {
1778c2ecf20Sopenharmony_ci		if (*(unsigned int *)buf != CTOR_PATTERN)
1788c2ecf20Sopenharmony_ci			fail = 1;
1798c2ecf20Sopenharmony_ci	} else {
1808c2ecf20Sopenharmony_ci		if (bytes)
1818c2ecf20Sopenharmony_ci			fail = !want_rcu;
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci	return fail;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci#define BULK_SIZE 100
1878c2ecf20Sopenharmony_cistatic void *bulk_array[BULK_SIZE];
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/*
1908c2ecf20Sopenharmony_ci * Test kmem_cache with given parameters:
1918c2ecf20Sopenharmony_ci *  want_ctor - use a constructor;
1928c2ecf20Sopenharmony_ci *  want_rcu - use SLAB_TYPESAFE_BY_RCU;
1938c2ecf20Sopenharmony_ci *  want_zero - use __GFP_ZERO.
1948c2ecf20Sopenharmony_ci */
1958c2ecf20Sopenharmony_cistatic int __init do_kmem_cache_size(size_t size, bool want_ctor,
1968c2ecf20Sopenharmony_ci				     bool want_rcu, bool want_zero,
1978c2ecf20Sopenharmony_ci				     int *total_failures)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	struct kmem_cache *c;
2008c2ecf20Sopenharmony_ci	int iter;
2018c2ecf20Sopenharmony_ci	bool fail = false;
2028c2ecf20Sopenharmony_ci	gfp_t alloc_mask = GFP_KERNEL | (want_zero ? __GFP_ZERO : 0);
2038c2ecf20Sopenharmony_ci	void *buf, *buf_copy;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	c = kmem_cache_create("test_cache", size, 1,
2068c2ecf20Sopenharmony_ci			      want_rcu ? SLAB_TYPESAFE_BY_RCU : 0,
2078c2ecf20Sopenharmony_ci			      want_ctor ? test_ctor : NULL);
2088c2ecf20Sopenharmony_ci	for (iter = 0; iter < 10; iter++) {
2098c2ecf20Sopenharmony_ci		/* Do a test of bulk allocations */
2108c2ecf20Sopenharmony_ci		if (!want_rcu && !want_ctor) {
2118c2ecf20Sopenharmony_ci			int ret;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci			ret = kmem_cache_alloc_bulk(c, alloc_mask, BULK_SIZE, bulk_array);
2148c2ecf20Sopenharmony_ci			if (!ret) {
2158c2ecf20Sopenharmony_ci				fail = true;
2168c2ecf20Sopenharmony_ci			} else {
2178c2ecf20Sopenharmony_ci				int i;
2188c2ecf20Sopenharmony_ci				for (i = 0; i < ret; i++)
2198c2ecf20Sopenharmony_ci					fail |= check_buf(bulk_array[i], size, want_ctor, want_rcu, want_zero);
2208c2ecf20Sopenharmony_ci				kmem_cache_free_bulk(c, ret, bulk_array);
2218c2ecf20Sopenharmony_ci			}
2228c2ecf20Sopenharmony_ci		}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci		buf = kmem_cache_alloc(c, alloc_mask);
2258c2ecf20Sopenharmony_ci		/* Check that buf is zeroed, if it must be. */
2268c2ecf20Sopenharmony_ci		fail |= check_buf(buf, size, want_ctor, want_rcu, want_zero);
2278c2ecf20Sopenharmony_ci		fill_with_garbage_skip(buf, size, want_ctor ? CTOR_BYTES : 0);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci		if (!want_rcu) {
2308c2ecf20Sopenharmony_ci			kmem_cache_free(c, buf);
2318c2ecf20Sopenharmony_ci			continue;
2328c2ecf20Sopenharmony_ci		}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci		/*
2358c2ecf20Sopenharmony_ci		 * If this is an RCU cache, use a critical section to ensure we
2368c2ecf20Sopenharmony_ci		 * can touch objects after they're freed.
2378c2ecf20Sopenharmony_ci		 */
2388c2ecf20Sopenharmony_ci		rcu_read_lock();
2398c2ecf20Sopenharmony_ci		/*
2408c2ecf20Sopenharmony_ci		 * Copy the buffer to check that it's not wiped on
2418c2ecf20Sopenharmony_ci		 * free().
2428c2ecf20Sopenharmony_ci		 */
2438c2ecf20Sopenharmony_ci		buf_copy = kmalloc(size, GFP_ATOMIC);
2448c2ecf20Sopenharmony_ci		if (buf_copy)
2458c2ecf20Sopenharmony_ci			memcpy(buf_copy, buf, size);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci		kmem_cache_free(c, buf);
2488c2ecf20Sopenharmony_ci		/*
2498c2ecf20Sopenharmony_ci		 * Check that |buf| is intact after kmem_cache_free().
2508c2ecf20Sopenharmony_ci		 * |want_zero| is false, because we wrote garbage to
2518c2ecf20Sopenharmony_ci		 * the buffer already.
2528c2ecf20Sopenharmony_ci		 */
2538c2ecf20Sopenharmony_ci		fail |= check_buf(buf, size, want_ctor, want_rcu,
2548c2ecf20Sopenharmony_ci				  false);
2558c2ecf20Sopenharmony_ci		if (buf_copy) {
2568c2ecf20Sopenharmony_ci			fail |= (bool)memcmp(buf, buf_copy, size);
2578c2ecf20Sopenharmony_ci			kfree(buf_copy);
2588c2ecf20Sopenharmony_ci		}
2598c2ecf20Sopenharmony_ci		rcu_read_unlock();
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci	kmem_cache_destroy(c);
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	*total_failures += fail;
2648c2ecf20Sopenharmony_ci	return 1;
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci/*
2688c2ecf20Sopenharmony_ci * Check that the data written to an RCU-allocated object survives
2698c2ecf20Sopenharmony_ci * reallocation.
2708c2ecf20Sopenharmony_ci */
2718c2ecf20Sopenharmony_cistatic int __init do_kmem_cache_rcu_persistent(int size, int *total_failures)
2728c2ecf20Sopenharmony_ci{
2738c2ecf20Sopenharmony_ci	struct kmem_cache *c;
2748c2ecf20Sopenharmony_ci	void *buf, *buf_contents, *saved_ptr;
2758c2ecf20Sopenharmony_ci	void **used_objects;
2768c2ecf20Sopenharmony_ci	int i, iter, maxiter = 1024;
2778c2ecf20Sopenharmony_ci	bool fail = false;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	c = kmem_cache_create("test_cache", size, size, SLAB_TYPESAFE_BY_RCU,
2808c2ecf20Sopenharmony_ci			      NULL);
2818c2ecf20Sopenharmony_ci	buf = kmem_cache_alloc(c, GFP_KERNEL);
2828c2ecf20Sopenharmony_ci	saved_ptr = buf;
2838c2ecf20Sopenharmony_ci	fill_with_garbage(buf, size);
2848c2ecf20Sopenharmony_ci	buf_contents = kmalloc(size, GFP_KERNEL);
2858c2ecf20Sopenharmony_ci	if (!buf_contents)
2868c2ecf20Sopenharmony_ci		goto out;
2878c2ecf20Sopenharmony_ci	used_objects = kmalloc_array(maxiter, sizeof(void *), GFP_KERNEL);
2888c2ecf20Sopenharmony_ci	if (!used_objects) {
2898c2ecf20Sopenharmony_ci		kfree(buf_contents);
2908c2ecf20Sopenharmony_ci		goto out;
2918c2ecf20Sopenharmony_ci	}
2928c2ecf20Sopenharmony_ci	memcpy(buf_contents, buf, size);
2938c2ecf20Sopenharmony_ci	kmem_cache_free(c, buf);
2948c2ecf20Sopenharmony_ci	/*
2958c2ecf20Sopenharmony_ci	 * Run for a fixed number of iterations. If we never hit saved_ptr,
2968c2ecf20Sopenharmony_ci	 * assume the test passes.
2978c2ecf20Sopenharmony_ci	 */
2988c2ecf20Sopenharmony_ci	for (iter = 0; iter < maxiter; iter++) {
2998c2ecf20Sopenharmony_ci		buf = kmem_cache_alloc(c, GFP_KERNEL);
3008c2ecf20Sopenharmony_ci		used_objects[iter] = buf;
3018c2ecf20Sopenharmony_ci		if (buf == saved_ptr) {
3028c2ecf20Sopenharmony_ci			fail = memcmp(buf_contents, buf, size);
3038c2ecf20Sopenharmony_ci			for (i = 0; i <= iter; i++)
3048c2ecf20Sopenharmony_ci				kmem_cache_free(c, used_objects[i]);
3058c2ecf20Sopenharmony_ci			goto free_out;
3068c2ecf20Sopenharmony_ci		}
3078c2ecf20Sopenharmony_ci	}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_cifree_out:
3108c2ecf20Sopenharmony_ci	kmem_cache_destroy(c);
3118c2ecf20Sopenharmony_ci	kfree(buf_contents);
3128c2ecf20Sopenharmony_ci	kfree(used_objects);
3138c2ecf20Sopenharmony_ciout:
3148c2ecf20Sopenharmony_ci	*total_failures += fail;
3158c2ecf20Sopenharmony_ci	return 1;
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic int __init do_kmem_cache_size_bulk(int size, int *total_failures)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	struct kmem_cache *c;
3218c2ecf20Sopenharmony_ci	int i, iter, maxiter = 1024;
3228c2ecf20Sopenharmony_ci	int num, bytes;
3238c2ecf20Sopenharmony_ci	bool fail = false;
3248c2ecf20Sopenharmony_ci	void *objects[10];
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	c = kmem_cache_create("test_cache", size, size, 0, NULL);
3278c2ecf20Sopenharmony_ci	for (iter = 0; (iter < maxiter) && !fail; iter++) {
3288c2ecf20Sopenharmony_ci		num = kmem_cache_alloc_bulk(c, GFP_KERNEL, ARRAY_SIZE(objects),
3298c2ecf20Sopenharmony_ci					    objects);
3308c2ecf20Sopenharmony_ci		for (i = 0; i < num; i++) {
3318c2ecf20Sopenharmony_ci			bytes = count_nonzero_bytes(objects[i], size);
3328c2ecf20Sopenharmony_ci			if (bytes)
3338c2ecf20Sopenharmony_ci				fail = true;
3348c2ecf20Sopenharmony_ci			fill_with_garbage(objects[i], size);
3358c2ecf20Sopenharmony_ci		}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci		if (num)
3388c2ecf20Sopenharmony_ci			kmem_cache_free_bulk(c, num, objects);
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci	kmem_cache_destroy(c);
3418c2ecf20Sopenharmony_ci	*total_failures += fail;
3428c2ecf20Sopenharmony_ci	return 1;
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci/*
3468c2ecf20Sopenharmony_ci * Test kmem_cache allocation by creating caches of different sizes, with and
3478c2ecf20Sopenharmony_ci * without constructors, with and without SLAB_TYPESAFE_BY_RCU.
3488c2ecf20Sopenharmony_ci */
3498c2ecf20Sopenharmony_cistatic int __init test_kmemcache(int *total_failures)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	int failures = 0, num_tests = 0;
3528c2ecf20Sopenharmony_ci	int i, flags, size;
3538c2ecf20Sopenharmony_ci	bool ctor, rcu, zero;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	for (i = 0; i < 10; i++) {
3568c2ecf20Sopenharmony_ci		size = 8 << i;
3578c2ecf20Sopenharmony_ci		for (flags = 0; flags < 8; flags++) {
3588c2ecf20Sopenharmony_ci			ctor = flags & 1;
3598c2ecf20Sopenharmony_ci			rcu = flags & 2;
3608c2ecf20Sopenharmony_ci			zero = flags & 4;
3618c2ecf20Sopenharmony_ci			if (ctor & zero)
3628c2ecf20Sopenharmony_ci				continue;
3638c2ecf20Sopenharmony_ci			num_tests += do_kmem_cache_size(size, ctor, rcu, zero,
3648c2ecf20Sopenharmony_ci							&failures);
3658c2ecf20Sopenharmony_ci		}
3668c2ecf20Sopenharmony_ci		num_tests += do_kmem_cache_size_bulk(size, &failures);
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci	REPORT_FAILURES_IN_FN();
3698c2ecf20Sopenharmony_ci	*total_failures += failures;
3708c2ecf20Sopenharmony_ci	return num_tests;
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci/* Test the behavior of SLAB_TYPESAFE_BY_RCU caches of different sizes. */
3748c2ecf20Sopenharmony_cistatic int __init test_rcu_persistent(int *total_failures)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	int failures = 0, num_tests = 0;
3778c2ecf20Sopenharmony_ci	int i, size;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	for (i = 0; i < 10; i++) {
3808c2ecf20Sopenharmony_ci		size = 8 << i;
3818c2ecf20Sopenharmony_ci		num_tests += do_kmem_cache_rcu_persistent(size, &failures);
3828c2ecf20Sopenharmony_ci	}
3838c2ecf20Sopenharmony_ci	REPORT_FAILURES_IN_FN();
3848c2ecf20Sopenharmony_ci	*total_failures += failures;
3858c2ecf20Sopenharmony_ci	return num_tests;
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci/*
3898c2ecf20Sopenharmony_ci * Run the tests. Each test function returns the number of executed tests and
3908c2ecf20Sopenharmony_ci * updates |failures| with the number of failed tests.
3918c2ecf20Sopenharmony_ci */
3928c2ecf20Sopenharmony_cistatic int __init test_meminit_init(void)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	int failures = 0, num_tests = 0;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	num_tests += test_pages(&failures);
3978c2ecf20Sopenharmony_ci	num_tests += test_kvmalloc(&failures);
3988c2ecf20Sopenharmony_ci	num_tests += test_kmemcache(&failures);
3998c2ecf20Sopenharmony_ci	num_tests += test_rcu_persistent(&failures);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	if (failures == 0)
4028c2ecf20Sopenharmony_ci		pr_info("all %d tests passed!\n", num_tests);
4038c2ecf20Sopenharmony_ci	else
4048c2ecf20Sopenharmony_ci		pr_info("failures: %d out of %d\n", failures, num_tests);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	return failures ? -EINVAL : 0;
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_cimodule_init(test_meminit_init);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
411