[RFC PATCH -next 07/16] mm/damon/sysfs: implement addr_unit file under context dir

Quanmin Yan posted 16 patches 1 month, 3 weeks ago
There is a newer version of this series
[RFC PATCH -next 07/16] mm/damon/sysfs: implement addr_unit file under context dir
Posted by Quanmin Yan 1 month, 3 weeks ago
From: SeongJae Park <sj@kernel.org>

Only DAMON kernel API callers can use addr_unit parameter.  Implement a
sysfs file to let DAMON sysfs ABI users use it.

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

diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index 6d2b0dab50cb..bea782b0a711 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -834,6 +834,7 @@ static const struct damon_sysfs_ops_name damon_sysfs_ops_names[] = {
 struct damon_sysfs_context {
 	struct kobject kobj;
 	enum damon_ops_id ops_id;
+	unsigned long addr_unit;
 	struct damon_sysfs_attrs *attrs;
 	struct damon_sysfs_targets *targets;
 	struct damon_sysfs_schemes *schemes;
@@ -849,6 +850,7 @@ static struct damon_sysfs_context *damon_sysfs_context_alloc(
 		return NULL;
 	context->kobj = (struct kobject){};
 	context->ops_id = ops_id;
+	context->addr_unit = 1;
 	return context;
 }
 
@@ -997,6 +999,25 @@ static ssize_t operations_store(struct kobject *kobj,
 	return -EINVAL;
 }
 
+static ssize_t addr_unit_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, "%lu\n", context->addr_unit);
+}
+
+static ssize_t addr_unit_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);
+	int err = kstrtoul(buf, 0, &context->addr_unit);
+
+	return err ? err : count;
+}
+
 static void damon_sysfs_context_release(struct kobject *kobj)
 {
 	kfree(container_of(kobj, struct damon_sysfs_context, kobj));
@@ -1008,9 +1029,13 @@ static struct kobj_attribute damon_sysfs_context_avail_operations_attr =
 static struct kobj_attribute damon_sysfs_context_operations_attr =
 		__ATTR_RW_MODE(operations, 0600);
 
+static struct kobj_attribute damon_sysfs_context_addr_unit_attr =
+		__ATTR_RW_MODE(addr_unit, 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,
 	NULL,
 };
 ATTRIBUTE_GROUPS(damon_sysfs_context);
@@ -1397,6 +1422,7 @@ static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
 	err = damon_select_ops(ctx, sys_ctx->ops_id);
 	if (err)
 		return err;
+	ctx->addr_unit = sys_ctx->addr_unit;
 	err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
 	if (err)
 		return err;
-- 
2.34.1
Re: [RFC PATCH -next 07/16] mm/damon/sysfs: implement addr_unit file under context dir
Posted by SeongJae Park 1 month, 2 weeks ago
Hi Quanmin,

On Wed, 13 Aug 2025 13:06:57 +0800 Quanmin Yan <yanquanmin1@huawei.com> wrote:

> From: SeongJae Park <sj@kernel.org>
> 
> Only DAMON kernel API callers can use addr_unit parameter.  Implement a
> sysfs file to let DAMON sysfs ABI users use it.

As I suggested on another reply[1], please squash attaching patch to this one
when you post next version of this series.

[1] https://lore.kernel.org/20250813170224.6128-1-sj@kernel.org

[...]


Thanks,
SJ

==== Attachment 0 (0001-mm-damon-sysfs-return-EINVAL-for-zero-addr_unit.patch) ====
From e0a5aa5e571ecd0f58b0914f8fc8562a60014ae8 Mon Sep 17 00:00:00 2001
From: SeongJae Park <sj@kernel.org>
Date: Wed, 13 Aug 2025 21:17:03 -0700
Subject: [PATCH] mm/damon/sysfs: return -EINVAL for zero addr_unit

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

diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index bea782b0a711..eb4269383bae 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -1422,6 +1422,8 @@ static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
 	err = damon_select_ops(ctx, sys_ctx->ops_id);
 	if (err)
 		return err;
+	if (!sys_ctx->addr_unit)
+		return -EINVAL;
 	ctx->addr_unit = sys_ctx->addr_unit;
 	err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
 	if (err)
-- 
2.39.5
Re: [RFC PATCH -next 07/16] mm/damon/sysfs: implement addr_unit file under context dir
Posted by Quanmin Yan 1 month, 2 weeks ago
Hi SJ,

在 2025/8/19 14:24, SeongJae Park 写道:
> Hi Quanmin,
>
> As I suggested on another reply[1], please squash attaching patch to this one
> when you post next version of this series.
>
> [1] https://lore.kernel.org/20250813170224.6128-1-sj@kernel.org
>
> [...]
>
>
> Thanks,
> SJ
>
> ==== Attachment 0 (0001-mm-damon-sysfs-return-EINVAL-for-zero-addr_unit.patch) ====
>  From e0a5aa5e571ecd0f58b0914f8fc8562a60014ae8 Mon Sep 17 00:00:00 2001
> From: SeongJae Park <sj@kernel.org>
> Date: Wed, 13 Aug 2025 21:17:03 -0700
> Subject: [PATCH] mm/damon/sysfs: return -EINVAL for zero addr_unit
>
> Signed-off-by: SeongJae Park <sj@kernel.org>
> ---
>   mm/damon/sysfs.c | 2 ++
>   1 file changed, 2 insertions(+)
>
> diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
> index bea782b0a711..eb4269383bae 100644
> --- a/mm/damon/sysfs.c
> +++ b/mm/damon/sysfs.c
> @@ -1422,6 +1422,8 @@ static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
>   	err = damon_select_ops(ctx, sys_ctx->ops_id);
>   	if (err)
>   		return err;
> +	if (!sys_ctx->addr_unit)
> +		return -EINVAL;
>   	ctx->addr_unit = sys_ctx->addr_unit;
>   	err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
>   	if (err)

Aha, returning -EINVAL when sys_ctx->addr_unit is 0 makes sense, but I wonder if it
might be better to prevent users from inputting 0 at the source instead? I've attempted
to modify patch 7 by adding a check in addr_unit_store. I'll send out the v2 version
of patch 7 later (PS: I am performing a comprehensive validation of the v2 patch series),
and we can discuss it then.

Thanks,
Quanmin Yan

Re: [RFC PATCH -next 07/16] mm/damon/sysfs: implement addr_unit file under context dir
Posted by SeongJae Park 1 month, 2 weeks ago
On Tue, 19 Aug 2025 22:45:58 +0800 Quanmin Yan <yanquanmin1@huawei.com> wrote:

> Hi SJ,
> 
> 在 2025/8/19 14:24, SeongJae Park 写道:
> > Hi Quanmin,
> >
> > As I suggested on another reply[1], please squash attaching patch to this one
> > when you post next version of this series.
> >
> > [1] https://lore.kernel.org/20250813170224.6128-1-sj@kernel.org
> >
> > [...]
> >
> >
> > Thanks,
> > SJ
> >
> > ==== Attachment 0 (0001-mm-damon-sysfs-return-EINVAL-for-zero-addr_unit.patch) ====
> >  From e0a5aa5e571ecd0f58b0914f8fc8562a60014ae8 Mon Sep 17 00:00:00 2001
> > From: SeongJae Park <sj@kernel.org>
> > Date: Wed, 13 Aug 2025 21:17:03 -0700
> > Subject: [PATCH] mm/damon/sysfs: return -EINVAL for zero addr_unit
> >
> > Signed-off-by: SeongJae Park <sj@kernel.org>
> > ---
> >   mm/damon/sysfs.c | 2 ++
> >   1 file changed, 2 insertions(+)
> >
> > diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
> > index bea782b0a711..eb4269383bae 100644
> > --- a/mm/damon/sysfs.c
> > +++ b/mm/damon/sysfs.c
> > @@ -1422,6 +1422,8 @@ static int damon_sysfs_apply_inputs(struct damon_ctx *ctx,
> >   	err = damon_select_ops(ctx, sys_ctx->ops_id);
> >   	if (err)
> >   		return err;
> > +	if (!sys_ctx->addr_unit)
> > +		return -EINVAL;
> >   	ctx->addr_unit = sys_ctx->addr_unit;
> >   	err = damon_sysfs_set_attrs(ctx, sys_ctx->attrs);
> >   	if (err)
> 
> Aha, returning -EINVAL when sys_ctx->addr_unit is 0 makes sense, but I wonder if it
> might be better to prevent users from inputting 0 at the source instead? I've attempted
> to modify patch 7 by adding a check in addr_unit_store.

I agree that is better.  Please don't forget adding your Signed-off-by: to the
patch.

> I'll send out the v2 version
> of patch 7 later (PS: I am performing a comprehensive validation of the v2 patch series),
> and we can discuss it then.

Looking forwrd to the patch! :)


Thanks,
SJ

[...]