11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/proc/base.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * proc base directory handling functions 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. 91da177e4SLinus Torvalds * Instead of using magical inumbers to determine the kind of object 101da177e4SLinus Torvalds * we allocate and fill in-core inodes upon lookup. They don't even 111da177e4SLinus Torvalds * go into icache. We cache the reference to task_struct upon lookup too. 121da177e4SLinus Torvalds * Eventually it should become a filesystem in its own. We don't use the 131da177e4SLinus Torvalds * rest of procfs anymore. 14e070ad49SMauricio Lin * 15e070ad49SMauricio Lin * 16e070ad49SMauricio Lin * Changelog: 17e070ad49SMauricio Lin * 17-Jan-2005 18e070ad49SMauricio Lin * Allan Bezerra 19e070ad49SMauricio Lin * Bruna Moreira <bruna.moreira@indt.org.br> 20e070ad49SMauricio Lin * Edjard Mota <edjard.mota@indt.org.br> 21e070ad49SMauricio Lin * Ilias Biris <ilias.biris@indt.org.br> 22e070ad49SMauricio Lin * Mauricio Lin <mauricio.lin@indt.org.br> 23e070ad49SMauricio Lin * 24e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 25e070ad49SMauricio Lin * 26e070ad49SMauricio Lin * A new process specific entry (smaps) included in /proc. It shows the 27e070ad49SMauricio Lin * size of rss for each memory area. The maps entry lacks information 28e070ad49SMauricio Lin * about physical memory size (rss) for each mapped file, i.e., 29e070ad49SMauricio Lin * rss information for executables and library files. 30e070ad49SMauricio Lin * This additional information is useful for any tools that need to know 31e070ad49SMauricio Lin * about physical memory consumption for a process specific library. 32e070ad49SMauricio Lin * 33e070ad49SMauricio Lin * Changelog: 34e070ad49SMauricio Lin * 21-Feb-2005 35e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 36e070ad49SMauricio Lin * Pud inclusion in the page table walking. 37e070ad49SMauricio Lin * 38e070ad49SMauricio Lin * ChangeLog: 39e070ad49SMauricio Lin * 10-Mar-2005 40e070ad49SMauricio Lin * 10LE Instituto Nokia de Tecnologia - INdT: 41e070ad49SMauricio Lin * A better way to walks through the page table as suggested by Hugh Dickins. 42e070ad49SMauricio Lin * 43e070ad49SMauricio Lin * Simo Piiroinen <simo.piiroinen@nokia.com>: 44e070ad49SMauricio Lin * Smaps information related to shared, private, clean and dirty pages. 45e070ad49SMauricio Lin * 46e070ad49SMauricio Lin * Paul Mundt <paul.mundt@nokia.com>: 47e070ad49SMauricio Lin * Overall revision about smaps. 481da177e4SLinus Torvalds */ 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <asm/uaccess.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/errno.h> 531da177e4SLinus Torvalds #include <linux/time.h> 541da177e4SLinus Torvalds #include <linux/proc_fs.h> 551da177e4SLinus Torvalds #include <linux/stat.h> 565995477aSAndrea Righi #include <linux/task_io_accounting_ops.h> 571da177e4SLinus Torvalds #include <linux/init.h> 5816f7e0feSRandy Dunlap #include <linux/capability.h> 591da177e4SLinus Torvalds #include <linux/file.h> 609f3acc31SAl Viro #include <linux/fdtable.h> 611da177e4SLinus Torvalds #include <linux/string.h> 621da177e4SLinus Torvalds #include <linux/seq_file.h> 631da177e4SLinus Torvalds #include <linux/namei.h> 646b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 651da177e4SLinus Torvalds #include <linux/mm.h> 66a63d83f4SDavid Rientjes #include <linux/swap.h> 67b835996fSDipankar Sarma #include <linux/rcupdate.h> 681da177e4SLinus Torvalds #include <linux/kallsyms.h> 692ec220e2SKen Chen #include <linux/stacktrace.h> 70d85f50d5SNeil Horman #include <linux/resource.h> 715096add8SKees Cook #include <linux/module.h> 721da177e4SLinus Torvalds #include <linux/mount.h> 731da177e4SLinus Torvalds #include <linux/security.h> 741da177e4SLinus Torvalds #include <linux/ptrace.h> 750d094efeSRoland McGrath #include <linux/tracehook.h> 7687ebdc00SAndrew Morton #include <linux/printk.h> 77a424316cSPaul Menage #include <linux/cgroup.h> 781da177e4SLinus Torvalds #include <linux/cpuset.h> 791da177e4SLinus Torvalds #include <linux/audit.h> 805addc5ddSAl Viro #include <linux/poll.h> 811651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 828ac773b4SAlexey Dobriyan #include <linux/oom.h> 833cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 8460347f67SPavel Emelyanov #include <linux/pid_namespace.h> 8522d917d8SEric W. Biederman #include <linux/user_namespace.h> 865ad4e53bSAl Viro #include <linux/fs_struct.h> 875a0e3ad6STejun Heo #include <linux/slab.h> 88640708a2SPavel Emelyanov #include <linux/flex_array.h> 89f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 90f133eccaSChris Metcalf #include <asm/hardwall.h> 91f133eccaSChris Metcalf #endif 9243d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h> 931da177e4SLinus Torvalds #include "internal.h" 94faf60af1SCyrill Gorcunov #include "fd.h" 951da177e4SLinus Torvalds 960f2fe20fSEric W. Biederman /* NOTE: 970f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 980f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 990f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 1000f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 1010f2fe20fSEric W. Biederman * 1020f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 1030f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 1040f2fe20fSEric W. Biederman */ 1050f2fe20fSEric W. Biederman 1061da177e4SLinus Torvalds struct pid_entry { 1071da177e4SLinus Torvalds char *name; 108c5141e6dSEric Dumazet int len; 109d161a13fSAl Viro umode_t mode; 110c5ef1c42SArjan van de Ven const struct inode_operations *iop; 11100977a59SArjan van de Ven const struct file_operations *fop; 11220cdc894SEric W. Biederman union proc_op op; 1131da177e4SLinus Torvalds }; 1141da177e4SLinus Torvalds 11561a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 11620cdc894SEric W. Biederman .name = (NAME), \ 117c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 11820cdc894SEric W. Biederman .mode = MODE, \ 11920cdc894SEric W. Biederman .iop = IOP, \ 12020cdc894SEric W. Biederman .fop = FOP, \ 12120cdc894SEric W. Biederman .op = OP, \ 12220cdc894SEric W. Biederman } 12320cdc894SEric W. Biederman 124631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops) \ 125631f9c18SAlexey Dobriyan NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) 126631f9c18SAlexey Dobriyan #define LNK(NAME, get_link) \ 12761a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 12820cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 129631f9c18SAlexey Dobriyan { .proc_get_link = get_link } ) 130631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops) \ 131631f9c18SAlexey Dobriyan NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) 132631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read) \ 13361a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 13420cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 135631f9c18SAlexey Dobriyan { .proc_read = read } ) 136631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show) \ 137be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 138be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 139631f9c18SAlexey Dobriyan { .proc_show = show } ) 1401da177e4SLinus Torvalds 141aed54175SVegard Nossum /* 142aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 143aed54175SVegard Nossum * and .. links. 144aed54175SVegard Nossum */ 145aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 146aed54175SVegard Nossum unsigned int n) 147aed54175SVegard Nossum { 148aed54175SVegard Nossum unsigned int i; 149aed54175SVegard Nossum unsigned int count; 150aed54175SVegard Nossum 151aed54175SVegard Nossum count = 0; 152aed54175SVegard Nossum for (i = 0; i < n; ++i) { 153aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 154aed54175SVegard Nossum ++count; 155aed54175SVegard Nossum } 156aed54175SVegard Nossum 157aed54175SVegard Nossum return count; 158aed54175SVegard Nossum } 159aed54175SVegard Nossum 160f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root) 1611da177e4SLinus Torvalds { 1627c2c7d99SHugh Dickins int result = -ENOENT; 1637c2c7d99SHugh Dickins 1640494f6ecSMiklos Szeredi task_lock(task); 165f7ad3c6bSMiklos Szeredi if (task->fs) { 166f7ad3c6bSMiklos Szeredi get_fs_root(task->fs, root); 1677c2c7d99SHugh Dickins result = 0; 1687c2c7d99SHugh Dickins } 1690494f6ecSMiklos Szeredi task_unlock(task); 1707c2c7d99SHugh Dickins return result; 1710494f6ecSMiklos Szeredi } 1720494f6ecSMiklos Szeredi 1737773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path) 1740494f6ecSMiklos Szeredi { 1757773fbc5SCyrill Gorcunov struct task_struct *task = get_proc_task(dentry->d_inode); 1760494f6ecSMiklos Szeredi int result = -ENOENT; 17799f89551SEric W. Biederman 17899f89551SEric W. Biederman if (task) { 179f7ad3c6bSMiklos Szeredi task_lock(task); 180f7ad3c6bSMiklos Szeredi if (task->fs) { 181f7ad3c6bSMiklos Szeredi get_fs_pwd(task->fs, path); 182f7ad3c6bSMiklos Szeredi result = 0; 183f7ad3c6bSMiklos Szeredi } 184f7ad3c6bSMiklos Szeredi task_unlock(task); 18599f89551SEric W. Biederman put_task_struct(task); 18699f89551SEric W. Biederman } 1871da177e4SLinus Torvalds return result; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 1907773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path) 1911da177e4SLinus Torvalds { 1927773fbc5SCyrill Gorcunov struct task_struct *task = get_proc_task(dentry->d_inode); 1931da177e4SLinus Torvalds int result = -ENOENT; 19499f89551SEric W. Biederman 19599f89551SEric W. Biederman if (task) { 196f7ad3c6bSMiklos Szeredi result = get_task_root(task, path); 19799f89551SEric W. Biederman put_task_struct(task); 19899f89551SEric W. Biederman } 1991da177e4SLinus Torvalds return result; 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds int res = 0; 2051da177e4SLinus Torvalds unsigned int len; 2061da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2071da177e4SLinus Torvalds if (!mm) 2081da177e4SLinus Torvalds goto out; 2091da177e4SLinus Torvalds if (!mm->arg_end) 2101da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds if (len > PAGE_SIZE) 2151da177e4SLinus Torvalds len = PAGE_SIZE; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2201da177e4SLinus Torvalds // assume application is using setproctitle(3). 2211da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2221da177e4SLinus Torvalds len = strnlen(buffer, res); 2231da177e4SLinus Torvalds if (len < res) { 2241da177e4SLinus Torvalds res = len; 2251da177e4SLinus Torvalds } else { 2261da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2271da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2281da177e4SLinus Torvalds len = PAGE_SIZE - res; 2291da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2301da177e4SLinus Torvalds res = strnlen(buffer, res); 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds out_mm: 2341da177e4SLinus Torvalds mmput(mm); 2351da177e4SLinus Torvalds out: 2361da177e4SLinus Torvalds return res; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2401da177e4SLinus Torvalds { 241e7dcd999SCong Wang struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ); 2422fadaef4SAl Viro int res = PTR_ERR(mm); 2432fadaef4SAl Viro if (mm && !IS_ERR(mm)) { 2441da177e4SLinus Torvalds unsigned int nwords = 0; 245dfe6b7d9SHannes Eder do { 2461da177e4SLinus Torvalds nwords += 2; 247dfe6b7d9SHannes Eder } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 2481da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 2491da177e4SLinus Torvalds if (res > PAGE_SIZE) 2501da177e4SLinus Torvalds res = PAGE_SIZE; 2511da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 2521da177e4SLinus Torvalds mmput(mm); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds return res; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2591da177e4SLinus Torvalds /* 2601da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2611da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2621da177e4SLinus Torvalds */ 2631da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 2641da177e4SLinus Torvalds { 265ffb45122SAlexey Dobriyan unsigned long wchan; 2669281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds wchan = get_wchan(task); 2691da177e4SLinus Torvalds 2709d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 271f83ce3e6SJake Edge if (!ptrace_may_access(task, PTRACE_MODE_READ)) 272f83ce3e6SJake Edge return 0; 273f83ce3e6SJake Edge else 2741da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 2759d65cb4aSAlexey Dobriyan else 2769d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2791da177e4SLinus Torvalds 280a9712bc1SAl Viro static int lock_trace(struct task_struct *task) 281a9712bc1SAl Viro { 282a9712bc1SAl Viro int err = mutex_lock_killable(&task->signal->cred_guard_mutex); 283a9712bc1SAl Viro if (err) 284a9712bc1SAl Viro return err; 285a9712bc1SAl Viro if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) { 286a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 287a9712bc1SAl Viro return -EPERM; 288a9712bc1SAl Viro } 289a9712bc1SAl Viro return 0; 290a9712bc1SAl Viro } 291a9712bc1SAl Viro 292a9712bc1SAl Viro static void unlock_trace(struct task_struct *task) 293a9712bc1SAl Viro { 294a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 295a9712bc1SAl Viro } 296a9712bc1SAl Viro 2972ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 2982ec220e2SKen Chen 2992ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH 64 3002ec220e2SKen Chen 3012ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, 3022ec220e2SKen Chen struct pid *pid, struct task_struct *task) 3032ec220e2SKen Chen { 3042ec220e2SKen Chen struct stack_trace trace; 3052ec220e2SKen Chen unsigned long *entries; 306a9712bc1SAl Viro int err; 3072ec220e2SKen Chen int i; 3082ec220e2SKen Chen 3092ec220e2SKen Chen entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL); 3102ec220e2SKen Chen if (!entries) 3112ec220e2SKen Chen return -ENOMEM; 3122ec220e2SKen Chen 3132ec220e2SKen Chen trace.nr_entries = 0; 3142ec220e2SKen Chen trace.max_entries = MAX_STACK_TRACE_DEPTH; 3152ec220e2SKen Chen trace.entries = entries; 3162ec220e2SKen Chen trace.skip = 0; 317a9712bc1SAl Viro 318a9712bc1SAl Viro err = lock_trace(task); 319a9712bc1SAl Viro if (!err) { 3202ec220e2SKen Chen save_stack_trace_tsk(task, &trace); 3212ec220e2SKen Chen 3222ec220e2SKen Chen for (i = 0; i < trace.nr_entries; i++) { 32351e03149SKonstantin Khlebnikov seq_printf(m, "[<%pK>] %pS\n", 3242ec220e2SKen Chen (void *)entries[i], (void *)entries[i]); 3252ec220e2SKen Chen } 326a9712bc1SAl Viro unlock_trace(task); 327a9712bc1SAl Viro } 3282ec220e2SKen Chen kfree(entries); 3292ec220e2SKen Chen 330a9712bc1SAl Viro return err; 3312ec220e2SKen Chen } 3322ec220e2SKen Chen #endif 3332ec220e2SKen Chen 3341da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3351da177e4SLinus Torvalds /* 3361da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3371da177e4SLinus Torvalds */ 3381da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3391da177e4SLinus Torvalds { 340172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 341826e08b0SIngo Molnar (unsigned long long)task->se.sum_exec_runtime, 342826e08b0SIngo Molnar (unsigned long long)task->sched_info.run_delay, 3432d72376bSIngo Molnar task->sched_info.pcount); 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds #endif 3461da177e4SLinus Torvalds 3479745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3489745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3499745512cSArjan van de Ven { 3509745512cSArjan van de Ven int i; 35113d77c37SHiroshi Shimamoto struct inode *inode = m->private; 35213d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3539745512cSArjan van de Ven 35413d77c37SHiroshi Shimamoto if (!task) 35513d77c37SHiroshi Shimamoto return -ESRCH; 35613d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3579745512cSArjan van de Ven for (i = 0; i < 32; i++) { 35834e49d4fSJoe Perches struct latency_record *lr = &task->latency_record[i]; 35934e49d4fSJoe Perches if (lr->backtrace[0]) { 3609745512cSArjan van de Ven int q; 3619745512cSArjan van de Ven seq_printf(m, "%i %li %li", 36234e49d4fSJoe Perches lr->count, lr->time, lr->max); 3639745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 36434e49d4fSJoe Perches unsigned long bt = lr->backtrace[q]; 36534e49d4fSJoe Perches if (!bt) 3669745512cSArjan van de Ven break; 36734e49d4fSJoe Perches if (bt == ULONG_MAX) 3689745512cSArjan van de Ven break; 36934e49d4fSJoe Perches seq_printf(m, " %ps", (void *)bt); 3709745512cSArjan van de Ven } 3719d6de12fSAlexey Dobriyan seq_putc(m, '\n'); 3729745512cSArjan van de Ven } 3739745512cSArjan van de Ven 3749745512cSArjan van de Ven } 37513d77c37SHiroshi Shimamoto put_task_struct(task); 3769745512cSArjan van de Ven return 0; 3779745512cSArjan van de Ven } 3789745512cSArjan van de Ven 3799745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3809745512cSArjan van de Ven { 38113d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 382d6643d12SHiroshi Shimamoto } 383d6643d12SHiroshi Shimamoto 3849745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 3859745512cSArjan van de Ven size_t count, loff_t *offs) 3869745512cSArjan van de Ven { 387496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 3889745512cSArjan van de Ven 38913d77c37SHiroshi Shimamoto if (!task) 39013d77c37SHiroshi Shimamoto return -ESRCH; 3919745512cSArjan van de Ven clear_all_latency_tracing(task); 39213d77c37SHiroshi Shimamoto put_task_struct(task); 3939745512cSArjan van de Ven 3949745512cSArjan van de Ven return count; 3959745512cSArjan van de Ven } 3969745512cSArjan van de Ven 3979745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 3989745512cSArjan van de Ven .open = lstats_open, 3999745512cSArjan van de Ven .read = seq_read, 4009745512cSArjan van de Ven .write = lstats_write, 4019745512cSArjan van de Ven .llseek = seq_lseek, 40213d77c37SHiroshi Shimamoto .release = single_release, 4039745512cSArjan van de Ven }; 4049745512cSArjan van de Ven 4059745512cSArjan van de Ven #endif 4069745512cSArjan van de Ven 4071da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4081da177e4SLinus Torvalds { 409a7f638f9SDavid Rientjes unsigned long totalpages = totalram_pages + total_swap_pages; 410b95c35e7SOleg Nesterov unsigned long points = 0; 4111da177e4SLinus Torvalds 41219c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 413b95c35e7SOleg Nesterov if (pid_alive(task)) 414a7f638f9SDavid Rientjes points = oom_badness(task, NULL, NULL, totalpages) * 415a7f638f9SDavid Rientjes 1000 / totalpages; 41619c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4171da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds 420d85f50d5SNeil Horman struct limit_names { 421d85f50d5SNeil Horman char *name; 422d85f50d5SNeil Horman char *unit; 423d85f50d5SNeil Horman }; 424d85f50d5SNeil Horman 425d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 426cff4edb5SKees Cook [RLIMIT_CPU] = {"Max cpu time", "seconds"}, 427d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 428d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 429d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 430d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 431d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 432d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 433d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 434d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 435d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 436d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 437d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 438d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 439d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 440d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4418808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 442d85f50d5SNeil Horman }; 443d85f50d5SNeil Horman 444d85f50d5SNeil Horman /* Display limits for a process */ 445d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 446d85f50d5SNeil Horman { 447d85f50d5SNeil Horman unsigned int i; 448d85f50d5SNeil Horman int count = 0; 449d85f50d5SNeil Horman unsigned long flags; 450d85f50d5SNeil Horman char *bufptr = buffer; 451d85f50d5SNeil Horman 452d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 453d85f50d5SNeil Horman 454a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags)) 455d85f50d5SNeil Horman return 0; 456d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 457d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 458d85f50d5SNeil Horman 459d85f50d5SNeil Horman /* 460d85f50d5SNeil Horman * print the file header 461d85f50d5SNeil Horman */ 462d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 463d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 464d85f50d5SNeil Horman 465d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 466d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 467d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 468d85f50d5SNeil Horman lnames[i].name, "unlimited"); 469d85f50d5SNeil Horman else 470d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 471d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 472d85f50d5SNeil Horman 473d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 474d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 475d85f50d5SNeil Horman else 476d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 477d85f50d5SNeil Horman rlim[i].rlim_max); 478d85f50d5SNeil Horman 479d85f50d5SNeil Horman if (lnames[i].unit) 480d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 481d85f50d5SNeil Horman lnames[i].unit); 482d85f50d5SNeil Horman else 483d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 484d85f50d5SNeil Horman } 485d85f50d5SNeil Horman 486d85f50d5SNeil Horman return count; 487d85f50d5SNeil Horman } 488d85f50d5SNeil Horman 489ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 490ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer) 491ebcb6734SRoland McGrath { 492ebcb6734SRoland McGrath long nr; 493ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 494a9712bc1SAl Viro int res = lock_trace(task); 495a9712bc1SAl Viro if (res) 496a9712bc1SAl Viro return res; 497ebcb6734SRoland McGrath 498ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 499a9712bc1SAl Viro res = sprintf(buffer, "running\n"); 500a9712bc1SAl Viro else if (nr < 0) 501a9712bc1SAl Viro res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 502a9712bc1SAl Viro else 503a9712bc1SAl Viro res = sprintf(buffer, 504ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 505ebcb6734SRoland McGrath nr, 506ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 507ebcb6734SRoland McGrath sp, pc); 508a9712bc1SAl Viro unlock_trace(task); 509a9712bc1SAl Viro return res; 510ebcb6734SRoland McGrath } 511ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 512ebcb6734SRoland McGrath 5131da177e4SLinus Torvalds /************************************************************************/ 5141da177e4SLinus Torvalds /* Here the fs part begins */ 5151da177e4SLinus Torvalds /************************************************************************/ 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* permission checks */ 518778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5191da177e4SLinus Torvalds { 520778c1144SEric W. Biederman struct task_struct *task; 521778c1144SEric W. Biederman int allowed = 0; 522df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 523df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 524df26c40eSEric W. Biederman * information. 525778c1144SEric W. Biederman */ 526778c1144SEric W. Biederman task = get_proc_task(inode); 527df26c40eSEric W. Biederman if (task) { 528006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 529778c1144SEric W. Biederman put_task_struct(task); 530df26c40eSEric W. Biederman } 531778c1144SEric W. Biederman return allowed; 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5346b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr) 5356d76fa58SLinus Torvalds { 5366d76fa58SLinus Torvalds int error; 5376d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5386d76fa58SLinus Torvalds 5396d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5406d76fa58SLinus Torvalds return -EPERM; 5416d76fa58SLinus Torvalds 5426d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5431025774cSChristoph Hellwig if (error) 5446d76fa58SLinus Torvalds return error; 5451025774cSChristoph Hellwig 5461025774cSChristoph Hellwig setattr_copy(inode, attr); 5471025774cSChristoph Hellwig mark_inode_dirty(inode); 5481025774cSChristoph Hellwig return 0; 5496d76fa58SLinus Torvalds } 5506d76fa58SLinus Torvalds 5510499680aSVasiliy Kulikov /* 5520499680aSVasiliy Kulikov * May current process learn task's sched/cmdline info (for hide_pid_min=1) 5530499680aSVasiliy Kulikov * or euid/egid (for hide_pid_min=2)? 5540499680aSVasiliy Kulikov */ 5550499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid, 5560499680aSVasiliy Kulikov struct task_struct *task, 5570499680aSVasiliy Kulikov int hide_pid_min) 5580499680aSVasiliy Kulikov { 5590499680aSVasiliy Kulikov if (pid->hide_pid < hide_pid_min) 5600499680aSVasiliy Kulikov return true; 5610499680aSVasiliy Kulikov if (in_group_p(pid->pid_gid)) 5620499680aSVasiliy Kulikov return true; 5630499680aSVasiliy Kulikov return ptrace_may_access(task, PTRACE_MODE_READ); 5640499680aSVasiliy Kulikov } 5650499680aSVasiliy Kulikov 5660499680aSVasiliy Kulikov 5670499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask) 5680499680aSVasiliy Kulikov { 5690499680aSVasiliy Kulikov struct pid_namespace *pid = inode->i_sb->s_fs_info; 5700499680aSVasiliy Kulikov struct task_struct *task; 5710499680aSVasiliy Kulikov bool has_perms; 5720499680aSVasiliy Kulikov 5730499680aSVasiliy Kulikov task = get_proc_task(inode); 574a2ef990aSXiaotian Feng if (!task) 575a2ef990aSXiaotian Feng return -ESRCH; 5760499680aSVasiliy Kulikov has_perms = has_pid_permissions(pid, task, 1); 5770499680aSVasiliy Kulikov put_task_struct(task); 5780499680aSVasiliy Kulikov 5790499680aSVasiliy Kulikov if (!has_perms) { 5800499680aSVasiliy Kulikov if (pid->hide_pid == 2) { 5810499680aSVasiliy Kulikov /* 5820499680aSVasiliy Kulikov * Let's make getdents(), stat(), and open() 5830499680aSVasiliy Kulikov * consistent with each other. If a process 5840499680aSVasiliy Kulikov * may not stat() a file, it shouldn't be seen 5850499680aSVasiliy Kulikov * in procfs at all. 5860499680aSVasiliy Kulikov */ 5870499680aSVasiliy Kulikov return -ENOENT; 5880499680aSVasiliy Kulikov } 5890499680aSVasiliy Kulikov 5900499680aSVasiliy Kulikov return -EPERM; 5910499680aSVasiliy Kulikov } 5920499680aSVasiliy Kulikov return generic_permission(inode, mask); 5930499680aSVasiliy Kulikov } 5940499680aSVasiliy Kulikov 5950499680aSVasiliy Kulikov 5960499680aSVasiliy Kulikov 597c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5986d76fa58SLinus Torvalds .setattr = proc_setattr, 5996d76fa58SLinus Torvalds }; 6006d76fa58SLinus Torvalds 6011da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 6041da177e4SLinus Torvalds size_t count, loff_t *ppos) 6051da177e4SLinus Torvalds { 606496ad9aaSAl Viro struct inode * inode = file_inode(file); 6071da177e4SLinus Torvalds unsigned long page; 6081da177e4SLinus Torvalds ssize_t length; 60999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 61099f89551SEric W. Biederman 61199f89551SEric W. Biederman length = -ESRCH; 61299f89551SEric W. Biederman if (!task) 61399f89551SEric W. Biederman goto out_no_task; 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 6161da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 61799f89551SEric W. Biederman 61899f89551SEric W. Biederman length = -ENOMEM; 619e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 62099f89551SEric W. Biederman goto out; 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds if (length >= 0) 6251da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 6261da177e4SLinus Torvalds free_page(page); 62799f89551SEric W. Biederman out: 62899f89551SEric W. Biederman put_task_struct(task); 62999f89551SEric W. Biederman out_no_task: 6301da177e4SLinus Torvalds return length; 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 63300977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 6341da177e4SLinus Torvalds .read = proc_info_read, 63587df8424SArnd Bergmann .llseek = generic_file_llseek, 6361da177e4SLinus Torvalds }; 6371da177e4SLinus Torvalds 638be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 639be614086SEric W. Biederman { 640be614086SEric W. Biederman struct inode *inode = m->private; 641be614086SEric W. Biederman struct pid_namespace *ns; 642be614086SEric W. Biederman struct pid *pid; 643be614086SEric W. Biederman struct task_struct *task; 644be614086SEric W. Biederman int ret; 645be614086SEric W. Biederman 646be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 647be614086SEric W. Biederman pid = proc_pid(inode); 648be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 649be614086SEric W. Biederman if (!task) 650be614086SEric W. Biederman return -ESRCH; 651be614086SEric W. Biederman 652be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 653be614086SEric W. Biederman 654be614086SEric W. Biederman put_task_struct(task); 655be614086SEric W. Biederman return ret; 656be614086SEric W. Biederman } 657be614086SEric W. Biederman 658be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 659be614086SEric W. Biederman { 660c6a34058SJovi Zhang return single_open(filp, proc_single_show, inode); 661be614086SEric W. Biederman } 662be614086SEric W. Biederman 663be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 664be614086SEric W. Biederman .open = proc_single_open, 665be614086SEric W. Biederman .read = seq_read, 666be614086SEric W. Biederman .llseek = seq_lseek, 667be614086SEric W. Biederman .release = single_release, 668be614086SEric W. Biederman }; 669be614086SEric W. Biederman 670b409e578SCong Wang static int __mem_open(struct inode *inode, struct file *file, unsigned int mode) 6711da177e4SLinus Torvalds { 672496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 673e268337dSLinus Torvalds struct mm_struct *mm; 674e268337dSLinus Torvalds 675e268337dSLinus Torvalds if (!task) 676e268337dSLinus Torvalds return -ESRCH; 677e268337dSLinus Torvalds 678b409e578SCong Wang mm = mm_access(task, mode); 679e268337dSLinus Torvalds put_task_struct(task); 680e268337dSLinus Torvalds 681e268337dSLinus Torvalds if (IS_ERR(mm)) 682e268337dSLinus Torvalds return PTR_ERR(mm); 683e268337dSLinus Torvalds 6846d08f2c7SOleg Nesterov if (mm) { 6856d08f2c7SOleg Nesterov /* ensure this mm_struct can't be freed */ 6866d08f2c7SOleg Nesterov atomic_inc(&mm->mm_count); 6876d08f2c7SOleg Nesterov /* but do not pin its memory */ 6886d08f2c7SOleg Nesterov mmput(mm); 6896d08f2c7SOleg Nesterov } 6906d08f2c7SOleg Nesterov 691e268337dSLinus Torvalds file->private_data = mm; 692e268337dSLinus Torvalds 6931da177e4SLinus Torvalds return 0; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 696b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file) 697b409e578SCong Wang { 698bc452b4bSDjalal Harouni int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH); 699bc452b4bSDjalal Harouni 700bc452b4bSDjalal Harouni /* OK to pass negative loff_t, we can catch out-of-range */ 701bc452b4bSDjalal Harouni file->f_mode |= FMODE_UNSIGNED_OFFSET; 702bc452b4bSDjalal Harouni 703bc452b4bSDjalal Harouni return ret; 704b409e578SCong Wang } 705b409e578SCong Wang 706572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf, 707572d34b9SOleg Nesterov size_t count, loff_t *ppos, int write) 7081da177e4SLinus Torvalds { 709e268337dSLinus Torvalds struct mm_struct *mm = file->private_data; 710572d34b9SOleg Nesterov unsigned long addr = *ppos; 711572d34b9SOleg Nesterov ssize_t copied; 7121da177e4SLinus Torvalds char *page; 7131da177e4SLinus Torvalds 714e268337dSLinus Torvalds if (!mm) 715e268337dSLinus Torvalds return 0; 7161da177e4SLinus Torvalds 717e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7181da177e4SLinus Torvalds if (!page) 719e268337dSLinus Torvalds return -ENOMEM; 7201da177e4SLinus Torvalds 721f7ca54f4SFrederik Deweerdt copied = 0; 7226d08f2c7SOleg Nesterov if (!atomic_inc_not_zero(&mm->mm_users)) 7236d08f2c7SOleg Nesterov goto free; 7246d08f2c7SOleg Nesterov 7251da177e4SLinus Torvalds while (count > 0) { 726572d34b9SOleg Nesterov int this_len = min_t(int, count, PAGE_SIZE); 7271da177e4SLinus Torvalds 728572d34b9SOleg Nesterov if (write && copy_from_user(page, buf, this_len)) { 7291da177e4SLinus Torvalds copied = -EFAULT; 7301da177e4SLinus Torvalds break; 7311da177e4SLinus Torvalds } 732572d34b9SOleg Nesterov 733572d34b9SOleg Nesterov this_len = access_remote_vm(mm, addr, page, this_len, write); 734572d34b9SOleg Nesterov if (!this_len) { 7351da177e4SLinus Torvalds if (!copied) 7361da177e4SLinus Torvalds copied = -EIO; 7371da177e4SLinus Torvalds break; 7381da177e4SLinus Torvalds } 739572d34b9SOleg Nesterov 740572d34b9SOleg Nesterov if (!write && copy_to_user(buf, page, this_len)) { 741572d34b9SOleg Nesterov copied = -EFAULT; 742572d34b9SOleg Nesterov break; 7431da177e4SLinus Torvalds } 744572d34b9SOleg Nesterov 745572d34b9SOleg Nesterov buf += this_len; 746572d34b9SOleg Nesterov addr += this_len; 747572d34b9SOleg Nesterov copied += this_len; 748572d34b9SOleg Nesterov count -= this_len; 749572d34b9SOleg Nesterov } 750572d34b9SOleg Nesterov *ppos = addr; 75130cd8903SKOSAKI Motohiro 7526d08f2c7SOleg Nesterov mmput(mm); 7536d08f2c7SOleg Nesterov free: 75430cd8903SKOSAKI Motohiro free_page((unsigned long) page); 7551da177e4SLinus Torvalds return copied; 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 758572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf, 759572d34b9SOleg Nesterov size_t count, loff_t *ppos) 760572d34b9SOleg Nesterov { 761572d34b9SOleg Nesterov return mem_rw(file, buf, count, ppos, 0); 762572d34b9SOleg Nesterov } 763572d34b9SOleg Nesterov 764572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf, 765572d34b9SOleg Nesterov size_t count, loff_t *ppos) 766572d34b9SOleg Nesterov { 767572d34b9SOleg Nesterov return mem_rw(file, (char __user*)buf, count, ppos, 1); 768572d34b9SOleg Nesterov } 769572d34b9SOleg Nesterov 77085863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 7711da177e4SLinus Torvalds { 7721da177e4SLinus Torvalds switch (orig) { 7731da177e4SLinus Torvalds case 0: 7741da177e4SLinus Torvalds file->f_pos = offset; 7751da177e4SLinus Torvalds break; 7761da177e4SLinus Torvalds case 1: 7771da177e4SLinus Torvalds file->f_pos += offset; 7781da177e4SLinus Torvalds break; 7791da177e4SLinus Torvalds default: 7801da177e4SLinus Torvalds return -EINVAL; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds force_successful_syscall_return(); 7831da177e4SLinus Torvalds return file->f_pos; 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds 786e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file) 787e268337dSLinus Torvalds { 788e268337dSLinus Torvalds struct mm_struct *mm = file->private_data; 78971879d3cSOleg Nesterov if (mm) 7906d08f2c7SOleg Nesterov mmdrop(mm); 791e268337dSLinus Torvalds return 0; 792e268337dSLinus Torvalds } 793e268337dSLinus Torvalds 79400977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 7951da177e4SLinus Torvalds .llseek = mem_lseek, 7961da177e4SLinus Torvalds .read = mem_read, 7971da177e4SLinus Torvalds .write = mem_write, 7981da177e4SLinus Torvalds .open = mem_open, 799e268337dSLinus Torvalds .release = mem_release, 8001da177e4SLinus Torvalds }; 8011da177e4SLinus Torvalds 802b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file) 803b409e578SCong Wang { 804b409e578SCong Wang return __mem_open(inode, file, PTRACE_MODE_READ); 805b409e578SCong Wang } 806b409e578SCong Wang 807315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 808315e28c8SJames Pearson size_t count, loff_t *ppos) 809315e28c8SJames Pearson { 810315e28c8SJames Pearson char *page; 811315e28c8SJames Pearson unsigned long src = *ppos; 812b409e578SCong Wang int ret = 0; 813b409e578SCong Wang struct mm_struct *mm = file->private_data; 814315e28c8SJames Pearson 815b409e578SCong Wang if (!mm) 816b409e578SCong Wang return 0; 817315e28c8SJames Pearson 818315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 819315e28c8SJames Pearson if (!page) 820b409e578SCong Wang return -ENOMEM; 821315e28c8SJames Pearson 822d6f64b89SAl Viro ret = 0; 823b409e578SCong Wang if (!atomic_inc_not_zero(&mm->mm_users)) 824b409e578SCong Wang goto free; 825315e28c8SJames Pearson while (count > 0) { 826e8905ec2SDjalal Harouni size_t this_len, max_len; 827e8905ec2SDjalal Harouni int retval; 828e8905ec2SDjalal Harouni 829e8905ec2SDjalal Harouni if (src >= (mm->env_end - mm->env_start)) 830e8905ec2SDjalal Harouni break; 831315e28c8SJames Pearson 832315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 833315e28c8SJames Pearson 834e8905ec2SDjalal Harouni max_len = min_t(size_t, PAGE_SIZE, count); 835e8905ec2SDjalal Harouni this_len = min(max_len, this_len); 836315e28c8SJames Pearson 837b409e578SCong Wang retval = access_remote_vm(mm, (mm->env_start + src), 838315e28c8SJames Pearson page, this_len, 0); 839315e28c8SJames Pearson 840315e28c8SJames Pearson if (retval <= 0) { 841315e28c8SJames Pearson ret = retval; 842315e28c8SJames Pearson break; 843315e28c8SJames Pearson } 844315e28c8SJames Pearson 845315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 846315e28c8SJames Pearson ret = -EFAULT; 847315e28c8SJames Pearson break; 848315e28c8SJames Pearson } 849315e28c8SJames Pearson 850315e28c8SJames Pearson ret += retval; 851315e28c8SJames Pearson src += retval; 852315e28c8SJames Pearson buf += retval; 853315e28c8SJames Pearson count -= retval; 854315e28c8SJames Pearson } 855315e28c8SJames Pearson *ppos = src; 856315e28c8SJames Pearson mmput(mm); 857b409e578SCong Wang 858b409e578SCong Wang free: 859315e28c8SJames Pearson free_page((unsigned long) page); 860315e28c8SJames Pearson return ret; 861315e28c8SJames Pearson } 862315e28c8SJames Pearson 863315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 864b409e578SCong Wang .open = environ_open, 865315e28c8SJames Pearson .read = environ_read, 86687df8424SArnd Bergmann .llseek = generic_file_llseek, 867b409e578SCong Wang .release = mem_release, 868315e28c8SJames Pearson }; 869315e28c8SJames Pearson 870fa0cbbf1SDavid Rientjes static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count, 871fa0cbbf1SDavid Rientjes loff_t *ppos) 872fa0cbbf1SDavid Rientjes { 873496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 874fa0cbbf1SDavid Rientjes char buffer[PROC_NUMBUF]; 875fa0cbbf1SDavid Rientjes int oom_adj = OOM_ADJUST_MIN; 876fa0cbbf1SDavid Rientjes size_t len; 877fa0cbbf1SDavid Rientjes unsigned long flags; 878fa0cbbf1SDavid Rientjes 879fa0cbbf1SDavid Rientjes if (!task) 880fa0cbbf1SDavid Rientjes return -ESRCH; 881fa0cbbf1SDavid Rientjes if (lock_task_sighand(task, &flags)) { 882fa0cbbf1SDavid Rientjes if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX) 883fa0cbbf1SDavid Rientjes oom_adj = OOM_ADJUST_MAX; 884fa0cbbf1SDavid Rientjes else 885fa0cbbf1SDavid Rientjes oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) / 886fa0cbbf1SDavid Rientjes OOM_SCORE_ADJ_MAX; 887fa0cbbf1SDavid Rientjes unlock_task_sighand(task, &flags); 888fa0cbbf1SDavid Rientjes } 889fa0cbbf1SDavid Rientjes put_task_struct(task); 890fa0cbbf1SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj); 891fa0cbbf1SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len); 892fa0cbbf1SDavid Rientjes } 893fa0cbbf1SDavid Rientjes 894fa0cbbf1SDavid Rientjes static ssize_t oom_adj_write(struct file *file, const char __user *buf, 895fa0cbbf1SDavid Rientjes size_t count, loff_t *ppos) 896fa0cbbf1SDavid Rientjes { 897fa0cbbf1SDavid Rientjes struct task_struct *task; 898fa0cbbf1SDavid Rientjes char buffer[PROC_NUMBUF]; 899fa0cbbf1SDavid Rientjes int oom_adj; 900fa0cbbf1SDavid Rientjes unsigned long flags; 901fa0cbbf1SDavid Rientjes int err; 902fa0cbbf1SDavid Rientjes 903fa0cbbf1SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 904fa0cbbf1SDavid Rientjes if (count > sizeof(buffer) - 1) 905fa0cbbf1SDavid Rientjes count = sizeof(buffer) - 1; 906fa0cbbf1SDavid Rientjes if (copy_from_user(buffer, buf, count)) { 907fa0cbbf1SDavid Rientjes err = -EFAULT; 908fa0cbbf1SDavid Rientjes goto out; 909fa0cbbf1SDavid Rientjes } 910fa0cbbf1SDavid Rientjes 911fa0cbbf1SDavid Rientjes err = kstrtoint(strstrip(buffer), 0, &oom_adj); 912fa0cbbf1SDavid Rientjes if (err) 913fa0cbbf1SDavid Rientjes goto out; 914fa0cbbf1SDavid Rientjes if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) && 915fa0cbbf1SDavid Rientjes oom_adj != OOM_DISABLE) { 916fa0cbbf1SDavid Rientjes err = -EINVAL; 917fa0cbbf1SDavid Rientjes goto out; 918fa0cbbf1SDavid Rientjes } 919fa0cbbf1SDavid Rientjes 920496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 921fa0cbbf1SDavid Rientjes if (!task) { 922fa0cbbf1SDavid Rientjes err = -ESRCH; 923fa0cbbf1SDavid Rientjes goto out; 924fa0cbbf1SDavid Rientjes } 925fa0cbbf1SDavid Rientjes 926fa0cbbf1SDavid Rientjes task_lock(task); 927fa0cbbf1SDavid Rientjes if (!task->mm) { 928fa0cbbf1SDavid Rientjes err = -EINVAL; 929fa0cbbf1SDavid Rientjes goto err_task_lock; 930fa0cbbf1SDavid Rientjes } 931fa0cbbf1SDavid Rientjes 932fa0cbbf1SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 933fa0cbbf1SDavid Rientjes err = -ESRCH; 934fa0cbbf1SDavid Rientjes goto err_task_lock; 935fa0cbbf1SDavid Rientjes } 936fa0cbbf1SDavid Rientjes 937fa0cbbf1SDavid Rientjes /* 938fa0cbbf1SDavid Rientjes * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum 939fa0cbbf1SDavid Rientjes * value is always attainable. 940fa0cbbf1SDavid Rientjes */ 941fa0cbbf1SDavid Rientjes if (oom_adj == OOM_ADJUST_MAX) 942fa0cbbf1SDavid Rientjes oom_adj = OOM_SCORE_ADJ_MAX; 943fa0cbbf1SDavid Rientjes else 944fa0cbbf1SDavid Rientjes oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; 945fa0cbbf1SDavid Rientjes 946fa0cbbf1SDavid Rientjes if (oom_adj < task->signal->oom_score_adj && 947fa0cbbf1SDavid Rientjes !capable(CAP_SYS_RESOURCE)) { 948fa0cbbf1SDavid Rientjes err = -EACCES; 949fa0cbbf1SDavid Rientjes goto err_sighand; 950fa0cbbf1SDavid Rientjes } 951fa0cbbf1SDavid Rientjes 952fa0cbbf1SDavid Rientjes /* 953fa0cbbf1SDavid Rientjes * /proc/pid/oom_adj is provided for legacy purposes, ask users to use 954fa0cbbf1SDavid Rientjes * /proc/pid/oom_score_adj instead. 955fa0cbbf1SDavid Rientjes */ 95687ebdc00SAndrew Morton pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n", 957fa0cbbf1SDavid Rientjes current->comm, task_pid_nr(current), task_pid_nr(task), 958fa0cbbf1SDavid Rientjes task_pid_nr(task)); 959fa0cbbf1SDavid Rientjes 960fa0cbbf1SDavid Rientjes task->signal->oom_score_adj = oom_adj; 961fa0cbbf1SDavid Rientjes trace_oom_score_adj_update(task); 962fa0cbbf1SDavid Rientjes err_sighand: 963fa0cbbf1SDavid Rientjes unlock_task_sighand(task, &flags); 964fa0cbbf1SDavid Rientjes err_task_lock: 965fa0cbbf1SDavid Rientjes task_unlock(task); 966fa0cbbf1SDavid Rientjes put_task_struct(task); 967fa0cbbf1SDavid Rientjes out: 968fa0cbbf1SDavid Rientjes return err < 0 ? err : count; 969fa0cbbf1SDavid Rientjes } 970fa0cbbf1SDavid Rientjes 971fa0cbbf1SDavid Rientjes static const struct file_operations proc_oom_adj_operations = { 972fa0cbbf1SDavid Rientjes .read = oom_adj_read, 973fa0cbbf1SDavid Rientjes .write = oom_adj_write, 974fa0cbbf1SDavid Rientjes .llseek = generic_file_llseek, 975fa0cbbf1SDavid Rientjes }; 976fa0cbbf1SDavid Rientjes 977a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf, 978a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 979a63d83f4SDavid Rientjes { 980496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 981a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 982a9c58b90SDavid Rientjes short oom_score_adj = OOM_SCORE_ADJ_MIN; 983a63d83f4SDavid Rientjes unsigned long flags; 984a63d83f4SDavid Rientjes size_t len; 985a63d83f4SDavid Rientjes 986a63d83f4SDavid Rientjes if (!task) 987a63d83f4SDavid Rientjes return -ESRCH; 988a63d83f4SDavid Rientjes if (lock_task_sighand(task, &flags)) { 989a63d83f4SDavid Rientjes oom_score_adj = task->signal->oom_score_adj; 990a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 991a63d83f4SDavid Rientjes } 992a63d83f4SDavid Rientjes put_task_struct(task); 993a9c58b90SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj); 994a63d83f4SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len); 995a63d83f4SDavid Rientjes } 996a63d83f4SDavid Rientjes 997a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, 998a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 999a63d83f4SDavid Rientjes { 1000a63d83f4SDavid Rientjes struct task_struct *task; 1001a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 1002a63d83f4SDavid Rientjes unsigned long flags; 10030a8cb8e3SAlexey Dobriyan int oom_score_adj; 1004a63d83f4SDavid Rientjes int err; 1005a63d83f4SDavid Rientjes 1006a63d83f4SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 1007a63d83f4SDavid Rientjes if (count > sizeof(buffer) - 1) 1008a63d83f4SDavid Rientjes count = sizeof(buffer) - 1; 1009723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) { 1010723548bfSDavid Rientjes err = -EFAULT; 1011723548bfSDavid Rientjes goto out; 1012723548bfSDavid Rientjes } 1013a63d83f4SDavid Rientjes 10140a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &oom_score_adj); 1015a63d83f4SDavid Rientjes if (err) 1016723548bfSDavid Rientjes goto out; 1017a63d83f4SDavid Rientjes if (oom_score_adj < OOM_SCORE_ADJ_MIN || 1018723548bfSDavid Rientjes oom_score_adj > OOM_SCORE_ADJ_MAX) { 1019723548bfSDavid Rientjes err = -EINVAL; 1020723548bfSDavid Rientjes goto out; 1021723548bfSDavid Rientjes } 1022a63d83f4SDavid Rientjes 1023496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 1024723548bfSDavid Rientjes if (!task) { 1025723548bfSDavid Rientjes err = -ESRCH; 1026723548bfSDavid Rientjes goto out; 1027723548bfSDavid Rientjes } 1028a63d83f4SDavid Rientjes 10293d5992d2SYing Han task_lock(task); 10303d5992d2SYing Han if (!task->mm) { 1031723548bfSDavid Rientjes err = -EINVAL; 1032723548bfSDavid Rientjes goto err_task_lock; 10333d5992d2SYing Han } 1034d19d5476SDavid Rientjes 1035d19d5476SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 1036d19d5476SDavid Rientjes err = -ESRCH; 1037d19d5476SDavid Rientjes goto err_task_lock; 1038d19d5476SDavid Rientjes } 1039d19d5476SDavid Rientjes 1040a9c58b90SDavid Rientjes if ((short)oom_score_adj < task->signal->oom_score_adj_min && 1041d19d5476SDavid Rientjes !capable(CAP_SYS_RESOURCE)) { 1042d19d5476SDavid Rientjes err = -EACCES; 1043d19d5476SDavid Rientjes goto err_sighand; 1044d19d5476SDavid Rientjes } 1045d19d5476SDavid Rientjes 1046a9c58b90SDavid Rientjes task->signal->oom_score_adj = (short)oom_score_adj; 1047dabb16f6SMandeep Singh Baines if (has_capability_noaudit(current, CAP_SYS_RESOURCE)) 1048a9c58b90SDavid Rientjes task->signal->oom_score_adj_min = (short)oom_score_adj; 104943d2b113SKAMEZAWA Hiroyuki trace_oom_score_adj_update(task); 105001dc52ebSDavidlohr Bueso 1051723548bfSDavid Rientjes err_sighand: 1052a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 1053d19d5476SDavid Rientjes err_task_lock: 1054d19d5476SDavid Rientjes task_unlock(task); 1055a63d83f4SDavid Rientjes put_task_struct(task); 1056723548bfSDavid Rientjes out: 1057723548bfSDavid Rientjes return err < 0 ? err : count; 1058a63d83f4SDavid Rientjes } 1059a63d83f4SDavid Rientjes 1060a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = { 1061a63d83f4SDavid Rientjes .read = oom_score_adj_read, 1062a63d83f4SDavid Rientjes .write = oom_score_adj_write, 10636038f373SArnd Bergmann .llseek = default_llseek, 1064a63d83f4SDavid Rientjes }; 1065a63d83f4SDavid Rientjes 10661da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10671da177e4SLinus Torvalds #define TMPBUFLEN 21 10681da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10691da177e4SLinus Torvalds size_t count, loff_t *ppos) 10701da177e4SLinus Torvalds { 1071496ad9aaSAl Viro struct inode * inode = file_inode(file); 107299f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10731da177e4SLinus Torvalds ssize_t length; 10741da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10751da177e4SLinus Torvalds 107699f89551SEric W. Biederman if (!task) 107799f89551SEric W. Biederman return -ESRCH; 10781da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 1079e1760bd5SEric W. Biederman from_kuid(file->f_cred->user_ns, 1080e1760bd5SEric W. Biederman audit_get_loginuid(task))); 108199f89551SEric W. Biederman put_task_struct(task); 10821da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10861da177e4SLinus Torvalds size_t count, loff_t *ppos) 10871da177e4SLinus Torvalds { 1088496ad9aaSAl Viro struct inode * inode = file_inode(file); 10891da177e4SLinus Torvalds char *page, *tmp; 10901da177e4SLinus Torvalds ssize_t length; 10911da177e4SLinus Torvalds uid_t loginuid; 1092e1760bd5SEric W. Biederman kuid_t kloginuid; 10931da177e4SLinus Torvalds 10947dc52157SPaul E. McKenney rcu_read_lock(); 10957dc52157SPaul E. McKenney if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { 10967dc52157SPaul E. McKenney rcu_read_unlock(); 10971da177e4SLinus Torvalds return -EPERM; 10987dc52157SPaul E. McKenney } 10997dc52157SPaul E. McKenney rcu_read_unlock(); 11001da177e4SLinus Torvalds 1101e0182909SAl Viro if (count >= PAGE_SIZE) 1102e0182909SAl Viro count = PAGE_SIZE - 1; 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds if (*ppos != 0) { 11051da177e4SLinus Torvalds /* No partial writes. */ 11061da177e4SLinus Torvalds return -EINVAL; 11071da177e4SLinus Torvalds } 1108e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 11091da177e4SLinus Torvalds if (!page) 11101da177e4SLinus Torvalds return -ENOMEM; 11111da177e4SLinus Torvalds length = -EFAULT; 11121da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 11131da177e4SLinus Torvalds goto out_free_page; 11141da177e4SLinus Torvalds 1115e0182909SAl Viro page[count] = '\0'; 11161da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 11171da177e4SLinus Torvalds if (tmp == page) { 11181da177e4SLinus Torvalds length = -EINVAL; 11191da177e4SLinus Torvalds goto out_free_page; 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds } 1122e1760bd5SEric W. Biederman kloginuid = make_kuid(file->f_cred->user_ns, loginuid); 1123e1760bd5SEric W. Biederman if (!uid_valid(kloginuid)) { 1124e1760bd5SEric W. Biederman length = -EINVAL; 1125e1760bd5SEric W. Biederman goto out_free_page; 1126e1760bd5SEric W. Biederman } 1127e1760bd5SEric W. Biederman 1128e1760bd5SEric W. Biederman length = audit_set_loginuid(kloginuid); 11291da177e4SLinus Torvalds if (likely(length == 0)) 11301da177e4SLinus Torvalds length = count; 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds out_free_page: 11331da177e4SLinus Torvalds free_page((unsigned long) page); 11341da177e4SLinus Torvalds return length; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 113700977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 11381da177e4SLinus Torvalds .read = proc_loginuid_read, 11391da177e4SLinus Torvalds .write = proc_loginuid_write, 114087df8424SArnd Bergmann .llseek = generic_file_llseek, 11411da177e4SLinus Torvalds }; 11421e0bd755SEric Paris 11431e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 11441e0bd755SEric Paris size_t count, loff_t *ppos) 11451e0bd755SEric Paris { 1146496ad9aaSAl Viro struct inode * inode = file_inode(file); 11471e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 11481e0bd755SEric Paris ssize_t length; 11491e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 11501e0bd755SEric Paris 11511e0bd755SEric Paris if (!task) 11521e0bd755SEric Paris return -ESRCH; 11531e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11541e0bd755SEric Paris audit_get_sessionid(task)); 11551e0bd755SEric Paris put_task_struct(task); 11561e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11571e0bd755SEric Paris } 11581e0bd755SEric Paris 11591e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11601e0bd755SEric Paris .read = proc_sessionid_read, 116187df8424SArnd Bergmann .llseek = generic_file_llseek, 11621e0bd755SEric Paris }; 11631da177e4SLinus Torvalds #endif 11641da177e4SLinus Torvalds 1165f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1166f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1167f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1168f4f154fdSAkinobu Mita { 1169496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 1170f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1171f4f154fdSAkinobu Mita size_t len; 1172f4f154fdSAkinobu Mita int make_it_fail; 1173f4f154fdSAkinobu Mita 1174f4f154fdSAkinobu Mita if (!task) 1175f4f154fdSAkinobu Mita return -ESRCH; 1176f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1177f4f154fdSAkinobu Mita put_task_struct(task); 1178f4f154fdSAkinobu Mita 1179f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11800c28f287SAkinobu Mita 11810c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1182f4f154fdSAkinobu Mita } 1183f4f154fdSAkinobu Mita 1184f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1185f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1186f4f154fdSAkinobu Mita { 1187f4f154fdSAkinobu Mita struct task_struct *task; 1188f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1189f4f154fdSAkinobu Mita int make_it_fail; 1190f4f154fdSAkinobu Mita 1191f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1192f4f154fdSAkinobu Mita return -EPERM; 1193f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1194f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1195f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1196f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1197f4f154fdSAkinobu Mita return -EFAULT; 1198cba8aafeSVincent Li make_it_fail = simple_strtol(strstrip(buffer), &end, 0); 1199cba8aafeSVincent Li if (*end) 1200cba8aafeSVincent Li return -EINVAL; 1201496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 1202f4f154fdSAkinobu Mita if (!task) 1203f4f154fdSAkinobu Mita return -ESRCH; 1204f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1205f4f154fdSAkinobu Mita put_task_struct(task); 1206cba8aafeSVincent Li 1207cba8aafeSVincent Li return count; 1208f4f154fdSAkinobu Mita } 1209f4f154fdSAkinobu Mita 121000977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1211f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1212f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 121387df8424SArnd Bergmann .llseek = generic_file_llseek, 1214f4f154fdSAkinobu Mita }; 1215f4f154fdSAkinobu Mita #endif 1216f4f154fdSAkinobu Mita 12179745512cSArjan van de Ven 121843ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 121943ae34cbSIngo Molnar /* 122043ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 122143ae34cbSIngo Molnar */ 122243ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 122343ae34cbSIngo Molnar { 122443ae34cbSIngo Molnar struct inode *inode = m->private; 122543ae34cbSIngo Molnar struct task_struct *p; 122643ae34cbSIngo Molnar 122743ae34cbSIngo Molnar p = get_proc_task(inode); 122843ae34cbSIngo Molnar if (!p) 122943ae34cbSIngo Molnar return -ESRCH; 123043ae34cbSIngo Molnar proc_sched_show_task(p, m); 123143ae34cbSIngo Molnar 123243ae34cbSIngo Molnar put_task_struct(p); 123343ae34cbSIngo Molnar 123443ae34cbSIngo Molnar return 0; 123543ae34cbSIngo Molnar } 123643ae34cbSIngo Molnar 123743ae34cbSIngo Molnar static ssize_t 123843ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 123943ae34cbSIngo Molnar size_t count, loff_t *offset) 124043ae34cbSIngo Molnar { 1241496ad9aaSAl Viro struct inode *inode = file_inode(file); 124243ae34cbSIngo Molnar struct task_struct *p; 124343ae34cbSIngo Molnar 124443ae34cbSIngo Molnar p = get_proc_task(inode); 124543ae34cbSIngo Molnar if (!p) 124643ae34cbSIngo Molnar return -ESRCH; 124743ae34cbSIngo Molnar proc_sched_set_task(p); 124843ae34cbSIngo Molnar 124943ae34cbSIngo Molnar put_task_struct(p); 125043ae34cbSIngo Molnar 125143ae34cbSIngo Molnar return count; 125243ae34cbSIngo Molnar } 125343ae34cbSIngo Molnar 125443ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 125543ae34cbSIngo Molnar { 1256c6a34058SJovi Zhang return single_open(filp, sched_show, inode); 125743ae34cbSIngo Molnar } 125843ae34cbSIngo Molnar 125943ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 126043ae34cbSIngo Molnar .open = sched_open, 126143ae34cbSIngo Molnar .read = seq_read, 126243ae34cbSIngo Molnar .write = sched_write, 126343ae34cbSIngo Molnar .llseek = seq_lseek, 12645ea473a1SAlexey Dobriyan .release = single_release, 126543ae34cbSIngo Molnar }; 126643ae34cbSIngo Molnar 126743ae34cbSIngo Molnar #endif 126843ae34cbSIngo Molnar 12695091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 12705091faa4SMike Galbraith /* 12715091faa4SMike Galbraith * Print out autogroup related information: 12725091faa4SMike Galbraith */ 12735091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v) 12745091faa4SMike Galbraith { 12755091faa4SMike Galbraith struct inode *inode = m->private; 12765091faa4SMike Galbraith struct task_struct *p; 12775091faa4SMike Galbraith 12785091faa4SMike Galbraith p = get_proc_task(inode); 12795091faa4SMike Galbraith if (!p) 12805091faa4SMike Galbraith return -ESRCH; 12815091faa4SMike Galbraith proc_sched_autogroup_show_task(p, m); 12825091faa4SMike Galbraith 12835091faa4SMike Galbraith put_task_struct(p); 12845091faa4SMike Galbraith 12855091faa4SMike Galbraith return 0; 12865091faa4SMike Galbraith } 12875091faa4SMike Galbraith 12885091faa4SMike Galbraith static ssize_t 12895091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf, 12905091faa4SMike Galbraith size_t count, loff_t *offset) 12915091faa4SMike Galbraith { 1292496ad9aaSAl Viro struct inode *inode = file_inode(file); 12935091faa4SMike Galbraith struct task_struct *p; 12945091faa4SMike Galbraith char buffer[PROC_NUMBUF]; 12950a8cb8e3SAlexey Dobriyan int nice; 12965091faa4SMike Galbraith int err; 12975091faa4SMike Galbraith 12985091faa4SMike Galbraith memset(buffer, 0, sizeof(buffer)); 12995091faa4SMike Galbraith if (count > sizeof(buffer) - 1) 13005091faa4SMike Galbraith count = sizeof(buffer) - 1; 13015091faa4SMike Galbraith if (copy_from_user(buffer, buf, count)) 13025091faa4SMike Galbraith return -EFAULT; 13035091faa4SMike Galbraith 13040a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &nice); 13050a8cb8e3SAlexey Dobriyan if (err < 0) 13060a8cb8e3SAlexey Dobriyan return err; 13075091faa4SMike Galbraith 13085091faa4SMike Galbraith p = get_proc_task(inode); 13095091faa4SMike Galbraith if (!p) 13105091faa4SMike Galbraith return -ESRCH; 13115091faa4SMike Galbraith 13122e5b5b3aSHiroshi Shimamoto err = proc_sched_autogroup_set_nice(p, nice); 13135091faa4SMike Galbraith if (err) 13145091faa4SMike Galbraith count = err; 13155091faa4SMike Galbraith 13165091faa4SMike Galbraith put_task_struct(p); 13175091faa4SMike Galbraith 13185091faa4SMike Galbraith return count; 13195091faa4SMike Galbraith } 13205091faa4SMike Galbraith 13215091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp) 13225091faa4SMike Galbraith { 13235091faa4SMike Galbraith int ret; 13245091faa4SMike Galbraith 13255091faa4SMike Galbraith ret = single_open(filp, sched_autogroup_show, NULL); 13265091faa4SMike Galbraith if (!ret) { 13275091faa4SMike Galbraith struct seq_file *m = filp->private_data; 13285091faa4SMike Galbraith 13295091faa4SMike Galbraith m->private = inode; 13305091faa4SMike Galbraith } 13315091faa4SMike Galbraith return ret; 13325091faa4SMike Galbraith } 13335091faa4SMike Galbraith 13345091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = { 13355091faa4SMike Galbraith .open = sched_autogroup_open, 13365091faa4SMike Galbraith .read = seq_read, 13375091faa4SMike Galbraith .write = sched_autogroup_write, 13385091faa4SMike Galbraith .llseek = seq_lseek, 13395091faa4SMike Galbraith .release = single_release, 13405091faa4SMike Galbraith }; 13415091faa4SMike Galbraith 13425091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */ 13435091faa4SMike Galbraith 13444614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf, 13454614a696Sjohn stultz size_t count, loff_t *offset) 13464614a696Sjohn stultz { 1347496ad9aaSAl Viro struct inode *inode = file_inode(file); 13484614a696Sjohn stultz struct task_struct *p; 13494614a696Sjohn stultz char buffer[TASK_COMM_LEN]; 13504614a696Sjohn stultz 13514614a696Sjohn stultz memset(buffer, 0, sizeof(buffer)); 13524614a696Sjohn stultz if (count > sizeof(buffer) - 1) 13534614a696Sjohn stultz count = sizeof(buffer) - 1; 13544614a696Sjohn stultz if (copy_from_user(buffer, buf, count)) 13554614a696Sjohn stultz return -EFAULT; 13564614a696Sjohn stultz 13574614a696Sjohn stultz p = get_proc_task(inode); 13584614a696Sjohn stultz if (!p) 13594614a696Sjohn stultz return -ESRCH; 13604614a696Sjohn stultz 13614614a696Sjohn stultz if (same_thread_group(current, p)) 13624614a696Sjohn stultz set_task_comm(p, buffer); 13634614a696Sjohn stultz else 13644614a696Sjohn stultz count = -EINVAL; 13654614a696Sjohn stultz 13664614a696Sjohn stultz put_task_struct(p); 13674614a696Sjohn stultz 13684614a696Sjohn stultz return count; 13694614a696Sjohn stultz } 13704614a696Sjohn stultz 13714614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v) 13724614a696Sjohn stultz { 13734614a696Sjohn stultz struct inode *inode = m->private; 13744614a696Sjohn stultz struct task_struct *p; 13754614a696Sjohn stultz 13764614a696Sjohn stultz p = get_proc_task(inode); 13774614a696Sjohn stultz if (!p) 13784614a696Sjohn stultz return -ESRCH; 13794614a696Sjohn stultz 13804614a696Sjohn stultz task_lock(p); 13814614a696Sjohn stultz seq_printf(m, "%s\n", p->comm); 13824614a696Sjohn stultz task_unlock(p); 13834614a696Sjohn stultz 13844614a696Sjohn stultz put_task_struct(p); 13854614a696Sjohn stultz 13864614a696Sjohn stultz return 0; 13874614a696Sjohn stultz } 13884614a696Sjohn stultz 13894614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp) 13904614a696Sjohn stultz { 1391c6a34058SJovi Zhang return single_open(filp, comm_show, inode); 13924614a696Sjohn stultz } 13934614a696Sjohn stultz 13944614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = { 13954614a696Sjohn stultz .open = comm_open, 13964614a696Sjohn stultz .read = seq_read, 13974614a696Sjohn stultz .write = comm_write, 13984614a696Sjohn stultz .llseek = seq_lseek, 13994614a696Sjohn stultz .release = single_release, 14004614a696Sjohn stultz }; 14014614a696Sjohn stultz 14027773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path) 1403925d1c40SMatt Helsley { 1404925d1c40SMatt Helsley struct task_struct *task; 1405925d1c40SMatt Helsley struct mm_struct *mm; 1406925d1c40SMatt Helsley struct file *exe_file; 1407925d1c40SMatt Helsley 14087773fbc5SCyrill Gorcunov task = get_proc_task(dentry->d_inode); 1409925d1c40SMatt Helsley if (!task) 1410925d1c40SMatt Helsley return -ENOENT; 1411925d1c40SMatt Helsley mm = get_task_mm(task); 1412925d1c40SMatt Helsley put_task_struct(task); 1413925d1c40SMatt Helsley if (!mm) 1414925d1c40SMatt Helsley return -ENOENT; 1415925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1416925d1c40SMatt Helsley mmput(mm); 1417925d1c40SMatt Helsley if (exe_file) { 1418925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1419925d1c40SMatt Helsley path_get(&exe_file->f_path); 1420925d1c40SMatt Helsley fput(exe_file); 1421925d1c40SMatt Helsley return 0; 1422925d1c40SMatt Helsley } else 1423925d1c40SMatt Helsley return -ENOENT; 1424925d1c40SMatt Helsley } 1425925d1c40SMatt Helsley 1426008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 14271da177e4SLinus Torvalds { 14281da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 1429408ef013SChristoph Hellwig struct path path; 14301da177e4SLinus Torvalds int error = -EACCES; 14311da177e4SLinus Torvalds 1432778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1433778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 14341da177e4SLinus Torvalds goto out; 14351da177e4SLinus Torvalds 1436408ef013SChristoph Hellwig error = PROC_I(inode)->op.proc_get_link(dentry, &path); 1437408ef013SChristoph Hellwig if (error) 1438408ef013SChristoph Hellwig goto out; 1439408ef013SChristoph Hellwig 1440b5fb63c1SChristoph Hellwig nd_jump_link(nd, &path); 1441408ef013SChristoph Hellwig return NULL; 14421da177e4SLinus Torvalds out: 1443008b150aSAl Viro return ERR_PTR(error); 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds 14463dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 14471da177e4SLinus Torvalds { 1448e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 14493dcd25f3SJan Blunck char *pathname; 14501da177e4SLinus Torvalds int len; 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds if (!tmp) 14531da177e4SLinus Torvalds return -ENOMEM; 14541da177e4SLinus Torvalds 14557b2a69baSEric W. Biederman pathname = d_path(path, tmp, PAGE_SIZE); 14563dcd25f3SJan Blunck len = PTR_ERR(pathname); 14573dcd25f3SJan Blunck if (IS_ERR(pathname)) 14581da177e4SLinus Torvalds goto out; 14593dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds if (len > buflen) 14621da177e4SLinus Torvalds len = buflen; 14633dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 14641da177e4SLinus Torvalds len = -EFAULT; 14651da177e4SLinus Torvalds out: 14661da177e4SLinus Torvalds free_page((unsigned long)tmp); 14671da177e4SLinus Torvalds return len; 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 14711da177e4SLinus Torvalds { 14721da177e4SLinus Torvalds int error = -EACCES; 14731da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14743dcd25f3SJan Blunck struct path path; 14751da177e4SLinus Torvalds 1476778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1477778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 14781da177e4SLinus Torvalds goto out; 14791da177e4SLinus Torvalds 14807773fbc5SCyrill Gorcunov error = PROC_I(inode)->op.proc_get_link(dentry, &path); 14811da177e4SLinus Torvalds if (error) 14821da177e4SLinus Torvalds goto out; 14831da177e4SLinus Torvalds 14843dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 14853dcd25f3SJan Blunck path_put(&path); 14861da177e4SLinus Torvalds out: 14871da177e4SLinus Torvalds return error; 14881da177e4SLinus Torvalds } 14891da177e4SLinus Torvalds 1490faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = { 14911da177e4SLinus Torvalds .readlink = proc_pid_readlink, 14926d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 14936d76fa58SLinus Torvalds .setattr = proc_setattr, 14941da177e4SLinus Torvalds }; 14951da177e4SLinus Torvalds 149628a6d671SEric W. Biederman 149728a6d671SEric W. Biederman /* building an inode */ 149828a6d671SEric W. Biederman 14996b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 150028a6d671SEric W. Biederman { 150128a6d671SEric W. Biederman struct inode * inode; 150228a6d671SEric W. Biederman struct proc_inode *ei; 1503c69e8d9cSDavid Howells const struct cred *cred; 150428a6d671SEric W. Biederman 150528a6d671SEric W. Biederman /* We need a new inode */ 150628a6d671SEric W. Biederman 150728a6d671SEric W. Biederman inode = new_inode(sb); 150828a6d671SEric W. Biederman if (!inode) 150928a6d671SEric W. Biederman goto out; 151028a6d671SEric W. Biederman 151128a6d671SEric W. Biederman /* Common stuff */ 151228a6d671SEric W. Biederman ei = PROC_I(inode); 151385fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 151428a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 151528a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 151628a6d671SEric W. Biederman 151728a6d671SEric W. Biederman /* 151828a6d671SEric W. Biederman * grab the reference to task. 151928a6d671SEric W. Biederman */ 15201a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 152128a6d671SEric W. Biederman if (!ei->pid) 152228a6d671SEric W. Biederman goto out_unlock; 152328a6d671SEric W. Biederman 152428a6d671SEric W. Biederman if (task_dumpable(task)) { 1525c69e8d9cSDavid Howells rcu_read_lock(); 1526c69e8d9cSDavid Howells cred = __task_cred(task); 1527c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1528c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1529c69e8d9cSDavid Howells rcu_read_unlock(); 153028a6d671SEric W. Biederman } 153128a6d671SEric W. Biederman security_task_to_inode(task, inode); 153228a6d671SEric W. Biederman 153328a6d671SEric W. Biederman out: 153428a6d671SEric W. Biederman return inode; 153528a6d671SEric W. Biederman 153628a6d671SEric W. Biederman out_unlock: 153728a6d671SEric W. Biederman iput(inode); 153828a6d671SEric W. Biederman return NULL; 153928a6d671SEric W. Biederman } 154028a6d671SEric W. Biederman 15416b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 154228a6d671SEric W. Biederman { 154328a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 154428a6d671SEric W. Biederman struct task_struct *task; 1545c69e8d9cSDavid Howells const struct cred *cred; 15460499680aSVasiliy Kulikov struct pid_namespace *pid = dentry->d_sb->s_fs_info; 1547c69e8d9cSDavid Howells 154828a6d671SEric W. Biederman generic_fillattr(inode, stat); 154928a6d671SEric W. Biederman 155028a6d671SEric W. Biederman rcu_read_lock(); 1551dcb0f222SEric W. Biederman stat->uid = GLOBAL_ROOT_UID; 1552dcb0f222SEric W. Biederman stat->gid = GLOBAL_ROOT_GID; 155328a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 155428a6d671SEric W. Biederman if (task) { 15550499680aSVasiliy Kulikov if (!has_pid_permissions(pid, task, 2)) { 15560499680aSVasiliy Kulikov rcu_read_unlock(); 15570499680aSVasiliy Kulikov /* 15580499680aSVasiliy Kulikov * This doesn't prevent learning whether PID exists, 15590499680aSVasiliy Kulikov * it only makes getattr() consistent with readdir(). 15600499680aSVasiliy Kulikov */ 15610499680aSVasiliy Kulikov return -ENOENT; 15620499680aSVasiliy Kulikov } 156328a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 156428a6d671SEric W. Biederman task_dumpable(task)) { 1565c69e8d9cSDavid Howells cred = __task_cred(task); 1566c69e8d9cSDavid Howells stat->uid = cred->euid; 1567c69e8d9cSDavid Howells stat->gid = cred->egid; 156828a6d671SEric W. Biederman } 156928a6d671SEric W. Biederman } 157028a6d671SEric W. Biederman rcu_read_unlock(); 157128a6d671SEric W. Biederman return 0; 157228a6d671SEric W. Biederman } 157328a6d671SEric W. Biederman 157428a6d671SEric W. Biederman /* dentry stuff */ 157528a6d671SEric W. Biederman 157628a6d671SEric W. Biederman /* 157728a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 157828a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 157928a6d671SEric W. Biederman * due to the way we treat inodes. 158028a6d671SEric W. Biederman * 158128a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 158228a6d671SEric W. Biederman * performed a setuid(), etc. 158328a6d671SEric W. Biederman * 158428a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 158528a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 158628a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 158728a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 158828a6d671SEric W. Biederman * made this apply to all per process world readable and executable 158928a6d671SEric W. Biederman * directories. 159028a6d671SEric W. Biederman */ 15910b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags) 159228a6d671SEric W. Biederman { 159334286d66SNick Piggin struct inode *inode; 159434286d66SNick Piggin struct task_struct *task; 1595c69e8d9cSDavid Howells const struct cred *cred; 1596c69e8d9cSDavid Howells 15970b728e19SAl Viro if (flags & LOOKUP_RCU) 159834286d66SNick Piggin return -ECHILD; 159934286d66SNick Piggin 160034286d66SNick Piggin inode = dentry->d_inode; 160134286d66SNick Piggin task = get_proc_task(inode); 160234286d66SNick Piggin 160328a6d671SEric W. Biederman if (task) { 160428a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 160528a6d671SEric W. Biederman task_dumpable(task)) { 1606c69e8d9cSDavid Howells rcu_read_lock(); 1607c69e8d9cSDavid Howells cred = __task_cred(task); 1608c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1609c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1610c69e8d9cSDavid Howells rcu_read_unlock(); 161128a6d671SEric W. Biederman } else { 1612dcb0f222SEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1613dcb0f222SEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 161428a6d671SEric W. Biederman } 161528a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 161628a6d671SEric W. Biederman security_task_to_inode(task, inode); 161728a6d671SEric W. Biederman put_task_struct(task); 161828a6d671SEric W. Biederman return 1; 161928a6d671SEric W. Biederman } 162028a6d671SEric W. Biederman d_drop(dentry); 162128a6d671SEric W. Biederman return 0; 162228a6d671SEric W. Biederman } 162328a6d671SEric W. Biederman 16246b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations = 162528a6d671SEric W. Biederman { 162628a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 162728a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 162828a6d671SEric W. Biederman }; 162928a6d671SEric W. Biederman 163028a6d671SEric W. Biederman /* Lookups */ 163128a6d671SEric W. Biederman 16321c0d04c9SEric W. Biederman /* 16331c0d04c9SEric W. Biederman * Fill a directory entry. 16341c0d04c9SEric W. Biederman * 16351c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 16361c0d04c9SEric W. Biederman * file type from dcache entry. 16371c0d04c9SEric W. Biederman * 16381c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 16391c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 16401c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 16411c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 16421c0d04c9SEric W. Biederman * by stat. 16431c0d04c9SEric W. Biederman */ 16446b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 16456b4e306aSEric W. Biederman const char *name, int len, 1646c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 164761a28784SEric W. Biederman { 16482fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 164961a28784SEric W. Biederman struct inode *inode; 165061a28784SEric W. Biederman struct qstr qname; 165161a28784SEric W. Biederman ino_t ino = 0; 165261a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 165361a28784SEric W. Biederman 165461a28784SEric W. Biederman qname.name = name; 165561a28784SEric W. Biederman qname.len = len; 165661a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 165761a28784SEric W. Biederman 165861a28784SEric W. Biederman child = d_lookup(dir, &qname); 165961a28784SEric W. Biederman if (!child) { 166061a28784SEric W. Biederman struct dentry *new; 166161a28784SEric W. Biederman new = d_alloc(dir, &qname); 166261a28784SEric W. Biederman if (new) { 166361a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 166461a28784SEric W. Biederman if (child) 166561a28784SEric W. Biederman dput(new); 166661a28784SEric W. Biederman else 166761a28784SEric W. Biederman child = new; 166861a28784SEric W. Biederman } 166961a28784SEric W. Biederman } 167061a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 167161a28784SEric W. Biederman goto end_instantiate; 167261a28784SEric W. Biederman inode = child->d_inode; 167361a28784SEric W. Biederman if (inode) { 167461a28784SEric W. Biederman ino = inode->i_ino; 167561a28784SEric W. Biederman type = inode->i_mode >> 12; 167661a28784SEric W. Biederman } 167761a28784SEric W. Biederman dput(child); 167861a28784SEric W. Biederman end_instantiate: 167961a28784SEric W. Biederman if (!ino) 168061a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 168161a28784SEric W. Biederman if (!ino) 168261a28784SEric W. Biederman ino = 1; 168361a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 168461a28784SEric W. Biederman } 168561a28784SEric W. Biederman 1686640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 1687640708a2SPavel Emelyanov 1688640708a2SPavel Emelyanov /* 1689640708a2SPavel Emelyanov * dname_to_vma_addr - maps a dentry name into two unsigned longs 1690640708a2SPavel Emelyanov * which represent vma start and end addresses. 1691640708a2SPavel Emelyanov */ 1692640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry, 1693640708a2SPavel Emelyanov unsigned long *start, unsigned long *end) 1694640708a2SPavel Emelyanov { 1695640708a2SPavel Emelyanov if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2) 1696640708a2SPavel Emelyanov return -EINVAL; 1697640708a2SPavel Emelyanov 1698640708a2SPavel Emelyanov return 0; 1699640708a2SPavel Emelyanov } 1700640708a2SPavel Emelyanov 17010b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags) 1702640708a2SPavel Emelyanov { 1703640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1704640708a2SPavel Emelyanov bool exact_vma_exists = false; 1705640708a2SPavel Emelyanov struct mm_struct *mm = NULL; 1706640708a2SPavel Emelyanov struct task_struct *task; 1707640708a2SPavel Emelyanov const struct cred *cred; 1708640708a2SPavel Emelyanov struct inode *inode; 1709640708a2SPavel Emelyanov int status = 0; 1710640708a2SPavel Emelyanov 17110b728e19SAl Viro if (flags & LOOKUP_RCU) 1712640708a2SPavel Emelyanov return -ECHILD; 1713640708a2SPavel Emelyanov 1714640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) { 171541735818SZhao Hongjiang status = -EPERM; 1716640708a2SPavel Emelyanov goto out_notask; 1717640708a2SPavel Emelyanov } 1718640708a2SPavel Emelyanov 1719640708a2SPavel Emelyanov inode = dentry->d_inode; 1720640708a2SPavel Emelyanov task = get_proc_task(inode); 1721640708a2SPavel Emelyanov if (!task) 1722640708a2SPavel Emelyanov goto out_notask; 1723640708a2SPavel Emelyanov 17242344bec7SCong Wang mm = mm_access(task, PTRACE_MODE_READ); 17252344bec7SCong Wang if (IS_ERR_OR_NULL(mm)) 1726640708a2SPavel Emelyanov goto out; 1727640708a2SPavel Emelyanov 1728640708a2SPavel Emelyanov if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) { 1729640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1730640708a2SPavel Emelyanov exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end); 1731640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1732640708a2SPavel Emelyanov } 1733640708a2SPavel Emelyanov 1734640708a2SPavel Emelyanov mmput(mm); 1735640708a2SPavel Emelyanov 1736640708a2SPavel Emelyanov if (exact_vma_exists) { 1737640708a2SPavel Emelyanov if (task_dumpable(task)) { 1738640708a2SPavel Emelyanov rcu_read_lock(); 1739640708a2SPavel Emelyanov cred = __task_cred(task); 1740640708a2SPavel Emelyanov inode->i_uid = cred->euid; 1741640708a2SPavel Emelyanov inode->i_gid = cred->egid; 1742640708a2SPavel Emelyanov rcu_read_unlock(); 1743640708a2SPavel Emelyanov } else { 1744dcb0f222SEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1745dcb0f222SEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 1746640708a2SPavel Emelyanov } 1747640708a2SPavel Emelyanov security_task_to_inode(task, inode); 1748640708a2SPavel Emelyanov status = 1; 1749640708a2SPavel Emelyanov } 1750640708a2SPavel Emelyanov 1751640708a2SPavel Emelyanov out: 1752640708a2SPavel Emelyanov put_task_struct(task); 1753640708a2SPavel Emelyanov 1754640708a2SPavel Emelyanov out_notask: 1755640708a2SPavel Emelyanov if (status <= 0) 1756640708a2SPavel Emelyanov d_drop(dentry); 1757640708a2SPavel Emelyanov 1758640708a2SPavel Emelyanov return status; 1759640708a2SPavel Emelyanov } 1760640708a2SPavel Emelyanov 1761640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = { 1762640708a2SPavel Emelyanov .d_revalidate = map_files_d_revalidate, 1763640708a2SPavel Emelyanov .d_delete = pid_delete_dentry, 1764640708a2SPavel Emelyanov }; 1765640708a2SPavel Emelyanov 1766640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path) 1767640708a2SPavel Emelyanov { 1768640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1769640708a2SPavel Emelyanov struct vm_area_struct *vma; 1770640708a2SPavel Emelyanov struct task_struct *task; 1771640708a2SPavel Emelyanov struct mm_struct *mm; 1772640708a2SPavel Emelyanov int rc; 1773640708a2SPavel Emelyanov 1774640708a2SPavel Emelyanov rc = -ENOENT; 1775640708a2SPavel Emelyanov task = get_proc_task(dentry->d_inode); 1776640708a2SPavel Emelyanov if (!task) 1777640708a2SPavel Emelyanov goto out; 1778640708a2SPavel Emelyanov 1779640708a2SPavel Emelyanov mm = get_task_mm(task); 1780640708a2SPavel Emelyanov put_task_struct(task); 1781640708a2SPavel Emelyanov if (!mm) 1782640708a2SPavel Emelyanov goto out; 1783640708a2SPavel Emelyanov 1784640708a2SPavel Emelyanov rc = dname_to_vma_addr(dentry, &vm_start, &vm_end); 1785640708a2SPavel Emelyanov if (rc) 1786640708a2SPavel Emelyanov goto out_mmput; 1787640708a2SPavel Emelyanov 1788640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1789640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end); 1790640708a2SPavel Emelyanov if (vma && vma->vm_file) { 1791640708a2SPavel Emelyanov *path = vma->vm_file->f_path; 1792640708a2SPavel Emelyanov path_get(path); 1793640708a2SPavel Emelyanov rc = 0; 1794640708a2SPavel Emelyanov } 1795640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1796640708a2SPavel Emelyanov 1797640708a2SPavel Emelyanov out_mmput: 1798640708a2SPavel Emelyanov mmput(mm); 1799640708a2SPavel Emelyanov out: 1800640708a2SPavel Emelyanov return rc; 1801640708a2SPavel Emelyanov } 1802640708a2SPavel Emelyanov 1803640708a2SPavel Emelyanov struct map_files_info { 18047b540d06SAl Viro fmode_t mode; 1805640708a2SPavel Emelyanov unsigned long len; 1806640708a2SPavel Emelyanov unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */ 1807640708a2SPavel Emelyanov }; 1808640708a2SPavel Emelyanov 1809640708a2SPavel Emelyanov static struct dentry * 1810640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, 1811640708a2SPavel Emelyanov struct task_struct *task, const void *ptr) 1812640708a2SPavel Emelyanov { 18137b540d06SAl Viro fmode_t mode = (fmode_t)(unsigned long)ptr; 1814640708a2SPavel Emelyanov struct proc_inode *ei; 1815640708a2SPavel Emelyanov struct inode *inode; 1816640708a2SPavel Emelyanov 1817640708a2SPavel Emelyanov inode = proc_pid_make_inode(dir->i_sb, task); 1818640708a2SPavel Emelyanov if (!inode) 1819640708a2SPavel Emelyanov return ERR_PTR(-ENOENT); 1820640708a2SPavel Emelyanov 1821640708a2SPavel Emelyanov ei = PROC_I(inode); 1822640708a2SPavel Emelyanov ei->op.proc_get_link = proc_map_files_get_link; 1823640708a2SPavel Emelyanov 1824640708a2SPavel Emelyanov inode->i_op = &proc_pid_link_inode_operations; 1825640708a2SPavel Emelyanov inode->i_size = 64; 1826640708a2SPavel Emelyanov inode->i_mode = S_IFLNK; 1827640708a2SPavel Emelyanov 18287b540d06SAl Viro if (mode & FMODE_READ) 1829640708a2SPavel Emelyanov inode->i_mode |= S_IRUSR; 18307b540d06SAl Viro if (mode & FMODE_WRITE) 1831640708a2SPavel Emelyanov inode->i_mode |= S_IWUSR; 1832640708a2SPavel Emelyanov 1833640708a2SPavel Emelyanov d_set_d_op(dentry, &tid_map_files_dentry_operations); 1834640708a2SPavel Emelyanov d_add(dentry, inode); 1835640708a2SPavel Emelyanov 1836640708a2SPavel Emelyanov return NULL; 1837640708a2SPavel Emelyanov } 1838640708a2SPavel Emelyanov 1839640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir, 184000cd8dd3SAl Viro struct dentry *dentry, unsigned int flags) 1841640708a2SPavel Emelyanov { 1842640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1843640708a2SPavel Emelyanov struct vm_area_struct *vma; 1844640708a2SPavel Emelyanov struct task_struct *task; 1845640708a2SPavel Emelyanov struct dentry *result; 1846640708a2SPavel Emelyanov struct mm_struct *mm; 1847640708a2SPavel Emelyanov 184841735818SZhao Hongjiang result = ERR_PTR(-EPERM); 1849640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) 1850640708a2SPavel Emelyanov goto out; 1851640708a2SPavel Emelyanov 1852640708a2SPavel Emelyanov result = ERR_PTR(-ENOENT); 1853640708a2SPavel Emelyanov task = get_proc_task(dir); 1854640708a2SPavel Emelyanov if (!task) 1855640708a2SPavel Emelyanov goto out; 1856640708a2SPavel Emelyanov 1857640708a2SPavel Emelyanov result = ERR_PTR(-EACCES); 1858eb94cd96SCyrill Gorcunov if (!ptrace_may_access(task, PTRACE_MODE_READ)) 1859640708a2SPavel Emelyanov goto out_put_task; 1860640708a2SPavel Emelyanov 1861640708a2SPavel Emelyanov result = ERR_PTR(-ENOENT); 1862640708a2SPavel Emelyanov if (dname_to_vma_addr(dentry, &vm_start, &vm_end)) 1863eb94cd96SCyrill Gorcunov goto out_put_task; 1864640708a2SPavel Emelyanov 1865640708a2SPavel Emelyanov mm = get_task_mm(task); 1866640708a2SPavel Emelyanov if (!mm) 1867eb94cd96SCyrill Gorcunov goto out_put_task; 1868640708a2SPavel Emelyanov 1869640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1870640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end); 1871640708a2SPavel Emelyanov if (!vma) 1872640708a2SPavel Emelyanov goto out_no_vma; 1873640708a2SPavel Emelyanov 187405f56484SStanislav Kinsbursky if (vma->vm_file) 18757b540d06SAl Viro result = proc_map_files_instantiate(dir, dentry, task, 18767b540d06SAl Viro (void *)(unsigned long)vma->vm_file->f_mode); 1877640708a2SPavel Emelyanov 1878640708a2SPavel Emelyanov out_no_vma: 1879640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1880640708a2SPavel Emelyanov mmput(mm); 1881640708a2SPavel Emelyanov out_put_task: 1882640708a2SPavel Emelyanov put_task_struct(task); 1883640708a2SPavel Emelyanov out: 1884640708a2SPavel Emelyanov return result; 1885640708a2SPavel Emelyanov } 1886640708a2SPavel Emelyanov 1887640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = { 1888640708a2SPavel Emelyanov .lookup = proc_map_files_lookup, 1889640708a2SPavel Emelyanov .permission = proc_fd_permission, 1890640708a2SPavel Emelyanov .setattr = proc_setattr, 1891640708a2SPavel Emelyanov }; 1892640708a2SPavel Emelyanov 1893640708a2SPavel Emelyanov static int 1894640708a2SPavel Emelyanov proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir) 1895640708a2SPavel Emelyanov { 1896640708a2SPavel Emelyanov struct dentry *dentry = filp->f_path.dentry; 1897640708a2SPavel Emelyanov struct inode *inode = dentry->d_inode; 1898640708a2SPavel Emelyanov struct vm_area_struct *vma; 1899640708a2SPavel Emelyanov struct task_struct *task; 1900640708a2SPavel Emelyanov struct mm_struct *mm; 1901640708a2SPavel Emelyanov ino_t ino; 1902640708a2SPavel Emelyanov int ret; 1903640708a2SPavel Emelyanov 190441735818SZhao Hongjiang ret = -EPERM; 1905640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) 1906640708a2SPavel Emelyanov goto out; 1907640708a2SPavel Emelyanov 1908640708a2SPavel Emelyanov ret = -ENOENT; 1909640708a2SPavel Emelyanov task = get_proc_task(inode); 1910640708a2SPavel Emelyanov if (!task) 1911640708a2SPavel Emelyanov goto out; 1912640708a2SPavel Emelyanov 1913640708a2SPavel Emelyanov ret = -EACCES; 1914eb94cd96SCyrill Gorcunov if (!ptrace_may_access(task, PTRACE_MODE_READ)) 1915640708a2SPavel Emelyanov goto out_put_task; 1916640708a2SPavel Emelyanov 1917640708a2SPavel Emelyanov ret = 0; 1918640708a2SPavel Emelyanov switch (filp->f_pos) { 1919640708a2SPavel Emelyanov case 0: 1920640708a2SPavel Emelyanov ino = inode->i_ino; 1921640708a2SPavel Emelyanov if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0) 1922eb94cd96SCyrill Gorcunov goto out_put_task; 1923640708a2SPavel Emelyanov filp->f_pos++; 1924640708a2SPavel Emelyanov case 1: 1925640708a2SPavel Emelyanov ino = parent_ino(dentry); 1926640708a2SPavel Emelyanov if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 1927eb94cd96SCyrill Gorcunov goto out_put_task; 1928640708a2SPavel Emelyanov filp->f_pos++; 1929640708a2SPavel Emelyanov default: 1930640708a2SPavel Emelyanov { 1931640708a2SPavel Emelyanov unsigned long nr_files, pos, i; 1932640708a2SPavel Emelyanov struct flex_array *fa = NULL; 1933640708a2SPavel Emelyanov struct map_files_info info; 1934640708a2SPavel Emelyanov struct map_files_info *p; 1935640708a2SPavel Emelyanov 1936640708a2SPavel Emelyanov mm = get_task_mm(task); 1937640708a2SPavel Emelyanov if (!mm) 1938eb94cd96SCyrill Gorcunov goto out_put_task; 1939640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1940640708a2SPavel Emelyanov 1941640708a2SPavel Emelyanov nr_files = 0; 1942640708a2SPavel Emelyanov 1943640708a2SPavel Emelyanov /* 1944640708a2SPavel Emelyanov * We need two passes here: 1945640708a2SPavel Emelyanov * 1946640708a2SPavel Emelyanov * 1) Collect vmas of mapped files with mmap_sem taken 1947640708a2SPavel Emelyanov * 2) Release mmap_sem and instantiate entries 1948640708a2SPavel Emelyanov * 1949640708a2SPavel Emelyanov * otherwise we get lockdep complained, since filldir() 1950640708a2SPavel Emelyanov * routine might require mmap_sem taken in might_fault(). 1951640708a2SPavel Emelyanov */ 1952640708a2SPavel Emelyanov 1953640708a2SPavel Emelyanov for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) { 1954640708a2SPavel Emelyanov if (vma->vm_file && ++pos > filp->f_pos) 1955640708a2SPavel Emelyanov nr_files++; 1956640708a2SPavel Emelyanov } 1957640708a2SPavel Emelyanov 1958640708a2SPavel Emelyanov if (nr_files) { 1959640708a2SPavel Emelyanov fa = flex_array_alloc(sizeof(info), nr_files, 1960640708a2SPavel Emelyanov GFP_KERNEL); 1961640708a2SPavel Emelyanov if (!fa || flex_array_prealloc(fa, 0, nr_files, 1962640708a2SPavel Emelyanov GFP_KERNEL)) { 1963640708a2SPavel Emelyanov ret = -ENOMEM; 1964640708a2SPavel Emelyanov if (fa) 1965640708a2SPavel Emelyanov flex_array_free(fa); 1966640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1967640708a2SPavel Emelyanov mmput(mm); 1968eb94cd96SCyrill Gorcunov goto out_put_task; 1969640708a2SPavel Emelyanov } 1970640708a2SPavel Emelyanov for (i = 0, vma = mm->mmap, pos = 2; vma; 1971640708a2SPavel Emelyanov vma = vma->vm_next) { 1972640708a2SPavel Emelyanov if (!vma->vm_file) 1973640708a2SPavel Emelyanov continue; 1974640708a2SPavel Emelyanov if (++pos <= filp->f_pos) 1975640708a2SPavel Emelyanov continue; 1976640708a2SPavel Emelyanov 19777b540d06SAl Viro info.mode = vma->vm_file->f_mode; 1978640708a2SPavel Emelyanov info.len = snprintf(info.name, 1979640708a2SPavel Emelyanov sizeof(info.name), "%lx-%lx", 1980640708a2SPavel Emelyanov vma->vm_start, vma->vm_end); 1981640708a2SPavel Emelyanov if (flex_array_put(fa, i++, &info, GFP_KERNEL)) 1982640708a2SPavel Emelyanov BUG(); 1983640708a2SPavel Emelyanov } 1984640708a2SPavel Emelyanov } 1985640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1986640708a2SPavel Emelyanov 1987640708a2SPavel Emelyanov for (i = 0; i < nr_files; i++) { 1988640708a2SPavel Emelyanov p = flex_array_get(fa, i); 1989640708a2SPavel Emelyanov ret = proc_fill_cache(filp, dirent, filldir, 1990640708a2SPavel Emelyanov p->name, p->len, 1991640708a2SPavel Emelyanov proc_map_files_instantiate, 19927b540d06SAl Viro task, 19937b540d06SAl Viro (void *)(unsigned long)p->mode); 1994640708a2SPavel Emelyanov if (ret) 1995640708a2SPavel Emelyanov break; 1996640708a2SPavel Emelyanov filp->f_pos++; 1997640708a2SPavel Emelyanov } 1998640708a2SPavel Emelyanov if (fa) 1999640708a2SPavel Emelyanov flex_array_free(fa); 2000640708a2SPavel Emelyanov mmput(mm); 2001640708a2SPavel Emelyanov } 2002640708a2SPavel Emelyanov } 2003640708a2SPavel Emelyanov 2004640708a2SPavel Emelyanov out_put_task: 2005640708a2SPavel Emelyanov put_task_struct(task); 2006640708a2SPavel Emelyanov out: 2007640708a2SPavel Emelyanov return ret; 2008640708a2SPavel Emelyanov } 2009640708a2SPavel Emelyanov 2010640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = { 2011640708a2SPavel Emelyanov .read = generic_read_dir, 2012640708a2SPavel Emelyanov .readdir = proc_map_files_readdir, 2013640708a2SPavel Emelyanov .llseek = default_llseek, 2014640708a2SPavel Emelyanov }; 2015640708a2SPavel Emelyanov 2016640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */ 2017640708a2SPavel Emelyanov 2018444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 2019c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2020444ceed8SEric W. Biederman { 2021c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2022444ceed8SEric W. Biederman struct inode *inode; 2023444ceed8SEric W. Biederman struct proc_inode *ei; 2024bd6daba9SKOSAKI Motohiro struct dentry *error = ERR_PTR(-ENOENT); 2025444ceed8SEric W. Biederman 202661a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2027444ceed8SEric W. Biederman if (!inode) 2028444ceed8SEric W. Biederman goto out; 2029444ceed8SEric W. Biederman 2030444ceed8SEric W. Biederman ei = PROC_I(inode); 2031444ceed8SEric W. Biederman inode->i_mode = p->mode; 2032444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 2033bfe86848SMiklos Szeredi set_nlink(inode, 2); /* Use getattr to fix if necessary */ 2034444ceed8SEric W. Biederman if (p->iop) 2035444ceed8SEric W. Biederman inode->i_op = p->iop; 2036444ceed8SEric W. Biederman if (p->fop) 2037444ceed8SEric W. Biederman inode->i_fop = p->fop; 2038444ceed8SEric W. Biederman ei->op = p->op; 2039fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2040444ceed8SEric W. Biederman d_add(dentry, inode); 2041444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 20420b728e19SAl Viro if (pid_revalidate(dentry, 0)) 2043444ceed8SEric W. Biederman error = NULL; 2044444ceed8SEric W. Biederman out: 2045444ceed8SEric W. Biederman return error; 2046444ceed8SEric W. Biederman } 2047444ceed8SEric W. Biederman 20481da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 20491da177e4SLinus Torvalds struct dentry *dentry, 2050c5141e6dSEric Dumazet const struct pid_entry *ents, 20517bcd6b0eSEric W. Biederman unsigned int nents) 20521da177e4SLinus Torvalds { 2053cd6a3ce9SEric W. Biederman struct dentry *error; 205499f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2055c5141e6dSEric Dumazet const struct pid_entry *p, *last; 20561da177e4SLinus Torvalds 2057cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 20581da177e4SLinus Torvalds 205999f89551SEric W. Biederman if (!task) 206099f89551SEric W. Biederman goto out_no_task; 20611da177e4SLinus Torvalds 206220cdc894SEric W. Biederman /* 206320cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 206420cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 206520cdc894SEric W. Biederman */ 20667bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20677bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 20681da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 20691da177e4SLinus Torvalds continue; 20701da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 20711da177e4SLinus Torvalds break; 20721da177e4SLinus Torvalds } 20737bcd6b0eSEric W. Biederman if (p > last) 20741da177e4SLinus Torvalds goto out; 20751da177e4SLinus Torvalds 2076444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 20771da177e4SLinus Torvalds out: 207899f89551SEric W. Biederman put_task_struct(task); 207999f89551SEric W. Biederman out_no_task: 2080cd6a3ce9SEric W. Biederman return error; 20811da177e4SLinus Torvalds } 20821da177e4SLinus Torvalds 2083c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 2084c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 208561a28784SEric W. Biederman { 208661a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 208761a28784SEric W. Biederman proc_pident_instantiate, task, p); 208861a28784SEric W. Biederman } 208961a28784SEric W. Biederman 209028a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 209128a6d671SEric W. Biederman void *dirent, filldir_t filldir, 2092c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 209328a6d671SEric W. Biederman { 209428a6d671SEric W. Biederman int i; 20952fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 209628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 209728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 2098c5141e6dSEric Dumazet const struct pid_entry *p, *last; 209928a6d671SEric W. Biederman ino_t ino; 210028a6d671SEric W. Biederman int ret; 210128a6d671SEric W. Biederman 210228a6d671SEric W. Biederman ret = -ENOENT; 210328a6d671SEric W. Biederman if (!task) 210461a28784SEric W. Biederman goto out_no_task; 210528a6d671SEric W. Biederman 210628a6d671SEric W. Biederman ret = 0; 210728a6d671SEric W. Biederman i = filp->f_pos; 210828a6d671SEric W. Biederman switch (i) { 210928a6d671SEric W. Biederman case 0: 211028a6d671SEric W. Biederman ino = inode->i_ino; 211128a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 211228a6d671SEric W. Biederman goto out; 211328a6d671SEric W. Biederman i++; 211428a6d671SEric W. Biederman filp->f_pos++; 211528a6d671SEric W. Biederman /* fall through */ 211628a6d671SEric W. Biederman case 1: 211728a6d671SEric W. Biederman ino = parent_ino(dentry); 211828a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 211928a6d671SEric W. Biederman goto out; 212028a6d671SEric W. Biederman i++; 212128a6d671SEric W. Biederman filp->f_pos++; 212228a6d671SEric W. Biederman /* fall through */ 212328a6d671SEric W. Biederman default: 212428a6d671SEric W. Biederman i -= 2; 212528a6d671SEric W. Biederman if (i >= nents) { 212628a6d671SEric W. Biederman ret = 1; 212728a6d671SEric W. Biederman goto out; 212828a6d671SEric W. Biederman } 212928a6d671SEric W. Biederman p = ents + i; 21307bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 21317bcd6b0eSEric W. Biederman while (p <= last) { 213261a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 213328a6d671SEric W. Biederman goto out; 213428a6d671SEric W. Biederman filp->f_pos++; 213528a6d671SEric W. Biederman p++; 213628a6d671SEric W. Biederman } 21371da177e4SLinus Torvalds } 21381da177e4SLinus Torvalds 213928a6d671SEric W. Biederman ret = 1; 214028a6d671SEric W. Biederman out: 214161a28784SEric W. Biederman put_task_struct(task); 214261a28784SEric W. Biederman out_no_task: 214328a6d671SEric W. Biederman return ret; 21441da177e4SLinus Torvalds } 21451da177e4SLinus Torvalds 21461da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 214728a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 214828a6d671SEric W. Biederman size_t count, loff_t *ppos) 214928a6d671SEric W. Biederman { 2150496ad9aaSAl Viro struct inode * inode = file_inode(file); 215104ff9708SAl Viro char *p = NULL; 215228a6d671SEric W. Biederman ssize_t length; 215328a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 215428a6d671SEric W. Biederman 215528a6d671SEric W. Biederman if (!task) 215604ff9708SAl Viro return -ESRCH; 215728a6d671SEric W. Biederman 215828a6d671SEric W. Biederman length = security_getprocattr(task, 21592fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 216004ff9708SAl Viro &p); 216128a6d671SEric W. Biederman put_task_struct(task); 216204ff9708SAl Viro if (length > 0) 216304ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 216404ff9708SAl Viro kfree(p); 216528a6d671SEric W. Biederman return length; 216628a6d671SEric W. Biederman } 216728a6d671SEric W. Biederman 216828a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 216928a6d671SEric W. Biederman size_t count, loff_t *ppos) 217028a6d671SEric W. Biederman { 2171496ad9aaSAl Viro struct inode * inode = file_inode(file); 217228a6d671SEric W. Biederman char *page; 217328a6d671SEric W. Biederman ssize_t length; 217428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 217528a6d671SEric W. Biederman 217628a6d671SEric W. Biederman length = -ESRCH; 217728a6d671SEric W. Biederman if (!task) 217828a6d671SEric W. Biederman goto out_no_task; 217928a6d671SEric W. Biederman if (count > PAGE_SIZE) 218028a6d671SEric W. Biederman count = PAGE_SIZE; 218128a6d671SEric W. Biederman 218228a6d671SEric W. Biederman /* No partial writes. */ 218328a6d671SEric W. Biederman length = -EINVAL; 218428a6d671SEric W. Biederman if (*ppos != 0) 218528a6d671SEric W. Biederman goto out; 218628a6d671SEric W. Biederman 218728a6d671SEric W. Biederman length = -ENOMEM; 2188e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 218928a6d671SEric W. Biederman if (!page) 219028a6d671SEric W. Biederman goto out; 219128a6d671SEric W. Biederman 219228a6d671SEric W. Biederman length = -EFAULT; 219328a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 219428a6d671SEric W. Biederman goto out_free; 219528a6d671SEric W. Biederman 2196107db7c7SDavid Howells /* Guard against adverse ptrace interaction */ 21979b1bf12dSKOSAKI Motohiro length = mutex_lock_interruptible(&task->signal->cred_guard_mutex); 2198107db7c7SDavid Howells if (length < 0) 2199107db7c7SDavid Howells goto out_free; 2200107db7c7SDavid Howells 220128a6d671SEric W. Biederman length = security_setprocattr(task, 22022fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 220328a6d671SEric W. Biederman (void*)page, count); 22049b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 220528a6d671SEric W. Biederman out_free: 220628a6d671SEric W. Biederman free_page((unsigned long) page); 220728a6d671SEric W. Biederman out: 220828a6d671SEric W. Biederman put_task_struct(task); 220928a6d671SEric W. Biederman out_no_task: 221028a6d671SEric W. Biederman return length; 221128a6d671SEric W. Biederman } 221228a6d671SEric W. Biederman 221300977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 221428a6d671SEric W. Biederman .read = proc_pid_attr_read, 221528a6d671SEric W. Biederman .write = proc_pid_attr_write, 221687df8424SArnd Bergmann .llseek = generic_file_llseek, 221728a6d671SEric W. Biederman }; 221828a6d671SEric W. Biederman 2219c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 2220631f9c18SAlexey Dobriyan REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2221631f9c18SAlexey Dobriyan REG("prev", S_IRUGO, proc_pid_attr_operations), 2222631f9c18SAlexey Dobriyan REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2223631f9c18SAlexey Dobriyan REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2224631f9c18SAlexey Dobriyan REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2225631f9c18SAlexey Dobriyan REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 222628a6d671SEric W. Biederman }; 222728a6d671SEric W. Biederman 222872d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 22291da177e4SLinus Torvalds void * dirent, filldir_t filldir) 22301da177e4SLinus Torvalds { 22311da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 223272d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 22331da177e4SLinus Torvalds } 22341da177e4SLinus Torvalds 223500977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 22361da177e4SLinus Torvalds .read = generic_read_dir, 223772d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 22386038f373SArnd Bergmann .llseek = default_llseek, 22391da177e4SLinus Torvalds }; 22401da177e4SLinus Torvalds 224172d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 224200cd8dd3SAl Viro struct dentry *dentry, unsigned int flags) 22431da177e4SLinus Torvalds { 22447bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 22457bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 2248c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 224972d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 225099f89551SEric W. Biederman .getattr = pid_getattr, 22516d76fa58SLinus Torvalds .setattr = proc_setattr, 22521da177e4SLinus Torvalds }; 22531da177e4SLinus Torvalds 22541da177e4SLinus Torvalds #endif 22551da177e4SLinus Torvalds 2256698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 22573cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 22583cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 22593cb4a0bbSKawai, Hidehiro { 2260496ad9aaSAl Viro struct task_struct *task = get_proc_task(file_inode(file)); 22613cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22623cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 22633cb4a0bbSKawai, Hidehiro size_t len; 22643cb4a0bbSKawai, Hidehiro int ret; 22653cb4a0bbSKawai, Hidehiro 22663cb4a0bbSKawai, Hidehiro if (!task) 22673cb4a0bbSKawai, Hidehiro return -ESRCH; 22683cb4a0bbSKawai, Hidehiro 22693cb4a0bbSKawai, Hidehiro ret = 0; 22703cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22713cb4a0bbSKawai, Hidehiro if (mm) { 22723cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 22733cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 22743cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 22753cb4a0bbSKawai, Hidehiro mmput(mm); 22763cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 22773cb4a0bbSKawai, Hidehiro } 22783cb4a0bbSKawai, Hidehiro 22793cb4a0bbSKawai, Hidehiro put_task_struct(task); 22803cb4a0bbSKawai, Hidehiro 22813cb4a0bbSKawai, Hidehiro return ret; 22823cb4a0bbSKawai, Hidehiro } 22833cb4a0bbSKawai, Hidehiro 22843cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 22853cb4a0bbSKawai, Hidehiro const char __user *buf, 22863cb4a0bbSKawai, Hidehiro size_t count, 22873cb4a0bbSKawai, Hidehiro loff_t *ppos) 22883cb4a0bbSKawai, Hidehiro { 22893cb4a0bbSKawai, Hidehiro struct task_struct *task; 22903cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22913cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 22923cb4a0bbSKawai, Hidehiro unsigned int val; 22933cb4a0bbSKawai, Hidehiro int ret; 22943cb4a0bbSKawai, Hidehiro int i; 22953cb4a0bbSKawai, Hidehiro unsigned long mask; 22963cb4a0bbSKawai, Hidehiro 22973cb4a0bbSKawai, Hidehiro ret = -EFAULT; 22983cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 22993cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 23003cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 23013cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 23023cb4a0bbSKawai, Hidehiro goto out_no_task; 23033cb4a0bbSKawai, Hidehiro 23043cb4a0bbSKawai, Hidehiro ret = -EINVAL; 23053cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 23063cb4a0bbSKawai, Hidehiro if (*end == '\n') 23073cb4a0bbSKawai, Hidehiro end++; 23083cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 23093cb4a0bbSKawai, Hidehiro goto out_no_task; 23103cb4a0bbSKawai, Hidehiro 23113cb4a0bbSKawai, Hidehiro ret = -ESRCH; 2312496ad9aaSAl Viro task = get_proc_task(file_inode(file)); 23133cb4a0bbSKawai, Hidehiro if (!task) 23143cb4a0bbSKawai, Hidehiro goto out_no_task; 23153cb4a0bbSKawai, Hidehiro 23163cb4a0bbSKawai, Hidehiro ret = end - buffer; 23173cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 23183cb4a0bbSKawai, Hidehiro if (!mm) 23193cb4a0bbSKawai, Hidehiro goto out_no_mm; 23203cb4a0bbSKawai, Hidehiro 23213cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 23223cb4a0bbSKawai, Hidehiro if (val & mask) 23233cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 23243cb4a0bbSKawai, Hidehiro else 23253cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 23263cb4a0bbSKawai, Hidehiro } 23273cb4a0bbSKawai, Hidehiro 23283cb4a0bbSKawai, Hidehiro mmput(mm); 23293cb4a0bbSKawai, Hidehiro out_no_mm: 23303cb4a0bbSKawai, Hidehiro put_task_struct(task); 23313cb4a0bbSKawai, Hidehiro out_no_task: 23323cb4a0bbSKawai, Hidehiro return ret; 23333cb4a0bbSKawai, Hidehiro } 23343cb4a0bbSKawai, Hidehiro 23353cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 23363cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 23373cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 233887df8424SArnd Bergmann .llseek = generic_file_llseek, 23393cb4a0bbSKawai, Hidehiro }; 23403cb4a0bbSKawai, Hidehiro #endif 23413cb4a0bbSKawai, Hidehiro 2342aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2343297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2344aba76fdbSAndrew Morton { 2345940389b8SAndrea Righi struct task_io_accounting acct = task->ioac; 2346297c5d92SAndrea Righi unsigned long flags; 2347293eb1e7SVasiliy Kulikov int result; 2348297c5d92SAndrea Righi 2349293eb1e7SVasiliy Kulikov result = mutex_lock_killable(&task->signal->cred_guard_mutex); 2350293eb1e7SVasiliy Kulikov if (result) 2351293eb1e7SVasiliy Kulikov return result; 2352293eb1e7SVasiliy Kulikov 2353293eb1e7SVasiliy Kulikov if (!ptrace_may_access(task, PTRACE_MODE_READ)) { 2354293eb1e7SVasiliy Kulikov result = -EACCES; 2355293eb1e7SVasiliy Kulikov goto out_unlock; 2356293eb1e7SVasiliy Kulikov } 23571d1221f3SVasiliy Kulikov 23585995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2359b2d002dbSAndrea Righi struct task_struct *t = task; 2360297c5d92SAndrea Righi 23615995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 23625995477aSAndrea Righi while_each_thread(task, t) 23635995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2364297c5d92SAndrea Righi 2365297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2366297c5d92SAndrea Righi } 2367293eb1e7SVasiliy Kulikov result = sprintf(buffer, 2368aba76fdbSAndrew Morton "rchar: %llu\n" 2369aba76fdbSAndrew Morton "wchar: %llu\n" 2370aba76fdbSAndrew Morton "syscr: %llu\n" 2371aba76fdbSAndrew Morton "syscw: %llu\n" 2372aba76fdbSAndrew Morton "read_bytes: %llu\n" 2373aba76fdbSAndrew Morton "write_bytes: %llu\n" 2374aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 23757c44319dSAlexander Beregalov (unsigned long long)acct.rchar, 23767c44319dSAlexander Beregalov (unsigned long long)acct.wchar, 23777c44319dSAlexander Beregalov (unsigned long long)acct.syscr, 23787c44319dSAlexander Beregalov (unsigned long long)acct.syscw, 23797c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes, 23807c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes, 23817c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes); 2382293eb1e7SVasiliy Kulikov out_unlock: 2383293eb1e7SVasiliy Kulikov mutex_unlock(&task->signal->cred_guard_mutex); 2384293eb1e7SVasiliy Kulikov return result; 2385aba76fdbSAndrew Morton } 2386297c5d92SAndrea Righi 2387297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2388297c5d92SAndrea Righi { 2389297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2390297c5d92SAndrea Righi } 2391297c5d92SAndrea Righi 2392297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2393297c5d92SAndrea Righi { 2394297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2395297c5d92SAndrea Righi } 2396297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2397aba76fdbSAndrew Morton 239822d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 239922d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file, 240022d917d8SEric W. Biederman struct seq_operations *seq_ops) 240122d917d8SEric W. Biederman { 240222d917d8SEric W. Biederman struct user_namespace *ns = NULL; 240322d917d8SEric W. Biederman struct task_struct *task; 240422d917d8SEric W. Biederman struct seq_file *seq; 240522d917d8SEric W. Biederman int ret = -EINVAL; 240622d917d8SEric W. Biederman 240722d917d8SEric W. Biederman task = get_proc_task(inode); 240822d917d8SEric W. Biederman if (task) { 240922d917d8SEric W. Biederman rcu_read_lock(); 241022d917d8SEric W. Biederman ns = get_user_ns(task_cred_xxx(task, user_ns)); 241122d917d8SEric W. Biederman rcu_read_unlock(); 241222d917d8SEric W. Biederman put_task_struct(task); 241322d917d8SEric W. Biederman } 241422d917d8SEric W. Biederman if (!ns) 241522d917d8SEric W. Biederman goto err; 241622d917d8SEric W. Biederman 241722d917d8SEric W. Biederman ret = seq_open(file, seq_ops); 241822d917d8SEric W. Biederman if (ret) 241922d917d8SEric W. Biederman goto err_put_ns; 242022d917d8SEric W. Biederman 242122d917d8SEric W. Biederman seq = file->private_data; 242222d917d8SEric W. Biederman seq->private = ns; 242322d917d8SEric W. Biederman 242422d917d8SEric W. Biederman return 0; 242522d917d8SEric W. Biederman err_put_ns: 242622d917d8SEric W. Biederman put_user_ns(ns); 242722d917d8SEric W. Biederman err: 242822d917d8SEric W. Biederman return ret; 242922d917d8SEric W. Biederman } 243022d917d8SEric W. Biederman 243122d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file) 243222d917d8SEric W. Biederman { 243322d917d8SEric W. Biederman struct seq_file *seq = file->private_data; 243422d917d8SEric W. Biederman struct user_namespace *ns = seq->private; 243522d917d8SEric W. Biederman put_user_ns(ns); 243622d917d8SEric W. Biederman return seq_release(inode, file); 243722d917d8SEric W. Biederman } 243822d917d8SEric W. Biederman 243922d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file) 244022d917d8SEric W. Biederman { 244122d917d8SEric W. Biederman return proc_id_map_open(inode, file, &proc_uid_seq_operations); 244222d917d8SEric W. Biederman } 244322d917d8SEric W. Biederman 244422d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file) 244522d917d8SEric W. Biederman { 244622d917d8SEric W. Biederman return proc_id_map_open(inode, file, &proc_gid_seq_operations); 244722d917d8SEric W. Biederman } 244822d917d8SEric W. Biederman 2449f76d207aSEric W. Biederman static int proc_projid_map_open(struct inode *inode, struct file *file) 2450f76d207aSEric W. Biederman { 2451f76d207aSEric W. Biederman return proc_id_map_open(inode, file, &proc_projid_seq_operations); 2452f76d207aSEric W. Biederman } 2453f76d207aSEric W. Biederman 245422d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = { 245522d917d8SEric W. Biederman .open = proc_uid_map_open, 245622d917d8SEric W. Biederman .write = proc_uid_map_write, 245722d917d8SEric W. Biederman .read = seq_read, 245822d917d8SEric W. Biederman .llseek = seq_lseek, 245922d917d8SEric W. Biederman .release = proc_id_map_release, 246022d917d8SEric W. Biederman }; 246122d917d8SEric W. Biederman 246222d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = { 246322d917d8SEric W. Biederman .open = proc_gid_map_open, 246422d917d8SEric W. Biederman .write = proc_gid_map_write, 246522d917d8SEric W. Biederman .read = seq_read, 246622d917d8SEric W. Biederman .llseek = seq_lseek, 246722d917d8SEric W. Biederman .release = proc_id_map_release, 246822d917d8SEric W. Biederman }; 2469f76d207aSEric W. Biederman 2470f76d207aSEric W. Biederman static const struct file_operations proc_projid_map_operations = { 2471f76d207aSEric W. Biederman .open = proc_projid_map_open, 2472f76d207aSEric W. Biederman .write = proc_projid_map_write, 2473f76d207aSEric W. Biederman .read = seq_read, 2474f76d207aSEric W. Biederman .llseek = seq_lseek, 2475f76d207aSEric W. Biederman .release = proc_id_map_release, 2476f76d207aSEric W. Biederman }; 247722d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */ 247822d917d8SEric W. Biederman 247947830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 248047830723SKees Cook struct pid *pid, struct task_struct *task) 248147830723SKees Cook { 2482a9712bc1SAl Viro int err = lock_trace(task); 2483a9712bc1SAl Viro if (!err) { 248447830723SKees Cook seq_printf(m, "%08x\n", task->personality); 2485a9712bc1SAl Viro unlock_trace(task); 2486a9712bc1SAl Viro } 2487a9712bc1SAl Viro return err; 248847830723SKees Cook } 248947830723SKees Cook 2490801199ceSEric W. Biederman /* 249128a6d671SEric W. Biederman * Thread groups 249228a6d671SEric W. Biederman */ 249300977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2494c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 249520cdc894SEric W. Biederman 2496c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 2497631f9c18SAlexey Dobriyan DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), 2498631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2499640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 2500640708a2SPavel Emelyanov DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations), 2501640708a2SPavel Emelyanov #endif 2502631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 25036b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 2504b2211a36SAndrew Morton #ifdef CONFIG_NET 2505631f9c18SAlexey Dobriyan DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 2506b2211a36SAndrew Morton #endif 2507631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2508631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2509631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2510a9712bc1SAl Viro ONE("personality", S_IRUGO, proc_pid_personality), 25113036e7b4SJiri Olsa INF("limits", S_IRUGO, proc_pid_limits), 251243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2513631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 251443ae34cbSIngo Molnar #endif 25155091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 25165091faa4SMike Galbraith REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), 25175091faa4SMike Galbraith #endif 25184614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 2519ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2520a9712bc1SAl Viro INF("syscall", S_IRUGO, proc_pid_syscall), 2521ebcb6734SRoland McGrath #endif 2522631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2523631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tgid_stat), 2524631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2525b7643757SSiddhesh Poyarekar REG("maps", S_IRUGO, proc_pid_maps_operations), 252628a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2527b7643757SSiddhesh Poyarekar REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), 252828a6d671SEric W. Biederman #endif 2529631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2530631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2531631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2532631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2533631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2534631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 2535631f9c18SAlexey Dobriyan REG("mountstats", S_IRUSR, proc_mountstats_operations), 25361e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2537631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2538b7643757SSiddhesh Poyarekar REG("smaps", S_IRUGO, proc_pid_smaps_operations), 2539ca6b0bf0SAl Viro REG("pagemap", S_IRUGO, proc_pagemap_operations), 254028a6d671SEric W. Biederman #endif 254128a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2542631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 254328a6d671SEric W. Biederman #endif 254428a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2545631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 254628a6d671SEric W. Biederman #endif 25472ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 2548a9712bc1SAl Viro ONE("stack", S_IRUGO, proc_pid_stack), 254928a6d671SEric W. Biederman #endif 255028a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2551631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 255228a6d671SEric W. Biederman #endif 25539745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2554631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 25559745512cSArjan van de Ven #endif 25568793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 2557631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 255828a6d671SEric W. Biederman #endif 2559a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2560631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 2561a424316cSPaul Menage #endif 2562631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 2563fa0cbbf1SDavid Rientjes REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), 2564a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 256528a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2566631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2567631f9c18SAlexey Dobriyan REG("sessionid", S_IRUGO, proc_sessionid_operations), 256828a6d671SEric W. Biederman #endif 2569f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2570631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2571f4f154fdSAkinobu Mita #endif 2572698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 2573631f9c18SAlexey Dobriyan REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), 25743cb4a0bbSKawai, Hidehiro #endif 2575aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 25761d1221f3SVasiliy Kulikov INF("io", S_IRUSR, proc_tgid_io_accounting), 2577aba76fdbSAndrew Morton #endif 2578f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 2579f133eccaSChris Metcalf INF("hardwall", S_IRUGO, proc_pid_hardwall), 2580f133eccaSChris Metcalf #endif 258122d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 258222d917d8SEric W. Biederman REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), 258322d917d8SEric W. Biederman REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), 2584f76d207aSEric W. Biederman REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), 258522d917d8SEric W. Biederman #endif 258628a6d671SEric W. Biederman }; 258728a6d671SEric W. Biederman 258828a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 258928a6d671SEric W. Biederman void * dirent, filldir_t filldir) 259028a6d671SEric W. Biederman { 259128a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 259228a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 259328a6d671SEric W. Biederman } 259428a6d671SEric W. Biederman 259500977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 259628a6d671SEric W. Biederman .read = generic_read_dir, 259728a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 25986038f373SArnd Bergmann .llseek = default_llseek, 259928a6d671SEric W. Biederman }; 260028a6d671SEric W. Biederman 260100cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 260200cd8dd3SAl Viro { 26037bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 26047bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 260528a6d671SEric W. Biederman } 260628a6d671SEric W. Biederman 2607c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 260828a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 260928a6d671SEric W. Biederman .getattr = pid_getattr, 261028a6d671SEric W. Biederman .setattr = proc_setattr, 26110499680aSVasiliy Kulikov .permission = proc_pid_permission, 261228a6d671SEric W. Biederman }; 261328a6d671SEric W. Biederman 261460347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 26151da177e4SLinus Torvalds { 261648e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 26178578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 261848e6484dSEric W. Biederman struct qstr name; 26191da177e4SLinus Torvalds 262048e6484dSEric W. Biederman name.name = buf; 262160347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 26224f522a24SAl Viro /* no ->d_hash() rejects on procfs */ 262360347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 262448e6484dSEric W. Biederman if (dentry) { 262548e6484dSEric W. Biederman shrink_dcache_parent(dentry); 262648e6484dSEric W. Biederman d_drop(dentry); 262748e6484dSEric W. Biederman dput(dentry); 26281da177e4SLinus Torvalds } 26291da177e4SLinus Torvalds 263048e6484dSEric W. Biederman name.name = buf; 263160347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 263260347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 263348e6484dSEric W. Biederman if (!leader) 263448e6484dSEric W. Biederman goto out; 263548e6484dSEric W. Biederman 263648e6484dSEric W. Biederman name.name = "task"; 263748e6484dSEric W. Biederman name.len = strlen(name.name); 263848e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 263948e6484dSEric W. Biederman if (!dir) 264048e6484dSEric W. Biederman goto out_put_leader; 264148e6484dSEric W. Biederman 264248e6484dSEric W. Biederman name.name = buf; 264360347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 264448e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 264548e6484dSEric W. Biederman if (dentry) { 264648e6484dSEric W. Biederman shrink_dcache_parent(dentry); 264748e6484dSEric W. Biederman d_drop(dentry); 264848e6484dSEric W. Biederman dput(dentry); 26491da177e4SLinus Torvalds } 265048e6484dSEric W. Biederman 265148e6484dSEric W. Biederman dput(dir); 265248e6484dSEric W. Biederman out_put_leader: 265348e6484dSEric W. Biederman dput(leader); 265448e6484dSEric W. Biederman out: 265548e6484dSEric W. Biederman return; 26561da177e4SLinus Torvalds } 26571da177e4SLinus Torvalds 26580895e91dSRandy Dunlap /** 26590895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 26600895e91dSRandy Dunlap * @task: task that should be flushed. 26610895e91dSRandy Dunlap * 26620895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 266360347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 26640895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 26650895e91dSRandy Dunlap * 26660895e91dSRandy Dunlap * Looks in the dcache for 26670895e91dSRandy Dunlap * /proc/@pid 26680895e91dSRandy Dunlap * /proc/@tgid/task/@pid 26690895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 26700895e91dSRandy Dunlap * from the dcache. 26710895e91dSRandy Dunlap * 26720895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 26730895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 26740895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 26750895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 26760895e91dSRandy Dunlap * dcache entries at process exit time. 26770895e91dSRandy Dunlap * 26780895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 26790895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 26800895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 268160347f67SPavel Emelyanov */ 268260347f67SPavel Emelyanov 268360347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 268460347f67SPavel Emelyanov { 26859fcc2d15SEric W. Biederman int i; 26869b4d1cbeSOleg Nesterov struct pid *pid, *tgid; 2687130f77ecSPavel Emelyanov struct upid *upid; 2688130f77ecSPavel Emelyanov 2689130f77ecSPavel Emelyanov pid = task_pid(task); 2690130f77ecSPavel Emelyanov tgid = task_tgid(task); 26919fcc2d15SEric W. Biederman 26929fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2693130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2694130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 26959b4d1cbeSOleg Nesterov tgid->numbers[i].nr); 2696130f77ecSPavel Emelyanov } 269760347f67SPavel Emelyanov } 269860347f67SPavel Emelyanov 26999711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 27009711ef99SAdrian Bunk struct dentry * dentry, 2701c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2702444ceed8SEric W. Biederman { 2703444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2704444ceed8SEric W. Biederman struct inode *inode; 2705444ceed8SEric W. Biederman 270661a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2707444ceed8SEric W. Biederman if (!inode) 2708444ceed8SEric W. Biederman goto out; 2709444ceed8SEric W. Biederman 2710444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2711444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2712444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2713444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2714aed54175SVegard Nossum 2715bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff, 2716bfe86848SMiklos Szeredi ARRAY_SIZE(tgid_base_stuff))); 2717444ceed8SEric W. Biederman 2718fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2719444ceed8SEric W. Biederman 2720444ceed8SEric W. Biederman d_add(dentry, inode); 2721444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 27220b728e19SAl Viro if (pid_revalidate(dentry, 0)) 2723444ceed8SEric W. Biederman error = NULL; 2724444ceed8SEric W. Biederman out: 2725444ceed8SEric W. Biederman return error; 2726444ceed8SEric W. Biederman } 2727444ceed8SEric W. Biederman 272800cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 27291da177e4SLinus Torvalds { 2730e656d8a6SEric W. Biederman struct dentry *result = NULL; 27311da177e4SLinus Torvalds struct task_struct *task; 27321da177e4SLinus Torvalds unsigned tgid; 2733b488893aSPavel Emelyanov struct pid_namespace *ns; 27341da177e4SLinus Torvalds 27351da177e4SLinus Torvalds tgid = name_to_int(dentry); 27361da177e4SLinus Torvalds if (tgid == ~0U) 27371da177e4SLinus Torvalds goto out; 27381da177e4SLinus Torvalds 2739b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2740de758734SEric W. Biederman rcu_read_lock(); 2741b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 27421da177e4SLinus Torvalds if (task) 27431da177e4SLinus Torvalds get_task_struct(task); 2744de758734SEric W. Biederman rcu_read_unlock(); 27451da177e4SLinus Torvalds if (!task) 27461da177e4SLinus Torvalds goto out; 27471da177e4SLinus Torvalds 2748444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 274948e6484dSEric W. Biederman put_task_struct(task); 27501da177e4SLinus Torvalds out: 2751cd6a3ce9SEric W. Biederman return result; 27521da177e4SLinus Torvalds } 27531da177e4SLinus Torvalds 27541da177e4SLinus Torvalds /* 27550804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 27560bc58a91SEric W. Biederman * 27571da177e4SLinus Torvalds */ 275819fd4bb2SEric W. Biederman struct tgid_iter { 275919fd4bb2SEric W. Biederman unsigned int tgid; 27600804ef4bSEric W. Biederman struct task_struct *task; 276119fd4bb2SEric W. Biederman }; 276219fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 276319fd4bb2SEric W. Biederman { 27640804ef4bSEric W. Biederman struct pid *pid; 27651da177e4SLinus Torvalds 276619fd4bb2SEric W. Biederman if (iter.task) 276719fd4bb2SEric W. Biederman put_task_struct(iter.task); 27680804ef4bSEric W. Biederman rcu_read_lock(); 27690804ef4bSEric W. Biederman retry: 277019fd4bb2SEric W. Biederman iter.task = NULL; 277119fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 27720804ef4bSEric W. Biederman if (pid) { 277319fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 277419fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 27750804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 27760804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 27770804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 27780804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 27790804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 27800804ef4bSEric W. Biederman * 27810804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 27820804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 27830804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 27840804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 27850804ef4bSEric W. Biederman * As we don't care in the case of readdir. 27860bc58a91SEric W. Biederman */ 278719fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 278819fd4bb2SEric W. Biederman iter.tgid += 1; 27890804ef4bSEric W. Biederman goto retry; 279019fd4bb2SEric W. Biederman } 279119fd4bb2SEric W. Biederman get_task_struct(iter.task); 27921da177e4SLinus Torvalds } 2793454cc105SEric W. Biederman rcu_read_unlock(); 279419fd4bb2SEric W. Biederman return iter; 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds 2797*021ada7dSAl Viro #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1) 27981da177e4SLinus Torvalds 279961a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 280019fd4bb2SEric W. Biederman struct tgid_iter iter) 280161a28784SEric W. Biederman { 280261a28784SEric W. Biederman char name[PROC_NUMBUF]; 280319fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 280461a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 280519fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 280661a28784SEric W. Biederman } 280761a28784SEric W. Biederman 28080499680aSVasiliy Kulikov static int fake_filldir(void *buf, const char *name, int namelen, 28090499680aSVasiliy Kulikov loff_t offset, u64 ino, unsigned d_type) 28100499680aSVasiliy Kulikov { 28110499680aSVasiliy Kulikov return 0; 28120499680aSVasiliy Kulikov } 28130499680aSVasiliy Kulikov 28141da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 28151da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 28161da177e4SLinus Torvalds { 281719fd4bb2SEric W. Biederman struct tgid_iter iter; 2818b488893aSPavel Emelyanov struct pid_namespace *ns; 28190499680aSVasiliy Kulikov filldir_t __filldir; 2820*021ada7dSAl Viro loff_t pos = filp->f_pos; 28211da177e4SLinus Torvalds 2822*021ada7dSAl Viro if (pos >= PID_MAX_LIMIT + TGID_OFFSET) 2823801199ceSEric W. Biederman goto out; 28241da177e4SLinus Torvalds 2825*021ada7dSAl Viro if (pos == TGID_OFFSET - 1) { 2826*021ada7dSAl Viro if (proc_fill_cache(filp, dirent, filldir, "self", 4, 2827*021ada7dSAl Viro NULL, NULL, NULL) < 0) 2828*021ada7dSAl Viro goto out; 2829*021ada7dSAl Viro iter.tgid = 0; 2830*021ada7dSAl Viro } else { 2831*021ada7dSAl Viro iter.tgid = pos - TGID_OFFSET; 2832*021ada7dSAl Viro } 283319fd4bb2SEric W. Biederman iter.task = NULL; 2834*021ada7dSAl Viro ns = filp->f_dentry->d_sb->s_fs_info; 283519fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 283619fd4bb2SEric W. Biederman iter.task; 283719fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 28380499680aSVasiliy Kulikov if (has_pid_permissions(ns, iter.task, 2)) 28390499680aSVasiliy Kulikov __filldir = filldir; 28400499680aSVasiliy Kulikov else 28410499680aSVasiliy Kulikov __filldir = fake_filldir; 28420499680aSVasiliy Kulikov 284319fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 28440499680aSVasiliy Kulikov if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) { 284519fd4bb2SEric W. Biederman put_task_struct(iter.task); 28460804ef4bSEric W. Biederman goto out; 28471da177e4SLinus Torvalds } 28481da177e4SLinus Torvalds } 28490804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 28500804ef4bSEric W. Biederman out: 28511da177e4SLinus Torvalds return 0; 28521da177e4SLinus Torvalds } 28531da177e4SLinus Torvalds 28540bc58a91SEric W. Biederman /* 285528a6d671SEric W. Biederman * Tasks 285628a6d671SEric W. Biederman */ 2857c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 2858631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 28593835541dSJerome Marchand DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 28606b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 2861631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2862631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2863631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2864a9712bc1SAl Viro ONE("personality", S_IRUGO, proc_pid_personality), 28653036e7b4SJiri Olsa INF("limits", S_IRUGO, proc_pid_limits), 286643ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2867631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 286843ae34cbSIngo Molnar #endif 28694614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 2870ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2871a9712bc1SAl Viro INF("syscall", S_IRUGO, proc_pid_syscall), 2872ebcb6734SRoland McGrath #endif 2873631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2874631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tid_stat), 2875631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2876b7643757SSiddhesh Poyarekar REG("maps", S_IRUGO, proc_tid_maps_operations), 287781841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE 287881841161SCyrill Gorcunov REG("children", S_IRUGO, proc_tid_children_operations), 287981841161SCyrill Gorcunov #endif 288028a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2881b7643757SSiddhesh Poyarekar REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations), 288228a6d671SEric W. Biederman #endif 2883631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2884631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2885631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2886631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2887631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2888631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 28891e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2890631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2891b7643757SSiddhesh Poyarekar REG("smaps", S_IRUGO, proc_tid_smaps_operations), 2892ca6b0bf0SAl Viro REG("pagemap", S_IRUGO, proc_pagemap_operations), 289328a6d671SEric W. Biederman #endif 289428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2895631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 289628a6d671SEric W. Biederman #endif 289728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2898631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 289928a6d671SEric W. Biederman #endif 29002ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 2901a9712bc1SAl Viro ONE("stack", S_IRUGO, proc_pid_stack), 290228a6d671SEric W. Biederman #endif 290328a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2904631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 290528a6d671SEric W. Biederman #endif 29069745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2907631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 29089745512cSArjan van de Ven #endif 29098793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 2910631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 291128a6d671SEric W. Biederman #endif 2912a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2913631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 2914a424316cSPaul Menage #endif 2915631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 2916fa0cbbf1SDavid Rientjes REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), 2917a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 291828a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2919631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 292026ec3c64SAl Viro REG("sessionid", S_IRUGO, proc_sessionid_operations), 292128a6d671SEric W. Biederman #endif 2922f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2923631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2924f4f154fdSAkinobu Mita #endif 2925297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 29261d1221f3SVasiliy Kulikov INF("io", S_IRUSR, proc_tid_io_accounting), 2927297c5d92SAndrea Righi #endif 2928f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 2929f133eccaSChris Metcalf INF("hardwall", S_IRUGO, proc_pid_hardwall), 2930f133eccaSChris Metcalf #endif 293122d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 293222d917d8SEric W. Biederman REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), 293322d917d8SEric W. Biederman REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), 2934f76d207aSEric W. Biederman REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), 293522d917d8SEric W. Biederman #endif 293628a6d671SEric W. Biederman }; 293728a6d671SEric W. Biederman 293828a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 293928a6d671SEric W. Biederman void * dirent, filldir_t filldir) 294028a6d671SEric W. Biederman { 294128a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 294228a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 294328a6d671SEric W. Biederman } 294428a6d671SEric W. Biederman 294500cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 294600cd8dd3SAl Viro { 29477bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 29487bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 294928a6d671SEric W. Biederman } 295028a6d671SEric W. Biederman 295100977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 295228a6d671SEric W. Biederman .read = generic_read_dir, 295328a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 29546038f373SArnd Bergmann .llseek = default_llseek, 295528a6d671SEric W. Biederman }; 295628a6d671SEric W. Biederman 2957c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 295828a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 295928a6d671SEric W. Biederman .getattr = pid_getattr, 296028a6d671SEric W. Biederman .setattr = proc_setattr, 296128a6d671SEric W. Biederman }; 296228a6d671SEric W. Biederman 2963444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2964c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2965444ceed8SEric W. Biederman { 2966444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2967444ceed8SEric W. Biederman struct inode *inode; 296861a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2969444ceed8SEric W. Biederman 2970444ceed8SEric W. Biederman if (!inode) 2971444ceed8SEric W. Biederman goto out; 2972444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2973444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2974444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2975444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2976aed54175SVegard Nossum 2977bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff, 2978bfe86848SMiklos Szeredi ARRAY_SIZE(tid_base_stuff))); 2979444ceed8SEric W. Biederman 2980fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2981444ceed8SEric W. Biederman 2982444ceed8SEric W. Biederman d_add(dentry, inode); 2983444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 29840b728e19SAl Viro if (pid_revalidate(dentry, 0)) 2985444ceed8SEric W. Biederman error = NULL; 2986444ceed8SEric W. Biederman out: 2987444ceed8SEric W. Biederman return error; 2988444ceed8SEric W. Biederman } 2989444ceed8SEric W. Biederman 299000cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 299128a6d671SEric W. Biederman { 299228a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 299328a6d671SEric W. Biederman struct task_struct *task; 299428a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 299528a6d671SEric W. Biederman unsigned tid; 2996b488893aSPavel Emelyanov struct pid_namespace *ns; 299728a6d671SEric W. Biederman 299828a6d671SEric W. Biederman if (!leader) 299928a6d671SEric W. Biederman goto out_no_task; 300028a6d671SEric W. Biederman 300128a6d671SEric W. Biederman tid = name_to_int(dentry); 300228a6d671SEric W. Biederman if (tid == ~0U) 300328a6d671SEric W. Biederman goto out; 300428a6d671SEric W. Biederman 3005b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 300628a6d671SEric W. Biederman rcu_read_lock(); 3007b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 300828a6d671SEric W. Biederman if (task) 300928a6d671SEric W. Biederman get_task_struct(task); 301028a6d671SEric W. Biederman rcu_read_unlock(); 301128a6d671SEric W. Biederman if (!task) 301228a6d671SEric W. Biederman goto out; 3013bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 301428a6d671SEric W. Biederman goto out_drop_task; 301528a6d671SEric W. Biederman 3016444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 301728a6d671SEric W. Biederman out_drop_task: 301828a6d671SEric W. Biederman put_task_struct(task); 301928a6d671SEric W. Biederman out: 302028a6d671SEric W. Biederman put_task_struct(leader); 302128a6d671SEric W. Biederman out_no_task: 302228a6d671SEric W. Biederman return result; 302328a6d671SEric W. Biederman } 302428a6d671SEric W. Biederman 302528a6d671SEric W. Biederman /* 30260bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 30270bc58a91SEric W. Biederman * 30280bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 30290bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 30300bc58a91SEric W. Biederman * directory we have more work todo. 30310bc58a91SEric W. Biederman * 30320bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 30330bc58a91SEric W. Biederman * 30340bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 30350bc58a91SEric W. Biederman * threads past it. 30360bc58a91SEric W. Biederman */ 3037cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 3038b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 30390bc58a91SEric W. Biederman { 3040a872ff0cSOleg Nesterov struct task_struct *pos; 30410bc58a91SEric W. Biederman 3042cc288738SEric W. Biederman rcu_read_lock(); 30430bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 30440bc58a91SEric W. Biederman if (tid && (nr > 0)) { 3045b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 3046a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 3047a872ff0cSOleg Nesterov goto found; 30480bc58a91SEric W. Biederman } 30490bc58a91SEric W. Biederman 30500bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 30510bc58a91SEric W. Biederman pos = NULL; 3052a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 3053a872ff0cSOleg Nesterov goto out; 3054a872ff0cSOleg Nesterov 3055a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 3056a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 3057a872ff0cSOleg Nesterov */ 3058a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 3059a872ff0cSOleg Nesterov pos = next_thread(pos); 3060a872ff0cSOleg Nesterov if (pos == leader) { 3061a872ff0cSOleg Nesterov pos = NULL; 3062a872ff0cSOleg Nesterov goto out; 3063a872ff0cSOleg Nesterov } 3064a872ff0cSOleg Nesterov } 3065a872ff0cSOleg Nesterov found: 3066a872ff0cSOleg Nesterov get_task_struct(pos); 3067a872ff0cSOleg Nesterov out: 3068cc288738SEric W. Biederman rcu_read_unlock(); 30690bc58a91SEric W. Biederman return pos; 30700bc58a91SEric W. Biederman } 30710bc58a91SEric W. Biederman 30720bc58a91SEric W. Biederman /* 30730bc58a91SEric W. Biederman * Find the next thread in the thread list. 30740bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 30750bc58a91SEric W. Biederman * 30760bc58a91SEric W. Biederman * The reference to the input task_struct is released. 30770bc58a91SEric W. Biederman */ 30780bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 30790bc58a91SEric W. Biederman { 3080c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3081cc288738SEric W. Biederman rcu_read_lock(); 3082c1df7fb8SOleg Nesterov if (pid_alive(start)) { 30830bc58a91SEric W. Biederman pos = next_thread(start); 3084c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 30850bc58a91SEric W. Biederman pos = NULL; 3086c1df7fb8SOleg Nesterov else 3087c1df7fb8SOleg Nesterov get_task_struct(pos); 3088c1df7fb8SOleg Nesterov } 3089cc288738SEric W. Biederman rcu_read_unlock(); 30900bc58a91SEric W. Biederman put_task_struct(start); 30910bc58a91SEric W. Biederman return pos; 30920bc58a91SEric W. Biederman } 30930bc58a91SEric W. Biederman 309461a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 309561a28784SEric W. Biederman struct task_struct *task, int tid) 309661a28784SEric W. Biederman { 309761a28784SEric W. Biederman char name[PROC_NUMBUF]; 309861a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 309961a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 310061a28784SEric W. Biederman proc_task_instantiate, task, NULL); 310161a28784SEric W. Biederman } 310261a28784SEric W. Biederman 31031da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 31041da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 31051da177e4SLinus Torvalds { 31062fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 31071da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 31087d895244SGuillaume Chazarain struct task_struct *leader = NULL; 31090bc58a91SEric W. Biederman struct task_struct *task; 31101da177e4SLinus Torvalds int retval = -ENOENT; 31111da177e4SLinus Torvalds ino_t ino; 31120bc58a91SEric W. Biederman int tid; 3113b488893aSPavel Emelyanov struct pid_namespace *ns; 31141da177e4SLinus Torvalds 31157d895244SGuillaume Chazarain task = get_proc_task(inode); 31167d895244SGuillaume Chazarain if (!task) 31177d895244SGuillaume Chazarain goto out_no_task; 31187d895244SGuillaume Chazarain rcu_read_lock(); 31197d895244SGuillaume Chazarain if (pid_alive(task)) { 31207d895244SGuillaume Chazarain leader = task->group_leader; 31217d895244SGuillaume Chazarain get_task_struct(leader); 31227d895244SGuillaume Chazarain } 31237d895244SGuillaume Chazarain rcu_read_unlock(); 31247d895244SGuillaume Chazarain put_task_struct(task); 312599f89551SEric W. Biederman if (!leader) 312699f89551SEric W. Biederman goto out_no_task; 31271da177e4SLinus Torvalds retval = 0; 31281da177e4SLinus Torvalds 3129ee568b25SLinus Torvalds switch ((unsigned long)filp->f_pos) { 31301da177e4SLinus Torvalds case 0: 31311da177e4SLinus Torvalds ino = inode->i_ino; 3132ee6f779bSZhang Le if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0) 31331da177e4SLinus Torvalds goto out; 3134ee6f779bSZhang Le filp->f_pos++; 31351da177e4SLinus Torvalds /* fall through */ 31361da177e4SLinus Torvalds case 1: 31371da177e4SLinus Torvalds ino = parent_ino(dentry); 3138ee6f779bSZhang Le if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0) 31391da177e4SLinus Torvalds goto out; 3140ee6f779bSZhang Le filp->f_pos++; 31411da177e4SLinus Torvalds /* fall through */ 31421da177e4SLinus Torvalds } 31431da177e4SLinus Torvalds 31440bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 31450bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 31460bc58a91SEric W. Biederman */ 3147b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 31482b47c361SMathieu Desnoyers tid = (int)filp->f_version; 31490bc58a91SEric W. Biederman filp->f_version = 0; 3150ee6f779bSZhang Le for (task = first_tid(leader, tid, filp->f_pos - 2, ns); 31510bc58a91SEric W. Biederman task; 3152ee6f779bSZhang Le task = next_tid(task), filp->f_pos++) { 3153b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 315461a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 31550bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 31560bc58a91SEric W. Biederman * pid for the next readir call */ 31572b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 31580bc58a91SEric W. Biederman put_task_struct(task); 31591da177e4SLinus Torvalds break; 31600bc58a91SEric W. Biederman } 31611da177e4SLinus Torvalds } 31621da177e4SLinus Torvalds out: 316399f89551SEric W. Biederman put_task_struct(leader); 316499f89551SEric W. Biederman out_no_task: 31651da177e4SLinus Torvalds return retval; 31661da177e4SLinus Torvalds } 31676e66b52bSEric W. Biederman 31686e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 31696e66b52bSEric W. Biederman { 31706e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 317199f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 31726e66b52bSEric W. Biederman generic_fillattr(inode, stat); 31736e66b52bSEric W. Biederman 317499f89551SEric W. Biederman if (p) { 317599f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 317699f89551SEric W. Biederman put_task_struct(p); 31776e66b52bSEric W. Biederman } 31786e66b52bSEric W. Biederman 31796e66b52bSEric W. Biederman return 0; 31806e66b52bSEric W. Biederman } 318128a6d671SEric W. Biederman 3182c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 318328a6d671SEric W. Biederman .lookup = proc_task_lookup, 318428a6d671SEric W. Biederman .getattr = proc_task_getattr, 318528a6d671SEric W. Biederman .setattr = proc_setattr, 31860499680aSVasiliy Kulikov .permission = proc_pid_permission, 318728a6d671SEric W. Biederman }; 318828a6d671SEric W. Biederman 318900977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 319028a6d671SEric W. Biederman .read = generic_read_dir, 319128a6d671SEric W. Biederman .readdir = proc_task_readdir, 31926038f373SArnd Bergmann .llseek = default_llseek, 319328a6d671SEric W. Biederman }; 3194