array.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. #include <linux/types.h>
  55. #include <linux/errno.h>
  56. #include <linux/time.h>
  57. #include <linux/kernel.h>
  58. #include <linux/kernel_stat.h>
  59. #include <linux/tty.h>
  60. #include <linux/string.h>
  61. #include <linux/mman.h>
  62. #include <linux/proc_fs.h>
  63. #include <linux/ioport.h>
  64. #include <linux/uaccess.h>
  65. #include <linux/io.h>
  66. #include <linux/mm.h>
  67. #include <linux/hugetlb.h>
  68. #include <linux/pagemap.h>
  69. #include <linux/swap.h>
  70. #include <linux/slab.h>
  71. #include <linux/smp.h>
  72. #include <linux/signal.h>
  73. #include <linux/highmem.h>
  74. #include <linux/file.h>
  75. #include <linux/fdtable.h>
  76. #include <linux/times.h>
  77. #include <linux/cpuset.h>
  78. #include <linux/rcupdate.h>
  79. #include <linux/delayacct.h>
  80. #include <linux/seq_file.h>
  81. #include <linux/pid_namespace.h>
  82. #include <linux/ptrace.h>
  83. #include <linux/tracehook.h>
  84. #include <linux/swapops.h>
  85. #include <asm/pgtable.h>
  86. #include <asm/processor.h>
  87. #include "internal.h"
  88. static inline void task_name(struct seq_file *m, struct task_struct *p)
  89. {
  90. int i;
  91. char *buf, *end;
  92. char *name;
  93. char tcomm[sizeof(p->comm)];
  94. get_task_comm(tcomm, p);
  95. seq_printf(m, "Name:\t");
  96. end = m->buf + m->size;
  97. buf = m->buf + m->count;
  98. name = tcomm;
  99. i = sizeof(tcomm);
  100. while (i && (buf < end)) {
  101. unsigned char c = *name;
  102. name++;
  103. i--;
  104. *buf = c;
  105. if (!c)
  106. break;
  107. if (c == '\\') {
  108. buf++;
  109. if (buf < end)
  110. *buf++ = c;
  111. continue;
  112. }
  113. if (c == '\n') {
  114. *buf++ = '\\';
  115. if (buf < end)
  116. *buf++ = 'n';
  117. continue;
  118. }
  119. buf++;
  120. }
  121. m->count = buf - m->buf;
  122. seq_printf(m, "\n");
  123. }
  124. /*
  125. * The task state array is a strange "bitmap" of
  126. * reasons to sleep. Thus "running" is zero, and
  127. * you can test for combinations of others with
  128. * simple bit tests.
  129. */
  130. static const char *task_state_array[] = {
  131. "R (running)", /* 0 */
  132. "S (sleeping)", /* 1 */
  133. "D (disk sleep)", /* 2 */
  134. "T (stopped)", /* 4 */
  135. "T (tracing stop)", /* 8 */
  136. "Z (zombie)", /* 16 */
  137. "X (dead)" /* 32 */
  138. };
  139. static inline const char *get_task_state(struct task_struct *tsk)
  140. {
  141. unsigned int state = (tsk->state & TASK_REPORT) | tsk->exit_state;
  142. const char **p = &task_state_array[0];
  143. while (state) {
  144. p++;
  145. state >>= 1;
  146. }
  147. return *p;
  148. }
  149. static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
  150. struct pid *pid, struct task_struct *p)
  151. {
  152. struct group_info *group_info;
  153. int g;
  154. struct fdtable *fdt = NULL;
  155. const struct cred *cred;
  156. pid_t ppid, tpid;
  157. rcu_read_lock();
  158. ppid = pid_alive(p) ?
  159. task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
  160. tpid = 0;
  161. if (pid_alive(p)) {
  162. struct task_struct *tracer = tracehook_tracer_task(p);
  163. if (tracer)
  164. tpid = task_pid_nr_ns(tracer, ns);
  165. }
  166. cred = get_cred((struct cred *) __task_cred(p));
  167. seq_printf(m,
  168. "State:\t%s\n"
  169. "Tgid:\t%d\n"
  170. "Pid:\t%d\n"
  171. "PPid:\t%d\n"
  172. "TracerPid:\t%d\n"
  173. "Uid:\t%d\t%d\t%d\t%d\n"
  174. "Gid:\t%d\t%d\t%d\t%d\n",
  175. get_task_state(p),
  176. task_tgid_nr_ns(p, ns),
  177. pid_nr_ns(pid, ns),
  178. ppid, tpid,
  179. cred->uid, cred->euid, cred->suid, cred->fsuid,
  180. cred->gid, cred->egid, cred->sgid, cred->fsgid);
  181. task_lock(p);
  182. if (p->files)
  183. fdt = files_fdtable(p->files);
  184. seq_printf(m,
  185. "FDSize:\t%d\n"
  186. "Groups:\t",
  187. fdt ? fdt->max_fds : 0);
  188. rcu_read_unlock();
  189. group_info = cred->group_info;
  190. task_unlock(p);
  191. for (g = 0; g < min(group_info->ngroups, NGROUPS_SMALL); g++)
  192. seq_printf(m, "%d ", GROUP_AT(group_info, g));
  193. put_cred(cred);
  194. seq_printf(m, "\n");
  195. }
  196. static void render_sigset_t(struct seq_file *m, const char *header,
  197. sigset_t *set)
  198. {
  199. int i;
  200. seq_printf(m, "%s", header);
  201. i = _NSIG;
  202. do {
  203. int x = 0;
  204. i -= 4;
  205. if (sigismember(set, i+1)) x |= 1;
  206. if (sigismember(set, i+2)) x |= 2;
  207. if (sigismember(set, i+3)) x |= 4;
  208. if (sigismember(set, i+4)) x |= 8;
  209. seq_printf(m, "%x", x);
  210. } while (i >= 4);
  211. seq_printf(m, "\n");
  212. }
  213. static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *ign,
  214. sigset_t *catch)
  215. {
  216. struct k_sigaction *k;
  217. int i;
  218. k = p->sighand->action;
  219. for (i = 1; i <= _NSIG; ++i, ++k) {
  220. if (k->sa.sa_handler == SIG_IGN)
  221. sigaddset(ign, i);
  222. else if (k->sa.sa_handler != SIG_DFL)
  223. sigaddset(catch, i);
  224. }
  225. }
  226. static inline void task_sig(struct seq_file *m, struct task_struct *p)
  227. {
  228. unsigned long flags;
  229. sigset_t pending, shpending, blocked, ignored, caught;
  230. int num_threads = 0;
  231. unsigned long qsize = 0;
  232. unsigned long qlim = 0;
  233. sigemptyset(&pending);
  234. sigemptyset(&shpending);
  235. sigemptyset(&blocked);
  236. sigemptyset(&ignored);
  237. sigemptyset(&caught);
  238. if (lock_task_sighand(p, &flags)) {
  239. pending = p->pending.signal;
  240. shpending = p->signal->shared_pending.signal;
  241. blocked = p->blocked;
  242. collect_sigign_sigcatch(p, &ignored, &caught);
  243. num_threads = atomic_read(&p->signal->count);
  244. qsize = atomic_read(&__task_cred(p)->user->sigpending);
  245. qlim = p->signal->rlim[RLIMIT_SIGPENDING].rlim_cur;
  246. unlock_task_sighand(p, &flags);
  247. }
  248. seq_printf(m, "Threads:\t%d\n", num_threads);
  249. seq_printf(m, "SigQ:\t%lu/%lu\n", qsize, qlim);
  250. /* render them all */
  251. render_sigset_t(m, "SigPnd:\t", &pending);
  252. render_sigset_t(m, "ShdPnd:\t", &shpending);
  253. render_sigset_t(m, "SigBlk:\t", &blocked);
  254. render_sigset_t(m, "SigIgn:\t", &ignored);
  255. render_sigset_t(m, "SigCgt:\t", &caught);
  256. }
  257. static void render_cap_t(struct seq_file *m, const char *header,
  258. kernel_cap_t *a)
  259. {
  260. unsigned __capi;
  261. seq_printf(m, "%s", header);
  262. CAP_FOR_EACH_U32(__capi) {
  263. seq_printf(m, "%08x",
  264. a->cap[(_KERNEL_CAPABILITY_U32S-1) - __capi]);
  265. }
  266. seq_printf(m, "\n");
  267. }
  268. static inline void task_cap(struct seq_file *m, struct task_struct *p)
  269. {
  270. const struct cred *cred;
  271. kernel_cap_t cap_inheritable, cap_permitted, cap_effective, cap_bset;
  272. rcu_read_lock();
  273. cred = __task_cred(p);
  274. cap_inheritable = cred->cap_inheritable;
  275. cap_permitted = cred->cap_permitted;
  276. cap_effective = cred->cap_effective;
  277. cap_bset = cred->cap_bset;
  278. rcu_read_unlock();
  279. render_cap_t(m, "CapInh:\t", &cap_inheritable);
  280. render_cap_t(m, "CapPrm:\t", &cap_permitted);
  281. render_cap_t(m, "CapEff:\t", &cap_effective);
  282. render_cap_t(m, "CapBnd:\t", &cap_bset);
  283. }
  284. static inline void task_context_switch_counts(struct seq_file *m,
  285. struct task_struct *p)
  286. {
  287. seq_printf(m, "voluntary_ctxt_switches:\t%lu\n"
  288. "nonvoluntary_ctxt_switches:\t%lu\n",
  289. p->nvcsw,
  290. p->nivcsw);
  291. }
  292. #ifdef CONFIG_MMU
  293. struct stack_stats {
  294. struct vm_area_struct *vma;
  295. unsigned long startpage;
  296. unsigned long usage;
  297. };
  298. static int stack_usage_pte_range(pmd_t *pmd, unsigned long addr,
  299. unsigned long end, struct mm_walk *walk)
  300. {
  301. struct stack_stats *ss = walk->private;
  302. struct vm_area_struct *vma = ss->vma;
  303. pte_t *pte, ptent;
  304. spinlock_t *ptl;
  305. int ret = 0;
  306. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  307. for (; addr != end; pte++, addr += PAGE_SIZE) {
  308. ptent = *pte;
  309. #ifdef CONFIG_STACK_GROWSUP
  310. if (pte_present(ptent) || is_swap_pte(ptent))
  311. ss->usage = addr - ss->startpage + PAGE_SIZE;
  312. #else
  313. if (pte_present(ptent) || is_swap_pte(ptent)) {
  314. ss->usage = ss->startpage - addr + PAGE_SIZE;
  315. pte++;
  316. ret = 1;
  317. break;
  318. }
  319. #endif
  320. }
  321. pte_unmap_unlock(pte - 1, ptl);
  322. cond_resched();
  323. return ret;
  324. }
  325. static inline unsigned long get_stack_usage_in_bytes(struct vm_area_struct *vma,
  326. struct task_struct *task)
  327. {
  328. struct stack_stats ss;
  329. struct mm_walk stack_walk = {
  330. .pmd_entry = stack_usage_pte_range,
  331. .mm = vma->vm_mm,
  332. .private = &ss,
  333. };
  334. if (!vma->vm_mm || is_vm_hugetlb_page(vma))
  335. return 0;
  336. ss.vma = vma;
  337. ss.startpage = task->stack_start & PAGE_MASK;
  338. ss.usage = 0;
  339. #ifdef CONFIG_STACK_GROWSUP
  340. walk_page_range(KSTK_ESP(task) & PAGE_MASK, vma->vm_end,
  341. &stack_walk);
  342. #else
  343. walk_page_range(vma->vm_start, (KSTK_ESP(task) & PAGE_MASK) + PAGE_SIZE,
  344. &stack_walk);
  345. #endif
  346. return ss.usage;
  347. }
  348. static inline void task_show_stack_usage(struct seq_file *m,
  349. struct task_struct *task)
  350. {
  351. struct vm_area_struct *vma;
  352. struct mm_struct *mm = get_task_mm(task);
  353. if (mm) {
  354. down_read(&mm->mmap_sem);
  355. vma = find_vma(mm, task->stack_start);
  356. if (vma)
  357. seq_printf(m, "Stack usage:\t%lu kB\n",
  358. get_stack_usage_in_bytes(vma, task) >> 10);
  359. up_read(&mm->mmap_sem);
  360. mmput(mm);
  361. }
  362. }
  363. #else
  364. static void task_show_stack_usage(struct seq_file *m, struct task_struct *task)
  365. {
  366. }
  367. #endif /* CONFIG_MMU */
  368. int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
  369. struct pid *pid, struct task_struct *task)
  370. {
  371. struct mm_struct *mm = get_task_mm(task);
  372. task_name(m, task);
  373. task_state(m, ns, pid, task);
  374. if (mm) {
  375. task_mem(m, mm);
  376. mmput(mm);
  377. }
  378. task_sig(m, task);
  379. task_cap(m, task);
  380. cpuset_task_status_allowed(m, task);
  381. #if defined(CONFIG_S390)
  382. task_show_regs(m, task);
  383. #endif
  384. task_context_switch_counts(m, task);
  385. task_show_stack_usage(m, task);
  386. return 0;
  387. }
  388. static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
  389. struct pid *pid, struct task_struct *task, int whole)
  390. {
  391. unsigned long vsize, eip, esp, wchan = ~0UL;
  392. long priority, nice;
  393. int tty_pgrp = -1, tty_nr = 0;
  394. sigset_t sigign, sigcatch;
  395. char state;
  396. pid_t ppid = 0, pgid = -1, sid = -1;
  397. int num_threads = 0;
  398. int permitted;
  399. struct mm_struct *mm;
  400. unsigned long long start_time;
  401. unsigned long cmin_flt = 0, cmaj_flt = 0;
  402. unsigned long min_flt = 0, maj_flt = 0;
  403. cputime_t cutime, cstime, utime, stime;
  404. cputime_t cgtime, gtime;
  405. unsigned long rsslim = 0;
  406. char tcomm[sizeof(task->comm)];
  407. unsigned long flags;
  408. state = *get_task_state(task);
  409. vsize = eip = esp = 0;
  410. permitted = ptrace_may_access(task, PTRACE_MODE_READ);
  411. mm = get_task_mm(task);
  412. if (mm) {
  413. vsize = task_vsize(mm);
  414. if (permitted) {
  415. eip = KSTK_EIP(task);
  416. esp = KSTK_ESP(task);
  417. }
  418. }
  419. get_task_comm(tcomm, task);
  420. sigemptyset(&sigign);
  421. sigemptyset(&sigcatch);
  422. cutime = cstime = utime = stime = cputime_zero;
  423. cgtime = gtime = cputime_zero;
  424. if (lock_task_sighand(task, &flags)) {
  425. struct signal_struct *sig = task->signal;
  426. if (sig->tty) {
  427. struct pid *pgrp = tty_get_pgrp(sig->tty);
  428. tty_pgrp = pid_nr_ns(pgrp, ns);
  429. put_pid(pgrp);
  430. tty_nr = new_encode_dev(tty_devnum(sig->tty));
  431. }
  432. num_threads = atomic_read(&sig->count);
  433. collect_sigign_sigcatch(task, &sigign, &sigcatch);
  434. cmin_flt = sig->cmin_flt;
  435. cmaj_flt = sig->cmaj_flt;
  436. cutime = sig->cutime;
  437. cstime = sig->cstime;
  438. cgtime = sig->cgtime;
  439. rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
  440. /* add up live thread stats at the group level */
  441. if (whole) {
  442. struct task_cputime cputime;
  443. struct task_struct *t = task;
  444. do {
  445. min_flt += t->min_flt;
  446. maj_flt += t->maj_flt;
  447. gtime = cputime_add(gtime, task_gtime(t));
  448. t = next_thread(t);
  449. } while (t != task);
  450. min_flt += sig->min_flt;
  451. maj_flt += sig->maj_flt;
  452. thread_group_cputime(task, &cputime);
  453. utime = cputime.utime;
  454. stime = cputime.stime;
  455. gtime = cputime_add(gtime, sig->gtime);
  456. }
  457. sid = task_session_nr_ns(task, ns);
  458. ppid = task_tgid_nr_ns(task->real_parent, ns);
  459. pgid = task_pgrp_nr_ns(task, ns);
  460. unlock_task_sighand(task, &flags);
  461. }
  462. if (permitted && (!whole || num_threads < 2))
  463. wchan = get_wchan(task);
  464. if (!whole) {
  465. min_flt = task->min_flt;
  466. maj_flt = task->maj_flt;
  467. utime = task_utime(task);
  468. stime = task_stime(task);
  469. gtime = task_gtime(task);
  470. }
  471. /* scale priority and nice values from timeslices to -20..20 */
  472. /* to make it look like a "normal" Unix priority/nice value */
  473. priority = task_prio(task);
  474. nice = task_nice(task);
  475. /* Temporary variable needed for gcc-2.96 */
  476. /* convert timespec -> nsec*/
  477. start_time =
  478. (unsigned long long)task->real_start_time.tv_sec * NSEC_PER_SEC
  479. + task->real_start_time.tv_nsec;
  480. /* convert nsec -> ticks */
  481. start_time = nsec_to_clock_t(start_time);
  482. seq_printf(m, "%d (%s) %c %d %d %d %d %d %u %lu \
  483. %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
  484. %lu %lu %lu %lu %lu %lu %lu %lu %d %d %u %u %llu %lu %ld\n",
  485. pid_nr_ns(pid, ns),
  486. tcomm,
  487. state,
  488. ppid,
  489. pgid,
  490. sid,
  491. tty_nr,
  492. tty_pgrp,
  493. task->flags,
  494. min_flt,
  495. cmin_flt,
  496. maj_flt,
  497. cmaj_flt,
  498. cputime_to_clock_t(utime),
  499. cputime_to_clock_t(stime),
  500. cputime_to_clock_t(cutime),
  501. cputime_to_clock_t(cstime),
  502. priority,
  503. nice,
  504. num_threads,
  505. start_time,
  506. vsize,
  507. mm ? get_mm_rss(mm) : 0,
  508. rsslim,
  509. mm ? mm->start_code : 0,
  510. mm ? mm->end_code : 0,
  511. (permitted && mm) ? task->stack_start : 0,
  512. esp,
  513. eip,
  514. /* The signal information here is obsolete.
  515. * It must be decimal for Linux 2.0 compatibility.
  516. * Use /proc/#/status for real-time signals.
  517. */
  518. task->pending.signal.sig[0] & 0x7fffffffUL,
  519. task->blocked.sig[0] & 0x7fffffffUL,
  520. sigign .sig[0] & 0x7fffffffUL,
  521. sigcatch .sig[0] & 0x7fffffffUL,
  522. wchan,
  523. 0UL,
  524. 0UL,
  525. task->exit_signal,
  526. task_cpu(task),
  527. task->rt_priority,
  528. task->policy,
  529. (unsigned long long)delayacct_blkio_ticks(task),
  530. cputime_to_clock_t(gtime),
  531. cputime_to_clock_t(cgtime));
  532. if (mm)
  533. mmput(mm);
  534. return 0;
  535. }
  536. int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
  537. struct pid *pid, struct task_struct *task)
  538. {
  539. return do_task_stat(m, ns, pid, task, 0);
  540. }
  541. int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
  542. struct pid *pid, struct task_struct *task)
  543. {
  544. return do_task_stat(m, ns, pid, task, 1);
  545. }
  546. int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
  547. struct pid *pid, struct task_struct *task)
  548. {
  549. int size = 0, resident = 0, shared = 0, text = 0, lib = 0, data = 0;
  550. struct mm_struct *mm = get_task_mm(task);
  551. if (mm) {
  552. size = task_statm(mm, &shared, &text, &data, &resident);
  553. mmput(mm);
  554. }
  555. seq_printf(m, "%d %d %d %d %d %d %d\n",
  556. size, resident, shared, text, lib, data, 0);
  557. return 0;
  558. }