// SPDX-License-Identifier: GPL-2.0 /* * Test cases for SL[AOU]B/page initialization at alloc/free time. */ #define pr_fmt(fmt) … #include <linux/init.h> #include <linux/kernel.h> #include <linux/mm.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/vmalloc.h> #define GARBAGE_INT … #define GARBAGE_BYTE … #define REPORT_FAILURES_IN_FN() … /* Calculate the number of uninitialized bytes in the buffer. */ static int __init count_nonzero_bytes(void *ptr, size_t size) { … } /* Fill a buffer with garbage, skipping |skip| first bytes. */ static void __init fill_with_garbage_skip(void *ptr, int size, size_t skip) { … } static void __init fill_with_garbage(void *ptr, size_t size) { … } static int __init do_alloc_pages_order(int order, int *total_failures) { … } /* Test the page allocator by calling alloc_pages with different orders. */ static int __init test_pages(int *total_failures) { … } /* Test kmalloc() with given parameters. */ static int __init do_kmalloc_size(size_t size, int *total_failures) { … } /* Test vmalloc() with given parameters. */ static int __init do_vmalloc_size(size_t size, int *total_failures) { … } /* Test kmalloc()/vmalloc() by allocating objects of different sizes. */ static int __init test_kvmalloc(int *total_failures) { … } #define CTOR_BYTES … #define CTOR_PATTERN … /* Initialize the first 4 bytes of the object. */ static void test_ctor(void *obj) { … } /* * Check the invariants for the buffer allocated from a slab cache. * If the cache has a test constructor, the first 4 bytes of the object must * always remain equal to CTOR_PATTERN. * If the cache isn't an RCU-typesafe one, or if the allocation is done with * __GFP_ZERO, then the object contents must be zeroed after allocation. * If the cache is an RCU-typesafe one, the object contents must never be * zeroed after the first use. This is checked by memcmp() in * do_kmem_cache_size(). */ static bool __init check_buf(void *buf, int size, bool want_ctor, bool want_rcu, bool want_zero) { … } #define BULK_SIZE … static void *bulk_array[BULK_SIZE]; /* * Test kmem_cache with given parameters: * want_ctor - use a constructor; * want_rcu - use SLAB_TYPESAFE_BY_RCU; * want_zero - use __GFP_ZERO. */ static int __init do_kmem_cache_size(size_t size, bool want_ctor, bool want_rcu, bool want_zero, int *total_failures) { … } /* * Check that the data written to an RCU-allocated object survives * reallocation. */ static int __init do_kmem_cache_rcu_persistent(int size, int *total_failures) { … } static int __init do_kmem_cache_size_bulk(int size, int *total_failures) { … } /* * Test kmem_cache allocation by creating caches of different sizes, with and * without constructors, with and without SLAB_TYPESAFE_BY_RCU. */ static int __init test_kmemcache(int *total_failures) { … } /* Test the behavior of SLAB_TYPESAFE_BY_RCU caches of different sizes. */ static int __init test_rcu_persistent(int *total_failures) { … } /* * Run the tests. Each test function returns the number of executed tests and * updates |failures| with the number of failed tests. */ static int __init test_meminit_init(void) { … } module_init(…) …; MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;