tools/testing/vma: cover hole filling through __mmap_region()
Subsystems (2)
PATCH 1/1 tools/testing/vma: cover hole filling through __mmap_region()
The mmap tests extend existing mappings one neighbor at a time, while merge tests construct merge state directly. Neither exercises filling a hole between compatible mappings through the mmap setup and completion path.
Fill a gap through __mmap_region() and require both neighbors to merge into one VMA. Repeat with only the new mapping's execute permission set and require three separate VMAs. Check boundaries, permissions, page offsets, map_count and tree lookups across the mapped pages.
The full VMA test suite passes all 28 tests with ASan and UBSan enabled.
--- tools/testing/vma/tests/mmap.c | 75 ++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)
tools/testing/vma/tests/mmap.c +75 -0
@@ -45,7 +45,82 @@ static bool test_mmap_region_basic(void)return true; } +static bool mmap_region_fill_hole (bool merge) +{ +const vma_flags_t vma_flags =mk_vma_flags (VMA_READ_BIT ,VMA_WRITE_BIT , +VMA_MAYREAD_BIT ,VMA_MAYWRITE_BIT ,VMA_MAYEXEC_BIT ); +vma_flags_t hole_flags = vma_flags; +struct mm_struct mm = {}; +struct vm_area_struct *vma; +unsigned long addr; +int count =0 ; +VMA_ITERATOR (vmi, &mm,0 ); + + current->mm = &mm; +if (!merge) +vma_flags_set (&hole_flags,VMA_EXEC_BIT ); + +/* Leave a hole between two otherwise mergeable mappings. */ + addr =__mmap_region (NULL ,0x300000 ,0x3000 , vma_flags,0x300 ,NULL ); +ASSERT_EQ (addr,0x300000 ); + addr =__mmap_region (NULL ,0x306000 ,0x3000 , vma_flags,0x306 ,NULL ); +ASSERT_EQ (addr,0x306000 ); +ASSERT_EQ (mm.map_count,2 ); +vma_iter_set (&vmi,0x303000 ); +ASSERT_EQ (vma_iter_load (&vmi),NULL ); +vma_iter_set (&vmi,0x305fff ); +ASSERT_EQ (vma_iter_load (&vmi),NULL ); + +/* A single flag difference must prevent merging with either neighbor. */ + addr =__mmap_region (NULL ,0x303000 ,0x3000 , hole_flags,0x303 ,NULL ); +ASSERT_EQ (addr,0x303000 ); +ASSERT_EQ (mm.map_count, merge ?1 :3 ); + +vma_iter_set (&vmi,0 ); +for_each_vma (vmi, vma) { +unsigned long start =0x300000 + count *0x3000 ; +unsigned long end = merge ?0x309000 : start +0x3000 ; +VMA_ITERATOR (lookup, &mm, start); + +ASSERT_EQ (vma->vm_start, start); +ASSERT_EQ (vma->vm_end, end); +ASSERT_EQ (vma_start_pgoff (vma), start >>PAGE_SHIFT ); +ASSERT_EQ (vma_start_anon_pgoff (vma), start >>PAGE_SHIFT ); +ASSERT_TRUE (vma_test_all (vma,VMA_READ_BIT ,VMA_WRITE_BIT , +VMA_MAYREAD_BIT ,VMA_MAYWRITE_BIT , +VMA_MAYEXEC_BIT )); +ASSERT_EQ (vma_test (vma,VMA_EXEC_BIT ), !merge && count ==1 ); +for (addr = start; addr < end; addr +=PAGE_SIZE ) { +vma_iter_set (&lookup, addr); +ASSERT_EQ (vma_iter_load (&lookup), vma); +vma_iter_set (&lookup, addr +PAGE_SIZE -1 ); +ASSERT_EQ (vma_iter_load (&lookup), vma); + } + count++; + } +ASSERT_EQ (count, mm.map_count); +vma_iter_set (&vmi,0x2fffff ); +ASSERT_EQ (vma_iter_load (&vmi),NULL ); +vma_iter_set (&vmi,0x309000 ); +ASSERT_EQ (vma_iter_load (&vmi),NULL ); + +ASSERT_EQ (cleanup_mm (&mm, &vmi), count); +return true; +} + +static bool test_mmap_region_fill_hole_merge (void ) +{ +return mmap_region_fill_hole (true); +} + +static bool test_mmap_region_fill_hole_flags_mismatch (void ) +{ +return mmap_region_fill_hole (false); +} +static void run_mmap_tests (int *num_tests,int *num_fail) {TEST (mmap_region_basic); +TEST (mmap_region_fill_hole_merge); +TEST (mmap_region_fill_hole_flags_mismatch); }
Signature · 1 line
2.55.0
Lorenzo Stoakes (ARM)
maintainerQuoted 5 lines · Tianyi Chen
The mmap tests extend existing mappings one neighbor at a time, while merge tests construct merge state directly. Neither exercises filling a hole between compatible mappings through the mmap setup and completion path.
Quoted 8 lines
Fill a gap through
__mmap_region()and require both neighbors to merge into one VMA. Repeat with only the new mapping's execute permission set and require three separate VMAs. Check boundaries, permissions, page offsets,map_countand tree lookups across the mapped pages.The full VMA test suite passes all 28 tests with ASan and UBSan enabled.
Assisted-by LLM
Thanks for adding the tag, always much appreciated!
I may actually set my own LLM loose on these tests to expand some more. Is a good area for such work I think.
Though it still needs massaging to get good code :)
Quoted 1 line
Signed-off-by Tianyi Chen <[email protected]>
General idea seems reasonable to me, and never any harm in adding more tests :)
A bunch of stuff to fix below, but with those addressed patch should be good.
Also please rebase this on the mm-unstable branch of Andrew's tree:
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git/
As there is a minor conflict with my upcoming work :)
Quoted 9 lines
--- tools/testing/vma/tests/mmap.c | 75 ++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)tools/testing/vma/tests/mmap.c +0
-0
tools/testing/vma/tests/mmap.c static bool test_mmap_region_basic(void) · line 52
Quoted 8 lines
tools/testing/vma/tests/mmap.c +5
-0@@ -45,7 +45,82 @@ static bool test_mmap_region_basic(void)return true; } +static bool mmap_region_fill_hole (bool merge) +{ +const vma_flags_t vma_flags =mk_vma_flags (VMA_READ_BIT ,VMA_WRITE_BIT , +VMA_MAYREAD_BIT ,VMA_MAYWRITE_BIT ,VMA_MAYEXEC_BIT ); +vma_flags_t hole_flags = vma_flags;
Hole is the wrong word, maybe 'middle_flags'?
Quoted 6 lines
tools/testing/vma/tests/mmap.c +6
-0+struct mm_struct mm = {}; +struct vm_area_struct *vma; +unsigned long addr; +int count =0 ; +VMA_ITERATOR (vmi, &mm,0 ); +
tools/testing/vma/tests/mmap.c static bool test_mmap_region_basic(void) · line 68
Quoted 10 lines
tools/testing/vma/tests/mmap.c +10
-0+ current->mm = &mm; +if (!merge) +vma_flags_set (&hole_flags,VMA_EXEC_BIT ); + +/* Leave a hole between two otherwise mergeable mappings. */ + addr =__mmap_region (NULL ,0x300000 ,0x3000 , vma_flags,0x300 ,NULL ); +ASSERT_EQ (addr,0x300000 ); + addr =__mmap_region (NULL ,0x306000 ,0x3000 , vma_flags,0x306 ,NULL ); +ASSERT_EQ (addr,0x306000 ); +ASSERT_EQ (mm.map_count,2 );
Everything here is reasonable but can you please add comments like other tests like:
/* Map at 0x306000, length 0x3000. */
etc.
tools/testing/vma/tests/mmap.c static bool test_mmap_region_basic(void) · line 72
Quoted 4 lines
tools/testing/vma/tests/mmap.c +4
-0+vma_iter_set (&vmi,0x303000 ); +ASSERT_EQ (vma_iter_load (&vmi),NULL ); +vma_iter_set (&vmi,0x305fff ); +ASSERT_EQ (vma_iter_load (&vmi),NULL );
Let's drop these 4 lines they're a bit useless I think.
tools/testing/vma/tests/mmap.c static bool test_mmap_region_basic(void) · line 74
Quoted 2 lines
tools/testing/vma/tests/mmap.c +2
-0+ +/* A single flag difference must prevent merging with either neighbor. */
Similar to above re: comment.
Also worth saying
tools/testing/vma/tests/mmap.c static bool test_mmap_region_basic(void) · line 82
Quoted 8 lines
tools/testing/vma/tests/mmap.c +8
-0+ addr =__mmap_region (NULL ,0x303000 ,0x3000 , hole_flags,0x303 ,NULL ); +ASSERT_EQ (addr,0x303000 ); +ASSERT_EQ (mm.map_count, merge ?1 :3 ); + +vma_iter_set (&vmi,0 ); +for_each_vma (vmi, vma) { +unsigned long start =0x300000 + count *0x3000 ; +unsigned long end = merge ?0x309000 : start +0x3000 ;
NIT: Can we make these const please?
tools/testing/vma/tests/mmap.c static bool test_mmap_region_basic(void) · line 88
Quoted 6 lines
tools/testing/vma/tests/mmap.c +6
-0+VMA_ITERATOR (lookup, &mm, start); + +ASSERT_EQ (vma->vm_start, start); +ASSERT_EQ (vma->vm_end, end); +ASSERT_EQ (vma_start_pgoff (vma), start >>PAGE_SHIFT ); +ASSERT_EQ (vma_start_anon_pgoff (vma), start >>PAGE_SHIFT );
Newline here maybe as basic stuff above.
tools/testing/vma/tests/mmap.c static bool test_mmap_region_basic(void) · line 92
Quoted 4 lines
tools/testing/vma/tests/mmap.c +4
-0+ASSERT_TRUE (vma_test_all (vma,VMA_READ_BIT ,VMA_WRITE_BIT , +VMA_MAYREAD_BIT ,VMA_MAYWRITE_BIT , +VMA_MAYEXEC_BIT )); +ASSERT_EQ (vma_test (vma,VMA_EXEC_BIT ), !merge && count ==1 );
Also can we separate out the !merge && count bit? So put this at the start of the for_each_vma() block:
/* If testing the non-merge case, middle VMA will be set VMA_EXEC. */ const bool is_middle_vma = count == 1; const expect_exec_vma = is_middle_vma && !merge;
Then here:
ASSERT_EQ(vma_test(vma, VMA_EXEC_BIT), expect_exec_vma);
Also a newline here would be nice.
tools/testing/vma/tests/mmap.c static bool test_mmap_region_basic(void) · line 98
Quoted 6 lines
tools/testing/vma/tests/mmap.c +6
-0+for (addr = start; addr < end; addr +=PAGE_SIZE ) { +vma_iter_set (&lookup, addr); +ASSERT_EQ (vma_iter_load (&lookup), vma); +vma_iter_set (&lookup, addr +PAGE_SIZE -1 ); +ASSERT_EQ (vma_iter_load (&lookup), vma); + }
Let's drop this entire block please I don't think it's achieving anything useful.
tools/testing/vma/tests/mmap.c static bool test_mmap_region_basic(void) · line 100
Quoted 2 lines
tools/testing/vma/tests/mmap.c +2
-0+ count++; + }
Newline here please.
tools/testing/vma/tests/mmap.c static bool test_mmap_region_basic(void) · line 105
Quoted 5 lines
tools/testing/vma/tests/mmap.c +5
-0+ASSERT_EQ (count, mm.map_count); +vma_iter_set (&vmi,0x2fffff ); +ASSERT_EQ (vma_iter_load (&vmi),NULL ); +vma_iter_set (&vmi,0x309000 ); +ASSERT_EQ (vma_iter_load (&vmi),NULL );
Again let's drop this block, it's not useful I don't think.
Quoted 22 lines
tools/testing/vma/tests/mmap.c +17
-1+ +ASSERT_EQ (cleanup_mm (&mm, &vmi), count); +return true; +} + +static bool test_mmap_region_fill_hole_merge (void ) +{ +return mmap_region_fill_hole (true); +} + +static bool test_mmap_region_fill_hole_flags_mismatch (void ) +{ +return mmap_region_fill_hole (false); +} +static void run_mmap_tests (int *num_tests,int *num_fail) {TEST (mmap_region_basic); +TEST (mmap_region_fill_hole_merge); +TEST (mmap_region_fill_hole_flags_mismatch); } --
Quoted 2 lines
2.55.0
--
Cheers, Lorenzo
Thanks for the detailed review.
I have rebased onto mm-unstable and incorporated the requested naming, comments, const qualifiers and simplifications, while retaining the new /dev/zero test. All 29 VMA tests pass with ASan and UBSan enabled.
The revised v2 patch is here:
https://lore.kernel.org/r/[email protected]
Thanks,
Tianyi
PATCH v2 1/1 tools/testing/vma: cover hole filling through __mmap_region()
In reply to Tianyi ChenThe mmap tests extend existing mappings one neighbor at a time, while merge tests construct merge state directly. Neither exercises filling a hole between compatible mappings through the mmap setup and completion path.
Fill a gap through __mmap_region() and require both neighbors to merge into one VMA. Repeat with only the new mapping's execute permission set and require three separate VMAs. Check boundaries, permissions, page offsets, map_count and cleanup.
---
Changes in v2:
- Rebase onto mm-unstable, preserving the new /dev/zero test.
- Rename hole_flags to middle_flags and describe each mapping's address and length in its comment.
- Remove the redundant hole, per-page and outer-boundary iterator checks.
- Use const bounds and named booleans for the middle VMA's expected execute permission, and separate assertion groups for readability.
Validation: all 29 VMA tests passed with ASan and UBSan enabled, built with clang 20 using the existing VMA Makefile.
v1: https://lore.kernel.org/r/[email protected]
Review: https://lore.kernel.org/r/ap5pmX-RddQ-c90O@gremlin
tools/testing/vma/tests/mmap.c | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)
tools/testing/vma/tests/mmap.c +68 -0
@@ -45,6 +45,72 @@ static bool test_mmap_region_basic(void)return true; } +static bool mmap_region_fill_hole (bool merge) +{ +const vma_flags_t vma_flags =mk_vma_flags (VMA_READ_BIT ,VMA_WRITE_BIT , +VMA_MAYREAD_BIT ,VMA_MAYWRITE_BIT ,VMA_MAYEXEC_BIT ); +vma_flags_t middle_flags = vma_flags; +struct mm_struct mm = {}; +struct vm_area_struct *vma; +unsigned long addr; +int count =0 ; +VMA_ITERATOR (vmi, &mm,0 ); + + current->mm = &mm; +if (!merge) +vma_flags_set (&middle_flags,VMA_EXEC_BIT ); + +/* Map at 0x300000, length 0x3000. */ + addr =__mmap_region (NULL ,0x300000 ,0x3000 , vma_flags,0x300 ,NULL ); +ASSERT_EQ (addr,0x300000 ); + +/* Map at 0x306000, length 0x3000, leaving a hole. */ + addr =__mmap_region (NULL ,0x306000 ,0x3000 , vma_flags,0x306 ,NULL ); +ASSERT_EQ (addr,0x306000 ); +ASSERT_EQ (mm.map_count,2 ); + +/* Map at 0x303000, length 0x3000, filling the hole. */ + addr =__mmap_region (NULL ,0x303000 ,0x3000 , middle_flags,0x303 ,NULL ); +ASSERT_EQ (addr,0x303000 ); +ASSERT_EQ (mm.map_count, merge ?1 :3 ); + +vma_iter_set (&vmi,0 ); +for_each_vma (vmi, vma) { +const unsigned long start =0x300000 + count *0x3000 ; +const unsigned long end = merge ?0x309000 : start +0x3000 ; +/* Only the middle VMA in the non-merge case has VMA_EXEC. */ +const bool is_middle_vma = count ==1 ; +const bool expect_exec_vma = is_middle_vma && !merge; + +ASSERT_EQ (vma->vm_start, start); +ASSERT_EQ (vma->vm_end, end); +ASSERT_EQ (vma_start_pgoff (vma), start >>PAGE_SHIFT ); +ASSERT_EQ (vma_start_anon_pgoff (vma), start >>PAGE_SHIFT ); + +ASSERT_TRUE (vma_test_all (vma,VMA_READ_BIT ,VMA_WRITE_BIT , +VMA_MAYREAD_BIT ,VMA_MAYWRITE_BIT , +VMA_MAYEXEC_BIT )); +ASSERT_EQ (vma_test (vma,VMA_EXEC_BIT ), expect_exec_vma); + + count++; + } + +ASSERT_EQ (count, mm.map_count); + +ASSERT_EQ (cleanup_mm (&mm, &vmi), count); +return true; +} + +static bool test_mmap_region_fill_hole_merge (void ) +{ +return mmap_region_fill_hole (true); +} + +static bool test_mmap_region_fill_hole_flags_mismatch (void ) +{ +return mmap_region_fill_hole (false); +} +static bool test_pure_anon_dev_zero (void ) { const vma_flags_t vma_flags = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,
@@ -84,5 +150,7 @@ static bool test_pure_anon_dev_zero(void)static void run_mmap_tests (int *num_tests,int *num_fail) {TEST (mmap_region_basic); +TEST (mmap_region_fill_hole_merge); +TEST (mmap_region_fill_hole_flags_mismatch);TEST (pure_anon_dev_zero); }
Signature · 1 line
2.55.0
Quoted 11 lines · Tianyi Chen
The mmap tests extend existing mappings one neighbor at a time, while merge tests construct merge state directly. Neither exercises filling a hole between compatible mappings through the mmap setup and completion path.
Fill a gap through
__mmap_region()and require both neighbors to merge into one VMA. Repeat with only the new mapping's execute permission set and require three separate VMAs. Check boundaries, permissions, page offsets,map_countand cleanup.Signed-off-by Tianyi Chen <[email protected]>
For future - please always send v2 patches not in-reply-to anything :P otherwise mails get buried.
Otherwise all LGTM, thanks for addressing feedback! So:
Quoted 10 lines
Assisted-by Codex:GPT-6---
Changes in v2:
- Rebase onto mm-unstable, preserving the new/dev/zerotest.
- Renamehole_flagstomiddle_flagsand describe each mapping's address and length in its comment.
- Remove the redundant hole, per-page and outer-boundary iterator checks.
- Use const bounds and named booleans for the middle VMA's expected execute permission, and separate assertion groups for readability.
Quoted 5 lines
Validation: all 29 VMA tests passed with ASan and UBSan enabled, built with clang 20 using the existing VMA
Makefile.v1: https://lore.kernel.org/r/[email protected]
Review: https://lore.kernel.org/r/ap5pmX-RddQ-c90O@gremlin
Thanks! :) very nice.
Quoted 90 lines
tools/testing/vma/tests/mmap.c | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)tools/testing/vma/tests/mmap.c +68
-1@@ -45,6 +45,72 @@ static bool test_mmap_region_basic(void)return true; } +static bool mmap_region_fill_hole (bool merge) +{ +const vma_flags_t vma_flags =mk_vma_flags (VMA_READ_BIT ,VMA_WRITE_BIT , +VMA_MAYREAD_BIT ,VMA_MAYWRITE_BIT ,VMA_MAYEXEC_BIT ); +vma_flags_t middle_flags = vma_flags; +struct mm_struct mm = {}; +struct vm_area_struct *vma; +unsigned long addr; +int count =0 ; +VMA_ITERATOR (vmi, &mm,0 ); + + current->mm = &mm; +if (!merge) +vma_flags_set (&middle_flags,VMA_EXEC_BIT ); + +/* Map at 0x300000, length 0x3000. */ + addr =__mmap_region (NULL ,0x300000 ,0x3000 , vma_flags,0x300 ,NULL ); +ASSERT_EQ (addr,0x300000 ); + +/* Map at 0x306000, length 0x3000, leaving a hole. */ + addr =__mmap_region (NULL ,0x306000 ,0x3000 , vma_flags,0x306 ,NULL ); +ASSERT_EQ (addr,0x306000 ); +ASSERT_EQ (mm.map_count,2 ); + +/* Map at 0x303000, length 0x3000, filling the hole. */ + addr =__mmap_region (NULL ,0x303000 ,0x3000 , middle_flags,0x303 ,NULL ); +ASSERT_EQ (addr,0x303000 ); +ASSERT_EQ (mm.map_count, merge ?1 :3 ); + +vma_iter_set (&vmi,0 ); +for_each_vma (vmi, vma) { +const unsigned long start =0x300000 + count *0x3000 ; +const unsigned long end = merge ?0x309000 : start +0x3000 ; +/* Only the middle VMA in the non-merge case has VMA_EXEC. */ +const bool is_middle_vma = count ==1 ; +const bool expect_exec_vma = is_middle_vma && !merge; + +ASSERT_EQ (vma->vm_start, start); +ASSERT_EQ (vma->vm_end, end); +ASSERT_EQ (vma_start_pgoff (vma), start >>PAGE_SHIFT ); +ASSERT_EQ (vma_start_anon_pgoff (vma), start >>PAGE_SHIFT ); + +ASSERT_TRUE (vma_test_all (vma,VMA_READ_BIT ,VMA_WRITE_BIT , +VMA_MAYREAD_BIT ,VMA_MAYWRITE_BIT , +VMA_MAYEXEC_BIT )); +ASSERT_EQ (vma_test (vma,VMA_EXEC_BIT ), expect_exec_vma); + + count++; + } + +ASSERT_EQ (count, mm.map_count); + +ASSERT_EQ (cleanup_mm (&mm, &vmi), count); +return true; +} + +static bool test_mmap_region_fill_hole_merge (void ) +{ +return mmap_region_fill_hole (true); +} + +static bool test_mmap_region_fill_hole_flags_mismatch (void ) +{ +return mmap_region_fill_hole (false); +} +static bool test_pure_anon_dev_zero (void ) { const vma_flags_t vma_flags = mk_vma_flags(VMA_READ_BIT, VMA_WRITE_BIT,@@ -84,5 +150,7 @@ static bool test_pure_anon_dev_zero(void)static void run_mmap_tests (int *num_tests,int *num_fail) {TEST (mmap_region_basic); +TEST (mmap_region_fill_hole_merge); +TEST (mmap_region_fill_hole_flags_mismatch);TEST (pure_anon_dev_zero); } --
Quoted 2 lines
2.55.0
--
Cheers, Lorenzo
Hi Lorenzo,
Thanks for the review and the Reviewed-by! I have recorded your tag.
Sorry about threading v2 under v1. I will send future revisions as new threads, with links to earlier versions in the changelog.
Thanks,
Tianyi