Skip to content

Commit 65376df

Browse files
hnaztorvalds
authored andcommitted
proc: revert /proc/<pid>/maps [stack:TID] annotation
Commit b764375 ("procfs: mark thread stack correctly in proc/<pid>/maps") added [stack:TID] annotation to /proc/<pid>/maps. Finding the task of a stack VMA requires walking the entire thread list, turning this into quadratic behavior: a thousand threads means a thousand stacks, so the rendering of /proc/<pid>/maps needs to look at a million combinations. The cost is not in proportion to the usefulness as described in the patch. Drop the [stack:TID] annotation to make /proc/<pid>/maps (and /proc/<pid>/numa_maps) usable again for higher thread counts. The [stack] annotation inside /proc/<pid>/task/<tid>/maps is retained, as identifying the stack VMA there is an O(1) operation. Siddesh said: "The end users needed a way to identify thread stacks programmatically and there wasn't a way to do that. I'm afraid I no longer remember (or have access to the resources that would aid my memory since I changed employers) the details of their requirement. However, I did do this on my own time because I thought it was an interesting project for me and nobody really gave any feedback then as to its utility, so as far as I am concerned you could roll back the main thread maps information since the information is available in the thread-specific files" Signed-off-by: Johannes Weiner <hannes@cmpxchg.org> Cc: "Kirill A. Shutemov" <kirill@shutemov.name> Cc: Siddhesh Poyarekar <siddhesh.poyarekar@gmail.com> Cc: Shaohua Li <shli@fb.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5c2ff95 commit 65376df

5 files changed

Lines changed: 48 additions & 106 deletions

File tree

‎Documentation/filesystems/proc.txt‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ address perms offset dev inode pathname
356356
a7cb1000-a7cb2000 ---p 00000000 00:00 0
357357
a7cb2000-a7eb2000 rw-p 00000000 00:00 0
358358
a7eb2000-a7eb3000 ---p 00000000 00:00 0
359-
a7eb3000-a7ed5000 rw-p 00000000 00:00 0 [stack:1001]
359+
a7eb3000-a7ed5000 rw-p 00000000 00:00 0
360360
a7ed5000-a8008000 r-xp 00000000 03:00 4222 /lib/libc.so.6
361361
a8008000-a800a000 r--p 00133000 03:00 4222 /lib/libc.so.6
362362
a800a000-a800b000 rw-p 00135000 03:00 4222 /lib/libc.so.6
@@ -388,18 +388,15 @@ is not associated with a file:
388388

389389
[heap] = the heap of the program
390390
[stack] = the stack of the main process
391-
[stack:1001] = the stack of the thread with tid 1001
392391
[vdso] = the "virtual dynamic shared object",
393392
the kernel system call handler
394393

395394
or if empty, the mapping is anonymous.
396395

397396
The /proc/PID/task/TID/maps is a view of the virtual memory from the viewpoint
398397
of the individual tasks of a process. In this file you will see a mapping marked
399-
as [stack] if that task sees it as a stack. This is a key difference from the
400-
content of /proc/PID/maps, where you will see all mappings that are being used
401-
as stack by all of those tasks. Hence, for the example above, the task-level
402-
map, i.e. /proc/PID/task/TID/maps for thread 1001 will look like this:
398+
as [stack] if that task sees it as a stack. Hence, for the example above, the
399+
task-level map, i.e. /proc/PID/task/TID/maps for thread 1001 will look like this:
403400

404401
08048000-08049000 r-xp 00000000 03:00 8312 /opt/test
405402
08049000-0804a000 rw-p 00001000 03:00 8312 /opt/test

‎fs/proc/task_mmu.c‎

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -259,23 +259,29 @@ static int do_maps_open(struct inode *inode, struct file *file,
259259
sizeof(struct proc_maps_private));
260260
}
261261

262-
static pid_t pid_of_stack(struct proc_maps_private *priv,
263-
struct vm_area_struct *vma, bool is_pid)
262+
/*
263+
* Indicate if the VMA is a stack for the given task; for
264+
* /proc/PID/maps that is the stack of the main task.
265+
*/
266+
static int is_stack(struct proc_maps_private *priv,
267+
struct vm_area_struct *vma, int is_pid)
264268
{
265-
struct inode *inode = priv->inode;
266-
struct task_struct *task;
267-
pid_t ret = 0;
269+
int stack = 0;
270+
271+
if (is_pid) {
272+
stack = vma->vm_start <= vma->vm_mm->start_stack &&
273+
vma->vm_end >= vma->vm_mm->start_stack;
274+
} else {
275+
struct inode *inode = priv->inode;
276+
struct task_struct *task;
268277

269-
rcu_read_lock();
270-
task = pid_task(proc_pid(inode), PIDTYPE_PID);
271-
if (task) {
272-
task = task_of_stack(task, vma, is_pid);
278+
rcu_read_lock();
279+
task = pid_task(proc_pid(inode), PIDTYPE_PID);
273280
if (task)
274-
ret = task_pid_nr_ns(task, inode->i_sb->s_fs_info);
281+
stack = vma_is_stack_for_task(vma, task);
282+
rcu_read_unlock();
275283
}
276-
rcu_read_unlock();
277-
278-
return ret;
284+
return stack;
279285
}
280286

281287
static void
@@ -335,8 +341,6 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
335341

336342
name = arch_vma_name(vma);
337343
if (!name) {
338-
pid_t tid;
339-
340344
if (!mm) {
341345
name = "[vdso]";
342346
goto done;
@@ -348,21 +352,8 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
348352
goto done;
349353
}
350354

351-
tid = pid_of_stack(priv, vma, is_pid);
352-
if (tid != 0) {
353-
/*
354-
* Thread stack in /proc/PID/task/TID/maps or
355-
* the main process stack.
356-
*/
357-
if (!is_pid || (vma->vm_start <= mm->start_stack &&
358-
vma->vm_end >= mm->start_stack)) {
359-
name = "[stack]";
360-
} else {
361-
/* Thread stack in /proc/PID/maps */
362-
seq_pad(m, ' ');
363-
seq_printf(m, "[stack:%d]", tid);
364-
}
365-
}
355+
if (is_stack(priv, vma, is_pid))
356+
name = "[stack]";
366357
}
367358

368359
done:
@@ -1618,19 +1609,8 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
16181609
seq_file_path(m, file, "\n\t= ");
16191610
} else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
16201611
seq_puts(m, " heap");
1621-
} else {
1622-
pid_t tid = pid_of_stack(proc_priv, vma, is_pid);
1623-
if (tid != 0) {
1624-
/*
1625-
* Thread stack in /proc/PID/task/TID/maps or
1626-
* the main process stack.
1627-
*/
1628-
if (!is_pid || (vma->vm_start <= mm->start_stack &&
1629-
vma->vm_end >= mm->start_stack))
1630-
seq_puts(m, " stack");
1631-
else
1632-
seq_printf(m, " stack:%d", tid);
1633-
}
1612+
} else if (is_stack(proc_priv, vma, is_pid)) {
1613+
seq_puts(m, " stack");
16341614
}
16351615

16361616
if (is_vm_hugetlb_page(vma))

‎fs/proc/task_nommu.c‎

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,26 @@ unsigned long task_statm(struct mm_struct *mm,
123123
return size;
124124
}
125125

126-
static pid_t pid_of_stack(struct proc_maps_private *priv,
127-
struct vm_area_struct *vma, bool is_pid)
126+
static int is_stack(struct proc_maps_private *priv,
127+
struct vm_area_struct *vma, int is_pid)
128128
{
129-
struct inode *inode = priv->inode;
130-
struct task_struct *task;
131-
pid_t ret = 0;
132-
133-
rcu_read_lock();
134-
task = pid_task(proc_pid(inode), PIDTYPE_PID);
135-
if (task) {
136-
task = task_of_stack(task, vma, is_pid);
129+
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);
137141
if (task)
138-
ret = task_pid_nr_ns(task, inode->i_sb->s_fs_info);
142+
stack = vma_is_stack_for_task(vma, task);
143+
rcu_read_unlock();
139144
}
140-
rcu_read_unlock();
141-
142-
return ret;
145+
return stack;
143146
}
144147

145148
/*
@@ -181,21 +184,9 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
181184
if (file) {
182185
seq_pad(m, ' ');
183186
seq_file_path(m, file, "");
184-
} else if (mm) {
185-
pid_t tid = pid_of_stack(priv, vma, is_pid);
186-
187-
if (tid != 0) {
188-
seq_pad(m, ' ');
189-
/*
190-
* Thread stack in /proc/PID/task/TID/maps or
191-
* the main process stack.
192-
*/
193-
if (!is_pid || (vma->vm_start <= mm->start_stack &&
194-
vma->vm_end >= mm->start_stack))
195-
seq_printf(m, "[stack]");
196-
else
197-
seq_printf(m, "[stack:%d]", tid);
198-
}
187+
} else if (mm && is_stack(priv, vma, is_pid)) {
188+
seq_pad(m, ' ');
189+
seq_printf(m, "[stack]");
199190
}
200191

201192
seq_putc(m, '\n');

‎include/linux/mm.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,8 +1341,7 @@ static inline int stack_guard_page_end(struct vm_area_struct *vma,
13411341
!vma_growsup(vma->vm_next, addr);
13421342
}
13431343

1344-
extern struct task_struct *task_of_stack(struct task_struct *task,
1345-
struct vm_area_struct *vma, bool in_group);
1344+
int vma_is_stack_for_task(struct vm_area_struct *vma, struct task_struct *t);
13461345

13471346
extern unsigned long move_page_tables(struct vm_area_struct *vma,
13481347
unsigned long old_addr, struct vm_area_struct *new_vma,

‎mm/util.c‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -230,36 +230,11 @@ void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
230230
}
231231

232232
/* Check if the vma is being used as a stack by this task */
233-
static int vm_is_stack_for_task(struct task_struct *t,
234-
struct vm_area_struct *vma)
233+
int vma_is_stack_for_task(struct vm_area_struct *vma, struct task_struct *t)
235234
{
236235
return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t));
237236
}
238237

239-
/*
240-
* Check if the vma is being used as a stack.
241-
* If is_group is non-zero, check in the entire thread group or else
242-
* just check in the current task. Returns the task_struct of the task
243-
* that the vma is stack for. Must be called under rcu_read_lock().
244-
*/
245-
struct task_struct *task_of_stack(struct task_struct *task,
246-
struct vm_area_struct *vma, bool in_group)
247-
{
248-
if (vm_is_stack_for_task(task, vma))
249-
return task;
250-
251-
if (in_group) {
252-
struct task_struct *t;
253-
254-
for_each_thread(task, t) {
255-
if (vm_is_stack_for_task(t, vma))
256-
return t;
257-
}
258-
}
259-
260-
return NULL;
261-
}
262-
263238
#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
264239
void arch_pick_mmap_layout(struct mm_struct *mm)
265240
{

0 commit comments

Comments
 (0)