Three functions (damos_apply_scheme, damos_trace_esz, damos_trace_stat) contain identical loops walking the schemes list to find a scheme's index for tracing. Extract the common pattern into damon_scheme_idx(). Signed-off-by: Josh Law --- mm/damon/core.c | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/mm/damon/core.c b/mm/damon/core.c index 6ee421141996..56372e577931 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -2045,6 +2045,19 @@ static void damos_walk_cancel(struct damon_ctx *ctx) mutex_unlock(&ctx->walk_control_lock); } +static unsigned int damon_scheme_idx(struct damon_ctx *c, struct damos *s) +{ + unsigned int idx = 0; + struct damos *siter; + + damon_for_each_scheme(siter, c) { + if (siter == s) + break; + idx++; + } + return idx; +} + static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t, struct damon_region *r, struct damos *s) { @@ -2060,7 +2073,6 @@ static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t, * index here. */ unsigned int cidx = 0; - struct damos *siter; /* schemes iterator */ unsigned int sidx = 0; struct damon_target *titer; /* targets iterator */ unsigned int tidx = 0; @@ -2068,11 +2080,7 @@ static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t, /* get indices for trace_damos_before_apply() */ if (trace_damos_before_apply_enabled()) { - damon_for_each_scheme(siter, c) { - if (siter == s) - break; - sidx++; - } + sidx = damon_scheme_idx(c, s); damon_for_each_target(titer, c) { if (titer == t) break; @@ -2394,15 +2402,7 @@ static void damos_set_effective_quota(struct damos_quota *quota) static void damos_trace_esz(struct damon_ctx *c, struct damos *s, struct damos_quota *quota) { - unsigned int cidx = 0, sidx = 0; - struct damos *siter; - - damon_for_each_scheme(siter, c) { - if (siter == s) - break; - sidx++; - } - trace_damos_esz(cidx, sidx, quota->esz); + trace_damos_esz(0, damon_scheme_idx(c, s), quota->esz); } static void damos_adjust_quota(struct damon_ctx *c, struct damos *s) @@ -2469,18 +2469,11 @@ static void damos_adjust_quota(struct damon_ctx *c, struct damos *s) static void damos_trace_stat(struct damon_ctx *c, struct damos *s) { - unsigned int cidx = 0, sidx = 0; - struct damos *siter; - if (!trace_damos_stat_after_apply_interval_enabled()) return; - damon_for_each_scheme(siter, c) { - if (siter == s) - break; - sidx++; - } - trace_damos_stat_after_apply_interval(cidx, sidx, &s->stat); + trace_damos_stat_after_apply_interval(0, damon_scheme_idx(c, s), + &s->stat); } static void kdamond_apply_schemes(struct damon_ctx *c) -- 2.34.1