mm: Refactor and improve VMA count limit code
Subsystems (9)
v4 cover mm: Refactor and improve VMA count limit code
Hi all,
This series refactors the VMA count limit code to improve clarity, test coverage, and observability.
The VMA count limit, controlled by sysctl_max_map_count, is a safeguard that prevents a single process from consuming excessive kernel memory by creating too many memory mappings.
A major change since v3 is the first patch in the series which instead of attempting to fix overshooting the limit now documents that this is the intended behavior. As Hugh pointed out, the lenient check (>) in do_mmap() and do_brk_flags() is intentional to allow for potential VMA merges or expansions when the process is at the sysctl_max_map_count limit. The consensus is that this historical behavior is correct but non-obvious.
This series now focuses on making that behavior clear and the surrounding code more robust. Based on feedback from Lorenzo and David, this series retains the helper function and the rename of map_count.
The refined v4 series is now structured as follows:
1. Documents the lenient VMA count checks with comments to clarify their purpose.
2. Adds a comprehensive selftest to codify the expected behavior at the limit, including the lenient mmap case.
3. Introduces max_vma_count() to abstract the max map count sysctl, making the sysctl static and converting all callers to use the new helper.
4. Renames mm_struct->map_count to the more explicit vma_count for better code clarity.
5. Adds a tracepoint for observability when a process fails to allocate a VMA due to the count limit.
Tested on x86_64 and arm64:
1. Build test:
allyesconfig for rename
2. Selftests:
cd tools/testing/selftests/mm && \
make && \
./run_vmtests.sh -t max_vma_count3. vma tests:
cd tools/testing/vma && \
make && \
./vmaLink to v3:
https://lore.kernel.org/r/[email protected]/
Thanks to everyone for the valuable discussion on previous revisions.
-- Kalesh
Kalesh Singh (5):
mm: Document lenient map_count checks
mm/selftests: add max_vma_count tests
mm: Introduce max_vma_count() to abstract the max map count sysctl
mm: rename mm_struct::map_count to vma_count mm/tracing: introduce trace_mm_insufficient_vma_slots event
MAINTAINERS | 2 + fs/binfmt_elf.c | 2 +- fs/coredump.c | 2 +- include/linux/mm.h | 2 - include/linux/mm_types.h | 2 +- include/trace/events/vma.h | 32 + kernel/fork.c | 2 +- mm/debug.c | 2 +- mm/internal.h | 3 + mm/mmap.c | 25 +- mm/mremap.c | 13 +- mm/nommu.c | 8 +- mm/util.c | 1 - mm/vma.c | 42 +- mm/vma_internal.h | 2 + tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + .../selftests/mm/max_vma_count_tests.c | 716 ++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 5 + tools/testing/vma/vma.c | 32 +- tools/testing/vma/vma_internal.h | 13 +- 21 files changed, 856 insertions(+), 52 deletions(-) create mode 100644 include/trace/events/vma.h create mode 100644 tools/testing/selftests/mm/max_vma_count_tests.c
base-commit: b227c04932039bccc21a0a89cd6df50fa57e4716
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v4 1/5 mm: Document lenient map_count checks
Add comments to the map_count limit checks in do_mmap() and do_brk_flags() to clarify their intended behavior.
The use of a strict inequality ('>') in these checks is intentional but non-obvious. It allows these functions to succeed when the VMA count is exactly at the sysctl_max_map_count limit. This historical behavior accounts for cases where the operation might not create a new VMA, but instead merge with or expand an existing one, in which case the VMA count does not increase.
These comments clarify the long-standing behavior and will help prevent future misinterpretation as an off-by-one error.
---
Changes in v4:
- Keep the existing lenient behavior, per Hugh
- Document this is intended, per Lorenzo
Changes in v3:
- Collect Reviewed-by and Acked-by tags.
Changes in v2:
- Fix mmap check, per Pedro
mm/mmap.c | 9 +++++++++ mm/vma.c | 6 ++++++ 2 files changed, 15 insertions(+)
mm/mmap.c +9 -0
@@ -374,6 +374,15 @@ unsigned long do_mmap(struct file *file, unsigned long addr,return -EOVERFLOW ;/* Too many mappings? */ +/* +* The check is intentionally lenient (>) to allow an mmap() at the limit +* to succeed. This is for historical reasons, as the new mapping might +* merge with an adjacent VMA and not increase the total VMA count. +* +* If a merge does not occur, the process is allowed to exceed the +* sysctl_max_map_count limit by one. This behavior is preserved to +* avoid breaking existing applications. +*/ if (mm->map_count > sysctl_max_map_count)return -ENOMEM ;
mm/vma.c +6 -0
@@ -2813,6 +2813,12 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,if (!may_expand_vm (mm, vm_flags, len >>PAGE_SHIFT ))return -ENOMEM ; +/* +* The check is intentionally lenient (>) to allow brk() to succeed at +* the limit. This is for historical reasons, as expanding the heap +* typically extends the existing brk VMA rather than creating a new one. +* See also the comment in do_mmap(). +*/ if (mm->map_count > sysctl_max_map_count)return -ENOMEM ;
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
Re: [PATCH v4 1/5] mm: Document lenient map_count checks
Quoted 10 lines · Kalesh Singh
Add comments to the
map_countlimit checks indo_mmap()anddo_brk_flags()to clarify their intended behavior.The use of a strict inequality ('>') in these checks is intentional but non-obvious. It allows these functions to succeed when the VMA count is exactly at the
sysctl_max_map_countlimit. This historical behavior accounts for cases where the operation might not create a new VMA, but instead merge with or expand an existing one, in which case the VMA count does not increase.
Quoted 5 lines
These comments clarify the long-standing behavior and will help prevent future misinterpretation as an off-by-one error.
Signed-off-by Kalesh Singh <[email protected]>---
Signature · 3 lines
Cheers David
PATCH v4 2/5 mm/selftests: add max_vma_count tests
In reply to Kalesh SinghAdd a new selftest to verify that the max VMA count limit is correctly enforced.
This test suite checks that various VMA operations (mmap, mprotect, munmap, mremap) succeed or fail as expected when the number of VMAs is close to the sysctl_max_map_count limit.
The test works by first creating a large number of VMAs to bring the process close to the limit, and then performing various operations that may or may not create new VMAs. The test then verifies that the operations that would exceed the limit fail, and that the operations that do not exceed the limit succeed.
NOTE: munmap is special as it's allowed to temporarily exceed the limit by one for splits as this will decrease back to the limit once the unmap succeeds.
---
Changes in v4:
- Update the mmap test cases to correctly validate the historical lenient behavior discussed in the v3 review. The test now confirms that an mmap() call succeeds at the limit and fails when one above the limit.
- Add comments to the test code to document this expected behavior.
Changes in v3:
- Rewrite test using kselftest harness, per Lorenzo
- Update test diagram to be vertical so as to not exceed 80 chars, per Lorenzo
- Use vm_util.h helpers, per Lorenzo
- Update .gitignore, per Lorenzo
- Add max_vma_count_tests to MEMORY MAPPING section in MAINTAINERS, per Lorenzo
- Remove /proc/*/maps debugging prints and globals, per Lorenzo
- rename guard regions to holes to avoid confusion with VMA guard regions, per David
Changes in v2:
- Add tests, per Liam (note that the do_brk_flags() path is not easily tested from userspace, so it's not included here). Exceeding the limit there should be uncommon.
MAINTAINERS | 1 + tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + .../selftests/mm/max_vma_count_tests.c | 716 ++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 5 + 5 files changed, 724 insertions(+) create mode 100644 tools/testing/selftests/mm/max_vma_count_tests.c
MAINTAINERS +1 -0
@@ -16581,6 +16581,7 @@ F: mm/vma.h F: mm/vma_exec.c F: mm/vma_init.c F: mm/vma_internal.h +F: tools/testing/selftests/mm/max_vma_count_tests.c F: tools/testing/selftests/mm/merge.c F: tools/testing/vma/
tools/testing/selftests/mm/.gitignore +1 -0
@@ -10,6 +10,7 @@ hugetlb-soft-offline khugepaged map_hugetlb map_populate +max_vma_count_tests thuge-gen compaction_test migration
tools/testing/selftests/mm/Makefile +1 -0
@@ -93,6 +93,7 @@ TEST_GEN_FILES += transhuge-stress TEST_GEN_FILES += uffd-stress TEST_GEN_FILES += uffd-unit-tests TEST_GEN_FILES += uffd-wp-mremap +TEST_GEN_FILES += max_vma_count_tests TEST_GEN_FILES += split_huge_page_test TEST_GEN_FILES += ksm_tests TEST_GEN_FILES += ksm_functional_tests
tools/testing/selftests/mm/max_vma_count_tests.c new file +716 -0
716 lines not on this page · raw message
tools/testing/selftests/mm/run_vmtests.sh +5 -0
@@ -49,6 +49,8 @@ separated by spaces: test madvise(2) MADV_GUARD_INSTALL and MADV_GUARD_REMOVE options - madv_populate test memadvise(2) MADV_POPULATE_{READ,WRITE} options +- max_vma_count + tests for max vma_count - memfd_secret test memfd_secret(2) - process_mrelease
@@ -426,6 +428,9 @@ fi # VADDR64 # vmalloc stability smoke test CATEGORY="vmalloc" run_test bash ./test_vmalloc.sh smoke +# test operations against max vma count limit +CATEGORY="max_vma_count" run_test ./max_vma_count_tests + CATEGORY="mremap" run_test ./mremap_dontunmap CATEGORY="hmm" run_test bash ./test_hmm.sh smoke
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
David Hildenbrand (Red Hat)
maintainerRe: [PATCH v4 2/5] mm/selftests: add max_vma_count tests
Quoted 25 lines · Kalesh Singh
Add a new selftest to verify that the max VMA count limit is correctly enforced.
This test suite checks that various VMA operations (mmap, mprotect, munmap, mremap) succeed or fail as expected when the number of VMAs is close to the
sysctl_max_map_countlimit.The test works by first creating a large number of VMAs to bring the process close to the limit, and then performing various operations that may or may not create new VMAs. The test then verifies that the operations that would exceed the limit fail, and that the operations that do not exceed the limit succeed.
NOTE: munmap is special as it's allowed to temporarily exceed the limit by one for splits as this will decrease back to the limit once the unmap succeeds.
Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Mike Rapoport <[email protected]>Cc Minchan Kim <[email protected]>Cc Pedro Falcato <[email protected]>Signed-off-by Kalesh Singh <[email protected]>
Quoted 1 line
---
[...]
No capacity to review the tests in detail :(
Quoted 15 lines
(quoted diff) +1
-0+tools/testing/selftests/mm/run_vmtests.sh +2
-0@@ -49,6 +49,8 @@ separated by spaces: test madvise(2) MADV_GUARD_INSTALL and MADV_GUARD_REMOVE options - madv_populate test memadvise(2) MADV_POPULATE_{READ,WRITE} options +- max_vma_count + tests for max vma_count - memfd_secret test memfd_secret(2) - process_mrelease
tools/testing/selftests/mm/run_vmtests.sh fi # VADDR64 · line 432
Quoted 5 lines
tools/testing/selftests/mm/run_vmtests.sh +2
-0@@ -426,6 +428,9 @@ fi # VADDR64 # vmalloc stability smoke test CATEGORY="vmalloc" run_test bash ./test_vmalloc.sh smoke +# test operations against max vma count limit +CATEGORY="max_vma_count" run_test ./max_vma_count_tests
I'd just call it CATEGORY="vma" or "vma_handling".
Which makes me wodnering whether "vma_merge" falls into the same category.
Smalls like mremap test is similar.
Point is that "CATEGORY" stops being really useful if we end up having a separate category for each test, right? :)
Signature · 3 lines
Cheers David
Re: [PATCH v4 2/5] mm/selftests: add max_vma_count tests
Quoted 29 lines · David Hildenbrand (Red Hat)
Quoted 25 lines · Kalesh Singh
Add a new selftest to verify that the max VMA count limit is correctly enforced.
This test suite checks that various VMA operations (mmap, mprotect, munmap, mremap) succeed or fail as expected when the number of VMAs is close to the
sysctl_max_map_countlimit.The test works by first creating a large number of VMAs to bring the process close to the limit, and then performing various operations that may or may not create new VMAs. The test then verifies that the operations that would exceed the limit fail, and that the operations that do not exceed the limit succeed.
NOTE: munmap is special as it's allowed to temporarily exceed the limit by one for splits as this will decrease back to the limit once the unmap succeeds.
Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Mike Rapoport <[email protected]>Cc Minchan Kim <[email protected]>Cc Pedro Falcato <[email protected]>Signed-off-by Kalesh Singh <[email protected]>Quoted 1 line
---
Quoted 3 lines
[...]
No capacity to review the tests in detail :(
Appreciate you taking a look :)
Quoted 24 lines
Quoted 16 lines
(quoted diff) +1
-0+tools/testing/selftests/mm/run_vmtests.sh +2
-0@@ -49,6 +49,8 @@ separated by spaces: test madvise(2) MADV_GUARD_INSTALL and MADV_GUARD_REMOVE options - madv_populate test memadvise(2) MADV_POPULATE_{READ,WRITE} options +- max_vma_count + tests for max vma_count - memfd_secret test memfd_secret(2) - process_mreleasetools/testing/selftests/mm/run_vmtests.sh fi # VADDR64 · line 432
Quoted 5 lines
tools/testing/selftests/mm/run_vmtests.sh +2
-0@@ -426,6 +428,9 @@ fi # VADDR64 # vmalloc stability smoke test CATEGORY="vmalloc" run_test bash ./test_vmalloc.sh smoke +# test operations against max vma count limit +CATEGORY="max_vma_count" run_test ./max_vma_count_testsI'd just call it CATEGORY="vma" or "
vma_handling".
Quoted 6 lines
Which makes me wodnering whether "
vma_merge" falls into the same category.Smalls like mremap test is similar.
Point is that "CATEGORY" stops being really useful if we end up having a separate category for each test, right? :)
I agree making both use a "vma" category seem more helpful. I'll wait for others' feedback before resending.
Thanks,
Kalesh
Quoted 5 lines
--
CheersDavid
PATCH v4 3/5 mm: Introduce max_vma_count() to abstract the max map count sysctl
In reply to Kalesh SinghIntroduce a new helper function, max_vma_count(), to act as the canonical accessor for the maximum VMA count limit.
The global variable sysctl_max_map_count is used in multiple files to check the VMA limit. This direct usage exposes an implementation detail and makes the code harder to read and maintain.
This patch abstracts the global variable behind the more aptly named max_vma_count() function. As a result, the sysctl_max_map_count variable can now be made static to mm/mmap.c, improving encapsulation.
All call sites are converted to use the new helper, making the limit checks more readable.
---
Changes in v4:
- Introduce max_vma_count() to abstract the max map count sysctl, replacing the previously proposed vma_count_remaining() helper -- since this remaining count can now be negative as some cases are allowed to exceed the limit.
- Convert all callers to use the new helper.
Changes in v3:
- Move vma_count_remaining() out of #if CONFIG_SYSCTL to fix build failure
- Use READ_ONCE() for sysclt_max_map_count, per David, Lorenzo
- Remove use of ternary op in vma_count_remaining, per Lorenzo
- Rebase on mm-new to fix conflicts in vma_internal.h and mm/internal.h
include/linux/mm.h | 2 -- mm/internal.h | 3 +++ mm/mmap.c | 9 ++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 10 +++++----- tools/testing/vma/vma_internal.h | 6 ++++++ 8 files changed, 27 insertions(+), 13 deletions(-)
include/linux/mm.h +0 -2
@@ -205,8 +205,6 @@ static inline void __mm_zero_struct_page(struct page *page)#define MAPCOUNT_ELF_CORE_MARGIN (5)#define DEFAULT_MAX_MAP_COUNT (USHRT_MAX - MAPCOUNT_ELF_CORE_MARGIN) -extern int sysctl_max_map_count; -extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;
mm/internal.h +3 -0
@@ -1702,4 +1702,7 @@ static inline int io_remap_pfn_range_complete(struct vm_area_struct *vma, return remap_pfn_range_complete(vma, addr, pfn, size, prot); } +/* mmap.c */ +int max_vma_count (void ); +#endif /* __MM_INTERNAL_H */
mm/mmap.c +8 -1
@@ -383,7 +383,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,* sysctl_max_map_count limit by one. This behavior is preserved to * avoid breaking existing applications. */ -if (mm->map_count > sysctl_max_map_count) +if (max_vma_count () - mm->map_count <0 )return -ENOMEM ;/*
@@ -1504,6 +1504,13 @@ struct vm_area_struct *_install_special_mapping( &special_mapping_vmops); } +static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; + +int max_vma_count (void ) +{ +return READ_ONCE (sysctl_max_map_count); +} +#ifdef CONFIG_SYSCTL #if defined(HAVE_ARCH_PICK_MMAP_LAYOUT ) || \ defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT )
mm/mremap.c +4 -3
@@ -1040,7 +1040,7 @@ static unsigned long prep_move_vma(struct vma_remap_struct *vrm)* We'd prefer to avoid failure later on in do_munmap: * which may split one vma into three before unmapping. */ -if (current->mm->map_count >= sysctl_max_map_count -3 ) +if (max_vma_count () - current->mm->map_count <4 )return -ENOMEM ;if (vma->vm_ops && vma->vm_ops->may_split) {
@@ -1811,9 +1811,10 @@ static unsigned long check_mremap_params(struct vma_remap_struct *vrm)* split in 3 before unmapping it. * That means 2 more maps (1 for each) to the ones we already hold. * Check whether current map count plus 2 still leads us to 4 maps below -* the threshold, otherwise return -ENOMEM here to be more safe. +* the threshold. In other words, is the current map count + 6 at or +* below the threshold? Otherwise return -ENOMEM here to be more safe. */ -if ((current->mm->map_count +2 ) >= sysctl_max_map_count -3 ) +if (max_vma_count () - current->mm->map_count <6 )return -ENOMEM ;return 0 ;
mm/nommu.c +1 -1
@@ -1317,7 +1317,7 @@ static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,return -ENOMEM ; mm = vma->vm_mm; -if (mm->map_count >= sysctl_max_map_count) +if (max_vma_count () - mm->map_count <1 )return -ENOMEM ; region =kmem_cache_alloc (vm_region_jar,GFP_KERNEL );
mm/util.c +0 -1
@@ -752,7 +752,6 @@ EXPORT_SYMBOL(folio_mc_copy);int sysctl_overcommit_memory__read_mostly =OVERCOMMIT_GUESS ;static int sysctl_overcommit_ratio__read_mostly =50 ;static unsigned long sysctl_overcommit_kbytes__read_mostly ; -int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ;unsigned long sysctl_user_reserve_kbytes__read_mostly =1UL <<17 ;/* 128MB */ unsigned long sysctl_admin_reserve_kbytes__read_mostly =1UL <<13 ;/* 8MB */
mm/vma.c +5 -5
@@ -493,8 +493,8 @@ void unmap_region(struct ma_state *mas, struct vm_area_struct *vma, }/* -* __split_vma() bypasses sysctl_max_map_count checking. We use this where it -* has already been checked or doesn't make sense to fail. +* __split_vma() bypasses max_vma_count() checks. We use this where +* it has already been checked or doesn't make sense to fail. * VMA Iterator will point to the original VMA. */ static __must_check int
@@ -594,7 +594,7 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,static int split_vma (struct vma_iterator *vmi,struct vm_area_struct *vma,unsigned long addr,int new_below) { -if (vma->vm_mm->map_count >= sysctl_max_map_count) +if (max_vma_count () - vma->vm_mm->map_count <1 )return -ENOMEM ;return __split_vma (vmi, vma, addr, new_below);
@@ -1347,7 +1347,7 @@ static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,* its limit temporarily, to help free resources as expected. */ if (vms->end < vms->vma->vm_end && - vms->vma->vm_mm->map_count >= sysctl_max_map_count) { +max_vma_count () - vms->vma->vm_mm->map_count <1 ) { error = -ENOMEM ; goto map_count_exceeded; }
@@ -2819,7 +2819,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,* typically extends the existing brk VMA rather than creating a new one. * See also the comment in do_mmap(). */ -if (mm->map_count > sysctl_max_map_count) +if (max_vma_count () - mm->map_count <0 )return -ENOMEM ;if (security_vm_enough_memory_mm (mm, len >>PAGE_SHIFT ))
tools/testing/vma/vma_internal.h +6 -0
@@ -1491,4 +1491,10 @@ static inline int do_munmap(struct mm_struct *, unsigned long, size_t, return 0; } +/* Helper to get max vma count */ +static int max_vma_count (void ) +{ +return sysctl_max_map_count; +} +#endif /* __MM_VMA_INTERNAL_H */
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
kernel test robot
build botRe: [PATCH v4 3/5] mm: Introduce max_vma_count() to abstract the max map count sysctl
kernel test robot noticed the following build errors:
Hi Kalesh,
kernel test robot noticed the following build errors:
[auto build test ERROR on b227c04932039bccc21a0a89cd6df50fa57e4716]
url: https://github.com/intel-lab-lkp/linux/commits/Kalesh-Singh/mm-Document-lenient-map_count-checks/20251029-052713 base: b227c04932039bccc21a0a89cd6df50fa57e4716 patch link: https://lore.kernel.org/r/20251028212528.681081-4-kaleshsingh%40google.com patch subject: [PATCH v4 3/5] mm: Introduce max_vma_count() to abstract the max map count sysctl config: arm-allnoconfig (https://download.01.org/0day-ci/archive/20251029/[email protected]/config) compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project d1c086e82af239b245fe8d7832f2753436634990) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251029/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> ld.lld: error: undefined symbol: max_vma_count >>> referenced by nommu.c >>> mm/nommu.o:(split_vma) in archive vmlinux.a
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v4 3/5] mm: Introduce max_vma_count() to abstract the max map count sysctl
In reply to Kalesh Singhkernel test robot noticed the following build errors:
Hi Kalesh,
kernel test robot noticed the following build errors:
[auto build test ERROR on b227c04932039bccc21a0a89cd6df50fa57e4716]
url: https://github.com/intel-lab-lkp/linux/commits/Kalesh-Singh/mm-Document-lenient-map_count-checks/20251029-052713 base: b227c04932039bccc21a0a89cd6df50fa57e4716 patch link: https://lore.kernel.org/r/20251028212528.681081-4-kaleshsingh%40google.com patch subject: [PATCH v4 3/5] mm: Introduce max_vma_count() to abstract the max map count sysctl config: m68k-allnoconfig (https://download.01.org/0day-ci/archive/20251029/[email protected]/config) compiler: m68k-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251029/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
m68k-linux-ld: mm/nommu.o: in function `split_vma.constprop.0':
>> nommu.c:(.text+0xc54): undefined reference to `max_vma_count'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v4 3/5] mm: Introduce max_vma_count() to abstract the max map count sysctl
Quoted 15 lines · kernel test robot
Hi Kalesh,
kernel test robot noticed the following build errors:
[auto build test ERROR on
b227c04932039bccc21a0a89cd6df50fa57e4716]url: https://github.com/intel-lab-lkp/linux/commits/Kalesh-Singh/mm-Document-lenient-map_count-checks/20251029-052713 base: b227c04932039bccc21a0a89cd6df50fa57e4716 patch link: https://lore.kernel.org/r/20251028212528.681081-4-kaleshsingh%40google.com patch subject: [PATCH v4 3/5] mm: Introduce max_vma_count() to abstract the max map count sysctl config: m68k-allnoconfig (https://download.01.org/0day-ci/archive/20251029/[email protected]/config) compiler: m68k-linux-gcc (GCC) 15.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251029/[email protected]/reproduce)
Quoted 9 lines
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All errors (new ones prefixed by >>):
m68k-linux-ld:
mm/nommu.o: in function `split_vma.constprop.0':Quoted 1 line
Quoted 1 line
nommu.c:(.text+0xc54): undefined reference to `max_vma_count'
Hi Andrew,
Could you squash the following diff to patch 3/5 to fix this build error please. I needed to move sysctl_max_map_count out of mm/mmap.c for the nommu case. Let me know if you'd prefer I resend the series instead.
Thanks,
Kalesh
mm/mmap.c +0 -15
@@ -1507,13 +1507,6 @@ struct vm_area_struct *_install_special_mapping( &special_mapping_vmops); } -static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; - -int max_vma_count (void ) -{ -return READ_ONCE (sysctl_max_map_count); -} -#ifdef CONFIG_SYSCTL #if defined(HAVE_ARCH_PICK_MMAP_LAYOUT ) || \ defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT )
@@ -1521,14 +1514,6 @@ int sysctl_legacy_va_layout;#endif static const struct ctl_table mmap_table[] = { - { - .procname = "max_map_count", - .data = &sysctl_max_map_count, - .maxlen = sizeof(sysctl_max_map_count), - .mode = 0644, - .proc_handler = proc_dointvec_minmax, - .extra1 = SYSCTL_ZERO, - }, #if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) {
mm/util.c +0 -0
@@ -755,6 +755,13 @@ static unsigned long sysctl_overcommit_kbytes
__read_mostly;
unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */ unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
+static int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT; +
+int max_vma_count(void)
+{
+ return READ_ONCE(sysctl_max_map_count);
+}
+
#ifdef CONFIG_SYSCTL
static int overcommit_ratio_handler(const struct ctl_table *table, int write,
@@ -858,6 +865,14 @@ static const struct ctl_table util_sysctl_table[] = {
.mode = 0644,
.proc_handler = proc_doulongvec_minmax,
},
+ {
+ .procname = "max_map_count", + .data = &sysctl_max_map_count, + .maxlen = sizeof(sysctl_max_map_count), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO,
+ },
};
static int __init init_vm_util_sysctls(void)
Quoted 7 lines
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wikiTo unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
PATCH v4 4/5 mm: rename mm_struct::map_count to vma_count
In reply to Kalesh SinghPerform a mechanical rename of mm_struct->map_count to vma_count.
The name "map_count" is ambiguous. It can be confused with other counters like the page mapcount (page->_mapcount), which tracks PTE references.
The new name, vma_count, is more precise and self-documenting, as this field has always counted the number of vm_area_structs associated with an mm_struct. This change improves code clarity and readability.
While at it, update the BUG_ON() in exit_mmap() to a WARN_ON_ONCE() to avoid crashing the kernel on a simple accounting mismatch. No other functional change is intended.
---
Changes in v4:
- Update the new max_vma_count_tests.c to use the vma_count name.
Changes in v3:
- Change vma_count BUG_ON() in exit_mmap() to WARN_ON_ONCE, per David and Lorenzo
- Collect Reviewed-by tags
fs/binfmt_elf.c | 2 +- fs/coredump.c | 2 +- include/linux/mm_types.h | 2 +- kernel/fork.c | 2 +- mm/debug.c | 2 +- mm/mmap.c | 6 ++-- mm/mremap.c | 4 +-- mm/nommu.c | 8 ++--- mm/vma.c | 30 ++++++++--------- .../selftests/mm/max_vma_count_tests.c | 28 ++++++++-------- tools/testing/vma/vma.c | 32 +++++++++---------- tools/testing/vma/vma_internal.h | 2 +- 12 files changed, 60 insertions(+), 60 deletions(-)
fs/binfmt_elf.c +1 -1
@@ -1660,7 +1660,7 @@ static int fill_files_note(struct memelfnote *note, struct coredump_params *cprm data[0 ] = count; data[1 ] =PAGE_SIZE ;/* -* Count usually is less than mm->map_count, +* Count usually is less than mm->vma_count, * we need to move filenames down. */ n = cprm->vma_count - count;
fs/coredump.c +1 -1
@@ -1733,7 +1733,7 @@ static bool dump_vma_snapshot(struct coredump_params *cprm) cprm->vma_data_size =0 ; gate_vma =get_gate_vma (mm); - cprm->vma_count = mm->map_count + (gate_vma ?1 :0 ); + cprm->vma_count = mm->vma_count + (gate_vma ?1 :0 ); cprm->vma_meta =kvmalloc_array (cprm->vma_count,sizeof (*cprm->vma_meta),GFP_KERNEL );if (!cprm->vma_meta) {
include/linux/mm_types.h +1 -1
@@ -1085,7 +1085,7 @@ struct mm_struct {#ifdef CONFIG_MMU atomic_long_t pgtables_bytes;/* size of all page tables */ #endif -int map_count;/* number of VMAs */ +int vma_count;/* number of VMAs */ spinlock_t page_table_lock;/* Protects page tables and some * counters
kernel/fork.c +1 -1
@@ -1038,7 +1038,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,mmap_init_lock (mm);INIT_LIST_HEAD (&mm->mmlist);mm_pgtables_bytes_init (mm); - mm->map_count =0 ; + mm->vma_count =0 ; mm->locked_vm =0 ;atomic64_set (&mm->pinned_vm,0 );memset (&mm->rss_stat,0 ,sizeof (mm->rss_stat));
mm/debug.c +1 -1
@@ -204,7 +204,7 @@ void dump_mm(const struct mm_struct *mm) mm->pgd,atomic_read (&mm->mm_users),atomic_read (&mm->mm_count),mm_pgtables_bytes (mm), - mm->map_count, + mm->vma_count, mm->hiwater_rss, mm->hiwater_vm, mm->total_vm, mm->locked_vm, (u64 )atomic64_read (&mm->pinned_vm), mm->data_vm, mm->exec_vm, mm->stack_vm,
mm/mmap.c +3 -3
@@ -383,7 +383,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,* sysctl_max_map_count limit by one. This behavior is preserved to * avoid breaking existing applications. */ -if (max_vma_count () - mm->map_count <0 ) +if (max_vma_count () - mm->vma_count <0 )return -ENOMEM ;/*
@@ -1314,7 +1314,7 @@ void exit_mmap(struct mm_struct *mm) vma =vma_next (&vmi); }while (vma &&likely (!xa_is_zero (vma))); -BUG_ON (count != mm->map_count); +WARN_ON_ONCE (count != mm->vma_count);trace_exit_mmap (mm); destroy:
@@ -1822,7 +1822,7 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)*/ vma_iter_bulk_store (&vmi, tmp); - mm->map_count++; + mm->vma_count++;if (tmp->vm_ops && tmp->vm_ops->open) tmp->vm_ops->open (tmp);
mm/mremap.c +2 -2
@@ -1040,7 +1040,7 @@ static unsigned long prep_move_vma(struct vma_remap_struct *vrm)* We'd prefer to avoid failure later on in do_munmap: * which may split one vma into three before unmapping. */ -if (max_vma_count () - current->mm->map_count <4 ) +if (max_vma_count () - current->mm->vma_count <4 )return -ENOMEM ;if (vma->vm_ops && vma->vm_ops->may_split) {
@@ -1814,7 +1814,7 @@ static unsigned long check_mremap_params(struct vma_remap_struct *vrm)* the threshold. In other words, is the current map count + 6 at or * below the threshold? Otherwise return -ENOMEM here to be more safe. */ -if (max_vma_count () - current->mm->map_count <6 ) +if (max_vma_count () - current->mm->vma_count <6 )return -ENOMEM ;return 0 ;
mm/nommu.c +4 -4
@@ -577,7 +577,7 @@ static void setup_vma_to_mm(struct vm_area_struct *vma, struct mm_struct *mm)static void cleanup_vma_from_mm (struct vm_area_struct *vma) { - vma->vm_mm->map_count--; + vma->vm_mm->vma_count--;/* remove the VMA from the mapping */ if (vma->vm_file) {struct address_space *mapping;
@@ -1199,7 +1199,7 @@ unsigned long do_mmap(struct file *file, goto error_just_free;setup_vma_to_mm (vma, current->mm); - current->mm->map_count++; + current->mm->vma_count++;/* add the VMA to the tree */ vma_iter_store_new (&vmi, vma);
@@ -1317,7 +1317,7 @@ static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,return -ENOMEM ; mm = vma->vm_mm; -if (max_vma_count () - mm->map_count <1 ) +if (max_vma_count () - mm->vma_count <1 )return -ENOMEM ; region =kmem_cache_alloc (vm_region_jar,GFP_KERNEL );
@@ -1367,7 +1367,7 @@ static int split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,setup_vma_to_mm (vma, mm);setup_vma_to_mm (new, mm);vma_iter_store_new (vmi, new); - mm->map_count++; + mm->vma_count++;return 0 ; err_vmi_preallocate:
mm/vma.c +15 -15
@@ -354,7 +354,7 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi,* (it may either follow vma or precede it). */ vma_iter_store_new (vmi, vp->insert); - mm->map_count++; + mm->vma_count++; }if (vp->anon_vma) {
@@ -385,7 +385,7 @@ static void vma_complete(struct vma_prepare *vp, struct vma_iterator *vmi, }if (vp->remove->anon_vma)anon_vma_merge (vp->vma, vp->remove); - mm->map_count--; + mm->vma_count--;mpol_put (vma_policy (vp->remove));if (!vp->remove2)WARN_ON_ONCE (vp->vma->vm_end < vp->remove->vm_end);
@@ -594,7 +594,7 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,static int split_vma (struct vma_iterator *vmi,struct vm_area_struct *vma,unsigned long addr,int new_below) { -if (max_vma_count () - vma->vm_mm->map_count <1 ) +if (max_vma_count () - vma->vm_mm->vma_count <1 )return -ENOMEM ;return __split_vma (vmi, vma, addr, new_below);
@@ -685,13 +685,13 @@ void validate_mm(struct mm_struct *mm) }#endif /* Check for a infinite loop */ -if (++i > mm->map_count +10 ) { +if (++i > mm->vma_count +10 ) { i =-1 ;break ; } } -if (i != mm->map_count) { -pr_emerg ("map_count %d vma iterator %d\n" , mm->map_count, i); +if (i != mm->vma_count) { +pr_emerg ("vma_count %d vma iterator %d\n" , mm->vma_count, i); bug =1 ; }VM_BUG_ON_MM (bug, mm);
@@ -1268,7 +1268,7 @@ static void vms_complete_munmap_vmas(struct vma_munmap_struct *vms,struct mm_struct *mm; mm = current->mm; - mm->map_count -= vms->vma_count; + mm->vma_count -= vms->vma_count; mm->locked_vm -= vms->locked_vm;if (vms->unlock)mmap_write_downgrade (mm);
@@ -1342,14 +1342,14 @@ static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,if (vms->start > vms->vma->vm_start) {/* -* Make sure that map_count on return from munmap() will +* Make sure that vma_count on return from munmap() will * not exceed its limit; but let map_count go just above * its limit temporarily, to help free resources as expected. */ if (vms->end < vms->vma->vm_end && -max_vma_count () - vms->vma->vm_mm->map_count <1 ) { +max_vma_count () - vms->vma->vm_mm->vma_count <1 ) { error = -ENOMEM ; - goto map_count_exceeded; + goto vma_count_exceeded; }/* Don't bother splitting the VMA if we can't unmap it anyway */
@@ -1463,7 +1463,7 @@ static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms, modify_vma_failed:reattach_vmas (mas_detach); start_split_failed: -map_count_exceeded: +vma_count_exceeded:return error; }
@@ -1781,7 +1781,7 @@ static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma)vma_start_write (vma);vma_iter_store_new (&vmi, vma);vma_link_file (vma,/* hold_rmap_lock= */ false); - mm->map_count++; + mm->vma_count++;validate_mm (mm);return 0 ; }
@@ -2498,7 +2498,7 @@ static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **vmap)/* Lock the VMA since it is modified after insertion into VMA tree */ vma_start_write (vma);vma_iter_store_new (vmi, vma); - map->mm->map_count++; + map->mm->vma_count++;vma_link_file (vma, map->hold_file_rmap_lock);/*
@@ -2819,7 +2819,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,* typically extends the existing brk VMA rather than creating a new one. * See also the comment in do_mmap(). */ -if (max_vma_count () - mm->map_count <0 ) +if (max_vma_count () - mm->vma_count <0 )return -ENOMEM ;if (security_vm_enough_memory_mm (mm, len >>PAGE_SHIFT ))
@@ -2857,7 +2857,7 @@ int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma,if (vma_iter_store_gfp (vmi, vma,GFP_KERNEL )) goto mas_store_fail; - mm->map_count++; + mm->vma_count++;validate_mm (mm); out:perf_event_mmap (vma);
tools/testing/selftests/mm/max_vma_count_tests.c +14 -14
@@ -18,7 +18,7 @@#include "../kselftest_harness.h" #include "vm_util.h" -#define DEFAULT_MAX_MAP_COUNT 65530 +#define DEFAULT_MAX_VMA_COUNT 65530#define TEST_AREA_NR_PAGES 3#define TEST_AREA_PROT (PROT_NONE)#define EXTRA_MAP_PROT (PROT_NONE)
@@ -42,9 +42,9 @@ static int get_max_vma_count(void);static bool set_max_vma_count (int val);static int get_current_vma_count (void );static bool is_test_area_mapped (char *test_area,int test_area_size); -static bool lower_max_map_count_if_needed (max_vma_count_data_t *self, +static bool lower_max_vma_count_if_needed (max_vma_count_data_t *self,struct __test_metadata *_metadata); -static void restore_max_map_count_if_needed (max_vma_count_data_t *self, +static void restore_max_vma_count_if_needed (max_vma_count_data_t *self,struct __test_metadata *_metadata);static bool free_vma_slots (max_vma_count_data_t *self,int slots_to_free); static void create_reservation(max_vma_count_data_t *self,
@@ -98,9 +98,9 @@ FIXTURE_SETUP(max_vma_count) self->test_area_size =TEST_AREA_NR_PAGES *psize (); -if (!lower_max_map_count_if_needed (self, _metadata)) { +if (!lower_max_vma_count_if_needed (self, _metadata)) {SKIP (return, -"max_map_count too high and cannot be lowered. Please rerun as root." ); +"max_vma_count too high and cannot be lowered. Please rerun as root." ); } initial_vma_count =get_current_vma_count ();
@@ -127,7 +127,7 @@ FIXTURE_TEARDOWN(max_vma_count)* mapping cleanup to process teardown for simplicity. */ -restore_max_map_count_if_needed (self, _metadata); +restore_max_vma_count_if_needed (self, _metadata); }static bool mmap_anon (max_vma_count_data_t *self)
@@ -542,7 +542,7 @@ TEST_HARNESS_MAIN/* --- Utilities --- */ -static bool lower_max_map_count_if_needed (max_vma_count_data_t *self, +static bool lower_max_vma_count_if_needed (max_vma_count_data_t *self,struct __test_metadata *_metadata) { self->max_vma_count =get_max_vma_count ();
@@ -550,19 +550,19 @@ static bool lower_max_map_count_if_needed(max_vma_count_data_t *self,ASSERT_GT (self->max_vma_count,0 ); self->original_max_vma_count =0 ; -if (self->max_vma_count >DEFAULT_MAX_MAP_COUNT ) { +if (self->max_vma_count >DEFAULT_MAX_VMA_COUNT ) { self->original_max_vma_count = self->max_vma_count;TH_LOG ("Max VMA count: %d; lowering to default %d for test..." , - self->max_vma_count,DEFAULT_MAX_MAP_COUNT ); + self->max_vma_count,DEFAULT_MAX_VMA_COUNT ); -if (!set_max_vma_count (DEFAULT_MAX_MAP_COUNT )) +if (!set_max_vma_count (DEFAULT_MAX_VMA_COUNT ))return false; - self->max_vma_count =DEFAULT_MAX_MAP_COUNT ; + self->max_vma_count =DEFAULT_MAX_VMA_COUNT ; }return true; } -static void restore_max_map_count_if_needed (max_vma_count_data_t *self, +static void restore_max_vma_count_if_needed (max_vma_count_data_t *self,struct __test_metadata *_metadata) {if (!self->original_max_vma_count)
@@ -572,7 +572,7 @@ static void restore_max_map_count_if_needed(max_vma_count_data_t *self,return ;if (!set_max_vma_count (self->original_max_vma_count)) -TH_LOG ("Failed to restore max_map_count to %d" , +TH_LOG ("Failed to restore max_vma_count to %d" , self->original_max_vma_count); }
@@ -617,7 +617,7 @@ static int get_current_vma_count(void)/* * The [vsyscall] mapping is a special mapping that -* doesn't count against the max_map_count limit. +* doesn't count against the max_vma_count limit. * Ignore it here to match the kernel's accounting. */ if (strcmp (vma_name,"[vsyscall]" ) !=0 )
tools/testing/vma/vma.c +16 -16
@@ -261,7 +261,7 @@ static int cleanup_mm(struct mm_struct *mm, struct vma_iterator *vmi) }mtree_destroy (&mm->mm_mt); - mm->map_count =0 ; + mm->vma_count =0 ;return count; }
@@ -500,7 +500,7 @@ static bool test_merge_new(void)INIT_LIST_HEAD (&vma_d->anon_vma_chain);list_add (&dummy_anon_vma_chain_d.same_vma, &vma_d->anon_vma_chain);ASSERT_FALSE (merged); -ASSERT_EQ (mm.map_count,4 ); +ASSERT_EQ (mm.vma_count,4 );/* * Merge BOTH sides.
@@ -519,7 +519,7 @@ static bool test_merge_new(void)ASSERT_EQ (vma->vm_pgoff,0 );ASSERT_EQ (vma->anon_vma, &dummy_anon_vma);ASSERT_TRUE (vma_write_started (vma)); -ASSERT_EQ (mm.map_count,3 ); +ASSERT_EQ (mm.vma_count,3 );/* * Merge to PREVIOUS VMA.
@@ -536,7 +536,7 @@ static bool test_merge_new(void)ASSERT_EQ (vma->vm_pgoff,0 );ASSERT_EQ (vma->anon_vma, &dummy_anon_vma);ASSERT_TRUE (vma_write_started (vma)); -ASSERT_EQ (mm.map_count,3 ); +ASSERT_EQ (mm.vma_count,3 );/* * Merge to NEXT VMA.
@@ -555,7 +555,7 @@ static bool test_merge_new(void)ASSERT_EQ (vma->vm_pgoff,6 );ASSERT_EQ (vma->anon_vma, &dummy_anon_vma);ASSERT_TRUE (vma_write_started (vma)); -ASSERT_EQ (mm.map_count,3 ); +ASSERT_EQ (mm.vma_count,3 );/* * Merge BOTH sides.
@@ -573,7 +573,7 @@ static bool test_merge_new(void)ASSERT_EQ (vma->vm_pgoff,0 );ASSERT_EQ (vma->anon_vma, &dummy_anon_vma);ASSERT_TRUE (vma_write_started (vma)); -ASSERT_EQ (mm.map_count,2 ); +ASSERT_EQ (mm.vma_count,2 );/* * Merge to NEXT VMA.
@@ -591,7 +591,7 @@ static bool test_merge_new(void)ASSERT_EQ (vma->vm_pgoff,0xa );ASSERT_EQ (vma->anon_vma, &dummy_anon_vma);ASSERT_TRUE (vma_write_started (vma)); -ASSERT_EQ (mm.map_count,2 ); +ASSERT_EQ (mm.vma_count,2 );/* * Merge BOTH sides.
@@ -608,7 +608,7 @@ static bool test_merge_new(void)ASSERT_EQ (vma->vm_pgoff,0 );ASSERT_EQ (vma->anon_vma, &dummy_anon_vma);ASSERT_TRUE (vma_write_started (vma)); -ASSERT_EQ (mm.map_count,1 ); +ASSERT_EQ (mm.vma_count,1 );/* * Final state.
@@ -967,7 +967,7 @@ static bool test_vma_merge_new_with_close(void)ASSERT_EQ (vma->vm_pgoff,0 );ASSERT_EQ (vma->vm_ops, &vm_ops);ASSERT_TRUE (vma_write_started (vma)); -ASSERT_EQ (mm.map_count,2 ); +ASSERT_EQ (mm.vma_count,2 );cleanup_mm (&mm, &vmi);return true;
@@ -1017,7 +1017,7 @@ static bool test_merge_existing(void)ASSERT_EQ (vma->vm_pgoff,2 );ASSERT_TRUE (vma_write_started (vma));ASSERT_TRUE (vma_write_started (vma_next)); -ASSERT_EQ (mm.map_count,2 ); +ASSERT_EQ (mm.vma_count,2 );/* Clear down and reset. */ ASSERT_EQ (cleanup_mm (&mm, &vmi),2 );
@@ -1045,7 +1045,7 @@ static bool test_merge_existing(void)ASSERT_EQ (vma_next->vm_pgoff,2 );ASSERT_EQ (vma_next->anon_vma, &dummy_anon_vma);ASSERT_TRUE (vma_write_started (vma_next)); -ASSERT_EQ (mm.map_count,1 ); +ASSERT_EQ (mm.vma_count,1 );/* Clear down and reset. We should have deleted vma. */ ASSERT_EQ (cleanup_mm (&mm, &vmi),1 );
@@ -1079,7 +1079,7 @@ static bool test_merge_existing(void)ASSERT_EQ (vma->vm_pgoff,6 );ASSERT_TRUE (vma_write_started (vma_prev));ASSERT_TRUE (vma_write_started (vma)); -ASSERT_EQ (mm.map_count,2 ); +ASSERT_EQ (mm.vma_count,2 );/* Clear down and reset. */ ASSERT_EQ (cleanup_mm (&mm, &vmi),2 );
@@ -1108,7 +1108,7 @@ static bool test_merge_existing(void)ASSERT_EQ (vma_prev->vm_pgoff,0 );ASSERT_EQ (vma_prev->anon_vma, &dummy_anon_vma);ASSERT_TRUE (vma_write_started (vma_prev)); -ASSERT_EQ (mm.map_count,1 ); +ASSERT_EQ (mm.vma_count,1 );/* Clear down and reset. We should have deleted vma. */ ASSERT_EQ (cleanup_mm (&mm, &vmi),1 );
@@ -1138,7 +1138,7 @@ static bool test_merge_existing(void)ASSERT_EQ (vma_prev->vm_pgoff,0 );ASSERT_EQ (vma_prev->anon_vma, &dummy_anon_vma);ASSERT_TRUE (vma_write_started (vma_prev)); -ASSERT_EQ (mm.map_count,1 ); +ASSERT_EQ (mm.vma_count,1 );/* Clear down and reset. We should have deleted prev and next. */ ASSERT_EQ (cleanup_mm (&mm, &vmi),1 );
@@ -1540,7 +1540,7 @@ static bool test_merge_extend(void)ASSERT_EQ (vma->vm_end,0x4000 );ASSERT_EQ (vma->vm_pgoff,0 );ASSERT_TRUE (vma_write_started (vma)); -ASSERT_EQ (mm.map_count,1 ); +ASSERT_EQ (mm.vma_count,1 );cleanup_mm (&mm, &vmi);return true;
@@ -1652,7 +1652,7 @@ static bool test_mmap_region_basic(void) 0x24d, NULL);ASSERT_EQ (addr,0x24d000 ); -ASSERT_EQ (mm.map_count,2 ); +ASSERT_EQ (mm.vma_count,2 );for_each_vma (vmi, vma) {if (vma->vm_start ==0x300000 ) {
tools/testing/vma/vma_internal.h +1 -1
@@ -261,7 +261,7 @@ typedef struct {struct mm_struct {struct maple_tree mm_mt; -int map_count;/* number of VMAs */ +int vma_count;/* number of VMAs */ unsigned long total_vm;/* Total pages mapped */ unsigned long locked_vm;/* Pages that have PG_mlocked set */ unsigned long data_vm;/* VM_WRITE & ~VM_SHARED & ~VM_STACK */
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog
PATCH v4 5/5 mm/tracing: introduce trace_mm_insufficient_vma_slots event
In reply to Kalesh SinghIntroduce the trace_mm_insufficient_vma_slots tracepoint to improve observability of VMA allocation failures.
This event fires when an operation is about to fail because it requires more VMA slots than are currently available, according to the sysctl_max_map_count limit. This is a preemptive check that occurs in call paths like mmap(), mremap(), and split_vma() before they attempt to create new VMAs.
This tracepoint can be used with event-driven telemetry, such as BPF programs, to collect data from devices in the field with minimal overhead.
The tracepoint captures the mm_struct pointer and the current vma_count at the time of failure. This allows for observing the distribution of these events to determine if there are legitimate bugs or if an increase to the limit is warranted.
---
Changes in v4:
- Update commit description to accurately reflect the trace event's parameters.
Changes in v3:
- capture the mm pointer as the unique identifier and capture the vma_count as well, instead of current task tgid, per Steve
- Add include/trace/events/vma.h to MEMORY MAPPING section in MAINTAINERS, per Lorenzo
- rename trace_max_vma_count_exceeded() to trace_mm_insufficient_vma_slots(), since this is a preemptive check, per Lorenzo
- Fix tools/testing/vma build errors, per Lorenzo
MAINTAINERS | 1 + include/trace/events/vma.h | 32 ++++++++++++++++++++++++++++++++ mm/mmap.c | 5 ++++- mm/mremap.c | 10 ++++++++-- mm/vma.c | 4 +++- mm/vma_internal.h | 2 ++ tools/testing/vma/vma_internal.h | 5 +++++ 7 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 include/trace/events/vma.h
MAINTAINERS +1 -0
@@ -16567,6 +16567,7 @@ S: Maintained W: http://www.linux-mm.org T: git git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm F: include/trace/events/mmap.h +F: include/trace/events/vma.h F: mm/interval_tree.c F: mm/mincore.c F: mm/mlock.c
include/trace/events/vma.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM vma + +#if !defined(_TRACE_VMA_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_VMA_H + +#include <linux/tracepoint.h> + +TRACE_EVENT (mm_insufficient_vma_slots, + + TP_PROTO(struct mm_struct *mm), + +TP_ARGS (mm), + +TP_STRUCT__entry ( + __field(void *, mm) +__field (int, vma_count) + ), + +TP_fast_assign ( + __entry->mm = mm; + __entry->vma_count = mm->vma_count; + ), + +TP_printk ("mm=%p vma_count=%d" , __entry->mm, __entry->vma_count) +); + +#endif /* _TRACE_VMA_H */ + +/* This part must be outside protection */ +#include <trace/define_trace.h>
mm/mmap.c +4 -1
@@ -56,6 +56,7 @@#define CREATE_TRACE_POINTS #include <trace/events/mmap.h> +#include <trace/events/vma.h> #include "internal.h"
@@ -383,8 +384,10 @@ unsigned long do_mmap(struct file *file, unsigned long addr,* sysctl_max_map_count limit by one. This behavior is preserved to * avoid breaking existing applications. */ -if (max_vma_count () - mm->vma_count <0 ) +if (max_vma_count () - mm->vma_count <0 ) { +trace_mm_insufficient_vma_slots (mm);return -ENOMEM ; + }/* * addr is returned from get_unmapped_area,
mm/mremap.c +8 -2
@@ -30,6 +30,8 @@#include <asm/cacheflush.h> #include <asm/tlb.h> +#include <trace/events/vma.h> +#include "internal.h" /* Classify the kind of remap operation being performed. */
@@ -1040,8 +1042,10 @@ static unsigned long prep_move_vma(struct vma_remap_struct *vrm)* We'd prefer to avoid failure later on in do_munmap: * which may split one vma into three before unmapping. */ -if (max_vma_count () - current->mm->vma_count <4 ) +if (max_vma_count () - current->mm->vma_count <4 ) { +trace_mm_insufficient_vma_slots (current->mm);return -ENOMEM ; + }if (vma->vm_ops && vma->vm_ops->may_split) {if (vma->vm_start != old_addr)
@@ -1814,8 +1818,10 @@ static unsigned long check_mremap_params(struct vma_remap_struct *vrm)* the threshold. In other words, is the current map count + 6 at or * below the threshold? Otherwise return -ENOMEM here to be more safe. */ -if (max_vma_count () - current->mm->vma_count <6 ) +if (max_vma_count () - current->mm->vma_count <6 ) { +trace_mm_insufficient_vma_slots (current->mm);return -ENOMEM ; + }return 0 ; }
mm/vma.c +3 -1
@@ -594,8 +594,10 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,static int split_vma (struct vma_iterator *vmi,struct vm_area_struct *vma,unsigned long addr,int new_below) { -if (max_vma_count () - vma->vm_mm->vma_count <1 ) +if (max_vma_count () - vma->vm_mm->vma_count <1 ) { +trace_mm_insufficient_vma_slots (vma->vm_mm);return -ENOMEM ; + }return __split_vma (vmi, vma, addr, new_below); }
mm/vma_internal.h +2 -0
@@ -52,4 +52,6 @@#include "internal.h" +#include <trace/events/vma.h> +#endif /* __MM_VMA_INTERNAL_H */
tools/testing/vma/vma_internal.h +5 -0
@@ -1497,4 +1497,9 @@ static int max_vma_count(void) return sysctl_max_map_count; } +/* Stub for trace_mm_insufficient_vma_slots */ +static inline void trace_mm_insufficient_vma_slots (struct mm_struct *mm) +{ +} +#endif /* __MM_VMA_INTERNAL_H */
Signature · 1 line
2.51.1.851.g4ebd6896fd-goog