1 // SPDX-License-Identifier: GPL-2.0
2 #include <kunit/test.h>
3 #include <kunit/test-bug.h>
5 #include <linux/slab.h>
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include "../mm/slab.h"
10 static struct kunit_resource resource
;
11 static int slab_errors
;
14 * Wrapper function for kmem_cache_create(), which reduces 2 parameters:
15 * 'align' and 'ctor', and sets SLAB_SKIP_KFENCE flag to avoid getting an
16 * object from kfence pool, where the operation could be caught by both
17 * our test and kfence sanity check.
19 static struct kmem_cache
*test_kmem_cache_create(const char *name
,
20 unsigned int size
, slab_flags_t flags
)
22 struct kmem_cache
*s
= kmem_cache_create(name
, size
, 0,
23 (flags
| SLAB_NO_USER_FLAGS
), NULL
);
24 s
->flags
|= SLAB_SKIP_KFENCE
;
28 static void test_clobber_zone(struct kunit
*test
)
30 struct kmem_cache
*s
= test_kmem_cache_create("TestSlub_RZ_alloc", 64,
32 u8
*p
= kmem_cache_alloc(s
, GFP_KERNEL
);
34 kasan_disable_current();
37 validate_slab_cache(s
);
38 KUNIT_EXPECT_EQ(test
, 2, slab_errors
);
40 kasan_enable_current();
41 kmem_cache_free(s
, p
);
42 kmem_cache_destroy(s
);
46 static void test_next_pointer(struct kunit
*test
)
48 struct kmem_cache
*s
= test_kmem_cache_create("TestSlub_next_ptr_free",
50 u8
*p
= kmem_cache_alloc(s
, GFP_KERNEL
);
52 unsigned long *ptr_addr
;
54 kmem_cache_free(s
, p
);
56 ptr_addr
= (unsigned long *)(p
+ s
->offset
);
61 * Expecting three errors.
62 * One for the corrupted freechain and the other one for the wrong
63 * count of objects in use. The third error is fixing broken cache.
65 validate_slab_cache(s
);
66 KUNIT_EXPECT_EQ(test
, 3, slab_errors
);
69 * Try to repair corrupted freepointer.
70 * Still expecting two errors. The first for the wrong count
72 * The second error is for fixing broken cache.
77 validate_slab_cache(s
);
78 KUNIT_EXPECT_EQ(test
, 2, slab_errors
);
81 * Previous validation repaired the count of objects in use.
82 * Now expecting no error.
85 validate_slab_cache(s
);
86 KUNIT_EXPECT_EQ(test
, 0, slab_errors
);
88 kmem_cache_destroy(s
);
91 static void test_first_word(struct kunit
*test
)
93 struct kmem_cache
*s
= test_kmem_cache_create("TestSlub_1th_word_free",
95 u8
*p
= kmem_cache_alloc(s
, GFP_KERNEL
);
97 kmem_cache_free(s
, p
);
100 validate_slab_cache(s
);
101 KUNIT_EXPECT_EQ(test
, 2, slab_errors
);
103 kmem_cache_destroy(s
);
106 static void test_clobber_50th_byte(struct kunit
*test
)
108 struct kmem_cache
*s
= test_kmem_cache_create("TestSlub_50th_word_free",
110 u8
*p
= kmem_cache_alloc(s
, GFP_KERNEL
);
112 kmem_cache_free(s
, p
);
115 validate_slab_cache(s
);
116 KUNIT_EXPECT_EQ(test
, 2, slab_errors
);
118 kmem_cache_destroy(s
);
122 static void test_clobber_redzone_free(struct kunit
*test
)
124 struct kmem_cache
*s
= test_kmem_cache_create("TestSlub_RZ_free", 64,
126 u8
*p
= kmem_cache_alloc(s
, GFP_KERNEL
);
128 kasan_disable_current();
129 kmem_cache_free(s
, p
);
132 validate_slab_cache(s
);
133 KUNIT_EXPECT_EQ(test
, 2, slab_errors
);
135 kasan_enable_current();
136 kmem_cache_destroy(s
);
139 static void test_kmalloc_redzone_access(struct kunit
*test
)
141 struct kmem_cache
*s
= test_kmem_cache_create("TestSlub_RZ_kmalloc", 32,
142 SLAB_KMALLOC
|SLAB_STORE_USER
|SLAB_RED_ZONE
);
143 u8
*p
= kmalloc_trace(s
, GFP_KERNEL
, 18);
145 kasan_disable_current();
147 /* Suppress the -Warray-bounds warning */
148 OPTIMIZER_HIDE_VAR(p
);
152 validate_slab_cache(s
);
153 KUNIT_EXPECT_EQ(test
, 2, slab_errors
);
155 kasan_enable_current();
156 kmem_cache_free(s
, p
);
157 kmem_cache_destroy(s
);
160 static int test_init(struct kunit
*test
)
164 kunit_add_named_resource(test
, NULL
, NULL
, &resource
,
165 "slab_errors", &slab_errors
);
169 static struct kunit_case test_cases
[] = {
170 KUNIT_CASE(test_clobber_zone
),
173 KUNIT_CASE(test_next_pointer
),
174 KUNIT_CASE(test_first_word
),
175 KUNIT_CASE(test_clobber_50th_byte
),
178 KUNIT_CASE(test_clobber_redzone_free
),
179 KUNIT_CASE(test_kmalloc_redzone_access
),
183 static struct kunit_suite test_suite
= {
186 .test_cases
= test_cases
,
188 kunit_test_suite(test_suite
);
190 MODULE_LICENSE("GPL");