Skip to content

Commit

Permalink
kunit: fix reference count leak in kfree_at_end
Browse files Browse the repository at this point in the history
The reference counting issue happens in the normal path of
kfree_at_end(). When kunit_alloc_and_get_resource() is invoked, the
function forgets to handle the returned resource object, whose refcount
increased inside, causing a refcount leak.

Fix this issue by calling kunit_alloc_resource() instead of
kunit_alloc_and_get_resource().

Fixed the following when applying:
Shuah Khan <[email protected]>

CHECK: Alignment should match open parenthesis
+	kunit_alloc_resource(test, NULL, kfree_res_free, GFP_KERNEL,
 				     (void *)to_free);

Signed-off-by: Xiyu Yang <[email protected]>
Signed-off-by: Xin Tan <[email protected]>
Reviewed-by: Daniel Latypov <[email protected]>
Reviewed-by: Brendan Higgins <[email protected]>
Signed-off-by: Shuah Khan <[email protected]>
  • Loading branch information
sherlly authored and shuahkh committed Oct 1, 2021
1 parent d8c23ea commit f62314b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/kunit/executor_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ static void kfree_at_end(struct kunit *test, const void *to_free)
/* kfree() handles NULL already, but avoid allocating a no-op cleanup. */
if (IS_ERR_OR_NULL(to_free))
return;
kunit_alloc_and_get_resource(test, NULL, kfree_res_free, GFP_KERNEL,
(void *)to_free);
kunit_alloc_resource(test, NULL, kfree_res_free, GFP_KERNEL,
(void *)to_free);
}

static struct kunit_suite *alloc_fake_suite(struct kunit *test,
Expand Down

0 comments on commit f62314b

Please sign in to comment.