kunit: Release resource upon __kunit_add_resource() failure in the Resource API
PATCH 1/1 kunit: Release resource upon __kunit_add_resource() failure in the Resource API
__kunit_add_resource() currently does the following things in order: initializes the resource refcount to 1, initializes the resource, and adds the resource to the test's resource list. Currently, __kunit_add_resource() only fails if the resource initialization fails.
The kunit_alloc_and_get_resource()
and kunit_alloc_resource() functions allocate memory for struct kunit_resource. However, if the subsequent call to __kunit_add_resource() fails, the functions return NULL without releasing the memory.
This patch adds calls to kunit_put_resource() in these functions before returning NULL to decrease the refcount of the resource that failed to initialize to 0. This will trigger kunit_release_resource(), which will both call kunit_resource->free and kfree() on the resource.
Since kunit_resource->free is user defined, comments were added to note that kunit_resource->free() should be able to handle any inconsistent state that may result from the resource init failure.
--- include/kunit/resource.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
include/kunit/resource.h +8 -2
@@ -216,7 +216,9 @@ static inline int kunit_add_named_resource(struct kunit *test,* 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). +* @free: a user supplied function to free the resource (if needed). Note that, +* if supplied, @free will run even if @init fails: Make sure it can handle any +* inconsistent state which may result. * @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. *
@@ -258,6 +260,7 @@ kunit_alloc_and_get_resource(struct kunit *test,kunit_get_resource (res);return res; } +kunit_put_resource (res);return NULL ; }
@@ -265,7 +268,9 @@ kunit_alloc_and_get_resource(struct kunit *test,* 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 (if needed). +* @free: a user supplied function to free the resource (if needed). Note that, +* if supplied, @free will run even if @init fails: Make sure it can handle any +* inconsistent state which may result. * @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. *
@@ -293,6 +298,7 @@ static inline void *kunit_alloc_resource(struct kunit *test,if (!__kunit_add_resource (test, init, free, res, context))return res->data; +kunit_put_resource (res);return NULL ; }
Signature · 1 line
2.51.0.rc0.205.g4a044479a3-goog