[PATCH] kunit: fix reference count leak in kfree_at_end

0 views
Skip to first unread message

Xiyu Yang

unread,
Sep 9, 2021, 1:28:30 AM9/9/21
to Brendan Higgins, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org, yuanx...@fudan.edu.cn, Xiyu Yang, Xin Tan
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_put_resource() at the end of function.

Signed-off-by: Xiyu Yang <xiyuy...@fudan.edu.cn>
Signed-off-by: Xin Tan <tanxi...@gmail.com>
---
lib/kunit/executor_test.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/kunit/executor_test.c b/lib/kunit/executor_test.c
index cdbe54b16501..3af30abad826 100644
--- a/lib/kunit/executor_test.c
+++ b/lib/kunit/executor_test.c
@@ -113,11 +113,13 @@ static void kfree_res_free(struct kunit_resource *res)
*/
static void kfree_at_end(struct kunit *test, const void *to_free)
{
+ struct kunit_resource *res;
/* 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,
+ res = kunit_alloc_and_get_resource(test, NULL, kfree_res_free, GFP_KERNEL,
(void *)to_free);
+ kunit_put_resource(res);
}

static struct kunit_suite *alloc_fake_suite(struct kunit *test,
--
2.7.4

David Gow

unread,
Sep 9, 2021, 2:54:15 AM9/9/21
to Xiyu Yang, Brendan Higgins, open list:KERNEL SELFTEST FRAMEWORK, KUnit Development, Linux Kernel Mailing List, yuanx...@fudan.edu.cn, Xin Tan, Daniel Latypov
On Thu, Sep 9, 2021 at 1:28 PM 'Xiyu Yang' via KUnit Development
<kuni...@googlegroups.com> wrote:
>
> 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_put_resource() at the end of function.
>
> Signed-off-by: Xiyu Yang <xiyuy...@fudan.edu.cn>
> Signed-off-by: Xin Tan <tanxi...@gmail.com>
> ---

Thanks for looking into this.

+ Daniel -- any thoughts?

> lib/kunit/executor_test.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/lib/kunit/executor_test.c b/lib/kunit/executor_test.c
> index cdbe54b16501..3af30abad826 100644
> --- a/lib/kunit/executor_test.c
> +++ b/lib/kunit/executor_test.c
> @@ -113,11 +113,13 @@ static void kfree_res_free(struct kunit_resource *res)
> */
> static void kfree_at_end(struct kunit *test, const void *to_free)
> {
> + struct kunit_resource *res;
> /* 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,
> + res = kunit_alloc_and_get_resource(test, NULL, kfree_res_free, GFP_KERNEL,
> (void *)to_free);
> + kunit_put_resource(res);

Why not just change this to kunit_alloc_resource()?

> }
>
> static struct kunit_suite *alloc_fake_suite(struct kunit *test,
> --
> 2.7.4
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/1631165296-81082-1-git-send-email-xiyuyang19%40fudan.edu.cn.

Xiyu Yang

unread,
Sep 9, 2021, 3:26:37 AM9/9/21
to Brendan Higgins, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org, yuanx...@fudan.edu.cn, Xiyu Yang, Xin Tan
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().

Signed-off-by: Xiyu Yang <xiyuy...@fudan.edu.cn>
Signed-off-by: Xin Tan <tanxi...@gmail.com>
---
lib/kunit/executor_test.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/kunit/executor_test.c b/lib/kunit/executor_test.c
index cdbe54b16501..c2dcfb1f6e97 100644
--- a/lib/kunit/executor_test.c
+++ b/lib/kunit/executor_test.c
@@ -116,7 +116,7 @@ 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,
+ kunit_alloc_resource(test, NULL, kfree_res_free, GFP_KERNEL,
(void *)to_free);
}

--
2.7.4

Daniel Latypov

unread,
Sep 9, 2021, 1:00:08 PM9/9/21
to Xiyu Yang, Brendan Higgins, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org, yuanx...@fudan.edu.cn, Xin Tan
On Thu, Sep 9, 2021 at 12:26 AM 'Xiyu Yang' via KUnit Development
<kuni...@googlegroups.com> wrote:
>
> 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().
>
> Signed-off-by: Xiyu Yang <xiyuy...@fudan.edu.cn>
> Signed-off-by: Xin Tan <tanxi...@gmail.com>

Reviewed-by: Daniel Latypov <dlat...@google.com>

Ah, thanks for finding and fixing this!
We really should have better documentation/otherwise make it clearer
that people shouldn't use the "_and_get" version.

I went and added some pr_info() calls to verify that these were being
leaked before and they're fixed now.

I copy-pasted this mistake into
https://lore.kernel.org/linux-kselftest/20210831171926.3...@google.com/.
I'll send a v3 fix for that patch as well.

> ---
> lib/kunit/executor_test.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/lib/kunit/executor_test.c b/lib/kunit/executor_test.c
> index cdbe54b16501..c2dcfb1f6e97 100644
> --- a/lib/kunit/executor_test.c
> +++ b/lib/kunit/executor_test.c
> @@ -116,7 +116,7 @@ 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,
> + kunit_alloc_resource(test, NULL, kfree_res_free, GFP_KERNEL,
> (void *)to_free);
> }
>
> --
> 2.7.4
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/1631172276-82914-1-git-send-email-xiyuyang19%40fudan.edu.cn.

Daniel Latypov

unread,
Sep 9, 2021, 1:15:44 PM9/9/21
to Xiyu Yang, Brendan Higgins, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org, yuanx...@fudan.edu.cn, Xin Tan
On Thu, Sep 9, 2021 at 9:59 AM Daniel Latypov <dlat...@google.com> wrote:
>
> On Thu, Sep 9, 2021 at 12:26 AM 'Xiyu Yang' via KUnit Development
> <kuni...@googlegroups.com> wrote:
> >
> > 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().
> >
> > Signed-off-by: Xiyu Yang <xiyuy...@fudan.edu.cn>
> > Signed-off-by: Xin Tan <tanxi...@gmail.com>
>
> Reviewed-by: Daniel Latypov <dlat...@google.com>
>
> Ah, thanks for finding and fixing this!
> We really should have better documentation/otherwise make it clearer
> that people shouldn't use the "_and_get" version.
>
> I went and added some pr_info() calls to verify that these were being
> leaked before and they're fixed now.
>
> I copy-pasted this mistake into
> https://lore.kernel.org/linux-kselftest/20210831171926.3...@google.com/.
> I'll send a v3 fix for that patch as well.

Fixed that patch:
https://lore.kernel.org/linux-kselftest/20210909171052.3...@google.com/

I assume this patch and that one shouldn't have merge conflicts, so we
don't need these to be applied in any specific order.

Brendan Higgins

unread,
Sep 28, 2021, 5:27:43 PM9/28/21
to Xiyu Yang, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org, yuanx...@fudan.edu.cn, Xin Tan
On Thu, Sep 9, 2021 at 12:26 AM Xiyu Yang <xiyuy...@fudan.edu.cn> wrote:
>
> 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().
>
> Signed-off-by: Xiyu Yang <xiyuy...@fudan.edu.cn>
> Signed-off-by: Xin Tan <tanxi...@gmail.com>

Reviewed-by: Brendan Higgins <brendan...@google.com>

Thanks!

Shuah Khan

unread,
Oct 1, 2021, 3:59:19 PM10/1/21
to Xiyu Yang, linux-k...@vger.kernel.org, kuni...@googlegroups.com, linux-...@vger.kernel.org, yuanx...@fudan.edu.cn, Xin Tan, Brendan Higgins, Shuah Khan
Thank you for the fix.

Applied now after fixing a checkpatch alignment check to

git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git/
kunit-fixes branch.

Please remember to run checkpatch.pl before sending patches.

thanks,
-- Shuah
Reply all
Reply to author
Forward
0 new messages