1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/proc/array.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1992 by Linus Torvalds
61da177e4SLinus Torvalds * based on ideas by Darren Senn
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * Fixes:
91da177e4SLinus Torvalds * Michael. K. Johnson: stat,statm extensions.
101da177e4SLinus Torvalds * <johnsonm@stolaf.edu>
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds * Pauline Middelink : Made cmdline,envline only break at '\0's, to
131da177e4SLinus Torvalds * make sure SET_PROCTITLE works. Also removed
141da177e4SLinus Torvalds * bad '!' which forced address recalculation for
151da177e4SLinus Torvalds * EVERY character on the current page.
161da177e4SLinus Torvalds * <middelin@polyware.iaf.nl>
171da177e4SLinus Torvalds *
181da177e4SLinus Torvalds * Danny ter Haar : added cpuinfo
191da177e4SLinus Torvalds * <dth@cistron.nl>
201da177e4SLinus Torvalds *
211da177e4SLinus Torvalds * Alessandro Rubini : profile extension.
221da177e4SLinus Torvalds * <rubini@ipvvis.unipv.it>
231da177e4SLinus Torvalds *
241da177e4SLinus Torvalds * Jeff Tranter : added BogoMips field to cpuinfo
251da177e4SLinus Torvalds * <Jeff_Tranter@Mitel.COM>
261da177e4SLinus Torvalds *
271da177e4SLinus Torvalds * Bruno Haible : remove 4K limit for the maps file
281da177e4SLinus Torvalds * <haible@ma2s2.mathematik.uni-karlsruhe.de>
291da177e4SLinus Torvalds *
301da177e4SLinus Torvalds * Yves Arrouye : remove removal of trailing spaces in get_array.
311da177e4SLinus Torvalds * <Yves.Arrouye@marin.fdn.fr>
321da177e4SLinus Torvalds *
331da177e4SLinus Torvalds * Jerome Forissier : added per-CPU time information to /proc/stat
341da177e4SLinus Torvalds * and /proc/<pid>/cpu extension
351da177e4SLinus Torvalds * <forissier@isia.cma.fr>
361da177e4SLinus Torvalds * - Incorporation and non-SMP safe operation
371da177e4SLinus Torvalds * of forissier patch in 2.1.78 by
381da177e4SLinus Torvalds * Hans Marcus <crowbar@concepts.nl>
391da177e4SLinus Torvalds *
401da177e4SLinus Torvalds * aeb@cwi.nl : /proc/partitions
411da177e4SLinus Torvalds *
421da177e4SLinus Torvalds *
431da177e4SLinus Torvalds * Alan Cox : security fixes.
44526719baSAlan Cox * <alan@lxorguk.ukuu.org.uk>
451da177e4SLinus Torvalds *
461da177e4SLinus Torvalds * Al Viro : safe handling of mm_struct
471da177e4SLinus Torvalds *
481da177e4SLinus Torvalds * Gerhard Wichert : added BIGMEM support
491da177e4SLinus Torvalds * Siemens AG <Gerhard.Wichert@pdb.siemens.de>
501da177e4SLinus Torvalds *
511da177e4SLinus Torvalds * Al Viro & Jeff Garzik : moved most of the thing into base.c and
521da177e4SLinus Torvalds * : proc_misc.c. The rest may eventually go into
531da177e4SLinus Torvalds * : base.c too.
541da177e4SLinus Torvalds */
551da177e4SLinus Torvalds
561da177e4SLinus Torvalds #include <linux/types.h>
571da177e4SLinus Torvalds #include <linux/errno.h>
581da177e4SLinus Torvalds #include <linux/time.h>
593ae700ecSMichael Weiß #include <linux/time_namespace.h>
601da177e4SLinus Torvalds #include <linux/kernel.h>
611da177e4SLinus Torvalds #include <linux/kernel_stat.h>
621da177e4SLinus Torvalds #include <linux/tty.h>
631da177e4SLinus Torvalds #include <linux/string.h>
641da177e4SLinus Torvalds #include <linux/mman.h>
656e84f315SIngo Molnar #include <linux/sched/mm.h>
666a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h>
67fd7d5627SJohn Ogness #include <linux/sched/task_stack.h>
6829930025SIngo Molnar #include <linux/sched/task.h>
6932ef5517SIngo Molnar #include <linux/sched/cputime.h>
701da177e4SLinus Torvalds #include <linux/proc_fs.h>
711da177e4SLinus Torvalds #include <linux/ioport.h>
728ea02606SIngo Molnar #include <linux/io.h>
731da177e4SLinus Torvalds #include <linux/mm.h>
741da177e4SLinus Torvalds #include <linux/hugetlb.h>
751da177e4SLinus Torvalds #include <linux/pagemap.h>
761da177e4SLinus Torvalds #include <linux/swap.h>
771da177e4SLinus Torvalds #include <linux/smp.h>
781da177e4SLinus Torvalds #include <linux/signal.h>
791da177e4SLinus Torvalds #include <linux/highmem.h>
801da177e4SLinus Torvalds #include <linux/file.h>
819f3acc31SAl Viro #include <linux/fdtable.h>
821da177e4SLinus Torvalds #include <linux/times.h>
831da177e4SLinus Torvalds #include <linux/cpuset.h>
844fb3a538SDipankar Sarma #include <linux/rcupdate.h>
8525890454SShailabh Nagar #include <linux/delayacct.h>
86ee992744SEric W. Biederman #include <linux/seq_file.h>
87b488893aSPavel Emelyanov #include <linux/pid_namespace.h>
88fae1fa0fSKees Cook #include <linux/prctl.h>
89f83ce3e6SJake Edge #include <linux/ptrace.h>
90edc924e0SAndy Shevchenko #include <linux/string_helpers.h>
91ae2975bcSEric W. Biederman #include <linux/user_namespace.h>
923e42979eSRichard W.M. Jones #include <linux/fs_struct.h>
93d6986ce2SYafang Shao #include <linux/kthread.h>
94f7d30434SKirill A. Shutemov #include <linux/mmu_context.h>
951da177e4SLinus Torvalds
961da177e4SLinus Torvalds #include <asm/processor.h>
971da177e4SLinus Torvalds #include "internal.h"
981da177e4SLinus Torvalds
proc_task_name(struct seq_file * m,struct task_struct * p,bool escape)9988b72b31STejun Heo void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape)
1001da177e4SLinus Torvalds {
1016b59808bSTejun Heo char tcomm[64];
1021da177e4SLinus Torvalds
103ed8fb78dSAlexey Dobriyan /*
104ed8fb78dSAlexey Dobriyan * Test before PF_KTHREAD because all workqueue worker threads are
105ed8fb78dSAlexey Dobriyan * kernel threads.
106ed8fb78dSAlexey Dobriyan */
1076b59808bSTejun Heo if (p->flags & PF_WQ_WORKER)
1086b59808bSTejun Heo wq_worker_comm(tcomm, sizeof(tcomm), p);
109d6986ce2SYafang Shao else if (p->flags & PF_KTHREAD)
110d6986ce2SYafang Shao get_kthread_comm(tcomm, sizeof(tcomm), p);
1116b59808bSTejun Heo else
1126b59808bSTejun Heo __get_task_comm(tcomm, sizeof(tcomm), p);
113edc924e0SAndy Shevchenko
1148d23b208SChristoph Hellwig if (escape)
1158d23b208SChristoph Hellwig seq_escape_str(m, tcomm, ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\");
1168d23b208SChristoph Hellwig else
1178d23b208SChristoph Hellwig seq_printf(m, "%.64s", tcomm);
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds
1201da177e4SLinus Torvalds /*
1211da177e4SLinus Torvalds * The task state array is a strange "bitmap" of
1221da177e4SLinus Torvalds * reasons to sleep. Thus "running" is zero, and
1231da177e4SLinus Torvalds * you can test for combinations of others with
1241da177e4SLinus Torvalds * simple bit tests.
1251da177e4SLinus Torvalds */
126e130aa70SMike Frysinger static const char * const task_state_array[] = {
12706eb6184SPeter Zijlstra
12806eb6184SPeter Zijlstra /* states in TASK_REPORT: */
12906eb6184SPeter Zijlstra "R (running)", /* 0x00 */
13006eb6184SPeter Zijlstra "S (sleeping)", /* 0x01 */
13106eb6184SPeter Zijlstra "D (disk sleep)", /* 0x02 */
13206eb6184SPeter Zijlstra "T (stopped)", /* 0x04 */
13306eb6184SPeter Zijlstra "t (tracing stop)", /* 0x08 */
13406eb6184SPeter Zijlstra "X (dead)", /* 0x10 */
13506eb6184SPeter Zijlstra "Z (zombie)", /* 0x20 */
1368ef9925bSPeter Zijlstra "P (parked)", /* 0x40 */
13706eb6184SPeter Zijlstra
13806eb6184SPeter Zijlstra /* states beyond TASK_REPORT: */
1398ef9925bSPeter Zijlstra "I (idle)", /* 0x80 */
1401da177e4SLinus Torvalds };
1411da177e4SLinus Torvalds
get_task_state(struct task_struct * tsk)1421da177e4SLinus Torvalds static inline const char *get_task_state(struct task_struct *tsk)
1431da177e4SLinus Torvalds {
14406eb6184SPeter Zijlstra BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != ARRAY_SIZE(task_state_array));
1451d48b080SPeter Zijlstra return task_state_array[task_state_index(tsk)];
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds
task_state(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * p)148df5f8314SEric W. Biederman static inline void task_state(struct seq_file *m, struct pid_namespace *ns,
149df5f8314SEric W. Biederman struct pid *pid, struct task_struct *p)
1501da177e4SLinus Torvalds {
151e9f238c3SEric W. Biederman struct user_namespace *user_ns = seq_user_ns(m);
1521da177e4SLinus Torvalds struct group_info *group_info;
15368c3411fSMateusz Guzik int g, umask = -1;
154abdba6e9SOleg Nesterov struct task_struct *tracer;
155c69e8d9cSDavid Howells const struct cred *cred;
156abdba6e9SOleg Nesterov pid_t ppid, tpid = 0, tgid, ngid;
1570f4a0d53SOleg Nesterov unsigned int max_fds = 0;
1581da177e4SLinus Torvalds
159b0fa9db6SOleg Nesterov rcu_read_lock();
160b488893aSPavel Emelyanov ppid = pid_alive(p) ?
161b488893aSPavel Emelyanov task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0;
162abdba6e9SOleg Nesterov
163abdba6e9SOleg Nesterov tracer = ptrace_parent(p);
1640d094efeSRoland McGrath if (tracer)
1650d094efeSRoland McGrath tpid = task_pid_nr_ns(tracer, ns);
166b0fafc11SOleg Nesterov
167b0fafc11SOleg Nesterov tgid = task_tgid_nr_ns(p, ns);
168b0fafc11SOleg Nesterov ngid = task_numa_group_id(p);
169de09a977SDavid Howells cred = get_task_cred(p);
1700f4a0d53SOleg Nesterov
1710f4a0d53SOleg Nesterov task_lock(p);
17268c3411fSMateusz Guzik if (p->fs)
17368c3411fSMateusz Guzik umask = p->fs->umask;
1740f4a0d53SOleg Nesterov if (p->files)
1750f4a0d53SOleg Nesterov max_fds = files_fdtable(p->files)->max_fds;
1760f4a0d53SOleg Nesterov task_unlock(p);
177b0fafc11SOleg Nesterov rcu_read_unlock();
1780f4a0d53SOleg Nesterov
17968c3411fSMateusz Guzik if (umask >= 0)
18068c3411fSMateusz Guzik seq_printf(m, "Umask:\t%#04o\n", umask);
181d0f02231SAndrei Vagin seq_puts(m, "State:\t");
182d0f02231SAndrei Vagin seq_puts(m, get_task_state(p));
1831da177e4SLinus Torvalds
18475ba1d07SJoe Perches seq_put_decimal_ull(m, "\nTgid:\t", tgid);
18575ba1d07SJoe Perches seq_put_decimal_ull(m, "\nNgid:\t", ngid);
18675ba1d07SJoe Perches seq_put_decimal_ull(m, "\nPid:\t", pid_nr_ns(pid, ns));
18775ba1d07SJoe Perches seq_put_decimal_ull(m, "\nPPid:\t", ppid);
18875ba1d07SJoe Perches seq_put_decimal_ull(m, "\nTracerPid:\t", tpid);
18975ba1d07SJoe Perches seq_put_decimal_ull(m, "\nUid:\t", from_kuid_munged(user_ns, cred->uid));
19075ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->euid));
19175ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->suid));
19275ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->fsuid));
19375ba1d07SJoe Perches seq_put_decimal_ull(m, "\nGid:\t", from_kgid_munged(user_ns, cred->gid));
19475ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->egid));
19575ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->sgid));
19675ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->fsgid));
19775ba1d07SJoe Perches seq_put_decimal_ull(m, "\nFDSize:\t", max_fds);
198f7a5f132SAlexey Dobriyan
199f7a5f132SAlexey Dobriyan seq_puts(m, "\nGroups:\t");
200c69e8d9cSDavid Howells group_info = cred->group_info;
2018d238027SArtem Bityutskiy for (g = 0; g < group_info->ngroups; g++)
20275ba1d07SJoe Perches seq_put_decimal_ull(m, g ? " " : "",
20381243eacSAlexey Dobriyan from_kgid_munged(user_ns, group_info->gid[g]));
204c69e8d9cSDavid Howells put_cred(cred);
205f7a5f132SAlexey Dobriyan /* Trailing space shouldn't have been added in the first place. */
206f7a5f132SAlexey Dobriyan seq_putc(m, ' ');
2071da177e4SLinus Torvalds
208e4bc3324SChen Hanxiao #ifdef CONFIG_PID_NS
209e4bc3324SChen Hanxiao seq_puts(m, "\nNStgid:");
210e4bc3324SChen Hanxiao for (g = ns->level; g <= pid->level; g++)
21175ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns));
212e4bc3324SChen Hanxiao seq_puts(m, "\nNSpid:");
213e4bc3324SChen Hanxiao for (g = ns->level; g <= pid->level; g++)
21475ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns));
215e4bc3324SChen Hanxiao seq_puts(m, "\nNSpgid:");
216e4bc3324SChen Hanxiao for (g = ns->level; g <= pid->level; g++)
21775ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns));
218e4bc3324SChen Hanxiao seq_puts(m, "\nNSsid:");
219e4bc3324SChen Hanxiao for (g = ns->level; g <= pid->level; g++)
22075ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns));
221e4bc3324SChen Hanxiao #endif
2229d6de12fSAlexey Dobriyan seq_putc(m, '\n');
223522dc4e5SChunguang Wu
224522dc4e5SChunguang Wu seq_printf(m, "Kthread:\t%c\n", p->flags & PF_KTHREAD ? '1' : '0');
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds
render_sigset_t(struct seq_file * m,const char * header,sigset_t * set)227138d22b5SCyrill Gorcunov void render_sigset_t(struct seq_file *m, const char *header,
228df5f8314SEric W. Biederman sigset_t *set)
2291da177e4SLinus Torvalds {
230df5f8314SEric W. Biederman int i;
2311da177e4SLinus Torvalds
2329d6de12fSAlexey Dobriyan seq_puts(m, header);
2331da177e4SLinus Torvalds
2341da177e4SLinus Torvalds i = _NSIG;
2351da177e4SLinus Torvalds do {
2361da177e4SLinus Torvalds int x = 0;
2371da177e4SLinus Torvalds
2381da177e4SLinus Torvalds i -= 4;
2391da177e4SLinus Torvalds if (sigismember(set, i+1)) x |= 1;
2401da177e4SLinus Torvalds if (sigismember(set, i+2)) x |= 2;
2411da177e4SLinus Torvalds if (sigismember(set, i+3)) x |= 4;
2421da177e4SLinus Torvalds if (sigismember(set, i+4)) x |= 8;
243209b14dcSRasmus Villemoes seq_putc(m, hex_asc[x]);
2441da177e4SLinus Torvalds } while (i >= 4);
2451da177e4SLinus Torvalds
2469d6de12fSAlexey Dobriyan seq_putc(m, '\n');
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds
collect_sigign_sigcatch(struct task_struct * p,sigset_t * sigign,sigset_t * sigcatch)2498977a27bSAlexey Dobriyan static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *sigign,
2508977a27bSAlexey Dobriyan sigset_t *sigcatch)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds struct k_sigaction *k;
2531da177e4SLinus Torvalds int i;
2541da177e4SLinus Torvalds
2551da177e4SLinus Torvalds k = p->sighand->action;
2561da177e4SLinus Torvalds for (i = 1; i <= _NSIG; ++i, ++k) {
2571da177e4SLinus Torvalds if (k->sa.sa_handler == SIG_IGN)
2588977a27bSAlexey Dobriyan sigaddset(sigign, i);
2591da177e4SLinus Torvalds else if (k->sa.sa_handler != SIG_DFL)
2608977a27bSAlexey Dobriyan sigaddset(sigcatch, i);
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds
task_sig(struct seq_file * m,struct task_struct * p)264df5f8314SEric W. Biederman static inline void task_sig(struct seq_file *m, struct task_struct *p)
2651da177e4SLinus Torvalds {
2665e6b3f42SOleg Nesterov unsigned long flags;
2671da177e4SLinus Torvalds sigset_t pending, shpending, blocked, ignored, caught;
2681da177e4SLinus Torvalds int num_threads = 0;
269197850a1SAlexey Dobriyan unsigned int qsize = 0;
2701da177e4SLinus Torvalds unsigned long qlim = 0;
2711da177e4SLinus Torvalds
2721da177e4SLinus Torvalds sigemptyset(&pending);
2731da177e4SLinus Torvalds sigemptyset(&shpending);
2741da177e4SLinus Torvalds sigemptyset(&blocked);
2751da177e4SLinus Torvalds sigemptyset(&ignored);
2761da177e4SLinus Torvalds sigemptyset(&caught);
2771da177e4SLinus Torvalds
2785e6b3f42SOleg Nesterov if (lock_task_sighand(p, &flags)) {
2791da177e4SLinus Torvalds pending = p->pending.signal;
2801da177e4SLinus Torvalds shpending = p->signal->shared_pending.signal;
2811da177e4SLinus Torvalds blocked = p->blocked;
2821da177e4SLinus Torvalds collect_sigign_sigcatch(p, &ignored, &caught);
2837e49827cSOleg Nesterov num_threads = get_nr_threads(p);
2847dc52157SPaul E. McKenney rcu_read_lock(); /* FIXME: is this correct? */
285de399236SAlexey Gladkov qsize = get_rlimit_value(task_ucounts(p), UCOUNT_RLIMIT_SIGPENDING);
2867dc52157SPaul E. McKenney rcu_read_unlock();
287d554ed89SJiri Slaby qlim = task_rlimit(p, RLIMIT_SIGPENDING);
2885e6b3f42SOleg Nesterov unlock_task_sighand(p, &flags);
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds
29175ba1d07SJoe Perches seq_put_decimal_ull(m, "Threads:\t", num_threads);
29275ba1d07SJoe Perches seq_put_decimal_ull(m, "\nSigQ:\t", qsize);
29375ba1d07SJoe Perches seq_put_decimal_ull(m, "/", qlim);
2941da177e4SLinus Torvalds
2951da177e4SLinus Torvalds /* render them all */
296f7a5f132SAlexey Dobriyan render_sigset_t(m, "\nSigPnd:\t", &pending);
297df5f8314SEric W. Biederman render_sigset_t(m, "ShdPnd:\t", &shpending);
298df5f8314SEric W. Biederman render_sigset_t(m, "SigBlk:\t", &blocked);
299df5f8314SEric W. Biederman render_sigset_t(m, "SigIgn:\t", &ignored);
300df5f8314SEric W. Biederman render_sigset_t(m, "SigCgt:\t", &caught);
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds
render_cap_t(struct seq_file * m,const char * header,kernel_cap_t * a)303df5f8314SEric W. Biederman static void render_cap_t(struct seq_file *m, const char *header,
304df5f8314SEric W. Biederman kernel_cap_t *a)
305e338d263SAndrew Morgan {
3069d6de12fSAlexey Dobriyan seq_puts(m, header);
307f122a08bSLinus Torvalds seq_put_hex_ll(m, NULL, a->val, 16);
3089d6de12fSAlexey Dobriyan seq_putc(m, '\n');
309e338d263SAndrew Morgan }
310e338d263SAndrew Morgan
task_cap(struct seq_file * m,struct task_struct * p)311df5f8314SEric W. Biederman static inline void task_cap(struct seq_file *m, struct task_struct *p)
3121da177e4SLinus Torvalds {
313c69e8d9cSDavid Howells const struct cred *cred;
31458319057SAndy Lutomirski kernel_cap_t cap_inheritable, cap_permitted, cap_effective,
31558319057SAndy Lutomirski cap_bset, cap_ambient;
316b6dff3ecSDavid Howells
317c69e8d9cSDavid Howells rcu_read_lock();
318c69e8d9cSDavid Howells cred = __task_cred(p);
319c69e8d9cSDavid Howells cap_inheritable = cred->cap_inheritable;
320c69e8d9cSDavid Howells cap_permitted = cred->cap_permitted;
321c69e8d9cSDavid Howells cap_effective = cred->cap_effective;
322c69e8d9cSDavid Howells cap_bset = cred->cap_bset;
32358319057SAndy Lutomirski cap_ambient = cred->cap_ambient;
324c69e8d9cSDavid Howells rcu_read_unlock();
325c69e8d9cSDavid Howells
326c69e8d9cSDavid Howells render_cap_t(m, "CapInh:\t", &cap_inheritable);
327c69e8d9cSDavid Howells render_cap_t(m, "CapPrm:\t", &cap_permitted);
328c69e8d9cSDavid Howells render_cap_t(m, "CapEff:\t", &cap_effective);
329c69e8d9cSDavid Howells render_cap_t(m, "CapBnd:\t", &cap_bset);
33058319057SAndy Lutomirski render_cap_t(m, "CapAmb:\t", &cap_ambient);
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds
task_seccomp(struct seq_file * m,struct task_struct * p)3332f4b3bf6SKees Cook static inline void task_seccomp(struct seq_file *m, struct task_struct *p)
3342f4b3bf6SKees Cook {
335af884cd4SKees Cook seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p));
3362f4b3bf6SKees Cook #ifdef CONFIG_SECCOMP
337af884cd4SKees Cook seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode);
33864bdc024SKenta.Tada@sony.com #ifdef CONFIG_SECCOMP_FILTER
339c818c03bSKees Cook seq_put_decimal_ull(m, "\nSeccomp_filters:\t",
340c818c03bSKees Cook atomic_read(&p->seccomp.filter_count));
3412f4b3bf6SKees Cook #endif
34264bdc024SKenta.Tada@sony.com #endif
34308b55775SAlexey Dobriyan seq_puts(m, "\nSpeculation_Store_Bypass:\t");
344fae1fa0fSKees Cook switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) {
345fae1fa0fSKees Cook case -EINVAL:
34608b55775SAlexey Dobriyan seq_puts(m, "unknown");
347fae1fa0fSKees Cook break;
348fae1fa0fSKees Cook case PR_SPEC_NOT_AFFECTED:
34908b55775SAlexey Dobriyan seq_puts(m, "not vulnerable");
350fae1fa0fSKees Cook break;
351356e4bffSThomas Gleixner case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
35208b55775SAlexey Dobriyan seq_puts(m, "thread force mitigated");
353356e4bffSThomas Gleixner break;
354fae1fa0fSKees Cook case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
35508b55775SAlexey Dobriyan seq_puts(m, "thread mitigated");
356fae1fa0fSKees Cook break;
357fae1fa0fSKees Cook case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
35808b55775SAlexey Dobriyan seq_puts(m, "thread vulnerable");
359fae1fa0fSKees Cook break;
360fae1fa0fSKees Cook case PR_SPEC_DISABLE:
36108b55775SAlexey Dobriyan seq_puts(m, "globally mitigated");
362fae1fa0fSKees Cook break;
363fae1fa0fSKees Cook default:
36408b55775SAlexey Dobriyan seq_puts(m, "vulnerable");
365fae1fa0fSKees Cook break;
366fae1fa0fSKees Cook }
367fe719888SAnand K Mistry
368fe719888SAnand K Mistry seq_puts(m, "\nSpeculationIndirectBranch:\t");
369fe719888SAnand K Mistry switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_INDIRECT_BRANCH)) {
370fe719888SAnand K Mistry case -EINVAL:
371fe719888SAnand K Mistry seq_puts(m, "unsupported");
372fe719888SAnand K Mistry break;
373fe719888SAnand K Mistry case PR_SPEC_NOT_AFFECTED:
374fe719888SAnand K Mistry seq_puts(m, "not affected");
375fe719888SAnand K Mistry break;
376fe719888SAnand K Mistry case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE:
377fe719888SAnand K Mistry seq_puts(m, "conditional force disabled");
378fe719888SAnand K Mistry break;
379fe719888SAnand K Mistry case PR_SPEC_PRCTL | PR_SPEC_DISABLE:
380fe719888SAnand K Mistry seq_puts(m, "conditional disabled");
381fe719888SAnand K Mistry break;
382fe719888SAnand K Mistry case PR_SPEC_PRCTL | PR_SPEC_ENABLE:
383fe719888SAnand K Mistry seq_puts(m, "conditional enabled");
384fe719888SAnand K Mistry break;
385fe719888SAnand K Mistry case PR_SPEC_ENABLE:
386fe719888SAnand K Mistry seq_puts(m, "always enabled");
387fe719888SAnand K Mistry break;
388fe719888SAnand K Mistry case PR_SPEC_DISABLE:
389fe719888SAnand K Mistry seq_puts(m, "always disabled");
390fe719888SAnand K Mistry break;
391fe719888SAnand K Mistry default:
392fe719888SAnand K Mistry seq_puts(m, "unknown");
393fe719888SAnand K Mistry break;
394fe719888SAnand K Mistry }
395af884cd4SKees Cook seq_putc(m, '\n');
3962f4b3bf6SKees Cook }
3972f4b3bf6SKees Cook
task_context_switch_counts(struct seq_file * m,struct task_struct * p)398df5f8314SEric W. Biederman static inline void task_context_switch_counts(struct seq_file *m,
399df5f8314SEric W. Biederman struct task_struct *p)
400b663a79cSMaxim Uvarov {
40175ba1d07SJoe Perches seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw);
40275ba1d07SJoe Perches seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw);
403f7a5f132SAlexey Dobriyan seq_putc(m, '\n');
404b663a79cSMaxim Uvarov }
405b663a79cSMaxim Uvarov
task_cpus_allowed(struct seq_file * m,struct task_struct * task)406d01d4827SHeiko Carstens static void task_cpus_allowed(struct seq_file *m, struct task_struct *task)
407d01d4827SHeiko Carstens {
408a0c2e07dSTejun Heo seq_printf(m, "Cpus_allowed:\t%*pb\n",
40986fbcd3bSPeter Zijlstra cpumask_pr_args(&task->cpus_mask));
410a0c2e07dSTejun Heo seq_printf(m, "Cpus_allowed_list:\t%*pbl\n",
41186fbcd3bSPeter Zijlstra cpumask_pr_args(&task->cpus_mask));
412d01d4827SHeiko Carstens }
413d01d4827SHeiko Carstens
task_core_dumping(struct seq_file * m,struct task_struct * task)4140258b5fdSEric W. Biederman static inline void task_core_dumping(struct seq_file *m, struct task_struct *task)
415c6434012SRoman Gushchin {
4160258b5fdSEric W. Biederman seq_put_decimal_ull(m, "CoreDumping:\t", !!task->signal->core_state);
417d0f02231SAndrei Vagin seq_putc(m, '\n');
418c6434012SRoman Gushchin }
419c6434012SRoman Gushchin
task_thp_status(struct seq_file * m,struct mm_struct * mm)420a1400af7SMichal Hocko static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm)
421a1400af7SMichal Hocko {
422a1400af7SMichal Hocko bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE);
423a1400af7SMichal Hocko
424a1400af7SMichal Hocko if (thp_enabled)
425a1400af7SMichal Hocko thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags);
426a1400af7SMichal Hocko seq_printf(m, "THP_enabled:\t%d\n", thp_enabled);
427a1400af7SMichal Hocko }
428a1400af7SMichal Hocko
task_untag_mask(struct seq_file * m,struct mm_struct * mm)429f7d30434SKirill A. Shutemov static inline void task_untag_mask(struct seq_file *m, struct mm_struct *mm)
430f7d30434SKirill A. Shutemov {
431f7d30434SKirill A. Shutemov seq_printf(m, "untag_mask:\t%#lx\n", mm_untag_mask(mm));
432f7d30434SKirill A. Shutemov }
433f7d30434SKirill A. Shutemov
arch_proc_pid_thread_features(struct seq_file * m,struct task_struct * task)4340ee44885SRick Edgecombe __weak void arch_proc_pid_thread_features(struct seq_file *m,
4350ee44885SRick Edgecombe struct task_struct *task)
4360ee44885SRick Edgecombe {
4370ee44885SRick Edgecombe }
4380ee44885SRick Edgecombe
proc_pid_status(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)439df5f8314SEric W. Biederman int proc_pid_status(struct seq_file *m, struct pid_namespace *ns,
440df5f8314SEric W. Biederman struct pid *pid, struct task_struct *task)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task);
4431da177e4SLinus Torvalds
44488b72b31STejun Heo seq_puts(m, "Name:\t");
44588b72b31STejun Heo proc_task_name(m, task, true);
44688b72b31STejun Heo seq_putc(m, '\n');
44788b72b31STejun Heo
448df5f8314SEric W. Biederman task_state(m, ns, pid, task);
4491da177e4SLinus Torvalds
4501da177e4SLinus Torvalds if (mm) {
451df5f8314SEric W. Biederman task_mem(m, mm);
4520258b5fdSEric W. Biederman task_core_dumping(m, task);
453a1400af7SMichal Hocko task_thp_status(m, mm);
454f7d30434SKirill A. Shutemov task_untag_mask(m, mm);
4551da177e4SLinus Torvalds mmput(mm);
4561da177e4SLinus Torvalds }
457df5f8314SEric W. Biederman task_sig(m, task);
458df5f8314SEric W. Biederman task_cap(m, task);
4592f4b3bf6SKees Cook task_seccomp(m, task);
460d01d4827SHeiko Carstens task_cpus_allowed(m, task);
461df5f8314SEric W. Biederman cpuset_task_status_allowed(m, task);
462df5f8314SEric W. Biederman task_context_switch_counts(m, task);
4630ee44885SRick Edgecombe arch_proc_pid_thread_features(m, task);
464df5f8314SEric W. Biederman return 0;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds
do_task_stat(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task,int whole)467ee992744SEric W. Biederman static int do_task_stat(struct seq_file *m, struct pid_namespace *ns,
468ee992744SEric W. Biederman struct pid *pid, struct task_struct *task, int whole)
4691da177e4SLinus Torvalds {
470b2f73922SIngo Molnar unsigned long vsize, eip, esp, wchan = 0;
471715be1fcSJan Engelhardt int priority, nice;
4721da177e4SLinus Torvalds int tty_pgrp = -1, tty_nr = 0;
4731da177e4SLinus Torvalds sigset_t sigign, sigcatch;
4741da177e4SLinus Torvalds char state;
475a593d6edSOleg Nesterov pid_t ppid = 0, pgid = -1, sid = -1;
4761da177e4SLinus Torvalds int num_threads = 0;
477f83ce3e6SJake Edge int permitted;
4781da177e4SLinus Torvalds struct mm_struct *mm;
4791da177e4SLinus Torvalds unsigned long long start_time;
4803820b0faSOleg Nesterov unsigned long cmin_flt, cmaj_flt, min_flt, maj_flt;
4813820b0faSOleg Nesterov u64 cutime, cstime, cgtime, utime, stime, gtime;
4821da177e4SLinus Torvalds unsigned long rsslim = 0;
483a593d6edSOleg Nesterov unsigned long flags;
4842d18f7f4SEric W. Biederman int exit_code = task->exit_code;
4853820b0faSOleg Nesterov struct signal_struct *sig = task->signal;
4863820b0faSOleg Nesterov unsigned int seq = 1;
4871da177e4SLinus Torvalds
4881da177e4SLinus Torvalds state = *get_task_state(task);
4891da177e4SLinus Torvalds vsize = eip = esp = 0;
490caaee623SJann Horn permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT);
4911da177e4SLinus Torvalds mm = get_task_mm(task);
4921da177e4SLinus Torvalds if (mm) {
4931da177e4SLinus Torvalds vsize = task_vsize(mm);
4940a1eb2d4SAndy Lutomirski /*
4950a1eb2d4SAndy Lutomirski * esp and eip are intentionally zeroed out. There is no
4960a1eb2d4SAndy Lutomirski * non-racy way to read them without freezing the task.
4970a1eb2d4SAndy Lutomirski * Programs that need reliable values can use ptrace(2).
498fd7d5627SJohn Ogness *
499fd7d5627SJohn Ogness * The only exception is if the task is core dumping because
500fd7d5627SJohn Ogness * a program is not able to use ptrace(2) in that case. It is
501fd7d5627SJohn Ogness * safe because the task has stopped executing permanently.
5020a1eb2d4SAndy Lutomirski */
503*553fbae0SNam Cao if (permitted && (task->flags & (PF_EXITING|PF_DUMPCORE|PF_POSTCOREDUMP))) {
5048bb2ee19SAlexey Dobriyan if (try_get_task_stack(task)) {
505fd7d5627SJohn Ogness eip = KSTK_EIP(task);
506fd7d5627SJohn Ogness esp = KSTK_ESP(task);
5078bb2ee19SAlexey Dobriyan put_task_stack(task);
5088bb2ee19SAlexey Dobriyan }
509fd7d5627SJohn Ogness }
510f83ce3e6SJake Edge }
5111da177e4SLinus Torvalds
5121da177e4SLinus Torvalds sigemptyset(&sigign);
5131da177e4SLinus Torvalds sigemptyset(&sigcatch);
5143cfd0885SAlan Cox
515a593d6edSOleg Nesterov if (lock_task_sighand(task, &flags)) {
51691593504SOleg Nesterov if (sig->tty) {
5175d0fdf1eSAlan Cox struct pid *pgrp = tty_get_pgrp(sig->tty);
5185d0fdf1eSAlan Cox tty_pgrp = pid_nr_ns(pgrp, ns);
5195d0fdf1eSAlan Cox put_pid(pgrp);
52091593504SOleg Nesterov tty_nr = new_encode_dev(tty_devnum(sig->tty));
521a593d6edSOleg Nesterov }
522a593d6edSOleg Nesterov
5237e49827cSOleg Nesterov num_threads = get_nr_threads(task);
5241da177e4SLinus Torvalds collect_sigign_sigcatch(task, &sigign, &sigcatch);
5251da177e4SLinus Torvalds
5266aa7de05SMark Rutland rsslim = READ_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur);
527a593d6edSOleg Nesterov
5281da177e4SLinus Torvalds if (whole) {
5292d18f7f4SEric W. Biederman if (sig->flags & (SIGNAL_GROUP_EXIT | SIGNAL_STOP_STOPPED))
5302d18f7f4SEric W. Biederman exit_code = sig->group_exit_code;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds
533b488893aSPavel Emelyanov sid = task_session_nr_ns(task, ns);
534a98fdcefSOleg Nesterov ppid = task_tgid_nr_ns(task->real_parent, ns);
535b488893aSPavel Emelyanov pgid = task_pgrp_nr_ns(task, ns);
536a593d6edSOleg Nesterov
537a593d6edSOleg Nesterov unlock_task_sighand(task, &flags);
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds
540f83ce3e6SJake Edge if (permitted && (!whole || num_threads < 2))
5414e046156SKees Cook wchan = !task_is_running(task);
5425d858e2dSOleg Nesterov
5433820b0faSOleg Nesterov do {
5443820b0faSOleg Nesterov seq++; /* 2 on the 1st/lockless path, otherwise odd */
5453820b0faSOleg Nesterov flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
5463820b0faSOleg Nesterov
5473820b0faSOleg Nesterov cmin_flt = sig->cmin_flt;
5483820b0faSOleg Nesterov cmaj_flt = sig->cmaj_flt;
5493820b0faSOleg Nesterov cutime = sig->cutime;
5503820b0faSOleg Nesterov cstime = sig->cstime;
5513820b0faSOleg Nesterov cgtime = sig->cgtime;
5523820b0faSOleg Nesterov
5533820b0faSOleg Nesterov if (whole) {
5543820b0faSOleg Nesterov struct task_struct *t;
5553820b0faSOleg Nesterov
5563820b0faSOleg Nesterov min_flt = sig->min_flt;
5573820b0faSOleg Nesterov maj_flt = sig->maj_flt;
5583820b0faSOleg Nesterov gtime = sig->gtime;
5593820b0faSOleg Nesterov
5603820b0faSOleg Nesterov rcu_read_lock();
5613820b0faSOleg Nesterov __for_each_thread(sig, t) {
5623820b0faSOleg Nesterov min_flt += t->min_flt;
5633820b0faSOleg Nesterov maj_flt += t->maj_flt;
5643820b0faSOleg Nesterov gtime += task_gtime(t);
5653820b0faSOleg Nesterov }
5663820b0faSOleg Nesterov rcu_read_unlock();
5673820b0faSOleg Nesterov }
5683820b0faSOleg Nesterov } while (need_seqretry(&sig->stats_lock, seq));
5693820b0faSOleg Nesterov done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
5703820b0faSOleg Nesterov
5715d858e2dSOleg Nesterov if (whole) {
5725d858e2dSOleg Nesterov thread_group_cputime_adjusted(task, &utime, &stime);
5735d858e2dSOleg Nesterov } else {
5745d858e2dSOleg Nesterov task_cputime_adjusted(task, &utime, &stime);
5751da177e4SLinus Torvalds min_flt = task->min_flt;
5761da177e4SLinus Torvalds maj_flt = task->maj_flt;
5776fac4829SFrederic Weisbecker gtime = task_gtime(task);
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds
5801da177e4SLinus Torvalds /* scale priority and nice values from timeslices to -20..20 */
5811da177e4SLinus Torvalds /* to make it look like a "normal" Unix priority/nice value */
5821da177e4SLinus Torvalds priority = task_prio(task);
5831da177e4SLinus Torvalds nice = task_nice(task);
5841da177e4SLinus Torvalds
5853ae700ecSMichael Weiß /* apply timens offset for boottime and convert nsec -> ticks */
5863ae700ecSMichael Weiß start_time =
5873ae700ecSMichael Weiß nsec_to_clock_t(timens_add_boottime_ns(task->start_boottime));
5881da177e4SLinus Torvalds
589d0f02231SAndrei Vagin seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns));
590d0f02231SAndrei Vagin seq_puts(m, " (");
59188b72b31STejun Heo proc_task_name(m, task, false);
592d0f02231SAndrei Vagin seq_puts(m, ") ");
593d0f02231SAndrei Vagin seq_putc(m, state);
59475ba1d07SJoe Perches seq_put_decimal_ll(m, " ", ppid);
59575ba1d07SJoe Perches seq_put_decimal_ll(m, " ", pgid);
59675ba1d07SJoe Perches seq_put_decimal_ll(m, " ", sid);
59775ba1d07SJoe Perches seq_put_decimal_ll(m, " ", tty_nr);
59875ba1d07SJoe Perches seq_put_decimal_ll(m, " ", tty_pgrp);
59975ba1d07SJoe Perches seq_put_decimal_ull(m, " ", task->flags);
60075ba1d07SJoe Perches seq_put_decimal_ull(m, " ", min_flt);
60175ba1d07SJoe Perches seq_put_decimal_ull(m, " ", cmin_flt);
60275ba1d07SJoe Perches seq_put_decimal_ull(m, " ", maj_flt);
60375ba1d07SJoe Perches seq_put_decimal_ull(m, " ", cmaj_flt);
6045613fda9SFrederic Weisbecker seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime));
6055613fda9SFrederic Weisbecker seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime));
6065613fda9SFrederic Weisbecker seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime));
6075613fda9SFrederic Weisbecker seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime));
60875ba1d07SJoe Perches seq_put_decimal_ll(m, " ", priority);
60975ba1d07SJoe Perches seq_put_decimal_ll(m, " ", nice);
61075ba1d07SJoe Perches seq_put_decimal_ll(m, " ", num_threads);
61175ba1d07SJoe Perches seq_put_decimal_ull(m, " ", 0);
61275ba1d07SJoe Perches seq_put_decimal_ull(m, " ", start_time);
61375ba1d07SJoe Perches seq_put_decimal_ull(m, " ", vsize);
61475ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0);
61575ba1d07SJoe Perches seq_put_decimal_ull(m, " ", rsslim);
61675ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0);
61775ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0);
61875ba1d07SJoe Perches seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0);
61975ba1d07SJoe Perches seq_put_decimal_ull(m, " ", esp);
62075ba1d07SJoe Perches seq_put_decimal_ull(m, " ", eip);
6211da177e4SLinus Torvalds /* The signal information here is obsolete.
6221da177e4SLinus Torvalds * It must be decimal for Linux 2.0 compatibility.
6231da177e4SLinus Torvalds * Use /proc/#/status for real-time signals.
6241da177e4SLinus Torvalds */
62575ba1d07SJoe Perches seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL);
62675ba1d07SJoe Perches seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL);
62775ba1d07SJoe Perches seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL);
62875ba1d07SJoe Perches seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL);
629b2f73922SIngo Molnar
630b2f73922SIngo Molnar /*
631b2f73922SIngo Molnar * We used to output the absolute kernel address, but that's an
632b2f73922SIngo Molnar * information leak - so instead we show a 0/1 flag here, to signal
633b2f73922SIngo Molnar * to user-space whether there's a wchan field in /proc/PID/wchan.
634b2f73922SIngo Molnar *
635b2f73922SIngo Molnar * This works with older implementations of procps as well.
636b2f73922SIngo Molnar */
6374e046156SKees Cook seq_put_decimal_ull(m, " ", wchan);
638b2f73922SIngo Molnar
63975ba1d07SJoe Perches seq_put_decimal_ull(m, " ", 0);
64075ba1d07SJoe Perches seq_put_decimal_ull(m, " ", 0);
64175ba1d07SJoe Perches seq_put_decimal_ll(m, " ", task->exit_signal);
64275ba1d07SJoe Perches seq_put_decimal_ll(m, " ", task_cpu(task));
64375ba1d07SJoe Perches seq_put_decimal_ull(m, " ", task->rt_priority);
64475ba1d07SJoe Perches seq_put_decimal_ull(m, " ", task->policy);
64575ba1d07SJoe Perches seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task));
64616a6d9beSFrederic Weisbecker seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime));
64716a6d9beSFrederic Weisbecker seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime));
6485b172087SCyrill Gorcunov
6495b172087SCyrill Gorcunov if (mm && permitted) {
65075ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->start_data);
65175ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->end_data);
65275ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->start_brk);
65375ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->arg_start);
65475ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->arg_end);
65575ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->env_start);
65675ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->env_end);
6575b172087SCyrill Gorcunov } else
65875ba1d07SJoe Perches seq_puts(m, " 0 0 0 0 0 0 0");
6595b172087SCyrill Gorcunov
6605b172087SCyrill Gorcunov if (permitted)
6612d18f7f4SEric W. Biederman seq_put_decimal_ll(m, " ", exit_code);
6625b172087SCyrill Gorcunov else
66375ba1d07SJoe Perches seq_puts(m, " 0");
6645b172087SCyrill Gorcunov
665bda7bad6SKAMEZAWA Hiroyuki seq_putc(m, '\n');
6661da177e4SLinus Torvalds if (mm)
6671da177e4SLinus Torvalds mmput(mm);
668ee992744SEric W. Biederman return 0;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds
proc_tid_stat(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)671ee992744SEric W. Biederman int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
672ee992744SEric W. Biederman struct pid *pid, struct task_struct *task)
6731da177e4SLinus Torvalds {
674ee992744SEric W. Biederman return do_task_stat(m, ns, pid, task, 0);
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds
proc_tgid_stat(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)677ee992744SEric W. Biederman int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,
678ee992744SEric W. Biederman struct pid *pid, struct task_struct *task)
6791da177e4SLinus Torvalds {
680ee992744SEric W. Biederman return do_task_stat(m, ns, pid, task, 1);
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds
proc_pid_statm(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)683a56d3fc7SEric W. Biederman int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns,
684a56d3fc7SEric W. Biederman struct pid *pid, struct task_struct *task)
6851da177e4SLinus Torvalds {
6861da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task);
6871da177e4SLinus Torvalds
6881da177e4SLinus Torvalds if (mm) {
6895c5ab971SAlexey Dobriyan unsigned long size;
6905c5ab971SAlexey Dobriyan unsigned long resident = 0;
6915c5ab971SAlexey Dobriyan unsigned long shared = 0;
6925c5ab971SAlexey Dobriyan unsigned long text = 0;
6935c5ab971SAlexey Dobriyan unsigned long data = 0;
6945c5ab971SAlexey Dobriyan
6951da177e4SLinus Torvalds size = task_statm(mm, &shared, &text, &data, &resident);
6961da177e4SLinus Torvalds mmput(mm);
6975c5ab971SAlexey Dobriyan
698bda7bad6SKAMEZAWA Hiroyuki /*
699bda7bad6SKAMEZAWA Hiroyuki * For quick read, open code by putting numbers directly
700bda7bad6SKAMEZAWA Hiroyuki * expected format is
701bda7bad6SKAMEZAWA Hiroyuki * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n",
702bda7bad6SKAMEZAWA Hiroyuki * size, resident, shared, text, data);
703bda7bad6SKAMEZAWA Hiroyuki */
70475ba1d07SJoe Perches seq_put_decimal_ull(m, "", size);
70575ba1d07SJoe Perches seq_put_decimal_ull(m, " ", resident);
70675ba1d07SJoe Perches seq_put_decimal_ull(m, " ", shared);
70775ba1d07SJoe Perches seq_put_decimal_ull(m, " ", text);
70875ba1d07SJoe Perches seq_put_decimal_ull(m, " ", 0);
70975ba1d07SJoe Perches seq_put_decimal_ull(m, " ", data);
71075ba1d07SJoe Perches seq_put_decimal_ull(m, " ", 0);
711bda7bad6SKAMEZAWA Hiroyuki seq_putc(m, '\n');
7125c5ab971SAlexey Dobriyan } else {
7135c5ab971SAlexey Dobriyan seq_write(m, "0 0 0 0 0 0 0\n", 14);
7145c5ab971SAlexey Dobriyan }
715a56d3fc7SEric W. Biederman return 0;
7161da177e4SLinus Torvalds }
71781841161SCyrill Gorcunov
7182e13ba54SIago López Galeiras #ifdef CONFIG_PROC_CHILDREN
71981841161SCyrill Gorcunov static struct pid *
get_children_pid(struct inode * inode,struct pid * pid_prev,loff_t pos)72081841161SCyrill Gorcunov get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos)
72181841161SCyrill Gorcunov {
72281841161SCyrill Gorcunov struct task_struct *start, *task;
72381841161SCyrill Gorcunov struct pid *pid = NULL;
72481841161SCyrill Gorcunov
72581841161SCyrill Gorcunov read_lock(&tasklist_lock);
72681841161SCyrill Gorcunov
72781841161SCyrill Gorcunov start = pid_task(proc_pid(inode), PIDTYPE_PID);
72881841161SCyrill Gorcunov if (!start)
72981841161SCyrill Gorcunov goto out;
73081841161SCyrill Gorcunov
73181841161SCyrill Gorcunov /*
73281841161SCyrill Gorcunov * Lets try to continue searching first, this gives
73381841161SCyrill Gorcunov * us significant speedup on children-rich processes.
73481841161SCyrill Gorcunov */
73581841161SCyrill Gorcunov if (pid_prev) {
73681841161SCyrill Gorcunov task = pid_task(pid_prev, PIDTYPE_PID);
73781841161SCyrill Gorcunov if (task && task->real_parent == start &&
73881841161SCyrill Gorcunov !(list_empty(&task->sibling))) {
73981841161SCyrill Gorcunov if (list_is_last(&task->sibling, &start->children))
74081841161SCyrill Gorcunov goto out;
74181841161SCyrill Gorcunov task = list_first_entry(&task->sibling,
74281841161SCyrill Gorcunov struct task_struct, sibling);
74381841161SCyrill Gorcunov pid = get_pid(task_pid(task));
74481841161SCyrill Gorcunov goto out;
74581841161SCyrill Gorcunov }
74681841161SCyrill Gorcunov }
74781841161SCyrill Gorcunov
74881841161SCyrill Gorcunov /*
74981841161SCyrill Gorcunov * Slow search case.
75081841161SCyrill Gorcunov *
75181841161SCyrill Gorcunov * We might miss some children here if children
75281841161SCyrill Gorcunov * are exited while we were not holding the lock,
75381841161SCyrill Gorcunov * but it was never promised to be accurate that
75481841161SCyrill Gorcunov * much.
75581841161SCyrill Gorcunov *
75681841161SCyrill Gorcunov * "Just suppose that the parent sleeps, but N children
75781841161SCyrill Gorcunov * exit after we printed their tids. Now the slow paths
75881841161SCyrill Gorcunov * skips N extra children, we miss N tasks." (c)
75981841161SCyrill Gorcunov *
76081841161SCyrill Gorcunov * So one need to stop or freeze the leader and all
76181841161SCyrill Gorcunov * its children to get a precise result.
76281841161SCyrill Gorcunov */
76381841161SCyrill Gorcunov list_for_each_entry(task, &start->children, sibling) {
76481841161SCyrill Gorcunov if (pos-- == 0) {
76581841161SCyrill Gorcunov pid = get_pid(task_pid(task));
76681841161SCyrill Gorcunov break;
76781841161SCyrill Gorcunov }
76881841161SCyrill Gorcunov }
76981841161SCyrill Gorcunov
77081841161SCyrill Gorcunov out:
77181841161SCyrill Gorcunov read_unlock(&tasklist_lock);
77281841161SCyrill Gorcunov return pid;
77381841161SCyrill Gorcunov }
77481841161SCyrill Gorcunov
children_seq_show(struct seq_file * seq,void * v)77581841161SCyrill Gorcunov static int children_seq_show(struct seq_file *seq, void *v)
77681841161SCyrill Gorcunov {
77704015e3fSChristoph Hellwig struct inode *inode = file_inode(seq->file);
77881841161SCyrill Gorcunov
7799d78edeaSAlexey Gladkov seq_printf(seq, "%d ", pid_nr_ns(v, proc_pid_ns(inode->i_sb)));
78025ce3191SJoe Perches return 0;
78181841161SCyrill Gorcunov }
78281841161SCyrill Gorcunov
children_seq_start(struct seq_file * seq,loff_t * pos)78381841161SCyrill Gorcunov static void *children_seq_start(struct seq_file *seq, loff_t *pos)
78481841161SCyrill Gorcunov {
78504015e3fSChristoph Hellwig return get_children_pid(file_inode(seq->file), NULL, *pos);
78681841161SCyrill Gorcunov }
78781841161SCyrill Gorcunov
children_seq_next(struct seq_file * seq,void * v,loff_t * pos)78881841161SCyrill Gorcunov static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos)
78981841161SCyrill Gorcunov {
79081841161SCyrill Gorcunov struct pid *pid;
79181841161SCyrill Gorcunov
79204015e3fSChristoph Hellwig pid = get_children_pid(file_inode(seq->file), v, *pos + 1);
79381841161SCyrill Gorcunov put_pid(v);
79481841161SCyrill Gorcunov
79581841161SCyrill Gorcunov ++*pos;
79681841161SCyrill Gorcunov return pid;
79781841161SCyrill Gorcunov }
79881841161SCyrill Gorcunov
children_seq_stop(struct seq_file * seq,void * v)79981841161SCyrill Gorcunov static void children_seq_stop(struct seq_file *seq, void *v)
80081841161SCyrill Gorcunov {
80181841161SCyrill Gorcunov put_pid(v);
80281841161SCyrill Gorcunov }
80381841161SCyrill Gorcunov
80481841161SCyrill Gorcunov static const struct seq_operations children_seq_ops = {
80581841161SCyrill Gorcunov .start = children_seq_start,
80681841161SCyrill Gorcunov .next = children_seq_next,
80781841161SCyrill Gorcunov .stop = children_seq_stop,
80881841161SCyrill Gorcunov .show = children_seq_show,
80981841161SCyrill Gorcunov };
81081841161SCyrill Gorcunov
children_seq_open(struct inode * inode,struct file * file)81181841161SCyrill Gorcunov static int children_seq_open(struct inode *inode, struct file *file)
81281841161SCyrill Gorcunov {
81304015e3fSChristoph Hellwig return seq_open(file, &children_seq_ops);
81481841161SCyrill Gorcunov }
81581841161SCyrill Gorcunov
81681841161SCyrill Gorcunov const struct file_operations proc_tid_children_operations = {
81781841161SCyrill Gorcunov .open = children_seq_open,
81881841161SCyrill Gorcunov .read = seq_read,
81981841161SCyrill Gorcunov .llseek = seq_lseek,
820171ef917SAlexey Dobriyan .release = seq_release,
82181841161SCyrill Gorcunov };
8222e13ba54SIago López Galeiras #endif /* CONFIG_PROC_CHILDREN */
823