Add a regression test that verifies damos_walk() does not deadlock
when racing with kdamond_fn() exit.
When kdamond_fn() finishes its main loop, it cancels remaining
damos_walk() requests and unsets damon_ctx->kdamond. Without the fix
in commit 33c3f6c2b48c, damos_walk() could be called right after
cancellation but before kdamond pointer unset, causing it to wait
forever for handling that never comes.
The test starts kdamond monitoring a short-lived process, waits for
the process to exit naturally triggering kdamond termination, then
rapidly calls update_schemes_tried_regions to hit the race window.
Without the fix this would hang indefinitely.
Fixes: 33c3f6c2b48c ("mm/damon/core: fix damos_walk() vs kdamond_fn() exit race")
Signed-off-by: Sailesh Nandanavanam <saileshnandanavanam@gmail.com>
---
tools/testing/selftests/damon/Makefile | 1 +
.../sysfs_damos_walk_kdamond_exit_race.py | 47 +++++++++++++++++++
2 files changed, 48 insertions(+)
create mode 100644 tools/testing/selftests/damon/sysfs_damos_walk_kdamond_exit_race.py
diff --git a/tools/testing/selftests/damon/Makefile b/tools/testing/selftests/damon/Makefile
index 2180c328a825..60c83d6c318e 100644
--- a/tools/testing/selftests/damon/Makefile
+++ b/tools/testing/selftests/damon/Makefile
@@ -20,6 +20,7 @@ TEST_PROGS += sysfs_update_removed_scheme_dir.sh
TEST_PROGS += sysfs_update_schemes_tried_regions_hang.py
TEST_PROGS += sysfs_memcg_path_leak.sh
TEST_PROGS += sysfs_no_op_commit_break.py
+TEST_PROGS += sysfs_damos_walk_kdamond_exit_race.py
EXTRA_CLEAN = __pycache__
diff --git a/tools/testing/selftests/damon/sysfs_damos_walk_kdamond_exit_race.py b/tools/testing/selftests/damon/sysfs_damos_walk_kdamond_exit_race.py
new file mode 100644
index 000000000000..9b9d66a23f51
--- /dev/null
+++ b/tools/testing/selftests/damon/sysfs_damos_walk_kdamond_exit_race.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+#
+# Regression test for damos_walk() vs kdamond_fn() exit race.
+import subprocess
+import time
+import _damon_sysfs
+def main():
+ proc = subprocess.Popen(['sleep', '0.3'])
+ kdamonds = _damon_sysfs.Kdamonds([_damon_sysfs.Kdamond(
+ contexts=[_damon_sysfs.DamonCtx(
+ ops='vaddr',
+ targets=[_damon_sysfs.DamonTarget(pid=proc.pid)],
+ schemes=[_damon_sysfs.Damos(
+ action='stat',
+ access_pattern=_damon_sysfs.DamosAccessPattern(
+ nr_accesses=[0, 200]))]
+ )]
+ )])
+ err = kdamonds.start()
+ if err is not None:
+ print('kdamond start failed: %s' % err)
+ exit(1)
+ proc.wait()
+ completed = False
+ timeout = time.time() + 5
+ while time.time() < timeout:
+ err = kdamonds.kdamonds[0].update_schemes_tried_regions()
+ if err is not None:
+ completed = True
+ break
+ try:
+ with open('/sys/kernel/mm/damon/admin/kdamonds/0/state') as f:
+ state = f.read().strip()
+ if state == 'off':
+ completed = True
+ break
+ except:
+ completed = True
+ break
+ if not completed:
+ print('FAIL: test timed out - possible damos_walk/kdamond exit race deadlock')
+ kdamonds.stop()
+ exit(1)
+ print('PASS: damos_walk() vs kdamond exit race not triggered')
+if __name__ == '__main__':
+ main()
--
2.34.1