From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Chen Wandun <chenwandun1@gmail.com>,
akpm@linux-foundation.org, ljs@kernel.org, shuah@kernel.org,
zokeefe@google.com
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-kselftest@vger.kernel.org
Subject: Re: [PATCH 1/2] mm/khugepaged: fix spurious -EINVAL from sub-PMD MADV_COLLAPSE range
Date: Fri, 8 May 2026 14:27:37 +0200 [thread overview]
Message-ID: <9eea2afb-8c35-47eb-b1de-6a08503c9679@kernel.org> (raw)
In-Reply-To: <20260507070558.3064142-2-chenwandun@lixiang.com>
On 5/7/26 09:05, Chen Wandun wrote:
> madvise_collapse() computes the THP-aligned window:
>
> hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK /* round up */
> hend = end & HPAGE_PMD_MASK /* round down */
>
> Previously this was done after kmalloc_obj(), so problem arose when
> the range contained no complete PMD-aligned window (hstart >= hend).
>
> When hstart > hend, (hend - hstart) wraps unsigned to a huge value, the
> final comparison fails and -EINVAL is returned instead of 0. Consider
> two single-page calls on a 2 MiB-aligned address:
>
> /* hstart == hend == aligned -> 0 == 0 -> returns 0 */
> madvise(aligned, PAGE_SIZE, MADV_COLLAPSE);
>
> /* hstart = aligned + 2MiB, hend = aligned
> * (hend - hstart) wraps unsigned -> returns -EINVAL */
> madvise(aligned + PAGE_SIZE, PAGE_SIZE, MADV_COLLAPSE);
>
> Both calls cover less than one THP and collapse nothing; both should
> return 0.
Okay, so we talk about a "userspace is being stupid" scenario.
>
> In addition, kmalloc_obj(), mmgrab() and lru_add_drain_all() were all
> called before discovering there was nothing to do, only for the code
> to kfree() and return immediately after.
Just a comment as you motivate here why this is suboptimal: we do not care about
a "userspace is being stupid" scenario being fast.
>
> Fix both by computing hstart/hend after thp_vma_allowable_order() but
> before kmalloc_obj(), and returning 0 early when hstart >= hend.
>
> Fixes: 7d8faaf15545 ("mm/madvise: introduce MADV_COLLAPSE sync hugepage collapse")
Fixes: is likely ok, but I don't think we want to treat this as a hotfix or CC
stable.
> Signed-off-by: Chen Wandun <chenwandun@lixiang.com>
> ---
> mm/khugepaged.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index b8452dbdb043..92473d93e837 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -2836,6 +2836,12 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
> if (!thp_vma_allowable_order(vma, vma->vm_flags, TVA_FORCED_COLLAPSE, PMD_ORDER))
> return -EINVAL;
>
> + hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
> + hend = end & HPAGE_PMD_MASK;
> +
> + if (hstart >= hend)
> + return 0;
> +
> cc = kmalloc_obj(*cc);
> if (!cc)
> return -ENOMEM;
> @@ -2845,9 +2851,6 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
> mmgrab(mm);
> lru_add_drain_all();
>
> - hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
> - hend = end & HPAGE_PMD_MASK;
> -
> for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
> enum scan_result result = SCAN_FAIL;
>
In general, LGTM, but see for conflict:
https://lore.kernel.org/all/20260409014323.2385982-1-ye.liu@linux.dev/
--
Cheers,
David
next prev parent reply other threads:[~2026-05-08 12:27 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-07 7:05 [PATCH 0/2] mm/khugepaged: fix sub-PMD MADV_COLLAPSE range handling Chen Wandun
2026-05-07 7:05 ` [PATCH 1/2] mm/khugepaged: fix spurious -EINVAL from sub-PMD MADV_COLLAPSE range Chen Wandun
2026-05-08 12:27 ` David Hildenbrand (Arm) [this message]
2026-05-08 15:02 ` Lorenzo Stoakes
2026-05-08 15:04 ` Lorenzo Stoakes
2026-05-09 7:53 ` Wandun
2026-05-08 19:29 ` David Hildenbrand (Arm)
2026-05-09 7:04 ` Wandun
2026-05-09 5:56 ` Wandun
2026-05-07 7:05 ` [PATCH 2/2] selftests/mm: add MADV_COLLAPSE sub-PMD range tests Chen Wandun
2026-05-08 12:23 ` David Hildenbrand (Arm)
2026-05-08 15:03 ` Lorenzo Stoakes
2026-05-09 9:47 ` [PATCH 0/2] mm/khugepaged: fix sub-PMD MADV_COLLAPSE range handling Lance Yang
2026-05-11 2:06 ` Wandun
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=9eea2afb-8c35-47eb-b1de-6a08503c9679@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=chenwandun1@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=shuah@kernel.org \
--cc=zokeefe@google.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