mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-08-22 17:01:14 +00:00
![David Gow](/assets/img/avatar_default.png)
KUnit's test-managed resources can be created in two ways: - Using the kunit_add_resource() family of functions, which accept a struct kunit_resource pointer, typically allocated statically or on the stack during the test. - Using the kunit_alloc_resource() family of functions, which allocate a struct kunit_resource using kzalloc() behind the scenes. Both of these families of functions accept a 'free' function to be called when the resource is finally disposed of. At present, KUnit will kfree() the resource if this 'free' function is specified, and will not if it is NULL. However, this can lead kunit_alloc_resource() to leak memory (if no 'free' function is passed in), or kunit_add_resource() to incorrectly kfree() memory which was allocated by some other means (on the stack, as part of a larger allocation, etc), if a 'free' function is provided. Instead, always kfree() if the resource was allocated with kunit_alloc_resource(), and never kfree() if it was passed into kunit_add_resource() by the user. (If the user of kunit_add_resource() wishes the resource be kfree()ed, they can call kfree() on the resource from within the 'free' function. This is implemented by adding a 'should_free' member to struct kunit_resource and setting it appropriately. To facilitate this, the various resource add/alloc functions have been refactored somewhat, making them all call a __kunit_add_resource() helper after setting the 'should_free' member appropriately. In the process, all other functions have been made static inline functions. Signed-off-by: David Gow <davidgow@google.com> Tested-by: Daniel Latypov <dlatypov@google.com> Reviewed-by: Brendan Higgins <brendanhiggins@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
79 lines
1.9 KiB
C
79 lines
1.9 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* KUnit resource API for test managed resources (allocations, etc.).
|
|
*
|
|
* Copyright (C) 2022, Google LLC.
|
|
* Author: Daniel Latypov <dlatypov@google.com>
|
|
*/
|
|
|
|
#include <kunit/resource.h>
|
|
#include <kunit/test.h>
|
|
#include <linux/kref.h>
|
|
|
|
/*
|
|
* Used for static resources and when a kunit_resource * has been created by
|
|
* kunit_alloc_resource(). When an init function is supplied, @data is passed
|
|
* into the init function; otherwise, we simply set the resource data field to
|
|
* the data value passed in. Doesn't initialize res->should_kfree.
|
|
*/
|
|
int __kunit_add_resource(struct kunit *test,
|
|
kunit_resource_init_t init,
|
|
kunit_resource_free_t free,
|
|
struct kunit_resource *res,
|
|
void *data)
|
|
{
|
|
int ret = 0;
|
|
unsigned long flags;
|
|
|
|
res->free = free;
|
|
kref_init(&res->refcount);
|
|
|
|
if (init) {
|
|
ret = init(res, data);
|
|
if (ret)
|
|
return ret;
|
|
} else {
|
|
res->data = data;
|
|
}
|
|
|
|
spin_lock_irqsave(&test->lock, flags);
|
|
list_add_tail(&res->node, &test->resources);
|
|
/* refcount for list is established by kref_init() */
|
|
spin_unlock_irqrestore(&test->lock, flags);
|
|
|
|
return ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(__kunit_add_resource);
|
|
|
|
void kunit_remove_resource(struct kunit *test, struct kunit_resource *res)
|
|
{
|
|
unsigned long flags;
|
|
bool was_linked;
|
|
|
|
spin_lock_irqsave(&test->lock, flags);
|
|
was_linked = !list_empty(&res->node);
|
|
list_del_init(&res->node);
|
|
spin_unlock_irqrestore(&test->lock, flags);
|
|
|
|
if (was_linked)
|
|
kunit_put_resource(res);
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_remove_resource);
|
|
|
|
int kunit_destroy_resource(struct kunit *test, kunit_resource_match_t match,
|
|
void *match_data)
|
|
{
|
|
struct kunit_resource *res = kunit_find_resource(test, match,
|
|
match_data);
|
|
|
|
if (!res)
|
|
return -ENOENT;
|
|
|
|
kunit_remove_resource(test, res);
|
|
|
|
/* We have a reference also via _find(); drop it. */
|
|
kunit_put_resource(res);
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(kunit_destroy_resource);
|