All of lore.kernel.org
 help / color / mirror / Atom feed
From: SJ Park <sj@kernel.org>
To: sashiko-bot@kernel.org
Cc: SJ Park <sj@kernel.org>, damon@lists.linux.dev
Subject: Re: [RFC PATCH v1.2 8/8] mm/damon/core: handle extreme memory state in get_in_active_mem_bp()
Date: Mon, 31 Aug 2026 17:37:47 -0700	[thread overview]
Message-ID: <20260901003748.88963-1-sj@kernel.org> (raw)
In-Reply-To: <20260831184956.1DA321F000E9@smtp.kernel.org>

On Mon, 31 Aug 2026 18:49:55 +0000 sashiko-bot@kernel.org wrote:

> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> 
> Pre-existing issues:
> - [High] Silent integer overflow on 32-bit systems in mult_frac() usage across DAMON quota metrics, leading to drastically incorrect memory metrics.
> --
> 
> commit ba2ca9efc083502fb5555a81a42e9de1ab623b4d
> Author: SJ Park <sj@kernel.org>
> 
> mm/damon/core: handle extreme memory state in get_in_active_mem_bp()
> 
> damos_get_in_active_mem_bp() uses the sum of the active and inactive
> memory amount as a denominator.  In an extreme and unlikely environment,
> active and inactive memory might be zero.  In this case, hence, it
> results in a divide by zero problem.  Avoid it by changing the
> denominator to one if it is zero, before it is being used.
> 
> Link: https://lore.kernel.org/20260721034756.147011-1-sj@kernel.org
> 
> > diff --git a/mm/damon/core.c b/mm/damon/core.c
> > index 3779a04753061..f8dddbff74a77 100644
> > --- a/mm/damon/core.c
> > +++ b/mm/damon/core.c
> > @@ -3015,7 +3015,7 @@ static unsigned int damos_get_in_active_mem_bp(bool active_ratio)
> >  		global_node_page_state(NR_LRU_BASE + LRU_ACTIVE_FILE);
> >  	inactive = global_node_page_state(NR_LRU_BASE + LRU_INACTIVE_ANON) +
> >  		global_node_page_state(NR_LRU_BASE + LRU_INACTIVE_FILE);
> > -	total = active + inactive;
> > +	total = max(active + inactive, 1);
> >  	if (active_ratio)
> >  		return mult_frac(active, 10000, total);
> 
> [Severity: High]
> This isn't a bug introduced by this patch, but does the use of mult_frac()
> here cause a silent integer overflow on 32-bit systems?
> 
> The mult_frac(x, n, d) macro calculates (x / d) * n + (x % d) * n / d. On
> 32-bit architectures, intermediate multiplications like (x % d) * n are
> evaluated as 32-bit unsigned integers if x is an unsigned long. This
> silently overflows if the result exceeds ~4.29 billion.
> 
> For example, in damos_get_in_active_mem_bp(), active is counted in pages.
> If active memory reaches roughly 429,496 pages (about 1.7 GB),
> 429,496 * 10,000 exceeds the 32-bit maximum of 4,294,967,295 and wraps
> around, resulting in drastically incorrect memory metrics.
> 
> This same unsigned long overflow seems to be pre-existing in other DAMON
> quota metrics in mm/damon/core.c as well:
> 
> - damos_get_node_mem_bp(): same issue with node memory.
> - damos_get_node_eligible_mem_bp(): node_eligible is cast to unsigned
>   long bytes, which can overflow if eligible memory exceeds ~419 KB.
> - damos_set_effective_quota(): total_charged_sz is in bytes, which
>   overflows when multiplied by 1,000,000 if it exceeds 4294 bytes.
> - damos_quota_score(): goal->current_value tracks PSI in microseconds,
>   which can easily be millions, overflowing when multiplied by 10,000.
> 
> Could these calculations be performed using 64-bit math on 32-bit systems
> to prevent runaway aggressive memory reclaim or total failure to reclaim?

Known low priority issue.

> 
> >  	return mult_frac(inactive, 10000, total);
> >  }
> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20260831140254.72004-1-sj@kernel.org?part=8


Thanks,
SJ

  reply	other threads:[~2026-09-01  0:37 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-31 14:02 [RFC PATCH v1.2 0/8] mm/damon: fix DAMOS bugs in core, paddr and vaddr SJ Park
2026-08-31 14:02 ` [RFC PATCH v1.2 1/8] mm/damon/core: skip applying scheme if region split for quota fails SJ Park
2026-08-31 17:45   ` sashiko-bot
2026-08-31 14:02 ` [RFC PATCH v1.2 2/8] mm/damon/paddr: respect folio end for DAMOS_STAT SJ Park
2026-08-31 17:54   ` sashiko-bot
2026-08-31 14:02 ` [RFC PATCH v1.2 3/8] mm/damon/paddr: respect folio end for DAMOS actions except STAT SJ Park
2026-08-31 18:06   ` sashiko-bot
2026-08-31 14:02 ` [RFC PATCH v1.2 4/8] mm/damon/vaddr: respect folio end for DAMOS_STAT SJ Park
2026-08-31 18:14   ` sashiko-bot
2026-08-31 14:02 ` [RFC PATCH v1.2 5/8] mm/damon/vaddr: respect folio end for DAMOS_MIGRATE_{HOT,COLD} SJ Park
2026-08-31 18:23   ` sashiko-bot
2026-08-31 14:02 ` [RFC PATCH v1.2 6/8] mm/damon/core: handle extreme memory state in damon_get_node_mem_bp() SJ Park
2026-08-31 18:29   ` sashiko-bot
2026-08-31 14:02 ` [RFC PATCH v1.2 7/8] mm/damon/core: handle extreme memory state in get_node_memcg_used_bp() SJ Park
2026-08-31 18:41   ` sashiko-bot
2026-09-01  0:36     ` SJ Park
2026-08-31 14:02 ` [RFC PATCH v1.2 8/8] mm/damon/core: handle extreme memory state in get_in_active_mem_bp() SJ Park
2026-08-31 18:49   ` sashiko-bot
2026-09-01  0:37     ` SJ Park [this message]
2026-09-01  0:50 ` [RFC PATCH v1.2 0/8] mm/damon: fix DAMOS bugs in core, paddr and vaddr SJ 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=20260901003748.88963-1-sj@kernel.org \
    --to=sj@kernel.org \
    --cc=damon@lists.linux.dev \
    --cc=sashiko-bot@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.