mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] mm/vma: fix expansion of VMA_MIXEDMAP_BIT VMAs
@ 2026-10-23 15:17 Lorenzo Stoakes (ARM)
  2026-10-23 17:30 ` Lorenzo Stoakes (ARM)
  2026-10-23 18:11 ` Luiz Capitulino
  0 siblings, 2 replies; 3+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-10-23 15:17 UTC (permalink / raw)
  To: Andrew Morton, Liam R. Howlett, Vlastimil Babka, Jann Horn,
	Pedro Falcato, Jakub Matěna
  Cc: linux-mm, linux-kernel, ljs, Luiz Capitulino, stable

Commit ca3d76b0aa80 ("mm: add merging after mremap resize") combined
merging with expansion of mremap()'d VMAs, however it also unintentionally
disallowed the expansion of unmergeable VMAs.

Previously it was possible to expand a mixed mapping which mremap() had
already ensured was a legal expansion.

This is clearly seen in the mremap() logic, as it checks
vma_is_fixed_mapping() to determine legality of an expansion rather than
vma_can_merge().

Additionally, copy_vma() allows for a failed merge to result in a new VMA
being instantiated if MREMAP_MAYMOVE is specified.

However, this is not attempted, as a failure in vma_merge_extend() is
assumed to be an -ENOMEM failure.

Since vma_merge_extend() is used exclusively by mremap() to perform a merge
and/or expansion, resolve the issue by invoking vma_expand() for
unmergeable VMAs there.

Explicitly assert that the VMA is both non-fixed and a mixed mapping.
This provides double insurance against broken changes in future.

Note that khugepaged_enter_vma() (which vma_merge_new_range() runs) is
redundant in this case, as a mixed mapping is not subject to khugepaged.

Also update the VMA userland tests to add a required predicate.

Reported-by: Luiz Capitulino <luizcap@redhat.com>
Closes: https://lore.kernel.org/all/20261023023106.515365-1-luizcap@redhat.com/
Fixes: ca3d76b0aa80 ("mm: add merging after mremap resize")
Cc: stable@vger.kernel.org
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
Confirmed that this fixes the repro and passes mm selftests and the VMA
userland tests.
---
 mm/vma.c                        | 19 ++++++++++++++++---
 tools/testing/vma/include/dup.h |  5 +++++
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/mm/vma.c b/mm/vma.c
index 1be613044b4a..88ef92ab24ec 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -1851,12 +1851,25 @@ struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi,
 					struct vm_area_struct *vma,
 					unsigned long delta)
 {
-	VMG_VMA_STATE(vmg, vmi, vma, vma, vma->vm_end, vma->vm_end + delta);
+	const unsigned long start =
+		vma_can_merge(vma) ? vma->vm_end : vma->vm_start;
+	VMG_VMA_STATE(vmg, vmi, vma, vma, start, vma->vm_end + delta);
 
-	vmg.next = vma_iter_next_rewind(vmi, NULL);
 	vmg.middle = NULL; /* We use the VMA to populate VMG fields only. */
 
-	return vma_merge_new_range(&vmg);
+	if (vma_can_merge(vma)) {
+		vmg.next = vma_iter_next_rewind(vmi, NULL);
+		return vma_merge_new_range(&vmg);
+	}
+
+	/* Mixed mappings are permitted to be expanded, not merged. */
+	VM_WARN_ON_ONCE(vma_is_fixed_mapping(vma));
+	VM_WARN_ON_ONCE(!vma_test(vma, VMA_MIXEDMAP_BIT));
+	vma_prev(vmi); /* Reposition to start of VMA. */
+	vmg.target = vma;
+	if (vma_expand(&vmg))
+		return NULL;
+	return vma;
 }
 
 void unlink_file_vma_batch_init(struct unlink_vma_file_batch *vb)
diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h
index f449129ea97a..e0eab4eca998 100644
--- a/tools/testing/vma/include/dup.h
+++ b/tools/testing/vma/include/dup.h
@@ -1727,3 +1727,8 @@ static inline bool vma_flags_can_merge(const vma_flags_t *flags)
 	 */
 	return !vma_flags_is_hugetlb(flags);
 }
+
+static inline bool vma_can_merge(const struct vm_area_struct *vma)
+{
+	return vma_flags_can_merge(&vma->flags);
+}

---
base-commit: 2bba28e98578de622ef307d00875aa8f8bca3d10
change-id: 20261023-vma-expansion-fix-6843391177c4

Cheers,
-- 
Lorenzo Stoakes (ARM) <ljs@kernel.org>


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

* Re: [PATCH] mm/vma: fix expansion of VMA_MIXEDMAP_BIT VMAs
  2026-10-23 15:17 [PATCH] mm/vma: fix expansion of VMA_MIXEDMAP_BIT VMAs Lorenzo Stoakes (ARM)
@ 2026-10-23 17:30 ` Lorenzo Stoakes (ARM)
  2026-10-23 18:11 ` Luiz Capitulino
  1 sibling, 0 replies; 3+ messages in thread
From: Lorenzo Stoakes (ARM) @ 2026-10-23 17:30 UTC (permalink / raw)
  To: Andrew Morton, Liam R. Howlett, Vlastimil Babka, Jann Horn,
	Pedro Falcato, Jakub Matěna
  Cc: linux-mm, linux-kernel, Luiz Capitulino, stable

On Fri, Oct 23, 2026 at 04:17:31PM +0100, Lorenzo Stoakes (ARM) wrote:
> Commit ca3d76b0aa80 ("mm: add merging after mremap resize") combined
> merging with expansion of mremap()'d VMAs, however it also unintentionally
> disallowed the expansion of unmergeable VMAs.
>
> Previously it was possible to expand a mixed mapping which mremap() had
> already ensured was a legal expansion.
>
> This is clearly seen in the mremap() logic, as it checks
> vma_is_fixed_mapping() to determine legality of an expansion rather than
> vma_can_merge().
>
> Additionally, copy_vma() allows for a failed merge to result in a new VMA
> being instantiated if MREMAP_MAYMOVE is specified.
>
> However, this is not attempted, as a failure in vma_merge_extend() is
> assumed to be an -ENOMEM failure.

Just to note that VMA_MIXEDMAP_BIT VMAs can _currently_ be expanded if not
attempted to be expanded in-place (e.g. MREMAP_FIXED, or if not enough space to
expand into).

So this patch doesn't re-add the ability to do that per se, it just fixes one
specific means of doing what is already a valid operation.

VMA_MIXEDMAP _expanding_ isn't really a problem - if no fault handler is
installed, faults in the expanded region will SIGBUS, and if one exists it'll be
invoked correctly.

This doesn't work for VMA_PFNMAP_BIT because the page offset might be co-opted
for CoW (!) and thus is unreliable.

And it seems some users do, at least in theory, require it, even if they are
using older kernel revisions :)

> Since vma_merge_extend() is used exclusively by mremap() to perform a merge
> and/or expansion, resolve the issue by invoking vma_expand() for
> unmergeable VMAs there.
>
> Explicitly assert that the VMA is both non-fixed and a mixed mapping.
> This provides double insurance against broken changes in future.
>
> Note that khugepaged_enter_vma() (which vma_merge_new_range() runs) is
> redundant in this case, as a mixed mapping is not subject to khugepaged.
>
> Also update the VMA userland tests to add a required predicate.
>
> Reported-by: Luiz Capitulino <luizcap@redhat.com>
> Closes: https://lore.kernel.org/all/20261023023106.515365-1-luizcap@redhat.com/
> Fixes: ca3d76b0aa80 ("mm: add merging after mremap resize")
> Cc: stable@vger.kernel.org
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
> Confirmed that this fixes the repro and passes mm selftests and the VMA
> userland tests.

--
Cheers, Lorenzo

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

* Re: [PATCH] mm/vma: fix expansion of VMA_MIXEDMAP_BIT VMAs
  2026-10-23 15:17 [PATCH] mm/vma: fix expansion of VMA_MIXEDMAP_BIT VMAs Lorenzo Stoakes (ARM)
  2026-10-23 17:30 ` Lorenzo Stoakes (ARM)
@ 2026-10-23 18:11 ` Luiz Capitulino
  1 sibling, 0 replies; 3+ messages in thread
From: Luiz Capitulino @ 2026-10-23 18:11 UTC (permalink / raw)
  To: Lorenzo Stoakes (ARM),
	Andrew Morton, Liam R. Howlett, Vlastimil Babka, Jann Horn,
	Pedro Falcato, Jakub Matěna
  Cc: linux-mm, linux-kernel, stable



On 10/23/26 11:17 AM, Lorenzo Stoakes (ARM) wrote:
> Commit ca3d76b0aa80 ("mm: add merging after mremap resize") combined
> merging with expansion of mremap()'d VMAs, however it also unintentionally
> disallowed the expansion of unmergeable VMAs.
> 
> Previously it was possible to expand a mixed mapping which mremap() had
> already ensured was a legal expansion.
> 
> This is clearly seen in the mremap() logic, as it checks
> vma_is_fixed_mapping() to determine legality of an expansion rather than
> vma_can_merge().
> 
> Additionally, copy_vma() allows for a failed merge to result in a new VMA
> being instantiated if MREMAP_MAYMOVE is specified.
> 
> However, this is not attempted, as a failure in vma_merge_extend() is
> assumed to be an -ENOMEM failure.
> 
> Since vma_merge_extend() is used exclusively by mremap() to perform a merge
> and/or expansion, resolve the issue by invoking vma_expand() for
> unmergeable VMAs there.
> 
> Explicitly assert that the VMA is both non-fixed and a mixed mapping.
> This provides double insurance against broken changes in future.
> 
> Note that khugepaged_enter_vma() (which vma_merge_new_range() runs) is
> redundant in this case, as a mixed mapping is not subject to khugepaged.
> 
> Also update the VMA userland tests to add a required predicate.
> 
> Reported-by: Luiz Capitulino <luizcap@redhat.com>
> Closes: https://lore.kernel.org/all/20261023023106.515365-1-luizcap@redhat.com/
> Fixes: ca3d76b0aa80 ("mm: add merging after mremap resize")
> Cc: stable@vger.kernel.org
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
> ---
> Confirmed that this fixes the repro and passes mm selftests and the VMA
> userland tests.

Thanks a lot for the ultra fast fix and for the attention you gave to
this issue.

I tested it the same you did and (of course) it worked. I'll work on
testing it on the environment that reproduced the issue originally, but
this may take a bit. I'll report back if there's anything unexpected.

Another detail is that I see that you CC'ed stable (which is of course
correct, since this reproduces since v6.1) but I guess this fix won't
apply since the VMA code has been changing a lot.


> ---
>   mm/vma.c                        | 19 ++++++++++++++++---
>   tools/testing/vma/include/dup.h |  5 +++++
>   2 files changed, 21 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/vma.c b/mm/vma.c
> index 1be613044b4a..88ef92ab24ec 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
> @@ -1851,12 +1851,25 @@ struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi,
>   					struct vm_area_struct *vma,
>   					unsigned long delta)
>   {
> -	VMG_VMA_STATE(vmg, vmi, vma, vma, vma->vm_end, vma->vm_end + delta);
> +	const unsigned long start =
> +		vma_can_merge(vma) ? vma->vm_end : vma->vm_start;
> +	VMG_VMA_STATE(vmg, vmi, vma, vma, start, vma->vm_end + delta);
>   
> -	vmg.next = vma_iter_next_rewind(vmi, NULL);
>   	vmg.middle = NULL; /* We use the VMA to populate VMG fields only. */
>   
> -	return vma_merge_new_range(&vmg);
> +	if (vma_can_merge(vma)) {
> +		vmg.next = vma_iter_next_rewind(vmi, NULL);
> +		return vma_merge_new_range(&vmg);
> +	}
> +
> +	/* Mixed mappings are permitted to be expanded, not merged. */
> +	VM_WARN_ON_ONCE(vma_is_fixed_mapping(vma));
> +	VM_WARN_ON_ONCE(!vma_test(vma, VMA_MIXEDMAP_BIT));
> +	vma_prev(vmi); /* Reposition to start of VMA. */
> +	vmg.target = vma;
> +	if (vma_expand(&vmg))
> +		return NULL;
> +	return vma;
>   }
>   
>   void unlink_file_vma_batch_init(struct unlink_vma_file_batch *vb)
> diff --git a/tools/testing/vma/include/dup.h b/tools/testing/vma/include/dup.h
> index f449129ea97a..e0eab4eca998 100644
> --- a/tools/testing/vma/include/dup.h
> +++ b/tools/testing/vma/include/dup.h
> @@ -1727,3 +1727,8 @@ static inline bool vma_flags_can_merge(const vma_flags_t *flags)
>   	 */
>   	return !vma_flags_is_hugetlb(flags);
>   }
> +
> +static inline bool vma_can_merge(const struct vm_area_struct *vma)
> +{
> +	return vma_flags_can_merge(&vma->flags);
> +}
> 
> ---
> base-commit: 2bba28e98578de622ef307d00875aa8f8bca3d10
> change-id: 20261023-vma-expansion-fix-6843391177c4
> 
> Cheers,


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

end of thread, other threads:[~2026-10-23 18:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-23 15:17 [PATCH] mm/vma: fix expansion of VMA_MIXEDMAP_BIT VMAs Lorenzo Stoakes (ARM)
2026-10-23 17:30 ` Lorenzo Stoakes (ARM)
2026-10-23 18:11 ` Luiz Capitulino

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®