Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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/20241213162409.41498-1-lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Jann Horn <jannh@google.com>
Cc: Liam R. Howlett <Liam.Howlett@Oracle.com>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Lorenzo Stoakes and committed by
Andrew Morton
7e8c8fd3 ec838c7d

+54 -1
+53
tools/testing/vma/vma.c
··· 1574 1574 return true; 1575 1575 } 1576 1576 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 + 1577 1628 int main(void) 1578 1629 { 1579 1630 int num_tests = 0, num_fail = 0; ··· 1657 1606 TEST(merge_extend); 1658 1607 TEST(copy_vma); 1659 1608 TEST(expand_only_mode); 1609 + 1610 + TEST(mmap_region_basic); 1660 1611 1661 1612 #undef TEST 1662 1613
+1 -1
tools/testing/vma/vma_internal.h
··· 996 996 997 997 static inline int security_vm_enough_memory_mm(struct mm_struct *, long) 998 998 { 999 - return true; 999 + return 0; 1000 1000 } 1001 1001 1002 1002 static inline bool may_expand_vm(struct mm_struct *, vm_flags_t, unsigned long)