From nobody Fri Apr 3 23:44:57 2026 Received: from sender-of-o55.zoho.eu (sender-of-o55.zoho.eu [136.143.169.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5C61366823 for ; Sun, 22 Mar 2026 21:36:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.169.55 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774215410; cv=pass; b=qWWzWbr8qPtlwLLQ9Z9+l9gaciYsaeT6y1zXBtpZR0ooiJuDFIjkvECo9+2TyFWG43RDd3z9kiJK9Kn6WVNy9A5RpsmWiJhiZF3Jxsi60feqQJhBCPU33D72gIATQysS94a9Inopt1r2StvVeNS9e/4qLQoeqltBJryhxFzmfaQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774215410; c=relaxed/simple; bh=yEQ0HmqWknkliaUeDQEhusyB8EwptCuwlnXaC/zQt3w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sa0okAhSeM9lPqj6arP/R2zaHQDdI6Dewy2paqFTB4GZV3dV9ymNKjXSMG89T1xXZDe/LYiuAaj+fVCHIztlNaOJ7Q7dKkFRS4RksNGbLtLVYKCc3/W3f5HGKYO09NOgKOwp8i4xLuG3jQyj3xCBgnwEtwLwdanHMTrCo0yxg60= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=objecting.org; spf=pass smtp.mailfrom=objecting.org; dkim=pass (1024-bit key) header.d=objecting.org header.i=objecting@objecting.org header.b=AFThKKQX; arc=pass smtp.client-ip=136.143.169.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=objecting.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=objecting.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=objecting.org header.i=objecting@objecting.org header.b="AFThKKQX" ARC-Seal: i=1; a=rsa-sha256; t=1774215395; cv=none; d=zohomail.eu; s=zohoarc; b=Bg+6s+QUK/mIlYYVa+pEfVGbxTNwLScwr4mjCcNPOzQZ/nwXhdGmHtiKYZ9qF0fF0X5P6lwN4g3UlWcCESqYWyemgxC5VELzU1DBf9vBeb07DyquBCY3SO/fCZRKW/uzcENY1fLEQoRZ4+4HvtLG54L+Ogb4P+b35ASUv+B3C/Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.eu; s=zohoarc; t=1774215395; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=HvXkdekkoCKye7mj9eb/H1RyQQk11r+xEZYMc+3T7us=; b=OWdJR5w7Mo51FYUjy4kJ+1TIXzZmepp5voCjL5t+kVR2sv2TRWvE8S11MKt8Oot+BLAQ+cPelv1oo2gINXiTQaS1iKDxx+1hCLu0hwkTMfAwQWqyw4RwG39NrE17vd55e2I7HvLFYzlCClvDVV+iw0XkCcyRxztcXtTeqbRyPSo= ARC-Authentication-Results: i=1; mx.zohomail.eu; dkim=pass header.i=objecting.org; spf=pass smtp.mailfrom=objecting@objecting.org; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1774215395; s=zmail; d=objecting.org; i=objecting@objecting.org; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-Id:Message-Id:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Reply-To; bh=HvXkdekkoCKye7mj9eb/H1RyQQk11r+xEZYMc+3T7us=; b=AFThKKQXBtmARcSc/AFM8aeiZlC1Ys/8f/hV83hmr2RGTO9Fo9q9IVs7Q6kFZGF9 a2+jPWdVkTsvns27UN36Oa0maEP9q14e6bGi7MDTXDi35bqhQfp5kI5gzHKd43Oueu6 /Qz7UcBw87S0z3dKkMk8vHpzH7jR6orRmd5Aihyc= Received: by mx.zoho.eu with SMTPS id 177421539290970.2907397850829; Sun, 22 Mar 2026 22:36:32 +0100 (CET) From: Josh Law To: sj@kernel.org, akpm@linux-foundation.org Cc: damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Josh Law Subject: [PATCH v2 1/2] mm/damon/core: optimize kdamond_apply_schemes() by inverting scheme and region loops Date: Sun, 22 Mar 2026 21:36:30 +0000 Message-Id: <20260322213631.259212-2-objecting@objecting.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260322213631.259212-1-objecting@objecting.org> References: <20260322213631.259212-1-objecting@objecting.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMailClient: External Content-Type: text/plain; charset="utf-8" Currently, kdamond_apply_schemes() iterates over all targets, then over all regions, and finally calls damon_do_apply_schemes() which iterates over all schemes. This nested structure causes scheme-level invariants (such as time intervals, activation status, and quota limits) to be evaluated inside the innermost loop for every single region. If a scheme is inactive, has not reached its apply interval, or has already fulfilled its quota (quota->charged_sz >=3D quota->esz), the kernel still needlessly iterates through thousands of regions only to repeatedly evaluate these same scheme-level conditions and continue. This patch inlines damon_do_apply_schemes() into kdamond_apply_schemes() and inverts the loop ordering. It now iterates over schemes on the outside, and targets/regions on the inside. This allows the code to evaluate scheme-level limits once per scheme. If a scheme's quota is met or it is inactive, we completely bypass the O(Targets * Regions) inner loop for that scheme. This drastically reduces unnecessary branching, cache thrashing, and CPU overhead in the kdamond hot path. Signed-off-by: Josh Law --- mm/damon/core.c | 72 +++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/mm/damon/core.c b/mm/damon/core.c index c884bb31c9b8..dece0da079c8 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -2112,40 +2112,6 @@ static void damos_apply_scheme(struct damon_ctx *c, = struct damon_target *t, damos_update_stat(s, sz, sz_applied, sz_ops_filter_passed); } =20 -static void damon_do_apply_schemes(struct damon_ctx *c, - struct damon_target *t, - struct damon_region *r) -{ - struct damos *s; - - damon_for_each_scheme(s, c) { - struct damos_quota *quota =3D &s->quota; - - if (time_before(c->passed_sample_intervals, s->next_apply_sis)) - continue; - - if (!s->wmarks.activated) - continue; - - /* Check the quota */ - if (quota->esz && quota->charged_sz >=3D quota->esz) - continue; - - if (damos_skip_charged_region(t, r, s, c->min_region_sz)) - continue; - - if (s->max_nr_snapshots && - s->max_nr_snapshots <=3D s->stat.nr_snapshots) - continue; - - if (damos_valid_target(c, r, s)) - damos_apply_scheme(c, t, r, s); - - if (damon_is_last_region(r, t)) - s->stat.nr_snapshots++; - } -} - /* * damon_feed_loop_next_input() - get next input to achieve a target score. * @last_input The last input. @@ -2494,17 +2460,39 @@ static void kdamond_apply_schemes(struct damon_ctx = *c) return; =20 mutex_lock(&c->walk_control_lock); - damon_for_each_target(t, c) { - if (c->ops.target_valid && c->ops.target_valid(t) =3D=3D false) - continue; - - damon_for_each_region(r, t) - damon_do_apply_schemes(c, t, r); - } - damon_for_each_scheme(s, c) { + struct damos_quota *quota =3D &s->quota; + if (time_before(c->passed_sample_intervals, s->next_apply_sis)) continue; + + if (!s->wmarks.activated) + continue; + + damon_for_each_target(t, c) { + if (c->ops.target_valid && c->ops.target_valid(t) =3D=3D false) + continue; + + damon_for_each_region(r, t) { + /* Check the quota */ + if (quota->esz && quota->charged_sz >=3D quota->esz) + goto next_scheme; + + if (s->max_nr_snapshots && + s->max_nr_snapshots <=3D s->stat.nr_snapshots) + goto next_scheme; + + if (damos_skip_charged_region(t, r, s, c->min_region_sz)) + continue; + + if (damos_valid_target(c, r, s)) + damos_apply_scheme(c, t, r, s); + + if (damon_is_last_region(r, t)) + s->stat.nr_snapshots++; + } + } +next_scheme: damos_walk_complete(c, s); damos_set_next_apply_sis(s, c); s->last_applied =3D NULL; --=20 2.34.1