* [PATCH v3] mm/khugepaged: avoid underflow in madvise_collapse for sub-PMD MADV_COLLAPSE
@ 2026-05-13 5:54 Wandun Chen
2026-05-13 7:24 ` David Hildenbrand (Arm)
2026-05-13 9:24 ` Lorenzo Stoakes
0 siblings, 2 replies; 3+ messages in thread
From: Wandun Chen @ 2026-05-13 5:54 UTC (permalink / raw)
To: linux-mm, linux-kernel
Cc: akpm, david, ljs, ziy, baolin.wang, liam, npache, ryan.roberts,
dev.jain, baohua, lance.yang
From: Chen Wandun <chenwandun@lixiang.com>
madvise_collapse() computes the THP-aligned window:
hstart = ALIGN(start, HPAGE_PMD_SIZE); /* round up */
hend = ALIGN_DOWN(end, HPAGE_PMD_SIZE); /* round down */
The following case will cause hstart > hend, and result in underflow
in the return statement, avoid it by returning zero early when
hstart > hend. The return value is due to input is valid to madvise(),
and there is nothing to collapse.
madvise(PMD-aligned + PAGE_SIZE, PAGE_SIZE, MADV_COLLAPSE);
In addition, kmalloc_obj(), mmgrab() and lru_add_drain_all() are
unnecessary when hstart == hend, so skip these operations by
returning early too.
Signed-off-by: Chen Wandun <chenwandun@lixiang.com>
---
v2 --> v3:
- Return 0 when hstart > hend, suggested by David and Lorenzo.
v1 --> v2:
- Rebase and resolve code conflict.
- Return -EINVAL when hstart > hend, suggested by Lorenzo.
- Drop Fixes tag, suggested by David and Lorenzo.
- Updated commit message to be more explicit, suggested by Lorenzo.
---
mm/khugepaged.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 28a843f30b32..fd7e893c998d 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -2837,6 +2837,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 = ALIGN(start, HPAGE_PMD_SIZE);
+ hend = ALIGN_DOWN(end, HPAGE_PMD_SIZE);
+
+ if (hstart >= hend)
+ return 0;
+
cc = kmalloc_obj(*cc);
if (!cc)
return -ENOMEM;
@@ -2846,9 +2852,6 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
mmgrab(mm);
lru_add_drain_all();
- hstart = ALIGN(start, HPAGE_PMD_SIZE);
- hend = ALIGN_DOWN(end, HPAGE_PMD_SIZE);
-
for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
enum scan_result result = SCAN_FAIL;
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v3] mm/khugepaged: avoid underflow in madvise_collapse for sub-PMD MADV_COLLAPSE
2026-05-13 5:54 [PATCH v3] mm/khugepaged: avoid underflow in madvise_collapse for sub-PMD MADV_COLLAPSE Wandun Chen
@ 2026-05-13 7:24 ` David Hildenbrand (Arm)
2026-05-13 9:24 ` Lorenzo Stoakes
1 sibling, 0 replies; 3+ messages in thread
From: David Hildenbrand (Arm) @ 2026-05-13 7:24 UTC (permalink / raw)
To: Wandun Chen, linux-mm, linux-kernel
Cc: akpm, ljs, ziy, baolin.wang, liam, npache, ryan.roberts, dev.jain,
baohua, lance.yang
On 5/13/26 07:54, Wandun Chen wrote:
> From: Chen Wandun <chenwandun@lixiang.com>
>
> madvise_collapse() computes the THP-aligned window:
>
> hstart = ALIGN(start, HPAGE_PMD_SIZE); /* round up */
> hend = ALIGN_DOWN(end, HPAGE_PMD_SIZE); /* round down */
>
> The following case will cause hstart > hend, and result in underflow
> in the return statement, avoid it by returning zero early when
> hstart > hend. The return value is due to input is valid to madvise(),
> and there is nothing to collapse.
>
> madvise(PMD-aligned + PAGE_SIZE, PAGE_SIZE, MADV_COLLAPSE);
>
> In addition, kmalloc_obj(), mmgrab() and lru_add_drain_all() are
> unnecessary when hstart == hend, so skip these operations by
> returning early too.
>
> Signed-off-by: Chen Wandun <chenwandun@lixiang.com>
> ---
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v3] mm/khugepaged: avoid underflow in madvise_collapse for sub-PMD MADV_COLLAPSE
2026-05-13 5:54 [PATCH v3] mm/khugepaged: avoid underflow in madvise_collapse for sub-PMD MADV_COLLAPSE Wandun Chen
2026-05-13 7:24 ` David Hildenbrand (Arm)
@ 2026-05-13 9:24 ` Lorenzo Stoakes
1 sibling, 0 replies; 3+ messages in thread
From: Lorenzo Stoakes @ 2026-05-13 9:24 UTC (permalink / raw)
To: Wandun Chen
Cc: linux-mm, linux-kernel, akpm, david, ziy, baolin.wang, liam,
npache, ryan.roberts, dev.jain, baohua, lance.yang
On Wed, May 13, 2026 at 01:54:28PM +0800, Wandun Chen wrote:
> From: Chen Wandun <chenwandun@lixiang.com>
>
> madvise_collapse() computes the THP-aligned window:
>
> hstart = ALIGN(start, HPAGE_PMD_SIZE); /* round up */
> hend = ALIGN_DOWN(end, HPAGE_PMD_SIZE); /* round down */
>
> The following case will cause hstart > hend, and result in underflow
> in the return statement, avoid it by returning zero early when
> hstart > hend. The return value is due to input is valid to madvise(),
> and there is nothing to collapse.
>
> madvise(PMD-aligned + PAGE_SIZE, PAGE_SIZE, MADV_COLLAPSE);
>
> In addition, kmalloc_obj(), mmgrab() and lru_add_drain_all() are
> unnecessary when hstart == hend, so skip these operations by
> returning early too.
>
> Signed-off-by: Chen Wandun <chenwandun@lixiang.com>
LGTM, so:
Reviewed-by: Lorenzo Stoakes <ljs@kernel.org>
Cheers, Lorenzo
> ---
> v2 --> v3:
> - Return 0 when hstart > hend, suggested by David and Lorenzo.
>
> v1 --> v2:
> - Rebase and resolve code conflict.
> - Return -EINVAL when hstart > hend, suggested by Lorenzo.
> - Drop Fixes tag, suggested by David and Lorenzo.
> - Updated commit message to be more explicit, suggested by Lorenzo.
> ---
> mm/khugepaged.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 28a843f30b32..fd7e893c998d 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -2837,6 +2837,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 = ALIGN(start, HPAGE_PMD_SIZE);
> + hend = ALIGN_DOWN(end, HPAGE_PMD_SIZE);
> +
> + if (hstart >= hend)
> + return 0;
> +
> cc = kmalloc_obj(*cc);
> if (!cc)
> return -ENOMEM;
> @@ -2846,9 +2852,6 @@ int madvise_collapse(struct vm_area_struct *vma, unsigned long start,
> mmgrab(mm);
> lru_add_drain_all();
>
> - hstart = ALIGN(start, HPAGE_PMD_SIZE);
> - hend = ALIGN_DOWN(end, HPAGE_PMD_SIZE);
> -
> for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
> enum scan_result result = SCAN_FAIL;
>
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-05-13 9:24 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-13 5:54 [PATCH v3] mm/khugepaged: avoid underflow in madvise_collapse for sub-PMD MADV_COLLAPSE Wandun Chen
2026-05-13 7:24 ` David Hildenbrand (Arm)
2026-05-13 9:24 ` Lorenzo Stoakes
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox