Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] mm/khugepaged: fix sub-PMD MADV_COLLAPSE range handling
@ 2026-05-07  7:05 Chen Wandun
  2026-05-07  7:05 ` [PATCH 1/2] mm/khugepaged: fix spurious -EINVAL from sub-PMD MADV_COLLAPSE range Chen Wandun
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Chen Wandun @ 2026-05-07  7:05 UTC (permalink / raw)
  To: akpm, david, ljs, shuah, zokeefe; +Cc: linux-kernel, linux-mm, linux-kselftest

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



^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2026-05-11  2:06 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox