Skip to contentSkip to conversation

mm/damon/core: fix commit_ops_filters by using correct nth function

PATCH Superseded Sang-Heon Jeon 2 participants 3 messages Aug 9, 2025 – Aug 10, 2025

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/damon/core: fix commit_ops_filters by using correct nth function

damos_commit_ops_filters() incorrectly uses damos_nth_filter() which iterates core_filters. As a result, performing a commit unintentionally corrupts ops_filters.

Add damos_nth_ops_filter() which iterates ops_filters. Use this function to fix issues caused by wrong iteration.

Also, add test to verify that modification is right way.

Fixes 3607cc590f18 ("mm/damon/core: support committing ops_filters") # 6.15.x

---
Changes from v1 [1]:
1. Fix code and commit message style.
2. Merge patch set into one patch.
3. Add fixes and cc section for backporting.

[1] https://lore.kernel.org/damon/[email protected]/

---
I tried to fix your all comments, but maybe i miss something. Then please let me know; I'll fix it as soon as possible.

---
 mm/damon/core.c                               | 14 +++-
 tools/testing/selftests/damon/Makefile        |  1 +
 .../damon/sysfs_no_op_commit_break.py         | 72 +++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/damon/sysfs_no_op_commit_break.py
mm/damon/core.c +13 -1
index 883d791a10e5..19c8f01fc81a 100644
@@ -862,6 +862,18 @@ static struct damos_filter *damos_nth_filter(int n, struct damos *s)
 	return NULL;
 }
 
+static struct damos_filter *damos_nth_ops_filter(int n, struct damos *s)
+{
+	struct damos_filter *filter;
+	int i = 0;
+
+	damos_for_each_ops_filter(filter, s) {
+		if (i++ == n)
+			return filter;
+	}
+	return NULL;
+}
+
 static void damos_commit_filter_arg(
 		struct damos_filter *dst, struct damos_filter *src)
 {
@@ -925,7 +937,7 @@ static int damos_commit_ops_filters(struct damos *dst, struct damos *src)
 	int i = 0, j = 0;
 
 	damos_for_each_ops_filter_safe(dst_filter, next, dst) {
-		src_filter = damos_nth_filter(i++, src);
+		src_filter = damos_nth_ops_filter(i++, src);
 		if (src_filter)
 			damos_commit_filter(dst_filter, src_filter);
 		else
tools/testing/selftests/damon/Makefile +1 -0
index 5b230deb19e8..44a4a819df55 100644
@@ -17,6 +17,7 @@ TEST_PROGS += reclaim.sh lru_sort.sh
 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
 
 EXTRA_CLEAN = __pycache__
 
tools/testing/selftests/damon/sysfs_no_op_commit_break.py new file +72 -0

72 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 8 lines · On Sat, 9 Aug 2025 22:07:56 +0900 Sang-Heon Jeon <[email protected]> wrote:

damos_commit_ops_filters() incorrectly uses damos_nth_filter() which iterates core_filters. As a result, performing a commit unintentionally corrupts ops_filters.

Add damos_nth_ops_filter() which iterates ops_filters. Use this function to fix issues caused by wrong iteration.

Also, add test to verify that modification is right way.

As I mentioned on v1 thread, please drop the test part, for ease of backporting.

Quoted 116 lines
Fixes 3607cc590f18 ("mm/damon/core: support committing ops_filters") # 6.15.x

---
Changes from v1 [1]:
1. Fix code and commit message style.
2. Merge patch set into one patch.
3. Add fixes and cc section for backporting.

[1] https://lore.kernel.org/damon/[email protected]/

---
I tried to fix your all comments, but maybe i miss something. Then please let me know; I'll fix it as soon as possible.

---
 mm/damon/core.c                               | 14 +++-
 tools/testing/selftests/damon/Makefile        |  1 +
 .../damon/sysfs_no_op_commit_break.py         | 72 +++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/damon/sysfs_no_op_commit_break.py
mm/damon/core.c +13 -1
index 883d791a10e5..19c8f01fc81a 100644
@@ -862,6 +862,18 @@ static struct damos_filter *damos_nth_filter(int n, struct damos *s)
 	return NULL;
 }
 
+static struct damos_filter *damos_nth_ops_filter(int n, struct damos *s)
+{
+	struct damos_filter *filter;
+	int i = 0;
+
+	damos_for_each_ops_filter(filter, s) {
+		if (i++ == n)
+			return filter;
+	}
+	return NULL;
+}
+
 static void damos_commit_filter_arg(
 		struct damos_filter *dst, struct damos_filter *src)
 {
@@ -925,7 +937,7 @@ static int damos_commit_ops_filters(struct damos *dst, struct damos *src)
 	int i = 0, j = 0;
 
 	damos_for_each_ops_filter_safe(dst_filter, next, dst) {
-		src_filter = damos_nth_filter(i++, src);
+		src_filter = damos_nth_ops_filter(i++, src);
 		if (src_filter)
 			damos_commit_filter(dst_filter, src_filter);
 		else
tools/testing/selftests/damon/Makefile +1 -0
index 5b230deb19e8..44a4a819df55 100644
@@ -17,6 +17,7 @@ TEST_PROGS += reclaim.sh lru_sort.sh
 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
 
 EXTRA_CLEAN = __pycache__
 
tools/testing/selftests/damon/sysfs_no_op_commit_break.py new file +43 -0
new file mode 100755
index 000000000000..fbefb1c83045
@@ -0,0 +1,72 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+
+import json
+import os
+import subprocess
+import sys
+
+import _damon_sysfs
+
+def dump_damon_status_dict(pid):
+    try:
+        subprocess.check_output(['which', 'drgn'], stderr=subprocess.DEVNULL)
+    except:
+        return None, 'drgn not found'
+    file_dir = os.path.dirname(os.path.abspath(__file__))
+    dump_script = os.path.join(file_dir, 'drgn_dump_damon_status.py')
+    rc = subprocess.call(['drgn', dump_script, pid, 'damon_dump_output'],
+        stderr=subprocess.DEVNULL)
+
+    if rc != 0:
+        return None, f'drgn fail: return code({rc})'
+    try:
+        with open('damon_dump_output', 'r') as f:
+            return json.load(f), None
+    except Exception as e:
+        return None, 'json.load fail (%s)' % e
+
+def main():
+    kdamonds = _damon_sysfs.Kdamonds(
+        [_damon_sysfs.Kdamond(
+            contexts=[_damon_sysfs.DamonCtx(
+                schemes=[_damon_sysfs.Damos(
+                    ops_filters=[
+                        _damon_sysfs.DamosFilter(
+                            type_='anon',
+                            matching=True,
+                            allow=True,
+                        )
+                    ]
+                )],
+            )])]
+    )

tools/testing/selftests/damon/sysfs_no_op_commit_break.py line 53

Quoted 10 lines
tools/testing/selftests/damon/sysfs_no_op_commit_break.py new file +10 -0
+
+    err = kdamonds.start()
+    if err is not None:
+        print('kdamond start failed: %s' % err)
+        exit(1)
+
+    before_commit_status, err = \
+        dump_damon_status_dict(kdamonds.kdamonds[0].pid)
+    if err is not None:
+        print(err)

Adding more context would be nice, e.g.,

print('before-commit status dump failed: %s' % err)

tools/testing/selftests/damon/sysfs_no_op_commit_break.py line 61

Quoted 8 lines
tools/testing/selftests/damon/sysfs_no_op_commit_break.py new file +8 -0
+        exit(1)
+
+    kdamonds.kdamonds[0].commit()
+
+    after_commit_status, err = \
+        dump_damon_status_dict(kdamonds.kdamonds[0].pid)
+    if err is not None:
+        print(err)

Adding more context would be nice, e.g.,

print('after-commit status dump failed: %s' % err)

Quoted 13 lines
tools/testing/selftests/damon/sysfs_no_op_commit_break.py new file +11 -0
+        exit(1)
+
+    if before_commit_status != after_commit_status:
+        print(f'before: {json.dump(before_commit_status, indent=2)}')
+        print(f'after: {json.dump(after_commit_status, indent=2)}')
+        exit(1)
+
+    kdamonds.stop()
+
+if __name__ == '__main__':
+    main()
Signature · 1 line
2.43.0

Other than above two trivial comments, nothing stands out to me.

Thanks,
SJ

#2 lore raw

Hi SeongJae

Quoted 7 lines · SeongJae Park
Quoted 4 lines · On Sat, 9 Aug 2025 22:07:56 +0900 Sang-Heon Jeon <[email protected]> wrote:

damos_commit_ops_filters() incorrectly uses damos_nth_filter() which iterates core_filters. As a result, performing a commit unintentionally corrupts ops_filters.

Quoted 7 lines
Quoted 4 lines

Add damos_nth_ops_filter() which iterates ops_filters. Use this function to fix issues caused by wrong iteration.

Also, add test to verify that modification is right way.

As I mentioned on v1 thread, please drop the test part, for ease of backporting.

As I mentioned on the v1 thread as well, I'll separate them back.

Quoted 129 lines
Quoted 116 lines
Fixes 3607cc590f18 ("mm/damon/core: support committing ops_filters") # 6.15.x

---
Changes from v1 [1]:
1. Fix code and commit message style.
2. Merge patch set into one patch.
3. Add fixes and cc section for backporting.

[1] https://lore.kernel.org/damon/[email protected]/

---
I tried to fix your all comments, but maybe i miss something. Then please let me know; I'll fix it as soon as possible.

---
 mm/damon/core.c                               | 14 +++-
 tools/testing/selftests/damon/Makefile        |  1 +
 .../damon/sysfs_no_op_commit_break.py         | 72 +++++++++++++++++++
 3 files changed, 86 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/damon/sysfs_no_op_commit_break.py
mm/damon/core.c +13 -1
index 883d791a10e5..19c8f01fc81a 100644
@@ -862,6 +862,18 @@ static struct damos_filter *damos_nth_filter(int n, struct damos *s)
      return NULL;
 }
 
+static struct damos_filter *damos_nth_ops_filter(int n, struct damos *s)
+{
+     struct damos_filter *filter;
+     int i = 0;
+
+     damos_for_each_ops_filter(filter, s) {
+             if (i++ == n)
+                     return filter;
+     }
+     return NULL;
+}
+
 static void damos_commit_filter_arg(
              struct damos_filter *dst, struct damos_filter *src)
 {
@@ -925,7 +937,7 @@ static int damos_commit_ops_filters(struct damos *dst, struct damos *src)
      int i = 0, j = 0;
 
      damos_for_each_ops_filter_safe(dst_filter, next, dst) {
-             src_filter = damos_nth_filter(i++, src);
+             src_filter = damos_nth_ops_filter(i++, src);
              if (src_filter)
                      damos_commit_filter(dst_filter, src_filter);
              else
tools/testing/selftests/damon/Makefile +1 -0
index 5b230deb19e8..44a4a819df55 100644
@@ -17,6 +17,7 @@ TEST_PROGS += reclaim.sh lru_sort.sh
 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
 
 EXTRA_CLEAN = __pycache__
 
tools/testing/selftests/damon/sysfs_no_op_commit_break.py new file +43 -0
new file mode 100755
index 000000000000..fbefb1c83045
@@ -0,0 +1,72 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+
+import json
+import os
+import subprocess
+import sys
+
+import _damon_sysfs
+
+def dump_damon_status_dict(pid):
+    try:
+        subprocess.check_output(['which', 'drgn'], stderr=subprocess.DEVNULL)
+    except:
+        return None, 'drgn not found'
+    file_dir = os.path.dirname(os.path.abspath(__file__))
+    dump_script = os.path.join(file_dir, 'drgn_dump_damon_status.py')
+    rc = subprocess.call(['drgn', dump_script, pid, 'damon_dump_output'],
+        stderr=subprocess.DEVNULL)
+
+    if rc != 0:
+        return None, f'drgn fail: return code({rc})'
+    try:
+        with open('damon_dump_output', 'r') as f:
+            return json.load(f), None
+    except Exception as e:
+        return None, 'json.load fail (%s)' % e
+
+def main():
+    kdamonds = _damon_sysfs.Kdamonds(
+        [_damon_sysfs.Kdamond(
+            contexts=[_damon_sysfs.DamonCtx(
+                schemes=[_damon_sysfs.Damos(
+                    ops_filters=[
+                        _damon_sysfs.DamosFilter(
+                            type_='anon',
+                            matching=True,
+                            allow=True,
+                        )
+                    ]
+                )],
+            )])]
+    )

tools/testing/selftests/damon/sysfs_no_op_commit_break.py line 53

Quoted 10 lines
tools/testing/selftests/damon/sysfs_no_op_commit_break.py new file +10 -0
+
+    err = kdamonds.start()
+    if err is not None:
+        print('kdamond start failed: %s' % err)
+        exit(1)
+
+    before_commit_status, err = \
+        dump_damon_status_dict(kdamonds.kdamonds[0].pid)
+    if err is not None:
+        print(err)

Adding more context would be nice, e.g.,

Quoted 1 line

print('before-commit status dump failed: %s' % err)

Sure, I'll add that to v3 patch.

Quoted 11 lines

tools/testing/selftests/damon/sysfs_no_op_commit_break.py line 61

Quoted 8 lines
tools/testing/selftests/damon/sysfs_no_op_commit_break.py new file +8 -0
+        exit(1)
+
+    kdamonds.kdamonds[0].commit()
+
+    after_commit_status, err = \
+        dump_damon_status_dict(kdamonds.kdamonds[0].pid)
+    if err is not None:
+        print(err)

Adding more context would be nice, e.g.,

Quoted 1 line

print('after-commit status dump failed: %s' % err)

ditto.

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/damon/sysfs_no_op_commit_break.py new file +7 -0
+        exit(1)
+
+    if before_commit_status != after_commit_status:
+        print(f'before: {json.dump(before_commit_status, indent=2)}')
+        print(f'after: {json.dump(after_commit_status, indent=2)}')
+        exit(1)
+
Quoted 9 lines
Quoted 6 lines
tools/testing/selftests/damon/sysfs_no_op_commit_break.py new file +4 -1
+    kdamonds.stop()
+
+if __name__ == '__main__':
+    main()
--

2.43.0

Other than above two trivial comments, nothing stands out to me.

Sounds great!

Quoted 2 lines

Thanks,
SJ

Best Regards.
Sang-Heon Jeon.