array.c 15 KB

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