DAMON development mailing list
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "SeongJae Park" <sj@kernel.org>
Cc: damon@lists.linux.dev
Subject: Re: [RFC PATCH v2.1 3/3] mm/damon/stat: detect and use fresh enabled value
Date: Sat, 18 Apr 2026 23:51:55 +0000	[thread overview]
Message-ID: <20260418235155.A4953C19424@smtp.kernel.org> (raw)
In-Reply-To: <20260418222758.39795-4-sj@kernel.org>

Sashiko AI review found 1 potential issue(s):
- [High] Bypassing damon_stat_stop() and overwriting damon_stat_context causes a memory leak when explicitly disabling or restarting a kdamond thread that stopped internally.
--

commit b9703c85b8e9f48ea8dc673102aa2ecc0e1ac448
Author: SeongJae Park <sj@kernel.org>
mm/damon/stat: detect and use fresh enabled value

This commit dynamically detects the status of kdamond instead of relying on
the saved parameter value to avoid showing stale data when the daemon stops
internally.

Link: https://lore.kernel.org/20260416040602.88665-1-sj@kernel.org

> diff --git a/mm/damon/stat.c b/mm/damon/stat.c
> index 99ba346f9e325..3951b762cbddf 100644
> --- a/mm/damon/stat.c
> +++ b/mm/damon/stat.c

[ ... ]

> @@ -273,17 +276,23 @@ static void damon_stat_stop(void)
>  	damon_stat_context = NULL;
>  }
>  
> +static bool damon_stat_enabled(void)
> +{
> +	if (!damon_stat_context)
> +		return false;
> +	return damon_is_running(damon_stat_context);
> +}
> +
>  static int damon_stat_enabled_store(
>  		const char *val, const struct kernel_param *kp)
>  {
> -	bool is_enabled = enabled;
>  	int err;
>  
>  	err = kstrtobool(val, &enabled);
>  	if (err)
>  		return err;
>  
> -	if (is_enabled == enabled)
> +	if (damon_stat_enabled() == enabled)
>  		return 0;

Does this early return cause a memory leak if kdamond stops internally?

If kdamond stops internally, damon_is_running() will return false, causing
damon_stat_enabled() to also return false. However, damon_stat_context
remains allocated.

If a user subsequently writes 'N' to the enabled parameter to clean up,
damon_stat_enabled() == enabled evaluates to false == false. This triggers
the early return here, bypassing damon_stat_stop() which would have
called damon_destroy_ctx(damon_stat_context) to free the memory.

Similarly, if a user writes 'Y' to restart the module, damon_stat_start()
will be called. Looking at damon_stat_start(), it allocates a new context
by calling damon_stat_build_ctx() and overwrites the damon_stat_context
pointer without freeing the previous one.

Can this leak the context in both scenarios?

>  
>  	if (!damon_initialized())
> @@ -293,16 +302,17 @@ static int damon_stat_enabled_store(
>  		 */
>  		return 0;
>  
> -	if (enabled) {
> -		err = damon_stat_start();
> -		if (err)
> -			enabled = false;
> -		return err;
> -	}
> +	if (enabled)
> +		return damon_stat_start();
>  	damon_stat_stop();
>  	return 0;
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260418222758.39795-1-sj@kernel.org?part=3

  reply	other threads:[~2026-04-18 23:51 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-18 22:27 [RFC PATCH v2.1 0/3] mm/damon/modules: detect and use fresh status SeongJae Park
2026-04-18 22:27 ` [RFC PATCH v2.1 1/3] mm/damon/reclaim: detect and use fresh enabled and kdamond_pid values SeongJae Park
2026-04-18 22:57   ` sashiko-bot
2026-04-19  1:47     ` SeongJae Park
2026-04-18 22:27 ` [RFC PATCH v2.1 2/3] mm/damon/lru_sort: " SeongJae Park
2026-04-18 23:29   ` sashiko-bot
2026-04-19  1:49     ` SeongJae Park
2026-04-18 22:27 ` [RFC PATCH v2.1 3/3] mm/damon/stat: detect and use fresh enabled value SeongJae Park
2026-04-18 23:51   ` sashiko-bot [this message]
2026-04-19  1:52     ` SeongJae Park

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=20260418235155.A4953C19424@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=damon@lists.linux.dev \
    --cc=sashiko@lists.linux.dev \
    --cc=sj@kernel.org \
    /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