[PATCH 2/4] mm/damon/sysfs: check contexts->nr before clear_schemes_tried_regions

Josh Law
2026-03-19T15:58:08Z

The CLEAR_SCHEMES_TRIED_REGIONS command accesses contexts_arr[0]
without verifying nr_contexts >= 1, causing a NULL pointer dereference
when no context is configured. Add the missing check.

Signed-off-by: Josh Law <objecting@objecting.org>
---
mm/damon/sysfs.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index b573b9d60784..36ad2e8956c9 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -1769,6 +1769,8 @@ static int damon_sysfs_handle_cmd(enum damon_sysfs_cmd cmd,
case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS:
return damon_sysfs_update_schemes_tried_regions(kdamond, false);
case DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS:
+ if (kdamond->contexts->nr != 1)
+ return -EINVAL;
return damon_sysfs_schemes_clear_regions(
kdamond->contexts->contexts_arr[0]->schemes);
case DAMON_SYSFS_CMD_UPDATE_SCHEMES_EFFECTIVE_QUOTAS:
--
2.34.1