From: Mike Rapoport <rppt@kernel.org>
To: klourencodev@gmail.com
Cc: linux-mm@kvack.org
Subject: Re: [PATCH] mm/memtest: prevent arithmetic underflow in end pointer calculation
Date: Sun, 28 Dec 2025 21:56:46 +0200 [thread overview]
Message-ID: <aVGLfmQJf4BPDTUq@kernel.org> (raw)
In-Reply-To: <20251220151019.19473-1-klourencodev@gmail.com>
On Sat, Dec 20, 2025 at 04:10:19PM +0100, klourencodev@gmail.com wrote:
> From: Kevin Lourenco <klourencodev@gmail.com>
>
> The computation of the loop end pointer can underflow when size is
> smaller than the alignment offset:
>
> (size - (start_phys_aligned - start_phys))
>
> If size < offset, the unsigned subtraction wraps to ~0, causing a
Is it exactly ~0?
> massive loop iteration that writes far beyond the intended region,
> leading to memory corruption during early boot.
>
> While unlikely in practice (memblock regions are typically KB/MB), cost is negligible
> (one comparison), but it prevents catastrophic memory corruption in
> edge cases.
>
> Signed-off-by: Kevin Lourenco <klourencodev@gmail.com>
> ---
> mm/memtest.c | 9 +++++++--
> 1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/mm/memtest.c b/mm/memtest.c
> index c2c609c39119..d86c41f1c189 100644
> --- a/mm/memtest.c
> +++ b/mm/memtest.c
> @@ -41,12 +41,17 @@ static void __init memtest(u64 pattern, phys_addr_t start_phys, phys_addr_t size
> {
> u64 *p, *start, *end;
> phys_addr_t start_bad, last_bad;
> - phys_addr_t start_phys_aligned;
> + phys_addr_t start_phys_aligned, offset;
> const size_t incr = sizeof(pattern);
>
> start_phys_aligned = ALIGN(start_phys, incr);
> start = __va(start_phys_aligned);
> - end = start + (size - (start_phys_aligned - start_phys)) / incr;
I believe VM_WARN_ON_ONCE(size < start_phys_aligned - start_phys) is
sufficient here to detect those theoretical edge cases.
> +
> + offset = start_phys_aligned - start_phys;
> + if (size < offset)
> + return;
> +
> + end = start + (size - offset) / incr;
> start_bad = 0;
> last_bad = 0;
>
> --
> 2.47.3
>
--
Sincerely yours,
Mike.
next prev parent reply other threads:[~2025-12-28 19:56 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-20 15:10 klourencodev
2025-12-28 19:56 ` Mike Rapoport [this message]
2025-12-29 15:47 ` Kevin Lourenco
2025-12-29 16:13 ` [PATCH v2] mm/memtest: add underflow detection for size calculation klourencodev
2025-12-30 13:36 ` Mike Rapoport
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=aVGLfmQJf4BPDTUq@kernel.org \
--to=rppt@kernel.org \
--cc=klourencodev@gmail.com \
--cc=linux-mm@kvack.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox