[PATCH] kunit: Release resource upon __kunit_add_resource() failure in the Resource API

From: Marie Zhussupova
Date: Tue Aug 12 2025 - 12:37:44 EST


__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.

Signed-off-by: Marie Zhussupova <marievic@xxxxxxxxxx>
---
include/kunit/resource.h | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/include/kunit/resource.h b/include/kunit/resource.h
index 4ad69a2642a5..2585e9a5242d 100644
--- a/include/kunit/resource.h
+++ b/include/kunit/resource.h
@@ -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;
}

--
2.51.0.rc0.205.g4a044479a3-goog