linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH 0/7] mm/damon: define and use DAMON initialization check function
@ 2025-09-12  2:39 SeongJae Park
  2025-09-12  2:39 ` [RFC PATCH 1/7] mm/damon/core: implement damon_initialized() function SeongJae Park
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: SeongJae Park @ 2025-09-12  2:39 UTC (permalink / raw)
  Cc: SeongJae Park, Andrew Morton, damon, kernel-team, linux-kernel, linux-mm

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.

[1] https://lore.kernel.org/20250909022238.2989-1-sj@kernel.org

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: f115189b48629e7a8aa707112190b7ccf9928d6b
-- 
2.39.5


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [RFC PATCH 1/7] mm/damon/core: implement damon_initialized() function
  2025-09-12  2:39 [RFC PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
@ 2025-09-12  2:39 ` SeongJae Park
  2025-09-12  2:39 ` [RFC PATCH 2/7] mm/damon/stat: use damon_initialized() SeongJae Park
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: SeongJae Park @ 2025-09-12  2:39 UTC (permalink / raw)
  Cc: SeongJae Park, Andrew Morton, 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 484b0558f426..0f012b1e39fa 100644
--- a/include/linux/damon.h
+++ b/include/linux/damon.h
@@ -941,6 +941,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 f32034973cc1..38ff417f4eb9 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -2880,6 +2880,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] 8+ messages in thread

* [RFC PATCH 2/7] mm/damon/stat: use damon_initialized()
  2025-09-12  2:39 [RFC PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
  2025-09-12  2:39 ` [RFC PATCH 1/7] mm/damon/core: implement damon_initialized() function SeongJae Park
@ 2025-09-12  2:39 ` SeongJae Park
  2025-09-12  2:39 ` [RFC PATCH 3/7] mm/damon/reclaim: " SeongJae Park
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: SeongJae Park @ 2025-09-12  2:39 UTC (permalink / raw)
  Cc: SeongJae Park, Andrew Morton, 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 c33df0ade183..d8010968bbed 100644
--- a/mm/damon/stat.c
+++ b/mm/damon/stat.c
@@ -220,8 +220,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)
 {
@@ -235,7 +233,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.
@@ -256,12 +254,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] 8+ messages in thread

* [RFC PATCH 3/7] mm/damon/reclaim: use damon_initialized()
  2025-09-12  2:39 [RFC PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
  2025-09-12  2:39 ` [RFC PATCH 1/7] mm/damon/core: implement damon_initialized() function SeongJae Park
  2025-09-12  2:39 ` [RFC PATCH 2/7] mm/damon/stat: use damon_initialized() SeongJae Park
@ 2025-09-12  2:39 ` SeongJae Park
  2025-09-12  2:39 ` [RFC PATCH 4/7] mm/damon/lru_sort: " SeongJae Park
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: SeongJae Park @ 2025-09-12  2:39 UTC (permalink / raw)
  Cc: SeongJae Park, Andrew Morton, 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] 8+ messages in thread

* [RFC PATCH 4/7] mm/damon/lru_sort: use damon_initialized()
  2025-09-12  2:39 [RFC PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
                   ` (2 preceding siblings ...)
  2025-09-12  2:39 ` [RFC PATCH 3/7] mm/damon/reclaim: " SeongJae Park
@ 2025-09-12  2:39 ` SeongJae Park
  2025-09-12  2:39 ` [RFC PATCH 5/7] samples/damon/wsse: " SeongJae Park
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: SeongJae Park @ 2025-09-12  2:39 UTC (permalink / raw)
  Cc: SeongJae Park, Andrew Morton, 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] 8+ messages in thread

* [RFC PATCH 5/7] samples/damon/wsse: use damon_initialized()
  2025-09-12  2:39 [RFC PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
                   ` (3 preceding siblings ...)
  2025-09-12  2:39 ` [RFC PATCH 4/7] mm/damon/lru_sort: " SeongJae Park
@ 2025-09-12  2:39 ` SeongJae Park
  2025-09-12  2:39 ` [RFC PATCH 6/7] samples/damon/prcl: " SeongJae Park
  2025-09-12  2:39 ` [RFC PATCH 7/7] samples/damon/mtier: " SeongJae Park
  6 siblings, 0 replies; 8+ messages in thread
From: SeongJae Park @ 2025-09-12  2:39 UTC (permalink / raw)
  Cc: SeongJae Park, Andrew Morton, 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] 8+ messages in thread

* [RFC PATCH 6/7] samples/damon/prcl: use damon_initialized()
  2025-09-12  2:39 [RFC PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
                   ` (4 preceding siblings ...)
  2025-09-12  2:39 ` [RFC PATCH 5/7] samples/damon/wsse: " SeongJae Park
@ 2025-09-12  2:39 ` SeongJae Park
  2025-09-12  2:39 ` [RFC PATCH 7/7] samples/damon/mtier: " SeongJae Park
  6 siblings, 0 replies; 8+ messages in thread
From: SeongJae Park @ 2025-09-12  2:39 UTC (permalink / raw)
  Cc: SeongJae Park, Andrew Morton, 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] 8+ messages in thread

* [RFC PATCH 7/7] samples/damon/mtier: use damon_initialized()
  2025-09-12  2:39 [RFC PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
                   ` (5 preceding siblings ...)
  2025-09-12  2:39 ` [RFC PATCH 6/7] samples/damon/prcl: " SeongJae Park
@ 2025-09-12  2:39 ` SeongJae Park
  6 siblings, 0 replies; 8+ messages in thread
From: SeongJae Park @ 2025-09-12  2:39 UTC (permalink / raw)
  Cc: SeongJae Park, Andrew Morton, 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] 8+ messages in thread

end of thread, other threads:[~2025-09-12  2:40 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-12  2:39 [RFC PATCH 0/7] mm/damon: define and use DAMON initialization check function SeongJae Park
2025-09-12  2:39 ` [RFC PATCH 1/7] mm/damon/core: implement damon_initialized() function SeongJae Park
2025-09-12  2:39 ` [RFC PATCH 2/7] mm/damon/stat: use damon_initialized() SeongJae Park
2025-09-12  2:39 ` [RFC PATCH 3/7] mm/damon/reclaim: " SeongJae Park
2025-09-12  2:39 ` [RFC PATCH 4/7] mm/damon/lru_sort: " SeongJae Park
2025-09-12  2:39 ` [RFC PATCH 5/7] samples/damon/wsse: " SeongJae Park
2025-09-12  2:39 ` [RFC PATCH 6/7] samples/damon/prcl: " SeongJae Park
2025-09-12  2:39 ` [RFC PATCH 7/7] samples/damon/mtier: " SeongJae Park

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox