Skip to contentSkip to conversation

mm/damon: various improvements/cleanups for code, tests and comments

RFC PATCH New SJ Park 2 participants 9 messages Nov 28

Subsystems (3)
lore
Share & tools
lore

SJ Park

author maintainer
#0 lore raw

v4 cover mm/damon: various improvements/cleanups for code, tests and comments

Refactor code, optimize code speed, add missing feature supports, and update comments 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. Update source file intro comments to call DAMON just DAMON, via patch 7.

Issues for patches 1 and 3 were found by Sashiko. Those are mild inefficiency and unsupported features, though. Hence not Cc-ing stable.

Changes from RFC v3
- RFC v3: https://lore.kernel.org/[email protected]
- Avoid NULL dereference when vm_normal_folio() returns NULL, in patch 2.
- Rebase to the latest mm-new.
Changes from RFC v2
- RFC v2: https://lore.kernel.org/[email protected]
- Set last_applied regardless of damos-filtering.
- Advance entire folio for invalid folio.
- update subject for unused imports removal.
- Add source file intro comments update patch.
- Rebase to the latest mm-new.
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 (7):
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 sys and time modules mm/damon: update file intro comments to call DAMON just DAMON

 mm/damon/core.c                               |  2 +-
 mm/damon/ops-common.c                         |  2 +-
 mm/damon/ops-common.h                         |  2 +-
 mm/damon/sysfs-schemes.c                      |  3 ---
 mm/damon/tests/core-kunit.h                   |  2 +-
 mm/damon/tests/paddr-kunit.h                  |  2 +-
 mm/damon/tests/sysfs-kunit.h                  |  2 +-
 mm/damon/tests/vaddr-kunit.h                  |  2 +-
 mm/damon/vaddr.c                              | 27 ++++++++++++-------
 .../selftests/damon/damos_apply_interval.py   |  2 --
 .../damon/sysfs_no_op_commit_break.py         |  1 -
 ...sysfs_update_schemes_tried_regions_hang.py |  1 -
 12 files changed, 25 insertions(+), 23 deletions(-)

base-commit: 23cd682622886800b912219e307cf46a99f69c10

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

RFC PATCH v4 1/7 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 750e643e020f..ca7ce9bd96e7 100644
@@ -883,12 +883,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, s,
 				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
#2 lore raw

RFC PATCH v4 2/7 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, 11 insertions(+), 7 deletions(-)
mm/damon/vaddr.c +11 -7
index ca7ce9bd96e7..fd649cd335c9 100644
@@ -837,6 +837,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)
 {
@@ -856,8 +862,9 @@ 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;
+		s->last_applied = folio;
 		if (damos_va_filter_out(s, folio, walk->vma, addr, NULL, pmd))
 			goto huge_out;
 		damos_va_migrate_dests_add(folio, walk->vma, addr, s,
@@ -885,6 +892,9 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 			continue;
 		page_idx = folio_page_idx(folio, pte_page(ptent));
 		nr = folio_nr_pages(folio) - page_idx;
+		if (damos_va_invalid_folio(folio, s))
+			continue;
+		s->last_applied = folio;
 		if (damos_va_filter_out(s, folio, walk->vma, addr, pte, NULL))
 			continue;
 		damos_va_migrate_dests_add(folio, walk->vma, addr, s,
@@ -993,12 +1003,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
#3 lore raw

RFC PATCH v4 3/7 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 | 5 +++++
 1 file changed, 5 insertions(+)
mm/damon/vaddr.c +5 -0
index fd649cd335c9..170f77f74e84 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;
 };
 
 /*
@@ -849,6 +850,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;
+	unsigned long *sz_filter_passed = priv->sz_filter_passed;
 	struct folio *folio;
 	spinlock_t *ptl;
 	pte_t *start_pte, *pte, ptent;
@@ -867,6 +869,7 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		s->last_applied = folio;
 		if (damos_va_filter_out(s, folio, walk->vma, addr, NULL, pmd))
 			goto huge_out;
+		*sz_filter_passed += folio_size(folio);
 		damos_va_migrate_dests_add(folio, walk->vma, addr, s,
 				migration_lists);
 huge_out:
@@ -897,6 +900,7 @@ static int damos_va_migrate_pmd_entry(pmd_t *pmd, unsigned long addr,
 		s->last_applied = folio;
 		if (damos_va_filter_out(s, folio, walk->vma, addr, pte, NULL))
 			continue;
+		*sz_filter_passed += folio_size(folio);
 		damos_va_migrate_dests_add(folio, walk->vma, addr, s,
 				migration_lists);
 	}
@@ -975,6 +979,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
#4 lore raw

RFC PATCH v4 4/7 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 750f70f0c31b..b0d07994f6c0 100644
@@ -2917,9 +2917,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
#5 lore raw

RFC PATCH v4 5/7 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 0bf7768b2006..0ad110be0fb9 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
#6 lore raw

RFC PATCH v4 6/7 selftests/damon: remove unused imports of sys and time 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 81774ddc55ac..5664f75186ba 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 625761c243b5..326a14f04053 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
author maintainer
#7 lore raw

RFC PATCH v4 7/7 mm/damon: update file intro comments to call DAMON just DAMON

In reply to SJ Park

DAMON source file intro comments call DAMON as Data Access Monitor. Since the introduction of DAMOS, DAMON has been somewhat more than a Data Access Monitor. Update the comments to call it just DAMON. Also some of the comments are not appropriate. Update those together.

---
 mm/damon/core.c              | 2 +-
 mm/damon/ops-common.c        | 2 +-
 mm/damon/ops-common.h        | 2 +-
 mm/damon/tests/core-kunit.h  | 2 +-
 mm/damon/tests/paddr-kunit.h | 2 +-
 mm/damon/tests/sysfs-kunit.h | 2 +-
 mm/damon/tests/vaddr-kunit.h | 2 +-
 7 files changed, 7 insertions(+), 7 deletions(-)
mm/damon/core.c +1 -1
index b791a2621bd6..fa32ebf7ad09 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Data Access Monitor
+ * DAMON Core
  */
 
 #define pr_fmt(fmt) "damon: " fmt
mm/damon/ops-common.c +1 -1
index 2216a0023154..38720794c9fc 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Common Code for Data Access Monitoring
+ * Common Code for DAMON Operation Set
  */
 
 #include <linux/hugetlb.h>
mm/damon/ops-common.h +1 -1
index ee2705825100..aabcf09ff501 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /*
- * Common Code for Data Access Monitoring
+ * Common Code for DAMON Operation Set
  */
 
 #include <linux/damon.h>
mm/damon/tests/core-kunit.h +1 -1
index 4446574a3565..73c3bd6a9612 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /*
- * Data Access Monitor Unit Tests
+ * DAMON Core Unit Tests
  */
 
 #ifdef CONFIG_DAMON_KUNIT_TEST
mm/damon/tests/paddr-kunit.h +1 -1
index 2b11f0e67a12..547e1b3c989f 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /*
- * Data Access Monitor Unit Tests
+ * DAMON Physical Address Space Operation Set Unit Tests
  */
 
 #ifdef CONFIG_DAMON_PADDR_KUNIT_TEST
mm/damon/tests/sysfs-kunit.h +1 -1
index 16373ba751db..c2bba2e01301 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /*
- * Data Access Monitor Unit Tests
+ * DAMON sysfs Interface Unit Tests
  */
 
 #ifdef CONFIG_DAMON_SYSFS_KUNIT_TEST
mm/damon/tests/vaddr-kunit.h +1 -1
index d61b503e319a..979e459327bf 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /*
- * Data Access Monitor Unit Tests
+ * DAMON Virtual Address Space Operation Set Unit Tests
  */
 
 #ifdef CONFIG_DAMON_VADDR_KUNIT_TEST
Signature · 1 line
2.47.3