blob: 34a47fb0c57f2570a4f7cb1f45373ddaf2afa883 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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.
Alan Cox526719b2008-10-27 15:19:48 +000044 * <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/types.h>
57#include <linux/errno.h>
58#include <linux/time.h>
Michael Weiß3ae700e2020-10-27 21:42:57 +010059#include <linux/time_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/kernel.h>
61#include <linux/kernel_stat.h>
62#include <linux/tty.h>
63#include <linux/string.h>
64#include <linux/mman.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010065#include <linux/sched/mm.h>
Ingo Molnar6a3827d2017-02-08 18:51:31 +010066#include <linux/sched/numa_balancing.h>
John Ognessfd7d5622017-09-14 11:42:17 +020067#include <linux/sched/task_stack.h>
Ingo Molnar29930022017-02-08 18:51:36 +010068#include <linux/sched/task.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010069#include <linux/sched/cputime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/proc_fs.h>
71#include <linux/ioport.h>
Ingo Molnar8ea02602007-07-16 09:46:31 +020072#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/mm.h>
74#include <linux/hugetlb.h>
75#include <linux/pagemap.h>
76#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/smp.h>
78#include <linux/signal.h>
79#include <linux/highmem.h>
80#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040081#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/times.h>
83#include <linux/cpuset.h>
Dipankar Sarma4fb3a532005-09-16 19:28:13 -070084#include <linux/rcupdate.h>
Shailabh Nagar25890452006-07-14 00:24:43 -070085#include <linux/delayacct.h>
Eric W. Biedermanee992742008-02-08 04:18:31 -080086#include <linux/seq_file.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070087#include <linux/pid_namespace.h>
Kees Cookfae1fa02018-05-01 15:31:45 -070088#include <linux/prctl.h>
Jake Edgef83ce3e2009-05-04 12:51:14 -060089#include <linux/ptrace.h>
Andy Shevchenkoedc924e2015-02-12 15:01:11 -080090#include <linux/string_helpers.h>
Eric W. Biedermanae2975b2011-11-14 15:56:38 -080091#include <linux/user_namespace.h>
Richard W.M. Jones3e429792016-05-20 17:00:05 -070092#include <linux/fs_struct.h>
Yafang Shaod6986ce2022-01-19 18:08:43 -080093#include <linux/kthread.h>
Kirill A. Shutemovf7d30432023-03-12 14:26:04 +030094#include <linux/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <asm/processor.h>
97#include "internal.h"
98
Tejun Heo88b72b32018-05-18 08:47:13 -070099void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Tejun Heo6b598082018-05-18 08:47:13 -0700101 char tcomm[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Alexey Dobriyaned8fb782022-07-23 20:09:07 +0300103 /*
104 * Test before PF_KTHREAD because all workqueue worker threads are
105 * kernel threads.
106 */
Tejun Heo6b598082018-05-18 08:47:13 -0700107 if (p->flags & PF_WQ_WORKER)
108 wq_worker_comm(tcomm, sizeof(tcomm), p);
Yafang Shaod6986ce2022-01-19 18:08:43 -0800109 else if (p->flags & PF_KTHREAD)
110 get_kthread_comm(tcomm, sizeof(tcomm), p);
Tejun Heo6b598082018-05-18 08:47:13 -0700111 else
112 __get_task_comm(tcomm, sizeof(tcomm), p);
Andy Shevchenkoedc924e2015-02-12 15:01:11 -0800113
Christoph Hellwig8d23b202021-09-07 19:57:33 -0700114 if (escape)
115 seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
116 else
117 seq_printf(m, "%.64s", tcomm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
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 */
Mike Frysingere130aa72011-05-26 16:25:51 -0700126static const char * const task_state_array[] = {
Peter Zijlstra06eb6182017-09-22 18:30:40 +0200127
128 /* states in TASK_REPORT: */
129 "R (running)", /* 0x00 */
130 "S (sleeping)", /* 0x01 */
131 "D (disk sleep)", /* 0x02 */
132 "T (stopped)", /* 0x04 */
133 "t (tracing stop)", /* 0x08 */
134 "X (dead)", /* 0x10 */
135 "Z (zombie)", /* 0x20 */
Peter Zijlstra8ef99252017-09-22 18:37:28 +0200136 "P (parked)", /* 0x40 */
Peter Zijlstra06eb6182017-09-22 18:30:40 +0200137
138 /* states beyond TASK_REPORT: */
Peter Zijlstra8ef99252017-09-22 18:37:28 +0200139 "I (idle)", /* 0x80 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
Ingo Molnar8ea02602007-07-16 09:46:31 +0200142static inline const char *get_task_state(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Peter Zijlstra06eb6182017-09-22 18:30:40 +0200144 BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != ARRAY_SIZE(task_state_array));
Peter Zijlstra1d48b082017-09-29 13:50:16 +0200145 return task_state_array[task_state_index(tsk)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800148static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
149 struct pid *pid, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Eric W. Biedermane9f238c2012-08-11 12:38:26 -0700151 struct user_namespace *user_ns = seq_user_ns(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct group_info *group_info;
Mateusz Guzik68c34112018-04-10 16:30:51 -0700153 int g, umask = -1;
Oleg Nesterovabdba6e2014-12-10 15:45:18 -0800154 struct task_struct *tracer;
David Howellsc69e8d92008-11-14 10:39:19 +1100155 const struct cred *cred;
Oleg Nesterovabdba6e2014-12-10 15:45:18 -0800156 pid_t ppid, tpid = 0, tgid, ngid;
Oleg Nesterov0f4a0d52014-12-10 15:45:12 -0800157 unsigned int max_fds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Oleg Nesterovb0fa9db2006-10-02 02:18:54 -0700159 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700160 ppid = pid_alive(p) ?
161 task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
Oleg Nesterovabdba6e2014-12-10 15:45:18 -0800162
163 tracer = ptrace_parent(p);
164 if (tracer)
165 tpid = task_pid_nr_ns(tracer, ns);
Oleg Nesterovb0fafc12014-12-10 15:45:15 -0800166
167 tgid = task_tgid_nr_ns(p, ns);
168 ngid = task_numa_group_id(p);
David Howellsde09a972010-07-29 12:45:49 +0100169 cred = get_task_cred(p);
Oleg Nesterov0f4a0d52014-12-10 15:45:12 -0800170
171 task_lock(p);
Mateusz Guzik68c34112018-04-10 16:30:51 -0700172 if (p->fs)
173 umask = p->fs->umask;
Oleg Nesterov0f4a0d52014-12-10 15:45:12 -0800174 if (p->files)
175 max_fds = files_fdtable(p->files)->max_fds;
176 task_unlock(p);
Oleg Nesterovb0fafc12014-12-10 15:45:15 -0800177 rcu_read_unlock();
Oleg Nesterov0f4a0d52014-12-10 15:45:12 -0800178
Mateusz Guzik68c34112018-04-10 16:30:51 -0700179 if (umask >= 0)
180 seq_printf(m, "Umask:\t%#04o\n", umask);
Andrei Vagind0f02232018-04-10 16:31:26 -0700181 seq_puts(m, "State:\t");
182 seq_puts(m, get_task_state(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Joe Perches75ba1d02016-10-07 17:02:20 -0700184 seq_put_decimal_ull(m, "\nTgid:\t", tgid);
185 seq_put_decimal_ull(m, "\nNgid:\t", ngid);
186 seq_put_decimal_ull(m, "\nPid:\t", pid_nr_ns(pid, ns));
187 seq_put_decimal_ull(m, "\nPPid:\t", ppid);
188 seq_put_decimal_ull(m, "\nTracerPid:\t", tpid);
189 seq_put_decimal_ull(m, "\nUid:\t", from_kuid_munged(user_ns, cred->uid));
190 seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->euid));
191 seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->suid));
192 seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->fsuid));
193 seq_put_decimal_ull(m, "\nGid:\t", from_kgid_munged(user_ns, cred->gid));
194 seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->egid));
195 seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->sgid));
196 seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->fsgid));
197 seq_put_decimal_ull(m, "\nFDSize:\t", max_fds);
Alexey Dobriyanf7a5f132016-10-07 17:02:17 -0700198
199 seq_puts(m, "\nGroups:\t");
David Howellsc69e8d92008-11-14 10:39:19 +1100200 group_info = cred->group_info;
Artem Bityutskiy8d238022012-12-17 16:03:17 -0800201 for (g = 0; g < group_info->ngroups; g++)
Joe Perches75ba1d02016-10-07 17:02:20 -0700202 seq_put_decimal_ull(m, g ? " " : "",
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700203 from_kgid_munged(user_ns, group_info->gid[g]));
David Howellsc69e8d92008-11-14 10:39:19 +1100204 put_cred(cred);
Alexey Dobriyanf7a5f132016-10-07 17:02:17 -0700205 /* Trailing space shouldn't have been added in the first place. */
206 seq_putc(m, ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Chen Hanxiaoe4bc3322015-04-15 16:16:30 -0700208#ifdef CONFIG_PID_NS
209 seq_puts(m, "\nNStgid:");
210 for (g = ns->level; g <= pid->level; g++)
Joe Perches75ba1d02016-10-07 17:02:20 -0700211 seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns));
Chen Hanxiaoe4bc3322015-04-15 16:16:30 -0700212 seq_puts(m, "\nNSpid:");
213 for (g = ns->level; g <= pid->level; g++)
Joe Perches75ba1d02016-10-07 17:02:20 -0700214 seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns));
Chen Hanxiaoe4bc3322015-04-15 16:16:30 -0700215 seq_puts(m, "\nNSpgid:");
216 for (g = ns->level; g <= pid->level; g++)
Joe Perches75ba1d02016-10-07 17:02:20 -0700217 seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns));
Chen Hanxiaoe4bc3322015-04-15 16:16:30 -0700218 seq_puts(m, "\nNSsid:");
219 for (g = ns->level; g <= pid->level; g++)
Joe Perches75ba1d02016-10-07 17:02:20 -0700220 seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns));
Chen Hanxiaoe4bc3322015-04-15 16:16:30 -0700221#endif
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800222 seq_putc(m, '\n');
Chunguang Wu522dc4e2023-04-16 13:24:04 +0800223
224 seq_printf(m, "Kthread:\t%c\n", p->flags & PF_KTHREAD ? '1' : '0');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Cyrill Gorcunov138d22b2012-12-17 16:05:02 -0800227void render_sigset_t(struct seq_file *m, const char *header,
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800228 sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800230 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800232 seq_puts(m, header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 i = _NSIG;
235 do {
236 int x = 0;
237
238 i -= 4;
239 if (sigismember(set, i+1)) x |= 1;
240 if (sigismember(set, i+2)) x |= 2;
241 if (sigismember(set, i+3)) x |= 4;
242 if (sigismember(set, i+4)) x |= 8;
Rasmus Villemoes209b14d2016-12-12 16:45:25 -0800243 seq_putc(m, hex_asc[x]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 } while (i >= 4);
245
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800246 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Alexey Dobriyan8977a272020-06-04 16:49:55 -0700249static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *sigign,
250 sigset_t *sigcatch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 struct k_sigaction *k;
253 int i;
254
255 k = p->sighand->action;
256 for (i = 1; i <= _NSIG; ++i, ++k) {
257 if (k->sa.sa_handler == SIG_IGN)
Alexey Dobriyan8977a272020-06-04 16:49:55 -0700258 sigaddset(sigign, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 else if (k->sa.sa_handler != SIG_DFL)
Alexey Dobriyan8977a272020-06-04 16:49:55 -0700260 sigaddset(sigcatch, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262}
263
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800264static inline void task_sig(struct seq_file *m, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Oleg Nesterov5e6b3f42006-10-02 02:18:52 -0700266 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 sigset_t pending, shpending, blocked, ignored, caught;
268 int num_threads = 0;
Alexey Dobriyan197850a12018-06-07 17:10:13 -0700269 unsigned int qsize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 unsigned long qlim = 0;
271
272 sigemptyset(&pending);
273 sigemptyset(&shpending);
274 sigemptyset(&blocked);
275 sigemptyset(&ignored);
276 sigemptyset(&caught);
277
Oleg Nesterov5e6b3f42006-10-02 02:18:52 -0700278 if (lock_task_sighand(p, &flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 pending = p->pending.signal;
280 shpending = p->signal->shared_pending.signal;
281 blocked = p->blocked;
282 collect_sigign_sigcatch(p, &ignored, &caught);
Oleg Nesterov7e498272010-05-26 14:43:22 -0700283 num_threads = get_nr_threads(p);
Paul E. McKenney7dc52152010-02-22 17:04:52 -0800284 rcu_read_lock(); /* FIXME: is this correct? */
Alexey Gladkovde399232022-05-18 19:17:30 +0200285 qsize = get_rlimit_value(task_ucounts(p), UCOUNT_RLIMIT_SIGPENDING);
Paul E. McKenney7dc52152010-02-22 17:04:52 -0800286 rcu_read_unlock();
Jiri Slabyd554ed892010-03-05 13:42:42 -0800287 qlim = task_rlimit(p, RLIMIT_SIGPENDING);
Oleg Nesterov5e6b3f42006-10-02 02:18:52 -0700288 unlock_task_sighand(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Joe Perches75ba1d02016-10-07 17:02:20 -0700291 seq_put_decimal_ull(m, "Threads:\t", num_threads);
292 seq_put_decimal_ull(m, "\nSigQ:\t", qsize);
293 seq_put_decimal_ull(m, "/", qlim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /* render them all */
Alexey Dobriyanf7a5f132016-10-07 17:02:17 -0700296 render_sigset_t(m, "\nSigPnd:\t", &pending);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800297 render_sigset_t(m, "ShdPnd:\t", &shpending);
298 render_sigset_t(m, "SigBlk:\t", &blocked);
299 render_sigset_t(m, "SigIgn:\t", &ignored);
300 render_sigset_t(m, "SigCgt:\t", &caught);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800303static void render_cap_t(struct seq_file *m, const char *header,
304 kernel_cap_t *a)
Andrew Morgane338d262008-02-04 22:29:42 -0800305{
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800306 seq_puts(m, header);
Linus Torvaldsf122a082023-02-28 11:39:09 -0800307 seq_put_hex_ll(m, NULL, a->val, 16);
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800308 seq_putc(m, '\n');
Andrew Morgane338d262008-02-04 22:29:42 -0800309}
310
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800311static inline void task_cap(struct seq_file *m, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
David Howellsc69e8d92008-11-14 10:39:19 +1100313 const struct cred *cred;
Andy Lutomirski58319052015-09-04 15:42:45 -0700314 kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
315 cap_bset, cap_ambient;
David Howellsb6dff3e2008-11-14 10:39:16 +1100316
David Howellsc69e8d92008-11-14 10:39:19 +1100317 rcu_read_lock();
318 cred = __task_cred(p);
319 cap_inheritable = cred->cap_inheritable;
320 cap_permitted = cred->cap_permitted;
321 cap_effective = cred->cap_effective;
322 cap_bset = cred->cap_bset;
Andy Lutomirski58319052015-09-04 15:42:45 -0700323 cap_ambient = cred->cap_ambient;
David Howellsc69e8d92008-11-14 10:39:19 +1100324 rcu_read_unlock();
325
326 render_cap_t(m, "CapInh:\t", &cap_inheritable);
327 render_cap_t(m, "CapPrm:\t", &cap_permitted);
328 render_cap_t(m, "CapEff:\t", &cap_effective);
329 render_cap_t(m, "CapBnd:\t", &cap_bset);
Andy Lutomirski58319052015-09-04 15:42:45 -0700330 render_cap_t(m, "CapAmb:\t", &cap_ambient);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Kees Cook2f4b3bf2012-12-17 16:03:14 -0800333static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
334{
Kees Cookaf884cd2016-12-12 16:45:05 -0800335 seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p));
Kees Cook2f4b3bf2012-12-17 16:03:14 -0800336#ifdef CONFIG_SECCOMP
Kees Cookaf884cd2016-12-12 16:45:05 -0800337 seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
Kenta.Tada@sony.com64bdc022021-03-21 15:52:19 +0000338#ifdef CONFIG_SECCOMP_FILTER
Kees Cookc818c032020-05-13 14:11:26 -0700339 seq_put_decimal_ull(m, "\nSeccomp_filters:\t",
340 atomic_read(&p->seccomp.filter_count));
Kees Cook2f4b3bf2012-12-17 16:03:14 -0800341#endif
Kenta.Tada@sony.com64bdc022021-03-21 15:52:19 +0000342#endif
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800343 seq_puts(m, "\nSpeculation_Store_Bypass:\t");
Kees Cookfae1fa02018-05-01 15:31:45 -0700344 switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
345 case -EINVAL:
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800346 seq_puts(m, "unknown");
Kees Cookfae1fa02018-05-01 15:31:45 -0700347 break;
348 case PR_SPEC_NOT_AFFECTED:
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800349 seq_puts(m, "not vulnerable");
Kees Cookfae1fa02018-05-01 15:31:45 -0700350 break;
Thomas Gleixner356e4bf2018-05-03 22:09:15 +0200351 case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800352 seq_puts(m, "thread force mitigated");
Thomas Gleixner356e4bf2018-05-03 22:09:15 +0200353 break;
Kees Cookfae1fa02018-05-01 15:31:45 -0700354 case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800355 seq_puts(m, "thread mitigated");
Kees Cookfae1fa02018-05-01 15:31:45 -0700356 break;
357 case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800358 seq_puts(m, "thread vulnerable");
Kees Cookfae1fa02018-05-01 15:31:45 -0700359 break;
360 case PR_SPEC_DISABLE:
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800361 seq_puts(m, "globally mitigated");
Kees Cookfae1fa02018-05-01 15:31:45 -0700362 break;
363 default:
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800364 seq_puts(m, "vulnerable");
Kees Cookfae1fa02018-05-01 15:31:45 -0700365 break;
366 }
Anand K Mistryfe719882020-12-15 20:42:36 -0800367
368 seq_puts(m, "\nSpeculationIndirectBranch:\t");
369 switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_INDIRECT_BRANCH)) {
370 case -EINVAL:
371 seq_puts(m, "unsupported");
372 break;
373 case PR_SPEC_NOT_AFFECTED:
374 seq_puts(m, "not affected");
375 break;
376 case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
377 seq_puts(m, "conditional force disabled");
378 break;
379 case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
380 seq_puts(m, "conditional disabled");
381 break;
382 case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
383 seq_puts(m, "conditional enabled");
384 break;
385 case PR_SPEC_ENABLE:
386 seq_puts(m, "always enabled");
387 break;
388 case PR_SPEC_DISABLE:
389 seq_puts(m, "always disabled");
390 break;
391 default:
392 seq_puts(m, "unknown");
393 break;
394 }
Kees Cookaf884cd2016-12-12 16:45:05 -0800395 seq_putc(m, '\n');
Kees Cook2f4b3bf2012-12-17 16:03:14 -0800396}
397
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800398static inline void task_context_switch_counts(struct seq_file *m,
399 struct task_struct *p)
Maxim Uvarovb663a792007-07-15 23:40:48 -0700400{
Joe Perches75ba1d02016-10-07 17:02:20 -0700401 seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
402 seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
Alexey Dobriyanf7a5f132016-10-07 17:02:17 -0700403 seq_putc(m, '\n');
Maxim Uvarovb663a792007-07-15 23:40:48 -0700404}
405
Heiko Carstensd01d4822009-09-21 11:06:27 +0200406static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
407{
Tejun Heoa0c2e072015-02-13 14:38:07 -0800408 seq_printf(m, "Cpus_allowed:\t%*pb\n",
Peter Zijlstra86fbcd32020-10-05 12:49:16 +0200409 cpumask_pr_args(&task->cpus_mask));
Tejun Heoa0c2e072015-02-13 14:38:07 -0800410 seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
Peter Zijlstra86fbcd32020-10-05 12:49:16 +0200411 cpumask_pr_args(&task->cpus_mask));
Heiko Carstensd01d4822009-09-21 11:06:27 +0200412}
413
Eric W. Biederman0258b5f2021-09-22 11:24:02 -0500414static inline void task_core_dumping(struct seq_file *m, struct task_struct *task)
Roman Gushchinc6434012017-11-17 15:26:45 -0800415{
Eric W. Biederman0258b5f2021-09-22 11:24:02 -0500416 seq_put_decimal_ull(m, "CoreDumping:\t", !!task->signal->core_state);
Andrei Vagind0f02232018-04-10 16:31:26 -0700417 seq_putc(m, '\n');
Roman Gushchinc6434012017-11-17 15:26:45 -0800418}
419
Michal Hockoa1400af2018-12-28 00:38:25 -0800420static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
421{
422 bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
423
424 if (thp_enabled)
425 thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
426 seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
427}
428
Kirill A. Shutemovf7d30432023-03-12 14:26:04 +0300429static inline void task_untag_mask(struct seq_file *m, struct mm_struct *mm)
430{
431 seq_printf(m, "untag_mask:\t%#lx\n", mm_untag_mask(mm));
432}
433
Rick Edgecombe0ee44882023-06-12 17:11:02 -0700434__weak void arch_proc_pid_thread_features(struct seq_file *m,
435 struct task_struct *task)
436{
437}
438
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800439int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
440 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct mm_struct *mm = get_task_mm(task);
443
Tejun Heo88b72b32018-05-18 08:47:13 -0700444 seq_puts(m, "Name:\t");
445 proc_task_name(m, task, true);
446 seq_putc(m, '\n');
447
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800448 task_state(m, ns, pid, task);
Ingo Molnar8ea02602007-07-16 09:46:31 +0200449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (mm) {
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800451 task_mem(m, mm);
Eric W. Biederman0258b5f2021-09-22 11:24:02 -0500452 task_core_dumping(m, task);
Michal Hockoa1400af2018-12-28 00:38:25 -0800453 task_thp_status(m, mm);
Kirill A. Shutemovf7d30432023-03-12 14:26:04 +0300454 task_untag_mask(m, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 mmput(mm);
456 }
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800457 task_sig(m, task);
458 task_cap(m, task);
Kees Cook2f4b3bf2012-12-17 16:03:14 -0800459 task_seccomp(m, task);
Heiko Carstensd01d4822009-09-21 11:06:27 +0200460 task_cpus_allowed(m, task);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800461 cpuset_task_status_allowed(m, task);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800462 task_context_switch_counts(m, task);
Rick Edgecombe0ee44882023-06-12 17:11:02 -0700463 arch_proc_pid_thread_features(m, task);
Eric W. Biedermandf5f8312008-02-08 04:18:33 -0800464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Eric W. Biedermanee992742008-02-08 04:18:31 -0800467static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
468 struct pid *pid, struct task_struct *task, int whole)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Ingo Molnarb2f73922015-09-30 15:59:17 +0200470 unsigned long vsize, eip, esp, wchan = 0;
Jan Engelhardt715be1f2012-05-31 16:26:19 -0700471 int priority, nice;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int tty_pgrp = -1, tty_nr = 0;
473 sigset_t sigign, sigcatch;
474 char state;
Ingo Molnar8ea02602007-07-16 09:46:31 +0200475 pid_t ppid = 0, pgid = -1, sid = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int num_threads = 0;
Jake Edgef83ce3e2009-05-04 12:51:14 -0600477 int permitted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct mm_struct *mm;
479 unsigned long long start_time;
Oleg Nesterov7601df82024-01-23 16:33:57 +0100480 unsigned long cmin_flt, cmaj_flt, min_flt, maj_flt;
481 u64 cutime, cstime, cgtime, utime, stime, gtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 unsigned long rsslim = 0;
Oleg Nesterova593d6e2006-10-02 02:18:53 -0700483 unsigned long flags;
Eric W. Biederman2d18f7f2021-12-20 19:16:34 -0600484 int exit_code = task->exit_code;
Oleg Nesterov7601df82024-01-23 16:33:57 +0100485 struct signal_struct *sig = task->signal;
486 unsigned int seq = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 state = *get_task_state(task);
489 vsize = eip = esp = 0;
Jann Horncaaee622016-01-20 15:00:04 -0800490 permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 mm = get_task_mm(task);
492 if (mm) {
493 vsize = task_vsize(mm);
Andy Lutomirski0a1eb2d2016-09-30 10:58:56 -0700494 /*
495 * esp and eip are intentionally zeroed out. There is no
496 * non-racy way to read them without freezing the task.
497 * Programs that need reliable values can use ptrace(2).
John Ognessfd7d5622017-09-14 11:42:17 +0200498 *
499 * The only exception is if the task is core dumping because
500 * a program is not able to use ptrace(2) in that case. It is
501 * safe because the task has stopped executing permanently.
Andy Lutomirski0a1eb2d2016-09-30 10:58:56 -0700502 */
John Ognesscb8f3812019-06-28 12:06:40 -0700503 if (permitted && (task->flags & (PF_EXITING|PF_DUMPCORE))) {
Alexey Dobriyan8bb2ee12018-01-18 16:34:05 -0800504 if (try_get_task_stack(task)) {
505 eip = KSTK_EIP(task);
506 esp = KSTK_ESP(task);
507 put_task_stack(task);
508 }
John Ognessfd7d5622017-09-14 11:42:17 +0200509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 sigemptyset(&sigign);
513 sigemptyset(&sigcatch);
Alan Cox3cfd0882006-09-29 02:00:41 -0700514
Oleg Nesterova593d6e2006-10-02 02:18:53 -0700515 if (lock_task_sighand(task, &flags)) {
Oleg Nesterov91593502006-12-08 02:36:07 -0800516 if (sig->tty) {
Alan Cox5d0fdf12008-04-30 00:53:31 -0700517 struct pid *pgrp = tty_get_pgrp(sig->tty);
518 tty_pgrp = pid_nr_ns(pgrp, ns);
519 put_pid(pgrp);
Oleg Nesterov91593502006-12-08 02:36:07 -0800520 tty_nr = new_encode_dev(tty_devnum(sig->tty));
Oleg Nesterova593d6e2006-10-02 02:18:53 -0700521 }
522
Oleg Nesterov7e498272010-05-26 14:43:22 -0700523 num_threads = get_nr_threads(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 collect_sigign_sigcatch(task, &sigign, &sigcatch);
525
Mark Rutland6aa7de02017-10-23 14:07:29 -0700526 rsslim = READ_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
Oleg Nesterova593d6e2006-10-02 02:18:53 -0700527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (whole) {
Eric W. Biederman2d18f7f2021-12-20 19:16:34 -0600529 if (sig->flags & (SIGNAL_GROUP_EXIT | SIGNAL_STOP_STOPPED))
530 exit_code = sig->group_exit_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700533 sid = task_session_nr_ns(task, ns);
Oleg Nesterova98fdce2008-01-15 00:02:37 +0300534 ppid = task_tgid_nr_ns(task->real_parent, ns);
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700535 pgid = task_pgrp_nr_ns(task, ns);
Oleg Nesterova593d6e2006-10-02 02:18:53 -0700536
537 unlock_task_sighand(task, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Jake Edgef83ce3e2009-05-04 12:51:14 -0600540 if (permitted && (!whole || num_threads < 2))
Kees Cook4e046152021-09-29 15:02:15 -0700541 wchan = !task_is_running(task);
Oleg Nesterov60f92ac2024-01-23 16:33:55 +0100542
Oleg Nesterov7601df82024-01-23 16:33:57 +0100543 do {
544 seq++; /* 2 on the 1st/lockless path, otherwise odd */
545 flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
546
547 cmin_flt = sig->cmin_flt;
548 cmaj_flt = sig->cmaj_flt;
549 cutime = sig->cutime;
550 cstime = sig->cstime;
551 cgtime = sig->cgtime;
552
553 if (whole) {
554 struct task_struct *t;
555
556 min_flt = sig->min_flt;
557 maj_flt = sig->maj_flt;
558 gtime = sig->gtime;
559
560 rcu_read_lock();
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 rcu_read_unlock();
567 }
568 } while (need_seqretry(&sig->stats_lock, seq));
569 done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
570
Oleg Nesterov60f92ac2024-01-23 16:33:55 +0100571 if (whole) {
572 thread_group_cputime_adjusted(task, &utime, &stime);
573 } else {
574 task_cputime_adjusted(task, &utime, &stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 min_flt = task->min_flt;
576 maj_flt = task->maj_flt;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100577 gtime = task_gtime(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
580 /* scale priority and nice values from timeslices to -20..20 */
581 /* to make it look like a "normal" Unix priority/nice value */
582 priority = task_prio(task);
583 nice = task_nice(task);
584
Michael Weiß3ae700e2020-10-27 21:42:57 +0100585 /* apply timens offset for boottime and convert nsec -> ticks */
586 start_time =
587 nsec_to_clock_t(timens_add_boottime_ns(task->start_boottime));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Andrei Vagind0f02232018-04-10 16:31:26 -0700589 seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns));
590 seq_puts(m, " (");
Tejun Heo88b72b32018-05-18 08:47:13 -0700591 proc_task_name(m, task, false);
Andrei Vagind0f02232018-04-10 16:31:26 -0700592 seq_puts(m, ") ");
593 seq_putc(m, state);
Joe Perches75ba1d02016-10-07 17:02:20 -0700594 seq_put_decimal_ll(m, " ", ppid);
595 seq_put_decimal_ll(m, " ", pgid);
596 seq_put_decimal_ll(m, " ", sid);
597 seq_put_decimal_ll(m, " ", tty_nr);
598 seq_put_decimal_ll(m, " ", tty_pgrp);
599 seq_put_decimal_ull(m, " ", task->flags);
600 seq_put_decimal_ull(m, " ", min_flt);
601 seq_put_decimal_ull(m, " ", cmin_flt);
602 seq_put_decimal_ull(m, " ", maj_flt);
603 seq_put_decimal_ull(m, " ", cmaj_flt);
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100604 seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime));
605 seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime));
606 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime));
607 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime));
Joe Perches75ba1d02016-10-07 17:02:20 -0700608 seq_put_decimal_ll(m, " ", priority);
609 seq_put_decimal_ll(m, " ", nice);
610 seq_put_decimal_ll(m, " ", num_threads);
611 seq_put_decimal_ull(m, " ", 0);
612 seq_put_decimal_ull(m, " ", start_time);
613 seq_put_decimal_ull(m, " ", vsize);
614 seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0);
615 seq_put_decimal_ull(m, " ", rsslim);
616 seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0);
617 seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0);
618 seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0);
619 seq_put_decimal_ull(m, " ", esp);
620 seq_put_decimal_ull(m, " ", eip);
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700621 /* The signal information here is obsolete.
622 * It must be decimal for Linux 2.0 compatibility.
623 * Use /proc/#/status for real-time signals.
624 */
Joe Perches75ba1d02016-10-07 17:02:20 -0700625 seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL);
626 seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL);
627 seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL);
628 seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL);
Ingo Molnarb2f73922015-09-30 15:59:17 +0200629
630 /*
631 * We used to output the absolute kernel address, but that's an
632 * information leak - so instead we show a 0/1 flag here, to signal
633 * to user-space whether there's a wchan field in /proc/PID/wchan.
634 *
635 * This works with older implementations of procps as well.
636 */
Kees Cook4e046152021-09-29 15:02:15 -0700637 seq_put_decimal_ull(m, " ", wchan);
Ingo Molnarb2f73922015-09-30 15:59:17 +0200638
Joe Perches75ba1d02016-10-07 17:02:20 -0700639 seq_put_decimal_ull(m, " ", 0);
640 seq_put_decimal_ull(m, " ", 0);
641 seq_put_decimal_ll(m, " ", task->exit_signal);
642 seq_put_decimal_ll(m, " ", task_cpu(task));
643 seq_put_decimal_ull(m, " ", task->rt_priority);
644 seq_put_decimal_ull(m, " ", task->policy);
645 seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task));
Frederic Weisbecker16a6d9b2017-01-31 04:09:21 +0100646 seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
647 seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
Cyrill Gorcunov5b172082012-05-31 16:26:44 -0700648
649 if (mm && permitted) {
Joe Perches75ba1d02016-10-07 17:02:20 -0700650 seq_put_decimal_ull(m, " ", mm->start_data);
651 seq_put_decimal_ull(m, " ", mm->end_data);
652 seq_put_decimal_ull(m, " ", mm->start_brk);
653 seq_put_decimal_ull(m, " ", mm->arg_start);
654 seq_put_decimal_ull(m, " ", mm->arg_end);
655 seq_put_decimal_ull(m, " ", mm->env_start);
656 seq_put_decimal_ull(m, " ", mm->env_end);
Cyrill Gorcunov5b172082012-05-31 16:26:44 -0700657 } else
Joe Perches75ba1d02016-10-07 17:02:20 -0700658 seq_puts(m, " 0 0 0 0 0 0 0");
Cyrill Gorcunov5b172082012-05-31 16:26:44 -0700659
660 if (permitted)
Eric W. Biederman2d18f7f2021-12-20 19:16:34 -0600661 seq_put_decimal_ll(m, " ", exit_code);
Cyrill Gorcunov5b172082012-05-31 16:26:44 -0700662 else
Joe Perches75ba1d02016-10-07 17:02:20 -0700663 seq_puts(m, " 0");
Cyrill Gorcunov5b172082012-05-31 16:26:44 -0700664
KAMEZAWA Hiroyukibda7bad2012-03-23 15:02:54 -0700665 seq_putc(m, '\n');
Ingo Molnar8ea02602007-07-16 09:46:31 +0200666 if (mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 mmput(mm);
Eric W. Biedermanee992742008-02-08 04:18:31 -0800668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Eric W. Biedermanee992742008-02-08 04:18:31 -0800671int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
672 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Eric W. Biedermanee992742008-02-08 04:18:31 -0800674 return do_task_stat(m, ns, pid, task, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Eric W. Biedermanee992742008-02-08 04:18:31 -0800677int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
678 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Eric W. Biedermanee992742008-02-08 04:18:31 -0800680 return do_task_stat(m, ns, pid, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -0800683int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
684 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct mm_struct *mm = get_task_mm(task);
Ingo Molnar8ea02602007-07-16 09:46:31 +0200687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (mm) {
Alexey Dobriyan5c5ab972020-04-06 20:09:05 -0700689 unsigned long size;
690 unsigned long resident = 0;
691 unsigned long shared = 0;
692 unsigned long text = 0;
693 unsigned long data = 0;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 size = task_statm(mm, &shared, &text, &data, &resident);
696 mmput(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Alexey Dobriyan5c5ab972020-04-06 20:09:05 -0700698 /*
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 }
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -0800715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
Cyrill Gorcunov818411612012-05-31 16:26:43 -0700717
Iago López Galeiras2e13ba52015-06-25 15:00:57 -0700718#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -0700719static struct pid *
720get_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
770out:
771 read_unlock(&tasklist_lock);
772 return pid;
773}
774
775static int children_seq_show(struct seq_file *seq, void *v)
776{
Christoph Hellwig04015e32018-05-16 07:21:53 +0200777 struct inode *inode = file_inode(seq->file);
Cyrill Gorcunov818411612012-05-31 16:26:43 -0700778
Alexey Gladkov9d78ede2020-05-18 20:07:38 +0200779 seq_printf(seq, "%d ", pid_nr_ns(v, proc_pid_ns(inode->i_sb)));
Joe Perches25ce3192015-04-15 16:18:17 -0700780 return 0;
Cyrill Gorcunov818411612012-05-31 16:26:43 -0700781}
782
783static void *children_seq_start(struct seq_file *seq, loff_t *pos)
784{
Christoph Hellwig04015e32018-05-16 07:21:53 +0200785 return get_children_pid(file_inode(seq->file), NULL, *pos);
Cyrill Gorcunov818411612012-05-31 16:26:43 -0700786}
787
788static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
789{
790 struct pid *pid;
791
Christoph Hellwig04015e32018-05-16 07:21:53 +0200792 pid = get_children_pid(file_inode(seq->file), v, *pos + 1);
Cyrill Gorcunov818411612012-05-31 16:26:43 -0700793 put_pid(v);
794
795 ++*pos;
796 return pid;
797}
798
799static void children_seq_stop(struct seq_file *seq, void *v)
800{
801 put_pid(v);
802}
803
804static 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
811static int children_seq_open(struct inode *inode, struct file *file)
812{
Christoph Hellwig04015e32018-05-16 07:21:53 +0200813 return seq_open(file, &children_seq_ops);
Cyrill Gorcunov818411612012-05-31 16:26:43 -0700814}
815
Cyrill Gorcunov818411612012-05-31 16:26:43 -0700816const struct file_operations proc_tid_children_operations = {
817 .open = children_seq_open,
818 .read = seq_read,
819 .llseek = seq_lseek,
Alexey Dobriyan171ef912018-02-06 15:37:10 -0800820 .release = seq_release,
Cyrill Gorcunov818411612012-05-31 16:26:43 -0700821};
Iago López Galeiras2e13ba52015-06-25 15:00:57 -0700822#endif /* CONFIG_PROC_CHILDREN */