From nobody Fri Apr 3 22:30:20 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 D331D1AC44D for ; Sun, 22 Mar 2026 18:47:13 +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=1774205235; cv=pass; b=ri2YVBPmZQYMctAV+QRQstXqlfdqYxxPKQyuc7GK9k8liWQTfN7JXbP+cO79cdyoT+BcEJvJi1oAtx0y7cJ/I1W7wxlGZUjMvovV99gIkUmLB7Evd6UUhaefvvGqhIvi7ULAb11+08czGmK1aqEC94q8LJlDELOwrzdOAeXVWOk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774205235; c=relaxed/simple; bh=yEQ0HmqWknkliaUeDQEhusyB8EwptCuwlnXaC/zQt3w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ox6xiYfsTdnteIVSEzaW3HeuMORxFpdCDttc4eyztH8vogCbStJgnL0PlaSNLhAwSybX5CdJiGqtboTB+P9K90Cnwm7V93puAFVGUVVH7tBKv8Gw0avZyA5Yp7SnzgaIklBP06mFQN9YROhs78EqOuScvfhKwnMCBmor4qSiGxA= 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=iVrJsWuT; 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="iVrJsWuT" ARC-Seal: i=1; a=rsa-sha256; t=1774205205; cv=none; d=zohomail.eu; s=zohoarc; b=Q7Gzmh/ifcY/OF8N/GJ5KKRWThZQmfsD9UqNRGNfrOd4GwthxfjJ6I0p5rWsL+0dHvrbgIm6Wfxu9hNxdfeZUJvg6ZLT9lLy0GS0Bbj1cHULXGSA0nJ4SwVcl/BFx3+GlI5V7iQIH4H1H3mRbnTCVErC0IV17HUkbXaz9c7tsaQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.eu; s=zohoarc; t=1774205205; 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=Z1bFu9JMKcytFKPllcHmQvZueX6RwzFSMs4eazwBV0wCQmkDv+SQXlTyQPglLGdO7hjd4lgLDUTgpBQMFtwy4pQ4L/eknAbip8mfTfTt2ZDrTFVBPETN7Iak+5iFkOw717fQ3aGglz/PYdk6LFFYj/e/M6+IixQgiESD7B8JGX4= 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=1774205205; 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=iVrJsWuTDkuvPjkKL/QTPYLcerO8uYe9Bgd+qDsLUYXVbSsfJ7WpDropvTaWhJ4s bc/yWS5lfAO7mTh9lRwQokFqr5sQbc4v5FJcnpz67L4cgcZRbPa9PEuQkOka5A6K+zV sX8MzPmYvg8rX7WcF+AjySmiKq0qHxgQcfoAnqWw= Received: by mx.zoho.eu with SMTPS id 1774205203626774.6951372831195; Sun, 22 Mar 2026 19:46:43 +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 Subject: [PATCH 1/2] mm/damon/core: optimize kdamond_apply_schemes() by inverting scheme and region loops Date: Sun, 22 Mar 2026 18:46:40 +0000 Message-Id: <20260322184641.251966-2-objecting@objecting.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260322184641.251966-1-objecting@objecting.org> References: <20260322184641.251966-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 From nobody Fri Apr 3 22:30:20 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 193F11459FA for ; Sun, 22 Mar 2026 18:47:12 +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=1774205234; cv=pass; b=qLksRzt10vykP1mwYUoyqlG10mXDTSVQQkJitOHIVQKXs63KtretjefcIPgFQ3PYvz8/vlhHPO7d0I+wd4zwYqIPRlNZv4pdOsAbifAZkDZYp98pcLRQtBNK1ugkI9/eOOnzocy3HWczFSJYEkU/iyEfhdp+YUbcby5cg0gZRQE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774205234; c=relaxed/simple; bh=W8dBMfzxr8V0OtT7VOhajRCtCl4Kjb5Y7IinemsCa/s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=W2n4QPBT1CFB+e4ysQpukhYff6vW/yXuECCUiZydY5jUBgprQ7b3eGxhQLEMcx/ZqODgihKvmOywwBUcB1fOR5QKYpSFjICOyrbBYfvIQMnF60ADjLHd9ptaL+FZLRxq9cXubJlyVmrA5xXBcFpc1U1Rhu/9eiJoWluG4SB3YMM= 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=l8pYAPpZ; 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="l8pYAPpZ" ARC-Seal: i=1; a=rsa-sha256; t=1774205206; cv=none; d=zohomail.eu; s=zohoarc; b=Zzg2qELQVF3tfbkI0jcLJC3JWMCMtcernSKxBrdjto4K7Yg4WRfokio6Ac69aoKIlRxZOBeEGHVTs8CUZz/XOlLCHBGKk71+G4LOqgWgSi86LS8VGEg+puyCjO2kAIVledLxM97n4kUiIw9ByYpB3XJKdPGPjOt6QLQI5Edl3nw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.eu; s=zohoarc; t=1774205206; 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=05a6CfwNgkeRFd6GC3+9/VEwzrQUx3NsZy711o2AxTU=; b=FmwsXgy/tGA1atYBBow9L+u7K+btTqA6f6EOL0MovVJTnoXuTAy1727/useKcsWtDbSFQArKfmt6IaBS1Zu8pypOTnMjAssJeMho8vSJ+w7YDvJmnqmEzvQZe1K67yT0Y+iMtS3FZweCZLCJlxKr1kfKQAIdvB7jDPywNamm6u8= 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=1774205206; 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=05a6CfwNgkeRFd6GC3+9/VEwzrQUx3NsZy711o2AxTU=; b=l8pYAPpZo2Kcy+acCpggJdzpcEbMTQCfAJKyF+uCcZ7XdONfJUSGyNon2hJpk7zi bpw+ckxFeu4fIHtH1mXn/7ip6ZeJKjHODk/n/LN9FRP9p4xL6TVDWrdgJnr1kkEW0j5 PzPQBQUUAP7e25C0MvLAU74AV9552/N9XCzjGUu4= Received: by mx.zoho.eu with SMTPS id 1774205204010549.1240841832505; Sun, 22 Mar 2026 19:46:44 +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 Subject: [PATCH 2/2] mm/damon/core: eliminate hot-path integer division in damon_max_nr_accesses() Date: Sun, 22 Mar 2026 18:46:41 +0000 Message-Id: <20260322184641.251966-3-objecting@objecting.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260322184641.251966-1-objecting@objecting.org> References: <20260322184641.251966-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" Hardware integer division is slow. The function damon_max_nr_accesses(), which is called very frequently (e.g., once per region per sample interval inside damon_update_region_access_rate), performs an integer division: attrs->aggr_interval / attrs->sample_interval. However, the struct damon_attrs already caches this exact ratio in the internal field aggr_samples (since earlier commits). We can eliminate the hardware division in the hot path by simply returning aggr_samples. This significantly reduces the CPU cycle overhead of updating the access rates for thousands of regions. Signed-off-by: Josh Law --- include/linux/damon.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index 6bd71546f7b2..fffdb08326a2 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -960,8 +960,7 @@ static inline bool damon_target_has_pid(const struct da= mon_ctx *ctx) static inline unsigned int damon_max_nr_accesses(const struct damon_attrs = *attrs) { /* {aggr,sample}_interval are unsigned long, hence could overflow */ - return min(attrs->aggr_interval / attrs->sample_interval, - (unsigned long)UINT_MAX); + return min(attrs->aggr_samples, (unsigned long)UINT_MAX); } =20 =20 --=20 2.34.1