]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm/damon/sysfs: use damos_walk() for update_schemes_tried_{bytes,regions}
authorSeongJae Park <sj@kernel.org>
Fri, 3 Jan 2025 17:43:59 +0000 (09:43 -0800)
committerAndrew Morton <akpm@linux-foundation.org>
Sun, 26 Jan 2025 04:22:26 +0000 (20:22 -0800)
DAMON sysfs interface uses damon_callback with its own complicated
synchronization facility to handle update_schemes_tried_bytes and
update_schemes_tried_regions commands.  But damos_walk() can support the
use case without the additional synchronizations.  Convert the code to use
damos_walk() instead.

Link: https://lkml.kernel.org/r/20250103174400.54890-10-sj@kernel.org
Signed-off-by: SeongJae Park <sj@kernel.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/damon/sysfs-common.h
mm/damon/sysfs-schemes.c
mm/damon/sysfs.c

index e79b4a65ff2d8b1fd99bee81c8c0d669ff69620b..81f1c845118f0595413641171c845348d41d5b5f 100644 (file)
@@ -55,6 +55,11 @@ bool damos_sysfs_regions_upd_done(void);
 
 int damon_sysfs_schemes_update_regions_stop(struct damon_ctx *ctx);
 
+void damos_sysfs_populate_region_dir(struct damon_sysfs_schemes *sysfs_schemes,
+               struct damon_ctx *ctx, struct damon_target *t,
+               struct damon_region *r, struct damos *s,
+               bool total_bytes_only);
+
 int damon_sysfs_schemes_clear_regions(
                struct damon_sysfs_schemes *sysfs_schemes);
 
index c57ab47686ffbc915ea0fe4914025224ef9da20b..1d8ad637051aa594646124f13a5404480cdc439f 100644 (file)
@@ -2183,6 +2183,54 @@ static int damon_sysfs_before_damos_apply(struct damon_ctx *ctx,
        return 0;
 }
 
+/**
+ * damos_sysfs_populate_region_dir() - Populate a schemes tried region dir.
+ * @sysfs_schemes:     Schemes directory to populate regions directory.
+ * @ctx:               Corresponding DAMON context.
+ * @t:                 DAMON target of @r.
+ * @r:                 DAMON region to populate the directory for.
+ * @s:                 Corresponding scheme.
+ * @total_bytes_only:  Whether the request is for bytes update only.
+ *
+ * Called from DAMOS walk callback while holding damon_sysfs_lock.
+ */
+void damos_sysfs_populate_region_dir(struct damon_sysfs_schemes *sysfs_schemes,
+               struct damon_ctx *ctx, struct damon_target *t,
+               struct damon_region *r, struct damos *s, bool total_bytes_only)
+{
+       struct damos *scheme;
+       struct damon_sysfs_scheme_regions *sysfs_regions;
+       struct damon_sysfs_scheme_region *region;
+       int schemes_idx = 0;
+
+       damon_for_each_scheme(scheme, ctx) {
+               if (scheme == s)
+                       break;
+               schemes_idx++;
+       }
+
+       /* user could have removed the scheme sysfs dir */
+       if (schemes_idx >= sysfs_schemes->nr)
+               return;
+
+       sysfs_regions = sysfs_schemes->schemes_arr[schemes_idx]->tried_regions;
+       sysfs_regions->total_bytes += r->ar.end - r->ar.start;
+       if (total_bytes_only)
+               return;
+
+       region = damon_sysfs_scheme_region_alloc(r);
+       if (!region)
+               return;
+       list_add_tail(&region->list, &sysfs_regions->regions_list);
+       sysfs_regions->nr_regions++;
+       if (kobject_init_and_add(&region->kobj,
+                               &damon_sysfs_scheme_region_ktype,
+                               &sysfs_regions->kobj, "%d",
+                               sysfs_regions->nr_regions++)) {
+               kobject_put(&region->kobj);
+       }
+}
+
 /*
  * DAMON callback that called after each accesses sampling.  While this
  * callback is registered, damon_sysfs_lock should be held to ensure the
index 917e6aca3f580cbaa4ae7466835d5504e7d34287..4ba44a314f2e90e6e156b8540a4d809152343d12 100644 (file)
@@ -1512,6 +1512,45 @@ static int damon_sysfs_damon_call(int (*fn)(void *data),
        return damon_call(kdamond->damon_ctx, &call_control);
 }
 
+struct damon_sysfs_schemes_walk_data {
+       struct damon_sysfs_kdamond *sysfs_kdamond;
+       bool total_bytes_only;
+};
+
+/* populate the region directory */
+static void damon_sysfs_schemes_tried_regions_upd_one(void *data, struct damon_ctx *ctx,
+               struct damon_target *t, struct damon_region *r,
+               struct damos *s)
+{
+       struct damon_sysfs_schemes_walk_data *walk_data = data;
+       struct damon_sysfs_kdamond *sysfs_kdamond = walk_data->sysfs_kdamond;
+
+       damos_sysfs_populate_region_dir(
+                       sysfs_kdamond->contexts->contexts_arr[0]->schemes,
+                       ctx, t, r, s, walk_data->total_bytes_only);
+}
+
+static int damon_sysfs_update_schemes_tried_regions(
+               struct damon_sysfs_kdamond *sysfs_kdamond, bool total_bytes_only)
+{
+       struct damon_sysfs_schemes_walk_data walk_data = {
+               .sysfs_kdamond = sysfs_kdamond,
+               .total_bytes_only = total_bytes_only,
+       };
+       struct damos_walk_control control = {
+               .walk_fn = damon_sysfs_schemes_tried_regions_upd_one,
+               .data = &walk_data,
+       };
+       struct damon_ctx *ctx = sysfs_kdamond->damon_ctx;
+
+       if (!ctx)
+               return -EINVAL;
+
+       damon_sysfs_schemes_clear_regions(
+                       sysfs_kdamond->contexts->contexts_arr[0]->schemes);
+       return damos_walk(ctx, &control);
+}
+
 /*
  * damon_sysfs_handle_cmd() - Handle a command for a specific kdamond.
  * @cmd:       The command to handle.
@@ -1542,6 +1581,10 @@ static int damon_sysfs_handle_cmd(enum damon_sysfs_cmd cmd,
        case DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS:
                return damon_sysfs_damon_call(
                                damon_sysfs_upd_schemes_stats, kdamond);
+       case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES:
+               return damon_sysfs_update_schemes_tried_regions(kdamond, true);
+       case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS:
+               return damon_sysfs_update_schemes_tried_regions(kdamond, false);
        case DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS:
                return damon_sysfs_schemes_clear_regions(
                        kdamond->contexts->contexts_arr[0]->schemes);