From nobody Tue Dec 16 23:43:22 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53204C07480 for ; Fri, 20 Oct 2023 17:23:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377922AbjJTRX1 (ORCPT ); Fri, 20 Oct 2023 13:23:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229905AbjJTRXY (ORCPT ); Fri, 20 Oct 2023 13:23:24 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2DA651A3; Fri, 20 Oct 2023 10:23:23 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5437EC433C8; Fri, 20 Oct 2023 17:23:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697822602; bh=Rn9BHGv/kMTRWnOOhqqlR6v0d9H0u3tnTjx9VDJfJCU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lk1FqpYByQMbf59yh0U8YLgrRJ43zTSnfY5ztYkNwJxmOlrcYYzGMmulb64GWPd27 WDnr/ul64sOEPEBEX/FpDD7+OZxQ8uoE0Z3aJRmivH6ITsRSScdktkyKelkVw9EWzz /YvbiC+RTKd5QA6LA86beLXArsehV6tLDXXWVWr1ZBYHK8TVlWJvgdQu8eDE1kLNFp Y09FFWInvtPhlAqZAV2wcN8olPgLL4o0Xj8ujhKXQCa21H/maEShe4WpqSSEIrcXg/ zChMs5xWAxN9PsyWly2J4u8y1YAbo07mJij27Eu/Tj0CAYBsIe7S6DVGlYjXB2W8SE DJbKUg/VjiXzA== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , Jakub Acs , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v2 1/5] mm/damon: implement a function for max nr_accesses safe calculation Date: Fri, 20 Oct 2023 17:23:13 +0000 Message-Id: <20231020172317.64192-2-sj@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231020172317.64192-1-sj@kernel.org> References: <20231020172317.64192-1-sj@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The maximum nr_accesses of given DAMON context can be calculated by dividing the aggregation interval by the sampling interval. Some logics in DAMON uses the maximum nr_accesses as a divisor. Hence, the value shouldn't be zero. Such case is avoided since DAMON avoids setting the agregation interval as samller than the sampling interval. However, since nr_accesses is unsigned int while the intervals are unsigned long, the maximum nr_accesses could be zero while casting. Implement a function that handles the corner case. Note that this commit is not fixing the real issue since this is only introducing the safe function that will replaces the problematic divisions. The replacements will be made by followup commits, to make backporting on stable series easier. Reported-by: Jakub Acs Fixes: 198f0f4c58b9 ("mm/damon/vaddr,paddr: support pageout prioritization") Cc: # 5.16.x Signed-off-by: SeongJae Park --- include/linux/damon.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/linux/damon.h b/include/linux/damon.h index 27b995c22497..ab2f17d9926b 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -681,6 +681,13 @@ static inline bool damon_target_has_pid(const struct d= amon_ctx *ctx) return ctx->ops.id =3D=3D DAMON_OPS_VADDR || ctx->ops.id =3D=3D DAMON_OPS= _FVADDR; } =20 +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); +} + =20 int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive); int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); --=20 2.34.1 From nobody Tue Dec 16 23:43:22 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D070CC19F5C for ; Fri, 20 Oct 2023 17:23:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377930AbjJTRX3 (ORCPT ); Fri, 20 Oct 2023 13:23:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229909AbjJTRX0 (ORCPT ); Fri, 20 Oct 2023 13:23:26 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EA258D4C; Fri, 20 Oct 2023 10:23:24 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1487BC433C7; Fri, 20 Oct 2023 17:23:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697822604; bh=OyGTPA4KF2+eYVdgVawdmd836X4nvWEWuxM0J39C7SY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tibiLrMDEW1tJPRD83m27OwLCd/3DldWtN6HSY+GRNl2CN2/lGFqdA8TQQ727MbK1 RXghEBiUVpRYW2wWEiF2yiDTw4Mz9aUN/5jKYr74Dv/eki3xYKDE93RdSo9tqctrMh CQYh7C/JT6Pd/m/Lx4jnpiYh3AL+5ZGjJ0Dgwm8qeCTjxEkZ//jSvkKcBywnn8qJsk xIov6mao/lOfYVmBmQThgDh3lKi7Zq5LeWX+G0A45G30D5VHCYpRwm1A1Anhin9GeL YqhXZxPUTshcAwA2xiNxXzGXvG7BgM88reL/1z9rKcS6B3dNgRns0LUK1vsgdWaqlG PvGSzwlgnkpAg== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , Jakub Acs , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v2 2/5] mm/damon/core: avoid divide-by-zero during monitoring results update Date: Fri, 20 Oct 2023 17:23:14 +0000 Message-Id: <20231020172317.64192-3-sj@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231020172317.64192-1-sj@kernel.org> References: <20231020172317.64192-1-sj@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When monitoring attributes are changed, DAMON updates access rate of the monitoring results accordingly. For that, it divides some values by the maximum nr_accesses. However, due to the type of the related variables, simple division-based calculation of the divisor can return zero. As a result, divide-by-zero is possible. Fix it by using damon_max_nr_accesses(), which handles the case. Reported-by: Jakub Acs Fixes: 2f5bef5a590b ("mm/damon/core: update monitoring results for new moni= toring attributes") Cc: # 6.3.x Signed-off-by: SeongJae Park --- mm/damon/core.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/mm/damon/core.c b/mm/damon/core.c index 9f4f7c378cf3..e194c8075235 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -500,20 +500,14 @@ static unsigned int damon_age_for_new_attrs(unsigned = int age, static unsigned int damon_accesses_bp_to_nr_accesses( unsigned int accesses_bp, struct damon_attrs *attrs) { - unsigned int max_nr_accesses =3D - attrs->aggr_interval / attrs->sample_interval; - - return accesses_bp * max_nr_accesses / 10000; + return accesses_bp * damon_max_nr_accesses(attrs) / 10000; } =20 /* convert nr_accesses to access ratio in bp (per 10,000) */ static unsigned int damon_nr_accesses_to_accesses_bp( unsigned int nr_accesses, struct damon_attrs *attrs) { - unsigned int max_nr_accesses =3D - attrs->aggr_interval / attrs->sample_interval; - - return nr_accesses * 10000 / max_nr_accesses; + return nr_accesses * 10000 / damon_max_nr_accesses(attrs); } =20 static unsigned int damon_nr_accesses_for_new_attrs(unsigned int nr_access= es, --=20 2.34.1 From nobody Tue Dec 16 23:43:22 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3B1DC25B41 for ; Fri, 20 Oct 2023 17:23:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377941AbjJTRXc (ORCPT ); Fri, 20 Oct 2023 13:23:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37020 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377927AbjJTRX1 (ORCPT ); Fri, 20 Oct 2023 13:23:27 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D512A3; Fri, 20 Oct 2023 10:23:26 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 78EB3C433CA; Fri, 20 Oct 2023 17:23:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697822606; bh=GfglrsOk5qGjGnmruIH7Op0UcLOq5YxxSRcQby/wYY8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t9O9pz8w8PavHtJRAmDjnfs+pzDqdv3x4tnFpqQpQW1CJed83FYVNs8li7TGn1UA+ inQgRhjia22JOeOI1IOX/SU72zUdILTB4zjucts/LRz4x26P/X2PpEbbKFOOvzENqP dKo+VdyORmHfwzLfarroponzPoeHT8djRKCvqTMW1WtHdE0g6GJD957y+s2jxH4Urs QFIzBdNY3ViZk53G1vYRSdwpxAlC/HrSaZug/ZAIIXroUlMmTWB8Z61PD7o6nsbsBr aDrtNJE0Qkr2Z/vqvRHNvfNu3zG7BvnrJYEkW94xpTpAp0LOz/mKWk9XUqXVvox+lk GpfvMq1vSBR6w== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , Jakub Acs , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v2 3/5] mm/damon/ops-common: avoid divide-by-zero during region hotness calculation Date: Fri, 20 Oct 2023 17:23:15 +0000 Message-Id: <20231020172317.64192-4-sj@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231020172317.64192-1-sj@kernel.org> References: <20231020172317.64192-1-sj@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When calculating the hotness of each region for the under-quota regions prioritization, DAMON divides some values by the maximum nr_accesses. However, due to the type of the related variables, simple division-based calculation of the divisor can return zero. As a result, divide-by-zero is possible. Fix it by using damon_max_nr_accesses(), which handles the case. Reported-by: Jakub Acs Fixes: 198f0f4c58b9 ("mm/damon/vaddr,paddr: support pageout prioritization") Cc: # 5.16.x Signed-off-by: SeongJae Park --- mm/damon/ops-common.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mm/damon/ops-common.c b/mm/damon/ops-common.c index ac1c3fa80f98..d25d99cb5f2b 100644 --- a/mm/damon/ops-common.c +++ b/mm/damon/ops-common.c @@ -73,7 +73,6 @@ void damon_pmdp_mkold(pmd_t *pmd, struct vm_area_struct *= vma, unsigned long addr int damon_hot_score(struct damon_ctx *c, struct damon_region *r, struct damos *s) { - unsigned int max_nr_accesses; int freq_subscore; unsigned int age_in_sec; int age_in_log, age_subscore; @@ -81,8 +80,8 @@ int damon_hot_score(struct damon_ctx *c, struct damon_reg= ion *r, unsigned int age_weight =3D s->quota.weight_age; int hotness; =20 - max_nr_accesses =3D c->attrs.aggr_interval / c->attrs.sample_interval; - freq_subscore =3D r->nr_accesses * DAMON_MAX_SUBSCORE / max_nr_accesses; + freq_subscore =3D r->nr_accesses * DAMON_MAX_SUBSCORE / + damon_max_nr_accesses(&c->attrs); =20 age_in_sec =3D (unsigned long)r->age * c->attrs.aggr_interval / 1000000; for (age_in_log =3D 0; age_in_log < DAMON_MAX_AGE_IN_LOG && age_in_sec; --=20 2.34.1 From nobody Tue Dec 16 23:43:22 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5185DC001DF for ; Fri, 20 Oct 2023 17:23:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377959AbjJTRXe (ORCPT ); Fri, 20 Oct 2023 13:23:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377916AbjJTRX3 (ORCPT ); Fri, 20 Oct 2023 13:23:29 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B067511B; Fri, 20 Oct 2023 10:23:27 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E62BEC433CB; Fri, 20 Oct 2023 17:23:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697822607; bh=6lmYsv8XJqW/eM+lXLzhU7HxZ5CdX1w8lmt0qOdrzr8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jHtmFc93b57yZn/bDpAh7gBhcfHbj20IeY+fuu4053yIw8MoFaaDnmQ0MsrNV8IEo X1ZOjlEl1Kmq4ngCLAeoC0621PhULQJp8lkT0Kw5Kc+APiSyMONoLooY1MQILhnSDk 1W2Lt47fEK5O7FwRtUdgtoRCWawLtS8RrNJAvD6jDE3sWD3ipUbb1AYqpnahNa2Anz tLyFDzmIrQ4k/2QD4TQMawE617DS+D+Cktc6QL7UdhoZ140dK+/y11tdkGzEEIlkn7 D8LoBKqBNUcwmM8as1ufxtbE2HDuPWHtSNOJh3KGLxL+1+9gAKAdCjl0b2J+e8c26P Q5JlslM9OsQqQ== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , Jakub Acs , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v2 4/5] mm/damon/lru_sort: avoid divide-by-zero in hot threshold calculation Date: Fri, 20 Oct 2023 17:23:16 +0000 Message-Id: <20231020172317.64192-5-sj@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231020172317.64192-1-sj@kernel.org> References: <20231020172317.64192-1-sj@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When calculating the hotness threshold for lru_prio scheme of DAMON_LRU_SORT, the module divides some values by the maximum nr_accesses. However, due to the type of the related variables, simple division-based calculation of the divisor can return zero. As a result, divide-by-zero is possible. Fix it by using damon_max_nr_accesses(), which handles the case. Reported-by: Jakub Acs Fixes: 40e983cca927 ("mm/damon: introduce DAMON-based LRU-lists Sorting") Cc: # 6.0.x Signed-off-by: SeongJae Park --- mm/damon/lru_sort.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mm/damon/lru_sort.c b/mm/damon/lru_sort.c index 3ecdcc029443..f2e5f9431892 100644 --- a/mm/damon/lru_sort.c +++ b/mm/damon/lru_sort.c @@ -195,9 +195,7 @@ static int damon_lru_sort_apply_parameters(void) if (err) return err; =20 - /* aggr_interval / sample_interval is the maximum nr_accesses */ - hot_thres =3D damon_lru_sort_mon_attrs.aggr_interval / - damon_lru_sort_mon_attrs.sample_interval * + hot_thres =3D damon_max_nr_accesses(&damon_lru_sort_mon_attrs) * hot_thres_access_freq / 1000; scheme =3D damon_lru_sort_new_hot_scheme(hot_thres); if (!scheme) --=20 2.34.1 From nobody Tue Dec 16 23:43:22 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C34EFC07480 for ; Fri, 20 Oct 2023 17:23:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377982AbjJTRXg (ORCPT ); Fri, 20 Oct 2023 13:23:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377945AbjJTRXc (ORCPT ); Fri, 20 Oct 2023 13:23:32 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24B9813E for ; Fri, 20 Oct 2023 10:23:29 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 744D4C433C9; Fri, 20 Oct 2023 17:23:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697822608; bh=A7qQGX2x61HuX9WngSjrxy/tMdxThwG7cO+vlCnt/oI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R2Uo8d1dOObRxHmQhvT+GaBkbeipS3EDjJHzlrqnXCOqDtzreW0cAfNfLBKnVc8lc lCprkGxeggXj98oVNf/z2gNIDicTFnmBoUSvD8XtZKmqF8BGuA1vA5ufLZh4OjhNfY xc9+xQc4jg/efEPeUXH3jErpnXwLGo738cZmOScqm/dYnZfejUTXwGj+LjFdURrRTu pAbE3gO8gyfQ/zOq8DRIK0aUFpnm+GIxyJPhGLoPRDPqkfz3HJWzOyFpKlnff7OBsQ 1he8H2Uc1OZyM9peBjSqaiWiqy51/HuuUooffHS1eN5ncUvPAJQtvNwZEIHW/YnzZt O2zyOi3nvaUSA== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/5] mm/damon/core: avoid divide-by-zero from pseudo-moving window length calculation Date: Fri, 20 Oct 2023 17:23:17 +0000 Message-Id: <20231020172317.64192-6-sj@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231020172317.64192-1-sj@kernel.org> References: <20231020172317.64192-1-sj@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When calculating the pseudo-moving access rate, DAMON divides some values by the maximum nr_accesses. However, due to the type of the related variables, simple division-based calculation of the divisor can return zero. As a result, divide-by-zero is possible. Fix it by using damon_max_nr_accesses(), which handles the case. Note that this is a fix for a commit that not in the mainline but mm tree. Fixes: ace30fb21af5 ("mm/damon/core: use pseudo-moving sum for nr_accesses_= bp") Signed-off-by: SeongJae Park --- Note that this is for a patch in mm-stable that not yet merged into the mainline. mm/damon/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/damon/core.c b/mm/damon/core.c index e194c8075235..aa2dc7087cd9 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -1665,7 +1665,7 @@ void damon_update_region_access_rate(struct damon_reg= ion *r, bool accessed, * aggr_interval, owing to validation of damon_set_attrs(). */ if (attrs->sample_interval) - len_window =3D attrs->aggr_interval / attrs->sample_interval; + len_window =3D damon_max_nr_accesses(attrs); r->nr_accesses_bp =3D damon_moving_sum(r->nr_accesses_bp, r->last_nr_accesses * 10000, len_window, accessed ? 10000 : 0); --=20 2.34.1