mremap() fails VMA expansion when VM_MIXEDMAP is set
Subsystems (2)
Growing a VM_MIXEDMAP VMA with mremap(), with or without MREMAP_MAYMOVE, fails with -ENOMEM when the address range after the VMA is free.
This regression was introduced in v6.1 by commit ca3d76b0aa80 ("mm: add merging after mremap resize"). This commit changed mremap() to use vma_merge() instead of vma_adjust() for the expansion case. However, vma_merge() returns NULL for a VMA that has any of the VM_SPECIAL flags set.
The issue is also present in latest Linus tree, except that the code has been refactored. In the current tree, the path now is expand_vma_in_place() -> vma_merge_extend() -> vma_merge_new_range(), which performs the check:
/* Special VMAs are unmergeable, also if no prev/next. */ if (vma_flags_test_any_mask(&vmg->vma_flags, VMA_SPECIAL_FLAGS) || (!prev && !next)) return NULL;
This issue was originally reproduced with an external module that sets VM_MIXEDMAP on a VMA because it wants to insert pages with vm_insert_page(). However, there's at least one in-tree reproducer: tcp_mmap().
Note that commit ca3d76b0aa80 introduced other VMA expansion regressions, but they were caught and fixed early:
- 6f12be792fde ("mm, mremap: fix mremap() expanding vma with addr inside vma")
- d014cd7c1c35 ("mm, mremap: fix mremap() expanding for vma's with vm_ops->close()")
Below you'll find a reproducer and an attempted fix.
Reproducer
==========
This reproducer was written by an LLM and uses tcp_mmap(). It reproduces against v6.1 and latest Linus tree (2a08195e4db5). The issue is not reproduced on v6.0.
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/socket.h>
int main(void)
{
int fd, pagesize;
void *p, *ret;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
return 1;
}
pagesize = getpagesize();
/* tcp_mmap() sets VM_MIXEDMAP on this vma. */
p = mmap(NULL, 2 * pagesize, PROT_READ, MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
perror("mmap");
return 1;
}
/* Drop the upper half, so growing back into it can happen in place. */
if (munmap((char *)p + pagesize, pagesize)) {
perror("munmap");
return 1;
}
errno = 0;
ret = mremap(p, pagesize, 2 * pagesize, MREMAP_MAYMOVE);
if (ret == MAP_FAILED) {
printf("BUG REPRODUCED errno=%d\n", errno);
return 1;
}
printf("NOT REPRODUCED\n");
return 0;
}Sketch fix
==========
In the fix below, I started with the same idea as d014cd7c1c35: on v6.1 I made mremap() expand VM_MIXEDMAP VMAs the same way that commit did for VMAs with vm_ops->close(). Then I adapted it to the current tree. It fixes the reproducer and I didn't get regressions in MM kselftests nor the tools/testing/vma test.
However, I'm not posting this as a fix because I'm unfamiliar with the VMA code so I'm not confident this is the right approach.
I really don't mind someone more familiar with the code taking this over.
mm/vma.c +22 -4
@@ -851,9 +851,15 @@ static int commit_merge(struct vma_merge_struct *vmg)return 0 ; } -/* We can only remove VMAs when merging if they do not have a close hook. */ static bool can_merge_remove_vma (struct vm_area_struct *vma) { +if (vma_flags_test (&vma->flags,VMA_MIXEDMAP_BIT )) +return false; + +/* +* We can only remove VMAs when merging if they do not have a +* close hook. +*/ return !vma->vm_ops || !vma->vm_ops->close; }
@@ -1093,6 +1099,20 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(return NULL ; } +static bool can_merge_vma (const struct vma_merge_struct *vmg) +{ +/* VMAs without prev/next are unmergeable */ +if (!vmg->prev && !vmg->next) +return false; + +/* We can't merge VMA_IO, VMA_DONTEXPAND or VMA_PFNMAP */ +if (vma_flags_test_any (&vmg->vma_flags,VMA_IO_BIT ,VMA_DONTEXPAND_BIT , +VMA_PFNMAP_BIT )) +return false; + +return true; +} +/* * vma_merge_new_range - Attempt to merge a new VMA into address space *
@@ -1152,9 +1172,7 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg) vmg->state =VMA_MERGE_NOMERGE ; -/* Special VMAs are unmergeable, also if no prev/next. */ -if (vma_flags_test_any_mask (&vmg->vma_flags,VMA_SPECIAL_FLAGS ) || - (!prev && !next)) +if (!can_merge_vma (vmg))return NULL ; can_merge_left =can_vma_merge_left (vmg);
Vlastimil Babka (SUSE)
reviewerQuoted 7 lines · Luiz Capitulino
Growing a
VM_MIXEDMAPVMA withmremap(), with or withoutMREMAP_MAYMOVE, fails with-ENOMEMwhen the address range after the VMA is free.This regression was introduced in v6.1 by commit
ca3d76b0aa80("mm: add merging after mremap resize"). This commit changedmremap()to usevma_merge()instead ofvma_adjust()for the expansion case. However,vma_merge()returns NULL for a VMA that has any of theVM_SPECIALflags set.
Hi,
6.1 was a rather long time ago. How was it discovered just now? I.e. what use case broke by the change and prompted creating the reproducer?
Thanks,
Vlastimil
Quoted 141 lines
The issue is also present in latest Linus tree, except that the code has been refactored. In the current tree, the path now is
expand_vma_in_place()->vma_merge_extend()->vma_merge_new_range(), which performs the check:/* Special VMAs are unmergeable, also if no prev/next. */ if (vma_flags_test_any_mask(&vmg->vma_flags, VMA_SPECIAL_FLAGS) || (!prev && !next)) return NULL;This issue was originally reproduced with an external module that sets
VM_MIXEDMAPon a VMA because it wants to insert pages withvm_insert_page(). However, there's at least one in-tree reproducer:tcp_mmap().Note that commit
ca3d76b0aa80introduced other VMA expansion regressions, but they were caught and fixed early:-
6f12be792fde("mm, mremap: fixmremap()expanding vma with addr inside vma")
-d014cd7c1c35("mm, mremap: fixmremap()expanding for vma's withvm_ops->close()")Below you'll find a reproducer and an attempted fix.
Reproducer
==========This reproducer was written by an LLM and uses
tcp_mmap(). It reproduces against v6.1 and latest Linus tree (2a08195e4db5). The issue is not reproduced on v6.0.#define
_GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/socket.h>int main(void)
{ int fd, pagesize; void *p, *ret; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); return 1; } pagesize = getpagesize(); /* tcp_mmap() sets VM_MIXEDMAP on this vma. */ p = mmap(NULL, 2 * pagesize, PROT_READ, MAP_SHARED, fd, 0); if (p == MAP_FAILED) { perror("mmap"); return 1; } /* Drop the upper half, so growing back into it can happen in place. */ if (munmap((char *)p + pagesize, pagesize)) { perror("munmap"); return 1; } errno = 0; ret = mremap(p, pagesize, 2 * pagesize, MREMAP_MAYMOVE); if (ret == MAP_FAILED) { printf("BUG REPRODUCED errno=%d\n", errno); return 1; } printf("NOT REPRODUCED\n"); return 0; }Sketch fix
==========In the fix below, I started with the same idea as
d014cd7c1c35: on v6.1 I mademremap()expandVM_MIXEDMAPVMAs the same way that commit did for VMAs withvm_ops->close(). Then I adapted it to the current tree. It fixes the reproducer and I didn't get regressions in MM kselftests nor thetools/testing/vmatest.However, I'm not posting this as a fix because I'm unfamiliar with the VMA code so I'm not confident this is the right approach.
I really don't mind someone more familiar with the code taking this over.
mm/vma.c +22
-4@@ -851,9 +851,15 @@ static int commit_merge(struct vma_merge_struct *vmg)return 0 ; } -/* We can only remove VMAs when merging if they do not have a close hook. */ static bool can_merge_remove_vma (struct vm_area_struct *vma) { +if (vma_flags_test (&vma->flags,VMA_MIXEDMAP_BIT )) +return false; + +/* +* We can only remove VMAs when merging if they do not have a +* close hook. +*/ return !vma->vm_ops || !vma->vm_ops->close; }@@ -1093,6 +1099,20 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(return NULL ; } +static bool can_merge_vma (const struct vma_merge_struct *vmg) +{ +/* VMAs without prev/next are unmergeable */ +if (!vmg->prev && !vmg->next) +return false; + +/* We can't merge VMA_IO, VMA_DONTEXPAND or VMA_PFNMAP */ +if (vma_flags_test_any (&vmg->vma_flags,VMA_IO_BIT ,VMA_DONTEXPAND_BIT , +VMA_PFNMAP_BIT )) +return false; + +return true; +} +/* * vma_merge_new_range - Attempt to merge a new VMA into address space * @@ -1152,9 +1172,7 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg) vmg->state =VMA_MERGE_NOMERGE ; -/* Special VMAs are unmergeable, also if no prev/next. */ -if (vma_flags_test_any_mask (&vmg->vma_flags,VMA_SPECIAL_FLAGS ) || - (!prev && !next)) +if (!can_merge_vma (vmg))return NULL ; can_merge_left =can_vma_merge_left (vmg);
Lorenzo Stoakes (ARM)
maintainerQuoted 7 lines · Vlastimil Babka (SUSE)
Quoted 4 lines · Luiz Capitulino
This regression was introduced in v6.1 by commit
ca3d76b0aa80("mm: add merging after mremap resize"). This commit changedmremap()to usevma_merge()instead ofvma_adjust()for the expansion case. However,vma_merge()returns NULL for a VMA that has any of theVM_SPECIALflags set.
Quoted 7 lines
Hi,
6.1 was a rather long time ago. How was it discovered just now? I.e. what use case broke by the change and prompted creating the reproducer?
Vlastimil
Thanks,
Thanks, I have the same questions :)
Do always include that kind of information and whether it was an LLM-discovered thing or whether you had an actual workload that was impacted, etc.
--
Cheers, Lorenzo
Quoted 13 lines · Lorenzo Stoakes (ARM)
Quoted 12 lines · Vlastimil Babka (SUSE)
Quoted 4 lines · Luiz Capitulino
This regression was introduced in v6.1 by commit
ca3d76b0aa80("mm: add merging after mremap resize"). This commit changedmremap()to usevma_merge()instead ofvma_adjust()for the expansion case. However,vma_merge()returns NULL for a VMA that has any of theVM_SPECIALflags set.Hi,
6.1 was a rather long time ago. How was it discovered just now? I.e. what use case broke by the change and prompted creating the reproducer?
Quoted 7 lines
Quoted 2 lines
Vlastimil
Thanks,Thanks, I have the same questions :)
Do always include that kind of information and whether it was an LLM-discovered thing or whether you had an actual workload that was impacted, etc.
Will do the next time, I claried the use-case in my response to Vlastimil.
Quoted 3 lines
--
Cheers, Lorenzo
Quoted 9 lines · Vlastimil Babka (SUSE)
Quoted 7 lines · Luiz Capitulino
Growing a
VM_MIXEDMAPVMA withmremap(), with or withoutMREMAP_MAYMOVE, fails with-ENOMEMwhen the address range after the VMA is free.This regression was introduced in v6.1 by commit
ca3d76b0aa80("mm: add merging after mremap resize"). This commit changedmremap()to usevma_merge()instead ofvma_adjust()for the expansion case. However,vma_merge()returns NULL for a VMA that has any of theVM_SPECIALflags set.
Quoted 4 lines
Hi,
6.1 was a rather long time ago. How was it discovered just now? I.e. what use case broke by the change and prompted creating the reproducer?
The use-case that broke was an application running on an older kernel and accessing a GPFS volume/FS. I was told that GPFS sets VM_MIXEDMAP on a VMA because it inserts pages in the process address space with vm_insert_page(). I then asked an LLM to check if there was anything in tree that would reproduce that, it found the tcp_mmap() case and created the attached reproducer. Btw, the LLM also told me there are more cases in-tree that can reproduce this (but they require special hardware or setup so I didn't investigate).
Quoted 146 lines
Thanks,
VlastimilQuoted 141 lines
The issue is also present in latest Linus tree, except that the code has been refactored. In the current tree, the path now is
expand_vma_in_place()->vma_merge_extend()->vma_merge_new_range(), which performs the check:/* Special VMAs are unmergeable, also if no prev/next. */ if (vma_flags_test_any_mask(&vmg->vma_flags, VMA_SPECIAL_FLAGS) || (!prev && !next)) return NULL;This issue was originally reproduced with an external module that sets
VM_MIXEDMAPon a VMA because it wants to insert pages withvm_insert_page(). However, there's at least one in-tree reproducer:tcp_mmap().Note that commit
ca3d76b0aa80introduced other VMA expansion regressions, but they were caught and fixed early:-
6f12be792fde("mm, mremap: fixmremap()expanding vma with addr inside vma")
-d014cd7c1c35("mm, mremap: fixmremap()expanding for vma's withvm_ops->close()")Below you'll find a reproducer and an attempted fix.
Reproducer
==========This reproducer was written by an LLM and uses
tcp_mmap(). It reproduces against v6.1 and latest Linus tree (2a08195e4db5). The issue is not reproduced on v6.0.#define
_GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/socket.h>int main(void)
{ int fd, pagesize; void *p, *ret; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { perror("socket"); return 1; } pagesize = getpagesize(); /* tcp_mmap() sets VM_MIXEDMAP on this vma. */ p = mmap(NULL, 2 * pagesize, PROT_READ, MAP_SHARED, fd, 0); if (p == MAP_FAILED) { perror("mmap"); return 1; } /* Drop the upper half, so growing back into it can happen in place. */ if (munmap((char *)p + pagesize, pagesize)) { perror("munmap"); return 1; } errno = 0; ret = mremap(p, pagesize, 2 * pagesize, MREMAP_MAYMOVE); if (ret == MAP_FAILED) { printf("BUG REPRODUCED errno=%d\n", errno); return 1; } printf("NOT REPRODUCED\n"); return 0; }Sketch fix
==========In the fix below, I started with the same idea as
d014cd7c1c35: on v6.1 I mademremap()expandVM_MIXEDMAPVMAs the same way that commit did for VMAs withvm_ops->close(). Then I adapted it to the current tree. It fixes the reproducer and I didn't get regressions in MM kselftests nor thetools/testing/vmatest.However, I'm not posting this as a fix because I'm unfamiliar with the VMA code so I'm not confident this is the right approach.
I really don't mind someone more familiar with the code taking this over.
mm/vma.c +22
-4@@ -851,9 +851,15 @@ static int commit_merge(struct vma_merge_struct *vmg)return 0 ; } -/* We can only remove VMAs when merging if they do not have a close hook. */ static bool can_merge_remove_vma (struct vm_area_struct *vma) { +if (vma_flags_test (&vma->flags,VMA_MIXEDMAP_BIT )) +return false; + +/* +* We can only remove VMAs when merging if they do not have a +* close hook. +*/ return !vma->vm_ops || !vma->vm_ops->close; }@@ -1093,6 +1099,20 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(return NULL ; } +static bool can_merge_vma (const struct vma_merge_struct *vmg) +{ +/* VMAs without prev/next are unmergeable */ +if (!vmg->prev && !vmg->next) +return false; + +/* We can't merge VMA_IO, VMA_DONTEXPAND or VMA_PFNMAP */ +if (vma_flags_test_any (&vmg->vma_flags,VMA_IO_BIT ,VMA_DONTEXPAND_BIT , +VMA_PFNMAP_BIT )) +return false; + +return true; +} +/* * vma_merge_new_range - Attempt to merge a new VMA into address space * @@ -1152,9 +1172,7 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg) vmg->state =VMA_MERGE_NOMERGE ; -/* Special VMAs are unmergeable, also if no prev/next. */ -if (vma_flags_test_any_mask (&vmg->vma_flags,VMA_SPECIAL_FLAGS ) || - (!prev && !next)) +if (!can_merge_vma (vmg))return NULL ; can_merge_left =can_vma_merge_left (vmg);
Pedro Falcato
reviewerQuoted 12 lines · Luiz Capitulino
Quoted 9 lines · Vlastimil Babka (SUSE)
Quoted 7 lines · Luiz Capitulino
Growing a
VM_MIXEDMAPVMA withmremap(), with or withoutMREMAP_MAYMOVE, fails with-ENOMEMwhen the address range after the VMA is free.This regression was introduced in v6.1 by commit
ca3d76b0aa80("mm: add merging after mremap resize"). This commit changedmremap()to usevma_merge()instead ofvma_adjust()for the expansion case. However,vma_merge()returns NULL for a VMA that has any of theVM_SPECIALflags set.
Quoted 7 lines
Quoted 4 lines
Hi,
6.1 was a rather long time ago. How was it discovered just now? I.e. what use case broke by the change and prompted creating the reproducer?
The use-case that broke was an application running on an older kernel and accessing a GPFS volume/FS. I was told that GPFS sets
VM_MIXEDMAPon
So the application is growing a file VMA with mremap?
Quoted 2 lines
a VMA because it inserts pages in the process address space with
vm_insert_page(). I then asked an LLM to check if there was anything in
FWIW this is really odd for a filesystem to do. I see we have two filesystems upstream (at least in 7.3) that do this; that is, cramfs and FUSE DAX. This is arguably weird and should be fixed. Perhaps the VM side isn't giving filesystems the right flag here.
Signature · 1 line
Pedro
Quoted 15 lines · Pedro Falcato
Quoted 14 lines · Luiz Capitulino
Quoted 11 lines · Vlastimil Babka (SUSE)
Quoted 7 lines · Luiz Capitulino
Growing a
VM_MIXEDMAPVMA withmremap(), with or withoutMREMAP_MAYMOVE, fails with-ENOMEMwhen the address range after the VMA is free.This regression was introduced in v6.1 by commit
ca3d76b0aa80("mm: add merging after mremap resize"). This commit changedmremap()to usevma_merge()instead ofvma_adjust()for the expansion case. However,vma_merge()returns NULL for a VMA that has any of theVM_SPECIALflags set.Hi,
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
6.1 was a rather long time ago. How was it discovered just now? I.e. what use case broke by the change and prompted creating the reproducer?
The use-case that broke was an application running on an older kernel and accessing a GPFS volume/FS. I was told that GPFS sets
VM_MIXEDMAPonSo the application is growing a file VMA with mremap?
To be honest, I don't know because I don't have access to the application/production environment that reproduced this. I got a detailed report with debugging information that allowed me to identify the regression.
I can try to get this information if it's important for what you're proposing below.
Quoted 8 lines
Quoted 2 lines
a VMA because it inserts pages in the process address space with
vm_insert_page(). I then asked an LLM to check if there was anything inFWIW this is really odd for a filesystem to do. I see we have two filesystems upstream (at least in 7.3) that do this; that is, cramfs and FUSE DAX. This is arguably weird and should be fixed. Perhaps the VM side isn't giving filesystems the right flag here.
Lorenzo Stoakes (ARM)
maintainerI'll look into this but obviously we won't make mixed map regions mergeable (nor were they ever), that's completely broken.
The most recent version of the mm code (which is going to be send to Linus soon in this merge window) documents why:
/**
* vma_flags_can_merge() - Do the specified VMA flags permit the VMA to be
* merged with another?
* @flags: The VMA flags to test.
* Returns: true if the flags permit merging, false otherwise. */
static inline bool vma_flags_can_merge(const vma_flags_t *flags)
{
/*
* VMA merging assumes that a VMA's flags and fields completely describe
* its state, which is only the case for mm-backed mappings.
*
* Mappings which are not mm-managed may have established state upon
* mapping not embodied in any attribute of the VMA.
*
* Additionally, private (CoW) PFN maps encode the source PFN of the
* range in vma->vm_pgoff, which may otherwise cause spurious merges.
*/
if (!vma_flags_is_mm_backed(flags))
return false;
/*
* hugetlb mappings contain per-VMA state, so their flags and fields do
* not completely describe them.
*/
return !vma_flags_is_hugetlb(flags);
}I'll dig in + get back to you.
--
Cheers, Lorenzo
Quoted 33 lines · Lorenzo Stoakes (ARM)
I'll look into this but obviously we won't make mixed map regions mergeable (nor were they ever), that's completely broken.
The most recent version of the mm code (which is going to be send to Linus soon in this merge window) documents why:
/**
*vma_flags_can_merge()- Do the specified VMA flags permit the VMA to be
* merged with another?
* @flags: The VMA flags to test.
* Returns: true if the flags permit merging, false otherwise. */
static inline boolvma_flags_can_merge(const vma_flags_t *flags){ /* * VMA merging assumes that a VMA's flags and fields completely describe * its state, which is only the case for mm-backed mappings. * * Mappings which are not mm-managed may have established state upon * mapping not embodied in any attribute of the VMA. * * Additionally, private (CoW) PFN maps encode the source PFN of the * range in vma->vm_pgoff, which may otherwise cause spurious merges. */ if (!vma_flags_is_mm_backed(flags)) return false; /* * hugetlb mappings contain per-VMA state, so their flags and fields do * not completely describe them. */ return !vma_flags_is_hugetlb(flags); }
Quoted 1 line
I'll dig in + get back to you.
Thanks Lorenzo, I really appreciate it.
Quoted 3 lines
--
Cheers, Lorenzo
Lorenzo Stoakes (ARM)
maintainerQuoted 7 lines · Luiz Capitulino
Quoted 1 line · Lorenzo Stoakes (ARM)
I'll dig in + get back to you.
Thanks Lorenzo, I really appreciate it.
You're welcome!
This report looks valid, essentially a pure expand should be allowed for mixed mappings (with mremap() having already asserted it is a valid expansion).
Writing a fix now.
--
Cheers, Lorenzo
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] mm: vma_merge_new_range() merges VM_MIXEDMAP VMAs in mmap() and mremap() - [Medium] mm: vma…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] mm: vma_merge_new_range() merges VM_MIXEDMAP VMAs in mmap() and mremap()
- [Medium] mm: vma: can_merge_vma() breaks test_vma_merge_special_flags() for VM_MIXEDMAP --
commit b9b12caabde38874f12fe6aa692b1dcf956d3f4a
Author: Luiz Capitulino <[email protected]>
mremap() fails VMA expansion when VM_MIXEDMAP is set
This patch allows vma_merge_new_range() to merge VM_MIXEDMAP VMAs while updating can_merge_remove_vma() to reject removing VM_MIXEDMAP VMAs so that in-place VMA expansion during mremap() can succeed.
Quoted 28 lines
mm/vma.c +10
-1@@ -851,9 +851,15 @@ static int commit_merge(struct vma_merge_struct *vmg)return 0 ; } -/* We can only remove VMAs when merging if they do not have a close hook. */ static bool can_merge_remove_vma (struct vm_area_struct *vma) { +if (vma_flags_test (&vma->flags,VMA_MIXEDMAP_BIT )) +return false; + +/* +* We can only remove VMAs when merging if they do not have a +* close hook. +*/ return !vma->vm_ops || !vma->vm_ops->close; }@@ -1093,6 +1099,20 @@ static __must_check struct vm_area_struct *vma_merge_existing_range(return NULL ; } +static bool can_merge_vma (const struct vma_merge_struct *vmg) +{ +/* VMAs without prev/next are unmergeable */
mm/vma.c static __must_check struct vm_area_struct *vma_merge_existing_range( · line 1114
Quoted 10 lines
mm/vma.c +10
-0+if (!vmg->prev && !vmg->next) +return false; + +/* We can't merge VMA_IO, VMA_DONTEXPAND or VMA_PFNMAP */ +if (vma_flags_test_any (&vmg->vma_flags,VMA_IO_BIT ,VMA_DONTEXPAND_BIT , +VMA_PFNMAP_BIT )) +return false; + +return true; +}
[Severity: High]
Does omitting VMA_MIXEDMAP_BIT from can_merge_vma() cause unintended merges of distinct VM_MIXEDMAP VMAs during mmap() and mremap() moves?
In vma_merge_new_range(), can_merge_remove_vma(next) is only checked when both can_merge_left and can_merge_right are true, and even then it only reduces a 3-way merge to a 2-way merge into prev:
mm/vma.c:vma_merge_new_range() {
...
if (can_merge_right) {
vmg->end = next->vm_end;
vmg->target = next;
}
if (can_merge_left) {
vmg->start = prev->vm_start;
vmg->target = prev;
...
if (can_merge_right && !can_merge_remove_vma(next))
vmg->end = end;
...
}Because vma_merge_new_range() is called by __mmap_region() and copy_vma() in addition to vma_merge_extend(), allowing VMA_MIXEDMAP_BIT here affects two other paths:
1. In __mmap_region(), when a driver's f_op->mmap_prepare callback sets VMA_MIXEDMAP_BIT (for example via mmap_action_map_kernel_pages() -> call_mmap_prepare() -> mmap_action_prepare() -> map_kernel_pages_prepare()) adjacent to a compatible existing VMA, vma_merge_new_range() will now expand prev or next instead of returning NULL:
mm/vma.c:__mmap_region() {
...
if (map.prev || map.next) {
VMG_MMAP_STATE(vmg, &map, /* vma = */ NULL);
vma = vma_merge_new_range(&vmg);
}
if (!vma) {
error = __mmap_new_vma(&map, &vma, &desc.action);
if (error)
goto unacct_error;
allocated_new = true;
}
if (have_mmap_prepare && !map_is_anon(&map) && allocated_new)
set_vma_user_defined_fields(vma, &map);
__mmap_complete(&map, vma);
if (have_mmap_prepare && allocated_new) {
error = mmap_action_complete(vma, &desc.action,
/*is_compat=*/false);
...
}When vma_merge_new_range() returns a merged VMA, allocated_new remains false, skipping both set_vma_user_defined_fields() and mmap_action_complete() (which skips map_kernel_pages_complete() and call_vma_mapped()).
2. When moving a VM_MIXEDMAP VMA via mremap() with MREMAP_MAYMOVE or MREMAP_FIXED adjacent to another compatible VM_MIXEDMAP VMA, move_vma() -> copy_vma_and_data() -> copy_vma() calls vma_merge_copied_range() -> vma_merge_new_range(). The moved range is merged into the adjacent VMA without checking can_merge_remove_vma(vmg->copied_from) and without calling vm_ops->open() in copy_vma(), after which move_vma() calls unmap_source_vma() -> do_vmi_munmap() and invokes vm_ops->close() on the source VMA.
Also, since vma_merge_existing_range() still rejects all VMA_SPECIAL_FLAGS upfront, is the VMA_MIXEDMAP_BIT check in can_merge_remove_vma() ever reachable from vma_merge_existing_range()?
mm/vma.c struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg) · line 1176
Quoted 13 lines
mm/vma.c +2
-3+/* * vma_merge_new_range - Attempt to merge a new VMA into address space * @@ -1152,9 +1172,7 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg) vmg->state =VMA_MERGE_NOMERGE ; -/* Special VMAs are unmergeable, also if no prev/next. */ -if (vma_flags_test_any_mask (&vmg->vma_flags,VMA_SPECIAL_FLAGS ) || - (!prev && !next)) +if (!can_merge_vma (vmg))return NULL ;
[Severity: Medium]
Does this change cause test_vma_merge_special_flags() in tools/testing/vma/tests/merge.c to fail on the VMA_MIXEDMAP_BIT iteration?
In test_vma_merge_special_flags(), the loop tests each flag in special_flags[] (including VMA_MIXEDMAP_BIT) against merge_new() -> vma_merge_new_range() when adjacent to vma_left:
tools/testing/vma/tests/merge.c:test_vma_merge_special_flags() {
...
vmg_set_range(&vmg, 0x3000, 0x4000, 3, vma_flags);
for (i = 0; i < ARRAY_SIZE(special_flags); i++) {
vma_flag_t special_flag = special_flags[i];
vma_flags_t flags = vma_flags;
vma_flags_set(&flags, special_flag);
vma_left->flags = flags;
vmg.vma_flags = flags;
vma = merge_new(&vmg);
ASSERT_EQ(vma, NULL);
ASSERT_EQ(vmg.state, VMA_MERGE_NOMERGE);
}
...
}Because can_merge_vma() allows VMA_MIXEDMAP_BIT and vmg.prev is vma_left with matching flags and pgoff, can_vma_merge_left() and vma_expand() succeed, returning vma_left with VMA_MERGE_SUCCESS and tripping the assertions.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1