Skip to content

Commit

Permalink
Merge tag 'linux-kselftest-kunit-5.9-rc1' of git://git.kernel.org/pub…
Browse files Browse the repository at this point in the history
…/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
  • Loading branch information
Linus Torvalds committed Aug 5, 2020
2 parents 2324d50 + d43c7fb commit 53e5504
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 173 deletions.
17 changes: 5 additions & 12 deletions Documentation/dev-tools/kunit/kunit-tool.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ compiles the kernel as a standalone Linux executable that can be run like any
other program directly inside of a host operating system. To be clear, it does
not require any virtualization support: it is just a regular program.

What is a kunitconfig?
======================
What is a .kunitconfig?
=======================

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

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

If you just want to use the defconfig that ships with the kernel, you can
append the ``--defconfig`` flag as well:

.. code-block:: bash
./tools/testing/kunit/kunit.py run --timeout=30 --jobs=`nproc --all` --defconfig
.. note::
This command is particularly helpful for getting started because it
just works. No kunitconfig needs to be present.
This command will work even without a .kunitconfig file: if no
.kunitconfig is present, a default one will be used instead.

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

Expand Down
2 changes: 1 addition & 1 deletion Documentation/dev-tools/kunit/start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The wrapper can be run with:

.. code-block:: bash
./tools/testing/kunit/kunit.py run --defconfig
./tools/testing/kunit/kunit.py run
For more information on this wrapper (also called kunit_tool) check out the
:doc:`kunit-tool` page.
Expand Down
210 changes: 180 additions & 30 deletions include/kunit/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/kref.h>

struct kunit_resource;

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

/**
* struct kunit_resource - represents a *test managed resource*
* @allocation: for the user to store arbitrary data.
* @data: for the user to store arbitrary data.
* @free: a user supplied function to free the resource. Populated by
* kunit_alloc_resource().
* kunit_resource_alloc().
*
* Represents a *test managed resource*, a resource which will automatically be
* cleaned up at the end of a test case.
*
* Resources are reference counted so if a resource is retrieved via
* kunit_alloc_and_get_resource() or kunit_find_resource(), we need
* to call kunit_put_resource() to reduce the resource reference count
* when finished with it. Note that kunit_alloc_resource() does not require a
* kunit_resource_put() because it does not retrieve the resource itself.
*
* Example:
*
* .. code-block:: c
Expand All @@ -42,40 +49,42 @@ typedef void (*kunit_resource_free_t)(struct kunit_resource *);
* static int kunit_kmalloc_init(struct kunit_resource *res, void *context)
* {
* struct kunit_kmalloc_params *params = context;
* res->allocation = kmalloc(params->size, params->gfp);
* res->data = kmalloc(params->size, params->gfp);
*
* if (!res->allocation)
* if (!res->data)
* return -ENOMEM;
*
* return 0;
* }
*
* static void kunit_kmalloc_free(struct kunit_resource *res)
* {
* kfree(res->allocation);
* kfree(res->data);
* }
*
* void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
* {
* struct kunit_kmalloc_params params;
* struct kunit_resource *res;
*
* params.size = size;
* params.gfp = gfp;
*
* res = kunit_alloc_resource(test, kunit_kmalloc_init,
* return kunit_alloc_resource(test, kunit_kmalloc_init,
* kunit_kmalloc_free, &params);
* if (res)
* return res->allocation;
*
* return NULL;
* }
*
* Resources can also be named, with lookup/removal done on a name
* basis also. kunit_add_named_resource(), kunit_find_named_resource()
* and kunit_destroy_named_resource(). Resource names must be
* unique within the test instance.
*/
struct kunit_resource {
void *allocation;
kunit_resource_free_t free;
void *data;
const char *name; /* optional name */

/* private: internal use only. */
kunit_resource_free_t free;
struct kref refcount;
struct list_head node;
};

Expand Down Expand Up @@ -283,6 +292,79 @@ struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
gfp_t internal_gfp,
void *context);

/**
* kunit_get_resource() - Hold resource for use. Should not need to be used
* by most users as we automatically get resources
* retrieved by kunit_find_resource*().
* @res: resource
*/
static inline void kunit_get_resource(struct kunit_resource *res)
{
kref_get(&res->refcount);
}

/*
* Called when refcount reaches zero via kunit_put_resources();
* should not be called directly.
*/
static inline void kunit_release_resource(struct kref *kref)
{
struct kunit_resource *res = container_of(kref, struct kunit_resource,
refcount);

/* If free function is defined, resource was dynamically allocated. */
if (res->free) {
res->free(res);
kfree(res);
}
}

/**
* kunit_put_resource() - When caller is done with retrieved resource,
* kunit_put_resource() should be called to drop
* reference count. The resource list maintains
* a reference count on resources, so if no users
* are utilizing a resource and it is removed from
* the resource list, it will be freed via the
* associated free function (if any). Only
* needs to be used if we alloc_and_get() or
* find() resource.
* @res: resource
*/
static inline void kunit_put_resource(struct kunit_resource *res)
{
kref_put(&res->refcount, kunit_release_resource);
}

/**
* kunit_add_resource() - Add a *test managed resource*.
* @test: The test context object.
* @init: a user-supplied function to initialize the result (if needed). If
* none is supplied, the resource data value is simply set to @data.
* If an init function is supplied, @data is passed to it instead.
* @free: a user-supplied function to free the resource (if needed).
* @data: value to pass to init function or set in resource data field.
*/
int kunit_add_resource(struct kunit *test,
kunit_resource_init_t init,
kunit_resource_free_t free,
struct kunit_resource *res,
void *data);

/**
* kunit_add_named_resource() - Add a named *test managed resource*.
* @test: The test context object.
* @init: a user-supplied function to initialize the resource data, if needed.
* @free: a user-supplied function to free the resource data, if needed.
* @name_data: name and data to be set for resource.
*/
int kunit_add_named_resource(struct kunit *test,
kunit_resource_init_t init,
kunit_resource_free_t free,
struct kunit_resource *res,
const char *name,
void *data);

/**
* kunit_alloc_resource() - Allocates a *test managed resource*.
* @test: The test context object.
Expand All @@ -295,7 +377,7 @@ struct kunit_resource *kunit_alloc_and_get_resource(struct kunit *test,
* cleaned up at the end of a test case. See &struct kunit_resource for an
* example.
*
* NOTE: KUnit needs to allocate memory for each kunit_resource object. You must
* Note: KUnit needs to allocate memory for a kunit_resource object. You must
* specify an @internal_gfp that is compatible with the use context of your
* resource.
*/
Expand All @@ -307,54 +389,122 @@ static inline void *kunit_alloc_resource(struct kunit *test,
{
struct kunit_resource *res;

res = kunit_alloc_and_get_resource(test, init, free, internal_gfp,
context);
res = kzalloc(sizeof(*res), internal_gfp);
if (!res)
return NULL;

if (res)
return res->allocation;
if (!kunit_add_resource(test, init, free, res, context))
return res->data;

return NULL;
}

typedef bool (*kunit_resource_match_t)(struct kunit *test,
const void *res,
struct kunit_resource *res,
void *match_data);

/**
* kunit_resource_instance_match() - Match a resource with the same instance.
* @test: Test case to which the resource belongs.
* @res: The data stored in kunit_resource->allocation.
* @res: The resource.
* @match_data: The resource pointer to match against.
*
* An instance of kunit_resource_match_t that matches a resource whose
* allocation matches @match_data.
*/
static inline bool kunit_resource_instance_match(struct kunit *test,
const void *res,
struct kunit_resource *res,
void *match_data)
{
return res == match_data;
return res->data == match_data;
}

/**
* kunit_resource_destroy() - Find a kunit_resource and destroy it.
* kunit_resource_name_match() - Match a resource with the same name.
* @test: Test case to which the resource belongs.
* @res: The resource.
* @match_name: The name to match against.
*/
static inline bool kunit_resource_name_match(struct kunit *test,
struct kunit_resource *res,
void *match_name)
{
return res->name && strcmp(res->name, match_name) == 0;
}

/**
* kunit_find_resource() - Find a resource using match function/data.
* @test: Test case to which the resource belongs.
* @match: match function to be applied to resources/match data.
* @match_data: data to be used in matching.
*/
static inline struct kunit_resource *
kunit_find_resource(struct kunit *test,
kunit_resource_match_t match,
void *match_data)
{
struct kunit_resource *res, *found = NULL;

spin_lock(&test->lock);

list_for_each_entry_reverse(res, &test->resources, node) {
if (match(test, res, (void *)match_data)) {
found = res;
kunit_get_resource(found);
break;
}
}

spin_unlock(&test->lock);

return found;
}

/**
* kunit_find_named_resource() - Find a resource using match name.
* @test: Test case to which the resource belongs.
* @name: match name.
*/
static inline struct kunit_resource *
kunit_find_named_resource(struct kunit *test,
const char *name)
{
return kunit_find_resource(test, kunit_resource_name_match,
(void *)name);
}

/**
* kunit_destroy_resource() - Find a kunit_resource and destroy it.
* @test: Test case to which the resource belongs.
* @match: Match function. Returns whether a given resource matches @match_data.
* @free: Must match free on the kunit_resource to free.
* @match_data: Data passed into @match.
*
* Free the latest kunit_resource of @test for which @free matches the
* kunit_resource_free_t associated with the resource and for which @match
* returns true.
*
* RETURNS:
* 0 if kunit_resource is found and freed, -ENOENT if not found.
*/
int kunit_resource_destroy(struct kunit *test,
int kunit_destroy_resource(struct kunit *test,
kunit_resource_match_t match,
kunit_resource_free_t free,
void *match_data);

static inline int kunit_destroy_named_resource(struct kunit *test,
const char *name)
{
return kunit_destroy_resource(test, kunit_resource_name_match,
(void *)name);
}

/**
* kunit_remove_resource: remove resource from resource list associated with
* test.
* @test: The test context object.
* @res: The resource to be removed.
*
* Note that the resource will not be immediately freed since it is likely
* the caller has a reference to it via alloc_and_get() or find();
* in this case a final call to kunit_put_resource() is required.
*/
void kunit_remove_resource(struct kunit *test, struct kunit_resource *res);

/**
* kunit_kmalloc() - Like kmalloc() except the allocation is *test managed*.
* @test: The test context object.
Expand Down
Loading

0 comments on commit 53e5504

Please sign in to comment.