^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Test cases for SL[AOU]B/page initialization at alloc/free time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define GARBAGE_INT (0x09A7BA9E)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define GARBAGE_BYTE (0x9E)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define REPORT_FAILURES_IN_FN() \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) if (failures) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) pr_info("%s failed %d out of %d times\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) __func__, failures, num_tests); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) else \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) pr_info("all %d tests in %s passed\n", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) num_tests, __func__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* Calculate the number of uninitialized bytes in the buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int __init count_nonzero_bytes(void *ptr, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned char *p = (unsigned char *)ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) for (i = 0; i < size; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (p[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ret++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* Fill a buffer with garbage, skipping |skip| first bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static void __init fill_with_garbage_skip(void *ptr, int size, size_t skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned int *p = (unsigned int *)((char *)ptr + skip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) WARN_ON(skip > size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) size -= skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) while (size >= sizeof(*p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) p[i] = GARBAGE_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) size -= sizeof(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) memset(&p[i], GARBAGE_BYTE, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static void __init fill_with_garbage(void *ptr, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) fill_with_garbage_skip(ptr, size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static int __init do_alloc_pages_order(int order, int *total_failures)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) size_t size = PAGE_SIZE << order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) page = alloc_pages(GFP_KERNEL, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) buf = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) fill_with_garbage(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) __free_pages(page, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) page = alloc_pages(GFP_KERNEL, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) buf = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (count_nonzero_bytes(buf, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) (*total_failures)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) fill_with_garbage(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) __free_pages(page, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Test the page allocator by calling alloc_pages with different orders. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int __init test_pages(int *total_failures)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int failures = 0, num_tests = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) for (i = 0; i < 10; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) num_tests += do_alloc_pages_order(i, &failures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) REPORT_FAILURES_IN_FN();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) *total_failures += failures;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return num_tests;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* Test kmalloc() with given parameters. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int __init do_kmalloc_size(size_t size, int *total_failures)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) buf = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) fill_with_garbage(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) buf = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (count_nonzero_bytes(buf, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) (*total_failures)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) fill_with_garbage(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* Test vmalloc() with given parameters. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static int __init do_vmalloc_size(size_t size, int *total_failures)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) buf = vmalloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) fill_with_garbage(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) vfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) buf = vmalloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (count_nonzero_bytes(buf, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) (*total_failures)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) fill_with_garbage(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) vfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* Test kmalloc()/vmalloc() by allocating objects of different sizes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int __init test_kvmalloc(int *total_failures)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) int failures = 0, num_tests = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int i, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) for (i = 0; i < 20; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) size = 1 << i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) num_tests += do_kmalloc_size(size, &failures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) num_tests += do_vmalloc_size(size, &failures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) REPORT_FAILURES_IN_FN();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *total_failures += failures;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return num_tests;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #define CTOR_BYTES (sizeof(unsigned int))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define CTOR_PATTERN (0x41414141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* Initialize the first 4 bytes of the object. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static void test_ctor(void *obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) *(unsigned int *)obj = CTOR_PATTERN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * Check the invariants for the buffer allocated from a slab cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * If the cache has a test constructor, the first 4 bytes of the object must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * always remain equal to CTOR_PATTERN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * If the cache isn't an RCU-typesafe one, or if the allocation is done with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * __GFP_ZERO, then the object contents must be zeroed after allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * If the cache is an RCU-typesafe one, the object contents must never be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * zeroed after the first use. This is checked by memcmp() in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * do_kmem_cache_size().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static bool __init check_buf(void *buf, int size, bool want_ctor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) bool want_rcu, bool want_zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) bool fail = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) bytes = count_nonzero_bytes(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) WARN_ON(want_ctor && want_zero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (want_zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (want_ctor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (*(unsigned int *)buf != CTOR_PATTERN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) fail = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) fail = !want_rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #define BULK_SIZE 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static void *bulk_array[BULK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * Test kmem_cache with given parameters:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * want_ctor - use a constructor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * want_rcu - use SLAB_TYPESAFE_BY_RCU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * want_zero - use __GFP_ZERO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static int __init do_kmem_cache_size(size_t size, bool want_ctor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) bool want_rcu, bool want_zero,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) int *total_failures)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct kmem_cache *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) int iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) bool fail = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) gfp_t alloc_mask = GFP_KERNEL | (want_zero ? __GFP_ZERO : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) void *buf, *buf_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) c = kmem_cache_create("test_cache", size, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) want_rcu ? SLAB_TYPESAFE_BY_RCU : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) want_ctor ? test_ctor : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) for (iter = 0; iter < 10; iter++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /* Do a test of bulk allocations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (!want_rcu && !want_ctor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) ret = kmem_cache_alloc_bulk(c, alloc_mask, BULK_SIZE, bulk_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) fail = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) for (i = 0; i < ret; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) fail |= check_buf(bulk_array[i], size, want_ctor, want_rcu, want_zero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) kmem_cache_free_bulk(c, ret, bulk_array);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) buf = kmem_cache_alloc(c, alloc_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /* Check that buf is zeroed, if it must be. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) fail |= check_buf(buf, size, want_ctor, want_rcu, want_zero);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) fill_with_garbage_skip(buf, size, want_ctor ? CTOR_BYTES : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!want_rcu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) kmem_cache_free(c, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * If this is an RCU cache, use a critical section to ensure we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * can touch objects after they're freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * Copy the buffer to check that it's not wiped on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * free().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) buf_copy = kmalloc(size, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (buf_copy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) memcpy(buf_copy, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) kmem_cache_free(c, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * Check that |buf| is intact after kmem_cache_free().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * |want_zero| is false, because we wrote garbage to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * the buffer already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) fail |= check_buf(buf, size, want_ctor, want_rcu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (buf_copy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) fail |= (bool)memcmp(buf, buf_copy, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) kfree(buf_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) kmem_cache_destroy(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) *total_failures += fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * Check that the data written to an RCU-allocated object survives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * reallocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int __init do_kmem_cache_rcu_persistent(int size, int *total_failures)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct kmem_cache *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) void *buf, *buf_contents, *saved_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) void **used_objects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int i, iter, maxiter = 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) bool fail = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) c = kmem_cache_create("test_cache", size, size, SLAB_TYPESAFE_BY_RCU,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) buf = kmem_cache_alloc(c, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) saved_ptr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) fill_with_garbage(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) buf_contents = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (!buf_contents)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) used_objects = kmalloc_array(maxiter, sizeof(void *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (!used_objects) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) kfree(buf_contents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) memcpy(buf_contents, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) kmem_cache_free(c, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * Run for a fixed number of iterations. If we never hit saved_ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * assume the test passes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) for (iter = 0; iter < maxiter; iter++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) buf = kmem_cache_alloc(c, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) used_objects[iter] = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (buf == saved_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) fail = memcmp(buf_contents, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) for (i = 0; i <= iter; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) kmem_cache_free(c, used_objects[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) goto free_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) free_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) kmem_cache_destroy(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) kfree(buf_contents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) kfree(used_objects);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) *total_failures += fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static int __init do_kmem_cache_size_bulk(int size, int *total_failures)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct kmem_cache *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int i, iter, maxiter = 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) int num, bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) bool fail = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) void *objects[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) c = kmem_cache_create("test_cache", size, size, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) for (iter = 0; (iter < maxiter) && !fail; iter++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) num = kmem_cache_alloc_bulk(c, GFP_KERNEL, ARRAY_SIZE(objects),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) objects);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) bytes = count_nonzero_bytes(objects[i], size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) fail = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) fill_with_garbage(objects[i], size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) kmem_cache_free_bulk(c, num, objects);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) kmem_cache_destroy(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) *total_failures += fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * Test kmem_cache allocation by creating caches of different sizes, with and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * without constructors, with and without SLAB_TYPESAFE_BY_RCU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static int __init test_kmemcache(int *total_failures)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) int failures = 0, num_tests = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) int i, flags, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) bool ctor, rcu, zero;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) for (i = 0; i < 10; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) size = 8 << i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) for (flags = 0; flags < 8; flags++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) ctor = flags & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) rcu = flags & 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) zero = flags & 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (ctor & zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) num_tests += do_kmem_cache_size(size, ctor, rcu, zero,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) &failures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) num_tests += do_kmem_cache_size_bulk(size, &failures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) REPORT_FAILURES_IN_FN();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) *total_failures += failures;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return num_tests;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /* Test the behavior of SLAB_TYPESAFE_BY_RCU caches of different sizes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static int __init test_rcu_persistent(int *total_failures)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) int failures = 0, num_tests = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) int i, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) for (i = 0; i < 10; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) size = 8 << i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) num_tests += do_kmem_cache_rcu_persistent(size, &failures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) REPORT_FAILURES_IN_FN();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) *total_failures += failures;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return num_tests;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * Run the tests. Each test function returns the number of executed tests and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * updates |failures| with the number of failed tests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static int __init test_meminit_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int failures = 0, num_tests = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) num_tests += test_pages(&failures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) num_tests += test_kvmalloc(&failures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) num_tests += test_kmemcache(&failures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) num_tests += test_rcu_persistent(&failures);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (failures == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) pr_info("all %d tests passed!\n", num_tests);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) pr_info("failures: %d out of %d\n", failures, num_tests);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return failures ? -EINVAL : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) module_init(test_meminit_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) MODULE_LICENSE("GPL");