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 --- 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