DAMON development mailing list
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Junxi Qian" <qjx1298677004@gmail.com>
Cc: damon@lists.linux.dev
Subject: Re: [PATCH v2] mm/damon/sysfs-schemes: fix use-after-free on memcg_path and goal path
Date: Mon, 20 Apr 2026 18:31:45 +0000	[thread overview]
Message-ID: <20260420183146.0DA7AC2BCB0@smtp.kernel.org> (raw)
In-Reply-To: <20260420125405.362137-1-qjx1298677004@gmail.com>

Sashiko AI review found 1 potential issue(s):
- [High] Using `mutex_lock(&damon_sysfs_lock)` in sysfs `.show` and `.store` callbacks introduces a classic ABBA deadlock against concurrent directory removal.
--

commit 01b23742d072b6f9a33d05db48195e9adf5cf0a9
Author: Junxi Qian <qjx1298677004@gmail.com>

mm/damon/sysfs-schemes: fix use-after-free on memcg_path and goal path

This commit wraps the string pointer reads and writes in memcg_path_show(),
memcg_path_store(), path_show(), and path_store() with damon_sysfs_lock
to prevent use-after-free bugs when racing with the kdamond thread.

> diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c
> index 5186966dafb35..1a890e2a4eb9c 100644
> --- a/mm/damon/sysfs-schemes.c
> +++ b/mm/damon/sysfs-schemes.c
> @@ -533,9 +533,14 @@ static ssize_t memcg_path_show(struct kobject *kobj,
>  {
>  	struct damon_sysfs_scheme_filter *filter = container_of(kobj,
>  			struct damon_sysfs_scheme_filter, kobj);
> +	ssize_t len;
>  
> -	return sysfs_emit(buf, "%s\n",
> +	mutex_lock(&damon_sysfs_lock);

Could this introduce an ABBA deadlock against concurrent directory removal?

When a user reads a sysfs file, kernfs acquires an active reference
(s_active) before executing the show callback, which now blocks
unconditionally on damon_sysfs_lock.

If another thread simultaneously writes to a file that removes directories
(like nr_filters_store), it will acquire damon_sysfs_lock and call
kobject_put(). The teardown path then invokes kernfs_drain(), which
synchronously waits for s_active to drop.

This leaves the teardown thread holding the lock and waiting for s_active,
while the read thread holds s_active and waits for the lock.

Should this use mutex_trylock(&damon_sysfs_lock) and return -EBUSY on
failure, similar to other sysfs callbacks in this subsystem?

> +	len = sysfs_emit(buf, "%s\n",
>  			filter->memcg_path ? filter->memcg_path : "");
> +	mutex_unlock(&damon_sysfs_lock);
> +
> +	return len;
>  }
>  
>  static ssize_t memcg_path_store(struct kobject *kobj,
> @@ -543,15 +548,20 @@ static ssize_t memcg_path_store(struct kobject *kobj,
>  {
>  	struct damon_sysfs_scheme_filter *filter = container_of(kobj,
>  			struct damon_sysfs_scheme_filter, kobj);
> -	char *path = kmalloc_array(size_add(count, 1), sizeof(*path),
> -				   GFP_KERNEL);
> +	char *path, *old;
>  
> +	path = kmalloc_array(size_add(count, 1), sizeof(*path), GFP_KERNEL);
>  	if (!path)
>  		return -ENOMEM;
>  
>  	strscpy(path, buf, count + 1);
> -	kfree(filter->memcg_path);
> +
> +	mutex_lock(&damon_sysfs_lock);

Does this face the same deadlock risk as the show function? 

If converted to mutex_trylock(), would we need to free the newly allocated
path buffer before returning -EBUSY?

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260420125405.362137-1-qjx1298677004@gmail.com?part=1

  reply	other threads:[~2026-04-20 18:31 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-20  8:53 [PATCH] mm/damon/sysfs-schemes: fix use-after-free on memcg_path and goal path Junxi Qian
2026-04-20 11:53 ` sashiko-bot
2026-04-20 12:54 ` [PATCH v2] " Junxi Qian
2026-04-20 18:31   ` sashiko-bot [this message]
2026-04-21  1:20   ` SeongJae Park
2026-04-21  1:23     ` SeongJae Park
2026-04-21 15:01     ` SeongJae Park
2026-04-21  7:06 ` [PATCH] " Junxi Qian
2026-04-21 23:47   ` SeongJae Park
2026-04-22  4:31     ` junxi qian
2026-04-22  4:35       ` 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=20260420183146.0DA7AC2BCB0@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=damon@lists.linux.dev \
    --cc=qjx1298677004@gmail.com \
    --cc=sashiko@lists.linux.dev \
    /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