]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm/damon/sysfs-schemes: implement DAMOS action destinations directory
authorSeongJae Park <sj@kernel.org>
Wed, 9 Jul 2025 00:59:33 +0000 (19:59 -0500)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 20 Jul 2025 01:59:48 +0000 (18:59 -0700)
DAMOS_MIGRATE_{HOT,COLD} can have multiple action destinations and their
weights.  Implement sysfs directory named 'dests' under each scheme
directory to let DAMON sysfs ABI users utilize the feature.  The interface
is similar to other multiple parameters directory like kdamonds or
filters.  The directory contains only nr_dests file initially.  Writing a
number of desired destinations to nr_dests creates directories of the
number.  Each of the created directories has two files named id and
weight.  Users can then write the destination's identifier (node id in
case of DAMOS_MIGRATE_*) and weight to the files.

Link: https://lkml.kernel.org/r/20250709005952.17776-4-bijan311@gmail.com
Signed-off-by: SeongJae Park <sj@kernel.org>
Cc: Bijan Tabatabai <bijantabatab@micron.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Ravi Shankar Jonnalagadda <ravis.opensrc@micron.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/damon/sysfs-schemes.c

index 601360e9b521eed4f118b0021284784b76686900..b9434cdaacdc084fb49efcbd20cbcd0590f1dae4 100644 (file)
@@ -1655,6 +1655,204 @@ static const struct kobj_type damon_sysfs_access_pattern_ktype = {
        .default_groups = damon_sysfs_access_pattern_groups,
 };
 
+/*
+ * dest (action destination) directory
+ */
+
+struct damos_sysfs_dest {
+       struct kobject kobj;
+       unsigned int id;
+       unsigned int weight;
+};
+
+static struct damos_sysfs_dest *damos_sysfs_dest_alloc(void)
+{
+       return kzalloc(sizeof(struct damos_sysfs_dest), GFP_KERNEL);
+}
+
+static ssize_t id_show(
+               struct kobject *kobj, struct kobj_attribute *attr, char *buf)
+{
+       struct damos_sysfs_dest *dest = container_of(kobj,
+                       struct damos_sysfs_dest, kobj);
+
+       return sysfs_emit(buf, "%u\n", dest->id);
+}
+
+static ssize_t id_store(struct kobject *kobj,
+               struct kobj_attribute *attr, const char *buf, size_t count)
+{
+       struct damos_sysfs_dest *dest = container_of(kobj,
+                       struct damos_sysfs_dest, kobj);
+       int err = kstrtouint(buf, 0, &dest->id);
+
+       return err ? err : count;
+}
+
+static ssize_t weight_show(
+               struct kobject *kobj, struct kobj_attribute *attr, char *buf)
+{
+       struct damos_sysfs_dest *dest = container_of(kobj,
+                       struct damos_sysfs_dest, kobj);
+
+       return sysfs_emit(buf, "%u\n", dest->weight);
+}
+
+static ssize_t weight_store(struct kobject *kobj,
+               struct kobj_attribute *attr, const char *buf, size_t count)
+{
+       struct damos_sysfs_dest *dest = container_of(kobj,
+                       struct damos_sysfs_dest, kobj);
+       int err = kstrtouint(buf, 0, &dest->weight);
+
+       return err ? err : count;
+}
+
+static void damos_sysfs_dest_release(struct kobject *kobj)
+{
+       struct damos_sysfs_dest *dest = container_of(kobj,
+                       struct damos_sysfs_dest, kobj);
+       kfree(dest);
+}
+
+static struct kobj_attribute damos_sysfs_dest_id_attr =
+               __ATTR_RW_MODE(id, 0600);
+
+static struct kobj_attribute damos_sysfs_dest_weight_attr =
+               __ATTR_RW_MODE(weight, 0600);
+
+static struct attribute *damos_sysfs_dest_attrs[] = {
+       &damos_sysfs_dest_id_attr.attr,
+       &damos_sysfs_dest_weight_attr.attr,
+       NULL,
+};
+ATTRIBUTE_GROUPS(damos_sysfs_dest);
+
+static const struct kobj_type damos_sysfs_dest_ktype = {
+       .release = damos_sysfs_dest_release,
+       .sysfs_ops = &kobj_sysfs_ops,
+       .default_groups = damos_sysfs_dest_groups,
+};
+
+/*
+ * dests (action destinations) directory
+ */
+
+struct damos_sysfs_dests {
+       struct kobject kobj;
+       struct damos_sysfs_dest **dests_arr;
+       int nr;
+};
+
+static struct damos_sysfs_dests *
+damos_sysfs_dests_alloc(void)
+{
+       return kzalloc(sizeof(struct damos_sysfs_dests), GFP_KERNEL);
+}
+
+static void damos_sysfs_dests_rm_dirs(
+               struct damos_sysfs_dests *dests)
+{
+       struct damos_sysfs_dest **dests_arr = dests->dests_arr;
+       int i;
+
+       for (i = 0; i < dests->nr; i++)
+               kobject_put(&dests_arr[i]->kobj);
+       dests->nr = 0;
+       kfree(dests_arr);
+       dests->dests_arr = NULL;
+}
+
+static int damos_sysfs_dests_add_dirs(
+               struct damos_sysfs_dests *dests, int nr_dests)
+{
+       struct damos_sysfs_dest **dests_arr, *dest;
+       int err, i;
+
+       damos_sysfs_dests_rm_dirs(dests);
+       if (!nr_dests)
+               return 0;
+
+       dests_arr = kmalloc_array(nr_dests, sizeof(*dests_arr),
+                       GFP_KERNEL | __GFP_NOWARN);
+       if (!dests_arr)
+               return -ENOMEM;
+       dests->dests_arr = dests_arr;
+
+       for (i = 0; i < nr_dests; i++) {
+               dest = damos_sysfs_dest_alloc();
+               if (!dest) {
+                       damos_sysfs_dests_rm_dirs(dests);
+                       return -ENOMEM;
+               }
+
+               err = kobject_init_and_add(&dest->kobj,
+                               &damos_sysfs_dest_ktype,
+                               &dests->kobj, "%d", i);
+               if (err) {
+                       kobject_put(&dest->kobj);
+                       damos_sysfs_dests_rm_dirs(dests);
+                       return err;
+               }
+
+               dests_arr[i] = dest;
+               dests->nr++;
+       }
+       return 0;
+}
+
+static ssize_t nr_dests_show(struct kobject *kobj,
+               struct kobj_attribute *attr, char *buf)
+{
+       struct damos_sysfs_dests *dests = container_of(kobj,
+                       struct damos_sysfs_dests, kobj);
+
+       return sysfs_emit(buf, "%d\n", dests->nr);
+}
+
+static ssize_t nr_dests_store(struct kobject *kobj,
+               struct kobj_attribute *attr, const char *buf, size_t count)
+{
+       struct damos_sysfs_dests *dests;
+       int nr, err = kstrtoint(buf, 0, &nr);
+
+       if (err)
+               return err;
+       if (nr < 0)
+               return -EINVAL;
+
+       dests = container_of(kobj, struct damos_sysfs_dests, kobj);
+
+       if (!mutex_trylock(&damon_sysfs_lock))
+               return -EBUSY;
+       err = damos_sysfs_dests_add_dirs(dests, nr);
+       mutex_unlock(&damon_sysfs_lock);
+       if (err)
+               return err;
+
+       return count;
+}
+
+static void damos_sysfs_dests_release(struct kobject *kobj)
+{
+       kfree(container_of(kobj, struct damos_sysfs_dests, kobj));
+}
+
+static struct kobj_attribute damos_sysfs_dests_nr_attr =
+               __ATTR_RW_MODE(nr_dests, 0600);
+
+static struct attribute *damos_sysfs_dests_attrs[] = {
+       &damos_sysfs_dests_nr_attr.attr,
+       NULL,
+};
+ATTRIBUTE_GROUPS(damos_sysfs_dests);
+
+static const struct kobj_type damos_sysfs_dests_ktype = {
+       .release = damos_sysfs_dests_release,
+       .sysfs_ops = &kobj_sysfs_ops,
+       .default_groups = damos_sysfs_dests_groups,
+};
+
 /*
  * scheme directory
  */
@@ -1672,6 +1870,7 @@ struct damon_sysfs_scheme {
        struct damon_sysfs_stats *stats;
        struct damon_sysfs_scheme_regions *tried_regions;
        int target_nid;
+       struct damos_sysfs_dests *dests;
 };
 
 struct damos_sysfs_action_name {
@@ -1762,6 +1961,22 @@ out:
        return err;
 }
 
+static int damos_sysfs_set_dests(struct damon_sysfs_scheme *scheme)
+{
+       struct damos_sysfs_dests *dests = damos_sysfs_dests_alloc();
+       int err;
+
+       if (!dests)
+               return -ENOMEM;
+       err = kobject_init_and_add(&dests->kobj, &damos_sysfs_dests_ktype,
+                       &scheme->kobj, "dests");
+       if (err)
+               kobject_put(&dests->kobj);
+       else
+               scheme->dests = dests;
+       return err;
+}
+
 static int damon_sysfs_scheme_set_quotas(struct damon_sysfs_scheme *scheme)
 {
        struct damon_sysfs_quotas *quotas = damon_sysfs_quotas_alloc();
@@ -1894,9 +2109,12 @@ static int damon_sysfs_scheme_add_dirs(struct damon_sysfs_scheme *scheme)
        err = damon_sysfs_scheme_set_access_pattern(scheme);
        if (err)
                return err;
-       err = damon_sysfs_scheme_set_quotas(scheme);
+       err = damos_sysfs_set_dests(scheme);
        if (err)
                goto put_access_pattern_out;
+       err = damon_sysfs_scheme_set_quotas(scheme);
+       if (err)
+               goto put_dests_out;
        err = damon_sysfs_scheme_set_watermarks(scheme);
        if (err)
                goto put_quotas_access_pattern_out;
@@ -1927,6 +2145,9 @@ put_watermarks_quotas_access_pattern_out:
 put_quotas_access_pattern_out:
        kobject_put(&scheme->quotas->kobj);
        scheme->quotas = NULL;
+put_dests_out:
+       kobject_put(&scheme->dests->kobj);
+       scheme->dests = NULL;
 put_access_pattern_out:
        kobject_put(&scheme->access_pattern->kobj);
        scheme->access_pattern = NULL;
@@ -1937,6 +2158,8 @@ static void damon_sysfs_scheme_rm_dirs(struct damon_sysfs_scheme *scheme)
 {
        damon_sysfs_access_pattern_rm_dirs(scheme->access_pattern);
        kobject_put(&scheme->access_pattern->kobj);
+       kobject_put(&scheme->dests->kobj);
+       damos_sysfs_dests_rm_dirs(scheme->dests);
        damon_sysfs_quotas_rm_dirs(scheme->quotas);
        kobject_put(&scheme->quotas->kobj);
        kobject_put(&scheme->watermarks->kobj);