Skip to content

Commit d977d56

Browse files
koct9itorvalds
authored andcommitted
mm: warn about VmData over RLIMIT_DATA
This patch provides a way of working around a slight regression introduced by commit 8463833 ("mm: rework virtual memory accounting"). Before that commit RLIMIT_DATA have control only over size of the brk region. But that change have caused problems with all existing versions of valgrind, because it set RLIMIT_DATA to zero. This patch fixes rlimit check (limit actually in bytes, not pages) and by default turns it into warning which prints at first VmData misuse: "mmap: top (795): VmData 516096 exceed data ulimit 512000. Will be forbidden soon." Behavior is controlled by boot param ignore_rlimit_data=y/n and by sysfs /sys/module/kernel/parameters/ignore_rlimit_data. For now it set to "y". [[email protected]: tweak kernel-parameters.txt text[ Signed-off-by: Konstantin Khlebnikov <[email protected]> Link: http://lkml.kernel.org/r/20151228211015.GL2194@uranus Reported-by: Christian Borntraeger <[email protected]> Cc: Cyrill Gorcunov <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Vegard Nossum <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Vladimir Davydov <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Quentin Casasnovas <[email protected]> Cc: Kees Cook <[email protected]> Cc: Willy Tarreau <[email protected]> Cc: Pavel Emelyanov <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4758e19 commit d977d56

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

Documentation/kernel-parameters.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
14961496
could change it dynamically, usually by
14971497
/sys/module/printk/parameters/ignore_loglevel.
14981498

1499+
ignore_rlimit_data
1500+
Ignore RLIMIT_DATA setting for data mappings,
1501+
print warning at first misuse. Can be changed via
1502+
/sys/module/kernel/parameters/ignore_rlimit_data.
1503+
14991504
ihash_entries= [KNL]
15001505
Set number of hash buckets for inode cache.
15011506

mm/internal.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,22 @@ static inline bool is_cow_mapping(vm_flags_t flags)
216216
return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
217217
}
218218

219+
static inline bool is_exec_mapping(vm_flags_t flags)
220+
{
221+
return (flags & (VM_EXEC | VM_WRITE)) == VM_EXEC;
222+
}
223+
224+
static inline bool is_stack_mapping(vm_flags_t flags)
225+
{
226+
return (flags & (VM_STACK_FLAGS & (VM_GROWSUP | VM_GROWSDOWN))) != 0;
227+
}
228+
229+
static inline bool is_data_mapping(vm_flags_t flags)
230+
{
231+
return (flags & ((VM_STACK_FLAGS & (VM_GROWSUP | VM_GROWSDOWN)) |
232+
VM_WRITE | VM_SHARED)) == VM_WRITE;
233+
}
234+
219235
/* mm/util.c */
220236
void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
221237
struct vm_area_struct *prev, struct rb_node *rb_parent);

mm/mmap.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <linux/memory.h>
4343
#include <linux/printk.h>
4444
#include <linux/userfaultfd_k.h>
45+
#include <linux/moduleparam.h>
4546

4647
#include <asm/uaccess.h>
4748
#include <asm/cacheflush.h>
@@ -69,6 +70,8 @@ const int mmap_rnd_compat_bits_max = CONFIG_ARCH_MMAP_RND_COMPAT_BITS_MAX;
6970
int mmap_rnd_compat_bits __read_mostly = CONFIG_ARCH_MMAP_RND_COMPAT_BITS;
7071
#endif
7172

73+
static bool ignore_rlimit_data = true;
74+
core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644);
7275

7376
static void unmap_region(struct mm_struct *mm,
7477
struct vm_area_struct *vma, struct vm_area_struct *prev,
@@ -2982,9 +2985,17 @@ bool may_expand_vm(struct mm_struct *mm, vm_flags_t flags, unsigned long npages)
29822985
if (mm->total_vm + npages > rlimit(RLIMIT_AS) >> PAGE_SHIFT)
29832986
return false;
29842987

2985-
if ((flags & (VM_WRITE | VM_SHARED | (VM_STACK_FLAGS &
2986-
(VM_GROWSUP | VM_GROWSDOWN)))) == VM_WRITE)
2987-
return mm->data_vm + npages <= rlimit(RLIMIT_DATA);
2988+
if (is_data_mapping(flags) &&
2989+
mm->data_vm + npages > rlimit(RLIMIT_DATA) >> PAGE_SHIFT) {
2990+
if (ignore_rlimit_data)
2991+
pr_warn_once("%s (%d): VmData %lu exceed data ulimit "
2992+
"%lu. Will be forbidden soon.\n",
2993+
current->comm, current->pid,
2994+
(mm->data_vm + npages) << PAGE_SHIFT,
2995+
rlimit(RLIMIT_DATA));
2996+
else
2997+
return false;
2998+
}
29882999

29893000
return true;
29903001
}
@@ -2993,11 +3004,11 @@ void vm_stat_account(struct mm_struct *mm, vm_flags_t flags, long npages)
29933004
{
29943005
mm->total_vm += npages;
29953006

2996-
if ((flags & (VM_EXEC | VM_WRITE)) == VM_EXEC)
3007+
if (is_exec_mapping(flags))
29973008
mm->exec_vm += npages;
2998-
else if (flags & (VM_STACK_FLAGS & (VM_GROWSUP | VM_GROWSDOWN)))
3009+
else if (is_stack_mapping(flags))
29993010
mm->stack_vm += npages;
3000-
else if ((flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
3011+
else if (is_data_mapping(flags))
30013012
mm->data_vm += npages;
30023013
}
30033014

0 commit comments

Comments
 (0)