Skip to contentSkip to conversation

DAMON: Fix return value from damos_wmark_metric_value

PATCH Superseded Alex Rusuf 3 participants 7 messages Apr 28, 2024

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Alex Rusuf

author
#0 lore raw

PATCH 1/1 DAMON: Fix return value from damos_wmark_metric_value

---
 mm/damon/core.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
mm/damon/core.c +6 -4
index 6d503c1c125e..994ccd272555 100644
@@ -1480,12 +1480,14 @@ static bool kdamond_need_stop(struct damon_ctx *ctx)
 	return true;
 }
 
-static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metric)
+static int damos_wmark_metric_value(enum damos_wmark_metric metric,
+				    unsigned long *metric_value)
 {
 	switch (metric) {
 	case DAMOS_WMARK_FREE_MEM_RATE:
-		return global_zone_page_state(NR_FREE_PAGES) * 1000 /
+		*metric_value = global_zone_page_state(NR_FREE_PAGES) * 1000 /
 		       totalram_pages();
+		return 0;
 	default:
 		break;
 	}
@@ -1500,10 +1502,10 @@ static unsigned long damos_wmark_wait_us(struct damos *scheme)
 {
 	unsigned long metric;
 
-	if (scheme->wmarks.metric == DAMOS_WMARK_NONE)
+	if (scheme->wmarks.metric == DAMOS_WMARK_NONE ||
+	    damos_wmark_metric_value(scheme->wmarks.metric, &metric))
 		return 0;
 
-	metric = damos_wmark_metric_value(scheme->wmarks.metric);
 	/* higher than high watermark or lower than low watermark */
 	if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) {
 		if (scheme->wmarks.activated)
Signature · 1 line
2.42.0
maintainer
#1 lore raw

Hi Alex,

Thank you for this patch! I have a few comments below.

For the consistency, could you please use 'mm/damon/core:' as the patch subject prefix instead of 'DAMON:'?

Quoted 1 line · On Sun, 28 Apr 2024 18:49:33 +0300 Alex Rusuf <[email protected]> wrote:

Could you pleae add more detailed explanation about this patch, including why the return value is wrong?

And, I think we would better to add below tags?

Fixes ee801b7dd782 ("mm/damon/schemes: activate schemes based on a watermarks mechanism")
Quoted 9 lines
---
 mm/damon/core.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
mm/damon/core.c +0 -0
index 6d503c1c125e..994ccd272555 100644

mm/damon/core.c static bool kdamond_need_stop(struct damon_ctx *ctx) · line 1484

Quoted 6 lines
mm/damon/core.c +2 -1
@@ -1480,12 +1480,14 @@ static bool kdamond_need_stop(struct damon_ctx *ctx)
 	return true;
 }
 
-static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metric)
+static int damos_wmark_metric_value(enum damos_wmark_metric metric,
+				    unsigned long *metric_value)

Because the return value is no more the metric value, what about renaming this function to, say, 'damos_get_wmark_metric_value()'?

Quoted 11 lines
mm/damon/core.c +2 -1
 {
 	switch (metric) {
 	case DAMOS_WMARK_FREE_MEM_RATE:
-		return global_zone_page_state(NR_FREE_PAGES) * 1000 /
+		*metric_value = global_zone_page_state(NR_FREE_PAGES) * 1000 /
 		       totalram_pages();
+		return 0;
 	default:
 		break;
 	}

mm/damon/core.c static unsigned long damos_wmark_wait_us(struct damos *scheme) · line 1507

Quoted 7 lines
mm/damon/core.c +2 -1
@@ -1500,10 +1502,10 @@ static unsigned long damos_wmark_wait_us(struct damos *scheme)
 {
 	unsigned long metric;
 
-	if (scheme->wmarks.metric == DAMOS_WMARK_NONE)
+	if (scheme->wmarks.metric == DAMOS_WMARK_NONE ||
+	    damos_wmark_metric_value(scheme->wmarks.metric, &metric))
 		return 0;

DAMOS_WMARK_NONE check can be handled from damos_wmark_metric_value() function. Could you please remove the check here?

Quoted 7 lines
mm/damon/core.c +0 -1
-	metric = damos_wmark_metric_value(scheme->wmarks.metric);
 	/* higher than high watermark or lower than low watermark */
 	if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) {
 		if (scheme->wmarks.activated)
Signature · 1 line
2.42.0

Thanks,
SJ

author
#3 lore raw

PATCH v2 1/1 mm/damon/core: Fix return value from damos_wmark_metric_value

In reply to Alex Rusuf

damos_wmark_metric_value's return value is 'unsigned long', so returning -EINVAL as 'unsigned long' may turn out to be very different from the expected one (using 2's complement) and treat as usual matric's value. So, fix that, checking if returned value is not 0.

Fixes ee801b7dd782 ("mm/damon/schemes: activate schemes based on a watermarks mechanism")
---
 mm/damon/core.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/damon/core.c +8 -4
index 6d503c1c125e..53d33b2fa0b9 100644
@@ -1480,12 +1480,17 @@ static bool kdamond_need_stop(struct damon_ctx *ctx)
 	return true;
 }
 
-static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metric)
+static int damos_get_wmark_metric_value(enum damos_wmark_metric metric,
+					unsigned long *metric_value)
 {
+	if (metric == DAMOS_WMARK_NONE)
+		return -EINVAL;
+
 	switch (metric) {
 	case DAMOS_WMARK_FREE_MEM_RATE:
-		return global_zone_page_state(NR_FREE_PAGES) * 1000 /
+		*metric_value = global_zone_page_state(NR_FREE_PAGES) * 1000 /
 		       totalram_pages();
+		return 0;
 	default:
 		break;
 	}
@@ -1500,10 +1505,9 @@ static unsigned long damos_wmark_wait_us(struct damos *scheme)
 {
 	unsigned long metric;
 
-	if (scheme->wmarks.metric == DAMOS_WMARK_NONE)
+	if (damos_get_wmark_metric_value(scheme->wmarks.metric, &metric))
 		return 0;
 
-	metric = damos_wmark_metric_value(scheme->wmarks.metric);
 	/* higher than high watermark or lower than low watermark */
 	if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) {
 		if (scheme->wmarks.activated)
Signature · 1 line
2.42.0
maintainer
#4 lore raw

Re: [PATCH v2] mm/damon/core: Fix return value from damos_wmark_metric_value

Hi Alex,

Quoted 9 lines · On Sun, 28 Apr 2024 21:41:59 +0300 Alex Rusuf <[email protected]> wrote:

damos_wmark_metric_value's return value is 'unsigned long', so returning -EINVAL as 'unsigned long' may turn out to be very different from the expected one (using 2's complement) and treat as usual matric's value. So, fix that, checking if returned value is not 0.

Fixes ee801b7dd782 ("mm/damon/schemes: activate schemes based on a watermarks mechanism")

---

We usually post new revisions of patches as new thread, instead of reply to previous revision. If you want to kindly letting reviewers understand the revision history of the patch, you could put the summary of the changes in each revisions here[1].

[1] https://www.kernel.org/doc/html/v4.17/process/submitting-patches.html#the-canonical-patch-format

Quoted 8 lines
 mm/damon/core.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/damon/core.c +0 -0
index 6d503c1c125e..53d33b2fa0b9 100644

mm/damon/core.c static bool kdamond_need_stop(struct damon_ctx *ctx) · line 1488

Quoted 10 lines
mm/damon/core.c +5 -1
@@ -1480,12 +1480,17 @@ static bool kdamond_need_stop(struct damon_ctx *ctx)
 	return true;
 }
 
-static unsigned long damos_wmark_metric_value(enum damos_wmark_metric metric)
+static int damos_get_wmark_metric_value(enum damos_wmark_metric metric,
+					unsigned long *metric_value)
 {
+	if (metric == DAMOS_WMARK_NONE)
+		return -EINVAL;
+

Below switch-case will handle this case too. How about removing this if statement?

Quoted 23 lines
mm/damon/core.c +3 -3
 	switch (metric) {
 	case DAMOS_WMARK_FREE_MEM_RATE:
-		return global_zone_page_state(NR_FREE_PAGES) * 1000 /
+		*metric_value = global_zone_page_state(NR_FREE_PAGES) * 1000 /
 		       totalram_pages();
+		return 0;
 	default:
 		break;
 	}
@@ -1500,10 +1505,9 @@ static unsigned long damos_wmark_wait_us(struct damos *scheme)
 {
 	unsigned long metric;
 
-	if (scheme->wmarks.metric == DAMOS_WMARK_NONE)
+	if (damos_get_wmark_metric_value(scheme->wmarks.metric, &metric))
 		return 0;
 
-	metric = damos_wmark_metric_value(scheme->wmarks.metric);
 	/* higher than high watermark or lower than low watermark */
 	if (metric > scheme->wmarks.high || scheme->wmarks.low > metric) {
 		if (scheme->wmarks.activated)
Signature · 1 line
2.42.0

Thanks,
SJ

author
#5 lore raw

Re: [PATCH v2] mm/damon/core: Fix return value from damos_wmark_metric_value

SJ,

thank you again for your comments, I'm not very familiar with sending patches here, so I'll try my best, thank you again!

BR,

Alex

maintainer
#6 lore raw

Re: [PATCH v2] mm/damon/core: Fix return value from damos_wmark_metric_value

Hi Alex,

Quoted 4 lines · On Sun, 28 Apr 2024 22:17:26 +0300 Alex Rusuf <[email protected]> wrote:

SJ,

thank you again for your comments, I'm not very familiar with sending patches here, so I'll try my best, thank you again!

No problem at all. Nobody is familiar with the process from the beginning, and you're doing great. I'm happy to help you. :)

I'll also try my best for making the process easier.

Thanks,
SJ

Quoted 4 lines

BR,

Alex