Skip to content

Commit 4e1c44b

Browse files
committed
kunit, slub: add test_kfree_rcu() and test_leak_destroy()
Add a test that will create cache, allocate one object, kfree_rcu() it and attempt to destroy it. As long as the usage of kvfree_rcu_barrier() in kmem_cache_destroy() works correctly, there should be no warnings in dmesg and the test should pass. Additionally add a test_leak_destroy() test that leaks an object on purpose and verifies that kmem_cache_destroy() catches it. Signed-off-by: Vlastimil Babka <[email protected]>
1 parent 6c6c47b commit 4e1c44b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib/slub_kunit.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/slab.h>
66
#include <linux/module.h>
77
#include <linux/kernel.h>
8+
#include <linux/rcupdate.h>
89
#include "../mm/slab.h"
910

1011
static struct kunit_resource resource;
@@ -157,6 +158,34 @@ static void test_kmalloc_redzone_access(struct kunit *test)
157158
kmem_cache_destroy(s);
158159
}
159160

161+
struct test_kfree_rcu_struct {
162+
struct rcu_head rcu;
163+
};
164+
165+
static void test_kfree_rcu(struct kunit *test)
166+
{
167+
struct kmem_cache *s = test_kmem_cache_create("TestSlub_kfree_rcu",
168+
sizeof(struct test_kfree_rcu_struct),
169+
SLAB_NO_MERGE);
170+
struct test_kfree_rcu_struct *p = kmem_cache_alloc(s, GFP_KERNEL);
171+
172+
kfree_rcu(p, rcu);
173+
kmem_cache_destroy(s);
174+
175+
KUNIT_EXPECT_EQ(test, 0, slab_errors);
176+
}
177+
178+
static void test_leak_destroy(struct kunit *test)
179+
{
180+
struct kmem_cache *s = test_kmem_cache_create("TestSlub_kfree_rcu",
181+
64, SLAB_NO_MERGE);
182+
kmem_cache_alloc(s, GFP_KERNEL);
183+
184+
kmem_cache_destroy(s);
185+
186+
KUNIT_EXPECT_EQ(test, 1, slab_errors);
187+
}
188+
160189
static int test_init(struct kunit *test)
161190
{
162191
slab_errors = 0;
@@ -177,6 +206,8 @@ static struct kunit_case test_cases[] = {
177206

178207
KUNIT_CASE(test_clobber_redzone_free),
179208
KUNIT_CASE(test_kmalloc_redzone_access),
209+
KUNIT_CASE(test_kfree_rcu),
210+
KUNIT_CASE(test_leak_destroy),
180211
{}
181212
};
182213

0 commit comments

Comments
 (0)