* [PATCH] mm/damon/sysfs: skip registration if DAMON core init fails
@ 2026-11-23 7:51 Aveline Noir
2026-11-23 15:36 ` SJ Park
2026-11-24 12:18 ` Enze Li
0 siblings, 2 replies; 5+ messages in thread
From: Aveline Noir @ 2026-11-23 7:51 UTC (permalink / raw)
To: SJ Park, Andrew Morton; +Cc: Lian Wang, Dawei Li, damon, linux-mm
Hi SJ,
Here's the sysfs fix I mentioned in the report queue review.
-- >8 --
While reviewing the DAMON report queue series, I noticed that DAMON
sysfs still registers its interface if DAMON core initialization fails.
If damon_init() fails to allocate damon_region_cache, starting a
kdamond with an initial region can pass a NULL cache to
kmem_cache_alloc() via damon_new_region().
Check damon_initialized() before creating the DAMON sysfs directory and
return -ENOMEM if the core is unavailable.
Fixes: a18709442869 ("mm/damon: improve damon_new_region strategy")
Signed-off-by: Aveline Noir <jm5905938@gmail.com>
---
Tests passed in x86-64 QEMU TCG guests on 9f004372ebeb.
---
mm/damon/sysfs.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index 779c98205a94..7de0f03a3fd5 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -3035,6 +3035,9 @@ static int __init damon_sysfs_init(void)
struct damon_sysfs_ui_dir *admin;
int err;
+ if (!damon_initialized())
+ return -ENOMEM;
+
damon_sysfs_root = kobject_create_and_add("damon", mm_kobj);
if (!damon_sysfs_root)
return -ENOMEM;
---
base-commit: 9f004372ebeb18fdeb699f9880a0e472df90135b
change-id: 20261123-damon-sysfs-init-guard-fc87e688acae
--
Thanks,
Aveline Noir
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/damon/sysfs: skip registration if DAMON core init fails
2026-11-23 7:51 [PATCH] mm/damon/sysfs: skip registration if DAMON core init fails Aveline Noir
@ 2026-11-23 15:36 ` SJ Park
2026-11-24 12:31 ` Enze Li
2026-11-24 12:18 ` Enze Li
1 sibling, 1 reply; 5+ messages in thread
From: SJ Park @ 2026-11-23 15:36 UTC (permalink / raw)
To: Aveline Noir; +Cc: SJ Park, Andrew Morton, Lian Wang, Dawei Li, damon, linux-mm
On Mon, 23 Nov 2026 18:51:45 +1100 Aveline Noir <jm5905938@gmail.com> wrote:
> Hi SJ,
>
> Here's the sysfs fix I mentioned in the report queue review.
>
> -- >8 --
>
> While reviewing the DAMON report queue series, I noticed that DAMON
> sysfs still registers its interface if DAMON core initialization fails.
> If damon_init() fails to allocate damon_region_cache, starting a
> kdamond with an initial region can pass a NULL cache to
> kmem_cache_alloc() via damon_new_region().
"While reviewing ..." part seems unnecessary for git history. The user impact
(NULL dereference) could better be documented. What about below?
'''
DAMON sysfs registers ints interface even if DAMON core initialization fails.
If damon_init() fails to allocate damon_region_cache, starting a kdamond with
an initial region can pass a NULL cache to kmem_cache_alloc() via
damon_new_region(). As a result, NULL dereference can happen. The
initialization failure is unlikely, but the consequence is quite bad.
'''
>
> Check damon_initialized() before creating the DAMON sysfs directory and
> return -ENOMEM if the core is unavailable.
Thank you for finding and fixing the issue!
>
> Fixes: a18709442869 ("mm/damon: improve damon_new_region strategy")
> Signed-off-by: Aveline Noir <jm5905938@gmail.com>
Other than the above commit message,
Reviewed-by: SJ Park <sj@kernel.org>
With the above assumption, I applied this patch to damon/next [1] tree with the
commit message update. I will repost it soon (~1 week) as an mm.git merge
request. So, no action from your side is needed for now.
[1] https://origin.kernel.org/doc/html/latest/mm/damon/maintainer-profile.html#scm-trees
Thanks,
SJ
[...]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/damon/sysfs: skip registration if DAMON core init fails
2026-11-23 7:51 [PATCH] mm/damon/sysfs: skip registration if DAMON core init fails Aveline Noir
2026-11-23 15:36 ` SJ Park
@ 2026-11-24 12:18 ` Enze Li
1 sibling, 0 replies; 5+ messages in thread
From: Enze Li @ 2026-11-24 12:18 UTC (permalink / raw)
To: Aveline Noir, SJ Park, Andrew Morton; +Cc: Lian Wang, Dawei Li, damon, linux-mm
Hi Aveline,
On 11/23/26 15:51, Aveline Noir wrote:
> Hi SJ,
>
> Here's the sysfs fix I mentioned in the report queue review.
>
> -- >8 --
>
> While reviewing the DAMON report queue series, I noticed that DAMON
> sysfs still registers its interface if DAMON core initialization fails.
> If damon_init() fails to allocate damon_region_cache, starting a
> kdamond with an initial region can pass a NULL cache to
> kmem_cache_alloc() via damon_new_region().
>
> Check damon_initialized() before creating the DAMON sysfs directory and
> return -ENOMEM if the core is unavailable.
>
> Fixes: a18709442869 ("mm/damon: improve damon_new_region strategy")
> Signed-off-by: Aveline Noir <jm5905938@gmail.com>
> ---
> Tests passed in x86-64 QEMU TCG guests on 9f004372ebeb.
I gave this patch a try and it works.
I forced damon_init() to fail with the following modification on an
x86_64 VM,
8<------------------------------------------------------
diff --git a/mm/damon/core.c b/mm/damon/core.c
index 986404b547eb..194b30731572 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -4313,6 +4313,10 @@ bool damon_initialized(void)
static int __init damon_init(void)
{
damon_region_cache = KMEM_CACHE(damon_region, 0);
+ if (damon_region_cache) {
+ kmem_cache_destroy(damon_region_cache);
+ damon_region_cache = NULL;
+ }
if (unlikely(!damon_region_cache)) {
pr_err("creating damon_region_cache fails\n");
return -ENOMEM;
------------------------------------------------------>8
then ran `damo start paddr --regions 0-10000`. On the unpatched kernel
that immediately Oops,
8<------------------------------------------------------
[ 461.012857] BUG: kernel NULL pointer dereference, address:
000000000000001c
[ 461.012881] #PF: supervisor read access in kernel mode
[ 461.012891] #PF: error_code(0x0000) - not-present page
[ 461.012901] PGD 139e4f067 P4D 139e4f067 PUD 124048067 PMD 0
[ 461.012913] Oops: Oops: 0000 [#1] SMP NOPTI
[ 461.012924] CPU: 3 UID: 0 PID: 1610 Comm: python3 Tainted: G E
7.4.0-rc3-new+ #2 PREEMPT(lazy)
[ 461.012938] Tainted: [E]=UNSIGNED_MODULE
[ 461.012945] Hardware name: Red Hat KVM, BIOS
rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014
[ 461.012956] RIP: 0010:kmem_cache_alloc_noprof+0x16/0x400
[ 461.013083] Call Trace:
[ 461.013088] <TASK>
[ 461.013094] damon_new_region+0x22/0x60
[ 461.013104] damon_set_regions+0x38a/0x4e0
[ 461.013112] ? damon_sysfs_build_ctx+0x32c/0x400
[ 461.013123] damon_sysfs_build_ctx+0x391/0x400
[ 461.013133] state_store+0x30e/0x3f0
[ 461.013141] ? __kmalloc_noprof+0x292/0x590
[ 461.013151] kernfs_fop_write_iter+0x14a/0x200
[ 461.013160] vfs_write+0x25d/0x440
[ 461.013170] ksys_write+0x6b/0xe0
[ 461.013178] do_syscall_64+0xc4/0x570
[ 461.013188] ? __x64_sys_close+0x3d/0x80
[ 461.013196] ? do_syscall_64+0x101/0x570
[ 461.013205] ? do_syscall_64+0x101/0x570
[ 461.013213] ? do_syscall_64+0x35/0x570
[ 461.013221] entry_SYSCALL_64_after_hwframe+0x76/0x7e
[ 461.013230] RIP: 0033:0x7fbdeba26a02
------------------------------------------------------>8
With your patch, /sys/kernel/mm/damon/ simply doesn't show up when core
init fails, and the same damo command just errors out. No Oops, no NULL
deref in dmesg. That's exactly the behaviour we want.
Feel free to add:
Tested-by: Enze Li <lienze@kylinos.cn>
Reviewed-by: Enze Li <lienze@kylinos.cn>
Thanks,
Enze
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/damon/sysfs: skip registration if DAMON core init fails
2026-11-23 15:36 ` SJ Park
@ 2026-11-24 12:31 ` Enze Li
2026-11-24 15:25 ` SJ Park
0 siblings, 1 reply; 5+ messages in thread
From: Enze Li @ 2026-11-24 12:31 UTC (permalink / raw)
To: SJ Park, Aveline Noir; +Cc: Andrew Morton, Lian Wang, Dawei Li, damon, linux-mm
Hi SJ,
One tiny thing in the commit message you proposed, pls see below,
On 11/23/26 23:36, SJ Park wrote:
> On Mon, 23 Nov 2026 18:51:45 +1100 Aveline Noir <jm5905938@gmail.com> wrote:
>
> '''
> DAMON sysfs registers ints interface even if DAMON core initialization fails.
It seems to me that "ints" should be "its". Other than that, everything
else in the message reads good to me.
Best regards,
Enze
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/damon/sysfs: skip registration if DAMON core init fails
2026-11-24 12:31 ` Enze Li
@ 2026-11-24 15:25 ` SJ Park
0 siblings, 0 replies; 5+ messages in thread
From: SJ Park @ 2026-11-24 15:25 UTC (permalink / raw)
To: Enze Li
Cc: SJ Park, Aveline Noir, Andrew Morton, Lian Wang, Dawei Li, damon,
linux-mm
On Tue, 24 Nov 2026 20:31:46 +0800 Enze Li <enze.li@linux.dev> wrote:
> Hi SJ,
>
> One tiny thing in the commit message you proposed, pls see below,
>
> On 11/23/26 23:36, SJ Park wrote:
> > On Mon, 23 Nov 2026 18:51:45 +1100 Aveline Noir <jm5905938@gmail.com> wrote:
> >
> > '''
> > DAMON sysfs registers ints interface even if DAMON core initialization fails.
>
> It seems to me that "ints" should be "its". Other than that, everything
> else in the message reads good to me.
Thank you, Enze. I will fix it as you suggested.
Thanks,
SJ
[...]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-24 15:26 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 7:51 [PATCH] mm/damon/sysfs: skip registration if DAMON core init fails Aveline Noir
2026-11-23 15:36 ` SJ Park
2026-11-24 12:31 ` Enze Li
2026-11-24 15:25 ` SJ Park
2026-11-24 12:18 ` Enze Li
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox