* [RFC PATCH v1.2 0/2] mm/damon/stat: add kdamond_pid parameter
@ 2026-04-16 0:21 SeongJae Park
2026-04-16 0:21 ` [RFC PATCH v1.2 1/2] mm/damon/stat: add a parameter for reading kdamond pid SeongJae Park
2026-04-16 0:21 ` [RFC PATCH v1.2 2/2] Docs/admin-guide/mm/damon/stat: document kdamond_pid parameter SeongJae Park
0 siblings, 2 replies; 7+ messages in thread
From: SeongJae Park @ 2026-04-16 0:21 UTC (permalink / raw)
Cc: SeongJae Park, Liam R. Howlett, Andrew Morton, David Hildenbrand,
Jonathan Corbet, Lorenzo Stoakes, Michal Hocko, Mike Rapoport,
Shuah Khan, Suren Baghdasaryan, Vlastimil Babka, damon, linux-doc,
linux-kernel, linux-mm
DAMON_STAT doesn't provide the pid of its kdamond, unlike DAMON_RECLAIM
and DAMON_LRU_SORT. This makes user-space management of DAMON_STAT
unnecessarily complicated. Provide the information via a new parameter,
namely kdamond_pid, and document it.
Changes from RFC v1.1
- rfc v1.1: https://lore.kernel.org/20260414235912.98174-1-sj@kernel.org
- Close the parentheses of error handling block.
Changes from RFC
- rfc: https://lore.kernel.org/20260414053742.90296-1-sj@kernel.org
- Fix damon_kdamond_pid() failure handling.
SeongJae Park (2):
mm/damon/stat: add a parameter for reading kdamond pid
Docs/admin-guide/mm/damon/stat: document kdamond_pid parameter
Documentation/admin-guide/mm/damon/stat.rst | 7 +++++++
mm/damon/stat.c | 18 ++++++++++++++++++
2 files changed, 25 insertions(+)
base-commit: bf44f59d29186d80db01e4124a8ab23b3b235b32
--
2.47.3
^ permalink raw reply [flat|nested] 7+ messages in thread
* [RFC PATCH v1.2 1/2] mm/damon/stat: add a parameter for reading kdamond pid
2026-04-16 0:21 [RFC PATCH v1.2 0/2] mm/damon/stat: add kdamond_pid parameter SeongJae Park
@ 2026-04-16 0:21 ` SeongJae Park
2026-04-16 0:59 ` sashiko-bot
2026-04-16 0:21 ` [RFC PATCH v1.2 2/2] Docs/admin-guide/mm/damon/stat: document kdamond_pid parameter SeongJae Park
1 sibling, 1 reply; 7+ messages in thread
From: SeongJae Park @ 2026-04-16 0:21 UTC (permalink / raw)
Cc: SeongJae Park, Andrew Morton, damon, linux-kernel, linux-mm
Knowing the pid of the kdamonds can help user-space management including
monitoring of DAMON's system resource consumption. To make it easier,
DAMON_SYSFS, DAMON_RECLAIM and DAMON_LRU_SORT provide the pid
information. DAMON_STAT is not providing it, though. Expose the pid of
DAMON_STAT kdamond via a new read-only module parameter, namely
kdamond_pid. This also makes DAMON modules usage more standardized,
because DAMON_RECLAIM and DAMON_LRU_SORT also provide the information
via their read-only parameters of the same name.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
mm/damon/stat.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/mm/damon/stat.c b/mm/damon/stat.c
index 99ba346f9e325..4901e9a7c3398 100644
--- a/mm/damon/stat.c
+++ b/mm/damon/stat.c
@@ -44,6 +44,15 @@ module_param(aggr_interval_us, ulong, 0400);
MODULE_PARM_DESC(aggr_interval_us,
"Current tuned aggregation interval in microseconds");
+/*
+ * PID of the DAMON thread
+ *
+ * If DAMON_STAT is enabled, this becomes the PID of the worker thread.
+ * Else, -1.
+ */
+static int kdamond_pid __read_mostly = -1;
+module_param(kdamond_pid, int, 0400);
+
static struct damon_ctx *damon_stat_context;
static unsigned long damon_stat_last_refresh_jiffies;
@@ -260,6 +269,14 @@ static int damon_stat_start(void)
damon_stat_context = NULL;
return err;
}
+ kdamond_pid = damon_kdamond_pid(damon_stat_context);
+ if (kdamond_pid < 0) {
+ err = kdamond_pid;
+ kdamond_pid = -1;
+ damon_destroy_ctx(damon_stat_context);
+ damon_stat_context = NULL;
+ return err;
+ }
damon_stat_last_refresh_jiffies = jiffies;
call_control.data = damon_stat_context;
@@ -269,6 +286,7 @@ static int damon_stat_start(void)
static void damon_stat_stop(void)
{
damon_stop(&damon_stat_context, 1);
+ kdamond_pid = -1;
damon_destroy_ctx(damon_stat_context);
damon_stat_context = NULL;
}
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [RFC PATCH v1.2 2/2] Docs/admin-guide/mm/damon/stat: document kdamond_pid parameter
2026-04-16 0:21 [RFC PATCH v1.2 0/2] mm/damon/stat: add kdamond_pid parameter SeongJae Park
2026-04-16 0:21 ` [RFC PATCH v1.2 1/2] mm/damon/stat: add a parameter for reading kdamond pid SeongJae Park
@ 2026-04-16 0:21 ` SeongJae Park
1 sibling, 0 replies; 7+ messages in thread
From: SeongJae Park @ 2026-04-16 0:21 UTC (permalink / raw)
Cc: SeongJae Park, Liam R. Howlett, Andrew Morton, David Hildenbrand,
Jonathan Corbet, Lorenzo Stoakes, Michal Hocko, Mike Rapoport,
Shuah Khan, Suren Baghdasaryan, Vlastimil Babka, damon, linux-doc,
linux-kernel, linux-mm
Update DAMON_STAT usage document for newly added kdamond_pid parameter.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
Documentation/admin-guide/mm/damon/stat.rst | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/Documentation/admin-guide/mm/damon/stat.rst b/Documentation/admin-guide/mm/damon/stat.rst
index c4b14daeb2dd6..46c5dd96aa2ed 100644
--- a/Documentation/admin-guide/mm/damon/stat.rst
+++ b/Documentation/admin-guide/mm/damon/stat.rst
@@ -89,3 +89,10 @@ percentiles of the idle time values via this read-only parameter. Reading the
parameter returns 101 idle time values in milliseconds, separated by comma.
Each value represents 0-th, 1st, 2nd, 3rd, ..., 99th and 100th percentile idle
times.
+
+kdamond_pid
+-----------
+
+PID of the DAMON thread.
+
+If DAMON_STAT is enabled, this becomes the PID of the worker thread. Else, -1.
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [RFC PATCH v1.2 1/2] mm/damon/stat: add a parameter for reading kdamond pid
2026-04-16 0:21 ` [RFC PATCH v1.2 1/2] mm/damon/stat: add a parameter for reading kdamond pid SeongJae Park
@ 2026-04-16 0:59 ` sashiko-bot
2026-04-16 3:35 ` SeongJae Park
0 siblings, 1 reply; 7+ messages in thread
From: sashiko-bot @ 2026-04-16 0:59 UTC (permalink / raw)
To: SeongJae Park; +Cc: damon
Sashiko AI review found 1 potential issue(s):
- [Medium] Stale `kdamond_pid` exposed to userspace if `damon_call()` fails during startup.
--
commit 46eb0a4278ccd2dec190db28b78b0766b8865901
Author: SeongJae Park <sj@kernel.org>
mm/damon/stat: add a parameter for reading kdamond pid
This commit exposes the pid of the DAMON_STAT kdamond via a new read-only
module parameter named kdamond_pid, standardizing usage with other DAMON
modules.
> diff --git a/mm/damon/stat.c b/mm/damon/stat.c
> index 99ba346f9e325..4901e9a7c3398 100644
> --- a/mm/damon/stat.c
> +++ b/mm/damon/stat.c
[ ... ]
> @@ -260,6 +269,14 @@ static int damon_stat_start(void)
> damon_stat_context = NULL;
> return err;
> }
> + kdamond_pid = damon_kdamond_pid(damon_stat_context);
> + if (kdamond_pid < 0) {
> + err = kdamond_pid;
> + kdamond_pid = -1;
> + damon_destroy_ctx(damon_stat_context);
> + damon_stat_context = NULL;
> + return err;
> + }
>
> damon_stat_last_refresh_jiffies = jiffies;
> call_control.data = damon_stat_context;
If damon_call() fails right after this and returns an error,
damon_stat_start() will pass that error back to its caller and the module
will revert to a disabled state.
Does this leave a stale kdamond_pid exposed to userspace? The newly added
documentation states: "If DAMON_STAT is enabled, this becomes the PID of
the worker thread. Else, -1." Since the initialization fails, the module
remains disabled, but kdamond_pid isn't reset to -1 on this error path.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260416002149.87090-1-sj@kernel.org?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC PATCH v1.2 1/2] mm/damon/stat: add a parameter for reading kdamond pid
2026-04-16 0:59 ` sashiko-bot
@ 2026-04-16 3:35 ` SeongJae Park
2026-04-16 4:06 ` SeongJae Park
0 siblings, 1 reply; 7+ messages in thread
From: SeongJae Park @ 2026-04-16 3:35 UTC (permalink / raw)
To: sashiko-bot; +Cc: SeongJae Park, damon
TL; DR: Sashiko found good point. I will fix it in the next revision.
On Thu, 16 Apr 2026 00:59:35 +0000 sashiko-bot@kernel.org wrote:
> Sashiko AI review found 1 potential issue(s):
> - [Medium] Stale `kdamond_pid` exposed to userspace if `damon_call()` fails during startup.
> --
>
> commit 46eb0a4278ccd2dec190db28b78b0766b8865901
> Author: SeongJae Park <sj@kernel.org>
>
> mm/damon/stat: add a parameter for reading kdamond pid
>
> This commit exposes the pid of the DAMON_STAT kdamond via a new read-only
> module parameter named kdamond_pid, standardizing usage with other DAMON
> modules.
>
> > diff --git a/mm/damon/stat.c b/mm/damon/stat.c
> > index 99ba346f9e325..4901e9a7c3398 100644
> > --- a/mm/damon/stat.c
> > +++ b/mm/damon/stat.c
>
> [ ... ]
>
> > @@ -260,6 +269,14 @@ static int damon_stat_start(void)
> > damon_stat_context = NULL;
> > return err;
> > }
> > + kdamond_pid = damon_kdamond_pid(damon_stat_context);
> > + if (kdamond_pid < 0) {
> > + err = kdamond_pid;
> > + kdamond_pid = -1;
> > + damon_destroy_ctx(damon_stat_context);
> > + damon_stat_context = NULL;
> > + return err;
> > + }
> >
> > damon_stat_last_refresh_jiffies = jiffies;
> > call_control.data = damon_stat_context;
>
> If damon_call() fails right after this and returns an error,
> damon_stat_start() will pass that error back to its caller and the module
> will revert to a disabled state.
>
> Does this leave a stale kdamond_pid exposed to userspace? The newly added
> documentation states: "If DAMON_STAT is enabled, this becomes the PID of
> the worker thread. Else, -1." Since the initialization fails, the module
> remains disabled, but kdamond_pid isn't reset to -1 on this error path.
Nice catch. I will do the kdamond_pid setup after damon_call() success, like
below. Also, I will remove the damon_stat_context deallocation and unset when
damon_kdamond_pid() failure, because it will be deallocated when
damon_stat_start() is called again. The change will look like below:
--- a/mm/damon/stat.c
+++ b/mm/damon/stat.c
@@ -222,18 +222,19 @@ static int damon_stat_start(void)
damon_stat_context = NULL;
return err;
}
+
+ damon_stat_last_refresh_jiffies = jiffies;
+ call_control.data = damon_stat_context;
+ err = damon_call(damon_stat_context, &call_control);
+ if (err)
+ return err;
+
kdamond_pid = damon_kdamond_pid(damon_stat_context);
if (kdamond_pid < 0) {
err = kdamond_pid;
kdamond_pid = -1;
- damon_destroy_ctx(damon_stat_context);
- damon_stat_context = NULL;
return err;
}
-
- damon_stat_last_refresh_jiffies = jiffies;
- call_control.data = damon_stat_context;
- return damon_call(damon_stat_context, &call_control);
}
static void damon_stat_stop(void)
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20260416002149.87090-1-sj@kernel.org?part=1
Thanks,
SJ
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC PATCH v1.2 1/2] mm/damon/stat: add a parameter for reading kdamond pid
2026-04-16 3:35 ` SeongJae Park
@ 2026-04-16 4:06 ` SeongJae Park
2026-04-16 4:29 ` SeongJae Park
0 siblings, 1 reply; 7+ messages in thread
From: SeongJae Park @ 2026-04-16 4:06 UTC (permalink / raw)
To: SeongJae Park; +Cc: sashiko-bot, damon
On Wed, 15 Apr 2026 20:35:45 -0700 SeongJae Park <sj@kernel.org> wrote:
> TL; DR: Sashiko found good point. I will fix it in the next revision.
>
> On Thu, 16 Apr 2026 00:59:35 +0000 sashiko-bot@kernel.org wrote:
>
> > Sashiko AI review found 1 potential issue(s):
> > - [Medium] Stale `kdamond_pid` exposed to userspace if `damon_call()` fails during startup.
> > --
> >
> > commit 46eb0a4278ccd2dec190db28b78b0766b8865901
> > Author: SeongJae Park <sj@kernel.org>
> >
> > mm/damon/stat: add a parameter for reading kdamond pid
> >
> > This commit exposes the pid of the DAMON_STAT kdamond via a new read-only
> > module parameter named kdamond_pid, standardizing usage with other DAMON
> > modules.
> >
> > > diff --git a/mm/damon/stat.c b/mm/damon/stat.c
> > > index 99ba346f9e325..4901e9a7c3398 100644
> > > --- a/mm/damon/stat.c
> > > +++ b/mm/damon/stat.c
> >
> > [ ... ]
> >
> > > @@ -260,6 +269,14 @@ static int damon_stat_start(void)
> > > damon_stat_context = NULL;
> > > return err;
> > > }
> > > + kdamond_pid = damon_kdamond_pid(damon_stat_context);
> > > + if (kdamond_pid < 0) {
> > > + err = kdamond_pid;
> > > + kdamond_pid = -1;
> > > + damon_destroy_ctx(damon_stat_context);
> > > + damon_stat_context = NULL;
> > > + return err;
> > > + }
> > >
> > > damon_stat_last_refresh_jiffies = jiffies;
> > > call_control.data = damon_stat_context;
> >
> > If damon_call() fails right after this and returns an error,
> > damon_stat_start() will pass that error back to its caller and the module
> > will revert to a disabled state.
> >
> > Does this leave a stale kdamond_pid exposed to userspace? The newly added
> > documentation states: "If DAMON_STAT is enabled, this becomes the PID of
> > the worker thread. Else, -1." Since the initialization fails, the module
> > remains disabled, but kdamond_pid isn't reset to -1 on this error path.
>
> Nice catch. I will do the kdamond_pid setup after damon_call() success, like
> below.
Wait... The same question was also raised [1] by Sashiko on the previous
version. And I was wrongly saying it is not this patch's fault. That was
completely wrong.
And the kdamond can stop even after the damon_kdamond_pid() call, if the
region_store_histogram allocation fails. In the case, kdamond_pid will again
be stale. 'enabled' parameter value will also be stale in the case. The worst
part of it is that in the case, DAMON_STAT cannot be restarted before the
system is reboot. The bug is similar to the one [2] that Liew found and
working on.
I will hold this patch and fix the bug first.
> Also, I will remove the damon_stat_context deallocation and unset when
> damon_kdamond_pid() failure, because it will be deallocated when
> damon_stat_start() is called again.
This also means my reply [1] to Sashiko's question about the damon_stat_context
leak on the previous version was wrong. I was saying it is indeed a bug, but
it was not.
[1] https://lore.kernel.org/20260414053742.90296-1-sj@kernel.org
[2] https://lore.kernel.org/20260415235529.86329-1-sj@kernel.org
Thanks,
SJ
[...]
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [RFC PATCH v1.2 1/2] mm/damon/stat: add a parameter for reading kdamond pid
2026-04-16 4:06 ` SeongJae Park
@ 2026-04-16 4:29 ` SeongJae Park
0 siblings, 0 replies; 7+ messages in thread
From: SeongJae Park @ 2026-04-16 4:29 UTC (permalink / raw)
To: SeongJae Park; +Cc: sashiko-bot, damon
On Wed, 15 Apr 2026 21:06:01 -0700 SeongJae Park <sj@kernel.org> wrote:
> On Wed, 15 Apr 2026 20:35:45 -0700 SeongJae Park <sj@kernel.org> wrote:
>
> > TL; DR: Sashiko found good point. I will fix it in the next revision.
> >
> > On Thu, 16 Apr 2026 00:59:35 +0000 sashiko-bot@kernel.org wrote:
> >
> > > Sashiko AI review found 1 potential issue(s):
> > > - [Medium] Stale `kdamond_pid` exposed to userspace if `damon_call()` fails during startup.
> > > --
> > >
> > > commit 46eb0a4278ccd2dec190db28b78b0766b8865901
> > > Author: SeongJae Park <sj@kernel.org>
> > >
> > > mm/damon/stat: add a parameter for reading kdamond pid
> > >
> > > This commit exposes the pid of the DAMON_STAT kdamond via a new read-only
> > > module parameter named kdamond_pid, standardizing usage with other DAMON
> > > modules.
> > >
> > > > diff --git a/mm/damon/stat.c b/mm/damon/stat.c
> > > > index 99ba346f9e325..4901e9a7c3398 100644
> > > > --- a/mm/damon/stat.c
> > > > +++ b/mm/damon/stat.c
> > >
> > > [ ... ]
> > >
> > > > @@ -260,6 +269,14 @@ static int damon_stat_start(void)
> > > > damon_stat_context = NULL;
> > > > return err;
> > > > }
> > > > + kdamond_pid = damon_kdamond_pid(damon_stat_context);
> > > > + if (kdamond_pid < 0) {
> > > > + err = kdamond_pid;
> > > > + kdamond_pid = -1;
> > > > + damon_destroy_ctx(damon_stat_context);
> > > > + damon_stat_context = NULL;
> > > > + return err;
> > > > + }
> > > >
> > > > damon_stat_last_refresh_jiffies = jiffies;
> > > > call_control.data = damon_stat_context;
> > >
> > > If damon_call() fails right after this and returns an error,
> > > damon_stat_start() will pass that error back to its caller and the module
> > > will revert to a disabled state.
> > >
> > > Does this leave a stale kdamond_pid exposed to userspace? The newly added
> > > documentation states: "If DAMON_STAT is enabled, this becomes the PID of
> > > the worker thread. Else, -1." Since the initialization fails, the module
> > > remains disabled, but kdamond_pid isn't reset to -1 on this error path.
> >
> > Nice catch. I will do the kdamond_pid setup after damon_call() success, like
> > below.
>
> Wait... The same question was also raised [1] by Sashiko on the previous
> version. And I was wrongly saying it is not this patch's fault. That was
> completely wrong.
>
> And the kdamond can stop even after the damon_kdamond_pid() call, if the
> region_store_histogram allocation fails. In the case, kdamond_pid will again
> be stale. 'enabled' parameter value will also be stale in the case. The worst
> part of it is that in the case, DAMON_STAT cannot be restarted before the
> system is reboot. The bug is similar to the one [2] that Liew found and
> working on.
The last two sentences are wrong... In case of damon_stat(), writing 'N' to
'enabled' will just update the parameter even if damon_stop() fails. Hence,
users can restart DAMON_STAT after 'enabled' value became stale, by writing 'N'
to 'enabled' parameter to reset the value, and then writing 'Y' to 'enabled'
parameter again, to invoke damon_start() call. Still far from good user
experience, but less critical than the bug that Liew is working on.
>
> I will hold this patch and fix the bug first.
My above opinion is same. I will fix the bugs first.
Thanks,
SJ
[...]
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-04-16 4:29 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-16 0:21 [RFC PATCH v1.2 0/2] mm/damon/stat: add kdamond_pid parameter SeongJae Park
2026-04-16 0:21 ` [RFC PATCH v1.2 1/2] mm/damon/stat: add a parameter for reading kdamond pid SeongJae Park
2026-04-16 0:59 ` sashiko-bot
2026-04-16 3:35 ` SeongJae Park
2026-04-16 4:06 ` SeongJae Park
2026-04-16 4:29 ` SeongJae Park
2026-04-16 0:21 ` [RFC PATCH v1.2 2/2] Docs/admin-guide/mm/damon/stat: document kdamond_pid parameter SeongJae Park
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.