Skip to content

Commit 58cb654

Browse files
oleg-nesterovtorvalds
authored andcommitted
proc/maps: make vm_is_stack() logic namespace-friendly
- Rename vm_is_stack() to task_of_stack() and change it to return "struct task_struct *" rather than the global (and thus wrong in general) pid_t. - Add the new pid_of_stack() helper which calls task_of_stack() and uses the right namespace to report the correct pid_t. Unfortunately we need to define this helper twice, in task_mmu.c and in task_nommu.c. perhaps it makes sense to add fs/proc/util.c and move at least pid_of_stack/task_of_stack there to avoid the code duplication. - Change show_map_vma() and show_numa_map() to use the new helper. Signed-off-by: Oleg Nesterov <oleg@redhat.com> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Cyrill Gorcunov <gorcunov@openvz.org> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Greg Ungerer <gerg@uclinux.org> Cc: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2c03376 commit 58cb654

4 files changed

Lines changed: 51 additions & 22 deletions

File tree

‎fs/proc/task_mmu.c‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,31 @@ static int do_maps_open(struct inode *inode, struct file *file,
261261
sizeof(struct proc_maps_private));
262262
}
263263

264+
static pid_t pid_of_stack(struct proc_maps_private *priv,
265+
struct vm_area_struct *vma, bool is_pid)
266+
{
267+
struct inode *inode = priv->inode;
268+
struct task_struct *task;
269+
pid_t ret = 0;
270+
271+
rcu_read_lock();
272+
task = pid_task(proc_pid(inode), PIDTYPE_PID);
273+
if (task) {
274+
task = task_of_stack(task, vma, is_pid);
275+
if (task)
276+
ret = task_pid_nr_ns(task, inode->i_sb->s_fs_info);
277+
}
278+
rcu_read_unlock();
279+
280+
return ret;
281+
}
282+
264283
static void
265284
show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
266285
{
267286
struct mm_struct *mm = vma->vm_mm;
268287
struct file *file = vma->vm_file;
269288
struct proc_maps_private *priv = m->private;
270-
struct task_struct *task = priv->task;
271289
vm_flags_t flags = vma->vm_flags;
272290
unsigned long ino = 0;
273291
unsigned long long pgoff = 0;
@@ -332,8 +350,7 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
332350
goto done;
333351
}
334352

335-
tid = vm_is_stack(task, vma, is_pid);
336-
353+
tid = pid_of_stack(priv, vma, is_pid);
337354
if (tid != 0) {
338355
/*
339356
* Thread stack in /proc/PID/task/TID/maps or
@@ -1446,7 +1463,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
14461463
} else if (vma->vm_start <= mm->brk && vma->vm_end >= mm->start_brk) {
14471464
seq_puts(m, " heap");
14481465
} else {
1449-
pid_t tid = vm_is_stack(task, vma, is_pid);
1466+
pid_t tid = pid_of_stack(proc_priv, vma, is_pid);
14501467
if (tid != 0) {
14511468
/*
14521469
* Thread stack in /proc/PID/task/TID/maps or

‎fs/proc/task_nommu.c‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ 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)
128+
{
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);
137+
if (task)
138+
ret = task_pid_nr_ns(task, inode->i_sb->s_fs_info);
139+
}
140+
rcu_read_unlock();
141+
142+
return ret;
143+
}
144+
126145
/*
127146
* display a single VMA to a sequenced file
128147
*/
@@ -163,7 +182,7 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
163182
seq_pad(m, ' ');
164183
seq_path(m, &file->f_path, "");
165184
} else if (mm) {
166-
pid_t tid = vm_is_stack(priv->task, vma, is_pid);
185+
pid_t tid = pid_of_stack(priv, vma, is_pid);
167186

168187
if (tid != 0) {
169188
seq_pad(m, ' ');

‎include/linux/mm.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,8 +1247,8 @@ static inline int stack_guard_page_end(struct vm_area_struct *vma,
12471247
!vma_growsup(vma->vm_next, addr);
12481248
}
12491249

1250-
extern pid_t
1251-
vm_is_stack(struct task_struct *task, struct vm_area_struct *vma, int in_group);
1250+
extern struct task_struct *task_of_stack(struct task_struct *task,
1251+
struct vm_area_struct *vma, bool in_group);
12521252

12531253
extern unsigned long move_page_tables(struct vm_area_struct *vma,
12541254
unsigned long old_addr, struct vm_area_struct *new_vma,

‎mm/util.c‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,32 +170,25 @@ static int vm_is_stack_for_task(struct task_struct *t,
170170
/*
171171
* Check if the vma is being used as a stack.
172172
* If is_group is non-zero, check in the entire thread group or else
173-
* just check in the current task. Returns the pid of the task that
174-
* the vma is stack for.
173+
* just check in the current task. Returns the task_struct of the task
174+
* that the vma is stack for. Must be called under rcu_read_lock().
175175
*/
176-
pid_t vm_is_stack(struct task_struct *task,
177-
struct vm_area_struct *vma, int in_group)
176+
struct task_struct *task_of_stack(struct task_struct *task,
177+
struct vm_area_struct *vma, bool in_group)
178178
{
179-
pid_t ret = 0;
180-
181179
if (vm_is_stack_for_task(task, vma))
182-
return task->pid;
180+
return task;
183181

184182
if (in_group) {
185183
struct task_struct *t;
186184

187-
rcu_read_lock();
188185
for_each_thread(task, t) {
189-
if (vm_is_stack_for_task(t, vma)) {
190-
ret = t->pid;
191-
goto done;
192-
}
186+
if (vm_is_stack_for_task(t, vma))
187+
return t;
193188
}
194-
done:
195-
rcu_read_unlock();
196189
}
197190

198-
return ret;
191+
return NULL;
199192
}
200193

201194
#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)

0 commit comments

Comments
 (0)