[RFC PATCH 02/10] mm/damon/sysfs: add pause file under context dir

SeongJae Park posted 10 patches 3 weeks, 1 day ago
There is a newer version of this series
[RFC PATCH 02/10] mm/damon/sysfs: add pause file under context dir
Posted by SeongJae Park 3 weeks, 1 day ago
Add pause DAMON sysfs file under the context directory.  It exposes the
damon_ctx->pause API parameter to the users so that they can use the
pause/resume feature.

Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/sysfs.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index 576d1ddd736bf..4cbb8b9aaba3c 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -866,6 +866,7 @@ struct damon_sysfs_context {
 	struct damon_sysfs_attrs *attrs;
 	struct damon_sysfs_targets *targets;
 	struct damon_sysfs_schemes *schemes;
+	bool pause;
 };
 
 static struct damon_sysfs_context *damon_sysfs_context_alloc(
@@ -1053,6 +1054,30 @@ static ssize_t addr_unit_store(struct kobject *kobj,
 	return count;
 }
 
+static ssize_t pause_show(struct kobject *kobj, struct kobj_attribute *attr,
+		char *buf)
+{
+	struct damon_sysfs_context *context = container_of(kobj,
+			struct damon_sysfs_context, kobj);
+
+	return sysfs_emit(buf, "%c\n", context->pause ? 'Y' : 'N');
+}
+
+static ssize_t pause_store(struct kobject *kobj, struct kobj_attribute *attr,
+		const char *buf, size_t count)
+{
+	struct damon_sysfs_context *context = container_of(kobj,
+			struct damon_sysfs_context, kobj);
+	bool pause;
+	int err = kstrtobool(buf, &pause);
+
+	if (err)
+		return err;
+	context->pause = pause;
+	return count;
+}
+
+
 static void damon_sysfs_context_release(struct kobject *kobj)
 {
 	kfree(container_of(kobj, struct damon_sysfs_context, kobj));
@@ -1067,10 +1092,14 @@ static struct kobj_attribute damon_sysfs_context_operations_attr =
 static struct kobj_attribute damon_sysfs_context_addr_unit_attr =
 		__ATTR_RW_MODE(addr_unit, 0600);
 
+static struct kobj_attribute damon_sysfs_context_pause_attr =
+		__ATTR_RW_MODE(pause, 0600);
+
 static struct attribute *damon_sysfs_context_attrs[] = {
 	&damon_sysfs_context_avail_operations_attr.attr,
 	&damon_sysfs_context_operations_attr.attr,
 	&damon_sysfs_context_addr_unit_attr.attr,
+	&damon_sysfs_context_pause_attr.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(damon_sysfs_context);
@@ -1470,6 +1499,7 @@ static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
 	if (sys_ctx->ops_id == DAMON_OPS_PADDR)
 		ctx->min_region_sz = max(
 				DAMON_MIN_REGION_SZ / sys_ctx->addr_unit, 1);
+	ctx->pause = sys_ctx->pause;
 	err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
 	if (err)
 		return err;
-- 
2.47.3
Re: [RFC PATCH 02/10] mm/damon/sysfs: add pause file under context dir
Posted by SeongJae Park 3 weeks ago
On Sun, 15 Mar 2026 14:00:01 -0700 SeongJae Park <sj@kernel.org> wrote:

> Add pause DAMON sysfs file under the context directory.  It exposes the
> damon_ctx->pause API parameter to the users so that they can use the
> pause/resume feature.
> 
> Signed-off-by: SeongJae Park <sj@kernel.org>
> ---
>  mm/damon/sysfs.c | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
> 
> diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
> index 576d1ddd736bf..4cbb8b9aaba3c 100644
> --- a/mm/damon/sysfs.c
> +++ b/mm/damon/sysfs.c
> @@ -866,6 +866,7 @@ struct damon_sysfs_context {
>  	struct damon_sysfs_attrs *attrs;
>  	struct damon_sysfs_targets *targets;
>  	struct damon_sysfs_schemes *schemes;
> +	bool pause;
>  };

sashiko.dev comments [1] below.

: Is the new pause field left uninitialized when a context is allocated?
: 
: Looking at damon_sysfs_context_alloc(), memory is allocated via kmalloc_obj()
: which does not zero-fill by default, and the new field is not explicitly
: initialized:
: 
: static struct damon_sysfs_context *damon_sysfs_context_alloc(
:                 enum damon_ops_id ops_id)
: {
:         struct damon_sysfs_context *context = kmalloc_obj(*context);
: 
:         if (!context)
:                 return NULL;
:         context->kobj = (struct kobject){};
:         context->ops_id = ops_id;
:         context->addr_unit = 1;
:         return context;
: }
: 
: If a user reads the pause sysfs file before writing to it, could this return
: uninitialized kernel heap memory?

Good catch.  I will add below fixup to the next spin.

'''
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -1432,6 +1432,7 @@ static struct damon_sysfs_context *damon_sysfs_context_alloc(
        context->kobj = (struct kobject){};
        context->ops_id = ops_id;
        context->addr_unit = 1;
+       context->pause = false;
        return context;
 }
'''

Btw, somehow sashiko.dev added the comment to not this patch but the sixth
patch of this series.

[1] https://sashiko.dev/#/patchset/20260315210012.94846-7-sj@kernel.org


Thanks,
SJ

[...]