Skip to contentSkip to conversation

mm/damon: support pin-point targets removal

RFC PATCH In review SeongJae Park 2 participants 8 messages Oct 16, 2025 – Oct 17, 2025

Subsystems (4)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

cover mm/damon: support pin-point targets removal

DAMON maintains the targets in a list, and allows committing only and entire list of targets having new parameters. Targets having same index on the lists are treated as matching source and destination targets. If an existing target cannot find a matching one in the sources list, the target is removed. This means that there is no way to remove only a specific monitoring target in the middle of the current targets list.

Such pin-point targets removal is really needed in some use cases, though. Monitoring accesses to virtual address spaces of processes that spawned from a same ancestor is one example. If a process of the group is terminated, the user may want to remove the matching DAMON target as soon as possible, to save in-kernel memory usage for the unnecessary target data. The user may also want to do that without turning DAMON off or removing unnecessary targets, to keep the current monitoring results for other active processes.

Extend DAMON kernel API and sysfs ABI to support the pin-point removal in the following way. For API, add a new damon_target field, namely 'obsolete'. If the field on parameters commit source target is set, it means the matching destination target is obsolete. Than the parameters commit logic removes the destination target from the existing targets list. For sysfs ABI, add a new file under target directory, namely 'obsolete_target'. It is connected with the 'obsolete' field of the commit source targets, so internally using the new API.

Bijan Tabatabai ([email protected]) has originally reported this issue, and participated in this solution design on a GitHub issue [1] for DAMON user-space tool.

SeongJae Park (5):
mm/damon/core: add damon_target->obsolete for pin-point removal mm/damon/sysfs: test commit input against realistic destination mm/damon/sysfs: implement obsolete_target file Docs/admin-guide/mm/damon/usage: document obsolete_target file Docs/ABI/damon: document obsolete_target sysfs file

 .../ABI/testing/sysfs-kernel-mm-damon         |  7 +++
 Documentation/admin-guide/mm/damon/usage.rst  | 13 +++--
 include/linux/damon.h                         |  6 +++
 mm/damon/core.c                               | 10 +++-
 mm/damon/sysfs.c                              | 51 ++++++++++++++++++-
 5 files changed, 82 insertions(+), 5 deletions(-)

base-commit: f82c3c7358c244b2ba14dadcae822c8d36cf2163

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

RFC PATCH 1/5 mm/damon/core: add damon_target->obsolete for pin-point removal

DAMON's monitoring targets parameters update function, damon_commit_targets(), is not providing a way to remove a target in the middle of existing targets list. Extend the API by adding a field to struct damon_target. If the field of a damon_commit_targets() source target is set, it indicates the matcing target on the existing targets list is obsolete. damon_commit_targets() understands that and remove those from the list, while respecting the index based matching for other non-obsolete targets.

---
 include/linux/damon.h |  6 ++++++
 mm/damon/core.c       | 10 +++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
include/linux/damon.h +6 -0
index 524dea87cac7..8a7b45b9e40d 100644
@@ -92,17 +92,23 @@ struct damon_region {
  * @nr_regions:		Number of monitoring target regions of this target.
  * @regions_list:	Head of the monitoring target regions of this target.
  * @list:		List head for siblings.
+ * @obsolete:		Whether the commit destination target is obsolete.
  *
  * Each monitoring context could have multiple targets.  For example, a context
  * for virtual memory address spaces could have multiple target processes.  The
  * @pid should be set for appropriate &struct damon_operations including the
  * virtual address spaces monitoring operations.
+ *
+ * @obsolte is used only for damon_commit_targets() source targets, to specify
+ * the matching destination targets are obsolte.  Read damon_commit_targets()
+ * to see how it is handled.
  */
 struct damon_target {
 	struct pid *pid;
 	unsigned int nr_regions;
 	struct list_head regions_list;
 	struct list_head list;
+	bool obsolete;
 };
 
 /**
mm/damon/core.c +9 -1
index 70e66562a1b3..3242a9573db0 100644
@@ -492,6 +492,7 @@ struct damon_target *damon_new_target(void)
 	t->nr_regions = 0;
 	INIT_LIST_HEAD(&t->regions_list);
 	INIT_LIST_HEAD(&t->list);
+	t->obsolete = false;
 
 	return t;
 }
@@ -1213,7 +1214,11 @@ static int damon_commit_targets(
 
 	damon_for_each_target_safe(dst_target, next, dst) {
 		src_target = damon_nth_target(i++, src);
-		if (src_target) {
+		/*
+		 * If src target is obsolete, do not commit the parameters to
+		 * the dst target, and further remove the dst target.
+		 */
+		if (src_target && !src_target->obsolete) {
 			err = damon_commit_target(
 					dst_target, damon_target_has_pid(dst),
 					src_target, damon_target_has_pid(src),
@@ -1236,6 +1241,9 @@ static int damon_commit_targets(
 	damon_for_each_target_safe(src_target, next, src) {
 		if (j++ < i)
 			continue;
+		/* target to remove has no matching dst */
+		if (src_target->obsolete)
+			return -EINVAL;
 		new_target = damon_new_target();
 		if (!new_target)
 			return -ENOMEM;
Signature · 1 line
2.47.3
Reviewed-by on v1 1/5
#2 lore raw

Re: [RFC PATCH 1/5] mm/damon/core: add damon_target->obsolete for pin-point removal

Thanks for working on this SJ!

Quoted 25 lines · SeongJae Park

DAMON's monitoring targets parameters update function, damon_commit_targets(), is not providing a way to remove a target in the middle of existing targets list. Extend the API by adding a field to struct damon_target. If the field of a damon_commit_targets() source target is set, it indicates the matcing target on the existing targets list is obsolete. damon_commit_targets() understands that and remove those from the list, while respecting the index based matching for other non-obsolete targets.

---
 include/linux/damon.h |  6 ++++++
 mm/damon/core.c       | 10 +++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
include/linux/damon.h +1 -0
index 524dea87cac7..8a7b45b9e40d 100644
@@ -92,17 +92,23 @@ struct damon_region {
  * @nr_regions:                Number of monitoring target regions of this target.
  * @regions_list:      Head of the monitoring target regions of this target.
  * @list:              List head for siblings.
+ * @obsolete:          Whether the commit destination target is obsolete.

include/linux/damon.h struct damon_region { · line 105

Quoted 10 lines
include/linux/damon.h +4 -0
  *
  * Each monitoring context could have multiple targets.  For example, a context
  * for virtual memory address spaces could have multiple target processes.  The
  * @pid should be set for appropriate &struct damon_operations including the
  * virtual address spaces monitoring operations.
+ *
+ * @obsolte is used only for damon_commit_targets() source targets, to specify
+ * the matching destination targets are obsolte.  Read damon_commit_targets()
+ * to see how it is handled.
  */

Nit: Twice in the above comment you've written "obsolte" instead of "obsolete."

With that fixed

for the entire series.

Thanks,
Bijan

Quoted 46 lines
include/linux/damon.h +1 -0
 struct damon_target {
        struct pid *pid;
        unsigned int nr_regions;
        struct list_head regions_list;
        struct list_head list;
+       bool obsolete;
 };
 
 /**
mm/damon/core.c +9 -2
index 70e66562a1b3..3242a9573db0 100644
@@ -492,6 +492,7 @@ struct damon_target *damon_new_target(void)
        t->nr_regions = 0;
        INIT_LIST_HEAD(&t->regions_list);
        INIT_LIST_HEAD(&t->list);
+       t->obsolete = false;
 
        return t;
 }
@@ -1213,7 +1214,11 @@ static int damon_commit_targets(
 
        damon_for_each_target_safe(dst_target, next, dst) {
                src_target = damon_nth_target(i++, src);
-               if (src_target) {
+               /*
+                * If src target is obsolete, do not commit the parameters to
+                * the dst target, and further remove the dst target.
+                */
+               if (src_target && !src_target->obsolete) {
                        err = damon_commit_target(
                                        dst_target, damon_target_has_pid(dst),
                                        src_target, damon_target_has_pid(src),
@@ -1236,6 +1241,9 @@ static int damon_commit_targets(
        damon_for_each_target_safe(src_target, next, src) {
                if (j++ < i)
                        continue;
+               /* target to remove has no matching dst */
+               if (src_target->obsolete)
+                       return -EINVAL;
                new_target = damon_new_target();
                if (!new_target)
                        return -ENOMEM;
--

2.47.3

author maintainer
#3 lore raw

Re: [RFC PATCH 1/5] mm/damon/core: add damon_target->obsolete for pin-point removal

Quoted 1 line · On Fri, 17 Oct 2025 09:52:16 -0500 Bijan Tabatabai <[email protected]> wrote:

Thanks for working on this SJ!

My pleasure!

Quoted 30 lines · SeongJae Park
Quoted 28 lines

DAMON's monitoring targets parameters update function, damon_commit_targets(), is not providing a way to remove a target in the middle of existing targets list. Extend the API by adding a field to struct damon_target. If the field of a damon_commit_targets() source target is set, it indicates the matcing target on the existing targets list is obsolete. damon_commit_targets() understands that and remove those from the list, while respecting the index based matching for other non-obsolete targets.

---
 include/linux/damon.h |  6 ++++++
 mm/damon/core.c       | 10 +++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
include/linux/damon.h +1 -0
index 524dea87cac7..8a7b45b9e40d 100644
@@ -92,17 +92,23 @@ struct damon_region {
  * @nr_regions:                Number of monitoring target regions of this target.
  * @regions_list:      Head of the monitoring target regions of this target.
  * @list:              List head for siblings.
+ * @obsolete:          Whether the commit destination target is obsolete.
  *
  * Each monitoring context could have multiple targets.  For example, a context
  * for virtual memory address spaces could have multiple target processes.  The
Quoted 8 lines

include/linux/damon.h struct damon_region { · line 105

Quoted 7 lines
include/linux/damon.h +4 -0
  * @pid should be set for appropriate &struct damon_operations including the
  * virtual address spaces monitoring operations.
+ *
+ * @obsolte is used only for damon_commit_targets() source targets, to specify
+ * the matching destination targets are obsolte.  Read damon_commit_targets()
+ * to see how it is handled.
  */

Nit: Twice in the above comment you've written "obsolte" instead of "obsolete."

Thank you for catching this, I will definitely fix those on the next spin.

Quoted 4 lines

With that fixed

for the entire series.

Thank you!

Thanks,
SJ

[...]

author maintainer
#4 lore raw

RFC PATCH 2/5 mm/damon/sysfs: test commit input against realistic destination

In reply to SeongJae Park

DAMON sysfs interface tests if given online parameters update request is valid, by committing those using the DAMON kernel API, to a test-purpose destination context. The test-purpose destination context is constructed using damon_new_ctx(), so it has no target, no scheme.

If a source target has obsolete field set, the test-purpose commit will fail because damon_commit_targets() fails when there is a source obsolete target that cannot find its matching destination target. DAMON sysfs interface is not letting users set the field for now, so there is no problem. However, a following commit will support that. Also there could be similar future changes that making commit fails based on current context structure.

Make the test purpose commit destination context similar to the current running one, by committing the running one to the test purpose context, before doing the real test-purpose commit.

---
 mm/damon/sysfs.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
mm/damon/sysfs.c +21 -1
index dfdf69bdbbd3..6848437b86af 100644
@@ -1680,6 +1680,26 @@ static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
 static struct damon_ctx *damon_sysfs_build_ctx(
 		struct damon_sysfs_context *sys_ctx);
 
+/*
+ * Return a new damon_ctx for testing new parameters to commit.
+ */
+static struct damon_ctx *damon_sysfs_new_test_ctx(
+		struct damon_ctx *running_ctx)
+{
+	struct damon_ctx *test_ctx;
+	int err;
+
+	test_ctx = damon_new_ctx();
+	if (!test_ctx)
+		return NULL;
+	err = damon_commit_ctx(test_ctx, running_ctx);
+	if (err) {
+		damon_destroy_ctx(test_ctx);
+		return NULL;
+	}
+	return test_ctx;
+}
+
 /*
  * damon_sysfs_commit_input() - Commit user inputs to a running kdamond.
  * @kdamond:	The kobject wrapper for the associated kdamond.
@@ -1701,7 +1721,7 @@ static int damon_sysfs_commit_input(void *data)
 	param_ctx = damon_sysfs_build_ctx(kdamond->contexts->contexts_arr[0]);
 	if (IS_ERR(param_ctx))
 		return PTR_ERR(param_ctx);
-	test_ctx = damon_new_ctx();
+	test_ctx = damon_sysfs_new_test_ctx(kdamond->damon_ctx);
 	if (!test_ctx)
 		return -ENOMEM;
 	err = damon_commit_ctx(test_ctx, param_ctx);
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

RFC PATCH 3/5 mm/damon/sysfs: implement obsolete_target file

In reply to SeongJae Park

There is no good way to remove DAMON targets in the middle of existing targets list. It restricts efficient and flexible DAMON use cases. Improve the usability by implementing a new DAMON sysfs interface file, namely obsolete_target, under each target directory. It is connected to the obsolete field of parameters commit source targets, so allows removing arbitrary targets in the middle of existing targets list.

Note that the sysfs files are not automatically updated. For example, let's suppose there are three targets in the running context, and a user removes the third target using this feature. If the user writes 'commit' to the kdamond 'state' file again, DAMON sysfs interface will again try to remove the third target. But because there is no matching target in the running context, the commit will fail. It is the user's responsibility to understand resulting DAMON internal targets list change, and construct sysfs files (using nr_targets and other sysfs files) to correctly represent it.

Also note that this is arguably an improvement rather than a fix of a broken things.

---
 mm/damon/sysfs.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
mm/damon/sysfs.c +29 -0
index 6848437b86af..26bc658ad873 100644
@@ -212,6 +212,7 @@ struct damon_sysfs_target {
 	struct kobject kobj;
 	struct damon_sysfs_regions *regions;
 	int pid;
+	bool obsolete;
 };
 
 static struct damon_sysfs_target *damon_sysfs_target_alloc(void)
@@ -263,6 +264,29 @@ static ssize_t pid_target_store(struct kobject *kobj,
 	return count;
 }
 
+static ssize_t obsolete_target_show(struct kobject *kobj,
+		struct kobj_attribute *attr, char *buf)
+{
+	struct damon_sysfs_target *target = container_of(kobj,
+			struct damon_sysfs_target, kobj);
+
+	return sysfs_emit(buf, "%c\n", target->obsolete ? 'Y' : 'N');
+}
+
+static ssize_t obsolete_target_store(struct kobject *kobj,
+		struct kobj_attribute *attr, const char *buf, size_t count)
+{
+	struct damon_sysfs_target *target = container_of(kobj,
+			struct damon_sysfs_target, kobj);
+	bool obsolete;
+	int err = kstrtobool(buf, &obsolete);
+
+	if (err)
+		return err;
+	target->obsolete = obsolete;
+	return count;
+}
+
 static void damon_sysfs_target_release(struct kobject *kobj)
 {
 	kfree(container_of(kobj, struct damon_sysfs_target, kobj));
@@ -271,8 +295,12 @@ static void damon_sysfs_target_release(struct kobject *kobj)
 static struct kobj_attribute damon_sysfs_target_pid_attr =
 		__ATTR_RW_MODE(pid_target, 0600);
 
+static struct kobj_attribute damon_sysfs_target_obsolete_attr =
+		__ATTR_RW_MODE(obsolete_target, 0600);
+
 static struct attribute *damon_sysfs_target_attrs[] = {
 	&damon_sysfs_target_pid_attr.attr,
+	&damon_sysfs_target_obsolete_attr.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(damon_sysfs_target);
@@ -1574,6 +1602,7 @@ static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target,
 			/* caller will destroy targets */
 			return -EINVAL;
 	}
+	t->obsolete = sys_target->obsolete;
 	return damon_sysfs_set_regions(t, sys_target->regions, ctx->min_sz_region);
 }
 
Signature · 1 line
2.47.3
author maintainer
#6 lore raw

RFC PATCH 4/5 Docs/admin-guide/mm/damon/usage: document obsolete_target file

In reply to SeongJae Park

Document the newly added obsolete_target DAMON sysfs file.

---
 Documentation/admin-guide/mm/damon/usage.rst | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
Documentation/admin-guide/mm/damon/usage.rst +10 -3
index 98958975604d..d8a3d6e740b3 100644
@@ -67,7 +67,7 @@ comma (",").
     │ │ │ │ │ │ │ intervals_goal/access_bp,aggrs,min_sample_us,max_sample_us
     │ │ │ │ │ │ nr_regions/min,max
     │ │ │ │ │ :ref:`targets <sysfs_targets>`/nr_targets
-    │ │ │ │ │ │ :ref:`0 <sysfs_target>`/pid_target
+    │ │ │ │ │ │ :ref:`0 <sysfs_target>`/pid_target,obsolete_target
     │ │ │ │ │ │ │ :ref:`regions <sysfs_regions>`/nr_regions
     │ │ │ │ │ │ │ │ :ref:`0 <sysfs_region>`/start,end
     │ │ │ │ │ │ │ │ ...
@@ -264,13 +264,20 @@ to ``N-1``.  Each directory represents each monitoring target.
 targets/<N>/
 ------------
 
-In each target directory, one file (``pid_target``) and one directory
-(``regions``) exist.
+In each target directory, two files (``pid_target`` and ``obsolete_target``)
+and one directory (``regions``) exist.
 
 If you wrote ``vaddr`` to the ``contexts/<N>/operations``, each target should
 be a process.  You can specify the process to DAMON by writing the pid of the
 process to the ``pid_target`` file.
 
+Users can selectively remove targets in the middle of the targets array by
+writing non-zero value to ``obsolete_target`` file and committing it (writing
+``commit`` to ``state`` file).  DAMON will remove the matching targets from its
+internal targets array.  Users are responsible to construct target directories
+again, so that those correctly represent the changed internal targets array.
+
+
 .. _sysfs_regions:
 
 targets/<N>/regions
Signature · 1 line
2.47.3
author maintainer
#7 lore raw

RFC PATCH 5/5 Docs/ABI/damon: document obsolete_target sysfs file

In reply to SeongJae Park

Update DAMON ABI document for the newly added obsolete_target DAMON sysfs file.

---
 Documentation/ABI/testing/sysfs-kernel-mm-damon | 7 +++++++
 1 file changed, 7 insertions(+)
Documentation/ABI/testing/sysfs-kernel-mm-damon +7 -0
index dce6c2cda4e8..4fb8b7a6d625 100644
@@ -164,6 +164,13 @@ Description:	Writing to and reading from this file sets and gets the pid of
 		the target process if the context is for virtual address spaces
 		monitoring, respectively.
 
+What:		/sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/targets/<T>/obsolete_target
+Date:		Oct 2025
+Contact:	SeongJae Park <[email protected]>
+Description:	Writing to and reading from this file sets and gets the
+		obsoleteness of the matching parameters commit destination
+		target.
+
 What:		/sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/targets/<T>/regions/nr_regions
 Date:		Mar 2022
 Contact:	SeongJae Park <[email protected]>
Signature · 1 line
2.47.3