mm/damon/vaddr: generalize damon_va_apply_three_regions()
authorSeongJae Park <sj@kernel.org>
Tue, 10 May 2022 01:20:54 +0000 (18:20 -0700)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 13 May 2022 14:20:08 +0000 (07:20 -0700)
'damon_va_apply_three_regions()' is for adjusting address ranges to fit in
three discontiguous ranges.  The function can be generalized for arbitrary
number of discontiguous ranges and reused for future usage, such as
arbitrary online regions update.  For such future usage, this commit
introduces a generalized version of the function called
'damon_set_regions()'.

Link: https://lkml.kernel.org/r/20220429160606.127307-4-sj@kernel.org
Signed-off-by: SeongJae Park <sj@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/damon/vaddr.c

index 5ba82ab..26e9ad8 100644 (file)
@@ -297,59 +297,77 @@ static bool damon_intersect(struct damon_region *r,
 }
 
 /*
- * Update damon regions for the three big regions of the given target
+ * damon_set_regions() - Set regions of a target for given address ranges.
+ * @t:         the given target.
+ * @ranges:    array of new monitoring target ranges.
+ * @nr_ranges: length of @ranges.
  *
- * t           the given target
- * bregions    the three big regions of the target
+ * This function adds new regions to, or modify existing regions of a
+ * monitoring target to fit in specific ranges.
+ *
+ * Return: 0 if success, or negative error code otherwise.
  */
-static void damon_va_apply_three_regions(struct damon_target *t,
-               struct damon_addr_range bregions[3])
+static int damon_set_regions(struct damon_target *t,
+               struct damon_addr_range *ranges, unsigned int nr_ranges)
 {
        struct damon_region *r, *next;
        unsigned int i;
 
-       /* Remove regions which are not in the three big regions now */
+       /* Remove regions which are not in the new ranges */
        damon_for_each_region_safe(r, next, t) {
-               for (i = 0; i < 3; i++) {
-                       if (damon_intersect(r, &bregions[i]))
+               for (i = 0; i < nr_ranges; i++) {
+                       if (damon_intersect(r, &ranges[i]))
                                break;
                }
-               if (i == 3)
+               if (i == nr_ranges)
                        damon_destroy_region(r, t);
        }
 
-       /* Adjust intersecting regions to fit with the three big regions */
-       for (i = 0; i < 3; i++) {
-               struct damon_region *first = NULL, *last;
-               struct damon_region *newr;
-               struct damon_addr_range *br;
+       /* Add new regions or resize existing regions to fit in the ranges */
+       for (i = 0; i < nr_ranges; i++) {
+               struct damon_region *first = NULL, *last, *newr;
+               struct damon_addr_range *range;
 
-               br = &bregions[i];
-               /* Get the first and last regions which intersects with br */
+               range = &ranges[i];
+               /* Get the first/last regions intersecting with the range */
                damon_for_each_region(r, t) {
-                       if (damon_intersect(r, br)) {
+                       if (damon_intersect(r, range)) {
                                if (!first)
                                        first = r;
                                last = r;
                        }
-                       if (r->ar.start >= br->end)
+                       if (r->ar.start >= range->end)
                                break;
                }
                if (!first) {
-                       /* no damon_region intersects with this big region */
+                       /* no region intersects with this range */
                        newr = damon_new_region(
-                                       ALIGN_DOWN(br->start,
+                                       ALIGN_DOWN(range->start,
                                                DAMON_MIN_REGION),
-                                       ALIGN(br->end, DAMON_MIN_REGION));
+                                       ALIGN(range->end, DAMON_MIN_REGION));
                        if (!newr)
-                               continue;
+                               return -ENOMEM;
                        damon_insert_region(newr, damon_prev_region(r), r, t);
                } else {
-                       first->ar.start = ALIGN_DOWN(br->start,
+                       /* resize intersecting regions to fit in this range */
+                       first->ar.start = ALIGN_DOWN(range->start,
                                        DAMON_MIN_REGION);
-                       last->ar.end = ALIGN(br->end, DAMON_MIN_REGION);
+                       last->ar.end = ALIGN(range->end, DAMON_MIN_REGION);
                }
        }
+       return 0;
+}
+
+/*
+ * Update damon regions for the three big regions of the given target
+ *
+ * t           the given target
+ * bregions    the three big regions of the target
+ */
+static void damon_va_apply_three_regions(struct damon_target *t,
+               struct damon_addr_range bregions[3])
+{
+       damon_set_regions(t, bregions, 3);
 }
 
 /*