Skip to contentSkip to conversation

mm/damon: define and use DAMON initialization check function

PATCH Applied SeongJae Park 2 participants 10 messages Sep 16, 2025

Subsystems (2)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

cover mm/damon: define and use DAMON initialization check function

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. Also those are basically duplications that make their maintenance difficult.

Make it reliable and easy to maintain, by implementing a new DAMON core layer API function for seeing if DAMON is ready to be used or not, and replacing the hacks of DAMON API callers with the new core layer function.

Changes from RFC
(https://lore.kernel.org/[email protected])
- Rebase on latest mm-new

[1] https://lore.kernel.org/[email protected]

SeongJae Park (7):
mm/damon/core: implement damon_initialized() function mm/damon/stat: use damon_initialized() mm/damon/reclaim: use damon_initialized() mm/damon/lru_sort: use damon_initialized() samples/damon/wsse: use damon_initialized() samples/damon/prcl: use damon_initialized() samples/damon/mtier: use damon_initialized()

 include/linux/damon.h |  1 +
 mm/damon/core.c       | 10 ++++++++++
 mm/damon/lru_sort.c   |  9 +++++++--
 mm/damon/reclaim.c    |  9 +++++++--
 mm/damon/stat.c       | 10 ++++++----
 samples/damon/mtier.c | 11 +++++++----
 samples/damon/prcl.c  | 11 +++++++----
 samples/damon/wsse.c  | 15 +++++++++------
 8 files changed, 54 insertions(+), 22 deletions(-)

base-commit: b6a9d79765ceb52c8889fd24e1ff3169cc12c80b

Signature · 1 line
2.39.5
author maintainer
#1 lore raw

PATCH 1/7 mm/damon/core: implement damon_initialized() function

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.

---
 include/linux/damon.h |  1 +
 mm/damon/core.c       | 10 ++++++++++
 2 files changed, 11 insertions(+)
include/linux/damon.h +1 -0
index aa7381be388c..cae8c613c5fc 100644
@@ -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);
mm/damon/core.c +10 -0
index 775121ae7a9b..93848b4c6944 100644
@@ -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);
Signature · 1 line
2.39.5
author maintainer
#2 lore raw

PATCH 2/7 mm/damon/stat: use damon_initialized()

In reply to SeongJae Park

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.

---
 mm/damon/stat.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
mm/damon/stat.c +6 -4
index 87bcd8866d4b..c4fbd8cfa5eb 100644
@@ -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;
Signature · 1 line
2.39.5
author maintainer
#3 lore raw

PATCH 3/7 mm/damon/reclaim: use damon_initialized()

In reply to SeongJae Park

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.

---
 mm/damon/reclaim.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
mm/damon/reclaim.c +7 -2
index 590f9d6c55ef..7ba3d0f9a19a 100644
@@ -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;
 
Signature · 1 line
2.39.5
author maintainer
#4 lore raw

PATCH 4/7 mm/damon/lru_sort: use damon_initialized()

In reply to SeongJae Park

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.

---
 mm/damon/lru_sort.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
mm/damon/lru_sort.c +7 -2
index ab6173a646bd..42b9a656f9de 100644
@@ -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;
 
Signature · 1 line
2.39.5
author maintainer
#5 lore raw

PATCH 5/7 samples/damon/wsse: use damon_initialized()

In reply to SeongJae Park

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.

---
 samples/damon/wsse.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
samples/damon/wsse.c +9 -6
index 21eaf15f987d..799ad4443943 100644
@@ -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)
Signature · 1 line
2.39.5
author maintainer
#6 lore raw

PATCH 6/7 samples/damon/prcl: use damon_initialized()

In reply to SeongJae Park

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.

---
 samples/damon/prcl.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
samples/damon/prcl.c +7 -4
index 0226652f94d5..b7c50f2656ce 100644
@@ -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)
Signature · 1 line
2.39.5
author maintainer
#7 lore raw

PATCH 7/7 samples/damon/mtier: use damon_initialized()

In reply to SeongJae Park

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.

---
 samples/damon/mtier.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
samples/damon/mtier.c +7 -4
index beaf36657dea..775838a23d93 100644
@@ -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)
Signature · 1 line
2.39.5
maintainer
#8 lore raw
In reply to SeongJae Park
Quoted 7 lines · On Mon, 15 Sep 2025 20:35:04 -0700 SeongJae Park <[email protected]> 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, ...?

Quoted 2 lines

Also those are basically duplications that
make their maintenance difficult.

Unclear. This means that the client hacks are no longer necessary after these changes?

author maintainer
#9 lore raw
Quoted 10 lines · On Mon, 15 Sep 2025 21:33:12 -0700 Andrew Morton <[email protected]>…
Quoted 7 lines · On Mon, 15 Sep 2025 20:35:04 -0700 SeongJae Park <[email protected]> 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.

Quoted 2 lines

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.

Quoted 6 lines

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.

Quoted 6 lines
Quoted 3 lines

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/[email protected]
"""

As always, please let me know if there is anything I can help.

Thanks,
SJ