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] samples/damon/mtier: handle damon_start() failure
Date: Tue, 09 Jun 2026 01:06:15 +0000	[thread overview]
Message-ID: <20260609010616.328991F00898@smtp.kernel.org> (raw)
In-Reply-To: <20260609005443.2122-1-sj@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] `damon_stop()` aborts its loop on the first error, leaving subsequent contexts running and triggering a Use-After-Free when their memory is destroyed.
--

commit 6da87efa9d6982132beff965026bf155183e5c93
Author: SeongJae Park <sj@kernel.org>

samples/damon/mtier: handle damon_start() failure

This commit adds cleanup for damon_start() failures in the memory tiering
DAMON module. When damon_start() fails, it stops any running contexts
and frees them to prevent memory leaks and exclusive mode locks.

Link: https://lore.kernel.org/20260608112455.274231F00893@smtp.kernel.org

> diff --git a/samples/damon/mtier.c b/samples/damon/mtier.c
> index eb1143de8df17..66b591f2180fa 100644
> --- a/samples/damon/mtier.c
> +++ b/samples/damon/mtier.c
[ ... ]
> @@ -185,7 +186,15 @@ static int damon_sample_mtier_start(void)
>  		return -ENOMEM;
>  	}
>  	ctxs[1] = ctx;
> -	return damon_start(ctxs, 2, true);
> +	err = damon_start(ctxs, 2, true);
> +	if (!err)
> +		return 0;
> +
> +	if (damon_is_running(ctxs[0]))
> +		damon_stop(ctxs, 1);
> +	damon_destroy_ctx(ctxs[0]);
> +	damon_destroy_ctx(ctxs[1]);
> +	return err;
>  }
>  
>  static void damon_sample_mtier_stop(void)

[Severity: High]
This is a pre-existing issue, but does damon_stop() leave contexts running
and cause a use-after-free here?

Looking at damon_stop() in mm/damon/core.c:

int damon_stop(struct damon_ctx **ctxs, int nr_ctxs)
{
	int i, err = 0;

	for (i = 0; i < nr_ctxs; i++) {
		err = __damon_stop(ctxs[i]);
		if (err)
			break;
	}
	return err;
}

If __damon_stop() returns an error for the first context (for instance, if
the kthread exited prematurely due to an allocation failure), the loop
breaks immediately.

When damon_sample_mtier_stop() calls damon_stop(ctxs, 2), this would mean
ctxs[1] is never stopped if stopping ctxs[0] returns an error.

The subsequent unconditional calls to damon_destroy_ctx(ctxs[1]) in
damon_sample_mtier_stop() would then free the context, its targets, and its
schemes while kdamond is still executing and dereferencing them.

Can this lead to a use-after-free?

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

  reply	other threads:[~2026-06-09  1:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-09  0:54 [RFC PATCH] samples/damon/mtier: handle damon_start() failure SeongJae Park
2026-06-09  1:06 ` sashiko-bot [this message]
2026-06-09  1:42   ` SeongJae Park
2026-06-11 14:25     ` XIAO WU

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=20260609010616.328991F00898@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=damon@lists.linux.dev \
    --cc=sashiko-reviews@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