mm: centralize and fix max map count limit checking
Subsystems (3)
PATCH 1/1 mm: centralize and fix max map count limit checking
The check against the max map count (sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.
For example, some paths would check map_count > sysctl_max_map_count before allocating a new VMA and incrementing the count, allowing the process to reach sysctl_max_map_count + 1:
int do_brk_flags(...)
{
if (mm->map_count > sysctl_max_map_count)
return -ENOMEM;
/* We can get here with mm->map_count == sysctl_max_map_count */
vma = vm_area_alloc(mm);
...
mm->map_count++ /* We've now exceeded the threshold. */
}To fix this and unify the logic, introduce a new function, exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.
To improve encapsulation, sysctl_max_map_count is now static to mm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.
--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)
include/linux/mm.h +10 -1
@@ -192,7 +192,16 @@ 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; +/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create. +* +* Returns true if the operation would cause the number of VMAs to exceed +* the sysctl_max_map_count limit, false otherwise. A rate-limited warning +* is logged if the limit is exceeded. +*/ +extern bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas);extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;
mm/mmap.c +14 -1
@@ -374,7 +374,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,return -EOVERFLOW ;/* Too many mappings? */ -if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,0 ))return -ENOMEM ;/*
@@ -1504,6 +1504,19 @@ struct vm_area_struct *_install_special_mapping(int sysctl_legacy_va_layout;#endif +static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; + +bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas) +{ +if (unlikely (mm->map_count + new_vmas > sysctl_max_map_count)) { +pr_warn_ratelimited ("%s (%d): Map count limit %u exceeded\n" , + current->comm, current->pid, + sysctl_max_map_count); +return true; + } +return false; +} +static const struct ctl_table mmap_table[] = { { .procname ="max_map_count" ,
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 (exceeds_max_map_count (current->mm,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 (exceeds_max_map_count (current->mm,6 ))return -ENOMEM ;return 0 ;
mm/nommu.c +1 -1
@@ -1316,7 +1316,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 (exceeds_max_map_count (mm,1 ))return -ENOMEM ; region =kmem_cache_alloc (vm_region_jar,GFP_KERNEL );
mm/util.c +0 -1
@@ -751,7 +751,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 +3 -3
@@ -592,7 +592,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 (exceeds_max_map_count (vma->vm_mm,1 ))return -ENOMEM ;return __split_vma (vmi, vma, addr, new_below);
@@ -1345,7 +1345,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) { +exceeds_max_map_count (vms->vma->vm_mm,1 )) { error = -ENOMEM ; goto map_count_exceeded; }
@@ -2772,7 +2772,7 @@ 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 ; -if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,1 ))return -ENOMEM ;if (security_vm_enough_memory_mm (mm, len >>PAGE_SHIFT ))
Signature · 1 line
2.51.0.338.gd7d06c2dae-goog
Pedro Falcato
reviewerQuoted 8 lines · Kalesh Singh
The check against the max map count (
sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.For example, some paths would check
map_count>sysctl_max_map_countbefore allocating a new VMA and incrementing the count, allowing the process to reachsysctl_max_map_count+ 1:
Quoted 11 lines
int do_brk_flags(...)
{
if (mm->map_count > sysctl_max_map_count)
return -ENOMEM;
/* We can get here with mm->map_count == sysctl_max_map_count */
vma = vm_area_alloc(mm);
...
mm->map_count++ /* We've now exceeded the threshold. */
}I think this should be fixed separately, and sent for stable.
Quoted 5 lines
To fix this and unify the logic, introduce a new function,
exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.
Thanks! In general I like the idea.
Quoted 27 lines
To improve encapsulation,
sysctl_max_map_countis now static tomm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.Cc Andrew Morton <[email protected]>Cc Minchan Kim <[email protected]>Cc Lorenzo Stoakes <[email protected]>Signed-off-by Kalesh Singh <[email protected]>--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)include/linux/mm.h +0
-1@@ -192,7 +192,16 @@ 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;
include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 204
Quoted 10 lines
include/linux/mm.h +10
-0+/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create. +* +* Returns true if the operation would cause the number of VMAs to exceed +* the sysctl_max_map_count limit, false otherwise. A rate-limited warning +* is logged if the limit is exceeded. +*/ +extern bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas);
No new "extern" in func declarations please.
Quoted 8 lines
include/linux/mm.h +0
-0extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;mm/mmap.c +0
-0
mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 378
Quoted 6 lines
mm/mmap.c +1
-1@@ -374,7 +374,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,return -EOVERFLOW ;/* Too many mappings? */ -if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,0 ))return -ENOMEM ;
If the brk example is incorrect, isn't this also wrong? /me is confused
Quoted 4 lines
mm/mmap.c +0
-0/* @@ -1504,6 +1504,19 @@ struct vm_area_struct *_install_special_mapping(int sysctl_legacy_va_layout;
mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 1514
Quoted 10 lines
mm/mmap.c +8
-0#endif +static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; + +bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas) +{ +if (unlikely (mm->map_count + new_vmas > sysctl_max_map_count)) { +pr_warn_ratelimited ("%s (%d): Map count limit %u exceeded\n" , + current->comm, current->pid, + sysctl_max_map_count);
I'm not entirely sold on the map count warn, even if it's rate limited. It sounds like something you can hit in nasty edge cases and nevertheless flood your dmesg (more frustrating if you can't fix the damn program).
In any case, if we are to make the checks more strict, we should also add asserts around the place. Though there's a little case in munmap() we can indeed go over temporarily, on purpose.
Signature · 1 line
Pedro
Quoted 15 lines · Pedro Falcato
Quoted 13 lines · Kalesh Singh
The check against the max map count (
sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.For example, some paths would check
map_count>sysctl_max_map_countbefore allocating a new VMA and incrementing the count, allowing the process to reachsysctl_max_map_count+ 1:int do_brk_flags(...) { if (mm->map_count > sysctl_max_map_count) return -ENOMEM;
Quoted 8 lines
Quoted 6 lines
/* We can get here with mm->map_count == sysctl_max_map_count */ vma = vm_area_alloc(mm); ... mm->map_count++ /* We've now exceeded the threshold. */ }I think this should be fixed separately, and sent for stable.
Hi Pedro, thanks for the review. I can split this out separate.
Quoted 35 lines
Quoted 6 lines
To fix this and unify the logic, introduce a new function,
exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.Thanks! In general I like the idea.
Quoted 26 lines
To improve encapsulation,
sysctl_max_map_countis now static tomm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.Cc Andrew Morton <[email protected]>Cc Minchan Kim <[email protected]>Cc Lorenzo Stoakes <[email protected]>Signed-off-by Kalesh Singh <[email protected]>--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)include/linux/mm.h +0
-0@@ -192,7 +192,16 @@ 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)
Quoted 13 lines
include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 204
Quoted 11 lines
include/linux/mm.h +10
-1-extern int sysctl_max_map_count; +/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create. +* +* Returns true if the operation would cause the number of VMAs to exceed +* the sysctl_max_map_count limit, false otherwise. A rate-limited warning +* is logged if the limit is exceeded. +*/ +extern bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas);No new "extern" in func declarations please.
Ack
Quoted 11 lines
Quoted 11 lines
include/linux/mm.h +0
-0extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;mm/mmap.c +0
-0@@ -374,7 +374,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,return -EOVERFLOW ;
Quoted 6 lines
mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 378
Quoted 4 lines
mm/mmap.c +1
-1/* Too many mappings? */ -if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,0 ))return -ENOMEM ;If the brk example is incorrect, isn't this also wrong? /me is confused
Ahh you are right, this will also go over by 1 once we return from mmap_region(). I'll batch this with the do_brk_flags() fix.
Quoted 6 lines
Quoted 6 lines
mm/mmap.c +0
-0/* @@ -1504,6 +1504,19 @@ struct vm_area_struct *_install_special_mapping(int sysctl_legacy_va_layout;#endif
Quoted 12 lines
mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 1514
Quoted 8 lines
mm/mmap.c +8
-0+static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; + +bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas) +{ +if (unlikely (mm->map_count + new_vmas > sysctl_max_map_count)) { +pr_warn_ratelimited ("%s (%d): Map count limit %u exceeded\n" , + current->comm, current->pid, + sysctl_max_map_count);I'm not entirely sold on the map count warn, even if it's rate limited. It sounds like something you can hit in nasty edge cases and nevertheless flood your dmesg (more frustrating if you can't fix the damn program).
I don't feel strongly about this, I can drop it in the next revision.
Quoted 4 lines
In any case, if we are to make the checks more strict, we should also add asserts around the place. Though there's a little case in
munmap()we can indeed go over temporarily, on purpose.
To confirm, do you mean we should WARN_ON() checks where map count is being incremented?
Quoted 2 lines
Though there's a little case in
munmap()we can indeed go over temporarily, on purpose.
For the 3 way split we need 1 additional VMA after munmap completed as one of the 3 gets unmapped. The check is done in the caller beforehand as __split_vma() explicitly doesn't check map_count. Though if we add asserts we'll need a variant of vma_complete() or the like that doesn't enforce the threshold.
Thanks,
Kalesh
Quoted 3 lines
--
Pedro
Pedro Falcato
reviewerQuoted 2 lines · Kalesh Singh
Quoted 1 line · Pedro Falcato
<snip>
Quoted 30 lines
Quoted 7 lines
mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 378
Quoted 5 lines
/* Too many mappings? */mm/mmap.c +1
-1-if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,0 ))return -ENOMEM ;If the brk example is incorrect, isn't this also wrong? /me is confused
Ahh you are right, this will also go over by 1 once we return from
mmap_region(). I'll batch this with thedo_brk_flags()fix.Quoted 6 lines
Quoted 6 lines
/*mm/mmap.c +0
-0@@ -1504,6 +1504,19 @@ struct vm_area_struct *_install_special_mapping(int sysctl_legacy_va_layout;#endif Quoted 12 lines
mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 1514
Quoted 8 lines
mm/mmap.c +8
-0+static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; + +bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas) +{ +if (unlikely (mm->map_count + new_vmas > sysctl_max_map_count)) { +pr_warn_ratelimited ("%s (%d): Map count limit %u exceeded\n" , + current->comm, current->pid, + sysctl_max_map_count);I'm not entirely sold on the map count warn, even if it's rate limited. It sounds like something you can hit in nasty edge cases and nevertheless flood your dmesg (more frustrating if you can't fix the damn program).
Quoted 1 line
I don't feel strongly about this, I can drop it in the next revision.
FWIW, I don't feel strongly about it either, and I would not mind if there's a way to shut it up (cmdline, or even sysctl knob?). Let's see if anyone has a stronger opinion.
Quoted 8 lines
Quoted 5 lines
In any case, if we are to make the checks more strict, we should also add asserts around the place. Though there's a little case in
munmap()we can indeed go over temporarily, on purpose.To confirm, do you mean we should
WARN_ON()checks where map count is being incremented?
Yes, _possibly_ gated off by CONFIG_DEBUG_VM.
Quoted 9 lines
Quoted 3 lines
Though there's a little case in
munmap()we can indeed go over temporarily, on purpose.For the 3 way split we need 1 additional VMA after munmap completed as one of the 3 gets unmapped. The check is done in the caller beforehand as
__split_vma()explicitly doesn't checkmap_count. Though if we add asserts we'll need a variant ofvma_complete()or the like that doesn't enforce the threshold.
Right, it might get a little hairy, which is partly why I'm not super into the idea. But definitely worth considering as a way to help prevent these sorts of problems in the future.
Signature · 1 line
Pedro
Quoted 62 lines · Pedro Falcato
Quoted 2 lines · Kalesh Singh
Quoted 1 line · Pedro Falcato
<snip>
Quoted 30 lines
Quoted 7 lines
mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 378
Quoted 5 lines
/* Too many mappings? */mm/mmap.c +1
-1-if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,0 ))return -ENOMEM ;If the brk example is incorrect, isn't this also wrong? /me is confused
Ahh you are right, this will also go over by 1 once we return from
mmap_region(). I'll batch this with thedo_brk_flags()fix.Quoted 6 lines
Quoted 6 lines
/*mm/mmap.c +0
-0@@ -1504,6 +1504,19 @@ struct vm_area_struct *_install_special_mapping(int sysctl_legacy_va_layout;#endif Quoted 12 lines
mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 1514
Quoted 8 lines
mm/mmap.c +8
-0+static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; + +bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas) +{ +if (unlikely (mm->map_count + new_vmas > sysctl_max_map_count)) { +pr_warn_ratelimited ("%s (%d): Map count limit %u exceeded\n" , + current->comm, current->pid, + sysctl_max_map_count);I'm not entirely sold on the map count warn, even if it's rate limited. It sounds like something you can hit in nasty edge cases and nevertheless flood your dmesg (more frustrating if you can't fix the damn program).
Quoted 1 line
I don't feel strongly about this, I can drop it in the next revision.
FWIW, I don't feel strongly about it either, and I would not mind if there's a way to shut it up (cmdline, or even sysctl knob?). Let's see if anyone has a stronger opinion.
Quoted 8 lines
Quoted 5 lines
In any case, if we are to make the checks more strict, we should also add asserts around the place. Though there's a little case in
munmap()we can indeed go over temporarily, on purpose.To confirm, do you mean we should
WARN_ON()checks where map count is being incremented?Yes, _possibly_ gated off by
CONFIG_DEBUG_VM.Quoted 9 lines
Quoted 3 lines
Though there's a little case in
munmap()we can indeed go over temporarily, on purpose.For the 3 way split we need 1 additional VMA after munmap completed as one of the 3 gets unmapped. The check is done in the caller beforehand as
__split_vma()explicitly doesn't checkmap_count. Though if we add asserts we'll need a variant ofvma_complete()or the like that doesn't enforce the threshold.
Quoted 3 lines
Right, it might get a little hairy, which is partly why I'm not super into the idea. But definitely worth considering as a way to help prevent these sorts of problems in the future.
Let me take a stab at this to see how it turns out.
Thanks,
Kalesh
Quoted 3 lines
--
Pedro
Quoted 93 lines · Pedro Falcato
Quoted 8 lines · Kalesh Singh
The check against the max map count (
sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.For example, some paths would check
map_count>sysctl_max_map_countbefore allocating a new VMA and incrementing the count, allowing the process to reachsysctl_max_map_count+ 1:Quoted 11 lines
int do_brk_flags(...) { if (mm->map_count > sysctl_max_map_count) return -ENOMEM; /* We can get here with mm->map_count == sysctl_max_map_count */ vma = vm_area_alloc(mm); ... mm->map_count++ /* We've now exceeded the threshold. */ }I think this should be fixed separately, and sent for stable.
Quoted 5 lines
To fix this and unify the logic, introduce a new function,
exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.Thanks! In general I like the idea.
Quoted 27 lines
To improve encapsulation,
sysctl_max_map_countis now static tomm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.Cc Andrew Morton <[email protected]>Cc Minchan Kim <[email protected]>Cc Lorenzo Stoakes <[email protected]>Signed-off-by Kalesh Singh <[email protected]>--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)include/linux/mm.h +0
-1@@ -192,7 +192,16 @@ 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;include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 204
Quoted 10 lines
include/linux/mm.h +10
-0+/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create. +* +* Returns true if the operation would cause the number of VMAs to exceed +* the sysctl_max_map_count limit, false otherwise. A rate-limited warning +* is logged if the limit is exceeded. +*/ +extern bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas);No new "extern" in func declarations please.
Quoted 8 lines
include/linux/mm.h +0
-0extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;mm/mmap.c +0
-0mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 378
Quoted 6 lines
mm/mmap.c +1
-1@@ -374,7 +374,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,return -EOVERFLOW ;/* Too many mappings? */ -if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,0 ))return -ENOMEM ;If the brk example is incorrect, isn't this also wrong? /me is confused
Quoted 6 lines
mm/mmap.c +0
-0/* @@ -1504,6 +1504,19 @@ struct vm_area_struct *_install_special_mapping(int sysctl_legacy_va_layout;#endif
Quoted 12 lines
mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 1514
Quoted 8 lines
mm/mmap.c +8
-0+static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; + +bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas) +{ +if (unlikely (mm->map_count + new_vmas > sysctl_max_map_count)) { +pr_warn_ratelimited ("%s (%d): Map count limit %u exceeded\n" , + current->comm, current->pid, + sysctl_max_map_count);I'm not entirely sold on the map count warn, even if it's rate limited. It sounds like something you can hit in nasty edge cases and nevertheless flood your dmesg (more frustrating if you can't fix the damn program).
How about dynamic_debug?
a1394bddf9b6, mm: page_alloc: dump migrate-failed pages
Quoted 108 lines · Minchan Kim
Quoted 93 lines · Pedro Falcato
Quoted 8 lines · Kalesh Singh
The check against the max map count (
sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.For example, some paths would check
map_count>sysctl_max_map_countbefore allocating a new VMA and incrementing the count, allowing the process to reachsysctl_max_map_count+ 1:Quoted 11 lines
int do_brk_flags(...) { if (mm->map_count > sysctl_max_map_count) return -ENOMEM; /* We can get here with mm->map_count == sysctl_max_map_count */ vma = vm_area_alloc(mm); ... mm->map_count++ /* We've now exceeded the threshold. */ }I think this should be fixed separately, and sent for stable.
Quoted 5 lines
To fix this and unify the logic, introduce a new function,
exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.Thanks! In general I like the idea.
Quoted 27 lines
To improve encapsulation,
sysctl_max_map_countis now static tomm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.Cc Andrew Morton <[email protected]>Cc Minchan Kim <[email protected]>Cc Lorenzo Stoakes <[email protected]>Signed-off-by Kalesh Singh <[email protected]>--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)include/linux/mm.h +0
-1@@ -192,7 +192,16 @@ 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;include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 204
Quoted 10 lines
include/linux/mm.h +10
-0+/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create. +* +* Returns true if the operation would cause the number of VMAs to exceed +* the sysctl_max_map_count limit, false otherwise. A rate-limited warning +* is logged if the limit is exceeded. +*/ +extern bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas);No new "extern" in func declarations please.
Quoted 8 lines
include/linux/mm.h +0
-0extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;mm/mmap.c +0
-0mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 378
Quoted 6 lines
mm/mmap.c +1
-1@@ -374,7 +374,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,return -EOVERFLOW ;/* Too many mappings? */ -if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,0 ))return -ENOMEM ;If the brk example is incorrect, isn't this also wrong? /me is confused
Quoted 6 lines
mm/mmap.c +0
-0/* @@ -1504,6 +1504,19 @@ struct vm_area_struct *_install_special_mapping(int sysctl_legacy_va_layout;#endif Quoted 12 lines
mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 1514
Quoted 8 lines
mm/mmap.c +8
-0+static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; + +bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas) +{ +if (unlikely (mm->map_count + new_vmas > sysctl_max_map_count)) { +pr_warn_ratelimited ("%s (%d): Map count limit %u exceeded\n" , + current->comm, current->pid, + sysctl_max_map_count);I'm not entirely sold on the map count warn, even if it's rate limited. It sounds like something you can hit in nasty edge cases and nevertheless flood your dmesg (more frustrating if you can't fix the damn program).
Quoted 3 lines
How about
dynamic_debug?
a1394bddf9b6, mm:page_alloc: dump migrate-failed pages
Hi Minchan,
Thanks for the suggestion to use dynamic_debug. As you may have seen in the discussion, it has moved to a capacity based helper (vma_count_remaining()) based on feedback for better readability at the call sites. Unfortunately, a side effect of that design is that we've lost the single, centralized failure point where a dynamic_debug message could be placed. I'm going to stick with that due to the readability benefits. However, you've raised a good point about observability. For this I am planning to add force increment/decrement via vma_count_* helpers and perhaps we can add trace events in the helpers to get similar observability.
Thanks,
Kalesh
Mike Rapoport
reviewerQuoted 49 lines · Kalesh Singh
The check against the max map count (
sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.For example, some paths would check
map_count>sysctl_max_map_countbefore allocating a new VMA and incrementing the count, allowing the process to reachsysctl_max_map_count+ 1:int do_brk_flags(...) { if (mm->map_count > sysctl_max_map_count) return -ENOMEM; /* We can get here with mm->map_count == sysctl_max_map_count */ vma = vm_area_alloc(mm); ... mm->map_count++ /* We've now exceeded the threshold. */ }To fix this and unify the logic, introduce a new function,
exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.To improve encapsulation,
sysctl_max_map_countis now static tomm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.Cc Andrew Morton <[email protected]>Cc Minchan Kim <[email protected]>Cc Lorenzo Stoakes <[email protected]>Signed-off-by Kalesh Singh <[email protected]>--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)include/linux/mm.h +0
-0@@ -192,7 +192,16 @@ 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)
include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 202
Quoted 10 lines
include/linux/mm.h +8
-1-extern int sysctl_max_map_count; +/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create. +* +* Returns true if the operation would cause the number of VMAs to exceed +* the sysctl_max_map_count limit, false otherwise. A rate-limited warning +* is logged if the limit is exceeded.
kernel-doc will be unhappy because of missing Return: description. Please s/Returns/Return:/
include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 204
Quoted 2 lines
include/linux/mm.h +2
-0+*/ +extern bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas);
No need for 'extern' here. And the declaration should go to mm/internal.h
Quoted 2 lines
include/linux/mm.h +0
-0extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;
Signature · 2 lines
Sincerely yours, Mike.
Quoted 52 lines · Mike Rapoport
Quoted 50 lines · Kalesh Singh
The check against the max map count (
sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.For example, some paths would check
map_count>sysctl_max_map_countbefore allocating a new VMA and incrementing the count, allowing the process to reachsysctl_max_map_count+ 1:int do_brk_flags(...) { if (mm->map_count > sysctl_max_map_count) return -ENOMEM; /* We can get here with mm->map_count == sysctl_max_map_count */ vma = vm_area_alloc(mm); ... mm->map_count++ /* We've now exceeded the threshold. */ }To fix this and unify the logic, introduce a new function,
exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.To improve encapsulation,
sysctl_max_map_countis now static tomm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.Cc Andrew Morton <[email protected]>Cc Minchan Kim <[email protected]>Cc Lorenzo Stoakes <[email protected]>Signed-off-by Kalesh Singh <[email protected]>--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)include/linux/mm.h +0
-0@@ -192,7 +192,16 @@ 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)
Quoted 12 lines
include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 202
Quoted 9 lines
include/linux/mm.h +8
-1-extern int sysctl_max_map_count; +/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create. +* +* Returns true if the operation would cause the number of VMAs to exceed +* the sysctl_max_map_count limit, false otherwise. A rate-limited warning +* is logged if the limit is exceeded. kernel-doc will be unhappy because of missing Return: description. Please s/Returns/Return:/
Hi Mike, thanks for catching this. I'll fix it in the next version.
-- Kalesh
Quoted 12 lines
include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 204
Quoted 3 lines
include/linux/mm.h +2
-0+*/ +extern bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas);No need for 'extern' here. And the declaration should go to
mm/internal.hinclude/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 207
Quoted 2 lines
include/linux/mm.h +0
-0extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;--
Sincerely yours,
Mike.
Quoted 47 lines · Kalesh Singh
The check against the max map count (
sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.For example, some paths would check
map_count>sysctl_max_map_countbefore allocating a new VMA and incrementing the count, allowing the process to reachsysctl_max_map_count+ 1:int do_brk_flags(...) { if (mm->map_count > sysctl_max_map_count) return -ENOMEM; /* We can get here with mm->map_count == sysctl_max_map_count */ vma = vm_area_alloc(mm); ... mm->map_count++ /* We've now exceeded the threshold. */ }To fix this and unify the logic, introduce a new function,
exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.To improve encapsulation,
sysctl_max_map_countis now static tomm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.Cc Andrew Morton <[email protected]>Cc Minchan Kim <[email protected]>Cc Lorenzo Stoakes <[email protected]>Signed-off-by Kalesh Singh <[email protected]>--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)include/linux/mm.h +0
-0
include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 198
Quoted 8 lines
include/linux/mm.h +4
-1@@ -192,7 +192,16 @@ 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; +/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create.
It's not always a "will" right? At least I remember that this was the worst case scenario in some ("may split").
"The number of new VMAs the operation may create in the worst case.
Signature · 3 lines
Cheers David / dhildenb
Quoted 61 lines · David Hildenbrand
Quoted 47 lines · Kalesh Singh
The check against the max map count (
sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.For example, some paths would check
map_count>sysctl_max_map_countbefore allocating a new VMA and incrementing the count, allowing the process to reachsysctl_max_map_count+ 1:int do_brk_flags(...) { if (mm->map_count > sysctl_max_map_count) return -ENOMEM; /* We can get here with mm->map_count == sysctl_max_map_count */ vma = vm_area_alloc(mm); ... mm->map_count++ /* We've now exceeded the threshold. */ }To fix this and unify the logic, introduce a new function,
exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.To improve encapsulation,
sysctl_max_map_countis now static tomm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.Cc Andrew Morton <[email protected]>Cc Minchan Kim <[email protected]>Cc Lorenzo Stoakes <[email protected]>Signed-off-by Kalesh Singh <[email protected]>--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)include/linux/mm.h +0
-0include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 198
Quoted 8 lines
include/linux/mm.h +4
-1@@ -192,7 +192,16 @@ 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; +/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create. It's not always a "will" right? At least I remember that this was the worst case scenario in some ("may split").
Quoted 2 lines
"The number of new VMAs the operation may create in the worst case.
Hi Daivd,
You are correct. Cases like mremap account for the worst case (3 way split on both src and dest). I'll update the description.
Thanks,
Kalesh
Quoted 6 lines
--
CheersDavid / dhildenb
Quoted 20 lines · Kalesh Singh
The check against the max map count (
sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.For example, some paths would check
map_count>sysctl_max_map_countbefore allocating a new VMA and incrementing the count, allowing the process to reachsysctl_max_map_count+ 1:int do_brk_flags(...) { if (mm->map_count > sysctl_max_map_count) return -ENOMEM; /* We can get here with mm->map_count == sysctl_max_map_count */ vma = vm_area_alloc(mm); ... mm->map_count++ /* We've now exceeded the threshold. */ }
Quoted 9 lines
To fix this and unify the logic, introduce a new function,
exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.To improve encapsulation,
sysctl_max_map_countis now static tomm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.
Yeah this is nice thanks!
Quoted 22 lines
Cc Andrew Morton <[email protected]>Cc Minchan Kim <[email protected]>Cc Lorenzo Stoakes <[email protected]>Signed-off-by Kalesh Singh <[email protected]>--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)include/linux/mm.h +0
-1@@ -192,7 +192,16 @@ 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;
include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 204
Quoted 10 lines
include/linux/mm.h +10
-0+/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create. +* +* Returns true if the operation would cause the number of VMAs to exceed +* the sysctl_max_map_count limit, false otherwise. A rate-limited warning +* is logged if the limit is exceeded. +*/ +extern bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas);
No new externs please (as Pedro also pointed out! :)
mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 377
Quoted 13 lines
include/linux/mm.h +0
-0extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;mm/mmap.c +1
-1@@ -374,7 +374,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,return -EOVERFLOW ;/* Too many mappings? */ -if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,0 ))
Yeah this last 'mystery meat' parameter isn't amazing though to be honest, it's kinda hard to read/understand.
E.g.:
int map_count_capacity(const struct *mm)
{
const int map_count = mm->map_count;
const int max_count = sysctl_max_map_count;
return max_count > map_count ? max_count - map_count : 0;
}Then this would become;
if (!map_count_capacity(mm)) {
// blah.
}Quoted 5 lines
mm/mmap.c +0
-0return -ENOMEM ;/* @@ -1504,6 +1504,19 @@ struct vm_area_struct *_install_special_mapping(int sysctl_legacy_va_layout;
mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 1514
Quoted 10 lines
mm/mmap.c +8
-0#endif +static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; + +bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas) +{ +if (unlikely (mm->map_count + new_vmas > sysctl_max_map_count)) { +pr_warn_ratelimited ("%s (%d): Map count limit %u exceeded\n" , + current->comm, current->pid, + sysctl_max_map_count);
Yeah not a fan of this, we aren't warning elsewhere, it's actually perfectly normal for somebody to exceed this, this isn't at the level of a warning.
Quoted 13 lines
mm/mmap.c +5
-0+return true; + } +return false; +} +static const struct ctl_table mmap_table[] = { { .procname ="max_map_count" ,mm/mremap.c +0
-0
mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 6 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;
In my version this would be:
if (map_count_capacity(current->mm) < 4) return -ENOMEM;
And etc. etc. I won't do them all :)
I do think this is clearer...
Quoted 74 lines
mm/mremap.c +3
-2if (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 (exceeds_max_map_count (current->mm,6 ))return -ENOMEM ;return 0 ;mm/nommu.c +1
-1@@ -1316,7 +1316,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 (exceeds_max_map_count (mm,1 ))return -ENOMEM ; region =kmem_cache_alloc (vm_region_jar,GFP_KERNEL );mm/util.c +0
-1@@ -751,7 +751,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 +3
-4@@ -592,7 +592,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 (exceeds_max_map_count (vma->vm_mm,1 ))return -ENOMEM ;return __split_vma (vmi, vma, addr, new_below);@@ -1345,7 +1345,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) { +exceeds_max_map_count (vms->vma->vm_mm,1 )) { error = -ENOMEM ; goto map_count_exceeded; }@@ -2772,7 +2772,7 @@ 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 ; -if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,1 ))return -ENOMEM ;if (security_vm_enough_memory_mm (mm, len >>PAGE_SHIFT )) --2.51.0.338.gd7d06c2dae-goog
Quoted 96 lines · Lorenzo Stoakes
Quoted 20 lines · Kalesh Singh
The check against the max map count (
sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.For example, some paths would check
map_count>sysctl_max_map_countbefore allocating a new VMA and incrementing the count, allowing the process to reachsysctl_max_map_count+ 1:int do_brk_flags(...) { if (mm->map_count > sysctl_max_map_count) return -ENOMEM; /* We can get here with mm->map_count == sysctl_max_map_count */ vma = vm_area_alloc(mm); ... mm->map_count++ /* We've now exceeded the threshold. */ }Quoted 9 lines
To fix this and unify the logic, introduce a new function,
exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.To improve encapsulation,
sysctl_max_map_countis now static tomm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.Yeah this is nice thanks!
Quoted 22 lines
Cc Andrew Morton <[email protected]>Cc Minchan Kim <[email protected]>Cc Lorenzo Stoakes <[email protected]>Signed-off-by Kalesh Singh <[email protected]>--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)include/linux/mm.h +0
-1@@ -192,7 +192,16 @@ 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;include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 204
Quoted 10 lines
include/linux/mm.h +10
-0+/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create. +* +* Returns true if the operation would cause the number of VMAs to exceed +* the sysctl_max_map_count limit, false otherwise. A rate-limited warning +* is logged if the limit is exceeded. +*/ +extern bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas);No new externs please (as Pedro also pointed out! :)
mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 377
Quoted 13 lines
include/linux/mm.h +0
-0extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;mm/mmap.c +1
-1@@ -374,7 +374,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,return -EOVERFLOW ;/* Too many mappings? */ -if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,0 ))Yeah this last 'mystery meat' parameter isn't amazing though to be honest, it's kinda hard to read/understand.
E.g.:
int
map_count_capacity(const struct *mm){ const int map_count = mm->map_count; const int max_count = sysctl_max_map_count; return max_count > map_count ? max_count - map_count : 0; }
Quoted 6 lines
Then this would become;
if (!map_count_capacity(mm)) { // blah. }
Hi Lorenzo,
I agree your suggestion is a lot clearer and readable. Thanks for the suggestion :) Let me update that in the next revision.
--Kalesh
Quoted 123 lines
Quoted 6 lines
mm/mmap.c +0
-0return -ENOMEM ;/* @@ -1504,6 +1504,19 @@ struct vm_area_struct *_install_special_mapping(int sysctl_legacy_va_layout;mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 1514
Quoted 10 lines
mm/mmap.c +8
-0#endif +static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; + +bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas) +{ +if (unlikely (mm->map_count + new_vmas > sysctl_max_map_count)) { +pr_warn_ratelimited ("%s (%d): Map count limit %u exceeded\n" , + current->comm, current->pid, + sysctl_max_map_count);Yeah not a fan of this, we aren't warning elsewhere, it's actually perfectly normal for somebody to exceed this, this isn't at the level of a warning.
Quoted 13 lines
mm/mmap.c +5
-0+return true; + } +return false; +} +static const struct ctl_table mmap_table[] = { { .procname ="max_map_count" ,mm/mremap.c +0
-0mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 6 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;In my version this would be:
if (map_count_capacity(current->mm) < 4) return -ENOMEM;And etc. etc. I won't do them all :)
I do think this is clearer...
Quoted 74 lines
mm/mremap.c +3
-2if (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 (exceeds_max_map_count (current->mm,6 ))return -ENOMEM ;return 0 ;mm/nommu.c +1
-1@@ -1316,7 +1316,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 (exceeds_max_map_count (mm,1 ))return -ENOMEM ; region =kmem_cache_alloc (vm_region_jar,GFP_KERNEL );mm/util.c +0
-1@@ -751,7 +751,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 +3
-4@@ -592,7 +592,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 (exceeds_max_map_count (vma->vm_mm,1 ))return -ENOMEM ;return __split_vma (vmi, vma, addr, new_below);@@ -1345,7 +1345,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) { +exceeds_max_map_count (vms->vma->vm_mm,1 )) { error = -ENOMEM ; goto map_count_exceeded; }@@ -2772,7 +2772,7 @@ 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 ; -if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,1 ))return -ENOMEM ;if (security_vm_enough_memory_mm (mm, len >>PAGE_SHIFT )) --2.51.0.338.gd7d06c2dae-goog
Quoted 143 lines · Lorenzo Stoakes
Quoted 20 lines · Kalesh Singh
The check against the max map count (
sysctl_max_map_count) was open-coded in several places. This led to inconsistent enforcement and subtle bugs where the limit could be exceeded.For example, some paths would check
map_count>sysctl_max_map_countbefore allocating a new VMA and incrementing the count, allowing the process to reachsysctl_max_map_count+ 1:int do_brk_flags(...) { if (mm->map_count > sysctl_max_map_count) return -ENOMEM; /* We can get here with mm->map_count == sysctl_max_map_count */ vma = vm_area_alloc(mm); ... mm->map_count++ /* We've now exceeded the threshold. */ }Quoted 9 lines
To fix this and unify the logic, introduce a new function,
exceeds_max_map_count(), to consolidate the check. All open-coded checks are replaced with calls to this new function, ensuring the limit is applied uniformly and correctly.To improve encapsulation,
sysctl_max_map_countis now static tomm/mmap.c. The new helper also adds a rate-limited warning to make debugging applications that exhaust their VMA limit easier.Yeah this is nice thanks!
Quoted 22 lines
Cc Andrew Morton <[email protected]>Cc Minchan Kim <[email protected]>Cc Lorenzo Stoakes <[email protected]>Signed-off-by Kalesh Singh <[email protected]>--- include/linux/mm.h | 11 ++++++++++- mm/mmap.c | 15 ++++++++++++++- mm/mremap.c | 7 ++++--- mm/nommu.c | 2 +- mm/util.c | 1 - mm/vma.c | 6 +++--- 6 files changed, 32 insertions(+), 10 deletions(-)include/linux/mm.h +0
-1@@ -192,7 +192,16 @@ 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;include/linux/mm.h static inline void __mm_zero_struct_page(struct page *page) · line 204
Quoted 10 lines
include/linux/mm.h +10
-0+/** +* exceeds_max_map_count - check if a VMA operation would exceed max_map_count +* @mm: The memory descriptor for the process. +* @new_vmas: The number of new VMAs the operation will create. +* +* Returns true if the operation would cause the number of VMAs to exceed +* the sysctl_max_map_count limit, false otherwise. A rate-limited warning +* is logged if the limit is exceeded. +*/ +extern bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas);No new externs please (as Pedro also pointed out! :)
mm/mmap.c unsigned long do_mmap(struct file *file, unsigned long addr, · line 377
Quoted 13 lines
include/linux/mm.h +0
-0extern unsigned long sysctl_user_reserve_kbytes;extern unsigned long sysctl_admin_reserve_kbytes;mm/mmap.c +1
-1@@ -374,7 +374,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr,return -EOVERFLOW ;/* Too many mappings? */ -if (mm->map_count > sysctl_max_map_count) +if (exceeds_max_map_count (mm,0 ))Yeah this last 'mystery meat' parameter isn't amazing though to be honest, it's kinda hard to read/understand.
E.g.:
int
map_count_capacity(const struct *mm){ const int map_count = mm->map_count; const int max_count = sysctl_max_map_count; return max_count > map_count ? max_count - map_count : 0; }Then this would become;
if (!map_count_capacity(mm)) { // blah. }Quoted 5 lines
mm/mmap.c +0
-0return -ENOMEM ;/* @@ -1504,6 +1504,19 @@ struct vm_area_struct *_install_special_mapping(int sysctl_legacy_va_layout;mm/mmap.c struct vm_area_struct *_install_special_mapping( · line 1514
Quoted 10 lines
mm/mmap.c +8
-0#endif +static int sysctl_max_map_count__read_mostly =DEFAULT_MAX_MAP_COUNT ; + +bool exceeds_max_map_count (struct mm_struct *mm,unsigned int new_vmas) +{ +if (unlikely (mm->map_count + new_vmas > sysctl_max_map_count)) { +pr_warn_ratelimited ("%s (%d): Map count limit %u exceeded\n" , + current->comm, current->pid, + sysctl_max_map_count);Yeah not a fan of this, we aren't warning elsewhere, it's actually perfectly normal for somebody to exceed this, this isn't at the level of a warning.
Quoted 13 lines
mm/mmap.c +5
-0+return true; + } +return false; +} +static const struct ctl_table mmap_table[] = { { .procname ="max_map_count" ,mm/mremap.c +0
-0mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 6 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;In my version this would be:
Quoted 3 lines
if (map_count_capacity(current->mm) < 4) return -ENOMEM;
Someone could write map_count_capacity(current->mm) <= 4 and reintroduce what this is trying to solve. And with the way it is written in this patch, someone could pass in the wrong number.
I'm not sure this is worth doing. There are places we allow the count to go higher.
Certainly fix the brk < to be <= and any other calculations, but the rest seem okay as-is to me. The only real way to be sure we don't cause a bug in the future is to have better testing.
Thanks,
Liam
Quoted 14 lines · Liam R. Howlett
Quoted 14 lines
mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 11 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;In my version this would be:
Quoted 7 lines
Quoted 3 lines
if (map_count_capacity(current->mm) < 4) return -ENOMEM;Someone could write
map_count_capacity(current->mm)<= 4 and reintroduce what this is trying to solve. And with the way it is written in this patch, someone could pass in the wrong number.
Right, but I think 'capacity' is pretty clear here, if the caller does something silly then that's on them...
Quoted 3 lines
I'm not sure this is worth doing. There are places we allow the count to go higher.
...But yeah, it's kinda borderline as to how useful this is.
I _do_ however like the 'put map count in one place statically' rather than having a global, so a minimal version of this could be to just have a helper function that gets the sysctl_max_map_count, e.g.:
if (current->mm->mmap_count >= max_map_count() - 3)
etc. etc.
Quoted 4 lines
Certainly fix the brk < to be <= and any other calculations, but the rest seem okay as-is to me. The only real way to be sure we don't cause a bug in the future is to have better testing.
Speaking of testing - Kalesh - do make sure to test the VMA tests to make sure this doesn't break those - they live in tools/testing/vma and you just have to do make && ./vma
Cheers!
Quoted 3 lines
Thanks,
Liam
Quoted 30 lines · Lorenzo Stoakes
Quoted 14 lines · Liam R. Howlett
Quoted 14 lines
mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 11 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;In my version this would be:
Quoted 7 lines
Quoted 3 lines
if (map_count_capacity(current->mm) < 4) return -ENOMEM;Someone could write
map_count_capacity(current->mm)<= 4 and reintroduce what this is trying to solve. And with the way it is written in this patch, someone could pass in the wrong number.Right, but I think 'capacity' is pretty clear here, if the caller does something silly then that's on them...
Quoted 3 lines
I'm not sure this is worth doing. There are places we allow the count to go higher.
Quoted 7 lines
...But yeah, it's kinda borderline as to how useful this is.
I _do_ however like the 'put map count in one place statically' rather than having a global, so a minimal version of this could be to just have a helper function that gets the
sysctl_max_map_count, e.g.:if (
current->mm->mmap_count>=max_map_count()- 3)
I enjoy seeing sysctl_max_map_count hidden. But map_count_capacity() is even more readable, so I like it.
I don't complete like the "capacity" term, but I cannot think of something better right now. Maybe something around "free" or "remaining", not sure.
I also don't completely like "map_count" (I know, I know, we call it like that in structures), because it reminds me of the mapcount ... talking somehow about "vmas" would be quite clear.
Anyhow, just as an inspiration my 2 cents ...
Signature · 3 lines
Cheers David / dhildenb
Quoted 43 lines · David Hildenbrand
Quoted 30 lines · Lorenzo Stoakes
Quoted 14 lines · Liam R. Howlett
Quoted 14 lines
mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 11 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;In my version this would be:
Quoted 7 lines
Quoted 3 lines
if (map_count_capacity(current->mm) < 4) return -ENOMEM;Someone could write
map_count_capacity(current->mm)<= 4 and reintroduce what this is trying to solve. And with the way it is written in this patch, someone could pass in the wrong number.Right, but I think 'capacity' is pretty clear here, if the caller does something silly then that's on them...
Quoted 3 lines
I'm not sure this is worth doing. There are places we allow the count to go higher.
Quoted 7 lines
...But yeah, it's kinda borderline as to how useful this is.
I _do_ however like the 'put map count in one place statically' rather than having a global, so a minimal version of this could be to just have a helper function that gets the
sysctl_max_map_count, e.g.:if (
current->mm->mmap_count>=max_map_count()- 3)I enjoy seeing
sysctl_max_map_counthidden. Butmap_count_capacity()is even more readable, so I like it.
Quoted 7 lines
I don't complete like the "capacity" term, but I cannot think of something better right now. Maybe something around "free" or "remaining", not sure.
I also don't completely like "
map_count" (I know, I know, we call it like that in structures), because it reminds me of the mapcount ... talking somehow about "vmas" would be quite clear.
Thanks David, my original implementation started with vma_limit() :). Maybe something like vma_count_remaining() ?
-- Kalesh
Quoted 8 lines
Anyhow, just as an inspiration my 2 cents ...
--
CheersDavid / dhildenb
Quoted 48 lines · Kalesh Singh
Quoted 47 lines · David Hildenbrand
Quoted 30 lines · Lorenzo Stoakes
Quoted 14 lines · Liam R. Howlett
Quoted 14 lines
mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 11 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;In my version this would be:
Quoted 7 lines
Quoted 3 lines
if (map_count_capacity(current->mm) < 4) return -ENOMEM;Someone could write
map_count_capacity(current->mm)<= 4 and reintroduce what this is trying to solve. And with the way it is written in this patch, someone could pass in the wrong number.Right, but I think 'capacity' is pretty clear here, if the caller does something silly then that's on them...
Quoted 3 lines
I'm not sure this is worth doing. There are places we allow the count to go higher.
Quoted 7 lines
...But yeah, it's kinda borderline as to how useful this is.
I _do_ however like the 'put map count in one place statically' rather than having a global, so a minimal version of this could be to just have a helper function that gets the
sysctl_max_map_count, e.g.:if (
current->mm->mmap_count>=max_map_count()- 3)I enjoy seeing
sysctl_max_map_counthidden. Butmap_count_capacity()is even more readable, so I like it.I don't complete like the "capacity" term, but I cannot think of something better right now. Maybe something around "free" or "remaining", not sure.
Quoted 6 lines
Quoted 3 lines
I also don't completely like "
map_count" (I know, I know, we call it like that in structures), because it reminds me of the mapcount ... talking somehow about "vmas" would be quite clear.Thanks David, my original implementation started with
vma_limit():). Maybe something likevma_count_remaining()?
Yes, reducing this confusion would very much be helpful. In fact, if you put it in its own function we could change the actual name with lower impact. map_count vs mapcount is annoying.
vma_headroom() ?additional_vma_space() ?
Maybe David would like:remedy_vma_space() which would be !poison_vma_space().. that's pretty clear.. :)
Cheers,
Liam
Quoted 56 lines · Liam R. Howlett
Quoted 48 lines · Kalesh Singh
Quoted 47 lines · David Hildenbrand
Quoted 30 lines · Lorenzo Stoakes
Quoted 14 lines · Liam R. Howlett
Quoted 14 lines
mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 11 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;In my version this would be:
Quoted 7 lines
Quoted 3 lines
if (map_count_capacity(current->mm) < 4) return -ENOMEM;Someone could write
map_count_capacity(current->mm)<= 4 and reintroduce what this is trying to solve. And with the way it is written in this patch, someone could pass in the wrong number.Right, but I think 'capacity' is pretty clear here, if the caller does something silly then that's on them...
Quoted 3 lines
I'm not sure this is worth doing. There are places we allow the count to go higher.
Quoted 7 lines
...But yeah, it's kinda borderline as to how useful this is.
I _do_ however like the 'put map count in one place statically' rather than having a global, so a minimal version of this could be to just have a helper function that gets the
sysctl_max_map_count, e.g.:if (
current->mm->mmap_count>=max_map_count()- 3)I enjoy seeing
sysctl_max_map_counthidden. Butmap_count_capacity()is even more readable, so I like it.I don't complete like the "capacity" term, but I cannot think of something better right now. Maybe something around "free" or "remaining", not sure.
Quoted 6 lines
Quoted 3 lines
I also don't completely like "
map_count" (I know, I know, we call it like that in structures), because it reminds me of the mapcount ... talking somehow about "vmas" would be quite clear.Thanks David, my original implementation started with
vma_limit():). Maybe something likevma_count_remaining()?
Quoted 6 lines
Yes, reducing this confusion would very much be helpful. In fact, if you put it in its own function we could change the actual name with lower impact.
map_countvs mapcount is annoying.
vma_headroom()?additional_vma_space()?
VMA space might be interpreted as VA space.
I think basing it on "vma_count" would be good.
vma_count_capacity()
vma_count_headroom()
vma_count_remaining()
vma_count_avail()
vma_count_left()
Quoted 4 lines
Maybe David would like:
remedy_vma_space()which would be !poison_vma_space().. that's pretty clear.. :)
:D Careful, sensitive topic; I know that Lorenzo is still crying about this late at night when nobody watches, screaming "DAVID, WHYYYYYYY" ;)
Signature · 3 lines
Cheers David / dhildenb
Quoted 57 lines · David Hildenbrand
Quoted 56 lines · Liam R. Howlett
Quoted 54 lines · Kalesh Singh
Quoted 43 lines · David Hildenbrand
Quoted 30 lines · Lorenzo Stoakes
Quoted 14 lines · Liam R. Howlett
Quoted 14 lines
mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 11 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;In my version this would be:
Quoted 7 lines
Quoted 3 lines
if (map_count_capacity(current->mm) < 4) return -ENOMEM;Someone could write
map_count_capacity(current->mm)<= 4 and reintroduce what this is trying to solve. And with the way it is written in this patch, someone could pass in the wrong number.Right, but I think 'capacity' is pretty clear here, if the caller does something silly then that's on them...
Quoted 3 lines
I'm not sure this is worth doing. There are places we allow the count to go higher.
Quoted 7 lines
...But yeah, it's kinda borderline as to how useful this is.
I _do_ however like the 'put map count in one place statically' rather than having a global, so a minimal version of this could be to just have a helper function that gets the
sysctl_max_map_count, e.g.:if (
current->mm->mmap_count>=max_map_count()- 3)I enjoy seeing
sysctl_max_map_counthidden. Butmap_count_capacity()is even more readable, so I like it.Quoted 7 lines
I don't complete like the "capacity" term, but I cannot think of something better right now. Maybe something around "free" or "remaining", not sure.
I also don't completely like "
map_count" (I know, I know, we call it like that in structures), because it reminds me of the mapcount ... talking somehow about "vmas" would be quite clear.Thanks David, my original implementation started with
vma_limit():). Maybe something likevma_count_remaining()?
Quoted 8 lines
Quoted 6 lines
Yes, reducing this confusion would very much be helpful. In fact, if you put it in its own function we could change the actual name with lower impact.
map_countvs mapcount is annoying.
vma_headroom()?additional_vma_space()?VMA space might be interpreted as VA space.
Fair enough.
Quoted 8 lines
I think basing it on "
vma_count" would be good.
vma_count_capacity()
vma_count_headroom()
vma_count_remaining()
headroom or remaining have my vote as the clearest.
Quoted 5 lines
vma_count_avail()
vma_count_left()
Mike Rapoport
reviewerQuoted 72 lines · Liam R. Howlett
Quoted 57 lines · David Hildenbrand
Quoted 56 lines · Liam R. Howlett
Quoted 54 lines · Kalesh Singh
Quoted 43 lines · David Hildenbrand
Quoted 30 lines · Lorenzo Stoakes
Quoted 14 lines · Liam R. Howlett
Quoted 14 lines
mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 11 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;In my version this would be:
Quoted 7 lines
Quoted 3 lines
if (map_count_capacity(current->mm) < 4) return -ENOMEM;Someone could write
map_count_capacity(current->mm)<= 4 and reintroduce what this is trying to solve. And with the way it is written in this patch, someone could pass in the wrong number.Right, but I think 'capacity' is pretty clear here, if the caller does something silly then that's on them...
Quoted 3 lines
I'm not sure this is worth doing. There are places we allow the count to go higher.
Quoted 7 lines
...But yeah, it's kinda borderline as to how useful this is.
I _do_ however like the 'put map count in one place statically' rather than having a global, so a minimal version of this could be to just have a helper function that gets the
sysctl_max_map_count, e.g.:if (
current->mm->mmap_count>=max_map_count()- 3)I enjoy seeing
sysctl_max_map_counthidden. Butmap_count_capacity()is even more readable, so I like it.Quoted 7 lines
I don't complete like the "capacity" term, but I cannot think of something better right now. Maybe something around "free" or "remaining", not sure.
I also don't completely like "
map_count" (I know, I know, we call it like that in structures), because it reminds me of the mapcount ... talking somehow about "vmas" would be quite clear.Thanks David, my original implementation started with
vma_limit():). Maybe something likevma_count_remaining()?Quoted 8 lines
Quoted 6 lines
Yes, reducing this confusion would very much be helpful. In fact, if you put it in its own function we could change the actual name with lower impact.
map_countvs mapcount is annoying.
vma_headroom()?additional_vma_space()?VMA space might be interpreted as VA space.
Fair enough.
Quoted 3 lines
I think basing it on "
vma_count" would be good.
Quoted 7 lines
Quoted 5 lines
vma_count_capacity()
vma_count_headroom()
vma_count_remaining()headroom or remaining have my vote as the clearest.
I like 'remaining' more
Quoted 4 lines
Quoted 4 lines
vma_count_avail()
vma_count_left()
Signature · 2 lines
Sincerely yours, Mike.
Quoted 16 lines · Lorenzo Stoakes
Quoted 14 lines · Liam R. Howlett
Quoted 14 lines
mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 11 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;In my version this would be:
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
if (map_count_capacity(current->mm) < 4) return -ENOMEM;Someone could write
map_count_capacity(current->mm)<= 4 and reintroduce what this is trying to solve. And with the way it is written in this patch, someone could pass in the wrong number.
Hi Liam,
I still think there is value to this as it's lot less likely to get the common case incorrectly:
if (!map_count_capacity(mm))
return -ENOMEM;
It also facilitate us adding the asserts as Pedro suggested (excluding the munmap() case.
Quoted 19 lines
Right, but I think 'capacity' is pretty clear here, if the caller does something silly then that's on them...
Quoted 3 lines
I'm not sure this is worth doing. There are places we allow the count to go higher.
...But yeah, it's kinda borderline as to how useful this is.
I _do_ however like the 'put map count in one place statically' rather than having a global, so a minimal version of this could be to just have a helper function that gets the
sysctl_max_map_count, e.g.:if (
current->mm->mmap_count>=max_map_count()- 3)etc. etc.
Quoted 1 line
Quoted 7 lines
Quoted 3 lines
Certainly fix the brk < to be <= and any other calculations, but the rest seem okay as-is to me. The only real way to be sure we don't cause a bug in the future is to have better testing.
Speaking of testing - Kalesh - do make sure to test the VMA tests to make sure this doesn't break those - they live in
tools/testing/vmaand you just have to do make && ./vma
Thanks Lorenzo, will do.
-- Kalesh
Quoted 6 lines
Cheers!
Quoted 3 lines
Thanks,
Liam
Quoted 28 lines · Kalesh Singh
Quoted 16 lines · Lorenzo Stoakes
Quoted 14 lines · Liam R. Howlett
Quoted 14 lines
mm/mremap.c static unsigned long prep_move_vma(struct vma_remap_struct *vrm) · line 1044
Quoted 11 lines
mm/mremap.c +1
-1@@ -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 (exceeds_max_map_count (current->mm,4 ))return -ENOMEM ;In my version this would be:
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
if (map_count_capacity(current->mm) < 4) return -ENOMEM;Someone could write
map_count_capacity(current->mm)<= 4 and reintroduce what this is trying to solve. And with the way it is written in this patch, someone could pass in the wrong number.Hi Liam,
Quoted 8 lines
I still think there is value to this as it's lot less likely to get the common case incorrectly:
if (!map_count_capacity(mm))
return -ENOMEM;It also facilitate us adding the asserts as Pedro suggested (excluding the
munmap()case.
And munmap() callers case, I guess.
There is still the possibility of us failing to unmap after a split and having shot ourselves, so just don't assert that in the case of the failure and recovery path (ie exit_mmap()).
Quoted 4 lines
Quoted 4 lines
Right, but I think 'capacity' is pretty clear here, if the caller does something silly then that's on them...
Turns out it's on us, not them :)
Quoted 16 lines
Quoted 16 lines
Quoted 4 lines
I'm not sure this is worth doing. There are places we allow the count to go higher.
...But yeah, it's kinda borderline as to how useful this is.
I _do_ however like the 'put map count in one place statically' rather than having a global, so a minimal version of this could be to just have a helper function that gets the
sysctl_max_map_count, e.g.:if (
current->mm->mmap_count>=max_map_count()- 3)etc. etc.
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Certainly fix the brk < to be <= and any other calculations, but the rest seem okay as-is to me. The only real way to be sure we don't cause a bug in the future is to have better testing.
Speaking of testing - Kalesh - do make sure to test the VMA tests to make sure this doesn't break those - they live in
tools/testing/vmaand you just have to do make && ./vma
Bonus points if you can add some tests for it!
Thanks,
Liam