Skip to content

Commit 0a75685

Browse files
bjh83shuahkh
authored andcommitted
kunit: test: add test resource management API
Create a common API for test managed resources like memory and test objects. A lot of times a test will want to set up infrastructure to be used in test cases; this could be anything from just wanting to allocate some memory to setting up a driver stack; this defines facilities for creating "test resources" which are managed by the test infrastructure and are automatically cleaned up at the conclusion of the test. Signed-off-by: Brendan Higgins <brendanhiggins@google.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Reviewed-by: Logan Gunthorpe <logang@deltatee.com> Reviewed-by: Stephen Boyd <sboyd@kernel.org> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 914cc63 commit 0a75685

2 files changed

Lines changed: 350 additions & 0 deletions

File tree

‎include/kunit/test.h‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,72 @@
99
#ifndef _KUNIT_TEST_H
1010
#define _KUNIT_TEST_H
1111

12+
#include <linux/slab.h>
1213
#include <linux/types.h>
1314

15+
struct kunit_resource;
16+
17+
typedef int (*kunit_resource_init_t)(struct kunit_resource *, void *);
18+
typedef void (*kunit_resource_free_t)(struct kunit_resource *);
19+
20+
/**
21+
* struct kunit_resource - represents a *test managed resource*
22+
* @allocation: for the user to store arbitrary data.
23+
* @free: a user supplied function to free the resource. Populated by
24+
* kunit_alloc_resource().
25+
*
26+
* Represents a *test managed resource*, a resource which will automatically be
27+
* cleaned up at the end of a test case.
28+
*
29+
* Example:
30+
*
31+
* .. code-block:: c
32+
*
33+
* struct kunit_kmalloc_params {
34+
* size_t size;
35+
* gfp_t gfp;
36+
* };
37+
*
38+
* static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
39+
* {
40+
* struct kunit_kmalloc_params *params = context;
41+
* res->allocation = kmalloc(params->size, params->gfp);
42+
*
43+
* if (!res->allocation)
44+
* return -ENOMEM;
45+
*
46+
* return 0;
47+
* }
48+
*
49+
* static void kunit_kmalloc_free(struct kunit_resource *res)
50+
* {
51+
* kfree(res->allocation);
52+
* }
53+
*
54+
* void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
55+
* {
56+
* struct kunit_kmalloc_params params;
57+
* struct kunit_resource *res;
58+
*
59+
* params.size = size;
60+
* params.gfp = gfp;
61+
*
62+
* res = kunit_alloc_resource(test, kunit_kmalloc_init,
63+
* kunit_kmalloc_free, &params);
64+
* if (res)
65+
* return res->allocation;
66+
*
67+
* return NULL;
68+
* }
69+
*/
70+
struct kunit_resource {
71+
void *allocation;
72+
kunit_resource_free_t free;
73+
74+
/* private: internal use only. */
75+
struct list_head node;
76+
};
77+
1478
struct kunit;
1579

1680
/**
@@ -114,6 +178,13 @@ struct kunit {
114178
* with the test case have terminated.
115179
*/
116180
bool success; /* Read only after test_case finishes! */
181+
spinlock_t lock; /* Guards all mutable test state. */
182+
/*
183+
* Because resources is a list that may be updated multiple times (with
184+
* new resources) from any thread associated with a test case, we must
185+
* protect it with some type of lock.
186+
*/
187+
struct list_head resources; /* Protected by lock. */
117188
};
118189

119190
void kunit_init_test(struct kunit *test, const char *name);
@@ -147,6 +218,122 @@ int kunit_run_tests(struct kunit_suite *suite);
147218
} \
148219
late_initcall(kunit_suite_init##suite)
149220

221+
/*
222+
* Like kunit_alloc_resource() below, but returns the struct kunit_resource
223+
* object that contains the allocation. This is mostly for testing purposes.
224+
*/
225+
struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
226+
kunit_resource_init_t init,
227+
kunit_resource_free_t free,
228+
gfp_t internal_gfp,
229+
void *context);
230+
231+
/**
232+
* kunit_alloc_resource() - Allocates a *test managed resource*.
233+
* @test: The test context object.
234+
* @init: a user supplied function to initialize the resource.
235+
* @free: a user supplied function to free the resource.
236+
* @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL
237+
* @context: for the user to pass in arbitrary data to the init function.
238+
*
239+
* Allocates a *test managed resource*, a resource which will automatically be
240+
* cleaned up at the end of a test case. See &struct kunit_resource for an
241+
* example.
242+
*
243+
* NOTE: KUnit needs to allocate memory for each kunit_resource object. You must
244+
* specify an @internal_gfp that is compatible with the use context of your
245+
* resource.
246+
*/
247+
static inline void *kunit_alloc_resource(struct kunit *test,
248+
kunit_resource_init_t init,
249+
kunit_resource_free_t free,
250+
gfp_t internal_gfp,
251+
void *context)
252+
{
253+
struct kunit_resource *res;
254+
255+
res = kunit_alloc_and_get_resource(test, init, free, internal_gfp,
256+
context);
257+
258+
if (res)
259+
return res->allocation;
260+
261+
return NULL;
262+
}
263+
264+
typedef bool (*kunit_resource_match_t)(struct kunit *test,
265+
const void *res,
266+
void *match_data);
267+
268+
/**
269+
* kunit_resource_instance_match() - Match a resource with the same instance.
270+
* @test: Test case to which the resource belongs.
271+
* @res: The data stored in kunit_resource->allocation.
272+
* @match_data: The resource pointer to match against.
273+
*
274+
* An instance of kunit_resource_match_t that matches a resource whose
275+
* allocation matches @match_data.
276+
*/
277+
static inline bool kunit_resource_instance_match(struct kunit *test,
278+
const void *res,
279+
void *match_data)
280+
{
281+
return res == match_data;
282+
}
283+
284+
/**
285+
* kunit_resource_destroy() - Find a kunit_resource and destroy it.
286+
* @test: Test case to which the resource belongs.
287+
* @match: Match function. Returns whether a given resource matches @match_data.
288+
* @free: Must match free on the kunit_resource to free.
289+
* @match_data: Data passed into @match.
290+
*
291+
* Free the latest kunit_resource of @test for which @free matches the
292+
* kunit_resource_free_t associated with the resource and for which @match
293+
* returns true.
294+
*
295+
* RETURNS:
296+
* 0 if kunit_resource is found and freed, -ENOENT if not found.
297+
*/
298+
int kunit_resource_destroy(struct kunit *test,
299+
kunit_resource_match_t match,
300+
kunit_resource_free_t free,
301+
void *match_data);
302+
303+
/**
304+
* kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
305+
* @test: The test context object.
306+
* @size: The size in bytes of the desired memory.
307+
* @gfp: flags passed to underlying kmalloc().
308+
*
309+
* Just like `kmalloc(...)`, except the allocation is managed by the test case
310+
* and is automatically cleaned up after the test case concludes. See &struct
311+
* kunit_resource for more information.
312+
*/
313+
void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp);
314+
315+
/**
316+
* kunit_kfree() - Like kfree except for allocations managed by KUnit.
317+
* @test: The test case to which the resource belongs.
318+
* @ptr: The memory allocation to free.
319+
*/
320+
void kunit_kfree(struct kunit *test, const void *ptr);
321+
322+
/**
323+
* kunit_kzalloc() - Just like kunit_kmalloc(), but zeroes the allocation.
324+
* @test: The test context object.
325+
* @size: The size in bytes of the desired memory.
326+
* @gfp: flags passed to underlying kmalloc().
327+
*
328+
* See kzalloc() and kunit_kmalloc() for more information.
329+
*/
330+
static inline void *kunit_kzalloc(struct kunit *test, size_t size, gfp_t gfp)
331+
{
332+
return kunit_kmalloc(test, size, gfp | __GFP_ZERO);
333+
}
334+
335+
void kunit_cleanup(struct kunit *test);
336+
150337
void __printf(3, 4) kunit_printk(const char *level,
151338
const struct kunit *test,
152339
const char *fmt, ...);

‎lib/kunit/test.c‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ static void kunit_print_test_case_ok_not_ok(struct kunit_case *test_case,
122122

123123
void kunit_init_test(struct kunit *test, const char *name)
124124
{
125+
spin_lock_init(&test->lock);
126+
INIT_LIST_HEAD(&test->resources);
125127
test->name = name;
126128
test->success = true;
127129
}
@@ -153,6 +155,8 @@ static void kunit_run_case(struct kunit_suite *suite,
153155
if (suite->exit)
154156
suite->exit(&test);
155157

158+
kunit_cleanup(&test);
159+
156160
test_case->success = test.success;
157161
}
158162

@@ -173,6 +177,165 @@ int kunit_run_tests(struct kunit_suite *suite)
173177
return 0;
174178
}
175179

180+
struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
181+
kunit_resource_init_t init,
182+
kunit_resource_free_t free,
183+
gfp_t internal_gfp,
184+
void *context)
185+
{
186+
struct kunit_resource *res;
187+
int ret;
188+
189+
res = kzalloc(sizeof(*res), internal_gfp);
190+
if (!res)
191+
return NULL;
192+
193+
ret = init(res, context);
194+
if (ret)
195+
return NULL;
196+
197+
res->free = free;
198+
spin_lock(&test->lock);
199+
list_add_tail(&res->node, &test->resources);
200+
spin_unlock(&test->lock);
201+
202+
return res;
203+
}
204+
205+
static void kunit_resource_free(struct kunit *test, struct kunit_resource *res)
206+
{
207+
res->free(res);
208+
kfree(res);
209+
}
210+
211+
static struct kunit_resource *kunit_resource_find(struct kunit *test,
212+
kunit_resource_match_t match,
213+
kunit_resource_free_t free,
214+
void *match_data)
215+
{
216+
struct kunit_resource *resource;
217+
218+
lockdep_assert_held(&test->lock);
219+
220+
list_for_each_entry_reverse(resource, &test->resources, node) {
221+
if (resource->free != free)
222+
continue;
223+
if (match(test, resource->allocation, match_data))
224+
return resource;
225+
}
226+
227+
return NULL;
228+
}
229+
230+
static struct kunit_resource *kunit_resource_remove(
231+
struct kunit *test,
232+
kunit_resource_match_t match,
233+
kunit_resource_free_t free,
234+
void *match_data)
235+
{
236+
struct kunit_resource *resource;
237+
238+
spin_lock(&test->lock);
239+
resource = kunit_resource_find(test, match, free, match_data);
240+
if (resource)
241+
list_del(&resource->node);
242+
spin_unlock(&test->lock);
243+
244+
return resource;
245+
}
246+
247+
int kunit_resource_destroy(struct kunit *test,
248+
kunit_resource_match_t match,
249+
kunit_resource_free_t free,
250+
void *match_data)
251+
{
252+
struct kunit_resource *resource;
253+
254+
resource = kunit_resource_remove(test, match, free, match_data);
255+
256+
if (!resource)
257+
return -ENOENT;
258+
259+
kunit_resource_free(test, resource);
260+
return 0;
261+
}
262+
263+
struct kunit_kmalloc_params {
264+
size_t size;
265+
gfp_t gfp;
266+
};
267+
268+
static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
269+
{
270+
struct kunit_kmalloc_params *params = context;
271+
272+
res->allocation = kmalloc(params->size, params->gfp);
273+
if (!res->allocation)
274+
return -ENOMEM;
275+
276+
return 0;
277+
}
278+
279+
static void kunit_kmalloc_free(struct kunit_resource *res)
280+
{
281+
kfree(res->allocation);
282+
}
283+
284+
void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
285+
{
286+
struct kunit_kmalloc_params params = {
287+
.size = size,
288+
.gfp = gfp
289+
};
290+
291+
return kunit_alloc_resource(test,
292+
kunit_kmalloc_init,
293+
kunit_kmalloc_free,
294+
gfp,
295+
&params);
296+
}
297+
298+
void kunit_kfree(struct kunit *test, const void *ptr)
299+
{
300+
int rc;
301+
302+
rc = kunit_resource_destroy(test,
303+
kunit_resource_instance_match,
304+
kunit_kmalloc_free,
305+
(void *)ptr);
306+
307+
WARN_ON(rc);
308+
}
309+
310+
void kunit_cleanup(struct kunit *test)
311+
{
312+
struct kunit_resource *resource;
313+
314+
/*
315+
* test->resources is a stack - each allocation must be freed in the
316+
* reverse order from which it was added since one resource may depend
317+
* on another for its entire lifetime.
318+
* Also, we cannot use the normal list_for_each constructs, even the
319+
* safe ones because *arbitrary* nodes may be deleted when
320+
* kunit_resource_free is called; the list_for_each_safe variants only
321+
* protect against the current node being deleted, not the next.
322+
*/
323+
while (true) {
324+
spin_lock(&test->lock);
325+
if (list_empty(&test->resources)) {
326+
spin_unlock(&test->lock);
327+
break;
328+
}
329+
resource = list_last_entry(&test->resources,
330+
struct kunit_resource,
331+
node);
332+
list_del(&resource->node);
333+
spin_unlock(&test->lock);
334+
335+
kunit_resource_free(test, resource);
336+
}
337+
}
338+
176339
void kunit_printk(const char *level,
177340
const struct kunit *test,
178341
const char *fmt, ...)

0 commit comments

Comments
 (0)