Skip to contentSkip to conversation

mm/damon: misc improvements/cleanups for core, vaddr, sysfs and tests

RFC PATCH In review SJ Park 2 participants 16 messages Nov 1

Subsystems (3)
lore
Share & tools
lore

SJ Park

author maintainer
#0 lore raw

v2 cover mm/damon: misc improvements/cleanups for core, vaddr, sysfs and tests

Refactor code, optimize code speed, and add missing feature supports on various areas of DAMON.

Optimize the efficiency of vaddr damos_migrate action under filter failures with patch 1. Use damos->last_applied on damos_migrate action in vaddr and support sz_filter_passed for damos_migrate action in vaddr, respectively with patch 2 and 3. Drop a redundant and racy quota goal target value validation in sysfs, in favor of that in the core layer, with patch 4. Drop unused variables and imports from selftests with patch 5 and 6.

Changes from RFC
- RFC: https://lore.kernel.org/[email protected]
- Drop charge_target_from fixup consolidation.
- Add a patch for using last_applied on vaddr migrate action.
- Add missed damos_va_migrate_private->sz_filter_passed setup.
- Fix wrong commit message for unused imports removal.

SJ Park (6):
mm/damon/vaddr: advance migration attempt in folio level for filter
out
mm/damon/vaddr: use damos->last_applied for migrate action mm/damon/vaddr: implement sz_filter_passed for migrate_{hot,cold}
action
mm/damon/sysfs-schemes: drop redundant and racy zero target_value
check
selftests/damon/damos_apply_interval: remove unused variables selftests/damon: remove unused imports of os and sys modules

 mm/damon/sysfs-schemes.c                      |  3 --
 mm/damon/vaddr.c                              | 28 +++++++++++--------
 .../selftests/damon/damos_apply_interval.py   |  2 --
 .../damon/sysfs_no_op_commit_break.py         |  1 -
 ...sysfs_update_schemes_tried_regions_hang.py |  1 -
 5 files changed, 17 insertions(+), 18 deletions(-)

base-commit: 807c0ce92f8424fbb7eb13e7ea48746f68b24dca

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

RFC PATCH v2 1/6 mm/damon/vaddr: advance migration attempt in folio level for filter out

damos_va_migrate_pmd_entry() finds folios to migrate under the given pmd, using a loop for the pmd address range. The loop advances in page size if a given folio is excluded by DAMOS ops filters. DAMOS ops filters work in folio granularity. Hence it is more efficient and makes sense to advance in the folio size.

Advance the loop in folio size for the ops filter out decision. For that, do the folio size based loop advancement offset update before applying DAMOS ops filters.

---
 mm/damon/vaddr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/damon/vaddr.c +2 -2
index b549496ea8e2c..c271b6d63c027 100644
@@ -881,12 +881,12 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		folio = vm_normal_folio(walk->vma, addr, ptent);
 		if (!folio)
 			continue;
+		page_idx = folio_page_idx(folio, pte_page(ptent));
+		nr = folio_nr_pages(folio) - page_idx;
 		if (damos_va_filter_out(s, folio, walk->vma, addr, pte, NULL))
 			continue;
 		damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
 				migration_lists);
-		page_idx = folio_page_idx(folio, pte_page(ptent));
-		nr = folio_nr_pages(folio) - page_idx;
 	}
 	pte_unmap_unlock(start_pte, ptl);
 	return 0;
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

RFC PATCH v2 2/6 mm/damon/vaddr: use damos->last_applied for migrate action

In reply to SJ Park

To avoid applying the same DAMOS action multiple times to folios that are located across adjacent regions, damos->last_applied was introduced. DAMOS_MIGRATE_{HOT,COLD} action support in vaddr is not utilizing it. Use it to avoid unnecessarily applying the same action to the same folio multiple times.

---
 mm/damon/vaddr.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
mm/damon/vaddr.c +10 -8
index c271b6d63c027..b1d7bcf7305bb 100644
@@ -834,6 +834,12 @@ static void damos_va_migrate_dests_add(struct folio *folio,
 	list_add(&folio->lru, &migration_lists[i]);
 }
 
+static inline bool damos_va_invalid_folio(struct folio *folio,
+		struct damos *s)
+{
+	return !folio || folio == s->last_applied;
+}
+
 static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		unsigned long next, struct mm_walk *walk)
 {
@@ -854,10 +860,11 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		if (!pmd_present(pmde))
 			goto huge_out;
 		folio = vm_normal_folio_pmd(walk->vma, addr, pmde);
-		if (!folio)
+		if (damos_va_invalid_folio(folio, s))
 			goto huge_out;
 		if (damos_va_filter_out(s, folio, walk->vma, addr, NULL, pmd))
 			goto huge_out;
+		s->last_applied = folio;
 		damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
 				migration_lists);
 huge_out:
@@ -879,12 +886,13 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		if (pte_none(ptent) || !pte_present(ptent))
 			continue;
 		folio = vm_normal_folio(walk->vma, addr, ptent);
-		if (!folio)
+		if (damos_va_invalid_folio(folio, s))
 			continue;
 		page_idx = folio_page_idx(folio, pte_page(ptent));
 		nr = folio_nr_pages(folio) - page_idx;
 		if (damos_va_filter_out(s, folio, walk->vma, addr, pte, NULL))
 			continue;
+		s->last_applied = folio;
 		damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
 				migration_lists);
 	}
@@ -991,12 +999,6 @@ struct damos_va_stat_private {
 	unsigned long *sz_filter_passed;
 };
 
-static inline bool damos_va_invalid_folio(struct folio *folio,
-		struct damos *s)
-{
-	return !folio || folio == s->last_applied;
-}
-
 static int damos_va_stat_pmd_entry(pmd_t *pmd, unsigned long addr,
 		unsigned long next, struct mm_walk *walk)
 {
Signature · 1 line
2.47.3
author maintainer
#6 lore raw

RFC PATCH v2 3/6 mm/damon/vaddr: implement sz_filter_passed for migrate_{hot,cold} action

In reply to SJ Park

Support of DAMOS ops filter and sz_filter_passed support is up to each operation set. DAMON_VADDR supports both for only damos_stat action. It intentionally does not support those for madvise-based actions because applying filters could make it unnecessarily complicated and expensive. The vaddr implementation of migrate_{hot,cold} action does support DAMOS ops filters but sz_filter_passed. There is no real reason to not support sz_filter_passed. Add the support.

Pass the sz_filter_passed pointer to the filter applying logic and accumulate the number.

---
 mm/damon/vaddr.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
mm/damon/vaddr.c +5 -1
index b1d7bcf7305bb..6fd0e07b60705 100644
@@ -776,6 +776,7 @@ static bool damos_va_filter_out(struct damos *scheme, struct folio *folio,
 struct damos_va_migrate_private {
 	struct list_head *migration_lists;
 	struct damos *scheme;
+	unsigned long *sz_filter_passed;
 };
 
 /*
@@ -846,7 +847,7 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 	struct damos_va_migrate_private *priv = walk->private;
 	struct list_head *migration_lists = priv->migration_lists;
 	struct damos *s = priv->scheme;
-	struct damos_migrate_dests *dests = &s->migrate_dests;
+	unsigned long *sz_filter_passed = priv->sz_filter_passed;
 	struct folio *folio;
 	spinlock_t *ptl;
 	pte_t *start_pte, *pte, ptent;
@@ -865,6 +866,7 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		if (damos_va_filter_out(s, folio, walk->vma, addr, NULL, pmd))
 			goto huge_out;
 		s->last_applied = folio;
+		*sz_filter_passed += folio_size(folio);
 		damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
 				migration_lists);
 huge_out:
@@ -893,6 +895,7 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		if (damos_va_filter_out(s, folio, walk->vma, addr, pte, NULL))
 			continue;
 		s->last_applied = folio;
+		*sz_filter_passed += folio_size(folio);
 		damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
 				migration_lists);
 	}
@@ -971,6 +974,7 @@ static unsigned long damos_va_migrate(struct damon_target *target,
 	priv.migration_lists = kmalloc_objs(*priv.migration_lists, nr_dests);
 	if (!priv.migration_lists)
 		return 0;
+	priv.sz_filter_passed = sz_filter_passed;
 
 	for (int i = 0; i < nr_dests; i++)
 		INIT_LIST_HEAD(&priv.migration_lists[i]);
Signature · 1 line
2.47.3
author maintainer
#9 lore raw

RFC PATCH v2 4/6 mm/damon/sysfs-schemes: drop redundant and racy zero target_value check

In reply to SJ Park

DAMON sysfs interface disallowed zero target_value because it could result in divide-by-zero in damos_quota_store(). Because similar checks were duplicated to other DAMON API callers, a patch series that starts with commit 610c4438fb66 ("mm/damon/core: error damos_commit_quota_goal() for zero target_value") added a same check in the core layer central parameters, and dropped redundant checks in DAMON_LRU_SORT and DAMON_SAMPLIE_MTIER. It forgot to drop that in the DAMON sysfs interface.

Now the zero target_value check in DAMON sysfs interface is only redundant. Further, it has a time-of-check to time-of-use (toctou) race with target_value_store(). It can confuse readers to believe [1] the division-by-zero can still happen. Drop the redundant and racy check.

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

---
 mm/damon/sysfs-schemes.c | 3 ---
 1 file changed, 3 deletions(-)
mm/damon/sysfs-schemes.c +0 -3
index 637f1bd0c950b..38efdc258ef3f 100644
@@ -2898,9 +2898,6 @@ static int damos_sysfs_add_quota_score(
 		struct damos_sysfs_quota_goal *sysfs_goal =
 			sysfs_goals->goals_arr[i];
 
-		if (!sysfs_goal->target_value)
-			continue;
-
 		goal = damos_new_quota_goal(sysfs_goal->metric,
 				sysfs_goal->complement,
 				sysfs_goal->target_value);
Signature · 1 line
2.47.3
author maintainer
#11 lore raw

RFC PATCH v2 5/6 selftests/damon/damos_apply_interval: remove unused variables

In reply to SJ Park

wss_collected and nr_quota_exceeds in damos_apply_interval.py are assigned to but never used. Remove.

The issue is found using flake8.

---
 tools/testing/selftests/damon/damos_apply_interval.py | 2 --
 1 file changed, 2 deletions(-)
tools/testing/selftests/damon/damos_apply_interval.py +0 -2
index 0bf7768b2006a..0ad110be0fb94 100755
@@ -42,8 +42,6 @@ def main():
         print('kdamond start failed: %s' % err)
         exit(1)
 
-    wss_collected = []
-    nr_quota_exceeds = 0
     while proc.poll() == None:
         time.sleep(0.1)
         err = kdamonds.kdamonds[0].update_schemes_stats()
Signature · 1 line
2.47.3
author maintainer
#13 lore raw

RFC PATCH v2 6/6 selftests/damon: remove unused imports of os and sys modules

In reply to SJ Park

sysfs_no_op_commit_break.py imports 'sys' module but never uses it. sysfs_update_schemes_tried_regions_hang imports 'time' module but never uses it. Remove the unnecessary imports.

The issue is found using flake8.

---
 tools/testing/selftests/damon/sysfs_no_op_commit_break.py        | 1 -
 .../selftests/damon/sysfs_update_schemes_tried_regions_hang.py   | 1 -
 2 files changed, 2 deletions(-)
tools/testing/selftests/damon/sysfs_no_op_commit_break.py +0 -1
index 81774ddc55ac4..5664f75186bac 100755
@@ -4,7 +4,6 @@
 import json
 import os
 import subprocess
-import sys
 
 import _damon_sysfs
 
tools/testing/selftests/damon/sysfs_update_schemes_tried_regions_hang.py +0 -1
index 625761c243b58..326a14f040534 100755
@@ -4,7 +4,6 @@
 import os
 import subprocess
 import sys
-import time
 
 sys.path.append(os.path.dirname(os.path.abspath(__file__)))
 import _damon_sysfs
Signature · 1 line
2.47.3
#12 lore raw

Re: [RFC PATCH v2 5/6] selftests/damon/damos_apply_interval: remove unused variables

In reply to SJ Park
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

wss_collected and nr_quota_exceeds in damos_apply_interval.py are assigned to but never used. Remove.

The issue is found using flake8.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#14 lore raw

Re: [RFC PATCH v2 6/6] selftests/damon: remove unused imports of os and sys modules

In reply to SJ Park
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] selftests/damon: commit subject names wrong removed modules --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] selftests/damon: commit subject names wrong removed modules
--

commit dfc99fb0f6f595e8bf824229e67f0b391cab2a8e
Author: SJ Park <[email protected]>

selftests/damon: remove unused imports of os and sys modules

This commit removes unused Python module imports from sysfs_no_op_commit_break.py and sysfs_update_schemes_tried_regions_hang.py in the DAMON selftests.

Quoted 9 lines

selftests/damon: remove unused imports of os and sys modules

sysfs_no_op_commit_break.py imports 'sys' module but never uses it. sysfs_update_schemes_tried_regions_hang imports 'time' module but never uses it. Remove the unnecessary imports.

The issue is found using flake8.

[Severity: Low]
As reported by Sashiko on RFC [1], should the commit subject be updated to refer to the sys and time modules instead of os and sys?

In tools/testing/selftests/damon/sysfs_no_op_commit_break.py, the patch removes import sys while import os remains and is used in dump_damon_status_dict():

file_dir = os.path.dirname(os.path.abspath(__file__)) dump_script = os.path.join(file_dir, 'drgn_dump_damon_status.py')

In tools/testing/selftests/damon/sysfs_update_schemes_tried_regions_hang.py, the patch removes import time while both import os and import sys remain and are used at module scope:

    sys.path.append(os.path.dirname(os.path.abspath(__file__)))

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

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#2 lore raw

Re: [RFC PATCH v2 1/6] mm/damon/vaddr: advance migration attempt in folio level for filter out

In reply to SJ Park
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines

damos_va_migrate_pmd_entry() finds folios to migrate under the given pmd, using a loop for the pmd address range. The loop advances in page size if a given folio is excluded by DAMOS ops filters. DAMOS ops filters work in folio granularity. Hence it is more efficient and makes sense to advance in the folio size.

Quoted 6 lines

Advance the loop in folio size for the ops filter out decision. For that, do the folio size based loop advancement offset update before applying DAMOS ops filters.

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#7 lore raw

Re: [RFC PATCH v2 3/6] mm/damon/vaddr: implement sz_filter_passed for migrate_{hot,cold} action

In reply to SJ Park
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] mm/damon/vaddr: undeclared dests in damos_va_migrate_pmd_entry() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] mm/damon/vaddr: undeclared dests in damos_va_migrate_pmd_entry()
--

commit 7670e03c4d866e7e831b34d6066cefe5b658b73a
Author: SJ Park <[email protected]>

mm/damon/vaddr: implement sz_filter_passed for migrate_{hot,cold} action

This commit adds sz_filter_passed accounting to the DAMON_VADDR migrate_hot and migrate_cold actions by passing the counter through damos_va_migrate_private and accumulating the size of folios that pass ops filters in damos_va_migrate_pmd_entry().

Quoted 4 lines
mm/damon/vaddr.c +0 -0
index b1d7bcf7305bb..6fd0e07b60705 100644

[ ... ]

Quoted 10 lines
mm/damon/vaddr.c +1 -1
@@ -846,7 +847,7 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 	struct damos_va_migrate_private *priv = walk->private;
 	struct list_head *migration_lists = priv->migration_lists;
 	struct damos *s = priv->scheme;
-	struct damos_migrate_dests *dests = &s->migrate_dests;
+	unsigned long *sz_filter_passed = priv->sz_filter_passed;
 	struct folio *folio;
 	spinlock_t *ptl;
 	pte_t *start_pte, *pte, ptent;

mm/damon/vaddr.c static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr, · line 871

Quoted 6 lines
mm/damon/vaddr.c +1 -0
@@ -865,6 +866,7 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		if (damos_va_filter_out(s, folio, walk->vma, addr, NULL, pmd))
 			goto huge_out;
 		s->last_applied = folio;
+		*sz_filter_passed += folio_size(folio);
 		damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
 				migration_lists);

[Severity: Low]
Was the declaration of dests in damos_va_migrate_pmd_entry() removed by accident when adding sz_filter_passed?

Both the transparent hugepage path above and the PTE loop below in damos_va_migrate_pmd_entry() still pass dests to damos_va_migrate_dests_add(), which causes mm/damon/vaddr.c to fail to compile with an undeclared identifier error when CONFIG_DAMON_VADDR is enabled.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#10 lore raw

Re: [RFC PATCH v2 4/6] mm/damon/sysfs-schemes: drop redundant and racy zero target_value check

In reply to SJ Park
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

DAMON sysfs interface disallowed zero target_value because it could result in divide-by-zero in damos_quota_store(). Because similar checks were duplicated to other DAMON API callers, a patch series that starts with commit 610c4438fb66 ("mm/damon/core: error damos_commit_quota_goal() for zero target_value") added a same check in the core layer central parameters, and dropped redundant checks in DAMON_LRU_SORT and DAMON_SAMPLIE_MTIER. It forgot to drop that in the DAMON sysfs interface.

Quoted 8 lines

Now the zero target_value check in DAMON sysfs interface is only redundant. Further, it has a time-of-check to time-of-use (toctou) race with target_value_store(). It can confuse readers to believe [1] the division-by-zero can still happen. Drop the redundant and racy check.

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#4 lore raw

Re: [RFC PATCH v2 2/6] mm/damon/vaddr: use damos->last_applied for migrate action

In reply to SJ Park
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] mm/damon/vaddr: last_applied not updated when damos_va_filter_out() rejects - [Medium]…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] mm/damon/vaddr: last_applied not updated when damos_va_filter_out() rejects
- [Medium] mm/damon/vaddr: stale folio in last_applied after damos_va_migrate()
- [Low] mm/damon/vaddr: single-page PTE step in damos_va_migrate_pmd_entry() --

commit af5ba00f84f8ca12cd15e44abbd02f8e8e38b95b
Author: SJ Park <[email protected]>

mm/damon/vaddr: use damos->last_applied for migrate action

This patch updates damos_va_migrate_pmd_entry() to check and record s->last_applied via damos_va_invalid_folio() so that DAMOS_MIGRATE_HOT and DAMOS_MIGRATE_COLD actions are not applied multiple times to folios spanning adjacent virtual address regions.

Quoted 31 lines
mm/damon/vaddr.c +8 -1
index c271b6d63c027..b1d7bcf7305bb 100644
@@ -834,6 +834,12 @@ static void damos_va_migrate_dests_add(struct folio *folio,
 	list_add(&folio->lru, &migration_lists[i]);
 }
 
+static inline bool damos_va_invalid_folio(struct folio *folio,
+		struct damos *s)
+{
+	return !folio || folio == s->last_applied;
+}
+
 static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		unsigned long next, struct mm_walk *walk)
 {
@@ -854,10 +860,11 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		if (!pmd_present(pmde))
 			goto huge_out;
 		folio = vm_normal_folio_pmd(walk->vma, addr, pmde);
-		if (!folio)
+		if (damos_va_invalid_folio(folio, s))
 			goto huge_out;
 		if (damos_va_filter_out(s, folio, walk->vma, addr, NULL, pmd))
 			goto huge_out;
+		s->last_applied = folio;
 		damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
 				migration_lists);
 huge_out:

mm/damon/vaddr.c static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr, · line 890

Quoted 6 lines
mm/damon/vaddr.c +1 -1
@@ -879,12 +886,13 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		if (pte_none(ptent) || !pte_present(ptent))
 			continue;
 		folio = vm_normal_folio(walk->vma, addr, ptent);
-		if (!folio)
+		if (damos_va_invalid_folio(folio, s))
 			continue;

[Severity: Low]
In damos_va_migrate_pmd_entry(), when damos_va_invalid_folio(folio, s) returns true because folio == s->last_applied for a PTE-mapped large folio at the start of the next region, the loop continues before computing page_idx and nr:

	nr = 1;
	ptent = ptep_get(pte);

	if (pte_none(ptent) || !pte_present(ptent))
		continue;
	folio = vm_normal_folio(walk->vma, addr, ptent);
	if (damos_va_invalid_folio(folio, s))
		continue;
	page_idx = folio_page_idx(folio, pte_page(ptent));
	nr = folio_nr_pages(folio) - page_idx;

Doesn't this leave nr at 1 and step through all remaining tail PTEs of that large folio one page at a time instead of advancing by folio_nr_pages(folio) - page_idx?

mm/damon/vaddr.c static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr, · line 898

Quoted 8 lines
mm/damon/vaddr.c +1 -0
 		page_idx = folio_page_idx(folio, pte_page(ptent));
 		nr = folio_nr_pages(folio) - page_idx;
 		if (damos_va_filter_out(s, folio, walk->vma, addr, pte, NULL))
 			continue;
+		s->last_applied = folio;
 		damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
 				migration_lists);
 	}

[Severity: Medium]
Should s->last_applied be updated even when damos_va_filter_out() rejects the folio in both the PMD and PTE paths of damos_va_migrate_pmd_entry(), matching damos_va_stat_pmd_entry() and damon_pa_migrate()?

When a PMD-mapped THP or PTE-mapped large folio spans two adjacent regions and is rejected by damos_va_filter_out() in the first region, jumping to huge_out or continuing before setting s->last_applied = folio leaves s->last_applied unchanged. When the second region is walked, damos_va_filter_out() is called a second time on the same folio.

If an ops filter such as DAMOS_FILTER_TYPE_YOUNG is used to exclude young folios (for example with DAMOS_MIGRATE_COLD), evaluating damos_va_filter_young_match() in the first region clears the page table young bit and marks the folio idle as a side effect:

mm/damon/vaddr.c:damos_va_filter_young_match() {
	...
	if (young && ptep)
		damon_ptep_mkold(ptep, vma, addr);
	else if (young && pmdp)
		damon_pmdp_mkold(pmdp, vma, addr);
	...
}

Wouldn't re-running damos_va_filter_out() on the same folio in the adjacent region then observe the folio as old, pass the filter, and migrate the young folio that was just rejected in the first region?

[Severity: Medium]
Also, how does s->last_applied deduplicate a folio across adjacent regions once damos_va_migrate() has migrated it in the first region?

In damos_va_migrate(), damon_migrate_pages() runs at the end of each region before the next region is walked:

mm/damon/vaddr.c:damos_va_migrate() {
	...
	damon_va_walk_page_range(mm, r->ar.start, r->ar.end, &walk_ops, &priv);
	mmput(mm);

	for (int i = 0; i < nr_dests; i++) {
		nid = use_target_nid ? s->target_nid : dests->node_id_arr[i];
		applied += damon_migrate_pages(&priv.migration_lists[i], nid);
		cond_resched();
	}
	...
}

When a large folio spanning two adjacent regions is migrated during the first region, migrate_pages() allocates a new destination folio, remaps all PTEs or PMDs across the VMA (including those in the second region) to the new folio, and frees the source folio.

When damos_va_migrate_pmd_entry() then walks the second region, vm_normal_folio() or vm_normal_folio_pmd() returns the newly allocated destination folio while s->last_applied still holds the old source folio pointer. Since the pointers do not match, won't damos_va_invalid_folio() return false in the second region, causing damos_va_filter_out() and damos_va_migrate_dests_add() to be evaluated again on the already-migrated folio?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#5 lore raw

Re: [RFC PATCH v2 2/6] mm/damon/vaddr: use damos->last_applied for migrate action

Quoted 70 lines · On Sun, 01 Nov 2026 17:27:03 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] mm/damon/vaddr: last_applied not updated when damos_va_filter_out() rejects
- [Medium] mm/damon/vaddr: stale folio in last_applied after damos_va_migrate()
- [Low] mm/damon/vaddr: single-page PTE step in damos_va_migrate_pmd_entry() --

commit af5ba00f84f8ca12cd15e44abbd02f8e8e38b95b
Author: SJ Park <[email protected]>

mm/damon/vaddr: use damos->last_applied for migrate action

This patch updates damos_va_migrate_pmd_entry() to check and record s->last_applied via damos_va_invalid_folio() so that DAMOS_MIGRATE_HOT and DAMOS_MIGRATE_COLD actions are not applied multiple times to folios spanning adjacent virtual address regions.

Quoted 31 lines
mm/damon/vaddr.c +8 -1
index c271b6d63c027..b1d7bcf7305bb 100644
@@ -834,6 +834,12 @@ static void damos_va_migrate_dests_add(struct folio *folio,
 	list_add(&folio->lru, &migration_lists[i]);
 }
 
+static inline bool damos_va_invalid_folio(struct folio *folio,
+		struct damos *s)
+{
+	return !folio || folio == s->last_applied;
+}
+
 static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		unsigned long next, struct mm_walk *walk)
 {
@@ -854,10 +860,11 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		if (!pmd_present(pmde))
 			goto huge_out;
 		folio = vm_normal_folio_pmd(walk->vma, addr, pmde);
-		if (!folio)
+		if (damos_va_invalid_folio(folio, s))
 			goto huge_out;
 		if (damos_va_filter_out(s, folio, walk->vma, addr, NULL, pmd))
 			goto huge_out;
+		s->last_applied = folio;
 		damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
 				migration_lists);
 huge_out:

mm/damon/vaddr.c static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr, · line 890

Quoted 6 lines
mm/damon/vaddr.c +1 -1
@@ -879,12 +886,13 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		if (pte_none(ptent) || !pte_present(ptent))
 			continue;
 		folio = vm_normal_folio(walk->vma, addr, ptent);
-		if (!folio)
+		if (damos_va_invalid_folio(folio, s))
 			continue;

[Severity: Low]
In damos_va_migrate_pmd_entry(), when damos_va_invalid_folio(folio, s) returns true because folio == s->last_applied for a PTE-mapped large folio at the start of the next region, the loop continues before computing page_idx and nr:

	nr = 1;
	ptent = ptep_get(pte);

	if (pte_none(ptent) || !pte_present(ptent))
		continue;
	folio = vm_normal_folio(walk->vma, addr, ptent);
	if (damos_va_invalid_folio(folio, s))
		continue;
	page_idx = folio_page_idx(folio, pte_page(ptent));
	nr = folio_nr_pages(folio) - page_idx;
Quoted 3 lines

Doesn't this leave nr at 1 and step through all remaining tail PTEs of that large folio one page at a time instead of advancing by folio_nr_pages(folio) - page_idx?

Good catch. I will setup nr and page_idx before calling damos_va_invalid_folio().

Quoted 35 lines

mm/damon/vaddr.c static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr, · line 898

Quoted 9 lines
mm/damon/vaddr.c +1 -0
 		page_idx = folio_page_idx(folio, pte_page(ptent));
 		nr = folio_nr_pages(folio) - page_idx;
 		if (damos_va_filter_out(s, folio, walk->vma, addr, pte, NULL))
 			continue;
+		s->last_applied = folio;
 		damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
 				migration_lists);
 	}

[Severity: Medium]
Should s->last_applied be updated even when damos_va_filter_out() rejects the folio in both the PMD and PTE paths of damos_va_migrate_pmd_entry(), matching damos_va_stat_pmd_entry() and damon_pa_migrate()?

When a PMD-mapped THP or PTE-mapped large folio spans two adjacent regions and is rejected by damos_va_filter_out() in the first region, jumping to huge_out or continuing before setting s->last_applied = folio leaves s->last_applied unchanged. When the second region is walked, damos_va_filter_out() is called a second time on the same folio.

If an ops filter such as DAMOS_FILTER_TYPE_YOUNG is used to exclude young folios (for example with DAMOS_MIGRATE_COLD), evaluating damos_va_filter_young_match() in the first region clears the page table young bit and marks the folio idle as a side effect:

mm/damon/vaddr.c:damos_va_filter_young_match() {
	...
	if (young && ptep)
		damon_ptep_mkold(ptep, vma, addr);
	else if (young && pmdp)
		damon_pmdp_mkold(pmdp, vma, addr);
	...
}
Quoted 3 lines

Wouldn't re-running damos_va_filter_out() on the same folio in the adjacent region then observe the folio as old, pass the filter, and migrate the young folio that was just rejected in the first region?

Makes sense. I will do so in the next version.

Quoted 26 lines

[Severity: Medium]
Also, how does s->last_applied deduplicate a folio across adjacent regions once damos_va_migrate() has migrated it in the first region?

In damos_va_migrate(), damon_migrate_pages() runs at the end of each region before the next region is walked:

mm/damon/vaddr.c:damos_va_migrate() {
	...
	damon_va_walk_page_range(mm, r->ar.start, r->ar.end, &walk_ops, &priv);
	mmput(mm);

	for (int i = 0; i < nr_dests; i++) {
		nid = use_target_nid ? s->target_nid : dests->node_id_arr[i];
		applied += damon_migrate_pages(&priv.migration_lists[i], nid);
		cond_resched();
	}
	...
}

When a large folio spanning two adjacent regions is migrated during the first region, migrate_pages() allocates a new destination folio, remaps all PTEs or PMDs across the VMA (including those in the second region) to the new folio, and frees the source folio.

Quoted 7 lines

When damos_va_migrate_pmd_entry() then walks the second region, vm_normal_folio() or vm_normal_folio_pmd() returns the newly allocated destination folio while s->last_applied still holds the old source folio pointer. Since the pointers do not match, won't damos_va_invalid_folio() return false in the second region, causing damos_va_filter_out() and damos_va_migrate_dests_add() to be evaluated again on the already-migrated folio?

That's a good room to improve. Not a blocker of this patch in my opinion, though.

Quoted 4 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2

Thanks,
SJ

author maintainer
#8 lore raw

Re: [RFC PATCH v2 3/6] mm/damon/vaddr: implement sz_filter_passed for migrate_{hot,cold} action

In reply to [email protected]
Quoted 40 lines · On Sun, 01 Nov 2026 17:21:39 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] mm/damon/vaddr: undeclared dests in damos_va_migrate_pmd_entry()
--

commit 7670e03c4d866e7e831b34d6066cefe5b658b73a
Author: SJ Park <[email protected]>

mm/damon/vaddr: implement sz_filter_passed for migrate_{hot,cold} action

This commit adds sz_filter_passed accounting to the DAMON_VADDR migrate_hot and migrate_cold actions by passing the counter through damos_va_migrate_private and accumulating the size of folios that pass ops filters in damos_va_migrate_pmd_entry().

Quoted 4 lines
mm/damon/vaddr.c +0 -0
index b1d7bcf7305bb..6fd0e07b60705 100644

[ ... ]

Quoted 10 lines
mm/damon/vaddr.c +1 -1
@@ -846,7 +847,7 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 	struct damos_va_migrate_private *priv = walk->private;
 	struct list_head *migration_lists = priv->migration_lists;
 	struct damos *s = priv->scheme;
-	struct damos_migrate_dests *dests = &s->migrate_dests;
+	unsigned long *sz_filter_passed = priv->sz_filter_passed;
 	struct folio *folio;
 	spinlock_t *ptl;
 	pte_t *start_pte, *pte, ptent;

mm/damon/vaddr.c static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr, · line 871

Quoted 6 lines
mm/damon/vaddr.c +1 -0
@@ -865,6 +866,7 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		if (damos_va_filter_out(s, folio, walk->vma, addr, NULL, pmd))
 			goto huge_out;
 		s->last_applied = folio;
+		*sz_filter_passed += folio_size(folio);
 		damos_va_migrate_dests_add(folio, walk->vma, addr, dests,
 				migration_lists);

[Severity: Low]
Was the declaration of dests in damos_va_migrate_pmd_entry() removed by accident when adding sz_filter_passed?

Quoted 5 lines

Both the transparent hugepage path above and the PTE loop below in damos_va_migrate_pmd_entry() still pass dests to damos_va_migrate_dests_add(), which causes mm/damon/vaddr.c to fail to compile with an undeclared identifier error when CONFIG_DAMON_VADDR is enabled.

Yes, it is a mistake I made while formatting this patch. I will fix it in the next spin.

Quoted 4 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3

Thanks,
SJ

author maintainer
#15 lore raw

Re: [RFC PATCH v2 6/6] selftests/damon: remove unused imports of os and sys modules

In reply to [email protected]
Quoted 19 lines · On Sun, 01 Nov 2026 17:20:33 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] selftests/damon: commit subject names wrong removed modules
--

commit dfc99fb0f6f595e8bf824229e67f0b391cab2a8e
Author: SJ Park <[email protected]>

selftests/damon: remove unused imports of os and sys modules

This commit removes unused Python module imports from sysfs_no_op_commit_break.py and sysfs_update_schemes_tried_regions_hang.py in the DAMON selftests.

Quoted 6 lines

selftests/damon: remove unused imports of os and sys modules

sysfs_no_op_commit_break.py imports 'sys' module but never uses it. sysfs_update_schemes_tried_regions_hang imports 'time' module but never uses it. Remove the unnecessary imports.

Quoted 7 lines
Quoted 3 lines

The issue is found using flake8.

[Severity: Low]
As reported by Sashiko on RFC [1], should the commit subject be updated to refer to the sys and time modules instead of os and sys?

Yes, I forgot updating the subject. I will do in the next spin.

Quoted 8 lines

In tools/testing/selftests/damon/sysfs_no_op_commit_break.py, the patch removes import sys while import os remains and is used in dump_damon_status_dict():

file_dir = os.path.dirname(os.path.abspath(__file__)) dump_script = os.path.join(file_dir, 'drgn_dump_damon_status.py')

Quoted 11 lines

In tools/testing/selftests/damon/sysfs_update_schemes_tried_regions_hang.py, the patch removes import time while both import os and import sys remain and are used at module scope:

    sys.path.append(os.path.dirname(os.path.abspath(__file__)))

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

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6

Thanks,
SJ