Commit 57e3cded authored by David Gow's avatar David Gow Committed by Shuah Khan

kunit: kmalloc_array: Use kunit_add_action()

The kunit_add_action() function is much simpler and cleaner to use that
the full KUnit resource API for simple things like the
kunit_kmalloc_array() functionality.

Replacing it allows us to get rid of a number of helper functions, and
leaves us with no uses of kunit_alloc_resource(), which has some
usability problems and is going to have its behaviour modified in an
upcoming patch.

Note that we need to use kunit_defer_trigger_all() to implement
kunit_kfree().
Reviewed-by: default avatarBenjamin Berg <benjamin.berg@intel.com>
Reviewed-by: default avatarMaxime Ripard <maxime@cerno.tech>
Tested-by: default avatarMaxime Ripard <maxime@cerno.tech>
Signed-off-by: default avatarDavid Gow <davidgow@google.com>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent 00e63f8a
...@@ -324,8 +324,11 @@ enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite); ...@@ -324,8 +324,11 @@ enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite);
* @gfp: flags passed to underlying kmalloc(). * @gfp: flags passed to underlying kmalloc().
* *
* Just like `kmalloc_array(...)`, except the allocation is managed by the test case * Just like `kmalloc_array(...)`, except the allocation is managed by the test case
* and is automatically cleaned up after the test case concludes. See &struct * and is automatically cleaned up after the test case concludes. See kunit_add_action()
* kunit_resource for more information. * for more information.
*
* Note that some internal context data is also allocated with GFP_KERNEL,
* regardless of the gfp passed in.
*/ */
void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp); void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
...@@ -336,6 +339,9 @@ void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp); ...@@ -336,6 +339,9 @@ void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp);
* @gfp: flags passed to underlying kmalloc(). * @gfp: flags passed to underlying kmalloc().
* *
* See kmalloc() and kunit_kmalloc_array() for more information. * See kmalloc() and kunit_kmalloc_array() for more information.
*
* Note that some internal context data is also allocated with GFP_KERNEL,
* regardless of the gfp passed in.
*/ */
static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp) static inline void *kunit_kmalloc(struct kunit *test, size_t size, gfp_t gfp)
{ {
......
...@@ -752,58 +752,28 @@ static struct notifier_block kunit_mod_nb = { ...@@ -752,58 +752,28 @@ static struct notifier_block kunit_mod_nb = {
}; };
#endif #endif
struct kunit_kmalloc_array_params { void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
size_t n;
size_t size;
gfp_t gfp;
};
static int kunit_kmalloc_array_init(struct kunit_resource *res, void *context)
{ {
struct kunit_kmalloc_array_params *params = context; void *data;
res->data = kmalloc_array(params->n, params->size, params->gfp); data = kmalloc_array(n, size, gfp);
if (!res->data)
return -ENOMEM;
return 0; if (!data)
} return NULL;
static void kunit_kmalloc_array_free(struct kunit_resource *res) if (kunit_add_action_or_reset(test, (kunit_action_t *)kfree, data) != 0)
{ return NULL;
kfree(res->data);
}
void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp) return data;
{
struct kunit_kmalloc_array_params params = {
.size = size,
.n = n,
.gfp = gfp
};
return kunit_alloc_resource(test,
kunit_kmalloc_array_init,
kunit_kmalloc_array_free,
gfp,
&params);
} }
EXPORT_SYMBOL_GPL(kunit_kmalloc_array); EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
static inline bool kunit_kfree_match(struct kunit *test,
struct kunit_resource *res, void *match_data)
{
/* Only match resources allocated with kunit_kmalloc() and friends. */
return res->free == kunit_kmalloc_array_free && res->data == match_data;
}
void kunit_kfree(struct kunit *test, const void *ptr) void kunit_kfree(struct kunit *test, const void *ptr)
{ {
if (!ptr) if (!ptr)
return; return;
if (kunit_destroy_resource(test, kunit_kfree_match, (void *)ptr)) kunit_release_action(test, (kunit_action_t *)kfree, (void *)ptr);
KUNIT_FAIL(test, "kunit_kfree: %px already freed or not allocated by kunit", ptr);
} }
EXPORT_SYMBOL_GPL(kunit_kfree); EXPORT_SYMBOL_GPL(kunit_kfree);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment