[PATCH v3 1/3] selftests/damon: prevent cross-context state pollution in DamonCtx
From: SJ Park
Date: Mon Jun 29 2026 - 10:52:37 EST
From: Kunwu Chan <chentao@xxxxxxxxxx>
DamonCtx.__init__() uses mutable default values for
monitoring_attrs, targets, and schemes. In Python these are
evaluated once at function definition time, so multiple
DamonCtx instances can unintentionally share the same lists
and DamonAttrs instance.
Replace the mutable defaults with None sentinels and
initialize the objects when needed.
Link: https://lore.kernel.org/20260601032314.424013-2-kunwu.chan@xxxxxxxxx
Co-developed-by: Wang Lian <lianux.mm@xxxxxxxxx>
Signed-off-by: Wang Lian <lianux.mm@xxxxxxxxx>
Signed-off-by: Kunwu Chan <chentao@xxxxxxxxxx>
Reviewed-by: SJ Park <sj@xxxxxxxxxx>
Cc: Kunwu Chan <chentao@xxxxxxxxxx>
Cc: Wang Lian <lianux.mm@xxxxxxxxx>
Signed-off-by: SJ Park <sj@xxxxxxxxxx>
---
tools/testing/selftests/damon/_damon_sysfs.py | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/damon/_damon_sysfs.py b/tools/testing/selftests/damon/_damon_sysfs.py
index 8b12cc0484405..2f6f2699db256 100644
--- a/tools/testing/selftests/damon/_damon_sysfs.py
+++ b/tools/testing/selftests/damon/_damon_sysfs.py
@@ -624,17 +624,23 @@ class DamonCtx:
pause = None
idx = None
- def __init__(self, ops='paddr', monitoring_attrs=DamonAttrs(), targets=[],
- schemes=[], pause=False):
+ def __init__(self, ops='paddr', monitoring_attrs=None, targets=None,
+ schemes=None, pause=False):
self.ops = ops
+ if monitoring_attrs is None:
+ monitoring_attrs = DamonAttrs()
self.monitoring_attrs = monitoring_attrs
self.monitoring_attrs.context = self
+ if targets is None:
+ targets = []
self.targets = targets
for idx, target in enumerate(self.targets):
target.idx = idx
target.context = self
+ if schemes is None:
+ schemes = []
self.schemes = schemes
for idx, scheme in enumerate(self.schemes):
scheme.idx = idx
--
2.47.3