Skip to contentSkip to conversation

mm/damon: deduplicate damon_{reclaim,lru_sort}_apply_parameters()

PATCH In review [email protected] 2 participants 4 messages Sep 20, 2022 – Sep 21, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/damon: deduplicate damon_{reclaim,lru_sort}_apply_parameters()

From: Kaixu Xia <[email protected]>

The bodies of damon_{reclaim,lru_sort}_apply_parameters() contain duplicates. This commit add a common function damon_set_region_biggest_system_ram_default() to removes the duplicates.

---
v2:
-change the common function name and add kernel comment.

 include/linux/damon.h |  3 ++-
 mm/damon/core.c       | 35 ++++++++++++++++++++++++++++++++++-
 mm/damon/lru_sort.c   | 13 +++----------
 mm/damon/reclaim.c    | 13 +++----------
 4 files changed, 42 insertions(+), 22 deletions(-)
include/linux/damon.h +2 -1
index e7808a84675f..ed5470f50bab 100644
@@ -557,7 +557,8 @@ static inline bool damon_target_has_pid(const struct damon_ctx *ctx)
 int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive);
 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
 
-bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end);
+int damon_set_region_biggest_system_ram_default(struct damon_target *t,
+				unsigned long *start, unsigned long *end);
 
 #endif	/* CONFIG_DAMON */
 
mm/damon/core.c +34 -1
index 9c80c6eb00c2..9e96cb9fb5db 100644
@@ -1245,7 +1245,8 @@ static int walk_system_ram(struct resource *res, void *arg)
  * Find biggest 'System RAM' resource and store its start and end address in
  * @start and @end, respectively.  If no System RAM is found, returns false.
  */
-bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end)
+static bool damon_find_biggest_system_ram(unsigned long *start,
+						unsigned long *end)
 
 {
 	struct damon_system_ram_region arg = {};
@@ -1259,6 +1260,38 @@ bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end)
 	return true;
 }
 
+/*
+ * damon_set_region_biggest_system_ram_default() - Set the region of the given
+ * monitoring target as requested, or biggest 'System RAM'.
+ * @t:		The monitoring target to set the region.
+ * @start:	The pointer to the start address of the region.
+ * @end:	The pointer to the end address of the region.
+ *
+ * This function sets the region of @t as requested by @start and @end.  If the
+ * values of the two pointers are pointing to are zero, however, this function
+ * finds the biggest 'System RAM' resource and set the region to cover the
+ * resource.  In the latter case, this function saves the start and end address
+ * of the resource in @start and @end, respectively.
+ *
+ * Return: 0 on success, negative error code otherwise.
+ */
+int damon_set_region_biggest_system_ram_default(struct damon_target *t,
+			unsigned long *start, unsigned long *end)
+{
+	struct damon_addr_range addr_range;
+
+	if (*start > *end)
+		return -EINVAL;
+
+	if (!*start && !*end &&
+		!damon_find_biggest_system_ram(start, end))
+		return -EINVAL;
+
+	addr_range.start = *start;
+	addr_range.end = *end;
+	return damon_set_regions(t, &addr_range, 1);
+}
+
 static int __init damon_init(void)
 {
 	damon_region_cache = KMEM_CACHE(damon_region, 0);
mm/damon/lru_sort.c +3 -10
index d7eb72b41cb6..efbc2bda8b9c 100644
@@ -188,7 +188,6 @@ static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres)
 static int damon_lru_sort_apply_parameters(void)
 {
 	struct damos *scheme;
-	struct damon_addr_range addr_range;
 	unsigned int hot_thres, cold_thres;
 	int err = 0;
 
@@ -211,15 +210,9 @@ static int damon_lru_sort_apply_parameters(void)
 		return -ENOMEM;
 	damon_add_scheme(ctx, scheme);
 
-	if (monitor_region_start > monitor_region_end)
-		return -EINVAL;
-	if (!monitor_region_start && !monitor_region_end &&
-	    !damon_find_biggest_system_ram(&monitor_region_start,
-					   &monitor_region_end))
-		return -EINVAL;
-	addr_range.start = monitor_region_start;
-	addr_range.end = monitor_region_end;
-	return damon_set_regions(target, &addr_range, 1);
+	return damon_set_region_biggest_system_ram_default(target,
+					&monitor_region_start,
+					&monitor_region_end);
 }
 
 static int damon_lru_sort_turn(bool on)
mm/damon/reclaim.c +3 -10
index 3d59ab11b7b3..162c9b1ca00f 100644
@@ -144,7 +144,6 @@ static struct damos *damon_reclaim_new_scheme(void)
 static int damon_reclaim_apply_parameters(void)
 {
 	struct damos *scheme;
-	struct damon_addr_range addr_range;
 	int err = 0;
 
 	err = damon_set_attrs(ctx, &damon_reclaim_mon_attrs);
@@ -157,15 +156,9 @@ static int damon_reclaim_apply_parameters(void)
 		return -ENOMEM;
 	damon_set_schemes(ctx, &scheme, 1);
 
-	if (monitor_region_start > monitor_region_end)
-		return -EINVAL;
-	if (!monitor_region_start && !monitor_region_end &&
-	    !damon_find_biggest_system_ram(&monitor_region_start,
-					   &monitor_region_end))
-		return -EINVAL;
-	addr_range.start = monitor_region_start;
-	addr_range.end = monitor_region_end;
-	return damon_set_regions(target, &addr_range, 1);
+	return damon_set_region_biggest_system_ram_default(target,
+					&monitor_region_start,
+					&monitor_region_end);
 }
 
 static int damon_reclaim_turn(bool on)
Signature · 1 line
2.27.0
maintainer Reviewed-by on v2 1/1
#1 lore raw

Hi Kaixu,

Quoted 4 lines · On Tue, 20 Sep 2022 11:44:21 +0800 [email protected] wrote:

From: Kaixu Xia <[email protected]>

The bodies of damon_{reclaim,lru_sort}_apply_parameters() contain duplicates. This commit add a common function

s/add/adds/

Quoted 1 line

damon_set_region_biggest_system_ram_default() to removes the

s/removes/remove/

Quoted 7 lines

duplicates.

---
v2:
-change the common function name and add kernel comment.

Adding a link to the previous version of the patch here would be very helpful for me. Could you please do so from next time?

Quoted 39 lines
 include/linux/damon.h |  3 ++-
 mm/damon/core.c       | 35 ++++++++++++++++++++++++++++++++++-
 mm/damon/lru_sort.c   | 13 +++----------
 mm/damon/reclaim.c    | 13 +++----------
 4 files changed, 42 insertions(+), 22 deletions(-)
include/linux/damon.h +2 -1
index e7808a84675f..ed5470f50bab 100644
@@ -557,7 +557,8 @@ static inline bool damon_target_has_pid(const struct damon_ctx *ctx)
 int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive);
 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
 
-bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end);
+int damon_set_region_biggest_system_ram_default(struct damon_target *t,
+				unsigned long *start, unsigned long *end);
 
 #endif	/* CONFIG_DAMON */
 
mm/damon/core.c +2 -1
index 9c80c6eb00c2..9e96cb9fb5db 100644
@@ -1245,7 +1245,8 @@ static int walk_system_ram(struct resource *res, void *arg)
  * Find biggest 'System RAM' resource and store its start and end address in
  * @start and @end, respectively.  If no System RAM is found, returns false.
  */
-bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end)
+static bool damon_find_biggest_system_ram(unsigned long *start,
+						unsigned long *end)
 
 {
 	struct damon_system_ram_region arg = {};
@@ -1259,6 +1260,38 @@ bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end)
 	return true;
 }
 

mm/damon/core.c bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end) · line 1272

Quoted 10 lines
mm/damon/core.c +10 -0
+/*
+ * damon_set_region_biggest_system_ram_default() - Set the region of the given
+ * monitoring target as requested, or biggest 'System RAM'.
+ * @t:		The monitoring target to set the region.
+ * @start:	The pointer to the start address of the region.
+ * @end:	The pointer to the end address of the region.
+ *
+ * This function sets the region of @t as requested by @start and @end.  If the
+ * values of the two pointers are pointing to are zero, however, this function
+ * finds the biggest 'System RAM' resource and set the region to cover the

s/set/sets/

mm/damon/core.c bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end) · line 1273

Quoted 1 line
mm/damon/core.c +1 -0
+ * resource.  In the latter case, this function saves the start and end address

s/address/addresses/

Quoted 88 lines
mm/damon/core.c +21 -0
+ * of the resource in @start and @end, respectively.
+ *
+ * Return: 0 on success, negative error code otherwise.
+ */
+int damon_set_region_biggest_system_ram_default(struct damon_target *t,
+			unsigned long *start, unsigned long *end)
+{
+	struct damon_addr_range addr_range;
+
+	if (*start > *end)
+		return -EINVAL;
+
+	if (!*start && !*end &&
+		!damon_find_biggest_system_ram(start, end))
+		return -EINVAL;
+
+	addr_range.start = *start;
+	addr_range.end = *end;
+	return damon_set_regions(t, &addr_range, 1);
+}
+
 static int __init damon_init(void)
 {
 	damon_region_cache = KMEM_CACHE(damon_region, 0);
mm/damon/lru_sort.c +3 -10
index d7eb72b41cb6..efbc2bda8b9c 100644
@@ -188,7 +188,6 @@ static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres)
 static int damon_lru_sort_apply_parameters(void)
 {
 	struct damos *scheme;
-	struct damon_addr_range addr_range;
 	unsigned int hot_thres, cold_thres;
 	int err = 0;
 
@@ -211,15 +210,9 @@ static int damon_lru_sort_apply_parameters(void)
 		return -ENOMEM;
 	damon_add_scheme(ctx, scheme);
 
-	if (monitor_region_start > monitor_region_end)
-		return -EINVAL;
-	if (!monitor_region_start && !monitor_region_end &&
-	    !damon_find_biggest_system_ram(&monitor_region_start,
-					   &monitor_region_end))
-		return -EINVAL;
-	addr_range.start = monitor_region_start;
-	addr_range.end = monitor_region_end;
-	return damon_set_regions(target, &addr_range, 1);
+	return damon_set_region_biggest_system_ram_default(target,
+					&monitor_region_start,
+					&monitor_region_end);
 }
 
 static int damon_lru_sort_turn(bool on)
mm/damon/reclaim.c +3 -10
index 3d59ab11b7b3..162c9b1ca00f 100644
@@ -144,7 +144,6 @@ static struct damos *damon_reclaim_new_scheme(void)
 static int damon_reclaim_apply_parameters(void)
 {
 	struct damos *scheme;
-	struct damon_addr_range addr_range;
 	int err = 0;
 
 	err = damon_set_attrs(ctx, &damon_reclaim_mon_attrs);
@@ -157,15 +156,9 @@ static int damon_reclaim_apply_parameters(void)
 		return -ENOMEM;
 	damon_set_schemes(ctx, &scheme, 1);
 
-	if (monitor_region_start > monitor_region_end)
-		return -EINVAL;
-	if (!monitor_region_start && !monitor_region_end &&
-	    !damon_find_biggest_system_ram(&monitor_region_start,
-					   &monitor_region_end))
-		return -EINVAL;
-	addr_range.start = monitor_region_start;
-	addr_range.end = monitor_region_end;
-	return damon_set_regions(target, &addr_range, 1);
+	return damon_set_region_biggest_system_ram_default(target,
+					&monitor_region_start,
+					&monitor_region_end);
 }
 
 static int damon_reclaim_turn(bool on)
Signature · 1 line
2.27.0

Other than above mean and trivial comments,

As the comments are only for trivial grammar things, I will revise and post one on my own.

Thanks,
SJ

author
#2 lore raw
Quoted 174 lines · SeongJae Park

Hi Kaixu,

Quoted 4 lines · On Tue, 20 Sep 2022 11:44:21 +0800 [email protected] wrote:

From: Kaixu Xia <[email protected]>

The bodies of damon_{reclaim,lru_sort}_apply_parameters() contain duplicates. This commit add a common function

s/add/adds/

Quoted 1 line

damon_set_region_biggest_system_ram_default() to removes the

s/removes/remove/

Quoted 7 lines

duplicates.

---
v2:
-change the common function name and add kernel comment.

Adding a link to the previous version of the patch here would be very helpful for me. Could you please do so from next time?

Quoted 39 lines
 include/linux/damon.h |  3 ++-
 mm/damon/core.c       | 35 ++++++++++++++++++++++++++++++++++-
 mm/damon/lru_sort.c   | 13 +++----------
 mm/damon/reclaim.c    | 13 +++----------
 4 files changed, 42 insertions(+), 22 deletions(-)
include/linux/damon.h +2 -1
index e7808a84675f..ed5470f50bab 100644
@@ -557,7 +557,8 @@ static inline bool damon_target_has_pid(const struct damon_ctx *ctx)
 int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive);
 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
 
-bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end);
+int damon_set_region_biggest_system_ram_default(struct damon_target *t,
+                             unsigned long *start, unsigned long *end);
 
 #endif       /* CONFIG_DAMON */
 
mm/damon/core.c +2 -1
index 9c80c6eb00c2..9e96cb9fb5db 100644
@@ -1245,7 +1245,8 @@ static int walk_system_ram(struct resource *res, void *arg)
  * Find biggest 'System RAM' resource and store its start and end address in
  * @start and @end, respectively.  If no System RAM is found, returns false.
  */
-bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end)
+static bool damon_find_biggest_system_ram(unsigned long *start,
+                                             unsigned long *end)
 
 {
      struct damon_system_ram_region arg = {};
@@ -1259,6 +1260,38 @@ bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end)
      return true;
 }
 

mm/damon/core.c bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end) · line 1272

Quoted 10 lines
mm/damon/core.c +10 -0
+/*
+ * damon_set_region_biggest_system_ram_default() - Set the region of the given
+ * monitoring target as requested, or biggest 'System RAM'.
+ * @t:               The monitoring target to set the region.
+ * @start:   The pointer to the start address of the region.
+ * @end:     The pointer to the end address of the region.
+ *
+ * This function sets the region of @t as requested by @start and @end.  If the
+ * values of the two pointers are pointing to are zero, however, this function
+ * finds the biggest 'System RAM' resource and set the region to cover the

s/set/sets/

mm/damon/core.c bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end) · line 1273

Quoted 1 line
mm/damon/core.c +1 -0
+ * resource.  In the latter case, this function saves the start and end address

s/address/addresses/

Quoted 87 lines
mm/damon/core.c +21 -0
+ * of the resource in @start and @end, respectively.
+ *
+ * Return: 0 on success, negative error code otherwise.
+ */
+int damon_set_region_biggest_system_ram_default(struct damon_target *t,
+                     unsigned long *start, unsigned long *end)
+{
+     struct damon_addr_range addr_range;
+
+     if (*start > *end)
+             return -EINVAL;
+
+     if (!*start && !*end &&
+             !damon_find_biggest_system_ram(start, end))
+             return -EINVAL;
+
+     addr_range.start = *start;
+     addr_range.end = *end;
+     return damon_set_regions(t, &addr_range, 1);
+}
+
 static int __init damon_init(void)
 {
      damon_region_cache = KMEM_CACHE(damon_region, 0);
mm/damon/lru_sort.c +3 -10
index d7eb72b41cb6..efbc2bda8b9c 100644
@@ -188,7 +188,6 @@ static struct damos *damon_lru_sort_new_cold_scheme(unsigned int cold_thres)
 static int damon_lru_sort_apply_parameters(void)
 {
      struct damos *scheme;
-     struct damon_addr_range addr_range;
      unsigned int hot_thres, cold_thres;
      int err = 0;
 
@@ -211,15 +210,9 @@ static int damon_lru_sort_apply_parameters(void)
              return -ENOMEM;
      damon_add_scheme(ctx, scheme);
 
-     if (monitor_region_start > monitor_region_end)
-             return -EINVAL;
-     if (!monitor_region_start && !monitor_region_end &&
-         !damon_find_biggest_system_ram(&monitor_region_start,
-                                        &monitor_region_end))
-             return -EINVAL;
-     addr_range.start = monitor_region_start;
-     addr_range.end = monitor_region_end;
-     return damon_set_regions(target, &addr_range, 1);
+     return damon_set_region_biggest_system_ram_default(target,
+                                     &monitor_region_start,
+                                     &monitor_region_end);
 }
 
 static int damon_lru_sort_turn(bool on)
mm/damon/reclaim.c +3 -11
index 3d59ab11b7b3..162c9b1ca00f 100644
@@ -144,7 +144,6 @@ static struct damos *damon_reclaim_new_scheme(void)
 static int damon_reclaim_apply_parameters(void)
 {
      struct damos *scheme;
-     struct damon_addr_range addr_range;
      int err = 0;
 
      err = damon_set_attrs(ctx, &damon_reclaim_mon_attrs);
@@ -157,15 +156,9 @@ static int damon_reclaim_apply_parameters(void)
              return -ENOMEM;
      damon_set_schemes(ctx, &scheme, 1);
 
-     if (monitor_region_start > monitor_region_end)
-             return -EINVAL;
-     if (!monitor_region_start && !monitor_region_end &&
-         !damon_find_biggest_system_ram(&monitor_region_start,
-                                        &monitor_region_end))
-             return -EINVAL;
-     addr_range.start = monitor_region_start;
-     addr_range.end = monitor_region_end;
-     return damon_set_regions(target, &addr_range, 1);
+     return damon_set_region_biggest_system_ram_default(target,
+                                     &monitor_region_start,
+                                     &monitor_region_end);
 }
 
 static int damon_reclaim_turn(bool on)
--
Quoted 1 line

2.27.0

Other than above mean and trivial comments,

Quoted 5 lines

As the comments are only for trivial grammar things, I will revise and post one on my own.

Thanks for your detailed comments!

Thanks,
Kaixu

Quoted 3 lines

Thanks,
SJ

maintainer
#3 lore raw
In reply to [email protected]
Quoted 48 lines · On Tue, 20 Sep 2022 11:44:21 +0800 [email protected] wrote:

From: Kaixu Xia <[email protected]>

The bodies of damon_{reclaim,lru_sort}_apply_parameters() contain duplicates. This commit add a common function damon_set_region_biggest_system_ram_default() to removes the duplicates.

---
v2:
-change the common function name and add kernel comment.

 include/linux/damon.h |  3 ++-
 mm/damon/core.c       | 35 ++++++++++++++++++++++++++++++++++-
 mm/damon/lru_sort.c   | 13 +++----------
 mm/damon/reclaim.c    | 13 +++----------
 4 files changed, 42 insertions(+), 22 deletions(-)
include/linux/damon.h +2 -1
index e7808a84675f..ed5470f50bab 100644
@@ -557,7 +557,8 @@ static inline bool damon_target_has_pid(const struct damon_ctx *ctx)
 int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive);
 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
 
-bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end);
+int damon_set_region_biggest_system_ram_default(struct damon_target *t,
+				unsigned long *start, unsigned long *end);
 
 #endif	/* CONFIG_DAMON */
 
mm/damon/core.c +2 -1
index 9c80c6eb00c2..9e96cb9fb5db 100644
@@ -1245,7 +1245,8 @@ static int walk_system_ram(struct resource *res, void *arg)
  * Find biggest 'System RAM' resource and store its start and end address in
  * @start and @end, respectively.  If no System RAM is found, returns false.
  */
-bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end)
+static bool damon_find_biggest_system_ram(unsigned long *start,
+						unsigned long *end)
 
 {
 	struct damon_system_ram_region arg = {};

mm/damon/core.c bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end) · line 1263

Quoted 4 lines
mm/damon/core.c +1 -0
@@ -1259,6 +1260,38 @@ bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end)
 	return true;
 }
 
+/*

We also need to add one more asterisk here to make this a valid kerneldoc comment.

Quoted 21 lines
mm/damon/core.c +21 -0
+ * damon_set_region_biggest_system_ram_default() - Set the region of the given
+ * monitoring target as requested, or biggest 'System RAM'.
+ * @t:		The monitoring target to set the region.
+ * @start:	The pointer to the start address of the region.
+ * @end:	The pointer to the end address of the region.
+ *
+ * This function sets the region of @t as requested by @start and @end.  If the
+ * values of the two pointers are pointing to are zero, however, this function
+ * finds the biggest 'System RAM' resource and set the region to cover the
+ * resource.  In the latter case, this function saves the start and end address
+ * of the resource in @start and @end, respectively.
+ *
+ * Return: 0 on success, negative error code otherwise.
+ */
+int damon_set_region_biggest_system_ram_default(struct damon_target *t,
+			unsigned long *start, unsigned long *end)
+{
+	struct damon_addr_range addr_range;
+
+	if (*start > *end)
+		return -EINVAL;

mm/damon/core.c bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end) · line 1294

Quoted 10 lines
mm/damon/core.c +10 -0
+
+	if (!*start && !*end &&
+		!damon_find_biggest_system_ram(start, end))
+		return -EINVAL;
+
+	addr_range.start = *start;
+	addr_range.end = *end;
+	return damon_set_regions(t, &addr_range, 1);
+}
+

[...]

Thanks,
SJ