Skip to contentSkip to conversation

mm/damon/sysfs: fix unexpected targets adding bug

PATCH Applied SeongJae Park 1 participant 4 messages Oct 22, 2023 – Oct 28, 2023

Subsystems (4)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

cover mm/damon/sysfs: fix unexpected targets adding bug

The sysfs code for online targets updating can result in adding more than expected monigoring targets to the context. It can result in unexpected amount of memory consumption and monitoring overhead. This patchset fixes the issue (patch 1), and add a kunit test for avoiding similar bug of future (patch 2).

SeongJae Park (2):
mm/damon/sysfs: remove requested targets when online-commit inputs mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets()

 mm/damon/Kconfig      | 12 ++++++
 mm/damon/sysfs-test.h | 86 +++++++++++++++++++++++++++++++++++++++++++
 mm/damon/sysfs.c      | 52 ++++++--------------------
 3 files changed, 109 insertions(+), 41 deletions(-)
 create mode 100644 mm/damon/sysfs-test.h

base-commit: 9a969da6ffb9609f5fa8d0b7fdc6859c37a10335

Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 1/2 mm/damon/sysfs: remove requested targets when online-commit inputs

damon_sysfs_set_targets(), which updates the targets of the context for online commitment, do not remove targets that removed from the corresponding sysfs files. As a result, more than intended targets of the context can exist and hence consume memory and monitoring CPU resource more than expected.

Fix it by removing all targets of the context and fill up again using the user input. This could cause unnecessary memory dealloc and realloc operations, but this is not a hot code path. Also, note that damon_target is stateless, and hence no data is lost.

Fixes da87878010e5 ("mm/damon/sysfs: support online inputs update")
Cc <[email protected]> # 5.19.x
---
 mm/damon/sysfs.c | 50 +++++++++---------------------------------------
 1 file changed, 9 insertions(+), 41 deletions(-)
mm/damon/sysfs.c +9 -41
index f73dc88d2d19..5268e8503722 100644
@@ -1150,58 +1150,26 @@ static int damon_sysfs_add_target(struct damon_sysfs_target *sys_target,
 	return err;
 }
 
-/*
- * Search a target in a context that corresponds to the sysfs target input.
- *
- * Return: pointer to the target if found, NULL if not found, or negative
- * error code if the search failed.
- */
-static struct damon_target *damon_sysfs_existing_target(
-		struct damon_sysfs_target *sys_target, struct damon_ctx *ctx)
-{
-	struct pid *pid;
-	struct damon_target *t;
-
-	if (!damon_target_has_pid(ctx)) {
-		/* Up to only one target for paddr could exist */
-		damon_for_each_target(t, ctx)
-			return t;
-		return NULL;
-	}
-
-	/* ops.id should be DAMON_OPS_VADDR or DAMON_OPS_FVADDR */
-	pid = find_get_pid(sys_target->pid);
-	if (!pid)
-		return ERR_PTR(-EINVAL);
-	damon_for_each_target(t, ctx) {
-		if (t->pid == pid) {
-			put_pid(pid);
-			return t;
-		}
-	}
-	put_pid(pid);
-	return NULL;
-}
-
 static int damon_sysfs_set_targets(struct damon_ctx *ctx,
 		struct damon_sysfs_targets *sysfs_targets)
 {
+	struct damon_target *t, *next;
 	int i, err;
 
 	/* Multiple physical address space monitoring targets makes no sense */
 	if (ctx->ops.id == DAMON_OPS_PADDR && sysfs_targets->nr > 1)
 		return -EINVAL;
 
+	damon_for_each_target_safe(t, next, ctx) {
+		if (damon_target_has_pid(ctx))
+			put_pid(t->pid);
+		damon_destroy_target(t);
+	}
+
 	for (i = 0; i < sysfs_targets->nr; i++) {
 		struct damon_sysfs_target *st = sysfs_targets->targets_arr[i];
-		struct damon_target *t = damon_sysfs_existing_target(st, ctx);
-
-		if (IS_ERR(t))
-			return PTR_ERR(t);
-		if (!t)
-			err = damon_sysfs_add_target(st, ctx);
-		else
-			err = damon_sysfs_set_regions(t, st->regions);
+
+		err = damon_sysfs_add_target(st, ctx);
 		if (err)
 			return err;
 	}
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

Re: [PATCH 1/2] mm/damon/sysfs: remove requested targets when online-commit inputs

Quoted 10 lines · On Sun, 22 Oct 2023 21:07:33 +0000 SeongJae Park <[email protected]> wrote:

damon_sysfs_set_targets(), which updates the targets of the context for online commitment, do not remove targets that removed from the corresponding sysfs files. As a result, more than intended targets of the context can exist and hence consume memory and monitoring CPU resource more than expected.

Fix it by removing all targets of the context and fill up again using the user input. This could cause unnecessary memory dealloc and realloc operations, but this is not a hot code path. Also, note that damon_target is stateless, and hence no data is lost.

This is not true. 'struct damon_target' contains monitoring results (regions_list). Hence, this patch makes all monitoring results to be removed whenever doing online-commit. I was confused with init_regions at the time of this writing, sorry.

I will send a fix for this soon.

Thanks,
SJ

author maintainer
#3 lore raw

PATCH 2/2 mm/damon/sysfs-test: add a unit test for damon_sysfs_set_targets()

In reply to SeongJae Park

damon_sysfs_set_targets() had a bug that can result in unexpected memory usage and monitoring overhead increase. The bug has fixed by a previous commit. Add a unit test for avoiding a similar bug of future.

---
 mm/damon/Kconfig      | 12 ++++++
 mm/damon/sysfs-test.h | 86 +++++++++++++++++++++++++++++++++++++++++++
 mm/damon/sysfs.c      |  2 +
 3 files changed, 100 insertions(+)
 create mode 100644 mm/damon/sysfs-test.h
mm/damon/Kconfig +12 -0
index 436c6b4cb5ec..29f43fbc2eff 100644
@@ -59,6 +59,18 @@ config DAMON_SYSFS
 	  This builds the sysfs interface for DAMON.  The user space can use
 	  the interface for arbitrary data access monitoring.
 
+config DAMON_SYSFS_KUNIT_TEST
+	bool "Test for damon debugfs interface" if !KUNIT_ALL_TESTS
+	depends on DAMON_SYSFS && KUNIT=y
+	default KUNIT_ALL_TESTS
+	help
+	  This builds the DAMON sysfs interface Kunit test suite.
+
+	  For more information on KUnit and unit tests in general, please refer
+	  to the KUnit documentation.
+
+	  If unsure, say N.
+
 config DAMON_DBGFS
 	bool "DAMON debugfs interface (DEPRECATED!)"
 	depends on DAMON_VADDR && DAMON_PADDR && DEBUG_FS
mm/damon/sysfs-test.h new file +86 -0

86 lines not on this page · raw message

mm/damon/sysfs.c +2 -0
index 5268e8503722..d13e353bee52 100644
@@ -1804,3 +1804,5 @@ static int __init damon_sysfs_init(void)
 	return err;
 }
 subsys_initcall(damon_sysfs_init);
+
+#include "sysfs-test.h"
Signature · 1 line
2.34.1