Skip to content

Commit 53e5504

Browse files
committed
Merge tag 'linux-kselftest-kunit-5.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest
Pull kunit updates from Shuah Khan: - Add a generic kunit_resource API extending it to support resources that are passed in to kunit in addition kunit allocated resources. In addition, KUnit resources are now refcounted to avoid passed in resources being released while in use by kunit. - Add support for named resources. - Important bug fixes from Brendan Higgins and Will Chen * tag 'linux-kselftest-kunit-5.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: kunit: tool: fix improper treatment of file location kunit: tool: fix broken default args in unit tests kunit: capture stderr on all make subprocess calls Documentation: kunit: Remove references to --defconfig kunit: add support for named resources kunit: generalize kunit_resource API beyond allocated resources
2 parents 2324d50 + d43c7fb commit 53e5504

File tree

9 files changed

+396
-173
lines changed

9 files changed

+396
-173
lines changed

Documentation/dev-tools/kunit/kunit-tool.rst

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ compiles the kernel as a standalone Linux executable that can be run like any
1919
other program directly inside of a host operating system. To be clear, it does
2020
not require any virtualization support: it is just a regular program.
2121

22-
What is a kunitconfig?
23-
======================
22+
What is a .kunitconfig?
23+
=======================
2424

2525
It's just a defconfig that kunit_tool looks for in the base directory.
2626
kunit_tool uses it to generate a .config as you might expect. In addition, it
2727
verifies that the generated .config contains the CONFIG options in the
28-
kunitconfig; the reason it does this is so that it is easy to be sure that a
28+
.kunitconfig; the reason it does this is so that it is easy to be sure that a
2929
CONFIG that enables a test actually ends up in the .config.
3030

3131
How do I use kunit_tool?
@@ -46,16 +46,9 @@ However, you most likely want to use it with the following options:
4646
- ``--timeout`` sets a maximum amount of time to allow tests to run.
4747
- ``--jobs`` sets the number of threads to use to build the kernel.
4848

49-
If you just want to use the defconfig that ships with the kernel, you can
50-
append the ``--defconfig`` flag as well:
51-
52-
.. code-block:: bash
53-
54-
./tools/testing/kunit/kunit.py run --timeout=30 --jobs=`nproc --all` --defconfig
55-
5649
.. note::
57-
This command is particularly helpful for getting started because it
58-
just works. No kunitconfig needs to be present.
50+
This command will work even without a .kunitconfig file: if no
51+
.kunitconfig is present, a default one will be used instead.
5952

6053
For a list of all the flags supported by kunit_tool, you can run:
6154

Documentation/dev-tools/kunit/start.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The wrapper can be run with:
1818

1919
.. code-block:: bash
2020
21-
./tools/testing/kunit/kunit.py run --defconfig
21+
./tools/testing/kunit/kunit.py run
2222
2323
For more information on this wrapper (also called kunit_tool) check out the
2424
:doc:`kunit-tool` page.

include/kunit/test.h

Lines changed: 180 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,42 @@ 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
* }
75+
*
76+
* Resources can also be named, with lookup/removal done on a name
77+
* basis also. kunit_add_named_resource(), kunit_find_named_resource()
78+
* and kunit_destroy_named_resource(). Resource names must be
79+
* unique within the test instance.
7380
*/
7481
struct kunit_resource {
75-
void *allocation;
76-
kunit_resource_free_t free;
82+
void *data;
83+
const char *name; /* optional name */
7784

7885
/* private: internal use only. */
86+
kunit_resource_free_t free;
87+
struct kref refcount;
7988
struct list_head node;
8089
};
8190

@@ -283,6 +292,79 @@ struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
283292
gfp_t internal_gfp,
284293
void *context);
285294

295+
/**
296+
* kunit_get_resource() - Hold resource for use. Should not need to be used
297+
* by most users as we automatically get resources
298+
* retrieved by kunit_find_resource*().
299+
* @res: resource
300+
*/
301+
static inline void kunit_get_resource(struct kunit_resource *res)
302+
{
303+
kref_get(&res->refcount);
304+
}
305+
306+
/*
307+
* Called when refcount reaches zero via kunit_put_resources();
308+
* should not be called directly.
309+
*/
310+
static inline void kunit_release_resource(struct kref *kref)
311+
{
312+
struct kunit_resource *res = container_of(kref, struct kunit_resource,
313+
refcount);
314+
315+
/* If free function is defined, resource was dynamically allocated. */
316+
if (res->free) {
317+
res->free(res);
318+
kfree(res);
319+
}
320+
}
321+
322+
/**
323+
* kunit_put_resource() - When caller is done with retrieved resource,
324+
* kunit_put_resource() should be called to drop
325+
* reference count. The resource list maintains
326+
* a reference count on resources, so if no users
327+
* are utilizing a resource and it is removed from
328+
* the resource list, it will be freed via the
329+
* associated free function (if any). Only
330+
* needs to be used if we alloc_and_get() or
331+
* find() resource.
332+
* @res: resource
333+
*/
334+
static inline void kunit_put_resource(struct kunit_resource *res)
335+
{
336+
kref_put(&res->refcount, kunit_release_resource);
337+
}
338+
339+
/**
340+
* kunit_add_resource() - Add a *test managed resource*.
341+
* @test: The test context object.
342+
* @init: a user-supplied function to initialize the result (if needed). If
343+
* none is supplied, the resource data value is simply set to @data.
344+
* If an init function is supplied, @data is passed to it instead.
345+
* @free: a user-supplied function to free the resource (if needed).
346+
* @data: value to pass to init function or set in resource data field.
347+
*/
348+
int kunit_add_resource(struct kunit *test,
349+
kunit_resource_init_t init,
350+
kunit_resource_free_t free,
351+
struct kunit_resource *res,
352+
void *data);
353+
354+
/**
355+
* kunit_add_named_resource() - Add a named *test managed resource*.
356+
* @test: The test context object.
357+
* @init: a user-supplied function to initialize the resource data, if needed.
358+
* @free: a user-supplied function to free the resource data, if needed.
359+
* @name_data: name and data to be set for resource.
360+
*/
361+
int kunit_add_named_resource(struct kunit *test,
362+
kunit_resource_init_t init,
363+
kunit_resource_free_t free,
364+
struct kunit_resource *res,
365+
const char *name,
366+
void *data);
367+
286368
/**
287369
* kunit_alloc_resource() - Allocates a *test managed resource*.
288370
* @test: The test context object.
@@ -295,7 +377,7 @@ struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
295377
* cleaned up at the end of a test case. See &struct kunit_resource for an
296378
* example.
297379
*
298-
* NOTE: KUnit needs to allocate memory for each kunit_resource object. You must
380+
* Note: KUnit needs to allocate memory for a kunit_resource object. You must
299381
* specify an @internal_gfp that is compatible with the use context of your
300382
* resource.
301383
*/
@@ -307,54 +389,122 @@ static inline void *kunit_alloc_resource(struct kunit *test,
307389
{
308390
struct kunit_resource *res;
309391

310-
res = kunit_alloc_and_get_resource(test, init, free, internal_gfp,
311-
context);
392+
res = kzalloc(sizeof(*res), internal_gfp);
393+
if (!res)
394+
return NULL;
312395

313-
if (res)
314-
return res->allocation;
396+
if (!kunit_add_resource(test, init, free, res, context))
397+
return res->data;
315398

316399
return NULL;
317400
}
318401

319402
typedef bool (*kunit_resource_match_t)(struct kunit *test,
320-
const void *res,
403+
struct kunit_resource *res,
321404
void *match_data);
322405

323406
/**
324407
* kunit_resource_instance_match() - Match a resource with the same instance.
325408
* @test: Test case to which the resource belongs.
326-
* @res: The data stored in kunit_resource->allocation.
409+
* @res: The resource.
327410
* @match_data: The resource pointer to match against.
328411
*
329412
* An instance of kunit_resource_match_t that matches a resource whose
330413
* allocation matches @match_data.
331414
*/
332415
static inline bool kunit_resource_instance_match(struct kunit *test,
333-
const void *res,
416+
struct kunit_resource *res,
334417
void *match_data)
335418
{
336-
return res == match_data;
419+
return res->data == match_data;
337420
}
338421

339422
/**
340-
* kunit_resource_destroy() - Find a kunit_resource and destroy it.
423+
* kunit_resource_name_match() - Match a resource with the same name.
424+
* @test: Test case to which the resource belongs.
425+
* @res: The resource.
426+
* @match_name: The name to match against.
427+
*/
428+
static inline bool kunit_resource_name_match(struct kunit *test,
429+
struct kunit_resource *res,
430+
void *match_name)
431+
{
432+
return res->name && strcmp(res->name, match_name) == 0;
433+
}
434+
435+
/**
436+
* kunit_find_resource() - Find a resource using match function/data.
437+
* @test: Test case to which the resource belongs.
438+
* @match: match function to be applied to resources/match data.
439+
* @match_data: data to be used in matching.
440+
*/
441+
static inline struct kunit_resource *
442+
kunit_find_resource(struct kunit *test,
443+
kunit_resource_match_t match,
444+
void *match_data)
445+
{
446+
struct kunit_resource *res, *found = NULL;
447+
448+
spin_lock(&test->lock);
449+
450+
list_for_each_entry_reverse(res, &test->resources, node) {
451+
if (match(test, res, (void *)match_data)) {
452+
found = res;
453+
kunit_get_resource(found);
454+
break;
455+
}
456+
}
457+
458+
spin_unlock(&test->lock);
459+
460+
return found;
461+
}
462+
463+
/**
464+
* kunit_find_named_resource() - Find a resource using match name.
465+
* @test: Test case to which the resource belongs.
466+
* @name: match name.
467+
*/
468+
static inline struct kunit_resource *
469+
kunit_find_named_resource(struct kunit *test,
470+
const char *name)
471+
{
472+
return kunit_find_resource(test, kunit_resource_name_match,
473+
(void *)name);
474+
}
475+
476+
/**
477+
* kunit_destroy_resource() - Find a kunit_resource and destroy it.
341478
* @test: Test case to which the resource belongs.
342479
* @match: Match function. Returns whether a given resource matches @match_data.
343-
* @free: Must match free on the kunit_resource to free.
344480
* @match_data: Data passed into @match.
345481
*
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-
*
350482
* RETURNS:
351483
* 0 if kunit_resource is found and freed, -ENOENT if not found.
352484
*/
353-
int kunit_resource_destroy(struct kunit *test,
485+
int kunit_destroy_resource(struct kunit *test,
354486
kunit_resource_match_t match,
355-
kunit_resource_free_t free,
356487
void *match_data);
357488

489+
static inline int kunit_destroy_named_resource(struct kunit *test,
490+
const char *name)
491+
{
492+
return kunit_destroy_resource(test, kunit_resource_name_match,
493+
(void *)name);
494+
}
495+
496+
/**
497+
* kunit_remove_resource: remove resource from resource list associated with
498+
* test.
499+
* @test: The test context object.
500+
* @res: The resource to be removed.
501+
*
502+
* Note that the resource will not be immediately freed since it is likely
503+
* the caller has a reference to it via alloc_and_get() or find();
504+
* in this case a final call to kunit_put_resource() is required.
505+
*/
506+
void kunit_remove_resource(struct kunit *test, struct kunit_resource *res);
507+
358508
/**
359509
* kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
360510
* @test: The test context object.

0 commit comments

Comments
 (0)