Skip to content

Commit d4cdd14

Browse files
alan-maguireshuahkh
authored andcommitted
kunit: generalize kunit_resource API beyond allocated resources
In its original form, the kunit resources API - consisting the struct kunit_resource and associated functions - was focused on adding allocated resources during test operation that would be automatically cleaned up on test completion. The recent RFC patch proposing converting KASAN tests to KUnit [1] showed another potential model - where outside of test context, but with a pointer to the test state, we wish to access/update test-related data, but expressly want to avoid allocations. It turns out we can generalize the kunit_resource to support static resources where the struct kunit_resource * is passed in and initialized for us. As part of this work, we also change the "allocation" field to the more general "data" name, as instead of associating an allocation, we can associate a pointer to static data. Static data is distinguished by a NULL free functions. A test is added to cover using kunit_add_resource() with a static resource and data. Finally we also make use of the kernel's krefcount interfaces to manage reference counting of KUnit resources. The motivation for this is simple; if we have kernel threads accessing and using resources (say via kunit_find_resource()) we need to ensure we do not remove said resources (or indeed free them if they were dynamically allocated) until the reference count reaches zero. A new function - kunit_put_resource() - is added to handle this, and it should be called after a thread using kunit_find_resource() is finished with the retrieved resource. We ensure that the functions needed to look up, use and drop reference count are "static inline"-defined so that they can be used by builtin code as well as modules in the case that KUnit is built as a module. A cosmetic change here also; I've tried moving to kunit_[action]_resource() as the format of function names for consistency and readability. [1] https://lkml.org/lkml/2020/2/26/1286 Signed-off-by: Alan Maguire <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 4877846 commit d4cdd14

File tree

4 files changed

+268
-129
lines changed

4 files changed

+268
-129
lines changed

include/kunit/test.h

Lines changed: 126 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/module.h>
1616
#include <linux/slab.h>
1717
#include <linux/types.h>
18+
#include <linux/kref.h>
1819

1920
struct kunit_resource;
2021

@@ -23,13 +24,19 @@ typedef void (*kunit_resource_free_t)(struct kunit_resource *);
2324

2425
/**
2526
* struct kunit_resource - represents a *test managed resource*
26-
* @allocation: for the user to store arbitrary data.
27+
* @data: for the user to store arbitrary data.
2728
* @free: a user supplied function to free the resource. Populated by
28-
* kunit_alloc_resource().
29+
* kunit_resource_alloc().
2930
*
3031
* Represents a *test managed resource*, a resource which will automatically be
3132
* cleaned up at the end of a test case.
3233
*
34+
* Resources are reference counted so if a resource is retrieved via
35+
* kunit_alloc_and_get_resource() or kunit_find_resource(), we need
36+
* to call kunit_put_resource() to reduce the resource reference count
37+
* when finished with it. Note that kunit_alloc_resource() does not require a
38+
* kunit_resource_put() because it does not retrieve the resource itself.
39+
*
3340
* Example:
3441
*
3542
* .. code-block:: c
@@ -42,40 +49,36 @@ typedef void (*kunit_resource_free_t)(struct kunit_resource *);
4249
* static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
4350
* {
4451
* struct kunit_kmalloc_params *params = context;
45-
* res->allocation = kmalloc(params->size, params->gfp);
52+
* res->data = kmalloc(params->size, params->gfp);
4653
*
47-
* if (!res->allocation)
54+
* if (!res->data)
4855
* return -ENOMEM;
4956
*
5057
* return 0;
5158
* }
5259
*
5360
* static void kunit_kmalloc_free(struct kunit_resource *res)
5461
* {
55-
* kfree(res->allocation);
62+
* kfree(res->data);
5663
* }
5764
*
5865
* void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
5966
* {
6067
* struct kunit_kmalloc_params params;
61-
* struct kunit_resource *res;
6268
*
6369
* params.size = size;
6470
* params.gfp = gfp;
6571
*
66-
* res = kunit_alloc_resource(test, kunit_kmalloc_init,
72+
* return kunit_alloc_resource(test, kunit_kmalloc_init,
6773
* kunit_kmalloc_free, &params);
68-
* if (res)
69-
* return res->allocation;
70-
*
71-
* return NULL;
7274
* }
7375
*/
7476
struct kunit_resource {
75-
void *allocation;
76-
kunit_resource_free_t free;
77+
void *data;
7778

7879
/* private: internal use only. */
80+
kunit_resource_free_t free;
81+
struct kref refcount;
7982
struct list_head node;
8083
};
8184

@@ -283,6 +286,64 @@ struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
283286
gfp_t internal_gfp,
284287
void *context);
285288

289+
/**
290+
* kunit_get_resource() - Hold resource for use. Should not need to be used
291+
* by most users as we automatically get resources
292+
* retrieved by kunit_find_resource*().
293+
* @res: resource
294+
*/
295+
static inline void kunit_get_resource(struct kunit_resource *res)
296+
{
297+
kref_get(&res->refcount);
298+
}
299+
300+
/*
301+
* Called when refcount reaches zero via kunit_put_resources();
302+
* should not be called directly.
303+
*/
304+
static inline void kunit_release_resource(struct kref *kref)
305+
{
306+
struct kunit_resource *res = container_of(kref, struct kunit_resource,
307+
refcount);
308+
309+
/* If free function is defined, resource was dynamically allocated. */
310+
if (res->free) {
311+
res->free(res);
312+
kfree(res);
313+
}
314+
}
315+
316+
/**
317+
* kunit_put_resource() - When caller is done with retrieved resource,
318+
* kunit_put_resource() should be called to drop
319+
* reference count. The resource list maintains
320+
* a reference count on resources, so if no users
321+
* are utilizing a resource and it is removed from
322+
* the resource list, it will be freed via the
323+
* associated free function (if any). Only
324+
* needs to be used if we alloc_and_get() or
325+
* find() resource.
326+
* @res: resource
327+
*/
328+
static inline void kunit_put_resource(struct kunit_resource *res)
329+
{
330+
kref_put(&res->refcount, kunit_release_resource);
331+
}
332+
333+
/**
334+
* kunit_add_resource() - Add a *test managed resource*.
335+
* @test: The test context object.
336+
* @init: a user-supplied function to initialize the result (if needed). If
337+
* none is supplied, the resource data value is simply set to @data.
338+
* If an init function is supplied, @data is passed to it instead.
339+
* @free: a user-supplied function to free the resource (if needed).
340+
* @data: value to pass to init function or set in resource data field.
341+
*/
342+
int kunit_add_resource(struct kunit *test,
343+
kunit_resource_init_t init,
344+
kunit_resource_free_t free,
345+
struct kunit_resource *res,
346+
void *data);
286347
/**
287348
* kunit_alloc_resource() - Allocates a *test managed resource*.
288349
* @test: The test context object.
@@ -295,7 +356,7 @@ struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
295356
* cleaned up at the end of a test case. See &struct kunit_resource for an
296357
* example.
297358
*
298-
* NOTE: KUnit needs to allocate memory for each kunit_resource object. You must
359+
* Note: KUnit needs to allocate memory for a kunit_resource object. You must
299360
* specify an @internal_gfp that is compatible with the use context of your
300361
* resource.
301362
*/
@@ -307,54 +368,89 @@ static inline void *kunit_alloc_resource(struct kunit *test,
307368
{
308369
struct kunit_resource *res;
309370

310-
res = kunit_alloc_and_get_resource(test, init, free, internal_gfp,
311-
context);
371+
res = kzalloc(sizeof(*res), internal_gfp);
372+
if (!res)
373+
return NULL;
312374

313-
if (res)
314-
return res->allocation;
375+
if (!kunit_add_resource(test, init, free, res, context))
376+
return res->data;
315377

316378
return NULL;
317379
}
318380

319381
typedef bool (*kunit_resource_match_t)(struct kunit *test,
320-
const void *res,
382+
struct kunit_resource *res,
321383
void *match_data);
322384

323385
/**
324386
* kunit_resource_instance_match() - Match a resource with the same instance.
325387
* @test: Test case to which the resource belongs.
326-
* @res: The data stored in kunit_resource->allocation.
388+
* @res: The resource.
327389
* @match_data: The resource pointer to match against.
328390
*
329391
* An instance of kunit_resource_match_t that matches a resource whose
330392
* allocation matches @match_data.
331393
*/
332394
static inline bool kunit_resource_instance_match(struct kunit *test,
333-
const void *res,
395+
struct kunit_resource *res,
334396
void *match_data)
335397
{
336-
return res == match_data;
398+
return res->data == match_data;
337399
}
338400

339401
/**
340-
* kunit_resource_destroy() - Find a kunit_resource and destroy it.
402+
* kunit_find_resource() - Find a resource using match function/data.
403+
* @test: Test case to which the resource belongs.
404+
* @match: match function to be applied to resources/match data.
405+
* @match_data: data to be used in matching.
406+
*/
407+
static inline struct kunit_resource *
408+
kunit_find_resource(struct kunit *test,
409+
kunit_resource_match_t match,
410+
void *match_data)
411+
{
412+
struct kunit_resource *res, *found = NULL;
413+
414+
spin_lock(&test->lock);
415+
416+
list_for_each_entry_reverse(res, &test->resources, node) {
417+
if (match(test, res, (void *)match_data)) {
418+
found = res;
419+
kunit_get_resource(found);
420+
break;
421+
}
422+
}
423+
424+
spin_unlock(&test->lock);
425+
426+
return found;
427+
}
428+
429+
/**
430+
* kunit_destroy_resource() - Find a kunit_resource and destroy it.
341431
* @test: Test case to which the resource belongs.
342432
* @match: Match function. Returns whether a given resource matches @match_data.
343-
* @free: Must match free on the kunit_resource to free.
344433
* @match_data: Data passed into @match.
345434
*
346-
* Free the latest kunit_resource of @test for which @free matches the
347-
* kunit_resource_free_t associated with the resource and for which @match
348-
* returns true.
349-
*
350435
* RETURNS:
351436
* 0 if kunit_resource is found and freed, -ENOENT if not found.
352437
*/
353-
int kunit_resource_destroy(struct kunit *test,
438+
int kunit_destroy_resource(struct kunit *test,
354439
kunit_resource_match_t match,
355-
kunit_resource_free_t free,
356440
void *match_data);
357441

442+
/**
443+
* kunit_remove_resource: remove resource from resource list associated with
444+
* test.
445+
* @test: The test context object.
446+
* @res: The resource to be removed.
447+
*
448+
* Note that the resource will not be immediately freed since it is likely
449+
* the caller has a reference to it via alloc_and_get() or find();
450+
* in this case a final call to kunit_put_resource() is required.
451+
*/
452+
void kunit_remove_resource(struct kunit *test, struct kunit_resource *res);
453+
358454
/**
359455
* kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
360456
* @test: The test context object.

0 commit comments

Comments
 (0)