kunit: Make kunit_remove_resource() idempotent
PATCH v2 1/2 kunit: Make kunit_remove_resource() idempotent
The kunit_remove_resource() function is used to unlink a resource from the list of resources in the test, making it no longer show up in kunit_find_resource().
However, this could lead to a race condition if two threads called kunit_remove_resource() on the same resource at the same time: the resource would be removed from the list twice (causing a crash at the second list_del()), and the refcount for the resource would be decremented twice (instead of once, for the reference held by the resource list).
Fix both problems, the first by using list_del_init(), and the second by checking if the resource has already been removed using list_empty(), and only decrementing its refcount if it has not.
Also add a KUnit test for the kunit_remove_resource() function which tests this behaviour.
---
Changes since v1:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Rebased on top of Daniel's split of the resource system into resource.{c,h}
- https://lore.kernel.org/linux-kselftest/[email protected]/
- https://lore.kernel.org/linux-kselftest/[email protected]/
lib/kunit/kunit-test.c | 35 +++++++++++++++++++++++++++++++++++ lib/kunit/resource.c | 8 ++++++-- 2 files changed, 41 insertions(+), 2 deletions(-)
lib/kunit/kunit-test.c +35 -0
@@ -190,6 +190,40 @@ static void kunit_resource_test_destroy_resource(struct kunit *test)KUNIT_EXPECT_TRUE (test,list_empty (&ctx->test.resources)); } +static void kunit_resource_test_remove_resource (struct kunit *test) +{ +struct kunit_test_resource_context *ctx = test->priv; +struct kunit_resource *res =kunit_alloc_and_get_resource ( + &ctx->test, + fake_resource_init, + fake_resource_free, +GFP_KERNEL , + ctx); + +/* The resource is in the list */ +KUNIT_EXPECT_FALSE (test,list_empty (&ctx->test.resources)); + +/* Remove the resource. The pointer is still valid, but it can't be +* found. +*/ +kunit_remove_resource (test, res); +KUNIT_EXPECT_TRUE (test,list_empty (&ctx->test.resources)); +/* We haven't been freed yet. */ +KUNIT_EXPECT_TRUE (test, ctx->is_resource_initialized); + +/* Removing the resource multiple times is valid. */ +kunit_remove_resource (test, res); +KUNIT_EXPECT_TRUE (test,list_empty (&ctx->test.resources)); +/* Despite having been removed twice (from only one reference), the +* resource still has not been freed. +*/ +KUNIT_EXPECT_TRUE (test, ctx->is_resource_initialized); + +/* Free the resource. */ +kunit_put_resource (res); +KUNIT_EXPECT_FALSE (test, ctx->is_resource_initialized); +} +static void kunit_resource_test_cleanup_resources (struct kunit *test) {int i;
@@ -387,6 +421,7 @@ static struct kunit_case kunit_resource_test_cases[] = {KUNIT_CASE (kunit_resource_test_init_resources),KUNIT_CASE (kunit_resource_test_alloc_resource),KUNIT_CASE (kunit_resource_test_destroy_resource), +KUNIT_CASE (kunit_resource_test_remove_resource),KUNIT_CASE (kunit_resource_test_cleanup_resources),KUNIT_CASE (kunit_resource_test_proper_free_ordering), KUNIT_CASE(kunit_resource_test_static),
lib/kunit/resource.c +6 -2
@@ -98,11 +98,15 @@ EXPORT_SYMBOL_GPL(kunit_alloc_and_get_resource);void kunit_remove_resource (struct kunit *test,struct kunit_resource *res) {unsigned long flags; +bool was_linked;spin_lock_irqsave (&test->lock, flags); -list_del (&res->node); + was_linked = !list_empty (&res->node); +list_del_init (&res->node);spin_unlock_irqrestore (&test->lock, flags); -kunit_put_resource (res); + +if (was_linked) +kunit_put_resource (res); }EXPORT_SYMBOL_GPL (kunit_remove_resource);
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
PATCH v2 2/2 kunit: Rework kunit_resource allocation policy
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.
---
Changes since v1:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Rebased on top of Daniel's split of the resource system into resource.{c,h}
- https://lore.kernel.org/linux-kselftest/[email protected]/
- https://lore.kernel.org/linux-kselftest/[email protected]/
- Renamed should_free to should_kfree
- Fixed an issue where should_kfree was overwritten.
- Tidied up some comments slightly.
include/kunit/resource.h | 142 +++++++++++++++++++++++++++++++-------- lib/kunit/resource.c | 64 ++---------------- 2 files changed, 122 insertions(+), 84 deletions(-)
include/kunit/resource.h +115 -27
@@ -25,11 +25,13 @@ typedef void (*kunit_resource_free_t)(struct kunit_resource *);* struct kunit_resource - represents a *test managed resource* * @data: for the user to store arbitrary data. * @name: optional name -* @free: a user supplied function to free the resource. Populated by -* kunit_resource_alloc(). +* @free: a user supplied function to free the resource. * * Represents a *test managed resource*, a resource which will automatically be -* cleaned up at the end of a test case. +* cleaned up at the end of a test case. This cleanup is performed by the 'free' +* function. The struct kunit_resource itself is freed automatically with +* kfree() if it was allocated by KUnit (e.g., by kunit_alloc_resource()), but +* must be freed by the user otherwise. * * Resources are reference counted so if a resource is retrieved via * kunit_alloc_and_get_resource() or kunit_find_resource(), we need
@@ -86,18 +88,9 @@ struct kunit_resource {/* private: internal use only. */ struct kref refcount;struct list_head node; +bool should_kfree; }; -/* -* Like kunit_alloc_resource() below, but returns the struct kunit_resource -* object that contains the allocation. This is mostly for testing purposes. -*/ -struct kunit_resource *kunit_alloc_and_get_resource (struct kunit *test, -kunit_resource_init_t init, -kunit_resource_free_t free, -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
@@ -118,11 +111,14 @@ 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) { +if (res->free) res->free (res); + +/* 'res' is valid here, as if should_kfree is set, res->free may not free +* 'res' itself, just res->data +*/ +if (res->should_kfree)kfree (res); - } }/**
@@ -142,6 +138,24 @@ static inline void kunit_put_resource(struct kunit_resource *res)kref_put (&res->refcount, kunit_release_resource); } +/** +* __kunit_add_resource() - Internal helper to add a resource. +* +* res->should_kfree is not initialised. +* @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). +* @res: The resource. +* @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_resource() - Add a *test managed resource*. * @test: The test context object.
@@ -152,11 +166,18 @@ static inline void kunit_put_resource(struct kunit_resource *res)* @res: The resource. * @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); +static inline int kunit_add_resource (struct kunit *test, +kunit_resource_init_t init, +kunit_resource_free_t free, +struct kunit_resource *res, +void *data) +{ + res->should_kfree = false; +return __kunit_add_resource (test, init, free, res, data); +} + +static inline struct kunit_resource * +kunit_find_named_resource (struct kunit *test,const char *name);/** * kunit_add_named_resource() - Add a named *test managed resource*.
@@ -167,18 +188,84 @@ int kunit_add_resource(struct kunit *test,* @name: name to be set for resource. * @data: value to pass to init function or set in resource data field. */ -int kunit_add_named_resource (struct kunit *test, +static inline 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) +{ +struct kunit_resource *existing; + +if (!name) +return -EINVAL ; + + existing =kunit_find_named_resource (test, name); +if (existing) { +kunit_put_resource (existing); +return -EEXIST ; + } + + res->name = name; + res->should_kfree = false; + +return __kunit_add_resource (test, init, free, res, data); +} + +/** +* kunit_alloc_and_get_resource() - Allocates and returns a *test managed resource*. +* @test: The test context object. +* @init: a user supplied function to initialize the resource. +* @free: a user supplied function to free the resource (if needed). +* @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL +* @context: for the user to pass in arbitrary data to the init function. +* +* Allocates a *test managed resource*, a resource which will automatically be +* cleaned up at the end of a test case. See &struct kunit_resource for an +* example. +* +* This is effectively identical to kunit_alloc_resource, but returns the +* struct kunit_resource pointer, not just the 'data' pointer. It therefore +* also increments the resource's refcount, so kunit_put_resource() should be +* called when you've finished with it. +* +* 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. +*/ +static inline struct kunit_resource * +kunit_alloc_and_get_resource (struct kunit *test,kunit_resource_init_t init,kunit_resource_free_t free, -struct kunit_resource *res, -const char *name, -void *data); +gfp_t internal_gfp, +void *context) +{ +struct kunit_resource *res; +int ret; + + res =kzalloc (sizeof (*res), internal_gfp); +if (!res) +return NULL ; + + res->should_kfree = true; + + ret =__kunit_add_resource (test, init, free, res, context); +if (!ret) { +/* +* bump refcount for get; kunit_resource_put() should be called +* when done. +*/ +kunit_get_resource (res); +return res; + } +return NULL ; +}/** * kunit_alloc_resource() - Allocates a *test managed resource*. * @test: The test context object. * @init: a user supplied function to initialize the resource. -* @free: a user supplied function to free the resource. +* @free: a user supplied function to free the resource (if needed). * @internal_gfp: gfp to use for internal allocations, if unsure, use GFP_KERNEL * @context: for the user to pass in arbitrary data to the init function. *
@@ -202,7 +289,8 @@ static inline void *kunit_alloc_resource(struct kunit *test,if (!res)return NULL ; -if (!kunit_add_resource (test, init, free, res, context)) + res->should_kfree = true; +if (!__kunit_add_resource (test, init, free, res, context))return res->data;return NULL ;
lib/kunit/resource.c +7 -57
77 lines not on this page · raw message
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
Re: [PATCH v2 2/2] kunit: Rework kunit_resource allocation policy
Quoted 31 lines · David Gow' via KUnit Development
KUnit's test-managed resources can be created in two ways:
- Using thekunit_add_resource()family of functions, which accept a structkunit_resourcepointer, typically allocated statically or on the stack during the test.
- Using thekunit_alloc_resource()family of functions, which allocate a structkunit_resourceusingkzalloc()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 leadkunit_alloc_resource()to leak memory (if no 'free' function is passed in), orkunit_add_resource()to incorrectlykfree()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 withkunit_alloc_resource(), and neverkfree()if it was passed intokunit_add_resource()by the user. (If the user ofkunit_add_resource()wishes the resource be kfree()ed, they can callkfree()on the resource from within the 'free' function.This is implemented by adding a '
should_free' member to structkunit_resourceand 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.
Quoted 2 lines
Signed-off-by David Gow <[email protected]>Tested-by Daniel Latypov <[email protected]>