From: SJ Park <sj@kernel.org>
To: sashiko-bot@kernel.org
Cc: SJ Park <sj@kernel.org>, damon@lists.linux.dev
Subject: Re: [RFC PATCH 2/2] mm/damon/lru_sort: skip damon_call() if ctx has not started
Date: Sun, 2 Aug 2026 12:16:05 -0700 [thread overview]
Message-ID: <20260802191607.10845-1-sj@kernel.org> (raw)
In-Reply-To: <20260802190838.015EB1F00A3A@smtp.kernel.org>
On Sun, 02 Aug 2026 19:08:37 +0000 sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
>
> Pre-existing issues:
> - [High] A TOCTOU race between `damon_start()` and `damon_call()` can cause `damon_call()` to loop indefinitely.
> --
>
> --- Patch [2]: [RFC PATCH 2/2] mm/damon/lru_sort: skip damon_call() if ctx has not started ---
> commit 9a410362ed5a4418640b6bf73c4ee49300c9dc16
> Author: SJ Park <sj@kernel.org>
>
> mm/damon/lru_sort: skip damon_call() if ctx has not started
>
> This commit prevents damon_call() from hanging when the DAMON context is
> initialized but not yet started. It introduces a boolean flag to track whether
> the context has ever successfully started, and skips the call if it has not.
>
> > diff --git a/mm/damon/lru_sort.c b/mm/damon/lru_sort.c
> > index 7e077084cb03a..7df45f9a0b3ae 100644
> > --- a/mm/damon/lru_sort.c
> > +++ b/mm/damon/lru_sort.c
> [ ... ]
> > @@ -364,11 +366,8 @@ static int damon_lru_sort_commit_inputs_store(const char *val,
> > if (!commit_inputs_request)
> > return 0;
> >
> > - /*
> > - * Skip damon_call() if ctx is not initialized to avoid
> > - * NULL pointer dereference.
> > - */
> > - if (!ctx)
> > + /* Skip damon_call() if ctx has not successfully started. */
> > + if (!damon_lru_sort_damon_has_started)
> > return -EINVAL;
> >
> > err = damon_call(ctx, &control);
>
> [Severity: High]
> This is a pre-existing issue, but could there be a TOCTOU race here between
> damon_start() and damon_call() that causes damon_call() to loop indefinitely?
>
> If a user starts DAMON (setting damon_lru_sort_damon_has_started = true),
> stops it (leaving call_controls_obsolete = true), and then concurrently
> starts DAMON again while calling damon_call() via commit_inputs, can
> damon_call() get stuck?
There is no such concurrent calls.
>
> Looking at mm/damon/core.c, __damon_start() sets ctx->kdamond before the new
> thread clears ctx->call_controls_obsolete:
>
> mm/damon/core.c:__damon_start() {
> ...
> ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d", nr_running_ctxs);
> ...
> }
>
> If damon_call() runs concurrently, it checks call_controls_obsolete without
> holding kdamond_lock, sees the stale true value, and jumps to the canceled
> label:
>
> mm/damon/core.c:damon_call() {
> ...
> if (ctx->call_controls_obsolete) {
> ...
> goto canceled;
> }
> ...
> canceled:
> while (damon_is_running(ctx))
> schedule_timeout_idle(msecs_to_jiffies(100));
> return -ECANCELED;
> }
>
> Since damon_is_running() returns true (because ctx->kdamond is set), and the
> new healthy thread will run indefinitely, will this task loop forever in
> TASK_IDLE?
So the issue cannot happen in real. But good point. Maybe the API or the
documentation can be safer. I will add this to my TODO list.
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20260802185430.9940-1-sj@kernel.org?part=2
Thanks,
SJ
prev parent reply other threads:[~2026-08-02 19:16 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-02 18:54 [RFC PATCH 0/2] mm/damon/{reclaim,lru_sort}: fix commit_inputs infinite hang SJ Park
2026-08-02 18:54 ` [RFC PATCH 1/2] mm/damon/reclaim: skip damon_call() if ctx has not started SJ Park
2026-08-02 19:04 ` sashiko-bot
2026-08-02 19:07 ` SJ Park
2026-08-02 18:54 ` [RFC PATCH 2/2] mm/damon/lru_sort: " SJ Park
2026-08-02 19:08 ` sashiko-bot
2026-08-02 19:16 ` SJ Park [this message]
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=20260802191607.10845-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