Skip to content

Commit ee99274

Browse files
ebiedermLinus Torvalds
authored andcommitted
proc: rewrite do_task_stat to correctly handle pid namespaces.
Currently (as pointed out by Oleg) do_task_stat has a race when calling task_pid_nr_ns with the task exiting. In addition do_task_stat is not currently displaying information in the context of the pid namespace that mounted the /proc filesystem. So "cut -d' ' -f 1 /proc/<pid>/stat" may not equal <pid>. This patch fixes the problem by converting to a single_open seq_file show method. Getting the pid namespace from the filesystem superblock instead of current, and simply using the the struct pid from the inode instead of attempting to get that same pid from the task. Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Cc: Oleg Nesterov <oleg@tv-sign.ru> Cc: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent be61408 commit ee99274

3 files changed

Lines changed: 20 additions & 17 deletions

File tree

‎fs/proc/array.c‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#include <linux/cpuset.h>
7878
#include <linux/rcupdate.h>
7979
#include <linux/delayacct.h>
80+
#include <linux/seq_file.h>
8081
#include <linux/pid_namespace.h>
8182

8283
#include <asm/pgtable.h>
@@ -390,14 +391,14 @@ static cputime_t task_gtime(struct task_struct *p)
390391
return p->gtime;
391392
}
392393

393-
static int do_task_stat(struct task_struct *task, char *buffer, int whole)
394+
static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
395+
struct pid *pid, struct task_struct *task, int whole)
394396
{
395397
unsigned long vsize, eip, esp, wchan = ~0UL;
396398
long priority, nice;
397399
int tty_pgrp = -1, tty_nr = 0;
398400
sigset_t sigign, sigcatch;
399401
char state;
400-
int res;
401402
pid_t ppid = 0, pgid = -1, sid = -1;
402403
int num_threads = 0;
403404
struct mm_struct *mm;
@@ -409,9 +410,6 @@ static int do_task_stat(struct task_struct *task, char *buffer, int whole)
409410
unsigned long rsslim = 0;
410411
char tcomm[sizeof(task->comm)];
411412
unsigned long flags;
412-
struct pid_namespace *ns;
413-
414-
ns = current->nsproxy->pid_ns;
415413

416414
state = *get_task_state(task);
417415
vsize = eip = esp = 0;
@@ -498,10 +496,10 @@ static int do_task_stat(struct task_struct *task, char *buffer, int whole)
498496
/* convert nsec -> ticks */
499497
start_time = nsec_to_clock_t(start_time);
500498

501-
res = sprintf(buffer, "%d (%s) %c %d %d %d %d %d %u %lu \
499+
seq_printf(m, "%d (%s) %c %d %d %d %d %d %u %lu \
502500
%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
503501
%lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld\n",
504-
task_pid_nr_ns(task, ns),
502+
pid_nr_ns(pid, ns),
505503
tcomm,
506504
state,
507505
ppid,
@@ -550,17 +548,19 @@ static int do_task_stat(struct task_struct *task, char *buffer, int whole)
550548
cputime_to_clock_t(cgtime));
551549
if (mm)
552550
mmput(mm);
553-
return res;
551+
return 0;
554552
}
555553

556-
int proc_tid_stat(struct task_struct *task, char *buffer)
554+
int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
555+
struct pid *pid, struct task_struct *task)
557556
{
558-
return do_task_stat(task, buffer, 0);
557+
return do_task_stat(m, ns, pid, task, 0);
559558
}
560559

561-
int proc_tgid_stat(struct task_struct *task, char *buffer)
560+
int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
561+
struct pid *pid, struct task_struct *task)
562562
{
563-
return do_task_stat(task, buffer, 1);
563+
return do_task_stat(m, ns, pid, task, 1);
564564
}
565565

566566
int proc_pid_statm(struct task_struct *task, char *buffer)

‎fs/proc/base.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,7 +2280,7 @@ static const struct pid_entry tgid_base_stuff[] = {
22802280
REG("sched", S_IRUGO|S_IWUSR, pid_sched),
22812281
#endif
22822282
INF("cmdline", S_IRUGO, pid_cmdline),
2283-
INF("stat", S_IRUGO, tgid_stat),
2283+
ONE("stat", S_IRUGO, tgid_stat),
22842284
INF("statm", S_IRUGO, pid_statm),
22852285
REG("maps", S_IRUGO, maps),
22862286
#ifdef CONFIG_NUMA
@@ -2611,7 +2611,7 @@ static const struct pid_entry tid_base_stuff[] = {
26112611
REG("sched", S_IRUGO|S_IWUSR, pid_sched),
26122612
#endif
26132613
INF("cmdline", S_IRUGO, pid_cmdline),
2614-
INF("stat", S_IRUGO, tid_stat),
2614+
ONE("stat", S_IRUGO, tid_stat),
26152615
INF("statm", S_IRUGO, pid_statm),
26162616
REG("maps", S_IRUGO, maps),
26172617
#ifdef CONFIG_NUMA

‎fs/proc/internal.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ extern int nommu_vma_show(struct seq_file *, struct vm_area_struct *);
4646

4747
extern int maps_protect;
4848

49-
extern void create_seq_entry(char *name, mode_t mode, const struct file_operations *f);
49+
extern void create_seq_entry(char *name, mode_t mode,
50+
const struct file_operations *f);
5051
extern int proc_exe_link(struct inode *, struct dentry **, struct vfsmount **);
51-
extern int proc_tid_stat(struct task_struct *, char *);
52-
extern int proc_tgid_stat(struct task_struct *, char *);
52+
extern int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
53+
struct pid *pid, struct task_struct *task);
54+
extern int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
55+
struct pid *pid, struct task_struct *task);
5356
extern int proc_pid_status(struct task_struct *, char *);
5457
extern int proc_pid_statm(struct task_struct *, char *);
5558
extern loff_t mem_lseek(struct file *file, loff_t offset, int orig);

0 commit comments

Comments
 (0)