Skip to contentSkip to conversation

mm/damon: increment pa_stat damon address range by folio size

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

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Usama Arif

author
#0 lore raw

PATCH 1/1 mm/damon: increment pa_stat damon address range by folio size

This is to take into account for folios with size > 1 page. Iterating at PAGE_SIZE increment would increment sz_filter_passed multiple times for the same folio by folio_size, providing incorrect stats. Hence go through the folio only once.

Fixes 6347f3385dd0 ("mm/damon/paddr: report filter-passed bytes back for DAMOS_STAT action")
---
 mm/damon/paddr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/damon/paddr.c +3 -1
index 6b4397de4199..cc789a97c6f5 100644
@@ -504,7 +504,8 @@ static unsigned long damon_pa_stat(struct damon_region *r, struct damos *s,
 	if (!damon_pa_scheme_has_filter(s))
 		return 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)
@@ -515,6 +516,7 @@ static unsigned long damon_pa_stat(struct damon_region *r, struct damos *s,
 		else
 			*sz_filter_passed += folio_size(folio);
 put_folio:
+		addr += folio_size(folio);
 		folio_put(folio);
 	}
 	return 0;
Signature · 1 line
2.43.5
maintainer
#1 lore raw

Hi Usama,

Let's use "mm/damon/paddr: " prefix for the patch title, to be more consistent with others.

Quoted 4 lines · On Mon, 13 Jan 2025 18:03:40 +0000 Usama Arif <[email protected]> wrote:

This is to take into account for folios with size > 1 page. Iterating at PAGE_SIZE increment would increment sz_filter_passed multiple times for the same folio by folio_size, providing incorrect stats.

damon_get_folio() returns NULL if the page is a tail page. Hence I think it will not increment sz_filter_passed multiple times?

Quoted 1 line

Hence go through the folio only once.

I tink this is still nice to do, for more efficiency. Can you post v2 of this patch after updating the commit message and addressing below comments?

Quoted 12 lines
Fixes 6347f3385dd0 ("mm/damon/paddr: report filter-passed bytes back for DAMOS_STAT action")
---
 mm/damon/paddr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/damon/paddr.c +0 -0
index 6b4397de4199..cc789a97c6f5 100644

mm/damon/paddr.c static unsigned long damon_pa_stat(struct damon_region *r, struct damos *s, · line 511

Quoted 9 lines
mm/damon/paddr.c +2 -1
@@ -504,7 +504,8 @@ static unsigned long damon_pa_stat(struct damon_region *r, struct damos *s,
 	if (!damon_pa_scheme_has_filter(s))
 		return 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)

In this case, the code does "continue". 'addr' is not advanced, so it will result in an infinite loop. Let's do 'addr += PAGE_SIZE' here, to avoid that.

Quoted 10 lines
mm/damon/paddr.c +1 -0
@@ -515,6 +516,7 @@ static unsigned long damon_pa_stat(struct damon_region *r, struct damos *s,
 		else
 			*sz_filter_passed += folio_size(folio);
 put_folio:
+		addr += folio_size(folio);
 		folio_put(folio);
 	}
 	return 0;
Signature · 1 line
2.43.5

Thanks,
SJ

author
#2 lore raw
Quoted 8 lines · SeongJae Park

Hi Usama,

Let's use "mm/damon/paddr: " prefix for the patch title, to be more consistent with others.

On Mon, 13 Jan 2025 18:03:40 +0000 Usama Arif <[email protected]> wrote:

Quoted 7 lines
Quoted 4 lines

This is to take into account for folios with size > 1 page. Iterating at PAGE_SIZE increment would increment sz_filter_passed multiple times for the same folio by folio_size, providing incorrect stats.

damon_get_folio() returns NULL if the page is a tail page. Hence I think it will not increment sz_filter_passed multiple times?

ahh I didn't look at the definition of damon_get_folio! just assumed it will get the folio irrespective of if its a tail page or not. Will change the commit message.

Just curious if returning NULL is what is expected by the user?
I see damon_get_folio used in a bunch a places. If the user limits damos action/ damon monitoring to a specific address range, and that covers some of the tail pages, but not the head page, I guess the damos action wont be applied.

Quoted 26 lines
Quoted 2 lines

Hence go through the folio only once.

I tink this is still nice to do, for more efficiency. Can you post v2 of this patch after updating the commit message and addressing below comments?

Quoted 20 lines
Fixes 6347f3385dd0 ("mm/damon/paddr: report filter-passed bytes back for DAMOS_STAT action")
---
 mm/damon/paddr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/damon/paddr.c +2 -1
index 6b4397de4199..cc789a97c6f5 100644
@@ -504,7 +504,8 @@ static unsigned long damon_pa_stat(struct damon_region *r, struct damos *s,
 	if (!damon_pa_scheme_has_filter(s))
 		return 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));
 
Quoted 4 lines

mm/damon/paddr.c static unsigned long damon_pa_stat(struct damon_region *r, struct damos *s, · line 511

Quoted 1 line
mm/damon/paddr.c +0 -0
 		if (!folio)

In this case, the code does "continue". 'addr' is not advanced, so it will result in an infinite loop. Let's do 'addr += PAGE_SIZE' here, to avoid that.

Thanks! Will fix this in v2.

Quoted 15 lines
Quoted 11 lines
mm/damon/paddr.c +1 -0
@@ -515,6 +516,7 @@ static unsigned long damon_pa_stat(struct damon_region *r, struct damos *s,
 		else
 			*sz_filter_passed += folio_size(folio);
 put_folio:
+		addr += folio_size(folio);
 		folio_put(folio);
 	}
 	return 0;
Signature · 1 line
2.43.5

Thanks,
SJ

maintainer
#3 lore raw
Quoted 19 lines · On Mon, 13 Jan 2025 18:53:32 +0000 Usama Arif <[email protected]> wrote:
Quoted 8 lines · SeongJae Park

Hi Usama,

Let's use "mm/damon/paddr: " prefix for the patch title, to be more consistent with others.

On Mon, 13 Jan 2025 18:03:40 +0000 Usama Arif <[email protected]> wrote:

Quoted 7 lines
Quoted 4 lines

This is to take into account for folios with size > 1 page. Iterating at PAGE_SIZE increment would increment sz_filter_passed multiple times for the same folio by folio_size, providing incorrect stats.

damon_get_folio() returns NULL if the page is a tail page. Hence I think it will not increment sz_filter_passed multiple times?

Quoted 7 lines

ahh I didn't look at the definition of damon_get_folio! just assumed it will get the folio irrespective of if its a tail page or not. Will change the commit message.

Just curious if returning NULL is what is expected by the user?
I see damon_get_folio used in a bunch a places. If the user limits damos action/ damon monitoring to a specific address range, and that covers some of the tail pages, but not the head page, I guess the damos action wont be applied.

Yes, I think damon_get_folio() users (mostly DAMON operations set layer) should aware that and use different functions if it can be a problem, depending on each case. I find no problematic use cases of the function off the top of my head, but if you find, please help us fixing those :)

[...]

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
mm/damon/paddr.c +2 -1
index 6b4397de4199..cc789a97c6f5 100644
@@ -504,7 +504,8 @@ static unsigned long damon_pa_stat(struct damon_region *r, struct damos *s,
 	if (!damon_pa_scheme_has_filter(s))
 		return 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));
 
Quoted 6 lines
Quoted 4 lines

mm/damon/paddr.c static unsigned long damon_pa_stat(struct damon_region *r, struct damos *s, · line 511

Quoted 1 line
mm/damon/paddr.c +0 -0
 		if (!folio)

In this case, the code does "continue". 'addr' is not advanced, so it will result in an infinite loop. Let's do 'addr += PAGE_SIZE' here, to avoid that.

Thanks! Will fix this in v2.

Nice, looking forward to it!

Thanks,
SJ

[...]