Skip to contentSkip to conversation

DAMON: Make Configurable for Various Address Spaces Including Physical Memory

RFC PATCH Superseded SeongJae Park 1 participant 5 messages Apr 9, 2020

Subsystems (3)

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

cover DAMON: Make Configurable for Various Address Spaces Including Physical Memory

From: SeongJae Park <[email protected]>

DAMON[1] is currently supporing only virtual memory address spaces of several target processes. Therefore, the user of DAMON should first select the target processes. This could be cumbersome in some cases and even makes no sense if the user want to monitor non-virtual address spaces. Especially, there were many requests and questions for support of physical memory monitoring.

There were also many questions about use of different access check mechanisms such as dedicated H/W features[2], idle page tracking, or perf-mem, instead of the PTE Accessed bit checking, which is currently used by DAMON. Supporting various access check mechanisms will make DAMON to be highly tunable for specific cases.

Fortunately, the core mechanisms of DAMON, the region-based sampling and adaptive regions adjustment, are not coupled with the virtual memory spaces and Accessed bit based access check. As long as there is a way to 1) address every region in the space and 2) check access to specific address, the core mechanisms could be applied. Nonetheless, current implementation of DAMON is highly coupled with the virtual memory address spaces.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://images.anandtech.com/doci/10591/HC28.AMD.Mike%20Clark.final-page-016.jpg

Baseline and Complete Git Trees
===============================

The patches are based on the v5.6 plus DAMON v8 patchset[1] and DAMOS RFC v6[2] patchset. You can also clone the complete git tree:

    $ git clone git://github.com/sjp38/linux -b cdamon/rfc/v1

The web is also available:
https://github.com/sjp38/linux/releases/tag/cdamon/rfc/v1

This patchset breaks the couplings and allows the target region definition and the access check to be configurable by users so that it can support various types of address spaces and use cases. Based on this patchset, you can configure DAMON to monitor physical memory or other special address spaces with your preferred access check mechanism.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/

Sequence of Patches
===================

The sequence of patches is as follow. The first patch defines the monitoring region again based on pure address range abstraction so that there is no assumption of virtual memory in there. The following patch cleans up code using the new abstraction. The third patch allows users to configure the initialization and dynamic update of the target address regions, which were highly coupled with virtual memory area, with their own things. Finally, the fourth patch further make the access check mechanism, which were based on PTE Accessed bit, configurable.

SeongJae Park (4):
mm/damon: Use vm-independent address range concept mm/damon: Clean up code using 'struct damon_addr_range' mm/damon: Make monitoring target regions init/update configurable mm/damon: Make access check configurable

 include/linux/damon.h |  15 +++-
 mm/damon-test.h       |  82 ++++++++++----------
 mm/damon.c            | 174 ++++++++++++++++++++----------------------
 3 files changed, 136 insertions(+), 135 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

RFC PATCH 1/4 mm/damon: Use vm-independent address range concept

From: SeongJae Park <[email protected]>

DAMON's main idea is not limited to virtual address space. To prepare for further expansion of the support for other address spaces including physical memory, this commit modifies one of its core struct, 'struct damon_region' to use virtual memory independent address space concept.

---
 include/linux/damon.h | 11 ++++---
 mm/damon-test.h       | 46 +++++++++++++-------------
 mm/damon.c            | 76 +++++++++++++++++++++----------------------
 3 files changed, 68 insertions(+), 65 deletions(-)
include/linux/damon.h +7 -4
index b0fa898ed6d8..d72dd524924f 100644
@@ -15,17 +15,20 @@
 #include <linux/time64.h>
 #include <linux/types.h>
 
+struct damon_addr_range {
+	unsigned long start;
+	unsigned long end;
+};
+
 /* Represents a monitoring target region on the virtual address space */
 struct damon_region {
-	unsigned long vm_start;
-	unsigned long vm_end;
+	struct damon_addr_range ar;
 	unsigned long sampling_addr;
 	unsigned int nr_accesses;
 	struct list_head list;
 
 	unsigned int age;
-	unsigned long last_vm_start;
-	unsigned long last_vm_end;
+	struct damon_addr_range last_ar;
 	unsigned int last_nr_accesses;
 };
 
mm/damon-test.h +23 -23

103 lines not on this page · raw message

mm/damon.c +38 -38

210 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

RFC PATCH 2/4 mm/damon: Clean up code using 'struct damon_addr_range'

In reply to SeongJae Park

From: SeongJae Park <[email protected]>

There are unnecessarily duplicated code in DAMON, that can be eliminated by using the new struct, 'damon_addr_range'. This commit cleans up the DAMON code in the way.

---
 mm/damon-test.h | 36 ++++++++++++++--------------
 mm/damon.c      | 64 +++++++++++++++++++------------------------------
 2 files changed, 42 insertions(+), 58 deletions(-)
mm/damon-test.h +18 -18
index 7fd66df1e493..7b2c903f1357 100644
@@ -165,7 +165,7 @@ static void damon_test_set_pids(struct kunit *test)
  */
 static void damon_test_three_regions_in_vmas(struct kunit *test)
 {
-	struct region regions[3] = {0,};
+	struct damon_addr_range regions[3] = {0,};
 	/* 10-20-25, 200-210-220, 300-305, 307-330 */
 	struct vm_area_struct vmas[] = {
 		(struct vm_area_struct) {.vm_start = 10, .vm_end = 20},
@@ -306,7 +306,7 @@ static void damon_test_write_rbuf(struct kunit *test)
  */
 static void damon_do_test_apply_three_regions(struct kunit *test,
 				unsigned long *regions, int nr_regions,
-				struct region *three_regions,
+				struct damon_addr_range *three_regions,
 				unsigned long *expected, int nr_expected)
 {
 	struct damon_task *t;
@@ -344,10 +344,10 @@ static void damon_test_apply_three_regions1(struct kunit *test)
 	unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
 				70, 80, 80, 90, 90, 100};
 	/* 5-27, 45-55, 73-104 */
-	struct region new_three_regions[3] = {
-		(struct region){.start = 5, .end = 27},
-		(struct region){.start = 45, .end = 55},
-		(struct region){.start = 73, .end = 104} };
+	struct damon_addr_range new_three_regions[3] = {
+		(struct damon_addr_range){.start = 5, .end = 27},
+		(struct damon_addr_range){.start = 45, .end = 55},
+		(struct damon_addr_range){.start = 73, .end = 104} };
 	/* 5-20-27, 45-55, 73-80-90-104 */
 	unsigned long expected[] = {5, 20, 20, 27, 45, 55,
 				73, 80, 80, 90, 90, 104};
@@ -366,10 +366,10 @@ static void damon_test_apply_three_regions2(struct kunit *test)
 	unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
 				70, 80, 80, 90, 90, 100};
 	/* 5-27, 56-57, 65-104 */
-	struct region new_three_regions[3] = {
-		(struct region){.start = 5, .end = 27},
-		(struct region){.start = 56, .end = 57},
-		(struct region){.start = 65, .end = 104} };
+	struct damon_addr_range new_three_regions[3] = {
+		(struct damon_addr_range){.start = 5, .end = 27},
+		(struct damon_addr_range){.start = 56, .end = 57},
+		(struct damon_addr_range){.start = 65, .end = 104} };
 	/* 5-20-27, 56-57, 65-80-90-104 */
 	unsigned long expected[] = {5, 20, 20, 27, 56, 57,
 				65, 80, 80, 90, 90, 104};
@@ -390,10 +390,10 @@ static void damon_test_apply_three_regions3(struct kunit *test)
 	unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
 				70, 80, 80, 90, 90, 100};
 	/* 5-27, 61-63, 65-104 */
-	struct region new_three_regions[3] = {
-		(struct region){.start = 5, .end = 27},
-		(struct region){.start = 61, .end = 63},
-		(struct region){.start = 65, .end = 104} };
+	struct damon_addr_range new_three_regions[3] = {
+		(struct damon_addr_range){.start = 5, .end = 27},
+		(struct damon_addr_range){.start = 61, .end = 63},
+		(struct damon_addr_range){.start = 65, .end = 104} };
 	/* 5-20-27, 61-63, 65-80-90-104 */
 	unsigned long expected[] = {5, 20, 20, 27, 61, 63,
 				65, 80, 80, 90, 90, 104};
@@ -415,10 +415,10 @@ static void damon_test_apply_three_regions4(struct kunit *test)
 	unsigned long regions[] = {10, 20, 20, 30, 50, 55, 55, 57, 57, 59,
 				70, 80, 80, 90, 90, 100};
 	/* 5-7, 30-32, 65-68 */
-	struct region new_three_regions[3] = {
-		(struct region){.start = 5, .end = 7},
-		(struct region){.start = 30, .end = 32},
-		(struct region){.start = 65, .end = 68} };
+	struct damon_addr_range new_three_regions[3] = {
+		(struct damon_addr_range){.start = 5, .end = 7},
+		(struct damon_addr_range){.start = 30, .end = 32},
+		(struct damon_addr_range){.start = 65, .end = 68} };
 	/* expect 5-7, 30-32, 65-68 */
 	unsigned long expected[] = {5, 7, 30, 32, 65, 68};
 
mm/damon.c +24 -40

171 lines not on this page · raw message

Signature · 1 line
2.17.1
#3 lore raw

RFC PATCH 3/4 mm/damon: Make monitoring target regions init/update configurable

In reply to SeongJae Park

From: SeongJae Park <[email protected]>

This commit allows DAMON users to configure their own monitoring target regions initializer / updater. Using this, users can confine the monitoring address spaces as they want. For example, users can track only stack, heap, or shared memory area, as they want.

---
 include/linux/damon.h |  2 ++
 mm/damon.c            | 20 +++++++++++++-------
 2 files changed, 15 insertions(+), 7 deletions(-)
include/linux/damon.h +2 -0
index d72dd524924f..a051b5d966ed 100644
@@ -93,6 +93,8 @@ struct damon_ctx {
 	struct list_head schemes_list;	/* 'damos' objects */
 
 	/* callbacks */
+	void (*init_target_regions)(struct damon_ctx *context);
+	void (*update_target_regions)(struct damon_ctx *context);
 	void (*sample_cb)(struct damon_ctx *context);
 	void (*aggregate_cb)(struct damon_ctx *context);
 };
mm/damon.c +13 -7
index 80fa3cab7720..da0e7efdf1e1 100644
@@ -57,6 +57,9 @@
 /* Get a random number in [l, r) */
 #define damon_rand(ctx, l, r) (l + prandom_u32_state(&ctx->rndseed) % (r - l))
 
+static void kdamond_init_vm_regions(struct damon_ctx *ctx);
+static void kdamond_update_vm_regions(struct damon_ctx *ctx);
+
 /* A monitoring context for debugfs interface users. */
 static struct damon_ctx damon_user_ctx = {
 	.sample_interval = 5 * 1000,
@@ -64,6 +67,9 @@ static struct damon_ctx damon_user_ctx = {
 	.regions_update_interval = 1000 * 1000,
 	.min_nr_regions = 10,
 	.max_nr_regions = 1000,
+
+	.init_target_regions = kdamond_init_vm_regions,
+	.update_target_regions = kdamond_update_vm_regions,
 };
 
 /*
@@ -327,7 +333,7 @@ static void swap_ranges(struct damon_addr_range *r1,
  *
  * This function receives an address space and finds three regions in it which
  * separated by the two biggest unmapped regions in the space.  Please refer to
- * below comments of 'damon_init_regions_of()' function to know why this is
+ * below comments of 'damon_init_vm_regions_of()' function to know why this is
  * necessary.
  *
  * Returns 0 if success, or negative error code otherwise.
@@ -439,7 +445,7 @@ static int damon_three_regions_of(struct damon_task *t,
  *   <BIG UNMAPPED REGION 2>
  *   <stack>
  */
-static void damon_init_regions_of(struct damon_ctx *c, struct damon_task *t)
+static void damon_init_vm_regions_of(struct damon_ctx *c, struct damon_task *t)
 {
 	struct damon_region *r;
 	struct damon_addr_range regions[3];
@@ -463,12 +469,12 @@ static void damon_init_regions_of(struct damon_ctx *c, struct damon_task *t)
 }
 
 /* Initialize '->regions_list' of every task */
-static void kdamond_init_regions(struct damon_ctx *ctx)
+static void kdamond_init_vm_regions(struct damon_ctx *ctx)
 {
 	struct damon_task *t;
 
 	damon_for_each_task(ctx, t)
-		damon_init_regions_of(ctx, t);
+		damon_init_vm_regions_of(ctx, t);
 }
 
 static void damon_mkold(struct mm_struct *mm, unsigned long addr)
@@ -1074,7 +1080,7 @@ static void damon_apply_three_regions(struct damon_ctx *ctx,
 /*
  * Update regions for current memory mappings
  */
-static void kdamond_update_regions(struct damon_ctx *ctx)
+static void kdamond_update_vm_regions(struct damon_ctx *ctx)
 {
 	struct damon_addr_range three_regions[3];
 	struct damon_task *t;
@@ -1126,7 +1132,7 @@ static int kdamond_fn(void *data)
 	unsigned int max_nr_accesses = 0;
 
 	pr_info("kdamond (%d) starts\n", ctx->kdamond->pid);
-	kdamond_init_regions(ctx);
+	ctx->init_target_regions(ctx);
 	while (!kdamond_need_stop(ctx)) {
 		kdamond_prepare_access_checks(ctx);
 		if (ctx->sample_cb)
@@ -1147,7 +1153,7 @@ static int kdamond_fn(void *data)
 		}
 
 		if (kdamond_need_update_regions(ctx))
-			kdamond_update_regions(ctx);
+			ctx->update_target_regions(ctx);
 	}
 	damon_flush_rbuffer(ctx);
 	damon_for_each_task(ctx, t) {
Signature · 1 line
2.17.1
#4 lore raw

RFC PATCH 4/4 mm/damon: Make access check configurable

In reply to SeongJae Park

From: SeongJae Park <[email protected]>

DAMON assumes the target region is in virtual address space and therefore uses PTE Accessed bit checking for access checking. However, some users might want to use architecture-specific, more accurate and light-weight access checking features. Also, some users might want to use DAMON for different address spaces such as physical memory space, which needs different ways to check the access.

This commit allows DAMON users to configure the access check function to their own version.

---
 include/linux/damon.h |  2 ++
 mm/damon.c            | 22 +++++++++++++---------
 2 files changed, 15 insertions(+), 9 deletions(-)
include/linux/damon.h +2 -0
index a051b5d966ed..188d5b89b303 100644
@@ -95,6 +95,8 @@ struct damon_ctx {
 	/* callbacks */
 	void (*init_target_regions)(struct damon_ctx *context);
 	void (*update_target_regions)(struct damon_ctx *context);
+	void (*prepare_access_checks)(struct damon_ctx *context);
+	unsigned int (*check_accesses)(struct damon_ctx *context);
 	void (*sample_cb)(struct damon_ctx *context);
 	void (*aggregate_cb)(struct damon_ctx *context);
 };
mm/damon.c +13 -9
index da0e7efdf1e1..20a66a6307d1 100644
@@ -59,6 +59,8 @@
 
 static void kdamond_init_vm_regions(struct damon_ctx *ctx);
 static void kdamond_update_vm_regions(struct damon_ctx *ctx);
+static void kdamond_prepare_vm_access_checks(struct damon_ctx *ctx);
+static unsigned int kdamond_check_vm_accesses(struct damon_ctx *ctx);
 
 /* A monitoring context for debugfs interface users. */
 static struct damon_ctx damon_user_ctx = {
@@ -70,6 +72,8 @@ static struct damon_ctx damon_user_ctx = {
 
 	.init_target_regions = kdamond_init_vm_regions,
 	.update_target_regions = kdamond_update_vm_regions,
+	.prepare_access_checks = kdamond_prepare_vm_access_checks,
+	.check_accesses = kdamond_check_vm_accesses,
 };
 
 /*
@@ -506,7 +510,7 @@ static void damon_mkold(struct mm_struct *mm, unsigned long addr)
 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
 }
 
-static void damon_prepare_access_check(struct damon_ctx *ctx,
+static void damon_prepare_vm_access_check(struct damon_ctx *ctx,
 			struct mm_struct *mm, struct damon_region *r)
 {
 	r->sampling_addr = damon_rand(ctx, r->ar.start, r->ar.end);
@@ -514,7 +518,7 @@ static void damon_prepare_access_check(struct damon_ctx *ctx,
 	damon_mkold(mm, r->sampling_addr);
 }
 
-static void kdamond_prepare_access_checks(struct damon_ctx *ctx)
+static void kdamond_prepare_vm_access_checks(struct damon_ctx *ctx)
 {
 	struct damon_task *t;
 	struct mm_struct *mm;
@@ -525,7 +529,7 @@ static void kdamond_prepare_access_checks(struct damon_ctx *ctx)
 		if (!mm)
 			continue;
 		damon_for_each_region(r, t)
-			damon_prepare_access_check(ctx, mm, r);
+			damon_prepare_vm_access_check(ctx, mm, r);
 		mmput(mm);
 	}
 }
@@ -563,7 +567,7 @@ static bool damon_young(struct mm_struct *mm, unsigned long addr,
  * mm	'mm_struct' for the given virtual address space
  * r	the region to be checked
  */
-static void damon_check_access(struct damon_ctx *ctx,
+static void damon_check_vm_access(struct damon_ctx *ctx,
 			struct mm_struct *mm, struct damon_region *r)
 {
 	static struct mm_struct *last_mm;
@@ -587,7 +591,7 @@ static void damon_check_access(struct damon_ctx *ctx,
 	last_addr = r->sampling_addr;
 }
 
-static unsigned int kdamond_check_accesses(struct damon_ctx *ctx)
+static unsigned int kdamond_check_vm_accesses(struct damon_ctx *ctx)
 {
 	struct damon_task *t;
 	struct mm_struct *mm;
@@ -599,12 +603,12 @@ static unsigned int kdamond_check_accesses(struct damon_ctx *ctx)
 		if (!mm)
 			continue;
 		damon_for_each_region(r, t) {
-			damon_check_access(ctx, mm, r);
+			damon_check_vm_access(ctx, mm, r);
 			max_nr_accesses = max(r->nr_accesses, max_nr_accesses);
 		}
-
 		mmput(mm);
 	}
+
 	return max_nr_accesses;
 }
 
@@ -1134,13 +1138,13 @@ static int kdamond_fn(void *data)
 	pr_info("kdamond (%d) starts\n", ctx->kdamond->pid);
 	ctx->init_target_regions(ctx);
 	while (!kdamond_need_stop(ctx)) {
-		kdamond_prepare_access_checks(ctx);
+		ctx->prepare_access_checks(ctx);
 		if (ctx->sample_cb)
 			ctx->sample_cb(ctx);
 
 		usleep_range(ctx->sample_interval, ctx->sample_interval + 1);
 
-		max_nr_accesses = kdamond_check_accesses(ctx);
+		max_nr_accesses = ctx->check_accesses(ctx);
 
 		if (kdamond_aggregate_interval_passed(ctx)) {
 			kdamond_merge_regions(ctx, max_nr_accesses / 10);
Signature · 1 line
2.17.1