Skip to contentSkip to conversation

mm/damon: have damon_get_folio return folio even for tail pages

PATCH Superseded Usama Arif 2 participants 4 messages Jan 20, 2025

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Usama Arif

author
#0 lore raw

PATCH v2 1/2 mm/damon: have damon_get_folio return folio even for tail pages

This effectively adds support for large folios in damon for paddr, as damon_pa_mkold/young won't get a null folio from this function and won't ignore it, hence access will be checked and reported. This also means that larger folios will be considered for different DAMOS actions like pageout, prioritization and migration. As these DAMOS actions will consider larger folios, iterate through the region at folio_size and not PAGE_SIZE intervals. This should not have an affect on vaddr, as damon_young_pmd_entry considers pmd entries.

---
 mm/damon/ops-common.c |  2 +-
 mm/damon/paddr.c      | 24 ++++++++++++++++++------
 2 files changed, 19 insertions(+), 7 deletions(-)
mm/damon/ops-common.c +1 -1
index d25d99cb5f2b..d511be201c4c 100644
@@ -24,7 +24,7 @@ struct folio *damon_get_folio(unsigned long pfn)
 	struct page *page = pfn_to_online_page(pfn);
 	struct folio *folio;
 
-	if (!page || PageTail(page))
+	if (!page)
 		return NULL;
 
 	folio = page_folio(page);
mm/damon/paddr.c +18 -6
index bd8cfe10121b..c0ccf4fade24 100644
@@ -266,11 +266,14 @@ static unsigned long damon_pa_pageout(struct damon_region *r, struct damos *s,
 		damos_add_filter(s, filter);
 	}
 
-	for (addr = r->ar.start; addr < r->ar.end; addr += PAGE_SIZE) {
+	addr = r->ar.start;
+	while (addr < r->ar.end) {
 		struct folio *folio = damon_get_folio(PHYS_PFN(addr));
 
-		if (!folio)
+		if (!folio) {
+			addr += PAGE_SIZE;
 			continue;
+		}
 
 		if (damos_pa_filter_out(s, folio))
 			goto put_folio;
@@ -286,6 +289,7 @@ static unsigned long damon_pa_pageout(struct damon_region *r, struct damos *s,
 		else
 			list_add(&folio->lru, &folio_list);
 put_folio:
+		addr += folio_size(folio);
 		folio_put(folio);
 	}
 	if (install_young_filter)
@@ -301,11 +305,14 @@ static inline unsigned long damon_pa_mark_accessed_or_deactivate(
 {
 	unsigned long addr, applied = 0;
 
-	for (addr = r->ar.start; addr < r->ar.end; addr += PAGE_SIZE) {
+	addr = r->ar.start;
+	while (addr < r->ar.end) {
 		struct folio *folio = damon_get_folio(PHYS_PFN(addr));
 
-		if (!folio)
+		if (!folio) {
+			addr += PAGE_SIZE;
 			continue;
+		}
 
 		if (damos_pa_filter_out(s, folio))
 			goto put_folio;
@@ -318,6 +325,7 @@ static inline unsigned long damon_pa_mark_accessed_or_deactivate(
 			folio_deactivate(folio);
 		applied += folio_nr_pages(folio);
 put_folio:
+		addr += folio_size(folio);
 		folio_put(folio);
 	}
 	return applied * PAGE_SIZE;
@@ -464,11 +472,14 @@ static unsigned long damon_pa_migrate(struct damon_region *r, struct damos *s,
 	unsigned long addr, applied;
 	LIST_HEAD(folio_list);
 
-	for (addr = r->ar.start; addr < r->ar.end; addr += PAGE_SIZE) {
+	addr = r->ar.start;
+	while (addr < r->ar.end) {
 		struct folio *folio = damon_get_folio(PHYS_PFN(addr));
 
-		if (!folio)
+		if (!folio) {
+			addr += PAGE_SIZE;
 			continue;
+		}
 
 		if (damos_pa_filter_out(s, folio))
 			goto put_folio;
@@ -479,6 +490,7 @@ static unsigned long damon_pa_migrate(struct damon_region *r, struct damos *s,
 			goto put_folio;
 		list_add(&folio->lru, &folio_list);
 put_folio:
+		addr += folio_size(folio);
 		folio_put(folio);
 	}
 	applied = damon_pa_migrate_pages(&folio_list, s->target_nid);
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH v2 2/2 mm/damon: introduce DAMOS filter type hugepage

This is to gather statistics to check if memory regions of specific access tempratures are backed by hugepages. This includes both THPs and hugetlbfs.
This filter can help to observe and prove the effectivenes of different schemes for shrinking/collapsing hugepages.

---
v1 -> v2:
- Wrap DAMOS_FILTER_TYPE_HUGEPAGE case with
CONFIG_PGTABLE_HAS_HUGE_LEAVES (SJ Park)

---
 include/linux/damon.h    | 4 ++++
 mm/damon/paddr.c         | 5 +++++
 mm/damon/sysfs-schemes.c | 3 +++
 3 files changed, 12 insertions(+)
include/linux/damon.h +4 -0
index af525252b853..b0dbf380ab76 100644
@@ -326,6 +326,7 @@ struct damos_stat {
  * @DAMOS_FILTER_TYPE_ANON:	Anonymous pages.
  * @DAMOS_FILTER_TYPE_MEMCG:	Specific memcg's pages.
  * @DAMOS_FILTER_TYPE_YOUNG:	Recently accessed pages.
+ * @DAMOS_FILTER_TYPE_HUGEPAGE:	Page is part of a hugepage.
  * @DAMOS_FILTER_TYPE_ADDR:	Address range.
  * @DAMOS_FILTER_TYPE_TARGET:	Data Access Monitoring target.
  * @NR_DAMOS_FILTER_TYPES:	Number of filter types.
@@ -345,6 +346,9 @@ enum damos_filter_type {
 	DAMOS_FILTER_TYPE_ANON,
 	DAMOS_FILTER_TYPE_MEMCG,
 	DAMOS_FILTER_TYPE_YOUNG,
+#if defined(CONFIG_PGTABLE_HAS_HUGE_LEAVES)
+	DAMOS_FILTER_TYPE_HUGEPAGE,
+#endif
 	DAMOS_FILTER_TYPE_ADDR,
 	DAMOS_FILTER_TYPE_TARGET,
 	NR_DAMOS_FILTER_TYPES,
mm/damon/paddr.c +5 -0
index c0ccf4fade24..224308140441 100644
@@ -222,6 +222,11 @@ static bool damos_pa_filter_match(struct damos_filter *filter,
 		if (matched)
 			damon_folio_mkold(folio);
 		break;
+#if defined(CONFIG_PGTABLE_HAS_HUGE_LEAVES)
+	case DAMOS_FILTER_TYPE_HUGEPAGE:
+		matched = folio_size(folio) == HPAGE_PMD_SIZE;
+		break;
+#endif
 	default:
 		break;
 	}
mm/damon/sysfs-schemes.c +3 -0
index 98f93ae9f59e..3eb3ec464591 100644
@@ -329,6 +329,9 @@ static const char * const damon_sysfs_scheme_filter_type_strs[] = {
 	"anon",
 	"memcg",
 	"young",
+#if defined(CONFIG_PGTABLE_HAS_HUGE_LEAVES)
+	"hugepage",
+#endif
 	"addr",
 	"target",
 };
Signature · 1 line
2.43.5
maintainer
#2 lore raw

Re: [PATCH v2 2/2] mm/damon: introduce DAMOS filter type hugepage

Quoted 30 lines · On Mon, 20 Jan 2025 10:16:50 +0000 Usama Arif <[email protected]> wrote:

This is to gather statistics to check if memory regions of specific access tempratures are backed by hugepages. This includes both THPs and hugetlbfs.
This filter can help to observe and prove the effectivenes of different schemes for shrinking/collapsing hugepages.

---
v1 -> v2:
- Wrap DAMOS_FILTER_TYPE_HUGEPAGE case with
CONFIG_PGTABLE_HAS_HUGE_LEAVES (SJ Park)

---
 include/linux/damon.h    | 4 ++++
 mm/damon/paddr.c         | 5 +++++
 mm/damon/sysfs-schemes.c | 3 +++
 3 files changed, 12 insertions(+)
include/linux/damon.h +1 -0
index af525252b853..b0dbf380ab76 100644
@@ -326,6 +326,7 @@ struct damos_stat {
  * @DAMOS_FILTER_TYPE_ANON:	Anonymous pages.
  * @DAMOS_FILTER_TYPE_MEMCG:	Specific memcg's pages.
  * @DAMOS_FILTER_TYPE_YOUNG:	Recently accessed pages.
+ * @DAMOS_FILTER_TYPE_HUGEPAGE:	Page is part of a hugepage.
  * @DAMOS_FILTER_TYPE_ADDR:	Address range.
  * @DAMOS_FILTER_TYPE_TARGET:	Data Access Monitoring target.
  * @NR_DAMOS_FILTER_TYPES:	Number of filter types.

include/linux/damon.h enum damos_filter_type { · line 351

Quoted 6 lines
include/linux/damon.h +3 -0
@@ -345,6 +346,9 @@ enum damos_filter_type {
 	DAMOS_FILTER_TYPE_ANON,
 	DAMOS_FILTER_TYPE_MEMCG,
 	DAMOS_FILTER_TYPE_YOUNG,
+#if defined(CONFIG_PGTABLE_HAS_HUGE_LEAVES)
+	DAMOS_FILTER_TYPE_HUGEPAGE,
+#endif

I'd prefer not enclosing this part with CONFIG_PGTABLE_HAS_HUGE_LEAVES, so that users can simply use it. If the config is not set, damos_pa_filter_match() will just say nothing is hugepage, and hence will work sanely, in my opinion.

Quoted 8 lines
include/linux/damon.h +0 -0
 	DAMOS_FILTER_TYPE_ADDR,
 	DAMOS_FILTER_TYPE_TARGET,
 	NR_DAMOS_FILTER_TYPES,
mm/damon/paddr.c +0 -0
index c0ccf4fade24..224308140441 100644

mm/damon/paddr.c static bool damos_pa_filter_match(struct damos_filter *filter, · line 229

Quoted 8 lines
mm/damon/paddr.c +5 -0
@@ -222,6 +222,11 @@ static bool damos_pa_filter_match(struct damos_filter *filter,
 		if (matched)
 			damon_folio_mkold(folio);
 		break;
+#if defined(CONFIG_PGTABLE_HAS_HUGE_LEAVES)
+	case DAMOS_FILTER_TYPE_HUGEPAGE:
+		matched = folio_size(folio) == HPAGE_PMD_SIZE;
+		break;
+#endif

This part looks good to me.

Quoted 8 lines
mm/damon/paddr.c +0 -0
 	default:
 		break;
 	}
mm/damon/sysfs-schemes.c +0 -0
index 98f93ae9f59e..3eb3ec464591 100644

mm/damon/sysfs-schemes.c static const char * const damon_sysfs_scheme_filter_type_strs[] = { · line 334

Quoted 6 lines
mm/damon/sysfs-schemes.c +3 -0
@@ -329,6 +329,9 @@ static const char * const damon_sysfs_scheme_filter_type_strs[] = {
 	"anon",
 	"memcg",
 	"young",
+#if defined(CONFIG_PGTABLE_HAS_HUGE_LEAVES)
+	"hugepage",
+#endif

Again, I'd prefer not enclosing this part.

Quoted 5 lines
mm/damon/sysfs-schemes.c +0 -0
 	"addr",
 	"target",
 };
Signature · 1 line
2.43.5

Thanks,
SJ

author
#3 lore raw

Re: [PATCH v2 2/2] mm/damon: introduce DAMOS filter type hugepage

Quoted 34 lines · SeongJae Park
Quoted 32 lines · On Mon, 20 Jan 2025 10:16:50 +0000 Usama Arif <[email protected]> wrote:

This is to gather statistics to check if memory regions of specific access tempratures are backed by hugepages. This includes both THPs and hugetlbfs.
This filter can help to observe and prove the effectivenes of different schemes for shrinking/collapsing hugepages.

---
v1 -> v2:
- Wrap DAMOS_FILTER_TYPE_HUGEPAGE case with
CONFIG_PGTABLE_HAS_HUGE_LEAVES (SJ Park)

---
 include/linux/damon.h    | 4 ++++
 mm/damon/paddr.c         | 5 +++++
 mm/damon/sysfs-schemes.c | 3 +++
 3 files changed, 12 insertions(+)
include/linux/damon.h +1 -0
index af525252b853..b0dbf380ab76 100644
@@ -326,6 +326,7 @@ struct damos_stat {
  * @DAMOS_FILTER_TYPE_ANON:	Anonymous pages.
  * @DAMOS_FILTER_TYPE_MEMCG:	Specific memcg's pages.
  * @DAMOS_FILTER_TYPE_YOUNG:	Recently accessed pages.
+ * @DAMOS_FILTER_TYPE_HUGEPAGE:	Page is part of a hugepage.
  * @DAMOS_FILTER_TYPE_ADDR:	Address range.
  * @DAMOS_FILTER_TYPE_TARGET:	Data Access Monitoring target.
  * @NR_DAMOS_FILTER_TYPES:	Number of filter types.
@@ -345,6 +346,9 @@ enum damos_filter_type {
 	DAMOS_FILTER_TYPE_ANON,
 	DAMOS_FILTER_TYPE_MEMCG,
Quoted 9 lines

include/linux/damon.h enum damos_filter_type { · line 351

Quoted 4 lines
include/linux/damon.h +3 -0
 	DAMOS_FILTER_TYPE_YOUNG,
+#if defined(CONFIG_PGTABLE_HAS_HUGE_LEAVES)
+	DAMOS_FILTER_TYPE_HUGEPAGE,
+#endif

I'd prefer not enclosing this part with CONFIG_PGTABLE_HAS_HUGE_LEAVES, so that users can simply use it. If the config is not set, damos_pa_filter_match() will just say nothing is hugepage, and hence will work sanely, in my opinion.

I was going along the path of not even exposing it to the userspace in sysfs, so that the user might not get confused about hugepages. But I agree the alternate is good a well, with just 0 being returned. I will send v3 without it.

Quoted 45 lines
Quoted 8 lines
include/linux/damon.h +0 -0
 	DAMOS_FILTER_TYPE_ADDR,
 	DAMOS_FILTER_TYPE_TARGET,
 	NR_DAMOS_FILTER_TYPES,
mm/damon/paddr.c +0 -0
index c0ccf4fade24..224308140441 100644

mm/damon/paddr.c static bool damos_pa_filter_match(struct damos_filter *filter, · line 229

Quoted 8 lines
mm/damon/paddr.c +5 -0
@@ -222,6 +222,11 @@ static bool damos_pa_filter_match(struct damos_filter *filter,
 		if (matched)
 			damon_folio_mkold(folio);
 		break;
+#if defined(CONFIG_PGTABLE_HAS_HUGE_LEAVES)
+	case DAMOS_FILTER_TYPE_HUGEPAGE:
+		matched = folio_size(folio) == HPAGE_PMD_SIZE;
+		break;
+#endif

This part looks good to me.

Quoted 8 lines
mm/damon/paddr.c +0 -0
 	default:
 		break;
 	}
mm/damon/sysfs-schemes.c +0 -0
index 98f93ae9f59e..3eb3ec464591 100644

mm/damon/sysfs-schemes.c static const char * const damon_sysfs_scheme_filter_type_strs[] = { · line 334

Quoted 6 lines
mm/damon/sysfs-schemes.c +3 -0
@@ -329,6 +329,9 @@ static const char * const damon_sysfs_scheme_filter_type_strs[] = {
 	"anon",
 	"memcg",
 	"young",
+#if defined(CONFIG_PGTABLE_HAS_HUGE_LEAVES)
+	"hugepage",
+#endif

Again, I'd prefer not enclosing this part.

Quoted 5 lines
mm/damon/sysfs-schemes.c +0 -0
 	"addr",
 	"target",
 };
Signature · 1 line
2.43.5

Thanks,
SJ