array.c 14 KB

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