xref: /linux/fs/proc/array.c (revision 6b3f7af57881f6d6250c6dcc4d910fe8e855a607)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/proc/array.c
4  *
5  *  Copyright (C) 1992  by Linus Torvalds
6  *  based on ideas by Darren Senn
7  *
8  * Fixes:
9  * Michael. K. Johnson: stat,statm extensions.
10  *                      <johnsonm@stolaf.edu>
11  *
12  * Pauline Middelink :  Made cmdline,envline only break at '\0's, to
13  *                      make sure SET_PROCTITLE works. Also removed
14  *                      bad '!' which forced address recalculation for
15  *                      EVERY character on the current page.
16  *                      <middelin@polyware.iaf.nl>
17  *
18  * Danny ter Haar    :	added cpuinfo
19  *			<dth@cistron.nl>
20  *
21  * Alessandro Rubini :  profile extension.
22  *                      <rubini@ipvvis.unipv.it>
23  *
24  * Jeff Tranter      :  added BogoMips field to cpuinfo
25  *                      <Jeff_Tranter@Mitel.COM>
26  *
27  * Bruno Haible      :  remove 4K limit for the maps file
28  *			<haible@ma2s2.mathematik.uni-karlsruhe.de>
29  *
30  * Yves Arrouye      :  remove removal of trailing spaces in get_array.
31  *			<Yves.Arrouye@marin.fdn.fr>
32  *
33  * Jerome Forissier  :  added per-CPU time information to /proc/stat
34  *                      and /proc/<pid>/cpu extension
35  *                      <forissier@isia.cma.fr>
36  *			- Incorporation and non-SMP safe operation
37  *			of forissier patch in 2.1.78 by
38  *			Hans Marcus <crowbar@concepts.nl>
39  *
40  * aeb@cwi.nl        :  /proc/partitions
41  *
42  *
43  * Alan Cox	     :  security fixes.
44  *			<alan@lxorguk.ukuu.org.uk>
45  *
46  * Al Viro           :  safe handling of mm_struct
47  *
48  * Gerhard Wichert   :  added BIGMEM support
49  * Siemens AG           <Gerhard.Wichert@pdb.siemens.de>
50  *
51  * Al Viro & Jeff Garzik :  moved most of the thing into base.c and
52  *			 :  proc_misc.c. The rest may eventually go into
53  *			 :  base.c too.
54  */
55 
56 #include <linux/types.h>
57 #include <linux/errno.h>
58 #include <linux/hex.h>
59 #include <linux/time.h>
60 #include <linux/time_namespace.h>
61 #include <linux/kernel.h>
62 #include <linux/kernel_stat.h>
63 #include <linux/tty.h>
64 #include <linux/string.h>
65 #include <linux/mman.h>
66 #include <linux/sched/mm.h>
67 #include <linux/sched/numa_balancing.h>
68 #include <linux/sched/task_stack.h>
69 #include <linux/sched/task.h>
70 #include <linux/sched/cputime.h>
71 #include <linux/proc_fs.h>
72 #include <linux/ioport.h>
73 #include <linux/io.h>
74 #include <linux/mm.h>
75 #include <linux/hugetlb.h>
76 #include <linux/pagemap.h>
77 #include <linux/swap.h>
78 #include <linux/smp.h>
79 #include <linux/signal.h>
80 #include <linux/highmem.h>
81 #include <linux/file.h>
82 #include <linux/fdtable.h>
83 #include <linux/times.h>
84 #include <linux/cpuset.h>
85 #include <linux/rcupdate.h>
86 #include <linux/delayacct.h>
87 #include <linux/seq_file.h>
88 #include <linux/pid_namespace.h>
89 #include <linux/prctl.h>
90 #include <linux/ptrace.h>
91 #include <linux/string_helpers.h>
92 #include <linux/user_namespace.h>
93 #include <linux/fs_struct.h>
94 #include <linux/kthread.h>
95 #include <linux/mmu_context.h>
96 
97 #include <asm/processor.h>
98 #include "internal.h"
99 
100 void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape)
101 {
102 	char tcomm[64];
103 
104 	/*
105 	 * Test before PF_KTHREAD because all workqueue worker threads are
106 	 * kernel threads.
107 	 */
108 	if (p->flags & PF_WQ_WORKER)
109 		wq_worker_comm(tcomm, sizeof(tcomm), p);
110 	else if (p->flags & PF_KTHREAD)
111 		get_kthread_comm(tcomm, sizeof(tcomm), p);
112 	else
113 		get_task_comm(tcomm, p);
114 
115 	if (escape)
116 		seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
117 	else
118 		seq_printf(m, "%.64s", tcomm);
119 }
120 
121 /*
122  * The task state array is a strange "bitmap" of
123  * reasons to sleep. Thus "running" is zero, and
124  * you can test for combinations of others with
125  * simple bit tests.
126  */
127 static const char * const task_state_array[] = {
128 
129 	/* states in TASK_REPORT: */
130 	"R (running)",		/* 0x00 */
131 	"S (sleeping)",		/* 0x01 */
132 	"D (disk sleep)",	/* 0x02 */
133 	"T (stopped)",		/* 0x04 */
134 	"t (tracing stop)",	/* 0x08 */
135 	"X (dead)",		/* 0x10 */
136 	"Z (zombie)",		/* 0x20 */
137 	"P (parked)",		/* 0x40 */
138 
139 	/* states beyond TASK_REPORT: */
140 	"I (idle)",		/* 0x80 */
141 };
142 
143 static inline const char *get_task_state(struct task_struct *tsk)
144 {
145 	BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != ARRAY_SIZE(task_state_array));
146 	return task_state_array[task_state_index(tsk)];
147 }
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 user_namespace *user_ns = seq_user_ns(m);
153 	struct group_info *group_info;
154 	int g, umask = -1;
155 	struct task_struct *tracer;
156 	const struct cred *cred;
157 	pid_t ppid, tpid = 0, tgid, ngid;
158 	unsigned int max_fds = 0;
159 
160 	rcu_read_lock();
161 	tracer = ptrace_parent(p);
162 	if (tracer)
163 		tpid = task_pid_nr_ns(tracer, ns);
164 
165 	ppid = task_ppid_nr_ns(p, ns);
166 	tgid = task_tgid_nr_ns(p, ns);
167 	ngid = task_numa_group_id(p);
168 	cred = get_task_cred(p);
169 
170 	task_lock(p);
171 	if (p->fs)
172 		umask = p->fs->umask;
173 	if (p->files)
174 		max_fds = files_fdtable(p->files)->max_fds;
175 	task_unlock(p);
176 	rcu_read_unlock();
177 
178 	if (umask >= 0)
179 		seq_printf(m, "Umask:\t%#04o\n", umask);
180 	seq_puts(m, "State:\t");
181 	seq_puts(m, get_task_state(p));
182 
183 	seq_put_decimal_ull(m, "\nTgid:\t", tgid);
184 	seq_put_decimal_ull(m, "\nNgid:\t", ngid);
185 	seq_put_decimal_ull(m, "\nPid:\t", pid_nr_ns(pid, ns));
186 	seq_put_decimal_ull(m, "\nPPid:\t", ppid);
187 	seq_put_decimal_ull(m, "\nTracerPid:\t", tpid);
188 	seq_put_decimal_ull(m, "\nUid:\t", from_kuid_munged(user_ns, cred->uid));
189 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->euid));
190 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->suid));
191 	seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->fsuid));
192 	seq_put_decimal_ull(m, "\nGid:\t", from_kgid_munged(user_ns, cred->gid));
193 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->egid));
194 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->sgid));
195 	seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->fsgid));
196 	seq_put_decimal_ull(m, "\nFDSize:\t", max_fds);
197 
198 	seq_puts(m, "\nGroups:\t");
199 	group_info = cred->group_info;
200 	for (g = 0; g < group_info->ngroups; g++)
201 		seq_put_decimal_ull(m, g ? " " : "",
202 				from_kgid_munged(user_ns, group_info->gid[g]));
203 	put_cred(cred);
204 	/* Trailing space shouldn't have been added in the first place. */
205 	seq_putc(m, ' ');
206 
207 #ifdef CONFIG_PID_NS
208 	seq_puts(m, "\nNStgid:");
209 	for (g = ns->level; g <= pid->level; g++)
210 		seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns));
211 	seq_puts(m, "\nNSpid:");
212 	for (g = ns->level; g <= pid->level; g++)
213 		seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns));
214 	seq_puts(m, "\nNSpgid:");
215 	for (g = ns->level; g <= pid->level; g++)
216 		seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns));
217 	seq_puts(m, "\nNSsid:");
218 	for (g = ns->level; g <= pid->level; g++)
219 		seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns));
220 #endif
221 	seq_putc(m, '\n');
222 
223 	seq_printf(m, "Kthread:\t%c\n", p->flags & PF_KTHREAD ? '1' : '0');
224 }
225 
226 void render_sigset_t(struct seq_file *m, const char *header,
227 				sigset_t *set)
228 {
229 	int i;
230 
231 	seq_puts(m, header);
232 
233 	i = _NSIG;
234 	do {
235 		int x = 0;
236 
237 		i -= 4;
238 		if (sigismember(set, i+1)) x |= 1;
239 		if (sigismember(set, i+2)) x |= 2;
240 		if (sigismember(set, i+3)) x |= 4;
241 		if (sigismember(set, i+4)) x |= 8;
242 		seq_putc(m, hex_asc[x]);
243 	} while (i >= 4);
244 
245 	seq_putc(m, '\n');
246 }
247 
248 static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *sigign,
249 				    sigset_t *sigcatch)
250 {
251 	struct k_sigaction *k;
252 	int i;
253 
254 	k = p->sighand->action;
255 	for (i = 1; i <= _NSIG; ++i, ++k) {
256 		if (k->sa.sa_handler == SIG_IGN)
257 			sigaddset(sigign, i);
258 		else if (k->sa.sa_handler != SIG_DFL)
259 			sigaddset(sigcatch, i);
260 	}
261 }
262 
263 static inline void task_sig(struct seq_file *m, struct task_struct *p)
264 {
265 	unsigned long flags;
266 	sigset_t pending, shpending, blocked, ignored, caught;
267 	int num_threads = 0;
268 	unsigned int qsize = 0;
269 	unsigned long qlim = 0;
270 
271 	sigemptyset(&pending);
272 	sigemptyset(&shpending);
273 	sigemptyset(&blocked);
274 	sigemptyset(&ignored);
275 	sigemptyset(&caught);
276 
277 	if (lock_task_sighand(p, &flags)) {
278 		pending = p->pending.signal;
279 		shpending = p->signal->shared_pending.signal;
280 		blocked = p->blocked;
281 		collect_sigign_sigcatch(p, &ignored, &caught);
282 		num_threads = get_nr_threads(p);
283 		rcu_read_lock();
284 		qsize = get_rlimit_value(task_ucounts(p), UCOUNT_RLIMIT_SIGPENDING);
285 		rcu_read_unlock();
286 		qlim = task_rlimit(p, RLIMIT_SIGPENDING);
287 		unlock_task_sighand(p, &flags);
288 	}
289 
290 	seq_put_decimal_ull(m, "Threads:\t", num_threads);
291 	seq_put_decimal_ull(m, "\nSigQ:\t", qsize);
292 	seq_put_decimal_ull(m, "/", qlim);
293 
294 	/* render them all */
295 	render_sigset_t(m, "\nSigPnd:\t", &pending);
296 	render_sigset_t(m, "ShdPnd:\t", &shpending);
297 	render_sigset_t(m, "SigBlk:\t", &blocked);
298 	render_sigset_t(m, "SigIgn:\t", &ignored);
299 	render_sigset_t(m, "SigCgt:\t", &caught);
300 }
301 
302 static void render_cap_t(struct seq_file *m, const char *header,
303 			kernel_cap_t *a)
304 {
305 	seq_puts(m, header);
306 	seq_put_hex_ll(m, NULL, a->val, 16);
307 	seq_putc(m, '\n');
308 }
309 
310 static inline void task_cap(struct seq_file *m, struct task_struct *p)
311 {
312 	const struct cred *cred;
313 	kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
314 			cap_bset, cap_ambient;
315 
316 	rcu_read_lock();
317 	cred = __task_cred(p);
318 	cap_inheritable	= cred->cap_inheritable;
319 	cap_permitted	= cred->cap_permitted;
320 	cap_effective	= cred->cap_effective;
321 	cap_bset	= cred->cap_bset;
322 	cap_ambient	= cred->cap_ambient;
323 	rcu_read_unlock();
324 
325 	render_cap_t(m, "CapInh:\t", &cap_inheritable);
326 	render_cap_t(m, "CapPrm:\t", &cap_permitted);
327 	render_cap_t(m, "CapEff:\t", &cap_effective);
328 	render_cap_t(m, "CapBnd:\t", &cap_bset);
329 	render_cap_t(m, "CapAmb:\t", &cap_ambient);
330 }
331 
332 static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
333 {
334 	seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p));
335 #ifdef CONFIG_SECCOMP
336 	seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
337 #ifdef CONFIG_SECCOMP_FILTER
338 	seq_put_decimal_ull(m, "\nSeccomp_filters:\t",
339 			    atomic_read(&p->seccomp.filter_count));
340 #endif
341 #endif
342 	seq_puts(m, "\nSpeculation_Store_Bypass:\t");
343 	switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
344 	case -EINVAL:
345 		seq_puts(m, "unknown");
346 		break;
347 	case PR_SPEC_NOT_AFFECTED:
348 		seq_puts(m, "not vulnerable");
349 		break;
350 	case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
351 		seq_puts(m, "thread force mitigated");
352 		break;
353 	case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
354 		seq_puts(m, "thread mitigated");
355 		break;
356 	case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
357 		seq_puts(m, "thread vulnerable");
358 		break;
359 	case PR_SPEC_DISABLE:
360 		seq_puts(m, "globally mitigated");
361 		break;
362 	default:
363 		seq_puts(m, "vulnerable");
364 		break;
365 	}
366 
367 	seq_puts(m, "\nSpeculationIndirectBranch:\t");
368 	switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_INDIRECT_BRANCH)) {
369 	case -EINVAL:
370 		seq_puts(m, "unsupported");
371 		break;
372 	case PR_SPEC_NOT_AFFECTED:
373 		seq_puts(m, "not affected");
374 		break;
375 	case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
376 		seq_puts(m, "conditional force disabled");
377 		break;
378 	case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
379 		seq_puts(m, "conditional disabled");
380 		break;
381 	case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
382 		seq_puts(m, "conditional enabled");
383 		break;
384 	case PR_SPEC_ENABLE:
385 		seq_puts(m, "always enabled");
386 		break;
387 	case PR_SPEC_DISABLE:
388 		seq_puts(m, "always disabled");
389 		break;
390 	default:
391 		seq_puts(m, "unknown");
392 		break;
393 	}
394 	seq_putc(m, '\n');
395 }
396 
397 static inline void task_context_switch_counts(struct seq_file *m,
398 						struct task_struct *p)
399 {
400 	seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
401 	seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
402 	seq_putc(m, '\n');
403 }
404 
405 static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
406 {
407 	seq_printf(m, "Cpus_allowed:\t%*pb\n",
408 		   cpumask_pr_args(&task->cpus_mask));
409 	seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
410 		   cpumask_pr_args(&task->cpus_mask));
411 }
412 
413 static inline void task_core_dumping(struct seq_file *m, struct task_struct *task)
414 {
415 	seq_put_decimal_ull(m, "CoreDumping:\t", !!task->signal->core_state);
416 	seq_putc(m, '\n');
417 }
418 
419 static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
420 {
421 	bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
422 
423 	if (thp_enabled)
424 		thp_enabled = !mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm);
425 	seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
426 }
427 
428 static inline void task_untag_mask(struct seq_file *m, struct mm_struct *mm)
429 {
430 	seq_printf(m, "untag_mask:\t%#lx\n", mm_untag_mask(mm));
431 }
432 
433 __weak void arch_proc_pid_thread_features(struct seq_file *m,
434 					  struct task_struct *task)
435 {
436 }
437 
438 int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
439 			struct pid *pid, struct task_struct *task)
440 {
441 	struct mm_struct *mm = get_task_mm(task);
442 
443 	seq_puts(m, "Name:\t");
444 	proc_task_name(m, task, true);
445 	seq_putc(m, '\n');
446 
447 	task_state(m, ns, pid, task);
448 
449 	if (mm) {
450 		task_mem(m, mm);
451 		task_core_dumping(m, task);
452 		task_thp_status(m, mm);
453 		task_untag_mask(m, mm);
454 		mmput(mm);
455 	}
456 	task_sig(m, task);
457 	task_cap(m, task);
458 	task_seccomp(m, task);
459 	task_cpus_allowed(m, task);
460 	cpuset_task_status_allowed(m, task);
461 	task_context_switch_counts(m, task);
462 	arch_proc_pid_thread_features(m, task);
463 	return 0;
464 }
465 
466 static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
467 			struct pid *pid, struct task_struct *task, int whole)
468 {
469 	unsigned long vsize, eip, esp, wchan = 0;
470 	int priority, nice;
471 	int tty_pgrp = -1, tty_nr = 0;
472 	sigset_t sigign, sigcatch;
473 	char state;
474 	pid_t ppid = 0, pgid = -1, sid = -1;
475 	int num_threads = 0;
476 	int permitted;
477 	struct mm_struct *mm;
478 	unsigned long long start_time;
479 	unsigned long cmin_flt, cmaj_flt, min_flt, maj_flt;
480 	u64 cutime, cstime, cgtime, utime, stime, gtime;
481 	unsigned long rsslim = 0;
482 	unsigned long flags;
483 	int exit_code = task->exit_code;
484 	struct signal_struct *sig = task->signal;
485 	int ret;
486 
487 	ret = down_read_killable(&task->signal->exec_update_lock);
488 	if (ret)
489 		return ret;
490 
491 	state = *get_task_state(task);
492 	vsize = eip = esp = 0;
493 	permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
494 	mm = get_task_mm(task);
495 	if (mm) {
496 		vsize = task_vsize(mm);
497 		/*
498 		 * esp and eip are intentionally zeroed out.  There is no
499 		 * non-racy way to read them without freezing the task.
500 		 * Programs that need reliable values can use ptrace(2).
501 		 *
502 		 * The only exception is if the task is core dumping because
503 		 * a program is not able to use ptrace(2) in that case. It is
504 		 * safe because the task has stopped executing permanently.
505 		 */
506 		if (permitted && (task->flags & (PF_EXITING|PF_DUMPCORE|PF_POSTCOREDUMP))) {
507 			if (try_get_task_stack(task)) {
508 				eip = KSTK_EIP(task);
509 				esp = KSTK_ESP(task);
510 				put_task_stack(task);
511 			}
512 		}
513 	}
514 
515 	sigemptyset(&sigign);
516 	sigemptyset(&sigcatch);
517 
518 	if (lock_task_sighand(task, &flags)) {
519 		if (sig->tty) {
520 			struct pid *pgrp = tty_get_pgrp(sig->tty);
521 			tty_pgrp = pid_nr_ns(pgrp, ns);
522 			put_pid(pgrp);
523 			tty_nr = new_encode_dev(tty_devnum(sig->tty));
524 		}
525 
526 		num_threads = get_nr_threads(task);
527 		collect_sigign_sigcatch(task, &sigign, &sigcatch);
528 
529 		rsslim = READ_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
530 
531 		if (whole) {
532 			if (sig->flags & (SIGNAL_GROUP_EXIT | SIGNAL_STOP_STOPPED))
533 				exit_code = sig->group_exit_code;
534 		}
535 
536 		sid = task_session_nr_ns(task, ns);
537 		ppid = task_ppid_nr_ns(task, ns);
538 		pgid = task_pgrp_nr_ns(task, ns);
539 
540 		unlock_task_sighand(task, &flags);
541 	}
542 
543 	if (permitted && (!whole || num_threads < 2))
544 		wchan = !task_is_running(task);
545 
546 	scoped_guard(rcu) {
547 		scoped_seqlock_read (&sig->stats_lock, ss_lock_irqsave) {
548 			cmin_flt = sig->cmin_flt;
549 			cmaj_flt = sig->cmaj_flt;
550 			cutime = sig->cutime;
551 			cstime = sig->cstime;
552 			cgtime = sig->cgtime;
553 
554 			if (whole) {
555 				struct task_struct *t;
556 
557 				min_flt = sig->min_flt;
558 				maj_flt = sig->maj_flt;
559 				gtime = sig->gtime;
560 
561 				__for_each_thread(sig, t) {
562 					min_flt += t->min_flt;
563 					maj_flt += t->maj_flt;
564 					gtime += task_gtime(t);
565 				}
566 			}
567 		}
568 	}
569 
570 	if (whole) {
571 		thread_group_cputime_adjusted(task, &utime, &stime);
572 	} else {
573 		task_cputime_adjusted(task, &utime, &stime);
574 		min_flt = task->min_flt;
575 		maj_flt = task->maj_flt;
576 		gtime = task_gtime(task);
577 	}
578 
579 	/* scale priority and nice values from timeslices to -20..20 */
580 	/* to make it look like a "normal" Unix priority/nice value  */
581 	priority = task_prio(task);
582 	nice = task_nice(task);
583 
584 	/* apply timens offset for boottime and convert nsec -> ticks */
585 	start_time =
586 		nsec_to_clock_t(timens_add_boottime_ns(task->start_boottime));
587 
588 	seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns));
589 	seq_puts(m, " (");
590 	proc_task_name(m, task, false);
591 	seq_puts(m, ") ");
592 	seq_putc(m, state);
593 	seq_put_decimal_ll(m, " ", ppid);
594 	seq_put_decimal_ll(m, " ", pgid);
595 	seq_put_decimal_ll(m, " ", sid);
596 	seq_put_decimal_ll(m, " ", tty_nr);
597 	seq_put_decimal_ll(m, " ", tty_pgrp);
598 	seq_put_decimal_ull(m, " ", task->flags);
599 	seq_put_decimal_ull(m, " ", min_flt);
600 	seq_put_decimal_ull(m, " ", cmin_flt);
601 	seq_put_decimal_ull(m, " ", maj_flt);
602 	seq_put_decimal_ull(m, " ", cmaj_flt);
603 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime));
604 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime));
605 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime));
606 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime));
607 	seq_put_decimal_ll(m, " ", priority);
608 	seq_put_decimal_ll(m, " ", nice);
609 	seq_put_decimal_ll(m, " ", num_threads);
610 	seq_put_decimal_ull(m, " ", 0);
611 	seq_put_decimal_ull(m, " ", start_time);
612 	seq_put_decimal_ull(m, " ", vsize);
613 	seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0);
614 	seq_put_decimal_ull(m, " ", rsslim);
615 	seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0);
616 	seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0);
617 	seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0);
618 	seq_put_decimal_ull(m, " ", esp);
619 	seq_put_decimal_ull(m, " ", eip);
620 	/* The signal information here is obsolete.
621 	 * It must be decimal for Linux 2.0 compatibility.
622 	 * Use /proc/#/status for real-time signals.
623 	 */
624 	seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL);
625 	seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL);
626 	seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL);
627 	seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL);
628 
629 	/*
630 	 * We used to output the absolute kernel address, but that's an
631 	 * information leak - so instead we show a 0/1 flag here, to signal
632 	 * to user-space whether there's a wchan field in /proc/PID/wchan.
633 	 *
634 	 * This works with older implementations of procps as well.
635 	 */
636 	seq_put_decimal_ull(m, " ", wchan);
637 
638 	seq_put_decimal_ull(m, " ", 0);
639 	seq_put_decimal_ull(m, " ", 0);
640 	seq_put_decimal_ll(m, " ", task->exit_signal);
641 	seq_put_decimal_ll(m, " ", task_cpu(task));
642 	seq_put_decimal_ull(m, " ", task->rt_priority);
643 	seq_put_decimal_ull(m, " ", task->policy);
644 	seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task));
645 	seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
646 	seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
647 
648 	if (mm && permitted) {
649 		seq_put_decimal_ull(m, " ", mm->start_data);
650 		seq_put_decimal_ull(m, " ", mm->end_data);
651 		seq_put_decimal_ull(m, " ", mm->start_brk);
652 		seq_put_decimal_ull(m, " ", mm->arg_start);
653 		seq_put_decimal_ull(m, " ", mm->arg_end);
654 		seq_put_decimal_ull(m, " ", mm->env_start);
655 		seq_put_decimal_ull(m, " ", mm->env_end);
656 	} else
657 		seq_puts(m, " 0 0 0 0 0 0 0");
658 
659 	if (permitted)
660 		seq_put_decimal_ll(m, " ", exit_code);
661 	else
662 		seq_puts(m, " 0");
663 
664 	seq_putc(m, '\n');
665 	up_read(&task->signal->exec_update_lock);
666 	if (mm)
667 		mmput(mm);
668 	return 0;
669 }
670 
671 int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
672 			struct pid *pid, struct task_struct *task)
673 {
674 	return do_task_stat(m, ns, pid, task, 0);
675 }
676 
677 int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
678 			struct pid *pid, struct task_struct *task)
679 {
680 	return do_task_stat(m, ns, pid, task, 1);
681 }
682 
683 int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
684 			struct pid *pid, struct task_struct *task)
685 {
686 	struct mm_struct *mm = get_task_mm(task);
687 
688 	if (mm) {
689 		unsigned long size;
690 		unsigned long resident = 0;
691 		unsigned long shared = 0;
692 		unsigned long text = 0;
693 		unsigned long data = 0;
694 
695 		size = task_statm(mm, &shared, &text, &data, &resident);
696 		mmput(mm);
697 
698 		/*
699 		 * For quick read, open code by putting numbers directly
700 		 * expected format is
701 		 * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
702 		 *               size, resident, shared, text, data);
703 		 */
704 		seq_put_decimal_ull(m, "", size);
705 		seq_put_decimal_ull(m, " ", resident);
706 		seq_put_decimal_ull(m, " ", shared);
707 		seq_put_decimal_ull(m, " ", text);
708 		seq_put_decimal_ull(m, " ", 0);
709 		seq_put_decimal_ull(m, " ", data);
710 		seq_put_decimal_ull(m, " ", 0);
711 		seq_putc(m, '\n');
712 	} else {
713 		seq_write(m, "0 0 0 0 0 0 0\n", 14);
714 	}
715 	return 0;
716 }
717 
718 #ifdef CONFIG_PROC_CHILDREN
719 static struct pid *
720 get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
721 {
722 	struct task_struct *start, *task;
723 	struct pid *pid = NULL;
724 
725 	read_lock(&tasklist_lock);
726 
727 	start = pid_task(proc_pid(inode), PIDTYPE_PID);
728 	if (!start)
729 		goto out;
730 
731 	/*
732 	 * Lets try to continue searching first, this gives
733 	 * us significant speedup on children-rich processes.
734 	 */
735 	if (pid_prev) {
736 		task = pid_task(pid_prev, PIDTYPE_PID);
737 		if (task && task->real_parent == start &&
738 		    !(list_empty(&task->sibling))) {
739 			if (list_is_last(&task->sibling, &start->children))
740 				goto out;
741 			task = list_first_entry(&task->sibling,
742 						struct task_struct, sibling);
743 			pid = get_pid(task_pid(task));
744 			goto out;
745 		}
746 	}
747 
748 	/*
749 	 * Slow search case.
750 	 *
751 	 * We might miss some children here if children
752 	 * are exited while we were not holding the lock,
753 	 * but it was never promised to be accurate that
754 	 * much.
755 	 *
756 	 * "Just suppose that the parent sleeps, but N children
757 	 *  exit after we printed their tids. Now the slow paths
758 	 *  skips N extra children, we miss N tasks." (c)
759 	 *
760 	 * So one need to stop or freeze the leader and all
761 	 * its children to get a precise result.
762 	 */
763 	list_for_each_entry(task, &start->children, sibling) {
764 		if (pos-- == 0) {
765 			pid = get_pid(task_pid(task));
766 			break;
767 		}
768 	}
769 
770 out:
771 	read_unlock(&tasklist_lock);
772 	return pid;
773 }
774 
775 static int children_seq_show(struct seq_file *seq, void *v)
776 {
777 	struct inode *inode = file_inode(seq->file);
778 
779 	seq_printf(seq, "%d ", pid_nr_ns(v, proc_pid_ns(inode->i_sb)));
780 	return 0;
781 }
782 
783 static void *children_seq_start(struct seq_file *seq, loff_t *pos)
784 {
785 	return get_children_pid(file_inode(seq->file), NULL, *pos);
786 }
787 
788 static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
789 {
790 	struct pid *pid;
791 
792 	pid = get_children_pid(file_inode(seq->file), v, *pos + 1);
793 	put_pid(v);
794 
795 	++*pos;
796 	return pid;
797 }
798 
799 static void children_seq_stop(struct seq_file *seq, void *v)
800 {
801 	put_pid(v);
802 }
803 
804 static const struct seq_operations children_seq_ops = {
805 	.start	= children_seq_start,
806 	.next	= children_seq_next,
807 	.stop	= children_seq_stop,
808 	.show	= children_seq_show,
809 };
810 
811 static int children_seq_open(struct inode *inode, struct file *file)
812 {
813 	return seq_open(file, &children_seq_ops);
814 }
815 
816 const struct file_operations proc_tid_children_operations = {
817 	.open    = children_seq_open,
818 	.read    = seq_read,
819 	.llseek  = seq_lseek,
820 	.release = seq_release,
821 };
822 #endif /* CONFIG_PROC_CHILDREN */
823