Skip to content

Commit 7e8c8fd

Browse files
lorenzo-stoakesakpm00
authored andcommitted
tools: testing: add simple __mmap_region() userland test
Introduce demonstrative, basic, __mmap_region() test upon which we can base further work upon moving forwards. This simply asserts that mappings can be made and merges occur as expected. As part of this change, fix the security_vm_enough_memory_mm() stub which was previously incorrectly implemented. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Lorenzo Stoakes <[email protected]> Cc: Jann Horn <[email protected]> Cc: Liam R. Howlett <[email protected]> Cc: Vlastimil Babka <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent ec838c7 commit 7e8c8fd

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

tools/testing/vma/vma.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,57 @@ static bool test_expand_only_mode(void)
15741574
return true;
15751575
}
15761576

1577+
static bool test_mmap_region_basic(void)
1578+
{
1579+
struct mm_struct mm = {};
1580+
unsigned long addr;
1581+
struct vm_area_struct *vma;
1582+
VMA_ITERATOR(vmi, &mm, 0);
1583+
1584+
current->mm = &mm;
1585+
1586+
/* Map at 0x300000, length 0x3000. */
1587+
addr = __mmap_region(NULL, 0x300000, 0x3000,
1588+
VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
1589+
0x300, NULL);
1590+
ASSERT_EQ(addr, 0x300000);
1591+
1592+
/* Map at 0x250000, length 0x3000. */
1593+
addr = __mmap_region(NULL, 0x250000, 0x3000,
1594+
VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
1595+
0x250, NULL);
1596+
ASSERT_EQ(addr, 0x250000);
1597+
1598+
/* Map at 0x303000, merging to 0x300000 of length 0x6000. */
1599+
addr = __mmap_region(NULL, 0x303000, 0x3000,
1600+
VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
1601+
0x303, NULL);
1602+
ASSERT_EQ(addr, 0x303000);
1603+
1604+
/* Map at 0x24d000, merging to 0x250000 of length 0x6000. */
1605+
addr = __mmap_region(NULL, 0x24d000, 0x3000,
1606+
VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE,
1607+
0x24d, NULL);
1608+
ASSERT_EQ(addr, 0x24d000);
1609+
1610+
ASSERT_EQ(mm.map_count, 2);
1611+
1612+
for_each_vma(vmi, vma) {
1613+
if (vma->vm_start == 0x300000) {
1614+
ASSERT_EQ(vma->vm_end, 0x306000);
1615+
ASSERT_EQ(vma->vm_pgoff, 0x300);
1616+
} else if (vma->vm_start == 0x24d000) {
1617+
ASSERT_EQ(vma->vm_end, 0x253000);
1618+
ASSERT_EQ(vma->vm_pgoff, 0x24d);
1619+
} else {
1620+
ASSERT_FALSE(true);
1621+
}
1622+
}
1623+
1624+
cleanup_mm(&mm, &vmi);
1625+
return true;
1626+
}
1627+
15771628
int main(void)
15781629
{
15791630
int num_tests = 0, num_fail = 0;
@@ -1607,6 +1658,8 @@ int main(void)
16071658
TEST(copy_vma);
16081659
TEST(expand_only_mode);
16091660

1661+
TEST(mmap_region_basic);
1662+
16101663
#undef TEST
16111664

16121665
printf("%d tests run, %d passed, %d failed.\n",

tools/testing/vma/vma_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ static inline bool is_file_hugepages(struct file *)
996996

997997
static inline int security_vm_enough_memory_mm(struct mm_struct *, long)
998998
{
999-
return true;
999+
return 0;
10001000
}
10011001

10021002
static inline bool may_expand_vm(struct mm_struct *, vm_flags_t, unsigned long)

0 commit comments

Comments
 (0)