linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/damon: deduplicate damon_{reclaim,lru_sort}_apply_parameters()
@ 2022-09-19  6:28 xiakaixu1987
  2022-09-19 16:56 ` SeongJae Park
  0 siblings, 1 reply; 3+ messages in thread
From: xiakaixu1987 @ 2022-09-19  6:28 UTC (permalink / raw)
  To: sj, akpm; +Cc: damon, linux-mm, linux-kernel, Kaixu Xia

From: Kaixu Xia <kaixuxia@tencent.com>

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

Signed-off-by: Kaixu Xia <kaixuxia@tencent.com>
---
 include/linux/damon.h |  3 ++-
 mm/damon/core.c       | 20 +++++++++++++++++++-
 mm/damon/lru_sort.c   | 13 +++----------
 mm/damon/reclaim.c    | 13 +++----------
 4 files changed, 27 insertions(+), 22 deletions(-)

diff --git a/include/linux/damon.h b/include/linux/damon.h
index e7808a84675f..2fd05568ef45 100644
--- a/include/linux/damon.h
+++ b/include/linux/damon.h
@@ -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_regions_from_system_ram(struct damon_target *t,
+				unsigned long *start, unsigned long *end);
 
 #endif	/* CONFIG_DAMON */
 
diff --git a/mm/damon/core.c b/mm/damon/core.c
index 9c80c6eb00c2..d967b2805a53 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -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,23 @@ bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end)
 	return true;
 }
 
+int damon_set_regions_from_system_ram(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);
diff --git a/mm/damon/lru_sort.c b/mm/damon/lru_sort.c
index d7eb72b41cb6..0276cbec632a 100644
--- a/mm/damon/lru_sort.c
+++ b/mm/damon/lru_sort.c
@@ -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_regions_from_system_ram(target,
+				&monitor_region_start,
+				&monitor_region_end);
 }
 
 static int damon_lru_sort_turn(bool on)
diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
index 3d59ab11b7b3..6297e1799190 100644
--- a/mm/damon/reclaim.c
+++ b/mm/damon/reclaim.c
@@ -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_regions_from_system_ram(target,
+				&monitor_region_start,
+				&monitor_region_end);
 }
 
 static int damon_reclaim_turn(bool on)
-- 
2.27.0



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/damon: deduplicate damon_{reclaim,lru_sort}_apply_parameters()
  2022-09-19  6:28 [PATCH] mm/damon: deduplicate damon_{reclaim,lru_sort}_apply_parameters() xiakaixu1987
@ 2022-09-19 16:56 ` SeongJae Park
  2022-09-20  1:43   ` Kaixu Xia
  0 siblings, 1 reply; 3+ messages in thread
From: SeongJae Park @ 2022-09-19 16:56 UTC (permalink / raw)
  To: xiakaixu1987; +Cc: sj, akpm, damon, linux-mm, linux-kernel, Kaixu Xia

Hi Kaixu,

On Mon, 19 Sep 2022 14:28:09 +0800 xiakaixu1987@gmail.com wrote:

> From: Kaixu Xia <kaixuxia@tencent.com>
> 
> The bodies of damon_{reclaim,lru_sort}_apply_parameters() contain
> duplicates.

Good finding!

> This commit add a common function
> damon_set_regions_from_system_ram() to removes the duplicates.
> 
> Signed-off-by: Kaixu Xia <kaixuxia@tencent.com>
> ---
>  include/linux/damon.h |  3 ++-
>  mm/damon/core.c       | 20 +++++++++++++++++++-
>  mm/damon/lru_sort.c   | 13 +++----------
>  mm/damon/reclaim.c    | 13 +++----------
>  4 files changed, 27 insertions(+), 22 deletions(-)
> 
> diff --git a/include/linux/damon.h b/include/linux/damon.h
> index e7808a84675f..2fd05568ef45 100644
> --- a/include/linux/damon.h
> +++ b/include/linux/damon.h
> @@ -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_regions_from_system_ram(struct damon_target *t,
> +				unsigned long *start, unsigned long *end);
>  
>  #endif	/* CONFIG_DAMON */
>  
> diff --git a/mm/damon/core.c b/mm/damon/core.c
> index 9c80c6eb00c2..d967b2805a53 100644
> --- a/mm/damon/core.c
> +++ b/mm/damon/core.c
> @@ -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,23 @@ bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end)
>  	return true;
>  }
>  
> +int damon_set_regions_from_system_ram(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;

This function sets the region as biggest system ram only if both *start and
*end are zero.  The name should be changed, and it would better to have a
kernel doc comment as the behavior is not very simple.  How about below?

/**
 * 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.
 */

> +
> +	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);
> diff --git a/mm/damon/lru_sort.c b/mm/damon/lru_sort.c
> index d7eb72b41cb6..0276cbec632a 100644
> --- a/mm/damon/lru_sort.c
> +++ b/mm/damon/lru_sort.c
> @@ -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_regions_from_system_ram(target,
> +				&monitor_region_start,
> +				&monitor_region_end);
>  }
>  
>  static int damon_lru_sort_turn(bool on)
> diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
> index 3d59ab11b7b3..6297e1799190 100644
> --- a/mm/damon/reclaim.c
> +++ b/mm/damon/reclaim.c
> @@ -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_regions_from_system_ram(target,
> +				&monitor_region_start,
> +				&monitor_region_end);
>  }
>  
>  static int damon_reclaim_turn(bool on)
> -- 
> 2.27.0
> 

Thanks,
SJ


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/damon: deduplicate damon_{reclaim,lru_sort}_apply_parameters()
  2022-09-19 16:56 ` SeongJae Park
@ 2022-09-20  1:43   ` Kaixu Xia
  0 siblings, 0 replies; 3+ messages in thread
From: Kaixu Xia @ 2022-09-20  1:43 UTC (permalink / raw)
  To: SeongJae Park; +Cc: akpm, damon, linux-mm, LKML, Kaixu Xia

On Tue, Sep 20, 2022 at 12:56 AM SeongJae Park <sj@kernel.org> wrote:
>
> Hi Kaixu,
>
> On Mon, 19 Sep 2022 14:28:09 +0800 xiakaixu1987@gmail.com wrote:
>
> > From: Kaixu Xia <kaixuxia@tencent.com>
> >
> > The bodies of damon_{reclaim,lru_sort}_apply_parameters() contain
> > duplicates.
>
> Good finding!
>
> > This commit add a common function
> > damon_set_regions_from_system_ram() to removes the duplicates.
> >
> > Signed-off-by: Kaixu Xia <kaixuxia@tencent.com>
> > ---
> >  include/linux/damon.h |  3 ++-
> >  mm/damon/core.c       | 20 +++++++++++++++++++-
> >  mm/damon/lru_sort.c   | 13 +++----------
> >  mm/damon/reclaim.c    | 13 +++----------
> >  4 files changed, 27 insertions(+), 22 deletions(-)
> >
> > diff --git a/include/linux/damon.h b/include/linux/damon.h
> > index e7808a84675f..2fd05568ef45 100644
> > --- a/include/linux/damon.h
> > +++ b/include/linux/damon.h
> > @@ -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_regions_from_system_ram(struct damon_target *t,
> > +                             unsigned long *start, unsigned long *end);
> >
> >  #endif       /* CONFIG_DAMON */
> >
> > diff --git a/mm/damon/core.c b/mm/damon/core.c
> > index 9c80c6eb00c2..d967b2805a53 100644
> > --- a/mm/damon/core.c
> > +++ b/mm/damon/core.c
> > @@ -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,23 @@ bool damon_find_biggest_system_ram(unsigned long *start, unsigned long *end)
> >       return true;
> >  }
> >
> > +int damon_set_regions_from_system_ram(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;
>
> This function sets the region as biggest system ram only if both *start and
> *end are zero.  The name should be changed, and it would better to have a
> kernel doc comment as the behavior is not very simple.  How about below?
>
> /**
>  * 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.
>  */
>
Thanks for your detailed comments! I'll follow that in the next version.


Thanks,
Kaixu
> > +
> > +     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);
> > diff --git a/mm/damon/lru_sort.c b/mm/damon/lru_sort.c
> > index d7eb72b41cb6..0276cbec632a 100644
> > --- a/mm/damon/lru_sort.c
> > +++ b/mm/damon/lru_sort.c
> > @@ -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_regions_from_system_ram(target,
> > +                             &monitor_region_start,
> > +                             &monitor_region_end);
> >  }
> >
> >  static int damon_lru_sort_turn(bool on)
> > diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
> > index 3d59ab11b7b3..6297e1799190 100644
> > --- a/mm/damon/reclaim.c
> > +++ b/mm/damon/reclaim.c
> > @@ -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_regions_from_system_ram(target,
> > +                             &monitor_region_start,
> > +                             &monitor_region_end);
> >  }
> >
> >  static int damon_reclaim_turn(bool on)
> > --
> > 2.27.0
> >
>
> Thanks,
> SJ


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-09-20  1:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-19  6:28 [PATCH] mm/damon: deduplicate damon_{reclaim,lru_sort}_apply_parameters() xiakaixu1987
2022-09-19 16:56 ` SeongJae Park
2022-09-20  1:43   ` Kaixu Xia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox