Skip to contentSkip to conversation

+ mm-add-a-field-to-store-names-for-private-anonymous-memory.patch added to -mm tree

PATCH [email protected] 1 participant 1 message Oct 15, 2013

Subsystems (7)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

Subject: + mm-add-a-field-to-store-names-for-private-anonymous-memory.patch added to -mm tree
To: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]
From: [email protected]
Date: Tue, 15 Oct 2013 14:28:48 -0700

The patch titled
Subject: mm: add a field to store names for private anonymous memory
has been added to the -mm tree. Its filename is
mm-add-a-field-to-store-names-for-private-anonymous-memory.patch

This patch should soon appear at
http://ozlabs.org/~akpm/mmots/broken-out/mm-add-a-field-to-store-names-for-private-anonymous-memory.patch
and later at
http://ozlabs.org/~akpm/mmotm/broken-out/mm-add-a-field-to-store-names-for-private-anonymous-memory.patch

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/SubmitChecklist when testing your code ***

The -mm tree is included into linux-next and is updated there every 3-4 working days

------------------------------------------------------
From: Colin Cross <[email protected]>
Subject: mm: add a field to store names for private anonymous memory

In many userspace applications, and especially in VM based applications like Android uses heavily, there are multiple different allocators in use.
At a minimum there is libc malloc and the stack, and in many cases there
are libc malloc, the stack, direct syscalls to mmap anonymous memory, and multiple VM heaps (one for small objects, one for big objects, etc.). Each of these layers usually has its own tools to inspect its usage; malloc by compiling a debug version, the VM through heap inspection tools, and for direct syscalls there is usually no way to track them.

On Android we heavily use a set of tools that use an extended version of the logic covered in Documentation/vm/pagemap.txt to walk all pages mapped in userspace and slice their usage by process, shared (COW) vs. unique mappings, backing, etc. This can account for real physical memory usage even in cases like fork without exec (which Android uses heavily to share as many private COW pages as possible between processes), Kernel SamePage Merging, and clean zero pages. It produces a measurement of the pages that only exist in that process (USS, for unique), and a measurement of the physical memory usage of that process with the cost of shared pages being evenly split between processes that share them (PSS).

If all anonymous memory is indistinguishable then figuring out the real physical memory usage (PSS) of each heap requires either a pagemap walking tool that can understand the heap debugging of every layer, or for every layer's heap debugging tools to implement the pagemap walking logic, in which case it is hard to get a consistent view of memory across the whole system.

This patch adds a field to /proc/pid/maps and /proc/pid/smaps to show a userspace-provided name for anonymous vmas. The names of named anonymous vmas are shown in /proc/pid/maps and /proc/pid/smaps as [anon:<name>].

Userspace can set the name for a region of memory by calling prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, start, len, (unsigned long)name); Setting the name to NULL clears it.

The name is stored in a user pointer in the shared union in vm_area_struct that points to a null terminated string inside the user process. vmas that point to the same address and are otherwise mergeable will be merged, but vmas that point to equivalent strings at different addresses will not be merged.

The idea to store a userspace pointer to reduce the complexity within mm (at the expense of the complexity of reading /proc/pid/mem) came from Dave Hansen. This results in no runtime overhead in the mm subsystem other than comparing the anon_name pointers when considering vma merging. The pointer is stored in a union with fields that are only used on file-backed mappings, so it does not increase memory usage.

Cc Pekka Enberg <[email protected]>
Cc Dave Hansen <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc "Eric W. Biederman" <[email protected]>
Cc Jan Glauber <[email protected]>
Cc John Stultz <[email protected]>
Cc Rob Landley <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc David Rientjes <[email protected]>
Cc Al Viro <[email protected]>
Cc Hugh Dickins <[email protected]>
Cc Rik van Riel <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Michel Lespinasse <[email protected]>
Cc Tang Chen <[email protected]>
Cc Robin Holt <[email protected]>
Cc Shaohua Li <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Johannes Weiner <[email protected]>

---

 Documentation/filesystems/proc.txt |    2 
 fs/proc/task_mmu.c                 |   22 ++++++++++
 include/linux/mm.h                 |    5 +-
 include/linux/mm_types.h           |   15 +++++++
 include/uapi/linux/prctl.h         |    3 +
 kernel/sys.c                       |   24 +++++++++++
 mm/madvise.c                       |   55 +++++++++++++++++++++++++--
 mm/mempolicy.c                     |    2 
 mm/mlock.c                         |    3 -
 mm/mmap.c                          |   44 ++++++++++++---------
 mm/mprotect.c                      |    3 -
 11 files changed, 152 insertions(+), 26 deletions(-)
Documentation/filesystems/proc.txt renamed from Documentation/filesystems/proc.txt~mm-add-a-field-to-store-names-for-private-anonymous-memory +2 -0
diff -puN Documentation/filesystems/proc.txt~mm-add-a-field-to-store-names-for-private-anonymous-memory Documentation/filesystems/proc.txt
@@ -369,6 +369,8 @@ is not associated with a file:
  [stack:1001]             = the stack of the thread with tid 1001
  [vdso]                   = the "virtual dynamic shared object",
                             the kernel system call handler
+ [anon:<name>]            = an anonymous mapping that has been
+                            named by userspace
 
  or if empty, the mapping is anonymous.
 
fs/proc/task_mmu.c renamed from fs/proc/task_mmu.c~mm-add-a-field-to-store-names-for-private-anonymous-memory +22 -0
diff -puN fs/proc/task_mmu.c~mm-add-a-field-to-store-names-for-private-anonymous-memory fs/proc/task_mmu.c
@@ -92,6 +92,22 @@ static void pad_len_spaces(struct seq_fi
 	seq_printf(m, "%*c", len, ' ');
 }
 
+static void seq_print_vma_name(struct seq_file *m, struct vm_area_struct *vma)
+{
+	struct mm_struct *mm = vma->vm_mm;
+	char anon_name[NAME_MAX + 1];
+	unsigned long addr;
+	int n;
+
+	n = access_remote_vm(mm, (unsigned long)vma_anon_name(vma),
+				anon_name, NAME_MAX, 0);
+	if (n > 0) {
+		seq_puts(m, "[anon:");
+		seq_write(m, anon_name, strnlen(anon_name, n));
+		seq_putc(m, ']');
+	}
+}
+
 #ifdef CONFIG_NUMA
 /*
  * These functions are for numa_maps but called in generic **maps seq_file
@@ -337,6 +353,12 @@ show_map_vma(struct seq_file *m, struct
 				pad_len_spaces(m, len);
 				seq_printf(m, "[stack:%d]", tid);
 			}
+			goto done;
+		}
+
+		if (vma_anon_name(vma)) {
+			pad_len_spaces(m, len);
+			seq_print_vma_name(m, vma);
 		}
 	}
 
include/linux/mm.h renamed from include/linux/mm.h~mm-add-a-field-to-store-names-for-private-anonymous-memory +4 -1
diff -puN include/linux/mm.h~mm-add-a-field-to-store-names-for-private-anonymous-memory include/linux/mm.h
@@ -1585,7 +1585,7 @@ extern int vma_adjust(struct vm_area_str
 extern struct vm_area_struct *vma_merge(struct mm_struct *,
 	struct vm_area_struct *prev, unsigned long addr, unsigned long end,
 	unsigned long vm_flags, struct anon_vma *, struct file *, pgoff_t,
-	struct mempolicy *);
+	struct mempolicy *, const char __user *);
 extern struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *);
 extern int split_vma(struct mm_struct *,
 	struct vm_area_struct *, unsigned long addr, int new_below);
@@ -1929,5 +1929,8 @@ void __init setup_nr_node_ids(void);
 static inline void setup_nr_node_ids(void) {}
 #endif
 
+int madvise_set_anon_name(unsigned long start, unsigned long len_in,
+				unsigned long name_addr);
+
 #endif /* __KERNEL__ */
 #endif /* _LINUX_MM_H */
include/linux/mm_types.h renamed from include/linux/mm_types.h~mm-add-a-field-to-store-names-for-private-anonymous-memory +15 -0
diff -puN include/linux/mm_types.h~mm-add-a-field-to-store-names-for-private-anonymous-memory include/linux/mm_types.h
@@ -263,6 +263,10 @@ struct vm_area_struct {
 	 * For areas with an address space and backing store,
 	 * linkage into the address_space->i_mmap interval tree, or
 	 * linkage of vma in the address_space->i_mmap_nonlinear list.
+	 *
+	 * For private anonymous mappings, a pointer to a null terminated string
+	 * in the user process containing the name given to the vma, or NULL
+	 * if unnamed.
 	 */
 	union {
 		struct {
@@ -270,6 +274,7 @@ struct vm_area_struct {
 			unsigned long rb_subtree_last;
 		} linear;
 		struct list_head nonlinear;
+		const char __user *anon_name;
 	} shared;
 
 	/*
@@ -463,4 +468,14 @@ static inline cpumask_t *mm_cpumask(stru
 	return mm->cpu_vm_mask_var;
 }
 
+
+/* Return the name for an anonymous mapping or NULL for a file-backed mapping */
+static inline const char __user *vma_anon_name(struct vm_area_struct *vma)
+{
+	if (vma->vm_file)
+		return NULL;
+
+	return vma->shared.anon_name;
+}
+
 #endif /* _LINUX_MM_TYPES_H */
include/uapi/linux/prctl.h renamed from include/uapi/linux/prctl.h~mm-add-a-field-to-store-names-for-private-anonymous-memory +3 -0
diff -puN include/uapi/linux/prctl.h~mm-add-a-field-to-store-names-for-private-anonymous-memory include/uapi/linux/prctl.h
@@ -149,4 +149,7 @@
 
 #define PR_GET_TID_ADDRESS	40
 
+#define PR_SET_VMA		0x53564d41
+# define PR_SET_VMA_ANON_NAME		0
+
 #endif /* _LINUX_PRCTL_H */
kernel/sys.c renamed from kernel/sys.c~mm-add-a-field-to-store-names-for-private-anonymous-memory +24 -0
diff -puN kernel/sys.c~mm-add-a-field-to-store-names-for-private-anonymous-memory kernel/sys.c
@@ -1836,6 +1836,27 @@ static int prctl_get_tid_address(struct
 }
 #endif
 
+static int prctl_set_vma(unsigned long opt, unsigned long addr,
+		unsigned long len, unsigned long arg)
+{
+	struct mm_struct *mm = current->mm;
+	int error;
+
+	down_write(&mm->mmap_sem);
+
+	switch (opt) {
+	case PR_SET_VMA_ANON_NAME:
+		error = madvise_set_anon_name(addr, len, arg);
+		break;
+	default:
+		error = -EINVAL;
+	}
+
+	up_write(&mm->mmap_sem);
+
+	return error;
+}
+
 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 		unsigned long, arg4, unsigned long, arg5)
 {
@@ -1999,6 +2020,9 @@ SYSCALL_DEFINE5(prctl, int, option, unsi
 		if (arg2 || arg3 || arg4 || arg5)
 			return -EINVAL;
 		return current->no_new_privs ? 1 : 0;
+	case PR_SET_VMA:
+		error = prctl_set_vma(arg2, arg3, arg4, arg5);
+		break;
 	default:
 		error = -EINVAL;
 		break;
mm/madvise.c renamed from mm/madvise.c~mm-add-a-field-to-store-names-for-private-anonymous-memory +51 -4

94 lines not on this page · raw message

mm/mempolicy.c renamed from mm/mempolicy.c~mm-add-a-field-to-store-names-for-private-anonymous-memory +1 -1
diff -puN mm/mempolicy.c~mm-add-a-field-to-store-names-for-private-anonymous-memory mm/mempolicy.c
@@ -767,7 +767,7 @@ static int mbind_range(struct mm_struct
 			((vmstart - vma->vm_start) >> PAGE_SHIFT);
 		prev = vma_merge(mm, prev, vmstart, vmend, vma->vm_flags,
 				  vma->anon_vma, vma->vm_file, pgoff,
-				  new_pol);
+				  new_pol, vma_anon_name(name));
 		if (prev) {
 			vma = prev;
 			next = vma->vm_next;
mm/mlock.c renamed from mm/mlock.c~mm-add-a-field-to-store-names-for-private-anonymous-memory +2 -1
diff -puN mm/mlock.c~mm-add-a-field-to-store-names-for-private-anonymous-memory mm/mlock.c
@@ -521,7 +521,8 @@ static int mlock_fixup(struct vm_area_st
 
 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
 	*prev = vma_merge(mm, *prev, start, end, newflags, vma->anon_vma,
-			  vma->vm_file, pgoff, vma_policy(vma));
+			  vma->vm_file, pgoff, vma_policy(vma),
+			  vma_anon_name(vma));
 	if (*prev) {
 		vma = *prev;
 		goto success;
mm/mmap.c renamed from mm/mmap.c~mm-add-a-field-to-store-names-for-private-anonymous-memory +26 -18

117 lines not on this page · raw message

mm/mprotect.c renamed from mm/mprotect.c~mm-add-a-field-to-store-names-for-private-anonymous-memory +2 -1
diff -puN mm/mprotect.c~mm-add-a-field-to-store-names-for-private-anonymous-memory mm/mprotect.c
@@ -274,7 +274,8 @@ mprotect_fixup(struct vm_area_struct *vm
 	 */
 	pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
 	*pprev = vma_merge(mm, *pprev, start, end, newflags,
-			vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
+			vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma),
+			vma_anon_name(vma));
 	if (*pprev) {
 		vma = *pprev;
 		goto success;

_

Patches currently in -mm which might be from [email protected] are

mm-rearrange-madvise-code-to-allow-for-reuse.patch mm-add-a-field-to-store-names-for-private-anonymous-memory.patch