linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Mark thread stack correctly in proc/<pid>/maps
@ 2012-01-14 12:35 Siddhesh Poyarekar
  2012-01-16 11:28 ` Jamie Lokier
  0 siblings, 1 reply; 42+ messages in thread
From: Siddhesh Poyarekar @ 2012-01-14 12:35 UTC (permalink / raw)
  To: linux-mm
  Cc: linux-kernel, Alexander Viro, linux-fsdevel, Michael Kerrisk,
	linux-man, Siddhesh Poyarekar

Memory mmaped by glibc for a thread stack currently shows up as a simple
anonymous map, which makes it difficult to differentiate between memory
usage of the thread on stack and other dynamic allocation. Since glibc
already uses MAP_STACK to request this mapping, the attached patch
uses this flag to add additional VM_STACK_FLAGS to the resulting vma
so that the mapping is treated as a stack and not any regular
anonymous mapping. Also, one may use vm_flags to decide if a vma is a
stack.

There is an additional complication with posix threads where the stack
guard for a thread stack may be larger than a page, unlike the case
for process stack where the stack guard is a page long. glibc
implements these guards by calling mprotect on the beginning page(s)
to remove all permissions. I have used this to remove vmas that have
the thread stack guard, from the /proc/maps output.

If accepted, this should also reflect in the man page for mmap since
MAP_STACK will no longer be a noop.

Signed-off-by: Siddhesh Poyarekar <siddhesh.poyarekar@gmail.com>
---
 fs/proc/task_mmu.c |    8 +++++---
 include/linux/mm.h |   17 +++++++++++++++++
 mm/mmap.c          |    3 +++
 3 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index e418c5a..98b5275 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -227,7 +227,10 @@ static void show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
 		pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
 	}
 
-	/* We don't show the stack guard page in /proc/maps */
+	/* We don't show the stack guard pages in /proc/maps */
+	if (thread_stack_guard(vma))
+		return;
+
 	start = vma->vm_start;
 	if (stack_guard_page_start(vma, start))
 		start += PAGE_SIZE;
@@ -259,8 +262,7 @@ static void show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
 				if (vma->vm_start <= mm->brk &&
 						vma->vm_end >= mm->start_brk) {
 					name = "[heap]";
-				} else if (vma->vm_start <= mm->start_stack &&
-					   vma->vm_end >= mm->start_stack) {
+				} else if (vma_is_stack(vma)) {
 					name = "[stack]";
 				}
 			} else {
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 17b27cd..9871e10 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1018,6 +1018,23 @@ static inline int vma_growsdown(struct vm_area_struct *vma, unsigned long addr)
 	return vma && (vma->vm_end == addr) && (vma->vm_flags & VM_GROWSDOWN);
 }
 
+static inline int vma_is_stack(struct vm_area_struct *vma)
+{
+	return vma && (vma->vm_flags & (VM_GROWSUP | VM_GROWSDOWN));
+}
+
+/*
+ * POSIX thread stack guards may be more than a page long and access to it
+ * should return an error (possibly a SIGSEGV). The glibc implementation does
+ * an mprotect(..., ..., PROT_NONE), so our guard vma has no permissions.
+ */
+static inline int thread_stack_guard(struct vm_area_struct *vma)
+{
+	return vma_is_stack(vma) &&
+		((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC | VM_MAYSHARE)) == 0) &&
+		vma_is_stack((vma->vm_flags & VM_GROWSDOWN)?vma->vm_next:vma->vm_prev);
+}
+
 static inline int stack_guard_page_start(struct vm_area_struct *vma,
 					     unsigned long addr)
 {
diff --git a/mm/mmap.c b/mm/mmap.c
index 3f758c7..2f9f540 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -992,6 +992,9 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
 	vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
 			mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
 
+	if (flags & MAP_STACK)
+		vm_flags |= VM_STACK_FLAGS;
+
 	if (flags & MAP_LOCKED)
 		if (!can_do_mlock())
 			return -EPERM;
-- 
1.7.7.4


^ permalink raw reply related	[flat|nested] 42+ messages in thread
* Re: + procfs-mark-thread-stack-correctly-in-proc-pid-maps.patch added to -mm tree
@ 2012-02-28 17:04 Oleg Nesterov
  2012-02-28 17:18 ` Siddhesh Poyarekar
  0 siblings, 1 reply; 42+ messages in thread
From: Oleg Nesterov @ 2012-02-28 17:04 UTC (permalink / raw)
  To: Siddhesh Poyarekar, KOSAKI Motohiro, Alexander Viro,
	Jamie Lokier, Mike Frysinger, Alexey Dobriyan, Matt Mackall,
	Andrew Morton
  Cc: linux-kernel

> +int vm_is_stack(struct task_struct *task,
> +			      struct vm_area_struct *vma, int in_group)
> +{
> +	if (vm_is_stack_for_task(task, vma))
> +		return 1;
> +
> +	if (in_group) {
> +		struct task_struct *t = task;
> +		while_each_thread(task, t) {
> +			if (vm_is_stack_for_task(t, vma))
> +				return 1;
> +		}
> +	}
> +
> +	return 0;
> +}

This is obviously wrong, while_each_thread() is not safe without
tasklist or siglock or rcu.

Oleg.


^ permalink raw reply	[flat|nested] 42+ messages in thread

end of thread, other threads:[~2012-03-04 20:04 UTC | newest]

Thread overview: 42+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-14 12:35 [PATCH] Mark thread stack correctly in proc/<pid>/maps Siddhesh Poyarekar
2012-01-16 11:28 ` Jamie Lokier
2012-01-16 13:08   ` Siddhesh Poyarekar
2012-01-16 16:31     ` Jamie Lokier
2012-01-16 17:01       ` Siddhesh Poyarekar
2012-01-17  4:54       ` Siddhesh Poyarekar
2012-02-02  6:24         ` [RESEND][PATCH] " Siddhesh Poyarekar
2012-02-02 21:40           ` KOSAKI Motohiro
2012-02-03  7:09             ` Siddhesh Poyarekar
2012-02-03  8:01               ` KOSAKI Motohiro
2012-02-03  9:49                 ` Siddhesh Poyarekar
2012-02-03 10:29                 ` Mike Frysinger
2012-02-03 18:34                 ` Siddhesh Poyarekar
2012-02-08  4:00                   ` Siddhesh Poyarekar
2012-02-08 17:57                     ` KOSAKI Motohiro
2012-02-11 10:19                       ` Siddhesh Poyarekar
2012-02-11 15:03                       ` [PATCH] " Siddhesh Poyarekar
2012-02-21  4:24                         ` [RESEND][PATCH] " Siddhesh Poyarekar
2012-02-22 23:00                           ` Andrew Morton
2012-02-23  4:03                             ` [PATCH] " Siddhesh Poyarekar
2012-02-23 20:22                               ` Andrew Morton
2012-02-24 13:05                                 ` Siddhesh Poyarekar
2012-02-26 16:17                                   ` [PATCH] x86_64: Record stack pointer before task execution begins Siddhesh Poyarekar
2012-02-27  6:17                                     ` [tip:x86/process] " tip-bot for Siddhesh Poyarekar
2012-02-23 23:47                               ` [PATCH] Mark thread stack correctly in proc/<pid>/maps Mike Frysinger
2012-02-24  5:47                                 ` Siddhesh Poyarekar
2012-02-24 16:12                                   ` Mike Frysinger
2012-02-24 18:23                                     ` Siddhesh Poyarekar
2012-03-01  5:20                                     ` [PATCH 1/2] Take rcu read lock when iterating through thread group Siddhesh Poyarekar
2012-03-01  5:20                                       ` [PATCH 2/2] procfs: Mark stack vma with pid of the owning task Siddhesh Poyarekar
2012-03-01 23:17                                         ` Andrew Morton
2012-03-01 16:51                                       ` [PATCH 1/2] Take rcu read lock when iterating through thread group Oleg Nesterov
2012-03-01 23:21                                       ` Andrew Morton
2012-03-04 20:04                                         ` Siddhesh Poyarekar
2012-02-23 23:17                         ` [PATCH] Mark thread stack correctly in proc/<pid>/maps KOSAKI Motohiro
2012-02-24  0:49                           ` KOSAKI Motohiro
2012-02-24  5:29                           ` Siddhesh Poyarekar
2012-02-24 16:14                             ` KOSAKI Motohiro
2012-02-24 18:58                               ` Siddhesh Poyarekar
2012-02-28 17:04 + procfs-mark-thread-stack-correctly-in-proc-pid-maps.patch added to -mm tree Oleg Nesterov
2012-02-28 17:18 ` Siddhesh Poyarekar
2012-02-28 17:40   ` Oleg Nesterov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).