linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Quanmin Yan <yanquanmin1@huawei.com>
To: <sj@kernel.org>
Cc: <akpm@linux-foundation.org>, <damon@lists.linux.dev>,
	<linux-kernel@vger.kernel.org>, <linux-mm@kvack.org>,
	<yanquanmin1@huawei.com>, <wangkefeng.wang@huawei.com>,
	<zuoze1@huawei.com>
Subject: [RFC PATCH -next 16/16] mm/damon/core: handle quota->esz overflow issues
Date: Wed, 13 Aug 2025 13:07:06 +0800	[thread overview]
Message-ID: <20250813050706.1564229-17-yanquanmin1@huawei.com> (raw)
In-Reply-To: <20250813050706.1564229-1-yanquanmin1@huawei.com>

In the original quota enforcement implementation, the traffic
calculation multiplied A by 1000000 due to time unit conversion,
making it highly prone to overflow on 32-bit systems:

damos_set_effective_quota
  if (quota->total_charged_ns)
    throughput = quota->total_charged_sz * 1000000 /
		quota->total_charged_ns;

Requiring total_charged_sz to be less than 4GB/1000000 is unreasonable.
Additionally, when overflow occurs and causes quota->esz to become
extremely small, the subsequent damos_apply_scheme logic permanently
sets sz to 0, while quota stop updating, ultimately leading to complete
functional failure:

damos_apply_scheme
  if (quota->esz && quota->charged_sz + sz > quota->esz)
    sz = ALIGN_DOWN(quota->esz - quota->charged_sz, DAMON_MIN_REGION);

Total charged stats use the unsigned long long data type to reduce
overflow risk, with data reset capability after overflow occurs.

Signed-off-by: Quanmin Yan <yanquanmin1@huawei.com>
---
 include/linux/damon.h |  4 ++--
 mm/damon/core.c       | 18 ++++++++++++------
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/include/linux/damon.h b/include/linux/damon.h
index d85850cf06c5..45aab331dfb7 100644
--- a/include/linux/damon.h
+++ b/include/linux/damon.h
@@ -247,8 +247,8 @@ struct damos_quota {
 
 /* private: */
 	/* For throughput estimation */
-	unsigned long total_charged_sz;
-	unsigned long total_charged_ns;
+	unsigned long long total_charged_sz;
+	unsigned long long total_charged_ns;
 
 	/* For charging the quota */
 	unsigned long charged_sz;
diff --git a/mm/damon/core.c b/mm/damon/core.c
index bc764f9dc5c5..5e05fdd91c12 100644
--- a/mm/damon/core.c
+++ b/mm/damon/core.c
@@ -15,6 +15,7 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/string_choices.h>
+#include <linux/math64.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/damon.h>
@@ -2059,8 +2060,8 @@ static unsigned long damos_quota_score(struct damos_quota *quota)
  */
 static void damos_set_effective_quota(struct damos_quota *quota)
 {
-	unsigned long throughput;
-	unsigned long esz = ULONG_MAX;
+	unsigned long long throughput;
+	unsigned long long esz = ULLONG_MAX;
 
 	if (!quota->ms && list_empty(&quota->goals)) {
 		quota->esz = quota->sz;
@@ -2077,11 +2078,16 @@ static void damos_set_effective_quota(struct damos_quota *quota)
 	}
 
 	if (quota->ms) {
-		if (quota->total_charged_ns)
-			throughput = quota->total_charged_sz * 1000000 /
-				quota->total_charged_ns;
-		else
+		if (quota->total_charged_ns &&
+			likely(quota->total_charged_sz < ULLONG_MAX / 1000000)) {
+			throughput = div64_u64(quota->total_charged_sz * 1000000,
+					quota->total_charged_ns);
+		} else {
 			throughput = PAGE_SIZE * 1024;
+			/* Reset the variable when an overflow occurs */
+			quota->total_charged_ns = 0;
+			quota->total_charged_sz = 0;
+		}
 		esz = min(throughput * quota->ms, esz);
 	}
 
-- 
2.34.1



  parent reply	other threads:[~2025-08-13  3:53 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-13  5:06 [RFC PATCH -next 00/16] mm/damon: support ARM32 with LPAE Quanmin Yan
2025-08-13  5:06 ` [RFC PATCH -next 01/16] mm/damon/core: add damon_ctx->addr_unit Quanmin Yan
2025-08-13  5:06 ` [RFC PATCH -next 02/16] mm/damon/paddr: support addr_unit for access monitoring Quanmin Yan
2025-08-13  5:06 ` [RFC PATCH -next 03/16] mm/damon/paddr: support addr_unit for DAMOS_PAGEOUT Quanmin Yan
2025-08-19  6:18   ` SeongJae Park
2025-08-19  6:26     ` SeongJae Park
2025-08-19 14:18       ` Quanmin Yan
2025-08-19 15:53         ` SeongJae Park
2025-08-13  5:06 ` [RFC PATCH -next 04/16] mm/damon/paddr: support addr_unit for DAMOS_LRU_[DE]PRIO Quanmin Yan
2025-08-19  6:19   ` SeongJae Park
2025-08-19  6:26     ` SeongJae Park
2025-08-13  5:06 ` [RFC PATCH -next 05/16] mm/damon/paddr: support addr_unit for MIGRATE_{HOT,COLD} Quanmin Yan
2025-08-19  6:21   ` SeongJae Park
2025-08-19  6:27     ` SeongJae Park
2025-08-13  5:06 ` [RFC PATCH -next 06/16] mm/damon/paddr: support addr_unit for DAMOS_STAT Quanmin Yan
2025-08-19  6:22   ` SeongJae Park
2025-08-19  6:27     ` SeongJae Park
2025-08-13  5:06 ` [RFC PATCH -next 07/16] mm/damon/sysfs: implement addr_unit file under context dir Quanmin Yan
2025-08-19  6:24   ` SeongJae Park
2025-08-19 14:45     ` Quanmin Yan
2025-08-19 15:56       ` SeongJae Park
2025-08-13  5:06 ` [RFC PATCH -next 08/16] Docs/mm/damon/design: document 'address unit' parameter Quanmin Yan
2025-08-13  5:06 ` [RFC PATCH -next 09/16] Docs/admin-guide/mm/damon/usage: document addr_unit file Quanmin Yan
2025-08-13  5:07 ` [RFC PATCH -next 10/16] Docs/ABI/damon: " Quanmin Yan
2025-08-13  5:07 ` [RFC PATCH -next 11/16] mm/damon: add addr_unit for DAMON_RECLAIM and LRU_SORT Quanmin Yan
2025-08-13 16:36   ` SeongJae Park
2025-08-14 12:59     ` Quanmin Yan
2025-08-14 16:11       ` SeongJae Park
2025-08-19 14:59         ` Quanmin Yan
2025-08-13  5:07 ` [RFC PATCH -next 12/16] mm/damon: add damon_ctx->min_region and damon_target->min_region Quanmin Yan
2025-08-13 16:49   ` SeongJae Park
2025-08-19 14:52     ` Quanmin Yan
2025-08-13  5:07 ` [RFC PATCH -next 13/16] mm/damon/sysfs: ensure valid addr_unit setting in damon_sysfs_apply_inputs() Quanmin Yan
2025-08-13 17:02   ` SeongJae Park
2025-08-20  8:45     ` Quanmin Yan
2025-08-13  5:07 ` [RFC PATCH -next 14/16] mm/damon/core: convert sz to byte units when updating state Quanmin Yan
2025-08-13 17:08   ` SeongJae Park
2025-08-20 10:10     ` Quanmin Yan
2025-08-13  5:07 ` [RFC PATCH -next 15/16] mm/damon: the byte statistics data type in damos_stat uses unsigned long long Quanmin Yan
2025-08-13 17:10   ` SeongJae Park
2025-08-20  9:54     ` Quanmin Yan
2025-08-20 19:57       ` SeongJae Park
2025-08-13  5:07 ` Quanmin Yan [this message]
2025-08-13 17:15   ` [RFC PATCH -next 16/16] mm/damon/core: handle quota->esz overflow issues SeongJae Park
2025-08-20 10:06     ` Quanmin Yan
2025-08-13 17:25 ` [RFC PATCH -next 00/16] mm/damon: support ARM32 with LPAE SeongJae Park
2025-08-14  0:57   ` SeongJae Park
2025-08-14 14:07     ` Quanmin Yan
2025-08-14 16:04       ` SeongJae Park
2025-08-20 10:19         ` Quanmin Yan
2025-08-13 17:28 ` SeongJae Park

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250813050706.1564229-17-yanquanmin1@huawei.com \
    --to=yanquanmin1@huawei.com \
    --cc=akpm@linux-foundation.org \
    --cc=damon@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=sj@kernel.org \
    --cc=wangkefeng.wang@huawei.com \
    --cc=zuoze1@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox