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> 591da177e4SLinus Torvalds #include <linux/kernel.h> 601da177e4SLinus Torvalds #include <linux/kernel_stat.h> 611da177e4SLinus Torvalds #include <linux/tty.h> 621da177e4SLinus Torvalds #include <linux/string.h> 631da177e4SLinus Torvalds #include <linux/mman.h> 646e84f315SIngo Molnar #include <linux/sched/mm.h> 656a3827d7SIngo Molnar #include <linux/sched/numa_balancing.h> 66fd7d5627SJohn Ogness #include <linux/sched/task_stack.h> 6729930025SIngo Molnar #include <linux/sched/task.h> 6832ef5517SIngo Molnar #include <linux/sched/cputime.h> 691da177e4SLinus Torvalds #include <linux/proc_fs.h> 701da177e4SLinus Torvalds #include <linux/ioport.h> 718ea02606SIngo Molnar #include <linux/uaccess.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> 900d094efeSRoland McGrath #include <linux/tracehook.h> 91edc924e0SAndy Shevchenko #include <linux/string_helpers.h> 92ae2975bcSEric W. Biederman #include <linux/user_namespace.h> 933e42979eSRichard W.M. Jones #include <linux/fs_struct.h> 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds #include <asm/processor.h> 961da177e4SLinus Torvalds #include "internal.h" 971da177e4SLinus Torvalds 9888b72b31STejun Heo void proc_task_name(struct seq_file *m, struct task_struct *p, bool escape) 991da177e4SLinus Torvalds { 100edc924e0SAndy Shevchenko char *buf; 1013a49f3d2SAndy Shevchenko size_t size; 1026b59808bSTejun Heo char tcomm[64]; 1033a49f3d2SAndy Shevchenko int ret; 1041da177e4SLinus Torvalds 1056b59808bSTejun Heo if (p->flags & PF_WQ_WORKER) 1066b59808bSTejun Heo wq_worker_comm(tcomm, sizeof(tcomm), p); 1076b59808bSTejun Heo else 1086b59808bSTejun Heo __get_task_comm(tcomm, sizeof(tcomm), p); 109edc924e0SAndy Shevchenko 1103a49f3d2SAndy Shevchenko size = seq_get_buf(m, &buf); 11188b72b31STejun Heo if (escape) { 11288b72b31STejun Heo ret = string_escape_str(tcomm, buf, size, 11388b72b31STejun Heo ESCAPE_SPACE | ESCAPE_SPECIAL, "\n\\"); 11488b72b31STejun Heo if (ret >= size) 11588b72b31STejun Heo ret = -1; 11688b72b31STejun Heo } else { 11788b72b31STejun Heo ret = strscpy(buf, tcomm, size); 11888b72b31STejun Heo } 119edc924e0SAndy Shevchenko 12088b72b31STejun Heo seq_commit(m, ret); 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* 1241da177e4SLinus Torvalds * The task state array is a strange "bitmap" of 1251da177e4SLinus Torvalds * reasons to sleep. Thus "running" is zero, and 1261da177e4SLinus Torvalds * you can test for combinations of others with 1271da177e4SLinus Torvalds * simple bit tests. 1281da177e4SLinus Torvalds */ 129e130aa70SMike Frysinger static const char * const task_state_array[] = { 13006eb6184SPeter Zijlstra 13106eb6184SPeter Zijlstra /* states in TASK_REPORT: */ 13206eb6184SPeter Zijlstra "R (running)", /* 0x00 */ 13306eb6184SPeter Zijlstra "S (sleeping)", /* 0x01 */ 13406eb6184SPeter Zijlstra "D (disk sleep)", /* 0x02 */ 13506eb6184SPeter Zijlstra "T (stopped)", /* 0x04 */ 13606eb6184SPeter Zijlstra "t (tracing stop)", /* 0x08 */ 13706eb6184SPeter Zijlstra "X (dead)", /* 0x10 */ 13806eb6184SPeter Zijlstra "Z (zombie)", /* 0x20 */ 1398ef9925bSPeter Zijlstra "P (parked)", /* 0x40 */ 14006eb6184SPeter Zijlstra 14106eb6184SPeter Zijlstra /* states beyond TASK_REPORT: */ 1428ef9925bSPeter Zijlstra "I (idle)", /* 0x80 */ 1431da177e4SLinus Torvalds }; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds static inline const char *get_task_state(struct task_struct *tsk) 1461da177e4SLinus Torvalds { 14706eb6184SPeter Zijlstra BUILD_BUG_ON(1 + ilog2(TASK_REPORT_MAX) != ARRAY_SIZE(task_state_array)); 1481d48b080SPeter Zijlstra return task_state_array[task_state_index(tsk)]; 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 151df5f8314SEric W. Biederman static inline void task_state(struct seq_file *m, struct pid_namespace *ns, 152df5f8314SEric W. Biederman struct pid *pid, struct task_struct *p) 1531da177e4SLinus Torvalds { 154e9f238c3SEric W. Biederman struct user_namespace *user_ns = seq_user_ns(m); 1551da177e4SLinus Torvalds struct group_info *group_info; 15668c3411fSMateusz Guzik int g, umask = -1; 157abdba6e9SOleg Nesterov struct task_struct *tracer; 158c69e8d9cSDavid Howells const struct cred *cred; 159abdba6e9SOleg Nesterov pid_t ppid, tpid = 0, tgid, ngid; 1600f4a0d53SOleg Nesterov unsigned int max_fds = 0; 1611da177e4SLinus Torvalds 162b0fa9db6SOleg Nesterov rcu_read_lock(); 163b488893aSPavel Emelyanov ppid = pid_alive(p) ? 164b488893aSPavel Emelyanov task_tgid_nr_ns(rcu_dereference(p->real_parent), ns) : 0; 165abdba6e9SOleg Nesterov 166abdba6e9SOleg Nesterov tracer = ptrace_parent(p); 1670d094efeSRoland McGrath if (tracer) 1680d094efeSRoland McGrath tpid = task_pid_nr_ns(tracer, ns); 169b0fafc11SOleg Nesterov 170b0fafc11SOleg Nesterov tgid = task_tgid_nr_ns(p, ns); 171b0fafc11SOleg Nesterov ngid = task_numa_group_id(p); 172de09a977SDavid Howells cred = get_task_cred(p); 1730f4a0d53SOleg Nesterov 1740f4a0d53SOleg Nesterov task_lock(p); 17568c3411fSMateusz Guzik if (p->fs) 17668c3411fSMateusz Guzik umask = p->fs->umask; 1770f4a0d53SOleg Nesterov if (p->files) 1780f4a0d53SOleg Nesterov max_fds = files_fdtable(p->files)->max_fds; 1790f4a0d53SOleg Nesterov task_unlock(p); 180b0fafc11SOleg Nesterov rcu_read_unlock(); 1810f4a0d53SOleg Nesterov 18268c3411fSMateusz Guzik if (umask >= 0) 18368c3411fSMateusz Guzik seq_printf(m, "Umask:\t%#04o\n", umask); 184d0f02231SAndrei Vagin seq_puts(m, "State:\t"); 185d0f02231SAndrei Vagin seq_puts(m, get_task_state(p)); 1861da177e4SLinus Torvalds 18775ba1d07SJoe Perches seq_put_decimal_ull(m, "\nTgid:\t", tgid); 18875ba1d07SJoe Perches seq_put_decimal_ull(m, "\nNgid:\t", ngid); 18975ba1d07SJoe Perches seq_put_decimal_ull(m, "\nPid:\t", pid_nr_ns(pid, ns)); 19075ba1d07SJoe Perches seq_put_decimal_ull(m, "\nPPid:\t", ppid); 19175ba1d07SJoe Perches seq_put_decimal_ull(m, "\nTracerPid:\t", tpid); 19275ba1d07SJoe Perches seq_put_decimal_ull(m, "\nUid:\t", from_kuid_munged(user_ns, cred->uid)); 19375ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->euid)); 19475ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->suid)); 19575ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kuid_munged(user_ns, cred->fsuid)); 19675ba1d07SJoe Perches seq_put_decimal_ull(m, "\nGid:\t", from_kgid_munged(user_ns, cred->gid)); 19775ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->egid)); 19875ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->sgid)); 19975ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", from_kgid_munged(user_ns, cred->fsgid)); 20075ba1d07SJoe Perches seq_put_decimal_ull(m, "\nFDSize:\t", max_fds); 201f7a5f132SAlexey Dobriyan 202f7a5f132SAlexey Dobriyan seq_puts(m, "\nGroups:\t"); 203c69e8d9cSDavid Howells group_info = cred->group_info; 2048d238027SArtem Bityutskiy for (g = 0; g < group_info->ngroups; g++) 20575ba1d07SJoe Perches seq_put_decimal_ull(m, g ? " " : "", 20681243eacSAlexey Dobriyan from_kgid_munged(user_ns, group_info->gid[g])); 207c69e8d9cSDavid Howells put_cred(cred); 208f7a5f132SAlexey Dobriyan /* Trailing space shouldn't have been added in the first place. */ 209f7a5f132SAlexey Dobriyan seq_putc(m, ' '); 2101da177e4SLinus Torvalds 211e4bc3324SChen Hanxiao #ifdef CONFIG_PID_NS 212e4bc3324SChen Hanxiao seq_puts(m, "\nNStgid:"); 213e4bc3324SChen Hanxiao for (g = ns->level; g <= pid->level; g++) 21475ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", task_tgid_nr_ns(p, pid->numbers[g].ns)); 215e4bc3324SChen Hanxiao seq_puts(m, "\nNSpid:"); 216e4bc3324SChen Hanxiao for (g = ns->level; g <= pid->level; g++) 21775ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", task_pid_nr_ns(p, pid->numbers[g].ns)); 218e4bc3324SChen Hanxiao seq_puts(m, "\nNSpgid:"); 219e4bc3324SChen Hanxiao for (g = ns->level; g <= pid->level; g++) 22075ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", task_pgrp_nr_ns(p, pid->numbers[g].ns)); 221e4bc3324SChen Hanxiao seq_puts(m, "\nNSsid:"); 222e4bc3324SChen Hanxiao for (g = ns->level; g <= pid->level; g++) 22375ba1d07SJoe Perches seq_put_decimal_ull(m, "\t", task_session_nr_ns(p, pid->numbers[g].ns)); 224e4bc3324SChen Hanxiao #endif 2259d6de12fSAlexey Dobriyan seq_putc(m, '\n'); 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 228138d22b5SCyrill Gorcunov void render_sigset_t(struct seq_file *m, const char *header, 229df5f8314SEric W. Biederman sigset_t *set) 2301da177e4SLinus Torvalds { 231df5f8314SEric W. Biederman int i; 2321da177e4SLinus Torvalds 2339d6de12fSAlexey Dobriyan seq_puts(m, header); 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds i = _NSIG; 2361da177e4SLinus Torvalds do { 2371da177e4SLinus Torvalds int x = 0; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds i -= 4; 2401da177e4SLinus Torvalds if (sigismember(set, i+1)) x |= 1; 2411da177e4SLinus Torvalds if (sigismember(set, i+2)) x |= 2; 2421da177e4SLinus Torvalds if (sigismember(set, i+3)) x |= 4; 2431da177e4SLinus Torvalds if (sigismember(set, i+4)) x |= 8; 244209b14dcSRasmus Villemoes seq_putc(m, hex_asc[x]); 2451da177e4SLinus Torvalds } while (i >= 4); 2461da177e4SLinus Torvalds 2479d6de12fSAlexey Dobriyan seq_putc(m, '\n'); 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2508977a27bSAlexey Dobriyan static void collect_sigign_sigcatch(struct task_struct *p, sigset_t *sigign, 2518977a27bSAlexey Dobriyan sigset_t *sigcatch) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds struct k_sigaction *k; 2541da177e4SLinus Torvalds int i; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds k = p->sighand->action; 2571da177e4SLinus Torvalds for (i = 1; i <= _NSIG; ++i, ++k) { 2581da177e4SLinus Torvalds if (k->sa.sa_handler == SIG_IGN) 2598977a27bSAlexey Dobriyan sigaddset(sigign, i); 2601da177e4SLinus Torvalds else if (k->sa.sa_handler != SIG_DFL) 2618977a27bSAlexey Dobriyan sigaddset(sigcatch, i); 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 265df5f8314SEric W. Biederman static inline void task_sig(struct seq_file *m, struct task_struct *p) 2661da177e4SLinus Torvalds { 2675e6b3f42SOleg Nesterov unsigned long flags; 2681da177e4SLinus Torvalds sigset_t pending, shpending, blocked, ignored, caught; 2691da177e4SLinus Torvalds int num_threads = 0; 270197850a1SAlexey Dobriyan unsigned int qsize = 0; 2711da177e4SLinus Torvalds unsigned long qlim = 0; 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds sigemptyset(&pending); 2741da177e4SLinus Torvalds sigemptyset(&shpending); 2751da177e4SLinus Torvalds sigemptyset(&blocked); 2761da177e4SLinus Torvalds sigemptyset(&ignored); 2771da177e4SLinus Torvalds sigemptyset(&caught); 2781da177e4SLinus Torvalds 2795e6b3f42SOleg Nesterov if (lock_task_sighand(p, &flags)) { 2801da177e4SLinus Torvalds pending = p->pending.signal; 2811da177e4SLinus Torvalds shpending = p->signal->shared_pending.signal; 2821da177e4SLinus Torvalds blocked = p->blocked; 2831da177e4SLinus Torvalds collect_sigign_sigcatch(p, &ignored, &caught); 2847e49827cSOleg Nesterov num_threads = get_nr_threads(p); 2857dc52157SPaul E. McKenney rcu_read_lock(); /* FIXME: is this correct? */ 286c69e8d9cSDavid Howells qsize = atomic_read(&__task_cred(p)->user->sigpending); 2877dc52157SPaul E. McKenney rcu_read_unlock(); 288d554ed89SJiri Slaby qlim = task_rlimit(p, RLIMIT_SIGPENDING); 2895e6b3f42SOleg Nesterov unlock_task_sighand(p, &flags); 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 29275ba1d07SJoe Perches seq_put_decimal_ull(m, "Threads:\t", num_threads); 29375ba1d07SJoe Perches seq_put_decimal_ull(m, "\nSigQ:\t", qsize); 29475ba1d07SJoe Perches seq_put_decimal_ull(m, "/", qlim); 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds /* render them all */ 297f7a5f132SAlexey Dobriyan render_sigset_t(m, "\nSigPnd:\t", &pending); 298df5f8314SEric W. Biederman render_sigset_t(m, "ShdPnd:\t", &shpending); 299df5f8314SEric W. Biederman render_sigset_t(m, "SigBlk:\t", &blocked); 300df5f8314SEric W. Biederman render_sigset_t(m, "SigIgn:\t", &ignored); 301df5f8314SEric W. Biederman render_sigset_t(m, "SigCgt:\t", &caught); 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 304df5f8314SEric W. Biederman static void render_cap_t(struct seq_file *m, const char *header, 305df5f8314SEric W. Biederman kernel_cap_t *a) 306e338d263SAndrew Morgan { 307e338d263SAndrew Morgan unsigned __capi; 308e338d263SAndrew Morgan 3099d6de12fSAlexey Dobriyan seq_puts(m, header); 310e338d263SAndrew Morgan CAP_FOR_EACH_U32(__capi) { 311d0f02231SAndrei Vagin seq_put_hex_ll(m, NULL, 312d0f02231SAndrei Vagin a->cap[CAP_LAST_U32 - __capi], 8); 313e338d263SAndrew Morgan } 3149d6de12fSAlexey Dobriyan seq_putc(m, '\n'); 315e338d263SAndrew Morgan } 316e338d263SAndrew Morgan 317df5f8314SEric W. Biederman static inline void task_cap(struct seq_file *m, struct task_struct *p) 3181da177e4SLinus Torvalds { 319c69e8d9cSDavid Howells const struct cred *cred; 32058319057SAndy Lutomirski kernel_cap_t cap_inheritable, cap_permitted, cap_effective, 32158319057SAndy Lutomirski cap_bset, cap_ambient; 322b6dff3ecSDavid Howells 323c69e8d9cSDavid Howells rcu_read_lock(); 324c69e8d9cSDavid Howells cred = __task_cred(p); 325c69e8d9cSDavid Howells cap_inheritable = cred->cap_inheritable; 326c69e8d9cSDavid Howells cap_permitted = cred->cap_permitted; 327c69e8d9cSDavid Howells cap_effective = cred->cap_effective; 328c69e8d9cSDavid Howells cap_bset = cred->cap_bset; 32958319057SAndy Lutomirski cap_ambient = cred->cap_ambient; 330c69e8d9cSDavid Howells rcu_read_unlock(); 331c69e8d9cSDavid Howells 332c69e8d9cSDavid Howells render_cap_t(m, "CapInh:\t", &cap_inheritable); 333c69e8d9cSDavid Howells render_cap_t(m, "CapPrm:\t", &cap_permitted); 334c69e8d9cSDavid Howells render_cap_t(m, "CapEff:\t", &cap_effective); 335c69e8d9cSDavid Howells render_cap_t(m, "CapBnd:\t", &cap_bset); 33658319057SAndy Lutomirski render_cap_t(m, "CapAmb:\t", &cap_ambient); 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 3392f4b3bf6SKees Cook static inline void task_seccomp(struct seq_file *m, struct task_struct *p) 3402f4b3bf6SKees Cook { 341af884cd4SKees Cook seq_put_decimal_ull(m, "NoNewPrivs:\t", task_no_new_privs(p)); 3422f4b3bf6SKees Cook #ifdef CONFIG_SECCOMP 343af884cd4SKees Cook seq_put_decimal_ull(m, "\nSeccomp:\t", p->seccomp.mode); 344c818c03bSKees Cook seq_put_decimal_ull(m, "\nSeccomp_filters:\t", 345c818c03bSKees Cook atomic_read(&p->seccomp.filter_count)); 3462f4b3bf6SKees Cook #endif 34708b55775SAlexey Dobriyan seq_puts(m, "\nSpeculation_Store_Bypass:\t"); 348fae1fa0fSKees Cook switch (arch_prctl_spec_ctrl_get(p, PR_SPEC_STORE_BYPASS)) { 349fae1fa0fSKees Cook case -EINVAL: 35008b55775SAlexey Dobriyan seq_puts(m, "unknown"); 351fae1fa0fSKees Cook break; 352fae1fa0fSKees Cook case PR_SPEC_NOT_AFFECTED: 35308b55775SAlexey Dobriyan seq_puts(m, "not vulnerable"); 354fae1fa0fSKees Cook break; 355356e4bffSThomas Gleixner case PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE: 35608b55775SAlexey Dobriyan seq_puts(m, "thread force mitigated"); 357356e4bffSThomas Gleixner break; 358fae1fa0fSKees Cook case PR_SPEC_PRCTL | PR_SPEC_DISABLE: 35908b55775SAlexey Dobriyan seq_puts(m, "thread mitigated"); 360fae1fa0fSKees Cook break; 361fae1fa0fSKees Cook case PR_SPEC_PRCTL | PR_SPEC_ENABLE: 36208b55775SAlexey Dobriyan seq_puts(m, "thread vulnerable"); 363fae1fa0fSKees Cook break; 364fae1fa0fSKees Cook case PR_SPEC_DISABLE: 36508b55775SAlexey Dobriyan seq_puts(m, "globally mitigated"); 366fae1fa0fSKees Cook break; 367fae1fa0fSKees Cook default: 36808b55775SAlexey Dobriyan seq_puts(m, "vulnerable"); 369fae1fa0fSKees Cook break; 370fae1fa0fSKees Cook } 371af884cd4SKees Cook seq_putc(m, '\n'); 3722f4b3bf6SKees Cook } 3732f4b3bf6SKees Cook 374df5f8314SEric W. Biederman static inline void task_context_switch_counts(struct seq_file *m, 375df5f8314SEric W. Biederman struct task_struct *p) 376b663a79cSMaxim Uvarov { 37775ba1d07SJoe Perches seq_put_decimal_ull(m, "voluntary_ctxt_switches:\t", p->nvcsw); 37875ba1d07SJoe Perches seq_put_decimal_ull(m, "\nnonvoluntary_ctxt_switches:\t", p->nivcsw); 379f7a5f132SAlexey Dobriyan seq_putc(m, '\n'); 380b663a79cSMaxim Uvarov } 381b663a79cSMaxim Uvarov 382d01d4827SHeiko Carstens static void task_cpus_allowed(struct seq_file *m, struct task_struct *task) 383d01d4827SHeiko Carstens { 384a0c2e07dSTejun Heo seq_printf(m, "Cpus_allowed:\t%*pb\n", 385*86fbcd3bSPeter Zijlstra cpumask_pr_args(&task->cpus_mask)); 386a0c2e07dSTejun Heo seq_printf(m, "Cpus_allowed_list:\t%*pbl\n", 387*86fbcd3bSPeter Zijlstra cpumask_pr_args(&task->cpus_mask)); 388d01d4827SHeiko Carstens } 389d01d4827SHeiko Carstens 390c6434012SRoman Gushchin static inline void task_core_dumping(struct seq_file *m, struct mm_struct *mm) 391c6434012SRoman Gushchin { 392d0f02231SAndrei Vagin seq_put_decimal_ull(m, "CoreDumping:\t", !!mm->core_state); 393d0f02231SAndrei Vagin seq_putc(m, '\n'); 394c6434012SRoman Gushchin } 395c6434012SRoman Gushchin 396a1400af7SMichal Hocko static inline void task_thp_status(struct seq_file *m, struct mm_struct *mm) 397a1400af7SMichal Hocko { 398a1400af7SMichal Hocko bool thp_enabled = IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE); 399a1400af7SMichal Hocko 400a1400af7SMichal Hocko if (thp_enabled) 401a1400af7SMichal Hocko thp_enabled = !test_bit(MMF_DISABLE_THP, &mm->flags); 402a1400af7SMichal Hocko seq_printf(m, "THP_enabled:\t%d\n", thp_enabled); 403a1400af7SMichal Hocko } 404a1400af7SMichal Hocko 405df5f8314SEric W. Biederman int proc_pid_status(struct seq_file *m, struct pid_namespace *ns, 406df5f8314SEric W. Biederman struct pid *pid, struct task_struct *task) 4071da177e4SLinus Torvalds { 4081da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 4091da177e4SLinus Torvalds 41088b72b31STejun Heo seq_puts(m, "Name:\t"); 41188b72b31STejun Heo proc_task_name(m, task, true); 41288b72b31STejun Heo seq_putc(m, '\n'); 41388b72b31STejun Heo 414df5f8314SEric W. Biederman task_state(m, ns, pid, task); 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds if (mm) { 417df5f8314SEric W. Biederman task_mem(m, mm); 418c6434012SRoman Gushchin task_core_dumping(m, mm); 419a1400af7SMichal Hocko task_thp_status(m, mm); 4201da177e4SLinus Torvalds mmput(mm); 4211da177e4SLinus Torvalds } 422df5f8314SEric W. Biederman task_sig(m, task); 423df5f8314SEric W. Biederman task_cap(m, task); 4242f4b3bf6SKees Cook task_seccomp(m, task); 425d01d4827SHeiko Carstens task_cpus_allowed(m, task); 426df5f8314SEric W. Biederman cpuset_task_status_allowed(m, task); 427df5f8314SEric W. Biederman task_context_switch_counts(m, task); 428df5f8314SEric W. Biederman return 0; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 431ee992744SEric W. Biederman static int do_task_stat(struct seq_file *m, struct pid_namespace *ns, 432ee992744SEric W. Biederman struct pid *pid, struct task_struct *task, int whole) 4331da177e4SLinus Torvalds { 434b2f73922SIngo Molnar unsigned long vsize, eip, esp, wchan = 0; 435715be1fcSJan Engelhardt int priority, nice; 4361da177e4SLinus Torvalds int tty_pgrp = -1, tty_nr = 0; 4371da177e4SLinus Torvalds sigset_t sigign, sigcatch; 4381da177e4SLinus Torvalds char state; 439a593d6edSOleg Nesterov pid_t ppid = 0, pgid = -1, sid = -1; 4401da177e4SLinus Torvalds int num_threads = 0; 441f83ce3e6SJake Edge int permitted; 4421da177e4SLinus Torvalds struct mm_struct *mm; 4431da177e4SLinus Torvalds unsigned long long start_time; 4441da177e4SLinus Torvalds unsigned long cmin_flt = 0, cmaj_flt = 0; 4451da177e4SLinus Torvalds unsigned long min_flt = 0, maj_flt = 0; 4465613fda9SFrederic Weisbecker u64 cutime, cstime, utime, stime; 44716a6d9beSFrederic Weisbecker u64 cgtime, gtime; 4481da177e4SLinus Torvalds unsigned long rsslim = 0; 449a593d6edSOleg Nesterov unsigned long flags; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds state = *get_task_state(task); 4521da177e4SLinus Torvalds vsize = eip = esp = 0; 453caaee623SJann Horn permitted = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS | PTRACE_MODE_NOAUDIT); 4541da177e4SLinus Torvalds mm = get_task_mm(task); 4551da177e4SLinus Torvalds if (mm) { 4561da177e4SLinus Torvalds vsize = task_vsize(mm); 4570a1eb2d4SAndy Lutomirski /* 4580a1eb2d4SAndy Lutomirski * esp and eip are intentionally zeroed out. There is no 4590a1eb2d4SAndy Lutomirski * non-racy way to read them without freezing the task. 4600a1eb2d4SAndy Lutomirski * Programs that need reliable values can use ptrace(2). 461fd7d5627SJohn Ogness * 462fd7d5627SJohn Ogness * The only exception is if the task is core dumping because 463fd7d5627SJohn Ogness * a program is not able to use ptrace(2) in that case. It is 464fd7d5627SJohn Ogness * safe because the task has stopped executing permanently. 4650a1eb2d4SAndy Lutomirski */ 466cb8f381fSJohn Ogness if (permitted && (task->flags & (PF_EXITING|PF_DUMPCORE))) { 4678bb2ee19SAlexey Dobriyan if (try_get_task_stack(task)) { 468fd7d5627SJohn Ogness eip = KSTK_EIP(task); 469fd7d5627SJohn Ogness esp = KSTK_ESP(task); 4708bb2ee19SAlexey Dobriyan put_task_stack(task); 4718bb2ee19SAlexey Dobriyan } 472fd7d5627SJohn Ogness } 473f83ce3e6SJake Edge } 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds sigemptyset(&sigign); 4761da177e4SLinus Torvalds sigemptyset(&sigcatch); 47764861634SMartin Schwidefsky cutime = cstime = utime = stime = 0; 47864861634SMartin Schwidefsky cgtime = gtime = 0; 4793cfd0885SAlan Cox 480a593d6edSOleg Nesterov if (lock_task_sighand(task, &flags)) { 481a593d6edSOleg Nesterov struct signal_struct *sig = task->signal; 482a593d6edSOleg Nesterov 48391593504SOleg Nesterov if (sig->tty) { 4845d0fdf1eSAlan Cox struct pid *pgrp = tty_get_pgrp(sig->tty); 4855d0fdf1eSAlan Cox tty_pgrp = pid_nr_ns(pgrp, ns); 4865d0fdf1eSAlan Cox put_pid(pgrp); 48791593504SOleg Nesterov tty_nr = new_encode_dev(tty_devnum(sig->tty)); 488a593d6edSOleg Nesterov } 489a593d6edSOleg Nesterov 4907e49827cSOleg Nesterov num_threads = get_nr_threads(task); 4911da177e4SLinus Torvalds collect_sigign_sigcatch(task, &sigign, &sigcatch); 4921da177e4SLinus Torvalds 493a593d6edSOleg Nesterov cmin_flt = sig->cmin_flt; 494a593d6edSOleg Nesterov cmaj_flt = sig->cmaj_flt; 495a593d6edSOleg Nesterov cutime = sig->cutime; 496a593d6edSOleg Nesterov cstime = sig->cstime; 4979ac52315SLaurent Vivier cgtime = sig->cgtime; 4986aa7de05SMark Rutland rsslim = READ_ONCE(sig->rlim[RLIMIT_RSS].rlim_cur); 499a593d6edSOleg Nesterov 5001da177e4SLinus Torvalds /* add up live thread stats at the group level */ 5011da177e4SLinus Torvalds if (whole) { 502a593d6edSOleg Nesterov struct task_struct *t = task; 5031da177e4SLinus Torvalds do { 5041da177e4SLinus Torvalds min_flt += t->min_flt; 5051da177e4SLinus Torvalds maj_flt += t->maj_flt; 5066fac4829SFrederic Weisbecker gtime += task_gtime(t); 507185ee40eSOleg Nesterov } while_each_thread(task, t); 508a593d6edSOleg Nesterov 509a593d6edSOleg Nesterov min_flt += sig->min_flt; 510a593d6edSOleg Nesterov maj_flt += sig->maj_flt; 511e80d0a1aSFrederic Weisbecker thread_group_cputime_adjusted(task, &utime, &stime); 51264861634SMartin Schwidefsky gtime += sig->gtime; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 515b488893aSPavel Emelyanov sid = task_session_nr_ns(task, ns); 516a98fdcefSOleg Nesterov ppid = task_tgid_nr_ns(task->real_parent, ns); 517b488893aSPavel Emelyanov pgid = task_pgrp_nr_ns(task, ns); 518a593d6edSOleg Nesterov 519a593d6edSOleg Nesterov unlock_task_sighand(task, &flags); 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 522f83ce3e6SJake Edge if (permitted && (!whole || num_threads < 2)) 5231da177e4SLinus Torvalds wchan = get_wchan(task); 5241da177e4SLinus Torvalds if (!whole) { 5251da177e4SLinus Torvalds min_flt = task->min_flt; 5261da177e4SLinus Torvalds maj_flt = task->maj_flt; 527e80d0a1aSFrederic Weisbecker task_cputime_adjusted(task, &utime, &stime); 5286fac4829SFrederic Weisbecker gtime = task_gtime(task); 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds /* scale priority and nice values from timeslices to -20..20 */ 5321da177e4SLinus Torvalds /* to make it look like a "normal" Unix priority/nice value */ 5331da177e4SLinus Torvalds priority = task_prio(task); 5341da177e4SLinus Torvalds nice = task_nice(task); 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds /* convert nsec -> ticks */ 537cf25e24dSPeter Zijlstra start_time = nsec_to_clock_t(task->start_boottime); 5381da177e4SLinus Torvalds 539d0f02231SAndrei Vagin seq_put_decimal_ull(m, "", pid_nr_ns(pid, ns)); 540d0f02231SAndrei Vagin seq_puts(m, " ("); 54188b72b31STejun Heo proc_task_name(m, task, false); 542d0f02231SAndrei Vagin seq_puts(m, ") "); 543d0f02231SAndrei Vagin seq_putc(m, state); 54475ba1d07SJoe Perches seq_put_decimal_ll(m, " ", ppid); 54575ba1d07SJoe Perches seq_put_decimal_ll(m, " ", pgid); 54675ba1d07SJoe Perches seq_put_decimal_ll(m, " ", sid); 54775ba1d07SJoe Perches seq_put_decimal_ll(m, " ", tty_nr); 54875ba1d07SJoe Perches seq_put_decimal_ll(m, " ", tty_pgrp); 54975ba1d07SJoe Perches seq_put_decimal_ull(m, " ", task->flags); 55075ba1d07SJoe Perches seq_put_decimal_ull(m, " ", min_flt); 55175ba1d07SJoe Perches seq_put_decimal_ull(m, " ", cmin_flt); 55275ba1d07SJoe Perches seq_put_decimal_ull(m, " ", maj_flt); 55375ba1d07SJoe Perches seq_put_decimal_ull(m, " ", cmaj_flt); 5545613fda9SFrederic Weisbecker seq_put_decimal_ull(m, " ", nsec_to_clock_t(utime)); 5555613fda9SFrederic Weisbecker seq_put_decimal_ull(m, " ", nsec_to_clock_t(stime)); 5565613fda9SFrederic Weisbecker seq_put_decimal_ll(m, " ", nsec_to_clock_t(cutime)); 5575613fda9SFrederic Weisbecker seq_put_decimal_ll(m, " ", nsec_to_clock_t(cstime)); 55875ba1d07SJoe Perches seq_put_decimal_ll(m, " ", priority); 55975ba1d07SJoe Perches seq_put_decimal_ll(m, " ", nice); 56075ba1d07SJoe Perches seq_put_decimal_ll(m, " ", num_threads); 56175ba1d07SJoe Perches seq_put_decimal_ull(m, " ", 0); 56275ba1d07SJoe Perches seq_put_decimal_ull(m, " ", start_time); 56375ba1d07SJoe Perches seq_put_decimal_ull(m, " ", vsize); 56475ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm ? get_mm_rss(mm) : 0); 56575ba1d07SJoe Perches seq_put_decimal_ull(m, " ", rsslim); 56675ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->start_code : 1) : 0); 56775ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm ? (permitted ? mm->end_code : 1) : 0); 56875ba1d07SJoe Perches seq_put_decimal_ull(m, " ", (permitted && mm) ? mm->start_stack : 0); 56975ba1d07SJoe Perches seq_put_decimal_ull(m, " ", esp); 57075ba1d07SJoe Perches seq_put_decimal_ull(m, " ", eip); 5711da177e4SLinus Torvalds /* The signal information here is obsolete. 5721da177e4SLinus Torvalds * It must be decimal for Linux 2.0 compatibility. 5731da177e4SLinus Torvalds * Use /proc/#/status for real-time signals. 5741da177e4SLinus Torvalds */ 57575ba1d07SJoe Perches seq_put_decimal_ull(m, " ", task->pending.signal.sig[0] & 0x7fffffffUL); 57675ba1d07SJoe Perches seq_put_decimal_ull(m, " ", task->blocked.sig[0] & 0x7fffffffUL); 57775ba1d07SJoe Perches seq_put_decimal_ull(m, " ", sigign.sig[0] & 0x7fffffffUL); 57875ba1d07SJoe Perches seq_put_decimal_ull(m, " ", sigcatch.sig[0] & 0x7fffffffUL); 579b2f73922SIngo Molnar 580b2f73922SIngo Molnar /* 581b2f73922SIngo Molnar * We used to output the absolute kernel address, but that's an 582b2f73922SIngo Molnar * information leak - so instead we show a 0/1 flag here, to signal 583b2f73922SIngo Molnar * to user-space whether there's a wchan field in /proc/PID/wchan. 584b2f73922SIngo Molnar * 585b2f73922SIngo Molnar * This works with older implementations of procps as well. 586b2f73922SIngo Molnar */ 587b2f73922SIngo Molnar if (wchan) 588b2f73922SIngo Molnar seq_puts(m, " 1"); 589b2f73922SIngo Molnar else 590b2f73922SIngo Molnar seq_puts(m, " 0"); 591b2f73922SIngo Molnar 59275ba1d07SJoe Perches seq_put_decimal_ull(m, " ", 0); 59375ba1d07SJoe Perches seq_put_decimal_ull(m, " ", 0); 59475ba1d07SJoe Perches seq_put_decimal_ll(m, " ", task->exit_signal); 59575ba1d07SJoe Perches seq_put_decimal_ll(m, " ", task_cpu(task)); 59675ba1d07SJoe Perches seq_put_decimal_ull(m, " ", task->rt_priority); 59775ba1d07SJoe Perches seq_put_decimal_ull(m, " ", task->policy); 59875ba1d07SJoe Perches seq_put_decimal_ull(m, " ", delayacct_blkio_ticks(task)); 59916a6d9beSFrederic Weisbecker seq_put_decimal_ull(m, " ", nsec_to_clock_t(gtime)); 60016a6d9beSFrederic Weisbecker seq_put_decimal_ll(m, " ", nsec_to_clock_t(cgtime)); 6015b172087SCyrill Gorcunov 6025b172087SCyrill Gorcunov if (mm && permitted) { 60375ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->start_data); 60475ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->end_data); 60575ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->start_brk); 60675ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->arg_start); 60775ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->arg_end); 60875ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->env_start); 60975ba1d07SJoe Perches seq_put_decimal_ull(m, " ", mm->env_end); 6105b172087SCyrill Gorcunov } else 61175ba1d07SJoe Perches seq_puts(m, " 0 0 0 0 0 0 0"); 6125b172087SCyrill Gorcunov 6135b172087SCyrill Gorcunov if (permitted) 61475ba1d07SJoe Perches seq_put_decimal_ll(m, " ", task->exit_code); 6155b172087SCyrill Gorcunov else 61675ba1d07SJoe Perches seq_puts(m, " 0"); 6175b172087SCyrill Gorcunov 618bda7bad6SKAMEZAWA Hiroyuki seq_putc(m, '\n'); 6191da177e4SLinus Torvalds if (mm) 6201da177e4SLinus Torvalds mmput(mm); 621ee992744SEric W. Biederman return 0; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 624ee992744SEric W. Biederman int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns, 625ee992744SEric W. Biederman struct pid *pid, struct task_struct *task) 6261da177e4SLinus Torvalds { 627ee992744SEric W. Biederman return do_task_stat(m, ns, pid, task, 0); 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 630ee992744SEric W. Biederman int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns, 631ee992744SEric W. Biederman struct pid *pid, struct task_struct *task) 6321da177e4SLinus Torvalds { 633ee992744SEric W. Biederman return do_task_stat(m, ns, pid, task, 1); 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 636a56d3fc7SEric W. Biederman int proc_pid_statm(struct seq_file *m, struct pid_namespace *ns, 637a56d3fc7SEric W. Biederman struct pid *pid, struct task_struct *task) 6381da177e4SLinus Torvalds { 6391da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds if (mm) { 6425c5ab971SAlexey Dobriyan unsigned long size; 6435c5ab971SAlexey Dobriyan unsigned long resident = 0; 6445c5ab971SAlexey Dobriyan unsigned long shared = 0; 6455c5ab971SAlexey Dobriyan unsigned long text = 0; 6465c5ab971SAlexey Dobriyan unsigned long data = 0; 6475c5ab971SAlexey Dobriyan 6481da177e4SLinus Torvalds size = task_statm(mm, &shared, &text, &data, &resident); 6491da177e4SLinus Torvalds mmput(mm); 6505c5ab971SAlexey Dobriyan 651bda7bad6SKAMEZAWA Hiroyuki /* 652bda7bad6SKAMEZAWA Hiroyuki * For quick read, open code by putting numbers directly 653bda7bad6SKAMEZAWA Hiroyuki * expected format is 654bda7bad6SKAMEZAWA Hiroyuki * seq_printf(m, "%lu %lu %lu %lu 0 %lu 0\n", 655bda7bad6SKAMEZAWA Hiroyuki * size, resident, shared, text, data); 656bda7bad6SKAMEZAWA Hiroyuki */ 65775ba1d07SJoe Perches seq_put_decimal_ull(m, "", size); 65875ba1d07SJoe Perches seq_put_decimal_ull(m, " ", resident); 65975ba1d07SJoe Perches seq_put_decimal_ull(m, " ", shared); 66075ba1d07SJoe Perches seq_put_decimal_ull(m, " ", text); 66175ba1d07SJoe Perches seq_put_decimal_ull(m, " ", 0); 66275ba1d07SJoe Perches seq_put_decimal_ull(m, " ", data); 66375ba1d07SJoe Perches seq_put_decimal_ull(m, " ", 0); 664bda7bad6SKAMEZAWA Hiroyuki seq_putc(m, '\n'); 6655c5ab971SAlexey Dobriyan } else { 6665c5ab971SAlexey Dobriyan seq_write(m, "0 0 0 0 0 0 0\n", 14); 6675c5ab971SAlexey Dobriyan } 668a56d3fc7SEric W. Biederman return 0; 6691da177e4SLinus Torvalds } 67081841161SCyrill Gorcunov 6712e13ba54SIago López Galeiras #ifdef CONFIG_PROC_CHILDREN 67281841161SCyrill Gorcunov static struct pid * 67381841161SCyrill Gorcunov get_children_pid(struct inode *inode, struct pid *pid_prev, loff_t pos) 67481841161SCyrill Gorcunov { 67581841161SCyrill Gorcunov struct task_struct *start, *task; 67681841161SCyrill Gorcunov struct pid *pid = NULL; 67781841161SCyrill Gorcunov 67881841161SCyrill Gorcunov read_lock(&tasklist_lock); 67981841161SCyrill Gorcunov 68081841161SCyrill Gorcunov start = pid_task(proc_pid(inode), PIDTYPE_PID); 68181841161SCyrill Gorcunov if (!start) 68281841161SCyrill Gorcunov goto out; 68381841161SCyrill Gorcunov 68481841161SCyrill Gorcunov /* 68581841161SCyrill Gorcunov * Lets try to continue searching first, this gives 68681841161SCyrill Gorcunov * us significant speedup on children-rich processes. 68781841161SCyrill Gorcunov */ 68881841161SCyrill Gorcunov if (pid_prev) { 68981841161SCyrill Gorcunov task = pid_task(pid_prev, PIDTYPE_PID); 69081841161SCyrill Gorcunov if (task && task->real_parent == start && 69181841161SCyrill Gorcunov !(list_empty(&task->sibling))) { 69281841161SCyrill Gorcunov if (list_is_last(&task->sibling, &start->children)) 69381841161SCyrill Gorcunov goto out; 69481841161SCyrill Gorcunov task = list_first_entry(&task->sibling, 69581841161SCyrill Gorcunov struct task_struct, sibling); 69681841161SCyrill Gorcunov pid = get_pid(task_pid(task)); 69781841161SCyrill Gorcunov goto out; 69881841161SCyrill Gorcunov } 69981841161SCyrill Gorcunov } 70081841161SCyrill Gorcunov 70181841161SCyrill Gorcunov /* 70281841161SCyrill Gorcunov * Slow search case. 70381841161SCyrill Gorcunov * 70481841161SCyrill Gorcunov * We might miss some children here if children 70581841161SCyrill Gorcunov * are exited while we were not holding the lock, 70681841161SCyrill Gorcunov * but it was never promised to be accurate that 70781841161SCyrill Gorcunov * much. 70881841161SCyrill Gorcunov * 70981841161SCyrill Gorcunov * "Just suppose that the parent sleeps, but N children 71081841161SCyrill Gorcunov * exit after we printed their tids. Now the slow paths 71181841161SCyrill Gorcunov * skips N extra children, we miss N tasks." (c) 71281841161SCyrill Gorcunov * 71381841161SCyrill Gorcunov * So one need to stop or freeze the leader and all 71481841161SCyrill Gorcunov * its children to get a precise result. 71581841161SCyrill Gorcunov */ 71681841161SCyrill Gorcunov list_for_each_entry(task, &start->children, sibling) { 71781841161SCyrill Gorcunov if (pos-- == 0) { 71881841161SCyrill Gorcunov pid = get_pid(task_pid(task)); 71981841161SCyrill Gorcunov break; 72081841161SCyrill Gorcunov } 72181841161SCyrill Gorcunov } 72281841161SCyrill Gorcunov 72381841161SCyrill Gorcunov out: 72481841161SCyrill Gorcunov read_unlock(&tasklist_lock); 72581841161SCyrill Gorcunov return pid; 72681841161SCyrill Gorcunov } 72781841161SCyrill Gorcunov 72881841161SCyrill Gorcunov static int children_seq_show(struct seq_file *seq, void *v) 72981841161SCyrill Gorcunov { 73004015e3fSChristoph Hellwig struct inode *inode = file_inode(seq->file); 73181841161SCyrill Gorcunov 7329d78edeaSAlexey Gladkov seq_printf(seq, "%d ", pid_nr_ns(v, proc_pid_ns(inode->i_sb))); 73325ce3191SJoe Perches return 0; 73481841161SCyrill Gorcunov } 73581841161SCyrill Gorcunov 73681841161SCyrill Gorcunov static void *children_seq_start(struct seq_file *seq, loff_t *pos) 73781841161SCyrill Gorcunov { 73804015e3fSChristoph Hellwig return get_children_pid(file_inode(seq->file), NULL, *pos); 73981841161SCyrill Gorcunov } 74081841161SCyrill Gorcunov 74181841161SCyrill Gorcunov static void *children_seq_next(struct seq_file *seq, void *v, loff_t *pos) 74281841161SCyrill Gorcunov { 74381841161SCyrill Gorcunov struct pid *pid; 74481841161SCyrill Gorcunov 74504015e3fSChristoph Hellwig pid = get_children_pid(file_inode(seq->file), v, *pos + 1); 74681841161SCyrill Gorcunov put_pid(v); 74781841161SCyrill Gorcunov 74881841161SCyrill Gorcunov ++*pos; 74981841161SCyrill Gorcunov return pid; 75081841161SCyrill Gorcunov } 75181841161SCyrill Gorcunov 75281841161SCyrill Gorcunov static void children_seq_stop(struct seq_file *seq, void *v) 75381841161SCyrill Gorcunov { 75481841161SCyrill Gorcunov put_pid(v); 75581841161SCyrill Gorcunov } 75681841161SCyrill Gorcunov 75781841161SCyrill Gorcunov static const struct seq_operations children_seq_ops = { 75881841161SCyrill Gorcunov .start = children_seq_start, 75981841161SCyrill Gorcunov .next = children_seq_next, 76081841161SCyrill Gorcunov .stop = children_seq_stop, 76181841161SCyrill Gorcunov .show = children_seq_show, 76281841161SCyrill Gorcunov }; 76381841161SCyrill Gorcunov 76481841161SCyrill Gorcunov static int children_seq_open(struct inode *inode, struct file *file) 76581841161SCyrill Gorcunov { 76604015e3fSChristoph Hellwig return seq_open(file, &children_seq_ops); 76781841161SCyrill Gorcunov } 76881841161SCyrill Gorcunov 76981841161SCyrill Gorcunov const struct file_operations proc_tid_children_operations = { 77081841161SCyrill Gorcunov .open = children_seq_open, 77181841161SCyrill Gorcunov .read = seq_read, 77281841161SCyrill Gorcunov .llseek = seq_lseek, 773171ef917SAlexey Dobriyan .release = seq_release, 77481841161SCyrill Gorcunov }; 7752e13ba54SIago López Galeiras #endif /* CONFIG_PROC_CHILDREN */ 776