procfs: provide stack information for threads
[linux-2.6.git] / fs / proc / array.c
1 /*
2  *  linux/fs/proc/array.c
3  *
4  *  Copyright (C) 1992  by Linus Torvalds
5  *  based on ideas by Darren Senn
6  *
7  * Fixes:
8  * Michael. K. Johnson: stat,statm extensions.
9  *                      <johnsonm@stolaf.edu>
10  *
11  * Pauline Middelink :  Made cmdline,envline only break at '\0's, to
12  *                      make sure SET_PROCTITLE works. Also removed
13  *                      bad '!' which forced address recalculation for
14  *                      EVERY character on the current page.
15  *                      <middelin@polyware.iaf.nl>
16  *
17  * Danny ter Haar    :  added cpuinfo
18  *                      <dth@cistron.nl>
19  *
20  * Alessandro Rubini :  profile extension.
21  *                      <rubini@ipvvis.unipv.it>
22  *
23  * Jeff Tranter      :  added BogoMips field to cpuinfo
24  *                      <Jeff_Tranter@Mitel.COM>
25  *
26  * Bruno Haible      :  remove 4K limit for the maps file
27  *                      <haible@ma2s2.mathematik.uni-karlsruhe.de>
28  *
29  * Yves Arrouye      :  remove removal of trailing spaces in get_array.
30  *                      <Yves.Arrouye@marin.fdn.fr>
31  *
32  * Jerome Forissier  :  added per-CPU time information to /proc/stat
33  *                      and /proc/<pid>/cpu extension
34  *                      <forissier@isia.cma.fr>
35  *                      - Incorporation and non-SMP safe operation
36  *                      of forissier patch in 2.1.78 by
37  *                      Hans Marcus <crowbar@concepts.nl>
38  *
39  * aeb@cwi.nl        :  /proc/partitions
40  *
41  *
42  * Alan Cox          :  security fixes.
43  *                      <alan@lxorguk.ukuu.org.uk>
44  *
45  * Al Viro           :  safe handling of mm_struct
46  *
47  * Gerhard Wichert   :  added BIGMEM support
48  * Siemens AG           <Gerhard.Wichert@pdb.siemens.de>
49  *
50  * Al Viro & Jeff Garzik :  moved most of the thing into base.c and
51  *                       :  proc_misc.c. The rest may eventually go into
52  *                       :  base.c too.
53  */
54
55 #include <linux/types.h>
56 #include <linux/errno.h>
57 #include <linux/time.h>
58 #include <linux/kernel.h>
59 #include <linux/kernel_stat.h>
60 #include <linux/tty.h>
61 #include <linux/string.h>
62 #include <linux/mman.h>
63 #include <linux/proc_fs.h>
64 #include <linux/ioport.h>
65 #include <linux/uaccess.h>
66 #include <linux/io.h>
67 #include <linux/mm.h>
68 #include <linux/hugetlb.h>
69 #include <linux/pagemap.h>
70 #include <linux/swap.h>
71 #include <linux/slab.h>
72 #include <linux/smp.h>
73 #include <linux/signal.h>
74 #include <linux/highmem.h>
75 #include <linux/file.h>
76 #include <linux/fdtable.h>
77 #include <linux/times.h>
78 #include <linux/cpuset.h>
79 #include <linux/rcupdate.h>
80 #include <linux/delayacct.h>
81 #include <linux/seq_file.h>
82 #include <linux/pid_namespace.h>
83 #include <linux/ptrace.h>
84 #include <linux/tracehook.h>
85 #include <linux/swapops.h>
86
87 #include <asm/pgtable.h>
88 #include <asm/processor.h>
89 #include "internal.h"
90
91 static inline void task_name(struct seq_file *m, struct task_struct *p)
92 {
93         int i;
94         char *buf, *end;
95         char *name;
96         char tcomm[sizeof(p->comm)];
97
98         get_task_comm(tcomm, p);
99
100         seq_printf(m, "Name:\t");
101         end = m->buf + m->size;
102         buf = m->buf + m->count;
103         name = tcomm;
104         i = sizeof(tcomm);
105         while (i && (buf < end)) {
106                 unsigned char c = *name;
107                 name++;
108                 i--;
109                 *buf = c;
110                 if (!c)
111                         break;
112                 if (c == '\\') {
113                         buf++;
114                         if (buf < end)
115                                 *buf++ = c;
116                         continue;
117                 }
118                 if (c == '\n') {
119                         *buf++ = '\\';
120                         if (buf < end)
121                                 *buf++ = 'n';
122                         continue;
123                 }
124                 buf++;
125         }
126         m->count = buf - m->buf;
127         seq_printf(m, "\n");
128 }
129
130 /*
131  * The task state array is a strange "bitmap" of
132  * reasons to sleep. Thus "running" is zero, and
133  * you can test for combinations of others with
134  * simple bit tests.
135  */
136 static const char *task_state_array[] = {
137         "R (running)",          /*  0 */
138         "S (sleeping)",         /*  1 */
139         "D (disk sleep)",       /*  2 */
140         "T (stopped)",          /*  4 */
141         "T (tracing stop)",     /*  8 */
142         "Z (zombie)",           /* 16 */
143         "X (dead)"              /* 32 */
144 };
145
146 static inline const char *get_task_state(struct task_struct *tsk)
147 {
148         unsigned int state = (tsk->state & TASK_REPORT) | tsk->exit_state;
149         const char **p = &task_state_array[0];
150
151         while (state) {
152                 p++;
153                 state >>= 1;
154         }
155         return *p;
156 }
157
158 static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
159                                 struct pid *pid, struct task_struct *p)
160 {
161         struct group_info *group_info;
162         int g;
163         struct fdtable *fdt = NULL;
164         const struct cred *cred;
165         pid_t ppid, tpid;
166
167         rcu_read_lock();
168         ppid = pid_alive(p) ?
169                 task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
170         tpid = 0;
171         if (pid_alive(p)) {
172                 struct task_struct *tracer = tracehook_tracer_task(p);
173                 if (tracer)
174                         tpid = task_pid_nr_ns(tracer, ns);
175         }
176         cred = get_cred((struct cred *) __task_cred(p));
177         seq_printf(m,
178                 "State:\t%s\n"
179                 "Tgid:\t%d\n"
180                 "Pid:\t%d\n"
181                 "PPid:\t%d\n"
182                 "TracerPid:\t%d\n"
183                 "Uid:\t%d\t%d\t%d\t%d\n"
184                 "Gid:\t%d\t%d\t%d\t%d\n",
185                 get_task_state(p),
186                 task_tgid_nr_ns(p, ns),
187                 pid_nr_ns(pid, ns),
188                 ppid, tpid,
189                 cred->uid, cred->euid, cred->suid, cred->fsuid,
190                 cred->gid, cred->egid, cred->sgid, cred->fsgid);
191
192         task_lock(p);
193         if (p->files)
194                 fdt = files_fdtable(p->files);
195         seq_printf(m,
196                 "FDSize:\t%d\n"
197                 "Groups:\t",
198                 fdt ? fdt->max_fds : 0);
199         rcu_read_unlock();
200
201         group_info = cred->group_info;
202         task_unlock(p);
203
204         for (g = 0; g < min(group_info->ngroups, NGROUPS_SMALL); g++)
205                 seq_printf(m, "%d ", GROUP_AT(group_info, g));
206         put_cred(cred);
207
208         seq_printf(m, "\n");
209 }
210
211 static void render_sigset_t(struct seq_file *m, const char *header,
212                                 sigset_t *set)
213 {
214         int i;
215
216         seq_printf(m, "%s", header);
217
218         i = _NSIG;
219         do {
220                 int x = 0;
221
222                 i -= 4;
223                 if (sigismember(set, i+1)) x |= 1;
224                 if (sigismember(set, i+2)) x |= 2;
225                 if (sigismember(set, i+3)) x |= 4;
226                 if (sigismember(set, i+4)) x |= 8;
227                 seq_printf(m, "%x", x);
228         } while (i >= 4);
229
230         seq_printf(m, "\n");
231 }
232
233 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
234                                     sigset_t *catch)
235 {
236         struct k_sigaction *k;
237         int i;
238
239         k = p->sighand->action;
240         for (i = 1; i <= _NSIG; ++i, ++k) {
241                 if (k->sa.sa_handler == SIG_IGN)
242                         sigaddset(ign, i);
243                 else if (k->sa.sa_handler != SIG_DFL)
244                         sigaddset(catch, i);
245         }
246 }
247
248 static inline void task_sig(struct seq_file *m, struct task_struct *p)
249 {
250         unsigned long flags;
251         sigset_t pending, shpending, blocked, ignored, caught;
252         int num_threads = 0;
253         unsigned long qsize = 0;
254         unsigned long qlim = 0;
255
256         sigemptyset(&pending);
257         sigemptyset(&shpending);
258         sigemptyset(&blocked);
259         sigemptyset(&ignored);
260         sigemptyset(&caught);
261
262         if (lock_task_sighand(p, &flags)) {
263                 pending = p->pending.signal;
264                 shpending = p->signal->shared_pending.signal;
265                 blocked = p->blocked;
266                 collect_sigign_sigcatch(p, &ignored, &caught);
267                 num_threads = atomic_read(&p->signal->count);
268                 qsize = atomic_read(&__task_cred(p)->user->sigpending);
269                 qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
270                 unlock_task_sighand(p, &flags);
271         }
272
273         seq_printf(m, "Threads:\t%d\n", num_threads);
274         seq_printf(m, "SigQ:\t%lu/%lu\n", qsize, qlim);
275
276         /* render them all */
277         render_sigset_t(m, "SigPnd:\t", &pending);
278         render_sigset_t(m, "ShdPnd:\t", &shpending);
279         render_sigset_t(m, "SigBlk:\t", &blocked);
280         render_sigset_t(m, "SigIgn:\t", &ignored);
281         render_sigset_t(m, "SigCgt:\t", &caught);
282 }
283
284 static void render_cap_t(struct seq_file *m, const char *header,
285                         kernel_cap_t *a)
286 {
287         unsigned __capi;
288
289         seq_printf(m, "%s", header);
290         CAP_FOR_EACH_U32(__capi) {
291                 seq_printf(m, "%08x",
292                            a->cap[(_KERNEL_CAPABILITY_U32S-1) - __capi]);
293         }
294         seq_printf(m, "\n");
295 }
296
297 static inline void task_cap(struct seq_file *m, struct task_struct *p)
298 {
299         const struct cred *cred;
300         kernel_cap_t cap_inheritable, cap_permitted, cap_effective, cap_bset;
301
302         rcu_read_lock();
303         cred = __task_cred(p);
304         cap_inheritable = cred->cap_inheritable;
305         cap_permitted   = cred->cap_permitted;
306         cap_effective   = cred->cap_effective;
307         cap_bset        = cred->cap_bset;
308         rcu_read_unlock();
309
310         render_cap_t(m, "CapInh:\t", &cap_inheritable);
311         render_cap_t(m, "CapPrm:\t", &cap_permitted);
312         render_cap_t(m, "CapEff:\t", &cap_effective);
313         render_cap_t(m, "CapBnd:\t", &cap_bset);
314 }
315
316 static inline void task_context_switch_counts(struct seq_file *m,
317                                                 struct task_struct *p)
318 {
319         seq_printf(m,   "voluntary_ctxt_switches:\t%lu\n"
320                         "nonvoluntary_ctxt_switches:\t%lu\n",
321                         p->nvcsw,
322                         p->nivcsw);
323 }
324
325 struct stack_stats {
326         struct vm_area_struct *vma;
327         unsigned long   startpage;
328         unsigned long   usage;
329 };
330
331 static int stack_usage_pte_range(pmd_t *pmd, unsigned long addr,
332                                 unsigned long end, struct mm_walk *walk)
333 {
334         struct stack_stats *ss = walk->private;
335         struct vm_area_struct *vma = ss->vma;
336         pte_t *pte, ptent;
337         spinlock_t *ptl;
338         int ret = 0;
339
340         pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
341         for (; addr != end; pte++, addr += PAGE_SIZE) {
342                 ptent = *pte;
343
344 #ifdef CONFIG_STACK_GROWSUP
345                 if (pte_present(ptent) || is_swap_pte(ptent))
346                         ss->usage = addr - ss->startpage + PAGE_SIZE;
347 #else
348                 if (pte_present(ptent) || is_swap_pte(ptent)) {
349                         ss->usage = ss->startpage - addr + PAGE_SIZE;
350                         pte++;
351                         ret = 1;
352                         break;
353                 }
354 #endif
355         }
356         pte_unmap_unlock(pte - 1, ptl);
357         cond_resched();
358         return ret;
359 }
360
361 static inline unsigned long get_stack_usage_in_bytes(struct vm_area_struct *vma,
362                                 struct task_struct *task)
363 {
364         struct stack_stats ss;
365         struct mm_walk stack_walk = {
366                 .pmd_entry = stack_usage_pte_range,
367                 .mm = vma->vm_mm,
368                 .private = &ss,
369         };
370
371         if (!vma->vm_mm || is_vm_hugetlb_page(vma))
372                 return 0;
373
374         ss.vma = vma;
375         ss.startpage = task->stack_start & PAGE_MASK;
376         ss.usage = 0;
377
378 #ifdef CONFIG_STACK_GROWSUP
379         walk_page_range(KSTK_ESP(task) & PAGE_MASK, vma->vm_end,
380                 &stack_walk);
381 #else
382         walk_page_range(vma->vm_start, (KSTK_ESP(task) & PAGE_MASK) + PAGE_SIZE,
383                 &stack_walk);
384 #endif
385         return ss.usage;
386 }
387
388 static inline void task_show_stack_usage(struct seq_file *m,
389                                                 struct task_struct *task)
390 {
391         struct vm_area_struct   *vma;
392         struct mm_struct        *mm = get_task_mm(task);
393
394         if (mm) {
395                 down_read(&mm->mmap_sem);
396                 vma = find_vma(mm, task->stack_start);
397                 if (vma)
398                         seq_printf(m, "Stack usage:\t%lu kB\n",
399                                 get_stack_usage_in_bytes(vma, task) >> 10);
400
401                 up_read(&mm->mmap_sem);
402                 mmput(mm);
403         }
404 }
405
406 int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
407                         struct pid *pid, struct task_struct *task)
408 {
409         struct mm_struct *mm = get_task_mm(task);
410
411         task_name(m, task);
412         task_state(m, ns, pid, task);
413
414         if (mm) {
415                 task_mem(m, mm);
416                 mmput(mm);
417         }
418         task_sig(m, task);
419         task_cap(m, task);
420         cpuset_task_status_allowed(m, task);
421 #if defined(CONFIG_S390)
422         task_show_regs(m, task);
423 #endif
424         task_context_switch_counts(m, task);
425         task_show_stack_usage(m, task);
426         return 0;
427 }
428
429 static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
430                         struct pid *pid, struct task_struct *task, int whole)
431 {
432         unsigned long vsize, eip, esp, wchan = ~0UL;
433         long priority, nice;
434         int tty_pgrp = -1, tty_nr = 0;
435         sigset_t sigign, sigcatch;
436         char state;
437         pid_t ppid = 0, pgid = -1, sid = -1;
438         int num_threads = 0;
439         int permitted;
440         struct mm_struct *mm;
441         unsigned long long start_time;
442         unsigned long cmin_flt = 0, cmaj_flt = 0;
443         unsigned long  min_flt = 0,  maj_flt = 0;
444         cputime_t cutime, cstime, utime, stime;
445         cputime_t cgtime, gtime;
446         unsigned long rsslim = 0;
447         char tcomm[sizeof(task->comm)];
448         unsigned long flags;
449
450         state = *get_task_state(task);
451         vsize = eip = esp = 0;
452         permitted = ptrace_may_access(task, PTRACE_MODE_READ);
453         mm = get_task_mm(task);
454         if (mm) {
455                 vsize = task_vsize(mm);
456                 if (permitted) {
457                         eip = KSTK_EIP(task);
458                         esp = KSTK_ESP(task);
459                 }
460         }
461
462         get_task_comm(tcomm, task);
463
464         sigemptyset(&sigign);
465         sigemptyset(&sigcatch);
466         cutime = cstime = utime = stime = cputime_zero;
467         cgtime = gtime = cputime_zero;
468
469         if (lock_task_sighand(task, &flags)) {
470                 struct signal_struct *sig = task->signal;
471
472                 if (sig->tty) {
473                         struct pid *pgrp = tty_get_pgrp(sig->tty);
474                         tty_pgrp = pid_nr_ns(pgrp, ns);
475                         put_pid(pgrp);
476                         tty_nr = new_encode_dev(tty_devnum(sig->tty));
477                 }
478
479                 num_threads = atomic_read(&sig->count);
480                 collect_sigign_sigcatch(task, &sigign, &sigcatch);
481
482                 cmin_flt = sig->cmin_flt;
483                 cmaj_flt = sig->cmaj_flt;
484                 cutime = sig->cutime;
485                 cstime = sig->cstime;
486                 cgtime = sig->cgtime;
487                 rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
488
489                 /* add up live thread stats at the group level */
490                 if (whole) {
491                         struct task_cputime cputime;
492                         struct task_struct *t = task;
493                         do {
494                                 min_flt += t->min_flt;
495                                 maj_flt += t->maj_flt;
496                                 gtime = cputime_add(gtime, task_gtime(t));
497                                 t = next_thread(t);
498                         } while (t != task);
499
500                         min_flt += sig->min_flt;
501                         maj_flt += sig->maj_flt;
502                         thread_group_cputime(task, &cputime);
503                         utime = cputime.utime;
504                         stime = cputime.stime;
505                         gtime = cputime_add(gtime, sig->gtime);
506                 }
507
508                 sid = task_session_nr_ns(task, ns);
509                 ppid = task_tgid_nr_ns(task->real_parent, ns);
510                 pgid = task_pgrp_nr_ns(task, ns);
511
512                 unlock_task_sighand(task, &flags);
513         }
514
515         if (permitted && (!whole || num_threads < 2))
516                 wchan = get_wchan(task);
517         if (!whole) {
518                 min_flt = task->min_flt;
519                 maj_flt = task->maj_flt;
520                 utime = task_utime(task);
521                 stime = task_stime(task);
522                 gtime = task_gtime(task);
523         }
524
525         /* scale priority and nice values from timeslices to -20..20 */
526         /* to make it look like a "normal" Unix priority/nice value  */
527         priority = task_prio(task);
528         nice = task_nice(task);
529
530         /* Temporary variable needed for gcc-2.96 */
531         /* convert timespec -> nsec*/
532         start_time =
533                 (unsigned long long)task->real_start_time.tv_sec * NSEC_PER_SEC
534                                 + task->real_start_time.tv_nsec;
535         /* convert nsec -> ticks */
536         start_time = nsec_to_clock_t(start_time);
537
538         seq_printf(m, "%d (%s) %c %d %d %d %d %d %u %lu \
539 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
540 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld\n",
541                 pid_nr_ns(pid, ns),
542                 tcomm,
543                 state,
544                 ppid,
545                 pgid,
546                 sid,
547                 tty_nr,
548                 tty_pgrp,
549                 task->flags,
550                 min_flt,
551                 cmin_flt,
552                 maj_flt,
553                 cmaj_flt,
554                 cputime_to_clock_t(utime),
555                 cputime_to_clock_t(stime),
556                 cputime_to_clock_t(cutime),
557                 cputime_to_clock_t(cstime),
558                 priority,
559                 nice,
560                 num_threads,
561                 start_time,
562                 vsize,
563                 mm ? get_mm_rss(mm) : 0,
564                 rsslim,
565                 mm ? mm->start_code : 0,
566                 mm ? mm->end_code : 0,
567                 (permitted) ? task->stack_start : 0,
568                 esp,
569                 eip,
570                 /* The signal information here is obsolete.
571                  * It must be decimal for Linux 2.0 compatibility.
572                  * Use /proc/#/status for real-time signals.
573                  */
574                 task->pending.signal.sig[0] & 0x7fffffffUL,
575                 task->blocked.sig[0] & 0x7fffffffUL,
576                 sigign      .sig[0] & 0x7fffffffUL,
577                 sigcatch    .sig[0] & 0x7fffffffUL,
578                 wchan,
579                 0UL,
580                 0UL,
581                 task->exit_signal,
582                 task_cpu(task),
583                 task->rt_priority,
584                 task->policy,
585                 (unsigned long long)delayacct_blkio_ticks(task),
586                 cputime_to_clock_t(gtime),
587                 cputime_to_clock_t(cgtime));
588         if (mm)
589                 mmput(mm);
590         return 0;
591 }
592
593 int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
594                         struct pid *pid, struct task_struct *task)
595 {
596         return do_task_stat(m, ns, pid, task, 0);
597 }
598
599 int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
600                         struct pid *pid, struct task_struct *task)
601 {
602         return do_task_stat(m, ns, pid, task, 1);
603 }
604
605 int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
606                         struct pid *pid, struct task_struct *task)
607 {
608         int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
609         struct mm_struct *mm = get_task_mm(task);
610
611         if (mm) {
612                 size = task_statm(mm, &shared, &text, &data, &resident);
613                 mmput(mm);
614         }
615         seq_printf(m, "%d %d %d %d %d %d %d\n",
616                         size, resident, shared, text, lib, data, 0);
617
618         return 0;
619 }