lib/tests/slub_kunit.c
Source file repositories/reference/linux-study-clean/lib/tests/slub_kunit.c
File Facts
- System
- Linux kernel
- Corpus path
lib/tests/slub_kunit.c- Extension
.c- Size
- 10093 bytes
- Lines
- 422
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kunit/test.hkunit/test-bug.hlinux/mm.hlinux/slab.hlinux/module.hlinux/kernel.hlinux/rcupdate.hlinux/delay.hlinux/perf_event.h../mm/slab.h
Detected Declarations
struct test_kfree_rcu_structstruct cache_destroy_workstruct test_nolock_contextfunction kmem_cache_createfunction test_clobber_zonefunction test_next_pointerfunction test_first_wordfunction test_clobber_50th_bytefunction test_clobber_redzone_freefunction test_kmalloc_redzone_accessfunction test_kfree_rcufunction cache_destroy_workfnfunction test_kfree_rcu_wq_destroyfunction test_leak_destroyfunction test_krealloc_redzone_zeroingfunction overflow_handler_test_kmalloc_kfree_nolockfunction test_kmalloc_kfree_nolockfunction test_init
Annotated Snippet
struct test_kfree_rcu_struct {
struct rcu_head rcu;
};
static void test_kfree_rcu(struct kunit *test)
{
struct kmem_cache *s;
struct test_kfree_rcu_struct *p;
if (IS_BUILTIN(CONFIG_SLUB_KUNIT_TEST))
kunit_skip(test, "can't do kfree_rcu() when test is built-in");
s = test_kmem_cache_create("TestSlub_kfree_rcu",
sizeof(struct test_kfree_rcu_struct),
SLAB_NO_MERGE);
p = kmem_cache_alloc(s, GFP_KERNEL);
kfree_rcu(p, rcu);
kmem_cache_destroy(s);
KUNIT_EXPECT_EQ(test, 0, slab_errors);
}
struct cache_destroy_work {
struct work_struct work;
struct kmem_cache *s;
};
static void cache_destroy_workfn(struct work_struct *w)
{
struct cache_destroy_work *cdw;
cdw = container_of(w, struct cache_destroy_work, work);
kmem_cache_destroy(cdw->s);
}
#define KMEM_CACHE_DESTROY_NR 10
static void test_kfree_rcu_wq_destroy(struct kunit *test)
{
struct test_kfree_rcu_struct *p;
struct cache_destroy_work cdw;
struct workqueue_struct *wq;
struct kmem_cache *s;
unsigned int delay;
int i;
if (IS_BUILTIN(CONFIG_SLUB_KUNIT_TEST))
kunit_skip(test, "can't do kfree_rcu() when test is built-in");
INIT_WORK_ONSTACK(&cdw.work, cache_destroy_workfn);
wq = alloc_workqueue("test_kfree_rcu_destroy_wq",
WQ_HIGHPRI | WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
if (!wq)
kunit_skip(test, "failed to alloc wq");
for (i = 0; i < KMEM_CACHE_DESTROY_NR; i++) {
s = test_kmem_cache_create("TestSlub_kfree_rcu_wq_destroy",
sizeof(struct test_kfree_rcu_struct),
SLAB_NO_MERGE);
if (!s)
kunit_skip(test, "failed to create cache");
delay = get_random_u8();
p = kmem_cache_alloc(s, GFP_KERNEL);
kfree_rcu(p, rcu);
cdw.s = s;
msleep(delay);
queue_work(wq, &cdw.work);
flush_work(&cdw.work);
}
destroy_workqueue(wq);
KUNIT_EXPECT_EQ(test, 0, slab_errors);
}
static void test_leak_destroy(struct kunit *test)
{
struct kmem_cache *s = test_kmem_cache_create("TestSlub_leak_destroy",
64, SLAB_NO_MERGE);
kmem_cache_alloc(s, GFP_KERNEL);
kmem_cache_destroy(s);
KUNIT_EXPECT_EQ(test, 2, slab_errors);
}
Annotation
- Immediate include surface: `kunit/test.h`, `kunit/test-bug.h`, `linux/mm.h`, `linux/slab.h`, `linux/module.h`, `linux/kernel.h`, `linux/rcupdate.h`, `linux/delay.h`.
- Detected declarations: `struct test_kfree_rcu_struct`, `struct cache_destroy_work`, `struct test_nolock_context`, `function kmem_cache_create`, `function test_clobber_zone`, `function test_next_pointer`, `function test_first_word`, `function test_clobber_50th_byte`, `function test_clobber_redzone_free`, `function test_kmalloc_redzone_access`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.