linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: SeongJae Park <sj@kernel.org>
To: Jinjie Ruan <ruanjinjie@huawei.com>
Cc: sj@kernel.org, akpm@linux-foundation.org,
	brendan.higgins@linux.dev, feng.tang@intel.com,
	damon@lists.linux.dev, linux-mm@kvack.org,
	kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH RESEND 1/2] mm/damon/core-test: Fix memory leak in damon_new_region()
Date: Mon, 18 Sep 2023 05:33:20 +0000	[thread overview]
Message-ID: <20230918053320.61408-1-sj@kernel.org> (raw)
In-Reply-To: <20230918051044.3814018-2-ruanjinjie@huawei.com>

Hi Jinjie,


Thank you for this patchset!

On Mon, 18 Sep 2023 13:10:43 +0800 Jinjie Ruan <ruanjinjie@huawei.com> wrote:

> The damon_region which is allocated by kmem_cache_alloc() in
> damon_new_region() in damon_test_regions() and
> damon_test_update_monitoring_result() are not freed and it causes below
> memory leak. So use damon_free_region() to free it.
> 
> unreferenced object 0xffff2b49c3edc000 (size 56):
>   comm "kunit_try_catch", pid 338, jiffies 4294895280 (age 557.084s)
>   hex dump (first 32 bytes):
>     01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00  ................
>     00 00 00 00 00 00 00 00 00 00 00 00 49 2b ff ff  ............I+..
>   backtrace:
>     [<0000000088e71769>] slab_post_alloc_hook+0xb8/0x368
>     [<00000000b528f67c>] kmem_cache_alloc+0x168/0x284
>     [<000000008603f022>] damon_new_region+0x28/0x54
>     [<00000000a3b8c64e>] damon_test_regions+0x38/0x270
>     [<00000000559c4801>] kunit_try_run_case+0x50/0xac
>     [<000000003932ed49>] kunit_generic_run_threadfn_adapter+0x20/0x2c
>     [<000000003c3e9211>] kthread+0x124/0x130
>     [<0000000028f85bdd>] ret_from_fork+0x10/0x20
> unreferenced object 0xffff2b49c5b20000 (size 56):
>   comm "kunit_try_catch", pid 354, jiffies 4294895304 (age 556.988s)
>   hex dump (first 32 bytes):
>     03 00 00 00 00 00 00 00 07 00 00 00 00 00 00 00  ................
>     00 00 00 00 00 00 00 00 96 00 00 00 49 2b ff ff  ............I+..
>   backtrace:
>     [<0000000088e71769>] slab_post_alloc_hook+0xb8/0x368
>     [<00000000b528f67c>] kmem_cache_alloc+0x168/0x284
>     [<000000008603f022>] damon_new_region+0x28/0x54
>     [<00000000ca019f80>] damon_test_update_monitoring_result+0x18/0x34
>     [<00000000559c4801>] kunit_try_run_case+0x50/0xac
>     [<000000003932ed49>] kunit_generic_run_threadfn_adapter+0x20/0x2c
>     [<000000003c3e9211>] kthread+0x124/0x130
>     [<0000000028f85bdd>] ret_from_fork+0x10/0x20

Nice finding!  Could you please share just a brief more detail about above cool
output, e.g., just the name of the tool you used, so that others can learn it
from your awesome commit message?

> 
> Fixes: 17ccae8bb5c9 ("mm/damon: add kunit tests")
> Fixes: f4c978b6594b ("mm/damon/core-test: add a test for damon_update_monitoring_results()")
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
> ---
>  mm/damon/core-test.h | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/mm/damon/core-test.h b/mm/damon/core-test.h
> index 6cc8b245586d..255f8c925c00 100644
> --- a/mm/damon/core-test.h
> +++ b/mm/damon/core-test.h
> @@ -34,6 +34,7 @@ static void damon_test_regions(struct kunit *test)
>  	KUNIT_EXPECT_EQ(test, 0u, damon_nr_regions(t));
>  
>  	damon_free_target(t);
> +	damon_free_region(r);

There is damon_destroy_region() function, which simply calls damon_del_region()
and damon_free_region().  Unless there is needs to access the region before
removing from the region, doing memory return together via the function is
recommended.

And this test code calls damon_del_region() just beofre above
KUNIT_EXPECT_EQ().  Hence, I think replacing the damon_del_region() call with
damon_destroy_region() rather than calling damon_free_region() may be simpler
and shorter.  Could you please do so?

>  }
>  
>  static unsigned int nr_damon_targets(struct damon_ctx *ctx)
> @@ -316,6 +317,8 @@ static void damon_test_update_monitoring_result(struct kunit *test)
>  	damon_update_monitoring_result(r, &old_attrs, &new_attrs);
>  	KUNIT_EXPECT_EQ(test, r->nr_accesses, 150);
>  	KUNIT_EXPECT_EQ(test, r->age, 20);
> +
> +	damon_free_region(r);

This looks nice.  Thank you for fixing this!

>  }
>  
>  static void damon_test_set_attrs(struct kunit *test)
> -- 
> 2.34.1
> 

Thanks,
SJ


  reply	other threads:[~2023-09-18  5:33 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-18  5:10 [PATCH RESEND 0/2] mm/damon/core-test: Fix memory leaks in core-test Jinjie Ruan
2023-09-18  5:10 ` [PATCH RESEND 1/2] mm/damon/core-test: Fix memory leak in damon_new_region() Jinjie Ruan
2023-09-18  5:33   ` SeongJae Park [this message]
2023-09-18  7:13     ` Ruan Jinjie
2023-09-18  5:10 ` [PATCH RESEND 2/2] mm/damon/core-test: Fix memory leak in damon_new_ctx() Jinjie Ruan
2023-09-18  5:38   ` Feng Tang
2023-09-18  5:43   ` SeongJae Park
2023-09-18  7:12     ` Ruan Jinjie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230918053320.61408-1-sj@kernel.org \
    --to=sj@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=brendan.higgins@linux.dev \
    --cc=damon@lists.linux.dev \
    --cc=feng.tang@intel.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ruanjinjie@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox