linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/damon/sysfs: check for online node in target_nid_store
@ 2025-12-10 11:17 Swaraj Gaikwad
  2025-12-10 15:09 ` SeongJae Park
  0 siblings, 1 reply; 4+ messages in thread
From: Swaraj Gaikwad @ 2025-12-10 11:17 UTC (permalink / raw)
  To: SeongJae Park, Andrew Morton, open list:DAMON, open list:DAMON,
	open list
  Cc: skhan, david.hunter.linux, Swaraj Gaikwad

The 'target_nid_store' function previously accepted any integer value
written to the 'target_nid' sysfs file without validation. This allowed
users to set invalid Node IDs (e.g., -1 or 9999).

This patch adds a check using 'node_online(nid)' to ensure the input
is a valid, online node. If the node is invalid, it returns
-EINVAL. This change also resolves the TODO comment "error handling
for target_nid range".

Test:
Built kernel successfully with CONFIG_DAMON_SYSFS=y. Verified the fix
using a bash script that first creates a DAMON scheme, resets
'target_nid' to a valid value (0), and then attempts to write
invalid values (9999 and -1).

Confirmed that writing 9999 and -1 previously succeeded (incorrectly),
but now fail by rejecting the write and retaining the valid value (0)
as expected.

Signed-off-by: Swaraj Gaikwad <swarajgaikwad1925@gmail.com>
---
 mm/damon/sysfs-schemes.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c
index 6536f16006c9..b18321e64423 100644
--- a/mm/damon/sysfs-schemes.c
+++ b/mm/damon/sysfs-schemes.c
@@ -2244,11 +2244,18 @@ static ssize_t target_nid_store(struct kobject *kobj,
 	struct damon_sysfs_scheme *scheme = container_of(kobj,
 			struct damon_sysfs_scheme, kobj);
 	int err = 0;
+	int nid;

-	/* TODO: error handling for target_nid range. */
-	err = kstrtoint(buf, 0, &scheme->target_nid);
+	err = kstrtoint(buf, 0, &nid);
+	if (err)
+		return err;

-	return err ? err : count;
+	if (!node_online(nid))
+		return -EINVAL;
+
+	scheme->target_nid = nid;
+
+	return count;
 }

 static void damon_sysfs_scheme_release(struct kobject *kobj)

base-commit: e9a6fb0bcdd7609be6969112f3fbfcce3b1d4a7c
--
2.52.0



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

end of thread, other threads:[~2025-12-10 21:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-10 11:17 [PATCH] mm/damon/sysfs: check for online node in target_nid_store Swaraj Gaikwad
2025-12-10 15:09 ` SeongJae Park
2025-12-11  3:07   ` [PATCH v2] mm/damon/sysfs-schemes: Remove outdated TODO in target_nid_store() Swaraj Gaikwad
2025-12-10 21:49     ` SeongJae Park

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).