Commit 6557004a authored by Josh Law's avatar Josh Law Committed by Andrew Morton
Browse files

mm/damon/sysfs: check contexts->nr in repeat_call_fn

damon_sysfs_repeat_call_fn() calls damon_sysfs_upd_tuned_intervals(),
damon_sysfs_upd_schemes_stats(), and
damon_sysfs_upd_schemes_effective_quotas() without checking contexts->nr. 
If nr_contexts is set to 0 via sysfs while DAMON is running, these
functions dereference contexts_arr[0] and cause a NULL pointer
dereference.  Add the missing check.

For example, the issue can be reproduced using DAMON sysfs interface and
DAMON user-space tool (damo) [1] like below.

    $ sudo damo start --refresh_interval 1s
    $ echo 0 | sudo tee \
            /sys/kernel/mm/damon/admin/kdamonds/0/contexts/nr_contexts

Link: https://patch.msgid.link/20260320163559.178101-3-objecting@objecting.org
Link: https://lkml.kernel.org/r/20260321175427.86000-4-sj@kernel.org
Link: https://github.com/damonitor/damo

 [1]
Fixes: d809a7c6 ("mm/damon/sysfs: implement refresh_ms file internal work")
Signed-off-by: default avatarJosh Law <objecting@objecting.org>
Reviewed-by: default avatarSeongJae Park <sj@kernel.org>
Signed-off-by: default avatarSeongJae Park <sj@kernel.org>
Cc: <stable@vger.kernel.org>	[6.17+]
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 1bfe9fb5
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -1620,9 +1620,12 @@ static int damon_sysfs_repeat_call_fn(void *data)

	if (!mutex_trylock(&damon_sysfs_lock))
		return 0;
	if (sysfs_kdamond->contexts->nr != 1)
		goto out;
	damon_sysfs_upd_tuned_intervals(sysfs_kdamond);
	damon_sysfs_upd_schemes_stats(sysfs_kdamond);
	damon_sysfs_upd_schemes_effective_quotas(sysfs_kdamond);
out:
	mutex_unlock(&damon_sysfs_lock);
	return 0;
}