* [PATCH 1/7] mm/damon/core: implement damon_initialized() function
2025-09-16 3:35 [PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
@ 2025-09-16 3:35 ` SeongJae Park
2025-09-16 3:35 ` [PATCH 2/7] mm/damon/stat: use damon_initialized() SeongJae Park
` (6 subsequent siblings)
7 siblings, 0 replies; 10+ messages in thread
From: SeongJae Park @ 2025-09-16 3:35 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, kernel-team, linux-kernel, linux-mm
If DAMON is tried to be used when it is not yet successfully
initialized, the caller could be crashed. DAMON core layer is not
providing a reliable way to see if it is successfully initialized and
therefore ready to be used, though. As a result, DAMON API callers are
implementing their own hacks to see it. The hacks simply assume DAMON
should be ready on module init time. It is not reliable as DAMON
initialization can indeed fail if KMEM_CACHE() fails, and difficult to
maintain as those are duplicates. Implement a core layer API function
for better reliability and maintainability to replace the hacks with
followup commits.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
include/linux/damon.h | 1 +
mm/damon/core.c | 10 ++++++++++
2 files changed, 11 insertions(+)
diff --git a/include/linux/damon.h b/include/linux/damon.h
index aa7381be388c..cae8c613c5fc 100644
--- a/include/linux/damon.h
+++ b/include/linux/damon.h
@@ -938,6 +938,7 @@ static inline unsigned int damon_max_nr_accesses(const struct damon_attrs *attrs
}
+bool damon_initialized(void);
int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive);
int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
bool damon_is_running(struct damon_ctx *ctx);
diff --git a/mm/damon/core.c b/mm/damon/core.c
index 775121ae7a9b..93848b4c6944 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -2863,6 +2863,16 @@ void damon_update_region_access_rate(struct damon_region *r, bool accessed,
r->nr_accesses++;
}
+/**
+ * damon_initialized() - Return if DAMON is ready to be used.
+ *
+ * Return: true if DAMON is ready to be used, false otherwise.
+ */
+bool damon_initialized(void)
+{
+ return damon_region_cache != NULL;
+}
+
static int __init damon_init(void)
{
damon_region_cache = KMEM_CACHE(damon_region, 0);
--
2.39.5
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 2/7] mm/damon/stat: use damon_initialized()
2025-09-16 3:35 [PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
2025-09-16 3:35 ` [PATCH 1/7] mm/damon/core: implement damon_initialized() function SeongJae Park
@ 2025-09-16 3:35 ` SeongJae Park
2025-09-16 3:35 ` [PATCH 3/7] mm/damon/reclaim: " SeongJae Park
` (5 subsequent siblings)
7 siblings, 0 replies; 10+ messages in thread
From: SeongJae Park @ 2025-09-16 3:35 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, kernel-team, linux-kernel, linux-mm
DAMON_STAT is assuming DAMON is ready to use in module_init time, and
uses its own hack to see if it is the time. Use damon_initialized(),
which is a way for seeing if DAMON is ready to be used that is more
reliable and better to maintain instead of the hack.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
mm/damon/stat.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/mm/damon/stat.c b/mm/damon/stat.c
index 87bcd8866d4b..c4fbd8cfa5eb 100644
--- a/mm/damon/stat.c
+++ b/mm/damon/stat.c
@@ -214,8 +214,6 @@ static void damon_stat_stop(void)
damon_destroy_ctx(damon_stat_context);
}
-static bool damon_stat_init_called;
-
static int damon_stat_enabled_store(
const char *val, const struct kernel_param *kp)
{
@@ -229,7 +227,7 @@ static int damon_stat_enabled_store(
if (is_enabled == enabled)
return 0;
- if (!damon_stat_init_called)
+ if (!damon_initialized())
/*
* probably called from command line parsing (parse_args()).
* Cannot call damon_new_ctx(). Let damon_stat_init() handle.
@@ -250,12 +248,16 @@ static int __init damon_stat_init(void)
{
int err = 0;
- damon_stat_init_called = true;
+ if (!damon_initialized()) {
+ err = -ENOMEM;
+ goto out;
+ }
/* probably set via command line */
if (enabled)
err = damon_stat_start();
+out:
if (err && enabled)
enabled = false;
return err;
--
2.39.5
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 3/7] mm/damon/reclaim: use damon_initialized()
2025-09-16 3:35 [PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
2025-09-16 3:35 ` [PATCH 1/7] mm/damon/core: implement damon_initialized() function SeongJae Park
2025-09-16 3:35 ` [PATCH 2/7] mm/damon/stat: use damon_initialized() SeongJae Park
@ 2025-09-16 3:35 ` SeongJae Park
2025-09-16 3:35 ` [PATCH 4/7] mm/damon/lru_sort: " SeongJae Park
` (4 subsequent siblings)
7 siblings, 0 replies; 10+ messages in thread
From: SeongJae Park @ 2025-09-16 3:35 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, kernel-team, linux-kernel, linux-mm
DAMON_RECLAIM is assuming DAMON is ready to use in module_init time, and
uses its own hack to see if it is the time. Use damon_initialized(),
which is a way for seeing if DAMON is ready to be used that is more
reliable and better to maintain instead of the hack.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
mm/damon/reclaim.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
index 590f9d6c55ef..7ba3d0f9a19a 100644
--- a/mm/damon/reclaim.c
+++ b/mm/damon/reclaim.c
@@ -349,7 +349,7 @@ static int damon_reclaim_enabled_store(const char *val,
return 0;
/* Called before init function. The function will handle this. */
- if (!ctx)
+ if (!damon_initialized())
goto set_param_out;
err = damon_reclaim_turn(enable);
@@ -372,8 +372,13 @@ MODULE_PARM_DESC(enabled,
static int __init damon_reclaim_init(void)
{
- int err = damon_modules_new_paddr_ctx_target(&ctx, &target);
+ int err;
+ if (!damon_initialized()) {
+ err = -ENOMEM;
+ goto out;
+ }
+ err = damon_modules_new_paddr_ctx_target(&ctx, &target);
if (err)
goto out;
--
2.39.5
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 4/7] mm/damon/lru_sort: use damon_initialized()
2025-09-16 3:35 [PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
` (2 preceding siblings ...)
2025-09-16 3:35 ` [PATCH 3/7] mm/damon/reclaim: " SeongJae Park
@ 2025-09-16 3:35 ` SeongJae Park
2025-09-16 3:35 ` [PATCH 5/7] samples/damon/wsse: " SeongJae Park
` (3 subsequent siblings)
7 siblings, 0 replies; 10+ messages in thread
From: SeongJae Park @ 2025-09-16 3:35 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, kernel-team, linux-kernel, linux-mm
DAMON_LRU_SORT is assuming DAMON is ready to use in module_init time,
and uses its own hack to see if it is the time. Use
damon_initialized(), which is a way for seeing if DAMON is ready to be
used that is more reliable and better to maintain instead of the hack.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
mm/damon/lru_sort.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/mm/damon/lru_sort.c b/mm/damon/lru_sort.c
index ab6173a646bd..42b9a656f9de 100644
--- a/mm/damon/lru_sort.c
+++ b/mm/damon/lru_sort.c
@@ -345,7 +345,7 @@ static int damon_lru_sort_enabled_store(const char *val,
return 0;
/* Called before init function. The function will handle this. */
- if (!ctx)
+ if (!damon_initialized())
goto set_param_out;
err = damon_lru_sort_turn(enable);
@@ -368,8 +368,13 @@ MODULE_PARM_DESC(enabled,
static int __init damon_lru_sort_init(void)
{
- int err = damon_modules_new_paddr_ctx_target(&ctx, &target);
+ int err;
+ if (!damon_initialized()) {
+ err = -ENOMEM;
+ goto out;
+ }
+ err = damon_modules_new_paddr_ctx_target(&ctx, &target);
if (err)
goto out;
--
2.39.5
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 5/7] samples/damon/wsse: use damon_initialized()
2025-09-16 3:35 [PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
` (3 preceding siblings ...)
2025-09-16 3:35 ` [PATCH 4/7] mm/damon/lru_sort: " SeongJae Park
@ 2025-09-16 3:35 ` SeongJae Park
2025-09-16 3:35 ` [PATCH 6/7] samples/damon/prcl: " SeongJae Park
` (2 subsequent siblings)
7 siblings, 0 replies; 10+ messages in thread
From: SeongJae Park @ 2025-09-16 3:35 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, kernel-team, linux-kernel, linux-mm
damon_sample_wsse is assuming DAMON is ready to use in module_init time,
and uses its own hack to see if it is the time. Use
damon_initialized(), which is a way for seeing if DAMON is ready to be
used that is more reliable and better to maintain instead of the hack.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
samples/damon/wsse.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/samples/damon/wsse.c b/samples/damon/wsse.c
index 21eaf15f987d..799ad4443943 100644
--- a/samples/damon/wsse.c
+++ b/samples/damon/wsse.c
@@ -102,8 +102,6 @@ static void damon_sample_wsse_stop(void)
}
}
-static bool init_called;
-
static int damon_sample_wsse_enable_store(
const char *val, const struct kernel_param *kp)
{
@@ -117,10 +115,10 @@ static int damon_sample_wsse_enable_store(
if (enabled == is_enabled)
return 0;
- if (enabled) {
- if (!init_called)
- return 0;
+ if (!damon_initialized())
+ return 0;
+ if (enabled) {
err = damon_sample_wsse_start();
if (err)
enabled = false;
@@ -134,7 +132,12 @@ static int __init damon_sample_wsse_init(void)
{
int err = 0;
- init_called = true;
+ if (!damon_initialized()) {
+ err = -ENOMEM;
+ if (enabled)
+ enabled = false;
+ }
+
if (enabled) {
err = damon_sample_wsse_start();
if (err)
--
2.39.5
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 6/7] samples/damon/prcl: use damon_initialized()
2025-09-16 3:35 [PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
` (4 preceding siblings ...)
2025-09-16 3:35 ` [PATCH 5/7] samples/damon/wsse: " SeongJae Park
@ 2025-09-16 3:35 ` SeongJae Park
2025-09-16 3:35 ` [PATCH 7/7] samples/damon/mtier: " SeongJae Park
2025-09-16 4:33 ` [PATCH 0/7] mm/damon: define and use DAMON initialization check function Andrew Morton
7 siblings, 0 replies; 10+ messages in thread
From: SeongJae Park @ 2025-09-16 3:35 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, kernel-team, linux-kernel, linux-mm
damon_sample_prcl is assuming DAMON is ready to use in module_init time,
and uses its own hack to see if it is the time. Use
damon_initialized(), which is a way for seeing if DAMON is ready to be
used that is more reliable and better to maintain instead of the hack.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
samples/damon/prcl.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/samples/damon/prcl.c b/samples/damon/prcl.c
index 0226652f94d5..b7c50f2656ce 100644
--- a/samples/damon/prcl.c
+++ b/samples/damon/prcl.c
@@ -122,8 +122,6 @@ static void damon_sample_prcl_stop(void)
}
}
-static bool init_called;
-
static int damon_sample_prcl_enable_store(
const char *val, const struct kernel_param *kp)
{
@@ -137,7 +135,7 @@ static int damon_sample_prcl_enable_store(
if (enabled == is_enabled)
return 0;
- if (!init_called)
+ if (!damon_initialized())
return 0;
if (enabled) {
@@ -154,7 +152,12 @@ static int __init damon_sample_prcl_init(void)
{
int err = 0;
- init_called = true;
+ if (!damon_initialized()) {
+ if (enabled)
+ enabled = false;
+ return -ENOMEM;
+ }
+
if (enabled) {
err = damon_sample_prcl_start();
if (err)
--
2.39.5
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 7/7] samples/damon/mtier: use damon_initialized()
2025-09-16 3:35 [PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
` (5 preceding siblings ...)
2025-09-16 3:35 ` [PATCH 6/7] samples/damon/prcl: " SeongJae Park
@ 2025-09-16 3:35 ` SeongJae Park
2025-09-16 4:33 ` [PATCH 0/7] mm/damon: define and use DAMON initialization check function Andrew Morton
7 siblings, 0 replies; 10+ messages in thread
From: SeongJae Park @ 2025-09-16 3:35 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, kernel-team, linux-kernel, linux-mm
damon_sample_mtier is assuming DAMON is ready to use in module_init
time, and uses its own hack to see if it is the time. Use
damon_initialized(), which is a way for seeing if DAMON is ready to be
used that is more reliable and better to maintain instead of the hack.
Signed-off-by: SeongJae Park <sj@kernel.org>
---
samples/damon/mtier.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/samples/damon/mtier.c b/samples/damon/mtier.c
index beaf36657dea..775838a23d93 100644
--- a/samples/damon/mtier.c
+++ b/samples/damon/mtier.c
@@ -193,8 +193,6 @@ static void damon_sample_mtier_stop(void)
damon_destroy_ctx(ctxs[1]);
}
-static bool init_called;
-
static int damon_sample_mtier_enable_store(
const char *val, const struct kernel_param *kp)
{
@@ -208,7 +206,7 @@ static int damon_sample_mtier_enable_store(
if (enabled == is_enabled)
return 0;
- if (!init_called)
+ if (!damon_initialized())
return 0;
if (enabled) {
@@ -225,7 +223,12 @@ static int __init damon_sample_mtier_init(void)
{
int err = 0;
- init_called = true;
+ if (!damon_initialized()) {
+ if (enabled)
+ enabled = false;
+ return -ENOMEM;
+ }
+
if (enabled) {
err = damon_sample_mtier_start();
if (err)
--
2.39.5
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH 0/7] mm/damon: define and use DAMON initialization check function
2025-09-16 3:35 [PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
` (6 preceding siblings ...)
2025-09-16 3:35 ` [PATCH 7/7] samples/damon/mtier: " SeongJae Park
@ 2025-09-16 4:33 ` Andrew Morton
2025-09-16 5:54 ` SeongJae Park
7 siblings, 1 reply; 10+ messages in thread
From: Andrew Morton @ 2025-09-16 4:33 UTC (permalink / raw)
To: SeongJae Park; +Cc: damon, kernel-team, linux-kernel, linux-mm
On Mon, 15 Sep 2025 20:35:04 -0700 SeongJae Park <sj@kernel.org> wrote:
> If DAMON is tried to be used by its API callers when it is not yet
> successfully initialized, the callers could be crashed. Such issues
> actually happened and were fixed [1]. DAMON API callers are therefore
> having their own hacks for seeing if it is safe to use DAMON or not.
> Those built on an untreliable assumption that DAMON should be ready to
> be used on module init time. DAMON initialization could fail if
> KMEM_CACHE() fails, though.
Wait. Is there any realistic expectation that KMEM_CACHE() will fail
when DAMON uses it? We do have the convention of assuming that
__init-time allocations do not fail. If they do, an oops or panic is
an acceptable response.
Are these problems actually real-world demonstrable things, or has
someone been playing with fault injection or, ...?
> Also those are basically duplications that
> make their maintenance difficult.
Unclear. This means that the client hacks are no longer necessary after
these changes?
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH 0/7] mm/damon: define and use DAMON initialization check function
2025-09-16 4:33 ` [PATCH 0/7] mm/damon: define and use DAMON initialization check function Andrew Morton
@ 2025-09-16 5:54 ` SeongJae Park
0 siblings, 0 replies; 10+ messages in thread
From: SeongJae Park @ 2025-09-16 5:54 UTC (permalink / raw)
To: Andrew Morton; +Cc: SeongJae Park, damon, kernel-team, linux-kernel, linux-mm
On Mon, 15 Sep 2025 21:33:12 -0700 Andrew Morton <akpm@linux-foundation.org> wrote:
> On Mon, 15 Sep 2025 20:35:04 -0700 SeongJae Park <sj@kernel.org> wrote:
>
> > If DAMON is tried to be used by its API callers when it is not yet
> > successfully initialized, the callers could be crashed. Such issues
> > actually happened and were fixed [1]. DAMON API callers are therefore
> > having their own hacks for seeing if it is safe to use DAMON or not.
> > Those built on an untreliable assumption that DAMON should be ready to
> > be used on module init time. DAMON initialization could fail if
> > KMEM_CACHE() fails, though.
>
> Wait. Is there any realistic expectation that KMEM_CACHE() will fail
> when DAMON uses it?
Not really. The commit message is saying just a theoretical and unlikely
possibility.
> We do have the convention of assuming that
> __init-time allocations do not fail. If they do, an oops or panic is
> an acceptable response.
>
> Are these problems actually real-world demonstrable things, or has
> someone been playing with fault injection or, ...?
I don't have a way to demonstrate it. The commit message was only for
discussing a theoretical and unlikely case. Maybe it was better to just not
mention.
>
> > Also those are basically duplications that
> > make their maintenance difficult.
>
> Unclear. This means that the client hacks are no longer necessary after
> these changes?
You're correct.
Sorry for the poor cover letter message. If I have to post a new version of
this patch series, I would update the cover letter message as below. Does it
look better? If so, could you please update the cover letter part of the
commit on the mm tree?
"""
DAMON is initialized in subsystem initialization time, by damon_init(). If
DAMON API functions are called before the initialization, the system could
crash. Actually such issues happened and were fixed [1] in the past. For the
fix, DAMON API callers have updated to check if DAMON is initialized or not,
using their own hacks. The hacks are unnecessarily duplicated on every DAMON
API callers and therefore it would be difficult to reliably maintain in the
long term.
Make it reliable and easy to maintain. For this, implement a new DAMON core
layer API function that returns if DAMON is successfully initialized. If it
returns true, it means DAMON API functions are safe to be used. After the
introduction of the new API, update DAMON API callers to use the new function
instead of their own hacks.
[1] https://lore.kernel.org/20250909022238.2989-1-sj@kernel.org
"""
As always, please let me know if there is anything I can help.
Thanks,
SJ
^ permalink raw reply [flat|nested] 10+ messages in thread