From: Wandun <chenwandun1@gmail.com>
To: Lance Yang <lance.yang@linux.dev>
Cc: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org,
shuah@kernel.org, zokeefe@google.com,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-kselftest@vger.kernel.org
Subject: Re: [PATCH 0/2] mm/khugepaged: fix sub-PMD MADV_COLLAPSE range handling
Date: Mon, 11 May 2026 10:06:43 +0800 [thread overview]
Message-ID: <8aa6374e-7bfe-47a4-be8e-8a4ea1cc58fe@gmail.com> (raw)
In-Reply-To: <20260509094732.83643-1-lance.yang@linux.dev>
On 5/9/26 17:47, Lance Yang wrote:
> Hi,
>
> scripts/get_maintainer.pl is your friend :)
> Please use it to Cc the relevant maintainers and reviewers next time.
Many thanks for your kind reminder :)
I will do it next time.
Best regards,
Wandun
>
> Cheers, Lance
>
> On Thu, May 07, 2026 at 03:05:56PM +0800, Chen Wandun wrote:
>> madvise_collapse() computes a THP-aligned window from the caller's range:
>>
>> hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK /* round up */
>> hend = end & HPAGE_PMD_MASK /* round down */
>>
>> When the caller's range is smaller than one PMD (2 MiB) and/or not
>> PMD-aligned, hstart can end up greater than hend. In that case the
>> collapsing loop is correctly skipped, but the return value was computed
>> as ((hend - hstart) >> HPAGE_PMD_SHIFT): with hstart > hend the
>> subtraction wraps unsigned, producing a huge value, the comparison
>> "thps != 0" fires, and -EINVAL is returned instead of 0.
>>
>> A concrete example:
>>
>> /* both cover less than one THP; both should return 0 */
>> madvise(aligned, PAGE_SIZE, MADV_COLLAPSE); /* OK, returns 0 */
>> madvise(aligned + PAGE_SIZE, PAGE_SIZE, MADV_COLLAPSE); /* returns -EINVAL */
>>
>> The fix moves the hstart/hend calculation before kmalloc_obj() and
>> returns 0 early when hstart >= hend. This also avoids the kmalloc,
>> mmgrab(), and lru_add_drain_all() calls for ranges that trivially
>> contain no PMD window. The same effect could be achieved by only
>> guarding the final return expression, but early-return keeps the
>> no-op path free of the allocator and drain overhead.
>>
>> Patch 1 fixes the kernel bug.
>> Patch 2 adds a selftest with two cases covering the hstart == hend
>> (aligned, was already correct) and hstart > hend (unaligned, was
>> broken) scenarios.
>>
>> Chen Wandun (2):
>> mm/khugepaged: fix spurious -EINVAL from sub-PMD MADV_COLLAPSE range
>> selftests/mm: add MADV_COLLAPSE sub-PMD range tests
>>
>> mm/khugepaged.c | 9 +-
>> tools/testing/selftests/mm/.gitignore | 1 +
>> tools/testing/selftests/mm/Makefile | 2 +
>> .../selftests/mm/ksft_madv_collapse.sh | 4 +
>> .../selftests/mm/madv_collapse_range.c | 141 ++++++++++++++++++
>> tools/testing/selftests/mm/run_vmtests.sh | 5 +
>> 6 files changed, 159 insertions(+), 3 deletions(-)
>> create mode 100755 tools/testing/selftests/mm/ksft_madv_collapse.sh
>> create mode 100644 tools/testing/selftests/mm/madv_collapse_range.c
>>
>> --
>> 2.43.0
>>
>>
prev parent reply other threads:[~2026-05-11 2:06 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)
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 [this message]
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=8aa6374e-7bfe-47a4-be8e-8a4ea1cc58fe@gmail.com \
--to=chenwandun1@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=lance.yang@linux.dev \
--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 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.