DAMON development mailing list
 help / color / mirror / Atom feed
From: SeongJae Park <sj@kernel.org>
To: sashiko-bot@kernel.org
Cc: SeongJae Park <sj@kernel.org>, damon@lists.linux.dev
Subject: Re: [RFC PATCH v2.1 1/3] mm/damon/reclaim: detect and use fresh enabled and kdamond_pid values
Date: Sat, 18 Apr 2026 18:47:59 -0700	[thread overview]
Message-ID: <20260419014800.877-1-sj@kernel.org> (raw)
In-Reply-To: <20260418225757.95221C19424@smtp.kernel.org>

On Sat, 18 Apr 2026 22:57:57 +0000 sashiko-bot@kernel.org wrote:

TL; DR: no blocker for this patch.

> Sashiko AI review found 2 potential issue(s):
> - [High] Missing context initialization check in the sysfs store handler can
>   trigger a NULL pointer dereference if module initialization fails.

Good catch, but not the fault of this patch.  I will separately work on it.

> - [Medium] The `damon_reclaim_enabled_store` function parses user input
>   directly into the global `enabled` variable before using it for control flow
>   decisions, causing a Time-Of-Check to Time-Of-Use (TOCTOU) race condition.

Sashiko-claiming issue cannot happen.  Read below for more details.

> --
> 
> commit 9d2c63ed13e63f6709b2752f9d974f5055f9d97e
> Author: SeongJae Park <sj@kernel.org>
> 
> mm/damon/reclaim: detect and use fresh enabled and kdamond_pid values
> 
> DAMON_RECLAIM updates the enabled and kdamond_pid parameter values dynamically
> to prevent stale values when the daemon is stopped unintentionally. This
> ensures users see the fresh running status and can restart the daemon when
> needed.
> 
> > diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
> > index 86da147786583..fe7fce26cf6ce 100644
> > --- a/mm/damon/reclaim.c
> > +++ b/mm/damon/reclaim.c
> 
> [ ... ]
> 
> > @@ -332,42 +316,83 @@ module_param_cb(addr_unit, &addr_unit_param_ops, &addr_unit, 0600);
> >  MODULE_PARM_DESC(addr_unit,
> >  	"Scale factor for DAMON_RECLAIM to ops address conversion (default: 1)");
> >  
> > +static bool damon_reclaim_enabled(void)
> > +{
> > +	if (!ctx)
> > +		return false;
> > +	return damon_is_running(ctx);
> > +}
> > +
> >  static int damon_reclaim_enabled_store(const char *val,
> >  		const struct kernel_param *kp)
> >  {
> > -	bool is_enabled = enabled;
> > -	bool enable;
> >  	int err;
> >  
> > -	err = kstrtobool(val, &enable);
> > +	err = kstrtobool(val, &enabled);
> 
> By parsing user input directly into the global enabled variable, does this
> introduce a race condition?
> 
> If multiple threads write to the sysfs enabled file concurrently, thread A's
> parsed input could be overwritten by thread B before thread A evaluates it
> in damon_reclaim_turn(enabled).
> 
> Could this cause thread A to execute the opposite action of what was
> requested while still returning success to the user?

Sysfs writes are protected by kernel_param_lock, so such concurrent
user inputs cannot happen.

> 
> >  	if (err)
> >  		return err;
> >  
> > -	if (is_enabled == enable)
> > +	if (damon_reclaim_enabled() == enabled)
> >  		return 0;
> >  
> >  	/* Called before init function.  The function will handle this. */
> >  	if (!damon_initialized())
> > -		goto set_param_out;
> > +		return 0;
> >  
> > -	err = damon_reclaim_turn(enable);
> > -	if (err)
> > -		return err;
> > +	return damon_reclaim_turn(enabled);
> 
> Can this trigger a null pointer dereference if the module initialization
> fails?
> 
> If damon_reclaim is built into the kernel and damon_reclaim_init() fails
> (for example, due to an allocation failure), the global ctx pointer remains
> null. Because it is a built-in module, the sysfs parameters remain exposed.
> 
> When a user writes 'Y' to the enabled file, damon_initialized() will pass
> because the core DAMON subsystem initialized successfully during boot. The
> code then calls damon_reclaim_turn(true) which passes the null ctx down to
> damon_commit_ctx(ctx, param_ctx).
> 
> Should there be an explicit if (!ctx) validation before attempting to start
> the reclaim process?

Good finding, but the bug was there even before this patch, so no blocker of
this patch.  I will separately work on the bug.


Thanks,
SJ

[...]

  reply	other threads:[~2026-04-19  1:48 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 [this message]
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
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=20260419014800.877-1-sj@kernel.org \
    --to=sj@kernel.org \
    --cc=damon@lists.linux.dev \
    --cc=sashiko-bot@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