[RFC PATCH v3 20/37] mm/damon/sysfs: implement sample filter directory

SeongJae Park
2025-12-08T06:30:19Z

Implement sysfs directory for setting individual DAMON sample filters.

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

diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index 4208fed2b8df..ba0c76c1300c 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -749,12 +749,46 @@ static const struct kobj_type damon_sysfs_intervals_ktype = {
.default_groups = damon_sysfs_intervals_groups,
};

+/*
+ * access check report filter directory
+ */
+
+struct damon_sysfs_sample_filter {
+ struct kobject kobj;
+};
+
+static struct damon_sysfs_sample_filter *damon_sysfs_sample_filter_alloc(void)
+{
+ return kzalloc(sizeof(struct damon_sysfs_sample_filter), GFP_KERNEL);
+}
+
+static void damon_sysfs_sample_filter_release(struct kobject *kobj)
+{
+ struct damon_sysfs_sample_filter *filter = container_of(kobj,
+ struct damon_sysfs_sample_filter, kobj);
+
+ kfree(filter);
+}
+
+static struct attribute *damon_sysfs_sample_filter_attrs[] = {
+ NULL,
+};
+ATTRIBUTE_GROUPS(damon_sysfs_sample_filter);
+
+static const struct kobj_type damon_sysfs_sample_filter_ktype = {
+ .release = damon_sysfs_sample_filter_release,
+ .sysfs_ops = &kobj_sysfs_ops,
+ .default_groups = damon_sysfs_sample_filter_groups,
+};
+
/*
* access check report filters directory
*/

struct damon_sysfs_sample_filters {
struct kobject kobj;
+ struct damon_sysfs_sample_filter **filters_arr;
+ int nr;
};

static struct damon_sysfs_sample_filters *
@@ -763,12 +797,99 @@ damon_sysfs_sample_filters_alloc(void)
return kzalloc(sizeof(struct damon_sysfs_sample_filters), GFP_KERNEL);
}

+static void damon_sysfs_sample_filters_rm_dirs(
+ struct damon_sysfs_sample_filters *filters)
+{
+ struct damon_sysfs_sample_filter **filters_arr = filters->filters_arr;
+ int i;
+
+ for (i = 0; i < filters->nr; i++)
+ kobject_put(&filters_arr[i]->kobj);
+ filters->nr = 0;
+ kfree(filters_arr);
+ filters->filters_arr = NULL;
+}
+
+static int damon_sysfs_sample_filters_add_dirs(
+ struct damon_sysfs_sample_filters *filters, int nr_filters)
+{
+ struct damon_sysfs_sample_filter **filters_arr, *filter;
+ int err, i;
+
+ damon_sysfs_sample_filters_rm_dirs(filters);
+ if (!nr_filters)
+ return 0;
+
+ filters_arr = kmalloc_array(nr_filters, sizeof(*filters_arr),
+ GFP_KERNEL | __GFP_NOWARN);
+ if (!filters_arr)
+ return -ENOMEM;
+ filters->filters_arr = filters_arr;
+
+ for (i = 0; i < nr_filters; i++) {
+ filter = damon_sysfs_sample_filter_alloc();
+ if (!filter) {
+ damon_sysfs_sample_filters_rm_dirs(filters);
+ return -ENOMEM;
+ }
+
+ err = kobject_init_and_add(&filter->kobj,
+ &damon_sysfs_sample_filter_ktype,
+ &filters->kobj, "%d", i);
+ if (err) {
+ kobject_put(&filter->kobj);
+ damon_sysfs_sample_filters_rm_dirs(filters);
+ return err;
+ }
+
+ filters_arr[i] = filter;
+ filters->nr++;
+ }
+ return 0;
+}
+
+static ssize_t nr_filters_show(struct kobject *kobj,
+ struct kobj_attribute *attr, char *buf)
+{
+ struct damon_sysfs_sample_filters *filters = container_of(kobj,
+ struct damon_sysfs_sample_filters, kobj);
+
+ return sysfs_emit(buf, "%d\n", filters->nr);
+}
+
+static ssize_t nr_filters_store(struct kobject *kobj,
+ struct kobj_attribute *attr, const char *buf, size_t count)
+{
+ struct damon_sysfs_sample_filters *filters;
+ int nr, err = kstrtoint(buf, 0, &nr);
+
+ if (err)
+ return err;
+ if (nr < 0)
+ return -EINVAL;
+
+ filters = container_of(kobj, struct damon_sysfs_sample_filters, kobj);
+
+ if (!mutex_trylock(&damon_sysfs_lock))
+ return -EBUSY;
+ err = damon_sysfs_sample_filters_add_dirs(filters, nr);
+ mutex_unlock(&damon_sysfs_lock);
+ if (err)
+ return err;
+
+ return count;
+}
+
static void damon_sysfs_sample_filters_release(struct kobject *kobj)
{
kfree(container_of(kobj, struct damon_sysfs_sample_filters, kobj));
}

+static struct kobj_attribute damon_sysfs_sample_filters_nr_attr =
+ __ATTR_RW_MODE(nr_filters, 0600);
+
static struct attribute *damon_sysfs_sample_filters_attrs[] = {
+ &damon_sysfs_sample_filters_nr_attr.attr,
NULL,
};
ATTRIBUTE_GROUPS(damon_sysfs_sample_filters);
@@ -940,6 +1061,7 @@ static void damon_sysfs_sample_rm_dirs(
if (sample->primitives)
kobject_put(&sample->primitives->kobj);
if (sample->filters) {
+ damon_sysfs_sample_filters_rm_dirs(sample->filters);
kobject_put(&sample->filters->kobj);
}
}
--
2.47.3