Skip to content

Commit b18cb64

Browse files
amlutoIngo Molnar
authored andcommitted
fs/proc: Stop trying to report thread stacks
This reverts more of: b764375 ("procfs: mark thread stack correctly in proc/<pid>/maps") ... which was partially reverted by: 65376df ("proc: revert /proc/<pid>/maps [stack:TID] annotation") Originally, /proc/PID/task/TID/maps was the same as /proc/TID/maps. In current kernels, /proc/PID/maps (or /proc/TID/maps even for threads) shows "[stack]" for VMAs in the mm's stack address range. In contrast, /proc/PID/task/TID/maps uses KSTK_ESP to guess the target thread's stack's VMA. This is racy, probably returns garbage and, on arches with CONFIG_TASK_INFO_IN_THREAD=y, is also crash-prone: KSTK_ESP is not safe to use on tasks that aren't known to be running ordinary process-context kernel code. This patch removes the difference and just shows "[stack]" for VMAs in the mm's stack range. This is IMO much more sensible -- the actual "stack" address really is treated specially by the VM code, and the current thread stack isn't even well-defined for programs that frequently switch stacks on their own. Reported-by: Jann Horn <[email protected]> Signed-off-by: Andy Lutomirski <[email protected]> Acked-by: Thomas Gleixner <[email protected]> Cc: Al Viro <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Brian Gerst <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Kees Cook <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Linux API <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Tycho Andersen <[email protected]> Link: http://lkml.kernel.org/r/3e678474ec14e0a0ec34c611016753eea2e1b8ba.1475257877.git.luto@kernel.org Signed-off-by: Ingo Molnar <[email protected]>
1 parent 0a1eb2d commit b18cb64

File tree

3 files changed

+20
-63
lines changed

3 files changed

+20
-63
lines changed

Documentation/filesystems/proc.txt

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -395,32 +395,6 @@ is not associated with a file:
395395

396396
or if empty, the mapping is anonymous.
397397

398-
The /proc/PID/task/TID/maps is a view of the virtual memory from the viewpoint
399-
of the individual tasks of a process. In this file you will see a mapping marked
400-
as [stack] if that task sees it as a stack. Hence, for the example above, the
401-
task-level map, i.e. /proc/PID/task/TID/maps for thread 1001 will look like this:
402-
403-
08048000-08049000 r-xp 00000000 03:00 8312 /opt/test
404-
08049000-0804a000 rw-p 00001000 03:00 8312 /opt/test
405-
0804a000-0806b000 rw-p 00000000 00:00 0 [heap]
406-
a7cb1000-a7cb2000 ---p 00000000 00:00 0
407-
a7cb2000-a7eb2000 rw-p 00000000 00:00 0
408-
a7eb2000-a7eb3000 ---p 00000000 00:00 0
409-
a7eb3000-a7ed5000 rw-p 00000000 00:00 0 [stack]
410-
a7ed5000-a8008000 r-xp 00000000 03:00 4222 /lib/libc.so.6
411-
a8008000-a800a000 r--p 00133000 03:00 4222 /lib/libc.so.6
412-
a800a000-a800b000 rw-p 00135000 03:00 4222 /lib/libc.so.6
413-
a800b000-a800e000 rw-p 00000000 00:00 0
414-
a800e000-a8022000 r-xp 00000000 03:00 14462 /lib/libpthread.so.0
415-
a8022000-a8023000 r--p 00013000 03:00 14462 /lib/libpthread.so.0
416-
a8023000-a8024000 rw-p 00014000 03:00 14462 /lib/libpthread.so.0
417-
a8024000-a8027000 rw-p 00000000 00:00 0
418-
a8027000-a8043000 r-xp 00000000 03:00 8317 /lib/ld-linux.so.2
419-
a8043000-a8044000 r--p 0001b000 03:00 8317 /lib/ld-linux.so.2
420-
a8044000-a8045000 rw-p 0001c000 03:00 8317 /lib/ld-linux.so.2
421-
aff35000-aff4a000 rw-p 00000000 00:00 0
422-
ffffe000-fffff000 r-xp 00000000 00:00 0 [vdso]
423-
424398
The /proc/PID/smaps is an extension based on maps, showing the memory
425399
consumption for each of the process's mappings. For each of mappings there
426400
is a series of lines such as the following:

fs/proc/task_mmu.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -266,24 +266,15 @@ static int do_maps_open(struct inode *inode, struct file *file,
266266
* /proc/PID/maps that is the stack of the main task.
267267
*/
268268
static int is_stack(struct proc_maps_private *priv,
269-
struct vm_area_struct *vma, int is_pid)
269+
struct vm_area_struct *vma)
270270
{
271-
int stack = 0;
272-
273-
if (is_pid) {
274-
stack = vma->vm_start <= vma->vm_mm->start_stack &&
275-
vma->vm_end >= vma->vm_mm->start_stack;
276-
} else {
277-
struct inode *inode = priv->inode;
278-
struct task_struct *task;
279-
280-
rcu_read_lock();
281-
task = pid_task(proc_pid(inode), PIDTYPE_PID);
282-
if (task)
283-
stack = vma_is_stack_for_task(vma, task);
284-
rcu_read_unlock();
285-
}
286-
return stack;
271+
/*
272+
* We make no effort to guess what a given thread considers to be
273+
* its "stack". It's not even well-defined for programs written
274+
* languages like Go.
275+
*/
276+
return vma->vm_start <= vma->vm_mm->start_stack &&
277+
vma->vm_end >= vma->vm_mm->start_stack;
287278
}
288279

289280
static void
@@ -354,7 +345,7 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
354345
goto done;
355346
}
356347

357-
if (is_stack(priv, vma, is_pid))
348+
if (is_stack(priv, vma))
358349
name = "[stack]";
359350
}
360351

@@ -1669,7 +1660,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
16691660
seq_file_path(m, file, "\n\t= ");
16701661
} else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
16711662
seq_puts(m, " heap");
1672-
} else if (is_stack(proc_priv, vma, is_pid)) {
1663+
} else if (is_stack(proc_priv, vma)) {
16731664
seq_puts(m, " stack");
16741665
}
16751666

fs/proc/task_nommu.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -124,25 +124,17 @@ unsigned long task_statm(struct mm_struct *mm,
124124
}
125125

126126
static int is_stack(struct proc_maps_private *priv,
127-
struct vm_area_struct *vma, int is_pid)
127+
struct vm_area_struct *vma)
128128
{
129129
struct mm_struct *mm = vma->vm_mm;
130-
int stack = 0;
131-
132-
if (is_pid) {
133-
stack = vma->vm_start <= mm->start_stack &&
134-
vma->vm_end >= mm->start_stack;
135-
} else {
136-
struct inode *inode = priv->inode;
137-
struct task_struct *task;
138-
139-
rcu_read_lock();
140-
task = pid_task(proc_pid(inode), PIDTYPE_PID);
141-
if (task)
142-
stack = vma_is_stack_for_task(vma, task);
143-
rcu_read_unlock();
144-
}
145-
return stack;
130+
131+
/*
132+
* We make no effort to guess what a given thread considers to be
133+
* its "stack". It's not even well-defined for programs written
134+
* languages like Go.
135+
*/
136+
return vma->vm_start <= mm->start_stack &&
137+
vma->vm_end >= mm->start_stack;
146138
}
147139

148140
/*
@@ -184,7 +176,7 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
184176
if (file) {
185177
seq_pad(m, ' ');
186178
seq_file_path(m, file, "");
187-
} else if (mm && is_stack(priv, vma, is_pid)) {
179+
} else if (mm && is_stack(priv, vma)) {
188180
seq_pad(m, ' ');
189181
seq_printf(m, "[stack]");
190182
}

0 commit comments

Comments
 (0)