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> 76a424316cSPaul Menage #include <linux/cgroup.h> 771da177e4SLinus Torvalds #include <linux/cpuset.h> 781da177e4SLinus Torvalds #include <linux/audit.h> 795addc5ddSAl Viro #include <linux/poll.h> 801651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 818ac773b4SAlexey Dobriyan #include <linux/oom.h> 823cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 8360347f67SPavel Emelyanov #include <linux/pid_namespace.h> 8422d917d8SEric W. Biederman #include <linux/user_namespace.h> 855ad4e53bSAl Viro #include <linux/fs_struct.h> 865a0e3ad6STejun Heo #include <linux/slab.h> 87640708a2SPavel Emelyanov #include <linux/flex_array.h> 88f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 89f133eccaSChris Metcalf #include <asm/hardwall.h> 90f133eccaSChris Metcalf #endif 9143d2b113SKAMEZAWA Hiroyuki #include <trace/events/oom.h> 921da177e4SLinus Torvalds #include "internal.h" 93*faf60af1SCyrill Gorcunov #include "fd.h" 941da177e4SLinus Torvalds 950f2fe20fSEric W. Biederman /* NOTE: 960f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 970f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 980f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 990f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 1000f2fe20fSEric W. Biederman * 1010f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 1020f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 1030f2fe20fSEric W. Biederman */ 1040f2fe20fSEric W. Biederman 1051da177e4SLinus Torvalds struct pid_entry { 1061da177e4SLinus Torvalds char *name; 107c5141e6dSEric Dumazet int len; 108d161a13fSAl Viro umode_t mode; 109c5ef1c42SArjan van de Ven const struct inode_operations *iop; 11000977a59SArjan van de Ven const struct file_operations *fop; 11120cdc894SEric W. Biederman union proc_op op; 1121da177e4SLinus Torvalds }; 1131da177e4SLinus Torvalds 11461a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 11520cdc894SEric W. Biederman .name = (NAME), \ 116c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 11720cdc894SEric W. Biederman .mode = MODE, \ 11820cdc894SEric W. Biederman .iop = IOP, \ 11920cdc894SEric W. Biederman .fop = FOP, \ 12020cdc894SEric W. Biederman .op = OP, \ 12120cdc894SEric W. Biederman } 12220cdc894SEric W. Biederman 123631f9c18SAlexey Dobriyan #define DIR(NAME, MODE, iops, fops) \ 124631f9c18SAlexey Dobriyan NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) 125631f9c18SAlexey Dobriyan #define LNK(NAME, get_link) \ 12661a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 12720cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 128631f9c18SAlexey Dobriyan { .proc_get_link = get_link } ) 129631f9c18SAlexey Dobriyan #define REG(NAME, MODE, fops) \ 130631f9c18SAlexey Dobriyan NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) 131631f9c18SAlexey Dobriyan #define INF(NAME, MODE, read) \ 13261a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 13320cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 134631f9c18SAlexey Dobriyan { .proc_read = read } ) 135631f9c18SAlexey Dobriyan #define ONE(NAME, MODE, show) \ 136be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 137be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 138631f9c18SAlexey Dobriyan { .proc_show = show } ) 1391da177e4SLinus Torvalds 140aed54175SVegard Nossum /* 141aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 142aed54175SVegard Nossum * and .. links. 143aed54175SVegard Nossum */ 144aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 145aed54175SVegard Nossum unsigned int n) 146aed54175SVegard Nossum { 147aed54175SVegard Nossum unsigned int i; 148aed54175SVegard Nossum unsigned int count; 149aed54175SVegard Nossum 150aed54175SVegard Nossum count = 0; 151aed54175SVegard Nossum for (i = 0; i < n; ++i) { 152aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 153aed54175SVegard Nossum ++count; 154aed54175SVegard Nossum } 155aed54175SVegard Nossum 156aed54175SVegard Nossum return count; 157aed54175SVegard Nossum } 158aed54175SVegard Nossum 159f7ad3c6bSMiklos Szeredi static int get_task_root(struct task_struct *task, struct path *root) 1601da177e4SLinus Torvalds { 1617c2c7d99SHugh Dickins int result = -ENOENT; 1627c2c7d99SHugh Dickins 1630494f6ecSMiklos Szeredi task_lock(task); 164f7ad3c6bSMiklos Szeredi if (task->fs) { 165f7ad3c6bSMiklos Szeredi get_fs_root(task->fs, root); 1667c2c7d99SHugh Dickins result = 0; 1677c2c7d99SHugh Dickins } 1680494f6ecSMiklos Szeredi task_unlock(task); 1697c2c7d99SHugh Dickins return result; 1700494f6ecSMiklos Szeredi } 1710494f6ecSMiklos Szeredi 1727773fbc5SCyrill Gorcunov static int proc_cwd_link(struct dentry *dentry, struct path *path) 1730494f6ecSMiklos Szeredi { 1747773fbc5SCyrill Gorcunov struct task_struct *task = get_proc_task(dentry->d_inode); 1750494f6ecSMiklos Szeredi int result = -ENOENT; 17699f89551SEric W. Biederman 17799f89551SEric W. Biederman if (task) { 178f7ad3c6bSMiklos Szeredi task_lock(task); 179f7ad3c6bSMiklos Szeredi if (task->fs) { 180f7ad3c6bSMiklos Szeredi get_fs_pwd(task->fs, path); 181f7ad3c6bSMiklos Szeredi result = 0; 182f7ad3c6bSMiklos Szeredi } 183f7ad3c6bSMiklos Szeredi task_unlock(task); 18499f89551SEric W. Biederman put_task_struct(task); 18599f89551SEric W. Biederman } 1861da177e4SLinus Torvalds return result; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1897773fbc5SCyrill Gorcunov static int proc_root_link(struct dentry *dentry, struct path *path) 1901da177e4SLinus Torvalds { 1917773fbc5SCyrill Gorcunov struct task_struct *task = get_proc_task(dentry->d_inode); 1921da177e4SLinus Torvalds int result = -ENOENT; 19399f89551SEric W. Biederman 19499f89551SEric W. Biederman if (task) { 195f7ad3c6bSMiklos Szeredi result = get_task_root(task, path); 19699f89551SEric W. Biederman put_task_struct(task); 19799f89551SEric W. Biederman } 1981da177e4SLinus Torvalds return result; 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2021da177e4SLinus Torvalds { 2031da177e4SLinus Torvalds int res = 0; 2041da177e4SLinus Torvalds unsigned int len; 2051da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2061da177e4SLinus Torvalds if (!mm) 2071da177e4SLinus Torvalds goto out; 2081da177e4SLinus Torvalds if (!mm->arg_end) 2091da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds if (len > PAGE_SIZE) 2141da177e4SLinus Torvalds len = PAGE_SIZE; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2191da177e4SLinus Torvalds // assume application is using setproctitle(3). 2201da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2211da177e4SLinus Torvalds len = strnlen(buffer, res); 2221da177e4SLinus Torvalds if (len < res) { 2231da177e4SLinus Torvalds res = len; 2241da177e4SLinus Torvalds } else { 2251da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2261da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2271da177e4SLinus Torvalds len = PAGE_SIZE - res; 2281da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2291da177e4SLinus Torvalds res = strnlen(buffer, res); 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds out_mm: 2331da177e4SLinus Torvalds mmput(mm); 2341da177e4SLinus Torvalds out: 2351da177e4SLinus Torvalds return res; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2391da177e4SLinus Torvalds { 240e7dcd999SCong Wang struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ); 2412fadaef4SAl Viro int res = PTR_ERR(mm); 2422fadaef4SAl Viro if (mm && !IS_ERR(mm)) { 2431da177e4SLinus Torvalds unsigned int nwords = 0; 244dfe6b7d9SHannes Eder do { 2451da177e4SLinus Torvalds nwords += 2; 246dfe6b7d9SHannes Eder } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 2471da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 2481da177e4SLinus Torvalds if (res > PAGE_SIZE) 2491da177e4SLinus Torvalds res = PAGE_SIZE; 2501da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 2511da177e4SLinus Torvalds mmput(mm); 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds return res; 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2581da177e4SLinus Torvalds /* 2591da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2601da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2611da177e4SLinus Torvalds */ 2621da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 2631da177e4SLinus Torvalds { 264ffb45122SAlexey Dobriyan unsigned long wchan; 2659281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds wchan = get_wchan(task); 2681da177e4SLinus Torvalds 2699d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 270f83ce3e6SJake Edge if (!ptrace_may_access(task, PTRACE_MODE_READ)) 271f83ce3e6SJake Edge return 0; 272f83ce3e6SJake Edge else 2731da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 2749d65cb4aSAlexey Dobriyan else 2759d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2781da177e4SLinus Torvalds 279a9712bc1SAl Viro static int lock_trace(struct task_struct *task) 280a9712bc1SAl Viro { 281a9712bc1SAl Viro int err = mutex_lock_killable(&task->signal->cred_guard_mutex); 282a9712bc1SAl Viro if (err) 283a9712bc1SAl Viro return err; 284a9712bc1SAl Viro if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) { 285a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 286a9712bc1SAl Viro return -EPERM; 287a9712bc1SAl Viro } 288a9712bc1SAl Viro return 0; 289a9712bc1SAl Viro } 290a9712bc1SAl Viro 291a9712bc1SAl Viro static void unlock_trace(struct task_struct *task) 292a9712bc1SAl Viro { 293a9712bc1SAl Viro mutex_unlock(&task->signal->cred_guard_mutex); 294a9712bc1SAl Viro } 295a9712bc1SAl Viro 2962ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 2972ec220e2SKen Chen 2982ec220e2SKen Chen #define MAX_STACK_TRACE_DEPTH 64 2992ec220e2SKen Chen 3002ec220e2SKen Chen static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, 3012ec220e2SKen Chen struct pid *pid, struct task_struct *task) 3022ec220e2SKen Chen { 3032ec220e2SKen Chen struct stack_trace trace; 3042ec220e2SKen Chen unsigned long *entries; 305a9712bc1SAl Viro int err; 3062ec220e2SKen Chen int i; 3072ec220e2SKen Chen 3082ec220e2SKen Chen entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL); 3092ec220e2SKen Chen if (!entries) 3102ec220e2SKen Chen return -ENOMEM; 3112ec220e2SKen Chen 3122ec220e2SKen Chen trace.nr_entries = 0; 3132ec220e2SKen Chen trace.max_entries = MAX_STACK_TRACE_DEPTH; 3142ec220e2SKen Chen trace.entries = entries; 3152ec220e2SKen Chen trace.skip = 0; 316a9712bc1SAl Viro 317a9712bc1SAl Viro err = lock_trace(task); 318a9712bc1SAl Viro if (!err) { 3192ec220e2SKen Chen save_stack_trace_tsk(task, &trace); 3202ec220e2SKen Chen 3212ec220e2SKen Chen for (i = 0; i < trace.nr_entries; i++) { 32251e03149SKonstantin Khlebnikov seq_printf(m, "[<%pK>] %pS\n", 3232ec220e2SKen Chen (void *)entries[i], (void *)entries[i]); 3242ec220e2SKen Chen } 325a9712bc1SAl Viro unlock_trace(task); 326a9712bc1SAl Viro } 3272ec220e2SKen Chen kfree(entries); 3282ec220e2SKen Chen 329a9712bc1SAl Viro return err; 3302ec220e2SKen Chen } 3312ec220e2SKen Chen #endif 3322ec220e2SKen Chen 3331da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3341da177e4SLinus Torvalds /* 3351da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3361da177e4SLinus Torvalds */ 3371da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3381da177e4SLinus Torvalds { 339172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 340826e08b0SIngo Molnar (unsigned long long)task->se.sum_exec_runtime, 341826e08b0SIngo Molnar (unsigned long long)task->sched_info.run_delay, 3422d72376bSIngo Molnar task->sched_info.pcount); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds #endif 3451da177e4SLinus Torvalds 3469745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3479745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3489745512cSArjan van de Ven { 3499745512cSArjan van de Ven int i; 35013d77c37SHiroshi Shimamoto struct inode *inode = m->private; 35113d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3529745512cSArjan van de Ven 35313d77c37SHiroshi Shimamoto if (!task) 35413d77c37SHiroshi Shimamoto return -ESRCH; 35513d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3569745512cSArjan van de Ven for (i = 0; i < 32; i++) { 35734e49d4fSJoe Perches struct latency_record *lr = &task->latency_record[i]; 35834e49d4fSJoe Perches if (lr->backtrace[0]) { 3599745512cSArjan van de Ven int q; 3609745512cSArjan van de Ven seq_printf(m, "%i %li %li", 36134e49d4fSJoe Perches lr->count, lr->time, lr->max); 3629745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 36334e49d4fSJoe Perches unsigned long bt = lr->backtrace[q]; 36434e49d4fSJoe Perches if (!bt) 3659745512cSArjan van de Ven break; 36634e49d4fSJoe Perches if (bt == ULONG_MAX) 3679745512cSArjan van de Ven break; 36834e49d4fSJoe Perches seq_printf(m, " %ps", (void *)bt); 3699745512cSArjan van de Ven } 3709d6de12fSAlexey Dobriyan seq_putc(m, '\n'); 3719745512cSArjan van de Ven } 3729745512cSArjan van de Ven 3739745512cSArjan van de Ven } 37413d77c37SHiroshi Shimamoto put_task_struct(task); 3759745512cSArjan van de Ven return 0; 3769745512cSArjan van de Ven } 3779745512cSArjan van de Ven 3789745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3799745512cSArjan van de Ven { 38013d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 381d6643d12SHiroshi Shimamoto } 382d6643d12SHiroshi Shimamoto 3839745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 3849745512cSArjan van de Ven size_t count, loff_t *offs) 3859745512cSArjan van de Ven { 38613d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 3879745512cSArjan van de Ven 38813d77c37SHiroshi Shimamoto if (!task) 38913d77c37SHiroshi Shimamoto return -ESRCH; 3909745512cSArjan van de Ven clear_all_latency_tracing(task); 39113d77c37SHiroshi Shimamoto put_task_struct(task); 3929745512cSArjan van de Ven 3939745512cSArjan van de Ven return count; 3949745512cSArjan van de Ven } 3959745512cSArjan van de Ven 3969745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 3979745512cSArjan van de Ven .open = lstats_open, 3989745512cSArjan van de Ven .read = seq_read, 3999745512cSArjan van de Ven .write = lstats_write, 4009745512cSArjan van de Ven .llseek = seq_lseek, 40113d77c37SHiroshi Shimamoto .release = single_release, 4029745512cSArjan van de Ven }; 4039745512cSArjan van de Ven 4049745512cSArjan van de Ven #endif 4059745512cSArjan van de Ven 4061da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4071da177e4SLinus Torvalds { 408a7f638f9SDavid Rientjes unsigned long totalpages = totalram_pages + total_swap_pages; 409b95c35e7SOleg Nesterov unsigned long points = 0; 4101da177e4SLinus Torvalds 41119c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 412b95c35e7SOleg Nesterov if (pid_alive(task)) 413a7f638f9SDavid Rientjes points = oom_badness(task, NULL, NULL, totalpages) * 414a7f638f9SDavid Rientjes 1000 / totalpages; 41519c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4161da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 419d85f50d5SNeil Horman struct limit_names { 420d85f50d5SNeil Horman char *name; 421d85f50d5SNeil Horman char *unit; 422d85f50d5SNeil Horman }; 423d85f50d5SNeil Horman 424d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 425cff4edb5SKees Cook [RLIMIT_CPU] = {"Max cpu time", "seconds"}, 426d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 427d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 428d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 429d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 430d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 431d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 432d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 433d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 434d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 435d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 436d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 437d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 438d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 439d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4408808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 441d85f50d5SNeil Horman }; 442d85f50d5SNeil Horman 443d85f50d5SNeil Horman /* Display limits for a process */ 444d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 445d85f50d5SNeil Horman { 446d85f50d5SNeil Horman unsigned int i; 447d85f50d5SNeil Horman int count = 0; 448d85f50d5SNeil Horman unsigned long flags; 449d85f50d5SNeil Horman char *bufptr = buffer; 450d85f50d5SNeil Horman 451d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 452d85f50d5SNeil Horman 453a6bebbc8SLai Jiangshan if (!lock_task_sighand(task, &flags)) 454d85f50d5SNeil Horman return 0; 455d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 456d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 457d85f50d5SNeil Horman 458d85f50d5SNeil Horman /* 459d85f50d5SNeil Horman * print the file header 460d85f50d5SNeil Horman */ 461d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 462d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 463d85f50d5SNeil Horman 464d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 465d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 466d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 467d85f50d5SNeil Horman lnames[i].name, "unlimited"); 468d85f50d5SNeil Horman else 469d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 470d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 471d85f50d5SNeil Horman 472d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 473d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 474d85f50d5SNeil Horman else 475d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 476d85f50d5SNeil Horman rlim[i].rlim_max); 477d85f50d5SNeil Horman 478d85f50d5SNeil Horman if (lnames[i].unit) 479d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 480d85f50d5SNeil Horman lnames[i].unit); 481d85f50d5SNeil Horman else 482d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 483d85f50d5SNeil Horman } 484d85f50d5SNeil Horman 485d85f50d5SNeil Horman return count; 486d85f50d5SNeil Horman } 487d85f50d5SNeil Horman 488ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 489ebcb6734SRoland McGrath static int proc_pid_syscall(struct task_struct *task, char *buffer) 490ebcb6734SRoland McGrath { 491ebcb6734SRoland McGrath long nr; 492ebcb6734SRoland McGrath unsigned long args[6], sp, pc; 493a9712bc1SAl Viro int res = lock_trace(task); 494a9712bc1SAl Viro if (res) 495a9712bc1SAl Viro return res; 496ebcb6734SRoland McGrath 497ebcb6734SRoland McGrath if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) 498a9712bc1SAl Viro res = sprintf(buffer, "running\n"); 499a9712bc1SAl Viro else if (nr < 0) 500a9712bc1SAl Viro res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc); 501a9712bc1SAl Viro else 502a9712bc1SAl Viro res = sprintf(buffer, 503ebcb6734SRoland McGrath "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", 504ebcb6734SRoland McGrath nr, 505ebcb6734SRoland McGrath args[0], args[1], args[2], args[3], args[4], args[5], 506ebcb6734SRoland McGrath sp, pc); 507a9712bc1SAl Viro unlock_trace(task); 508a9712bc1SAl Viro return res; 509ebcb6734SRoland McGrath } 510ebcb6734SRoland McGrath #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ 511ebcb6734SRoland McGrath 5121da177e4SLinus Torvalds /************************************************************************/ 5131da177e4SLinus Torvalds /* Here the fs part begins */ 5141da177e4SLinus Torvalds /************************************************************************/ 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* permission checks */ 517778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5181da177e4SLinus Torvalds { 519778c1144SEric W. Biederman struct task_struct *task; 520778c1144SEric W. Biederman int allowed = 0; 521df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 522df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 523df26c40eSEric W. Biederman * information. 524778c1144SEric W. Biederman */ 525778c1144SEric W. Biederman task = get_proc_task(inode); 526df26c40eSEric W. Biederman if (task) { 527006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 528778c1144SEric W. Biederman put_task_struct(task); 529df26c40eSEric W. Biederman } 530778c1144SEric W. Biederman return allowed; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5336b4e306aSEric W. Biederman int proc_setattr(struct dentry *dentry, struct iattr *attr) 5346d76fa58SLinus Torvalds { 5356d76fa58SLinus Torvalds int error; 5366d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5376d76fa58SLinus Torvalds 5386d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5396d76fa58SLinus Torvalds return -EPERM; 5406d76fa58SLinus Torvalds 5416d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5421025774cSChristoph Hellwig if (error) 5436d76fa58SLinus Torvalds return error; 5441025774cSChristoph Hellwig 5451025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 5461025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) { 5471025774cSChristoph Hellwig error = vmtruncate(inode, attr->ia_size); 5481025774cSChristoph Hellwig if (error) 5491025774cSChristoph Hellwig return error; 5501025774cSChristoph Hellwig } 5511025774cSChristoph Hellwig 5521025774cSChristoph Hellwig setattr_copy(inode, attr); 5531025774cSChristoph Hellwig mark_inode_dirty(inode); 5541025774cSChristoph Hellwig return 0; 5556d76fa58SLinus Torvalds } 5566d76fa58SLinus Torvalds 5570499680aSVasiliy Kulikov /* 5580499680aSVasiliy Kulikov * May current process learn task's sched/cmdline info (for hide_pid_min=1) 5590499680aSVasiliy Kulikov * or euid/egid (for hide_pid_min=2)? 5600499680aSVasiliy Kulikov */ 5610499680aSVasiliy Kulikov static bool has_pid_permissions(struct pid_namespace *pid, 5620499680aSVasiliy Kulikov struct task_struct *task, 5630499680aSVasiliy Kulikov int hide_pid_min) 5640499680aSVasiliy Kulikov { 5650499680aSVasiliy Kulikov if (pid->hide_pid < hide_pid_min) 5660499680aSVasiliy Kulikov return true; 5670499680aSVasiliy Kulikov if (in_group_p(pid->pid_gid)) 5680499680aSVasiliy Kulikov return true; 5690499680aSVasiliy Kulikov return ptrace_may_access(task, PTRACE_MODE_READ); 5700499680aSVasiliy Kulikov } 5710499680aSVasiliy Kulikov 5720499680aSVasiliy Kulikov 5730499680aSVasiliy Kulikov static int proc_pid_permission(struct inode *inode, int mask) 5740499680aSVasiliy Kulikov { 5750499680aSVasiliy Kulikov struct pid_namespace *pid = inode->i_sb->s_fs_info; 5760499680aSVasiliy Kulikov struct task_struct *task; 5770499680aSVasiliy Kulikov bool has_perms; 5780499680aSVasiliy Kulikov 5790499680aSVasiliy Kulikov task = get_proc_task(inode); 580a2ef990aSXiaotian Feng if (!task) 581a2ef990aSXiaotian Feng return -ESRCH; 5820499680aSVasiliy Kulikov has_perms = has_pid_permissions(pid, task, 1); 5830499680aSVasiliy Kulikov put_task_struct(task); 5840499680aSVasiliy Kulikov 5850499680aSVasiliy Kulikov if (!has_perms) { 5860499680aSVasiliy Kulikov if (pid->hide_pid == 2) { 5870499680aSVasiliy Kulikov /* 5880499680aSVasiliy Kulikov * Let's make getdents(), stat(), and open() 5890499680aSVasiliy Kulikov * consistent with each other. If a process 5900499680aSVasiliy Kulikov * may not stat() a file, it shouldn't be seen 5910499680aSVasiliy Kulikov * in procfs at all. 5920499680aSVasiliy Kulikov */ 5930499680aSVasiliy Kulikov return -ENOENT; 5940499680aSVasiliy Kulikov } 5950499680aSVasiliy Kulikov 5960499680aSVasiliy Kulikov return -EPERM; 5970499680aSVasiliy Kulikov } 5980499680aSVasiliy Kulikov return generic_permission(inode, mask); 5990499680aSVasiliy Kulikov } 6000499680aSVasiliy Kulikov 6010499680aSVasiliy Kulikov 6020499680aSVasiliy Kulikov 603c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 6046d76fa58SLinus Torvalds .setattr = proc_setattr, 6056d76fa58SLinus Torvalds }; 6066d76fa58SLinus Torvalds 6071da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 6101da177e4SLinus Torvalds size_t count, loff_t *ppos) 6111da177e4SLinus Torvalds { 6122fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 6131da177e4SLinus Torvalds unsigned long page; 6141da177e4SLinus Torvalds ssize_t length; 61599f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 61699f89551SEric W. Biederman 61799f89551SEric W. Biederman length = -ESRCH; 61899f89551SEric W. Biederman if (!task) 61999f89551SEric W. Biederman goto out_no_task; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 6221da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 62399f89551SEric W. Biederman 62499f89551SEric W. Biederman length = -ENOMEM; 625e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 62699f89551SEric W. Biederman goto out; 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds if (length >= 0) 6311da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 6321da177e4SLinus Torvalds free_page(page); 63399f89551SEric W. Biederman out: 63499f89551SEric W. Biederman put_task_struct(task); 63599f89551SEric W. Biederman out_no_task: 6361da177e4SLinus Torvalds return length; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds 63900977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 6401da177e4SLinus Torvalds .read = proc_info_read, 64187df8424SArnd Bergmann .llseek = generic_file_llseek, 6421da177e4SLinus Torvalds }; 6431da177e4SLinus Torvalds 644be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 645be614086SEric W. Biederman { 646be614086SEric W. Biederman struct inode *inode = m->private; 647be614086SEric W. Biederman struct pid_namespace *ns; 648be614086SEric W. Biederman struct pid *pid; 649be614086SEric W. Biederman struct task_struct *task; 650be614086SEric W. Biederman int ret; 651be614086SEric W. Biederman 652be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 653be614086SEric W. Biederman pid = proc_pid(inode); 654be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 655be614086SEric W. Biederman if (!task) 656be614086SEric W. Biederman return -ESRCH; 657be614086SEric W. Biederman 658be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 659be614086SEric W. Biederman 660be614086SEric W. Biederman put_task_struct(task); 661be614086SEric W. Biederman return ret; 662be614086SEric W. Biederman } 663be614086SEric W. Biederman 664be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 665be614086SEric W. Biederman { 666c6a34058SJovi Zhang return single_open(filp, proc_single_show, inode); 667be614086SEric W. Biederman } 668be614086SEric W. Biederman 669be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 670be614086SEric W. Biederman .open = proc_single_open, 671be614086SEric W. Biederman .read = seq_read, 672be614086SEric W. Biederman .llseek = seq_lseek, 673be614086SEric W. Biederman .release = single_release, 674be614086SEric W. Biederman }; 675be614086SEric W. Biederman 676b409e578SCong Wang static int __mem_open(struct inode *inode, struct file *file, unsigned int mode) 6771da177e4SLinus Torvalds { 678e268337dSLinus Torvalds struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 679e268337dSLinus Torvalds struct mm_struct *mm; 680e268337dSLinus Torvalds 681e268337dSLinus Torvalds if (!task) 682e268337dSLinus Torvalds return -ESRCH; 683e268337dSLinus Torvalds 684b409e578SCong Wang mm = mm_access(task, mode); 685e268337dSLinus Torvalds put_task_struct(task); 686e268337dSLinus Torvalds 687e268337dSLinus Torvalds if (IS_ERR(mm)) 688e268337dSLinus Torvalds return PTR_ERR(mm); 689e268337dSLinus Torvalds 6906d08f2c7SOleg Nesterov if (mm) { 6916d08f2c7SOleg Nesterov /* ensure this mm_struct can't be freed */ 6926d08f2c7SOleg Nesterov atomic_inc(&mm->mm_count); 6936d08f2c7SOleg Nesterov /* but do not pin its memory */ 6946d08f2c7SOleg Nesterov mmput(mm); 6956d08f2c7SOleg Nesterov } 6966d08f2c7SOleg Nesterov 697e268337dSLinus Torvalds file->private_data = mm; 698e268337dSLinus Torvalds 6991da177e4SLinus Torvalds return 0; 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds 702b409e578SCong Wang static int mem_open(struct inode *inode, struct file *file) 703b409e578SCong Wang { 704bc452b4bSDjalal Harouni int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH); 705bc452b4bSDjalal Harouni 706bc452b4bSDjalal Harouni /* OK to pass negative loff_t, we can catch out-of-range */ 707bc452b4bSDjalal Harouni file->f_mode |= FMODE_UNSIGNED_OFFSET; 708bc452b4bSDjalal Harouni 709bc452b4bSDjalal Harouni return ret; 710b409e578SCong Wang } 711b409e578SCong Wang 712572d34b9SOleg Nesterov static ssize_t mem_rw(struct file *file, char __user *buf, 713572d34b9SOleg Nesterov size_t count, loff_t *ppos, int write) 7141da177e4SLinus Torvalds { 715e268337dSLinus Torvalds struct mm_struct *mm = file->private_data; 716572d34b9SOleg Nesterov unsigned long addr = *ppos; 717572d34b9SOleg Nesterov ssize_t copied; 7181da177e4SLinus Torvalds char *page; 7191da177e4SLinus Torvalds 720e268337dSLinus Torvalds if (!mm) 721e268337dSLinus Torvalds return 0; 7221da177e4SLinus Torvalds 723e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7241da177e4SLinus Torvalds if (!page) 725e268337dSLinus Torvalds return -ENOMEM; 7261da177e4SLinus Torvalds 727f7ca54f4SFrederik Deweerdt copied = 0; 7286d08f2c7SOleg Nesterov if (!atomic_inc_not_zero(&mm->mm_users)) 7296d08f2c7SOleg Nesterov goto free; 7306d08f2c7SOleg Nesterov 7311da177e4SLinus Torvalds while (count > 0) { 732572d34b9SOleg Nesterov int this_len = min_t(int, count, PAGE_SIZE); 7331da177e4SLinus Torvalds 734572d34b9SOleg Nesterov if (write && copy_from_user(page, buf, this_len)) { 7351da177e4SLinus Torvalds copied = -EFAULT; 7361da177e4SLinus Torvalds break; 7371da177e4SLinus Torvalds } 738572d34b9SOleg Nesterov 739572d34b9SOleg Nesterov this_len = access_remote_vm(mm, addr, page, this_len, write); 740572d34b9SOleg Nesterov if (!this_len) { 7411da177e4SLinus Torvalds if (!copied) 7421da177e4SLinus Torvalds copied = -EIO; 7431da177e4SLinus Torvalds break; 7441da177e4SLinus Torvalds } 745572d34b9SOleg Nesterov 746572d34b9SOleg Nesterov if (!write && copy_to_user(buf, page, this_len)) { 747572d34b9SOleg Nesterov copied = -EFAULT; 748572d34b9SOleg Nesterov break; 7491da177e4SLinus Torvalds } 750572d34b9SOleg Nesterov 751572d34b9SOleg Nesterov buf += this_len; 752572d34b9SOleg Nesterov addr += this_len; 753572d34b9SOleg Nesterov copied += this_len; 754572d34b9SOleg Nesterov count -= this_len; 755572d34b9SOleg Nesterov } 756572d34b9SOleg Nesterov *ppos = addr; 75730cd8903SKOSAKI Motohiro 7586d08f2c7SOleg Nesterov mmput(mm); 7596d08f2c7SOleg Nesterov free: 76030cd8903SKOSAKI Motohiro free_page((unsigned long) page); 7611da177e4SLinus Torvalds return copied; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 764572d34b9SOleg Nesterov static ssize_t mem_read(struct file *file, char __user *buf, 765572d34b9SOleg Nesterov size_t count, loff_t *ppos) 766572d34b9SOleg Nesterov { 767572d34b9SOleg Nesterov return mem_rw(file, buf, count, ppos, 0); 768572d34b9SOleg Nesterov } 769572d34b9SOleg Nesterov 770572d34b9SOleg Nesterov static ssize_t mem_write(struct file *file, const char __user *buf, 771572d34b9SOleg Nesterov size_t count, loff_t *ppos) 772572d34b9SOleg Nesterov { 773572d34b9SOleg Nesterov return mem_rw(file, (char __user*)buf, count, ppos, 1); 774572d34b9SOleg Nesterov } 775572d34b9SOleg Nesterov 77685863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 7771da177e4SLinus Torvalds { 7781da177e4SLinus Torvalds switch (orig) { 7791da177e4SLinus Torvalds case 0: 7801da177e4SLinus Torvalds file->f_pos = offset; 7811da177e4SLinus Torvalds break; 7821da177e4SLinus Torvalds case 1: 7831da177e4SLinus Torvalds file->f_pos += offset; 7841da177e4SLinus Torvalds break; 7851da177e4SLinus Torvalds default: 7861da177e4SLinus Torvalds return -EINVAL; 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds force_successful_syscall_return(); 7891da177e4SLinus Torvalds return file->f_pos; 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds 792e268337dSLinus Torvalds static int mem_release(struct inode *inode, struct file *file) 793e268337dSLinus Torvalds { 794e268337dSLinus Torvalds struct mm_struct *mm = file->private_data; 79571879d3cSOleg Nesterov if (mm) 7966d08f2c7SOleg Nesterov mmdrop(mm); 797e268337dSLinus Torvalds return 0; 798e268337dSLinus Torvalds } 799e268337dSLinus Torvalds 80000977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 8011da177e4SLinus Torvalds .llseek = mem_lseek, 8021da177e4SLinus Torvalds .read = mem_read, 8031da177e4SLinus Torvalds .write = mem_write, 8041da177e4SLinus Torvalds .open = mem_open, 805e268337dSLinus Torvalds .release = mem_release, 8061da177e4SLinus Torvalds }; 8071da177e4SLinus Torvalds 808b409e578SCong Wang static int environ_open(struct inode *inode, struct file *file) 809b409e578SCong Wang { 810b409e578SCong Wang return __mem_open(inode, file, PTRACE_MODE_READ); 811b409e578SCong Wang } 812b409e578SCong Wang 813315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 814315e28c8SJames Pearson size_t count, loff_t *ppos) 815315e28c8SJames Pearson { 816315e28c8SJames Pearson char *page; 817315e28c8SJames Pearson unsigned long src = *ppos; 818b409e578SCong Wang int ret = 0; 819b409e578SCong Wang struct mm_struct *mm = file->private_data; 820315e28c8SJames Pearson 821b409e578SCong Wang if (!mm) 822b409e578SCong Wang return 0; 823315e28c8SJames Pearson 824315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 825315e28c8SJames Pearson if (!page) 826b409e578SCong Wang return -ENOMEM; 827315e28c8SJames Pearson 828d6f64b89SAl Viro ret = 0; 829b409e578SCong Wang if (!atomic_inc_not_zero(&mm->mm_users)) 830b409e578SCong Wang goto free; 831315e28c8SJames Pearson while (count > 0) { 832e8905ec2SDjalal Harouni size_t this_len, max_len; 833e8905ec2SDjalal Harouni int retval; 834e8905ec2SDjalal Harouni 835e8905ec2SDjalal Harouni if (src >= (mm->env_end - mm->env_start)) 836e8905ec2SDjalal Harouni break; 837315e28c8SJames Pearson 838315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 839315e28c8SJames Pearson 840e8905ec2SDjalal Harouni max_len = min_t(size_t, PAGE_SIZE, count); 841e8905ec2SDjalal Harouni this_len = min(max_len, this_len); 842315e28c8SJames Pearson 843b409e578SCong Wang retval = access_remote_vm(mm, (mm->env_start + src), 844315e28c8SJames Pearson page, this_len, 0); 845315e28c8SJames Pearson 846315e28c8SJames Pearson if (retval <= 0) { 847315e28c8SJames Pearson ret = retval; 848315e28c8SJames Pearson break; 849315e28c8SJames Pearson } 850315e28c8SJames Pearson 851315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 852315e28c8SJames Pearson ret = -EFAULT; 853315e28c8SJames Pearson break; 854315e28c8SJames Pearson } 855315e28c8SJames Pearson 856315e28c8SJames Pearson ret += retval; 857315e28c8SJames Pearson src += retval; 858315e28c8SJames Pearson buf += retval; 859315e28c8SJames Pearson count -= retval; 860315e28c8SJames Pearson } 861315e28c8SJames Pearson *ppos = src; 862315e28c8SJames Pearson mmput(mm); 863b409e578SCong Wang 864b409e578SCong Wang free: 865315e28c8SJames Pearson free_page((unsigned long) page); 866315e28c8SJames Pearson return ret; 867315e28c8SJames Pearson } 868315e28c8SJames Pearson 869315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 870b409e578SCong Wang .open = environ_open, 871315e28c8SJames Pearson .read = environ_read, 87287df8424SArnd Bergmann .llseek = generic_file_llseek, 873b409e578SCong Wang .release = mem_release, 874315e28c8SJames Pearson }; 875315e28c8SJames Pearson 8761da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 8771da177e4SLinus Torvalds size_t count, loff_t *ppos) 8781da177e4SLinus Torvalds { 8792fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8808578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 8811da177e4SLinus Torvalds size_t len; 88228b83c51SKOSAKI Motohiro int oom_adjust = OOM_DISABLE; 88328b83c51SKOSAKI Motohiro unsigned long flags; 8841da177e4SLinus Torvalds 88599f89551SEric W. Biederman if (!task) 88699f89551SEric W. Biederman return -ESRCH; 88728b83c51SKOSAKI Motohiro 88828b83c51SKOSAKI Motohiro if (lock_task_sighand(task, &flags)) { 88928b83c51SKOSAKI Motohiro oom_adjust = task->signal->oom_adj; 89028b83c51SKOSAKI Motohiro unlock_task_sighand(task, &flags); 89128b83c51SKOSAKI Motohiro } 89228b83c51SKOSAKI Motohiro 89399f89551SEric W. Biederman put_task_struct(task); 89499f89551SEric W. Biederman 8958578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 8960c28f287SAkinobu Mita 8970c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 9011da177e4SLinus Torvalds size_t count, loff_t *ppos) 9021da177e4SLinus Torvalds { 90399f89551SEric W. Biederman struct task_struct *task; 9045d863b89SKOSAKI Motohiro char buffer[PROC_NUMBUF]; 9050a8cb8e3SAlexey Dobriyan int oom_adjust; 90628b83c51SKOSAKI Motohiro unsigned long flags; 9075d863b89SKOSAKI Motohiro int err; 9081da177e4SLinus Torvalds 9098578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 9108578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 9118578cea7SEric W. Biederman count = sizeof(buffer) - 1; 912723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) { 913723548bfSDavid Rientjes err = -EFAULT; 914723548bfSDavid Rientjes goto out; 915723548bfSDavid Rientjes } 9165d863b89SKOSAKI Motohiro 9170a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &oom_adjust); 9185d863b89SKOSAKI Motohiro if (err) 919723548bfSDavid Rientjes goto out; 9208ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 921723548bfSDavid Rientjes oom_adjust != OOM_DISABLE) { 922723548bfSDavid Rientjes err = -EINVAL; 923723548bfSDavid Rientjes goto out; 924723548bfSDavid Rientjes } 9255d863b89SKOSAKI Motohiro 9262fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 927723548bfSDavid Rientjes if (!task) { 928723548bfSDavid Rientjes err = -ESRCH; 929723548bfSDavid Rientjes goto out; 930723548bfSDavid Rientjes } 93128b83c51SKOSAKI Motohiro 9323d5992d2SYing Han task_lock(task); 9333d5992d2SYing Han if (!task->mm) { 934723548bfSDavid Rientjes err = -EINVAL; 935723548bfSDavid Rientjes goto err_task_lock; 9363d5992d2SYing Han } 9373d5992d2SYing Han 938d19d5476SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 939d19d5476SDavid Rientjes err = -ESRCH; 940d19d5476SDavid Rientjes goto err_task_lock; 941d19d5476SDavid Rientjes } 942d19d5476SDavid Rientjes 943d19d5476SDavid Rientjes if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) { 944d19d5476SDavid Rientjes err = -EACCES; 945d19d5476SDavid Rientjes goto err_sighand; 946d19d5476SDavid Rientjes } 947d19d5476SDavid Rientjes 94851b1bd2aSDavid Rientjes /* 94951b1bd2aSDavid Rientjes * Warn that /proc/pid/oom_adj is deprecated, see 95051b1bd2aSDavid Rientjes * Documentation/feature-removal-schedule.txt. 95151b1bd2aSDavid Rientjes */ 952c2142704SLinus Torvalds printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n", 953be8f684dSDavid Rientjes current->comm, task_pid_nr(current), task_pid_nr(task), 954be8f684dSDavid Rientjes task_pid_nr(task)); 95528b83c51SKOSAKI Motohiro task->signal->oom_adj = oom_adjust; 956a63d83f4SDavid Rientjes /* 957a63d83f4SDavid Rientjes * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum 958a63d83f4SDavid Rientjes * value is always attainable. 959a63d83f4SDavid Rientjes */ 960a63d83f4SDavid Rientjes if (task->signal->oom_adj == OOM_ADJUST_MAX) 961a63d83f4SDavid Rientjes task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX; 962a63d83f4SDavid Rientjes else 963a63d83f4SDavid Rientjes task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) / 964a63d83f4SDavid Rientjes -OOM_DISABLE; 96543d2b113SKAMEZAWA Hiroyuki trace_oom_score_adj_update(task); 966723548bfSDavid Rientjes err_sighand: 96728b83c51SKOSAKI Motohiro unlock_task_sighand(task, &flags); 968d19d5476SDavid Rientjes err_task_lock: 969d19d5476SDavid Rientjes task_unlock(task); 97099f89551SEric W. Biederman put_task_struct(task); 971723548bfSDavid Rientjes out: 972723548bfSDavid Rientjes return err < 0 ? err : count; 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds 97500977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 9761da177e4SLinus Torvalds .read = oom_adjust_read, 9771da177e4SLinus Torvalds .write = oom_adjust_write, 97887df8424SArnd Bergmann .llseek = generic_file_llseek, 9791da177e4SLinus Torvalds }; 9801da177e4SLinus Torvalds 981a63d83f4SDavid Rientjes static ssize_t oom_score_adj_read(struct file *file, char __user *buf, 982a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 983a63d83f4SDavid Rientjes { 984a63d83f4SDavid Rientjes struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 985a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 986a63d83f4SDavid Rientjes int oom_score_adj = OOM_SCORE_ADJ_MIN; 987a63d83f4SDavid Rientjes unsigned long flags; 988a63d83f4SDavid Rientjes size_t len; 989a63d83f4SDavid Rientjes 990a63d83f4SDavid Rientjes if (!task) 991a63d83f4SDavid Rientjes return -ESRCH; 992a63d83f4SDavid Rientjes if (lock_task_sighand(task, &flags)) { 993a63d83f4SDavid Rientjes oom_score_adj = task->signal->oom_score_adj; 994a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 995a63d83f4SDavid Rientjes } 996a63d83f4SDavid Rientjes put_task_struct(task); 997a63d83f4SDavid Rientjes len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj); 998a63d83f4SDavid Rientjes return simple_read_from_buffer(buf, count, ppos, buffer, len); 999a63d83f4SDavid Rientjes } 1000a63d83f4SDavid Rientjes 1001a63d83f4SDavid Rientjes static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, 1002a63d83f4SDavid Rientjes size_t count, loff_t *ppos) 1003a63d83f4SDavid Rientjes { 1004a63d83f4SDavid Rientjes struct task_struct *task; 1005a63d83f4SDavid Rientjes char buffer[PROC_NUMBUF]; 1006a63d83f4SDavid Rientjes unsigned long flags; 10070a8cb8e3SAlexey Dobriyan int oom_score_adj; 1008a63d83f4SDavid Rientjes int err; 1009a63d83f4SDavid Rientjes 1010a63d83f4SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 1011a63d83f4SDavid Rientjes if (count > sizeof(buffer) - 1) 1012a63d83f4SDavid Rientjes count = sizeof(buffer) - 1; 1013723548bfSDavid Rientjes if (copy_from_user(buffer, buf, count)) { 1014723548bfSDavid Rientjes err = -EFAULT; 1015723548bfSDavid Rientjes goto out; 1016723548bfSDavid Rientjes } 1017a63d83f4SDavid Rientjes 10180a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &oom_score_adj); 1019a63d83f4SDavid Rientjes if (err) 1020723548bfSDavid Rientjes goto out; 1021a63d83f4SDavid Rientjes if (oom_score_adj < OOM_SCORE_ADJ_MIN || 1022723548bfSDavid Rientjes oom_score_adj > OOM_SCORE_ADJ_MAX) { 1023723548bfSDavid Rientjes err = -EINVAL; 1024723548bfSDavid Rientjes goto out; 1025723548bfSDavid Rientjes } 1026a63d83f4SDavid Rientjes 1027a63d83f4SDavid Rientjes task = get_proc_task(file->f_path.dentry->d_inode); 1028723548bfSDavid Rientjes if (!task) { 1029723548bfSDavid Rientjes err = -ESRCH; 1030723548bfSDavid Rientjes goto out; 1031723548bfSDavid Rientjes } 1032a63d83f4SDavid Rientjes 10333d5992d2SYing Han task_lock(task); 10343d5992d2SYing Han if (!task->mm) { 1035723548bfSDavid Rientjes err = -EINVAL; 1036723548bfSDavid Rientjes goto err_task_lock; 10373d5992d2SYing Han } 1038d19d5476SDavid Rientjes 1039d19d5476SDavid Rientjes if (!lock_task_sighand(task, &flags)) { 1040d19d5476SDavid Rientjes err = -ESRCH; 1041d19d5476SDavid Rientjes goto err_task_lock; 1042d19d5476SDavid Rientjes } 1043d19d5476SDavid Rientjes 1044dabb16f6SMandeep Singh Baines if (oom_score_adj < task->signal->oom_score_adj_min && 1045d19d5476SDavid Rientjes !capable(CAP_SYS_RESOURCE)) { 1046d19d5476SDavid Rientjes err = -EACCES; 1047d19d5476SDavid Rientjes goto err_sighand; 1048d19d5476SDavid Rientjes } 1049d19d5476SDavid Rientjes 1050a63d83f4SDavid Rientjes task->signal->oom_score_adj = oom_score_adj; 1051dabb16f6SMandeep Singh Baines if (has_capability_noaudit(current, CAP_SYS_RESOURCE)) 1052dabb16f6SMandeep Singh Baines task->signal->oom_score_adj_min = oom_score_adj; 105343d2b113SKAMEZAWA Hiroyuki trace_oom_score_adj_update(task); 1054a63d83f4SDavid Rientjes /* 1055a63d83f4SDavid Rientjes * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is 1056a63d83f4SDavid Rientjes * always attainable. 1057a63d83f4SDavid Rientjes */ 1058a63d83f4SDavid Rientjes if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) 1059a63d83f4SDavid Rientjes task->signal->oom_adj = OOM_DISABLE; 1060a63d83f4SDavid Rientjes else 1061a63d83f4SDavid Rientjes task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) / 1062a63d83f4SDavid Rientjes OOM_SCORE_ADJ_MAX; 1063723548bfSDavid Rientjes err_sighand: 1064a63d83f4SDavid Rientjes unlock_task_sighand(task, &flags); 1065d19d5476SDavid Rientjes err_task_lock: 1066d19d5476SDavid Rientjes task_unlock(task); 1067a63d83f4SDavid Rientjes put_task_struct(task); 1068723548bfSDavid Rientjes out: 1069723548bfSDavid Rientjes return err < 0 ? err : count; 1070a63d83f4SDavid Rientjes } 1071a63d83f4SDavid Rientjes 1072a63d83f4SDavid Rientjes static const struct file_operations proc_oom_score_adj_operations = { 1073a63d83f4SDavid Rientjes .read = oom_score_adj_read, 1074a63d83f4SDavid Rientjes .write = oom_score_adj_write, 10756038f373SArnd Bergmann .llseek = default_llseek, 1076a63d83f4SDavid Rientjes }; 1077a63d83f4SDavid Rientjes 10781da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10791da177e4SLinus Torvalds #define TMPBUFLEN 21 10801da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10811da177e4SLinus Torvalds size_t count, loff_t *ppos) 10821da177e4SLinus Torvalds { 10832fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 108499f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10851da177e4SLinus Torvalds ssize_t length; 10861da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10871da177e4SLinus Torvalds 108899f89551SEric W. Biederman if (!task) 108999f89551SEric W. Biederman return -ESRCH; 10901da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10910c11b942SAl Viro audit_get_loginuid(task)); 109299f89551SEric W. Biederman put_task_struct(task); 10931da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10971da177e4SLinus Torvalds size_t count, loff_t *ppos) 10981da177e4SLinus Torvalds { 10992fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 11001da177e4SLinus Torvalds char *page, *tmp; 11011da177e4SLinus Torvalds ssize_t length; 11021da177e4SLinus Torvalds uid_t loginuid; 11031da177e4SLinus Torvalds 11047dc52157SPaul E. McKenney rcu_read_lock(); 11057dc52157SPaul E. McKenney if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { 11067dc52157SPaul E. McKenney rcu_read_unlock(); 11071da177e4SLinus Torvalds return -EPERM; 11087dc52157SPaul E. McKenney } 11097dc52157SPaul E. McKenney rcu_read_unlock(); 11101da177e4SLinus Torvalds 1111e0182909SAl Viro if (count >= PAGE_SIZE) 1112e0182909SAl Viro count = PAGE_SIZE - 1; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds if (*ppos != 0) { 11151da177e4SLinus Torvalds /* No partial writes. */ 11161da177e4SLinus Torvalds return -EINVAL; 11171da177e4SLinus Torvalds } 1118e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 11191da177e4SLinus Torvalds if (!page) 11201da177e4SLinus Torvalds return -ENOMEM; 11211da177e4SLinus Torvalds length = -EFAULT; 11221da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 11231da177e4SLinus Torvalds goto out_free_page; 11241da177e4SLinus Torvalds 1125e0182909SAl Viro page[count] = '\0'; 11261da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 11271da177e4SLinus Torvalds if (tmp == page) { 11281da177e4SLinus Torvalds length = -EINVAL; 11291da177e4SLinus Torvalds goto out_free_page; 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds } 11320a300be6SEric Paris length = audit_set_loginuid(loginuid); 11331da177e4SLinus Torvalds if (likely(length == 0)) 11341da177e4SLinus Torvalds length = count; 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds out_free_page: 11371da177e4SLinus Torvalds free_page((unsigned long) page); 11381da177e4SLinus Torvalds return length; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 114100977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 11421da177e4SLinus Torvalds .read = proc_loginuid_read, 11431da177e4SLinus Torvalds .write = proc_loginuid_write, 114487df8424SArnd Bergmann .llseek = generic_file_llseek, 11451da177e4SLinus Torvalds }; 11461e0bd755SEric Paris 11471e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 11481e0bd755SEric Paris size_t count, loff_t *ppos) 11491e0bd755SEric Paris { 11501e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 11511e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 11521e0bd755SEric Paris ssize_t length; 11531e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 11541e0bd755SEric Paris 11551e0bd755SEric Paris if (!task) 11561e0bd755SEric Paris return -ESRCH; 11571e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11581e0bd755SEric Paris audit_get_sessionid(task)); 11591e0bd755SEric Paris put_task_struct(task); 11601e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11611e0bd755SEric Paris } 11621e0bd755SEric Paris 11631e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11641e0bd755SEric Paris .read = proc_sessionid_read, 116587df8424SArnd Bergmann .llseek = generic_file_llseek, 11661e0bd755SEric Paris }; 11671da177e4SLinus Torvalds #endif 11681da177e4SLinus Torvalds 1169f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1170f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1171f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1172f4f154fdSAkinobu Mita { 1173f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1174f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1175f4f154fdSAkinobu Mita size_t len; 1176f4f154fdSAkinobu Mita int make_it_fail; 1177f4f154fdSAkinobu Mita 1178f4f154fdSAkinobu Mita if (!task) 1179f4f154fdSAkinobu Mita return -ESRCH; 1180f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1181f4f154fdSAkinobu Mita put_task_struct(task); 1182f4f154fdSAkinobu Mita 1183f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11840c28f287SAkinobu Mita 11850c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1186f4f154fdSAkinobu Mita } 1187f4f154fdSAkinobu Mita 1188f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1189f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1190f4f154fdSAkinobu Mita { 1191f4f154fdSAkinobu Mita struct task_struct *task; 1192f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1193f4f154fdSAkinobu Mita int make_it_fail; 1194f4f154fdSAkinobu Mita 1195f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1196f4f154fdSAkinobu Mita return -EPERM; 1197f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1198f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1199f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1200f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1201f4f154fdSAkinobu Mita return -EFAULT; 1202cba8aafeSVincent Li make_it_fail = simple_strtol(strstrip(buffer), &end, 0); 1203cba8aafeSVincent Li if (*end) 1204cba8aafeSVincent Li return -EINVAL; 1205f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1206f4f154fdSAkinobu Mita if (!task) 1207f4f154fdSAkinobu Mita return -ESRCH; 1208f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1209f4f154fdSAkinobu Mita put_task_struct(task); 1210cba8aafeSVincent Li 1211cba8aafeSVincent Li return count; 1212f4f154fdSAkinobu Mita } 1213f4f154fdSAkinobu Mita 121400977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1215f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1216f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 121787df8424SArnd Bergmann .llseek = generic_file_llseek, 1218f4f154fdSAkinobu Mita }; 1219f4f154fdSAkinobu Mita #endif 1220f4f154fdSAkinobu Mita 12219745512cSArjan van de Ven 122243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 122343ae34cbSIngo Molnar /* 122443ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 122543ae34cbSIngo Molnar */ 122643ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 122743ae34cbSIngo Molnar { 122843ae34cbSIngo Molnar struct inode *inode = m->private; 122943ae34cbSIngo Molnar struct task_struct *p; 123043ae34cbSIngo Molnar 123143ae34cbSIngo Molnar p = get_proc_task(inode); 123243ae34cbSIngo Molnar if (!p) 123343ae34cbSIngo Molnar return -ESRCH; 123443ae34cbSIngo Molnar proc_sched_show_task(p, m); 123543ae34cbSIngo Molnar 123643ae34cbSIngo Molnar put_task_struct(p); 123743ae34cbSIngo Molnar 123843ae34cbSIngo Molnar return 0; 123943ae34cbSIngo Molnar } 124043ae34cbSIngo Molnar 124143ae34cbSIngo Molnar static ssize_t 124243ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 124343ae34cbSIngo Molnar size_t count, loff_t *offset) 124443ae34cbSIngo Molnar { 124543ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 124643ae34cbSIngo Molnar struct task_struct *p; 124743ae34cbSIngo Molnar 124843ae34cbSIngo Molnar p = get_proc_task(inode); 124943ae34cbSIngo Molnar if (!p) 125043ae34cbSIngo Molnar return -ESRCH; 125143ae34cbSIngo Molnar proc_sched_set_task(p); 125243ae34cbSIngo Molnar 125343ae34cbSIngo Molnar put_task_struct(p); 125443ae34cbSIngo Molnar 125543ae34cbSIngo Molnar return count; 125643ae34cbSIngo Molnar } 125743ae34cbSIngo Molnar 125843ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 125943ae34cbSIngo Molnar { 1260c6a34058SJovi Zhang return single_open(filp, sched_show, inode); 126143ae34cbSIngo Molnar } 126243ae34cbSIngo Molnar 126343ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 126443ae34cbSIngo Molnar .open = sched_open, 126543ae34cbSIngo Molnar .read = seq_read, 126643ae34cbSIngo Molnar .write = sched_write, 126743ae34cbSIngo Molnar .llseek = seq_lseek, 12685ea473a1SAlexey Dobriyan .release = single_release, 126943ae34cbSIngo Molnar }; 127043ae34cbSIngo Molnar 127143ae34cbSIngo Molnar #endif 127243ae34cbSIngo Molnar 12735091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 12745091faa4SMike Galbraith /* 12755091faa4SMike Galbraith * Print out autogroup related information: 12765091faa4SMike Galbraith */ 12775091faa4SMike Galbraith static int sched_autogroup_show(struct seq_file *m, void *v) 12785091faa4SMike Galbraith { 12795091faa4SMike Galbraith struct inode *inode = m->private; 12805091faa4SMike Galbraith struct task_struct *p; 12815091faa4SMike Galbraith 12825091faa4SMike Galbraith p = get_proc_task(inode); 12835091faa4SMike Galbraith if (!p) 12845091faa4SMike Galbraith return -ESRCH; 12855091faa4SMike Galbraith proc_sched_autogroup_show_task(p, m); 12865091faa4SMike Galbraith 12875091faa4SMike Galbraith put_task_struct(p); 12885091faa4SMike Galbraith 12895091faa4SMike Galbraith return 0; 12905091faa4SMike Galbraith } 12915091faa4SMike Galbraith 12925091faa4SMike Galbraith static ssize_t 12935091faa4SMike Galbraith sched_autogroup_write(struct file *file, const char __user *buf, 12945091faa4SMike Galbraith size_t count, loff_t *offset) 12955091faa4SMike Galbraith { 12965091faa4SMike Galbraith struct inode *inode = file->f_path.dentry->d_inode; 12975091faa4SMike Galbraith struct task_struct *p; 12985091faa4SMike Galbraith char buffer[PROC_NUMBUF]; 12990a8cb8e3SAlexey Dobriyan int nice; 13005091faa4SMike Galbraith int err; 13015091faa4SMike Galbraith 13025091faa4SMike Galbraith memset(buffer, 0, sizeof(buffer)); 13035091faa4SMike Galbraith if (count > sizeof(buffer) - 1) 13045091faa4SMike Galbraith count = sizeof(buffer) - 1; 13055091faa4SMike Galbraith if (copy_from_user(buffer, buf, count)) 13065091faa4SMike Galbraith return -EFAULT; 13075091faa4SMike Galbraith 13080a8cb8e3SAlexey Dobriyan err = kstrtoint(strstrip(buffer), 0, &nice); 13090a8cb8e3SAlexey Dobriyan if (err < 0) 13100a8cb8e3SAlexey Dobriyan return err; 13115091faa4SMike Galbraith 13125091faa4SMike Galbraith p = get_proc_task(inode); 13135091faa4SMike Galbraith if (!p) 13145091faa4SMike Galbraith return -ESRCH; 13155091faa4SMike Galbraith 13162e5b5b3aSHiroshi Shimamoto err = proc_sched_autogroup_set_nice(p, nice); 13175091faa4SMike Galbraith if (err) 13185091faa4SMike Galbraith count = err; 13195091faa4SMike Galbraith 13205091faa4SMike Galbraith put_task_struct(p); 13215091faa4SMike Galbraith 13225091faa4SMike Galbraith return count; 13235091faa4SMike Galbraith } 13245091faa4SMike Galbraith 13255091faa4SMike Galbraith static int sched_autogroup_open(struct inode *inode, struct file *filp) 13265091faa4SMike Galbraith { 13275091faa4SMike Galbraith int ret; 13285091faa4SMike Galbraith 13295091faa4SMike Galbraith ret = single_open(filp, sched_autogroup_show, NULL); 13305091faa4SMike Galbraith if (!ret) { 13315091faa4SMike Galbraith struct seq_file *m = filp->private_data; 13325091faa4SMike Galbraith 13335091faa4SMike Galbraith m->private = inode; 13345091faa4SMike Galbraith } 13355091faa4SMike Galbraith return ret; 13365091faa4SMike Galbraith } 13375091faa4SMike Galbraith 13385091faa4SMike Galbraith static const struct file_operations proc_pid_sched_autogroup_operations = { 13395091faa4SMike Galbraith .open = sched_autogroup_open, 13405091faa4SMike Galbraith .read = seq_read, 13415091faa4SMike Galbraith .write = sched_autogroup_write, 13425091faa4SMike Galbraith .llseek = seq_lseek, 13435091faa4SMike Galbraith .release = single_release, 13445091faa4SMike Galbraith }; 13455091faa4SMike Galbraith 13465091faa4SMike Galbraith #endif /* CONFIG_SCHED_AUTOGROUP */ 13475091faa4SMike Galbraith 13484614a696Sjohn stultz static ssize_t comm_write(struct file *file, const char __user *buf, 13494614a696Sjohn stultz size_t count, loff_t *offset) 13504614a696Sjohn stultz { 13514614a696Sjohn stultz struct inode *inode = file->f_path.dentry->d_inode; 13524614a696Sjohn stultz struct task_struct *p; 13534614a696Sjohn stultz char buffer[TASK_COMM_LEN]; 13544614a696Sjohn stultz 13554614a696Sjohn stultz memset(buffer, 0, sizeof(buffer)); 13564614a696Sjohn stultz if (count > sizeof(buffer) - 1) 13574614a696Sjohn stultz count = sizeof(buffer) - 1; 13584614a696Sjohn stultz if (copy_from_user(buffer, buf, count)) 13594614a696Sjohn stultz return -EFAULT; 13604614a696Sjohn stultz 13614614a696Sjohn stultz p = get_proc_task(inode); 13624614a696Sjohn stultz if (!p) 13634614a696Sjohn stultz return -ESRCH; 13644614a696Sjohn stultz 13654614a696Sjohn stultz if (same_thread_group(current, p)) 13664614a696Sjohn stultz set_task_comm(p, buffer); 13674614a696Sjohn stultz else 13684614a696Sjohn stultz count = -EINVAL; 13694614a696Sjohn stultz 13704614a696Sjohn stultz put_task_struct(p); 13714614a696Sjohn stultz 13724614a696Sjohn stultz return count; 13734614a696Sjohn stultz } 13744614a696Sjohn stultz 13754614a696Sjohn stultz static int comm_show(struct seq_file *m, void *v) 13764614a696Sjohn stultz { 13774614a696Sjohn stultz struct inode *inode = m->private; 13784614a696Sjohn stultz struct task_struct *p; 13794614a696Sjohn stultz 13804614a696Sjohn stultz p = get_proc_task(inode); 13814614a696Sjohn stultz if (!p) 13824614a696Sjohn stultz return -ESRCH; 13834614a696Sjohn stultz 13844614a696Sjohn stultz task_lock(p); 13854614a696Sjohn stultz seq_printf(m, "%s\n", p->comm); 13864614a696Sjohn stultz task_unlock(p); 13874614a696Sjohn stultz 13884614a696Sjohn stultz put_task_struct(p); 13894614a696Sjohn stultz 13904614a696Sjohn stultz return 0; 13914614a696Sjohn stultz } 13924614a696Sjohn stultz 13934614a696Sjohn stultz static int comm_open(struct inode *inode, struct file *filp) 13944614a696Sjohn stultz { 1395c6a34058SJovi Zhang return single_open(filp, comm_show, inode); 13964614a696Sjohn stultz } 13974614a696Sjohn stultz 13984614a696Sjohn stultz static const struct file_operations proc_pid_set_comm_operations = { 13994614a696Sjohn stultz .open = comm_open, 14004614a696Sjohn stultz .read = seq_read, 14014614a696Sjohn stultz .write = comm_write, 14024614a696Sjohn stultz .llseek = seq_lseek, 14034614a696Sjohn stultz .release = single_release, 14044614a696Sjohn stultz }; 14054614a696Sjohn stultz 14067773fbc5SCyrill Gorcunov static int proc_exe_link(struct dentry *dentry, struct path *exe_path) 1407925d1c40SMatt Helsley { 1408925d1c40SMatt Helsley struct task_struct *task; 1409925d1c40SMatt Helsley struct mm_struct *mm; 1410925d1c40SMatt Helsley struct file *exe_file; 1411925d1c40SMatt Helsley 14127773fbc5SCyrill Gorcunov task = get_proc_task(dentry->d_inode); 1413925d1c40SMatt Helsley if (!task) 1414925d1c40SMatt Helsley return -ENOENT; 1415925d1c40SMatt Helsley mm = get_task_mm(task); 1416925d1c40SMatt Helsley put_task_struct(task); 1417925d1c40SMatt Helsley if (!mm) 1418925d1c40SMatt Helsley return -ENOENT; 1419925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1420925d1c40SMatt Helsley mmput(mm); 1421925d1c40SMatt Helsley if (exe_file) { 1422925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1423925d1c40SMatt Helsley path_get(&exe_file->f_path); 1424925d1c40SMatt Helsley fput(exe_file); 1425925d1c40SMatt Helsley return 0; 1426925d1c40SMatt Helsley } else 1427925d1c40SMatt Helsley return -ENOENT; 1428925d1c40SMatt Helsley } 1429925d1c40SMatt Helsley 1430008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 14311da177e4SLinus Torvalds { 14321da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 1433408ef013SChristoph Hellwig struct path path; 14341da177e4SLinus Torvalds int error = -EACCES; 14351da177e4SLinus Torvalds 1436778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1437778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 14381da177e4SLinus Torvalds goto out; 14391da177e4SLinus Torvalds 1440408ef013SChristoph Hellwig error = PROC_I(inode)->op.proc_get_link(dentry, &path); 1441408ef013SChristoph Hellwig if (error) 1442408ef013SChristoph Hellwig goto out; 1443408ef013SChristoph Hellwig 1444b5fb63c1SChristoph Hellwig nd_jump_link(nd, &path); 1445408ef013SChristoph Hellwig return NULL; 14461da177e4SLinus Torvalds out: 1447008b150aSAl Viro return ERR_PTR(error); 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 14503dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 14511da177e4SLinus Torvalds { 1452e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 14533dcd25f3SJan Blunck char *pathname; 14541da177e4SLinus Torvalds int len; 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds if (!tmp) 14571da177e4SLinus Torvalds return -ENOMEM; 14581da177e4SLinus Torvalds 14597b2a69baSEric W. Biederman pathname = d_path(path, tmp, PAGE_SIZE); 14603dcd25f3SJan Blunck len = PTR_ERR(pathname); 14613dcd25f3SJan Blunck if (IS_ERR(pathname)) 14621da177e4SLinus Torvalds goto out; 14633dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds if (len > buflen) 14661da177e4SLinus Torvalds len = buflen; 14673dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 14681da177e4SLinus Torvalds len = -EFAULT; 14691da177e4SLinus Torvalds out: 14701da177e4SLinus Torvalds free_page((unsigned long)tmp); 14711da177e4SLinus Torvalds return len; 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 14751da177e4SLinus Torvalds { 14761da177e4SLinus Torvalds int error = -EACCES; 14771da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14783dcd25f3SJan Blunck struct path path; 14791da177e4SLinus Torvalds 1480778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1481778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 14821da177e4SLinus Torvalds goto out; 14831da177e4SLinus Torvalds 14847773fbc5SCyrill Gorcunov error = PROC_I(inode)->op.proc_get_link(dentry, &path); 14851da177e4SLinus Torvalds if (error) 14861da177e4SLinus Torvalds goto out; 14871da177e4SLinus Torvalds 14883dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 14893dcd25f3SJan Blunck path_put(&path); 14901da177e4SLinus Torvalds out: 14911da177e4SLinus Torvalds return error; 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds 1494*faf60af1SCyrill Gorcunov const struct inode_operations proc_pid_link_inode_operations = { 14951da177e4SLinus Torvalds .readlink = proc_pid_readlink, 14966d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 14976d76fa58SLinus Torvalds .setattr = proc_setattr, 14981da177e4SLinus Torvalds }; 14991da177e4SLinus Torvalds 150028a6d671SEric W. Biederman 150128a6d671SEric W. Biederman /* building an inode */ 150228a6d671SEric W. Biederman 15036b4e306aSEric W. Biederman struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 150428a6d671SEric W. Biederman { 150528a6d671SEric W. Biederman struct inode * inode; 150628a6d671SEric W. Biederman struct proc_inode *ei; 1507c69e8d9cSDavid Howells const struct cred *cred; 150828a6d671SEric W. Biederman 150928a6d671SEric W. Biederman /* We need a new inode */ 151028a6d671SEric W. Biederman 151128a6d671SEric W. Biederman inode = new_inode(sb); 151228a6d671SEric W. Biederman if (!inode) 151328a6d671SEric W. Biederman goto out; 151428a6d671SEric W. Biederman 151528a6d671SEric W. Biederman /* Common stuff */ 151628a6d671SEric W. Biederman ei = PROC_I(inode); 151785fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 151828a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 151928a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 152028a6d671SEric W. Biederman 152128a6d671SEric W. Biederman /* 152228a6d671SEric W. Biederman * grab the reference to task. 152328a6d671SEric W. Biederman */ 15241a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 152528a6d671SEric W. Biederman if (!ei->pid) 152628a6d671SEric W. Biederman goto out_unlock; 152728a6d671SEric W. Biederman 152828a6d671SEric W. Biederman if (task_dumpable(task)) { 1529c69e8d9cSDavid Howells rcu_read_lock(); 1530c69e8d9cSDavid Howells cred = __task_cred(task); 1531c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1532c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1533c69e8d9cSDavid Howells rcu_read_unlock(); 153428a6d671SEric W. Biederman } 153528a6d671SEric W. Biederman security_task_to_inode(task, inode); 153628a6d671SEric W. Biederman 153728a6d671SEric W. Biederman out: 153828a6d671SEric W. Biederman return inode; 153928a6d671SEric W. Biederman 154028a6d671SEric W. Biederman out_unlock: 154128a6d671SEric W. Biederman iput(inode); 154228a6d671SEric W. Biederman return NULL; 154328a6d671SEric W. Biederman } 154428a6d671SEric W. Biederman 15456b4e306aSEric W. Biederman int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 154628a6d671SEric W. Biederman { 154728a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 154828a6d671SEric W. Biederman struct task_struct *task; 1549c69e8d9cSDavid Howells const struct cred *cred; 15500499680aSVasiliy Kulikov struct pid_namespace *pid = dentry->d_sb->s_fs_info; 1551c69e8d9cSDavid Howells 155228a6d671SEric W. Biederman generic_fillattr(inode, stat); 155328a6d671SEric W. Biederman 155428a6d671SEric W. Biederman rcu_read_lock(); 1555dcb0f222SEric W. Biederman stat->uid = GLOBAL_ROOT_UID; 1556dcb0f222SEric W. Biederman stat->gid = GLOBAL_ROOT_GID; 155728a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 155828a6d671SEric W. Biederman if (task) { 15590499680aSVasiliy Kulikov if (!has_pid_permissions(pid, task, 2)) { 15600499680aSVasiliy Kulikov rcu_read_unlock(); 15610499680aSVasiliy Kulikov /* 15620499680aSVasiliy Kulikov * This doesn't prevent learning whether PID exists, 15630499680aSVasiliy Kulikov * it only makes getattr() consistent with readdir(). 15640499680aSVasiliy Kulikov */ 15650499680aSVasiliy Kulikov return -ENOENT; 15660499680aSVasiliy Kulikov } 156728a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 156828a6d671SEric W. Biederman task_dumpable(task)) { 1569c69e8d9cSDavid Howells cred = __task_cred(task); 1570c69e8d9cSDavid Howells stat->uid = cred->euid; 1571c69e8d9cSDavid Howells stat->gid = cred->egid; 157228a6d671SEric W. Biederman } 157328a6d671SEric W. Biederman } 157428a6d671SEric W. Biederman rcu_read_unlock(); 157528a6d671SEric W. Biederman return 0; 157628a6d671SEric W. Biederman } 157728a6d671SEric W. Biederman 157828a6d671SEric W. Biederman /* dentry stuff */ 157928a6d671SEric W. Biederman 158028a6d671SEric W. Biederman /* 158128a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 158228a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 158328a6d671SEric W. Biederman * due to the way we treat inodes. 158428a6d671SEric W. Biederman * 158528a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 158628a6d671SEric W. Biederman * performed a setuid(), etc. 158728a6d671SEric W. Biederman * 158828a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 158928a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 159028a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 159128a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 159228a6d671SEric W. Biederman * made this apply to all per process world readable and executable 159328a6d671SEric W. Biederman * directories. 159428a6d671SEric W. Biederman */ 15950b728e19SAl Viro int pid_revalidate(struct dentry *dentry, unsigned int flags) 159628a6d671SEric W. Biederman { 159734286d66SNick Piggin struct inode *inode; 159834286d66SNick Piggin struct task_struct *task; 1599c69e8d9cSDavid Howells const struct cred *cred; 1600c69e8d9cSDavid Howells 16010b728e19SAl Viro if (flags & LOOKUP_RCU) 160234286d66SNick Piggin return -ECHILD; 160334286d66SNick Piggin 160434286d66SNick Piggin inode = dentry->d_inode; 160534286d66SNick Piggin task = get_proc_task(inode); 160634286d66SNick Piggin 160728a6d671SEric W. Biederman if (task) { 160828a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 160928a6d671SEric W. Biederman task_dumpable(task)) { 1610c69e8d9cSDavid Howells rcu_read_lock(); 1611c69e8d9cSDavid Howells cred = __task_cred(task); 1612c69e8d9cSDavid Howells inode->i_uid = cred->euid; 1613c69e8d9cSDavid Howells inode->i_gid = cred->egid; 1614c69e8d9cSDavid Howells rcu_read_unlock(); 161528a6d671SEric W. Biederman } else { 1616dcb0f222SEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1617dcb0f222SEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 161828a6d671SEric W. Biederman } 161928a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 162028a6d671SEric W. Biederman security_task_to_inode(task, inode); 162128a6d671SEric W. Biederman put_task_struct(task); 162228a6d671SEric W. Biederman return 1; 162328a6d671SEric W. Biederman } 162428a6d671SEric W. Biederman d_drop(dentry); 162528a6d671SEric W. Biederman return 0; 162628a6d671SEric W. Biederman } 162728a6d671SEric W. Biederman 16286b4e306aSEric W. Biederman const struct dentry_operations pid_dentry_operations = 162928a6d671SEric W. Biederman { 163028a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 163128a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 163228a6d671SEric W. Biederman }; 163328a6d671SEric W. Biederman 163428a6d671SEric W. Biederman /* Lookups */ 163528a6d671SEric W. Biederman 16361c0d04c9SEric W. Biederman /* 16371c0d04c9SEric W. Biederman * Fill a directory entry. 16381c0d04c9SEric W. Biederman * 16391c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 16401c0d04c9SEric W. Biederman * file type from dcache entry. 16411c0d04c9SEric W. Biederman * 16421c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 16431c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 16441c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 16451c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 16461c0d04c9SEric W. Biederman * by stat. 16471c0d04c9SEric W. Biederman */ 16486b4e306aSEric W. Biederman int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 16496b4e306aSEric W. Biederman const char *name, int len, 1650c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 165161a28784SEric W. Biederman { 16522fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 165361a28784SEric W. Biederman struct inode *inode; 165461a28784SEric W. Biederman struct qstr qname; 165561a28784SEric W. Biederman ino_t ino = 0; 165661a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 165761a28784SEric W. Biederman 165861a28784SEric W. Biederman qname.name = name; 165961a28784SEric W. Biederman qname.len = len; 166061a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 166161a28784SEric W. Biederman 166261a28784SEric W. Biederman child = d_lookup(dir, &qname); 166361a28784SEric W. Biederman if (!child) { 166461a28784SEric W. Biederman struct dentry *new; 166561a28784SEric W. Biederman new = d_alloc(dir, &qname); 166661a28784SEric W. Biederman if (new) { 166761a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 166861a28784SEric W. Biederman if (child) 166961a28784SEric W. Biederman dput(new); 167061a28784SEric W. Biederman else 167161a28784SEric W. Biederman child = new; 167261a28784SEric W. Biederman } 167361a28784SEric W. Biederman } 167461a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 167561a28784SEric W. Biederman goto end_instantiate; 167661a28784SEric W. Biederman inode = child->d_inode; 167761a28784SEric W. Biederman if (inode) { 167861a28784SEric W. Biederman ino = inode->i_ino; 167961a28784SEric W. Biederman type = inode->i_mode >> 12; 168061a28784SEric W. Biederman } 168161a28784SEric W. Biederman dput(child); 168261a28784SEric W. Biederman end_instantiate: 168361a28784SEric W. Biederman if (!ino) 168461a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 168561a28784SEric W. Biederman if (!ino) 168661a28784SEric W. Biederman ino = 1; 168761a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 168861a28784SEric W. Biederman } 168961a28784SEric W. Biederman 1690640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 1691640708a2SPavel Emelyanov 1692640708a2SPavel Emelyanov /* 1693640708a2SPavel Emelyanov * dname_to_vma_addr - maps a dentry name into two unsigned longs 1694640708a2SPavel Emelyanov * which represent vma start and end addresses. 1695640708a2SPavel Emelyanov */ 1696640708a2SPavel Emelyanov static int dname_to_vma_addr(struct dentry *dentry, 1697640708a2SPavel Emelyanov unsigned long *start, unsigned long *end) 1698640708a2SPavel Emelyanov { 1699640708a2SPavel Emelyanov if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2) 1700640708a2SPavel Emelyanov return -EINVAL; 1701640708a2SPavel Emelyanov 1702640708a2SPavel Emelyanov return 0; 1703640708a2SPavel Emelyanov } 1704640708a2SPavel Emelyanov 17050b728e19SAl Viro static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags) 1706640708a2SPavel Emelyanov { 1707640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1708640708a2SPavel Emelyanov bool exact_vma_exists = false; 1709640708a2SPavel Emelyanov struct mm_struct *mm = NULL; 1710640708a2SPavel Emelyanov struct task_struct *task; 1711640708a2SPavel Emelyanov const struct cred *cred; 1712640708a2SPavel Emelyanov struct inode *inode; 1713640708a2SPavel Emelyanov int status = 0; 1714640708a2SPavel Emelyanov 17150b728e19SAl Viro if (flags & LOOKUP_RCU) 1716640708a2SPavel Emelyanov return -ECHILD; 1717640708a2SPavel Emelyanov 1718640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) { 1719640708a2SPavel Emelyanov status = -EACCES; 1720640708a2SPavel Emelyanov goto out_notask; 1721640708a2SPavel Emelyanov } 1722640708a2SPavel Emelyanov 1723640708a2SPavel Emelyanov inode = dentry->d_inode; 1724640708a2SPavel Emelyanov task = get_proc_task(inode); 1725640708a2SPavel Emelyanov if (!task) 1726640708a2SPavel Emelyanov goto out_notask; 1727640708a2SPavel Emelyanov 17282344bec7SCong Wang mm = mm_access(task, PTRACE_MODE_READ); 17292344bec7SCong Wang if (IS_ERR_OR_NULL(mm)) 1730640708a2SPavel Emelyanov goto out; 1731640708a2SPavel Emelyanov 1732640708a2SPavel Emelyanov if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) { 1733640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1734640708a2SPavel Emelyanov exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end); 1735640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1736640708a2SPavel Emelyanov } 1737640708a2SPavel Emelyanov 1738640708a2SPavel Emelyanov mmput(mm); 1739640708a2SPavel Emelyanov 1740640708a2SPavel Emelyanov if (exact_vma_exists) { 1741640708a2SPavel Emelyanov if (task_dumpable(task)) { 1742640708a2SPavel Emelyanov rcu_read_lock(); 1743640708a2SPavel Emelyanov cred = __task_cred(task); 1744640708a2SPavel Emelyanov inode->i_uid = cred->euid; 1745640708a2SPavel Emelyanov inode->i_gid = cred->egid; 1746640708a2SPavel Emelyanov rcu_read_unlock(); 1747640708a2SPavel Emelyanov } else { 1748dcb0f222SEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID; 1749dcb0f222SEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID; 1750640708a2SPavel Emelyanov } 1751640708a2SPavel Emelyanov security_task_to_inode(task, inode); 1752640708a2SPavel Emelyanov status = 1; 1753640708a2SPavel Emelyanov } 1754640708a2SPavel Emelyanov 1755640708a2SPavel Emelyanov out: 1756640708a2SPavel Emelyanov put_task_struct(task); 1757640708a2SPavel Emelyanov 1758640708a2SPavel Emelyanov out_notask: 1759640708a2SPavel Emelyanov if (status <= 0) 1760640708a2SPavel Emelyanov d_drop(dentry); 1761640708a2SPavel Emelyanov 1762640708a2SPavel Emelyanov return status; 1763640708a2SPavel Emelyanov } 1764640708a2SPavel Emelyanov 1765640708a2SPavel Emelyanov static const struct dentry_operations tid_map_files_dentry_operations = { 1766640708a2SPavel Emelyanov .d_revalidate = map_files_d_revalidate, 1767640708a2SPavel Emelyanov .d_delete = pid_delete_dentry, 1768640708a2SPavel Emelyanov }; 1769640708a2SPavel Emelyanov 1770640708a2SPavel Emelyanov static int proc_map_files_get_link(struct dentry *dentry, struct path *path) 1771640708a2SPavel Emelyanov { 1772640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1773640708a2SPavel Emelyanov struct vm_area_struct *vma; 1774640708a2SPavel Emelyanov struct task_struct *task; 1775640708a2SPavel Emelyanov struct mm_struct *mm; 1776640708a2SPavel Emelyanov int rc; 1777640708a2SPavel Emelyanov 1778640708a2SPavel Emelyanov rc = -ENOENT; 1779640708a2SPavel Emelyanov task = get_proc_task(dentry->d_inode); 1780640708a2SPavel Emelyanov if (!task) 1781640708a2SPavel Emelyanov goto out; 1782640708a2SPavel Emelyanov 1783640708a2SPavel Emelyanov mm = get_task_mm(task); 1784640708a2SPavel Emelyanov put_task_struct(task); 1785640708a2SPavel Emelyanov if (!mm) 1786640708a2SPavel Emelyanov goto out; 1787640708a2SPavel Emelyanov 1788640708a2SPavel Emelyanov rc = dname_to_vma_addr(dentry, &vm_start, &vm_end); 1789640708a2SPavel Emelyanov if (rc) 1790640708a2SPavel Emelyanov goto out_mmput; 1791640708a2SPavel Emelyanov 1792640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1793640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end); 1794640708a2SPavel Emelyanov if (vma && vma->vm_file) { 1795640708a2SPavel Emelyanov *path = vma->vm_file->f_path; 1796640708a2SPavel Emelyanov path_get(path); 1797640708a2SPavel Emelyanov rc = 0; 1798640708a2SPavel Emelyanov } 1799640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1800640708a2SPavel Emelyanov 1801640708a2SPavel Emelyanov out_mmput: 1802640708a2SPavel Emelyanov mmput(mm); 1803640708a2SPavel Emelyanov out: 1804640708a2SPavel Emelyanov return rc; 1805640708a2SPavel Emelyanov } 1806640708a2SPavel Emelyanov 1807640708a2SPavel Emelyanov struct map_files_info { 1808640708a2SPavel Emelyanov struct file *file; 1809640708a2SPavel Emelyanov unsigned long len; 1810640708a2SPavel Emelyanov unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */ 1811640708a2SPavel Emelyanov }; 1812640708a2SPavel Emelyanov 1813640708a2SPavel Emelyanov static struct dentry * 1814640708a2SPavel Emelyanov proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, 1815640708a2SPavel Emelyanov struct task_struct *task, const void *ptr) 1816640708a2SPavel Emelyanov { 1817640708a2SPavel Emelyanov const struct file *file = ptr; 1818640708a2SPavel Emelyanov struct proc_inode *ei; 1819640708a2SPavel Emelyanov struct inode *inode; 1820640708a2SPavel Emelyanov 1821640708a2SPavel Emelyanov if (!file) 1822640708a2SPavel Emelyanov return ERR_PTR(-ENOENT); 1823640708a2SPavel Emelyanov 1824640708a2SPavel Emelyanov inode = proc_pid_make_inode(dir->i_sb, task); 1825640708a2SPavel Emelyanov if (!inode) 1826640708a2SPavel Emelyanov return ERR_PTR(-ENOENT); 1827640708a2SPavel Emelyanov 1828640708a2SPavel Emelyanov ei = PROC_I(inode); 1829640708a2SPavel Emelyanov ei->op.proc_get_link = proc_map_files_get_link; 1830640708a2SPavel Emelyanov 1831640708a2SPavel Emelyanov inode->i_op = &proc_pid_link_inode_operations; 1832640708a2SPavel Emelyanov inode->i_size = 64; 1833640708a2SPavel Emelyanov inode->i_mode = S_IFLNK; 1834640708a2SPavel Emelyanov 1835640708a2SPavel Emelyanov if (file->f_mode & FMODE_READ) 1836640708a2SPavel Emelyanov inode->i_mode |= S_IRUSR; 1837640708a2SPavel Emelyanov if (file->f_mode & FMODE_WRITE) 1838640708a2SPavel Emelyanov inode->i_mode |= S_IWUSR; 1839640708a2SPavel Emelyanov 1840640708a2SPavel Emelyanov d_set_d_op(dentry, &tid_map_files_dentry_operations); 1841640708a2SPavel Emelyanov d_add(dentry, inode); 1842640708a2SPavel Emelyanov 1843640708a2SPavel Emelyanov return NULL; 1844640708a2SPavel Emelyanov } 1845640708a2SPavel Emelyanov 1846640708a2SPavel Emelyanov static struct dentry *proc_map_files_lookup(struct inode *dir, 184700cd8dd3SAl Viro struct dentry *dentry, unsigned int flags) 1848640708a2SPavel Emelyanov { 1849640708a2SPavel Emelyanov unsigned long vm_start, vm_end; 1850640708a2SPavel Emelyanov struct vm_area_struct *vma; 1851640708a2SPavel Emelyanov struct task_struct *task; 1852640708a2SPavel Emelyanov struct dentry *result; 1853640708a2SPavel Emelyanov struct mm_struct *mm; 1854640708a2SPavel Emelyanov 1855640708a2SPavel Emelyanov result = ERR_PTR(-EACCES); 1856640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) 1857640708a2SPavel Emelyanov goto out; 1858640708a2SPavel Emelyanov 1859640708a2SPavel Emelyanov result = ERR_PTR(-ENOENT); 1860640708a2SPavel Emelyanov task = get_proc_task(dir); 1861640708a2SPavel Emelyanov if (!task) 1862640708a2SPavel Emelyanov goto out; 1863640708a2SPavel Emelyanov 1864640708a2SPavel Emelyanov result = ERR_PTR(-EACCES); 1865eb94cd96SCyrill Gorcunov if (!ptrace_may_access(task, PTRACE_MODE_READ)) 1866640708a2SPavel Emelyanov goto out_put_task; 1867640708a2SPavel Emelyanov 1868640708a2SPavel Emelyanov result = ERR_PTR(-ENOENT); 1869640708a2SPavel Emelyanov if (dname_to_vma_addr(dentry, &vm_start, &vm_end)) 1870eb94cd96SCyrill Gorcunov goto out_put_task; 1871640708a2SPavel Emelyanov 1872640708a2SPavel Emelyanov mm = get_task_mm(task); 1873640708a2SPavel Emelyanov if (!mm) 1874eb94cd96SCyrill Gorcunov goto out_put_task; 1875640708a2SPavel Emelyanov 1876640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1877640708a2SPavel Emelyanov vma = find_exact_vma(mm, vm_start, vm_end); 1878640708a2SPavel Emelyanov if (!vma) 1879640708a2SPavel Emelyanov goto out_no_vma; 1880640708a2SPavel Emelyanov 1881640708a2SPavel Emelyanov result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file); 1882640708a2SPavel Emelyanov 1883640708a2SPavel Emelyanov out_no_vma: 1884640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1885640708a2SPavel Emelyanov mmput(mm); 1886640708a2SPavel Emelyanov out_put_task: 1887640708a2SPavel Emelyanov put_task_struct(task); 1888640708a2SPavel Emelyanov out: 1889640708a2SPavel Emelyanov return result; 1890640708a2SPavel Emelyanov } 1891640708a2SPavel Emelyanov 1892640708a2SPavel Emelyanov static const struct inode_operations proc_map_files_inode_operations = { 1893640708a2SPavel Emelyanov .lookup = proc_map_files_lookup, 1894640708a2SPavel Emelyanov .permission = proc_fd_permission, 1895640708a2SPavel Emelyanov .setattr = proc_setattr, 1896640708a2SPavel Emelyanov }; 1897640708a2SPavel Emelyanov 1898640708a2SPavel Emelyanov static int 1899640708a2SPavel Emelyanov proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir) 1900640708a2SPavel Emelyanov { 1901640708a2SPavel Emelyanov struct dentry *dentry = filp->f_path.dentry; 1902640708a2SPavel Emelyanov struct inode *inode = dentry->d_inode; 1903640708a2SPavel Emelyanov struct vm_area_struct *vma; 1904640708a2SPavel Emelyanov struct task_struct *task; 1905640708a2SPavel Emelyanov struct mm_struct *mm; 1906640708a2SPavel Emelyanov ino_t ino; 1907640708a2SPavel Emelyanov int ret; 1908640708a2SPavel Emelyanov 1909640708a2SPavel Emelyanov ret = -EACCES; 1910640708a2SPavel Emelyanov if (!capable(CAP_SYS_ADMIN)) 1911640708a2SPavel Emelyanov goto out; 1912640708a2SPavel Emelyanov 1913640708a2SPavel Emelyanov ret = -ENOENT; 1914640708a2SPavel Emelyanov task = get_proc_task(inode); 1915640708a2SPavel Emelyanov if (!task) 1916640708a2SPavel Emelyanov goto out; 1917640708a2SPavel Emelyanov 1918640708a2SPavel Emelyanov ret = -EACCES; 1919eb94cd96SCyrill Gorcunov if (!ptrace_may_access(task, PTRACE_MODE_READ)) 1920640708a2SPavel Emelyanov goto out_put_task; 1921640708a2SPavel Emelyanov 1922640708a2SPavel Emelyanov ret = 0; 1923640708a2SPavel Emelyanov switch (filp->f_pos) { 1924640708a2SPavel Emelyanov case 0: 1925640708a2SPavel Emelyanov ino = inode->i_ino; 1926640708a2SPavel Emelyanov if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0) 1927eb94cd96SCyrill Gorcunov goto out_put_task; 1928640708a2SPavel Emelyanov filp->f_pos++; 1929640708a2SPavel Emelyanov case 1: 1930640708a2SPavel Emelyanov ino = parent_ino(dentry); 1931640708a2SPavel Emelyanov if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 1932eb94cd96SCyrill Gorcunov goto out_put_task; 1933640708a2SPavel Emelyanov filp->f_pos++; 1934640708a2SPavel Emelyanov default: 1935640708a2SPavel Emelyanov { 1936640708a2SPavel Emelyanov unsigned long nr_files, pos, i; 1937640708a2SPavel Emelyanov struct flex_array *fa = NULL; 1938640708a2SPavel Emelyanov struct map_files_info info; 1939640708a2SPavel Emelyanov struct map_files_info *p; 1940640708a2SPavel Emelyanov 1941640708a2SPavel Emelyanov mm = get_task_mm(task); 1942640708a2SPavel Emelyanov if (!mm) 1943eb94cd96SCyrill Gorcunov goto out_put_task; 1944640708a2SPavel Emelyanov down_read(&mm->mmap_sem); 1945640708a2SPavel Emelyanov 1946640708a2SPavel Emelyanov nr_files = 0; 1947640708a2SPavel Emelyanov 1948640708a2SPavel Emelyanov /* 1949640708a2SPavel Emelyanov * We need two passes here: 1950640708a2SPavel Emelyanov * 1951640708a2SPavel Emelyanov * 1) Collect vmas of mapped files with mmap_sem taken 1952640708a2SPavel Emelyanov * 2) Release mmap_sem and instantiate entries 1953640708a2SPavel Emelyanov * 1954640708a2SPavel Emelyanov * otherwise we get lockdep complained, since filldir() 1955640708a2SPavel Emelyanov * routine might require mmap_sem taken in might_fault(). 1956640708a2SPavel Emelyanov */ 1957640708a2SPavel Emelyanov 1958640708a2SPavel Emelyanov for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) { 1959640708a2SPavel Emelyanov if (vma->vm_file && ++pos > filp->f_pos) 1960640708a2SPavel Emelyanov nr_files++; 1961640708a2SPavel Emelyanov } 1962640708a2SPavel Emelyanov 1963640708a2SPavel Emelyanov if (nr_files) { 1964640708a2SPavel Emelyanov fa = flex_array_alloc(sizeof(info), nr_files, 1965640708a2SPavel Emelyanov GFP_KERNEL); 1966640708a2SPavel Emelyanov if (!fa || flex_array_prealloc(fa, 0, nr_files, 1967640708a2SPavel Emelyanov GFP_KERNEL)) { 1968640708a2SPavel Emelyanov ret = -ENOMEM; 1969640708a2SPavel Emelyanov if (fa) 1970640708a2SPavel Emelyanov flex_array_free(fa); 1971640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1972640708a2SPavel Emelyanov mmput(mm); 1973eb94cd96SCyrill Gorcunov goto out_put_task; 1974640708a2SPavel Emelyanov } 1975640708a2SPavel Emelyanov for (i = 0, vma = mm->mmap, pos = 2; vma; 1976640708a2SPavel Emelyanov vma = vma->vm_next) { 1977640708a2SPavel Emelyanov if (!vma->vm_file) 1978640708a2SPavel Emelyanov continue; 1979640708a2SPavel Emelyanov if (++pos <= filp->f_pos) 1980640708a2SPavel Emelyanov continue; 1981640708a2SPavel Emelyanov 1982640708a2SPavel Emelyanov get_file(vma->vm_file); 1983640708a2SPavel Emelyanov info.file = vma->vm_file; 1984640708a2SPavel Emelyanov info.len = snprintf(info.name, 1985640708a2SPavel Emelyanov sizeof(info.name), "%lx-%lx", 1986640708a2SPavel Emelyanov vma->vm_start, vma->vm_end); 1987640708a2SPavel Emelyanov if (flex_array_put(fa, i++, &info, GFP_KERNEL)) 1988640708a2SPavel Emelyanov BUG(); 1989640708a2SPavel Emelyanov } 1990640708a2SPavel Emelyanov } 1991640708a2SPavel Emelyanov up_read(&mm->mmap_sem); 1992640708a2SPavel Emelyanov 1993640708a2SPavel Emelyanov for (i = 0; i < nr_files; i++) { 1994640708a2SPavel Emelyanov p = flex_array_get(fa, i); 1995640708a2SPavel Emelyanov ret = proc_fill_cache(filp, dirent, filldir, 1996640708a2SPavel Emelyanov p->name, p->len, 1997640708a2SPavel Emelyanov proc_map_files_instantiate, 1998640708a2SPavel Emelyanov task, p->file); 1999640708a2SPavel Emelyanov if (ret) 2000640708a2SPavel Emelyanov break; 2001640708a2SPavel Emelyanov filp->f_pos++; 2002640708a2SPavel Emelyanov fput(p->file); 2003640708a2SPavel Emelyanov } 2004640708a2SPavel Emelyanov for (; i < nr_files; i++) { 2005640708a2SPavel Emelyanov /* 2006640708a2SPavel Emelyanov * In case of error don't forget 2007640708a2SPavel Emelyanov * to put rest of file refs. 2008640708a2SPavel Emelyanov */ 2009640708a2SPavel Emelyanov p = flex_array_get(fa, i); 2010640708a2SPavel Emelyanov fput(p->file); 2011640708a2SPavel Emelyanov } 2012640708a2SPavel Emelyanov if (fa) 2013640708a2SPavel Emelyanov flex_array_free(fa); 2014640708a2SPavel Emelyanov mmput(mm); 2015640708a2SPavel Emelyanov } 2016640708a2SPavel Emelyanov } 2017640708a2SPavel Emelyanov 2018640708a2SPavel Emelyanov out_put_task: 2019640708a2SPavel Emelyanov put_task_struct(task); 2020640708a2SPavel Emelyanov out: 2021640708a2SPavel Emelyanov return ret; 2022640708a2SPavel Emelyanov } 2023640708a2SPavel Emelyanov 2024640708a2SPavel Emelyanov static const struct file_operations proc_map_files_operations = { 2025640708a2SPavel Emelyanov .read = generic_read_dir, 2026640708a2SPavel Emelyanov .readdir = proc_map_files_readdir, 2027640708a2SPavel Emelyanov .llseek = default_llseek, 2028640708a2SPavel Emelyanov }; 2029640708a2SPavel Emelyanov 2030640708a2SPavel Emelyanov #endif /* CONFIG_CHECKPOINT_RESTORE */ 2031640708a2SPavel Emelyanov 2032444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 2033c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2034444ceed8SEric W. Biederman { 2035c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2036444ceed8SEric W. Biederman struct inode *inode; 2037444ceed8SEric W. Biederman struct proc_inode *ei; 2038bd6daba9SKOSAKI Motohiro struct dentry *error = ERR_PTR(-ENOENT); 2039444ceed8SEric W. Biederman 204061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2041444ceed8SEric W. Biederman if (!inode) 2042444ceed8SEric W. Biederman goto out; 2043444ceed8SEric W. Biederman 2044444ceed8SEric W. Biederman ei = PROC_I(inode); 2045444ceed8SEric W. Biederman inode->i_mode = p->mode; 2046444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 2047bfe86848SMiklos Szeredi set_nlink(inode, 2); /* Use getattr to fix if necessary */ 2048444ceed8SEric W. Biederman if (p->iop) 2049444ceed8SEric W. Biederman inode->i_op = p->iop; 2050444ceed8SEric W. Biederman if (p->fop) 2051444ceed8SEric W. Biederman inode->i_fop = p->fop; 2052444ceed8SEric W. Biederman ei->op = p->op; 2053fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2054444ceed8SEric W. Biederman d_add(dentry, inode); 2055444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 20560b728e19SAl Viro if (pid_revalidate(dentry, 0)) 2057444ceed8SEric W. Biederman error = NULL; 2058444ceed8SEric W. Biederman out: 2059444ceed8SEric W. Biederman return error; 2060444ceed8SEric W. Biederman } 2061444ceed8SEric W. Biederman 20621da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 20631da177e4SLinus Torvalds struct dentry *dentry, 2064c5141e6dSEric Dumazet const struct pid_entry *ents, 20657bcd6b0eSEric W. Biederman unsigned int nents) 20661da177e4SLinus Torvalds { 2067cd6a3ce9SEric W. Biederman struct dentry *error; 206899f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2069c5141e6dSEric Dumazet const struct pid_entry *p, *last; 20701da177e4SLinus Torvalds 2071cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 20721da177e4SLinus Torvalds 207399f89551SEric W. Biederman if (!task) 207499f89551SEric W. Biederman goto out_no_task; 20751da177e4SLinus Torvalds 207620cdc894SEric W. Biederman /* 207720cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 207820cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 207920cdc894SEric W. Biederman */ 20807bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20817bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 20821da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 20831da177e4SLinus Torvalds continue; 20841da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 20851da177e4SLinus Torvalds break; 20861da177e4SLinus Torvalds } 20877bcd6b0eSEric W. Biederman if (p > last) 20881da177e4SLinus Torvalds goto out; 20891da177e4SLinus Torvalds 2090444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 20911da177e4SLinus Torvalds out: 209299f89551SEric W. Biederman put_task_struct(task); 209399f89551SEric W. Biederman out_no_task: 2094cd6a3ce9SEric W. Biederman return error; 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds 2097c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 2098c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 209961a28784SEric W. Biederman { 210061a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 210161a28784SEric W. Biederman proc_pident_instantiate, task, p); 210261a28784SEric W. Biederman } 210361a28784SEric W. Biederman 210428a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 210528a6d671SEric W. Biederman void *dirent, filldir_t filldir, 2106c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 210728a6d671SEric W. Biederman { 210828a6d671SEric W. Biederman int i; 21092fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 211028a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 211128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 2112c5141e6dSEric Dumazet const struct pid_entry *p, *last; 211328a6d671SEric W. Biederman ino_t ino; 211428a6d671SEric W. Biederman int ret; 211528a6d671SEric W. Biederman 211628a6d671SEric W. Biederman ret = -ENOENT; 211728a6d671SEric W. Biederman if (!task) 211861a28784SEric W. Biederman goto out_no_task; 211928a6d671SEric W. Biederman 212028a6d671SEric W. Biederman ret = 0; 212128a6d671SEric W. Biederman i = filp->f_pos; 212228a6d671SEric W. Biederman switch (i) { 212328a6d671SEric W. Biederman case 0: 212428a6d671SEric W. Biederman ino = inode->i_ino; 212528a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 212628a6d671SEric W. Biederman goto out; 212728a6d671SEric W. Biederman i++; 212828a6d671SEric W. Biederman filp->f_pos++; 212928a6d671SEric W. Biederman /* fall through */ 213028a6d671SEric W. Biederman case 1: 213128a6d671SEric W. Biederman ino = parent_ino(dentry); 213228a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 213328a6d671SEric W. Biederman goto out; 213428a6d671SEric W. Biederman i++; 213528a6d671SEric W. Biederman filp->f_pos++; 213628a6d671SEric W. Biederman /* fall through */ 213728a6d671SEric W. Biederman default: 213828a6d671SEric W. Biederman i -= 2; 213928a6d671SEric W. Biederman if (i >= nents) { 214028a6d671SEric W. Biederman ret = 1; 214128a6d671SEric W. Biederman goto out; 214228a6d671SEric W. Biederman } 214328a6d671SEric W. Biederman p = ents + i; 21447bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 21457bcd6b0eSEric W. Biederman while (p <= last) { 214661a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 214728a6d671SEric W. Biederman goto out; 214828a6d671SEric W. Biederman filp->f_pos++; 214928a6d671SEric W. Biederman p++; 215028a6d671SEric W. Biederman } 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds 215328a6d671SEric W. Biederman ret = 1; 215428a6d671SEric W. Biederman out: 215561a28784SEric W. Biederman put_task_struct(task); 215661a28784SEric W. Biederman out_no_task: 215728a6d671SEric W. Biederman return ret; 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds 21601da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 216128a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 216228a6d671SEric W. Biederman size_t count, loff_t *ppos) 216328a6d671SEric W. Biederman { 21642fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 216504ff9708SAl Viro char *p = NULL; 216628a6d671SEric W. Biederman ssize_t length; 216728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 216828a6d671SEric W. Biederman 216928a6d671SEric W. Biederman if (!task) 217004ff9708SAl Viro return -ESRCH; 217128a6d671SEric W. Biederman 217228a6d671SEric W. Biederman length = security_getprocattr(task, 21732fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 217404ff9708SAl Viro &p); 217528a6d671SEric W. Biederman put_task_struct(task); 217604ff9708SAl Viro if (length > 0) 217704ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 217804ff9708SAl Viro kfree(p); 217928a6d671SEric W. Biederman return length; 218028a6d671SEric W. Biederman } 218128a6d671SEric W. Biederman 218228a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 218328a6d671SEric W. Biederman size_t count, loff_t *ppos) 218428a6d671SEric W. Biederman { 21852fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 218628a6d671SEric W. Biederman char *page; 218728a6d671SEric W. Biederman ssize_t length; 218828a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 218928a6d671SEric W. Biederman 219028a6d671SEric W. Biederman length = -ESRCH; 219128a6d671SEric W. Biederman if (!task) 219228a6d671SEric W. Biederman goto out_no_task; 219328a6d671SEric W. Biederman if (count > PAGE_SIZE) 219428a6d671SEric W. Biederman count = PAGE_SIZE; 219528a6d671SEric W. Biederman 219628a6d671SEric W. Biederman /* No partial writes. */ 219728a6d671SEric W. Biederman length = -EINVAL; 219828a6d671SEric W. Biederman if (*ppos != 0) 219928a6d671SEric W. Biederman goto out; 220028a6d671SEric W. Biederman 220128a6d671SEric W. Biederman length = -ENOMEM; 2202e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 220328a6d671SEric W. Biederman if (!page) 220428a6d671SEric W. Biederman goto out; 220528a6d671SEric W. Biederman 220628a6d671SEric W. Biederman length = -EFAULT; 220728a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 220828a6d671SEric W. Biederman goto out_free; 220928a6d671SEric W. Biederman 2210107db7c7SDavid Howells /* Guard against adverse ptrace interaction */ 22119b1bf12dSKOSAKI Motohiro length = mutex_lock_interruptible(&task->signal->cred_guard_mutex); 2212107db7c7SDavid Howells if (length < 0) 2213107db7c7SDavid Howells goto out_free; 2214107db7c7SDavid Howells 221528a6d671SEric W. Biederman length = security_setprocattr(task, 22162fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 221728a6d671SEric W. Biederman (void*)page, count); 22189b1bf12dSKOSAKI Motohiro mutex_unlock(&task->signal->cred_guard_mutex); 221928a6d671SEric W. Biederman out_free: 222028a6d671SEric W. Biederman free_page((unsigned long) page); 222128a6d671SEric W. Biederman out: 222228a6d671SEric W. Biederman put_task_struct(task); 222328a6d671SEric W. Biederman out_no_task: 222428a6d671SEric W. Biederman return length; 222528a6d671SEric W. Biederman } 222628a6d671SEric W. Biederman 222700977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 222828a6d671SEric W. Biederman .read = proc_pid_attr_read, 222928a6d671SEric W. Biederman .write = proc_pid_attr_write, 223087df8424SArnd Bergmann .llseek = generic_file_llseek, 223128a6d671SEric W. Biederman }; 223228a6d671SEric W. Biederman 2233c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 2234631f9c18SAlexey Dobriyan REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2235631f9c18SAlexey Dobriyan REG("prev", S_IRUGO, proc_pid_attr_operations), 2236631f9c18SAlexey Dobriyan REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2237631f9c18SAlexey Dobriyan REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2238631f9c18SAlexey Dobriyan REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 2239631f9c18SAlexey Dobriyan REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), 224028a6d671SEric W. Biederman }; 224128a6d671SEric W. Biederman 224272d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 22431da177e4SLinus Torvalds void * dirent, filldir_t filldir) 22441da177e4SLinus Torvalds { 22451da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 224672d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds 224900977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 22501da177e4SLinus Torvalds .read = generic_read_dir, 225172d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 22526038f373SArnd Bergmann .llseek = default_llseek, 22531da177e4SLinus Torvalds }; 22541da177e4SLinus Torvalds 225572d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 225600cd8dd3SAl Viro struct dentry *dentry, unsigned int flags) 22571da177e4SLinus Torvalds { 22587bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 22597bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds 2262c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 226372d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 226499f89551SEric W. Biederman .getattr = pid_getattr, 22656d76fa58SLinus Torvalds .setattr = proc_setattr, 22661da177e4SLinus Torvalds }; 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds #endif 22691da177e4SLinus Torvalds 2270698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 22713cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 22723cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 22733cb4a0bbSKawai, Hidehiro { 22743cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 22753cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 22763cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 22773cb4a0bbSKawai, Hidehiro size_t len; 22783cb4a0bbSKawai, Hidehiro int ret; 22793cb4a0bbSKawai, Hidehiro 22803cb4a0bbSKawai, Hidehiro if (!task) 22813cb4a0bbSKawai, Hidehiro return -ESRCH; 22823cb4a0bbSKawai, Hidehiro 22833cb4a0bbSKawai, Hidehiro ret = 0; 22843cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22853cb4a0bbSKawai, Hidehiro if (mm) { 22863cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 22873cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 22883cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 22893cb4a0bbSKawai, Hidehiro mmput(mm); 22903cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 22913cb4a0bbSKawai, Hidehiro } 22923cb4a0bbSKawai, Hidehiro 22933cb4a0bbSKawai, Hidehiro put_task_struct(task); 22943cb4a0bbSKawai, Hidehiro 22953cb4a0bbSKawai, Hidehiro return ret; 22963cb4a0bbSKawai, Hidehiro } 22973cb4a0bbSKawai, Hidehiro 22983cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 22993cb4a0bbSKawai, Hidehiro const char __user *buf, 23003cb4a0bbSKawai, Hidehiro size_t count, 23013cb4a0bbSKawai, Hidehiro loff_t *ppos) 23023cb4a0bbSKawai, Hidehiro { 23033cb4a0bbSKawai, Hidehiro struct task_struct *task; 23043cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 23053cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 23063cb4a0bbSKawai, Hidehiro unsigned int val; 23073cb4a0bbSKawai, Hidehiro int ret; 23083cb4a0bbSKawai, Hidehiro int i; 23093cb4a0bbSKawai, Hidehiro unsigned long mask; 23103cb4a0bbSKawai, Hidehiro 23113cb4a0bbSKawai, Hidehiro ret = -EFAULT; 23123cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 23133cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 23143cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 23153cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 23163cb4a0bbSKawai, Hidehiro goto out_no_task; 23173cb4a0bbSKawai, Hidehiro 23183cb4a0bbSKawai, Hidehiro ret = -EINVAL; 23193cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 23203cb4a0bbSKawai, Hidehiro if (*end == '\n') 23213cb4a0bbSKawai, Hidehiro end++; 23223cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 23233cb4a0bbSKawai, Hidehiro goto out_no_task; 23243cb4a0bbSKawai, Hidehiro 23253cb4a0bbSKawai, Hidehiro ret = -ESRCH; 23263cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 23273cb4a0bbSKawai, Hidehiro if (!task) 23283cb4a0bbSKawai, Hidehiro goto out_no_task; 23293cb4a0bbSKawai, Hidehiro 23303cb4a0bbSKawai, Hidehiro ret = end - buffer; 23313cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 23323cb4a0bbSKawai, Hidehiro if (!mm) 23333cb4a0bbSKawai, Hidehiro goto out_no_mm; 23343cb4a0bbSKawai, Hidehiro 23353cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 23363cb4a0bbSKawai, Hidehiro if (val & mask) 23373cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 23383cb4a0bbSKawai, Hidehiro else 23393cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 23403cb4a0bbSKawai, Hidehiro } 23413cb4a0bbSKawai, Hidehiro 23423cb4a0bbSKawai, Hidehiro mmput(mm); 23433cb4a0bbSKawai, Hidehiro out_no_mm: 23443cb4a0bbSKawai, Hidehiro put_task_struct(task); 23453cb4a0bbSKawai, Hidehiro out_no_task: 23463cb4a0bbSKawai, Hidehiro return ret; 23473cb4a0bbSKawai, Hidehiro } 23483cb4a0bbSKawai, Hidehiro 23493cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 23503cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 23513cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 235287df8424SArnd Bergmann .llseek = generic_file_llseek, 23533cb4a0bbSKawai, Hidehiro }; 23543cb4a0bbSKawai, Hidehiro #endif 23553cb4a0bbSKawai, Hidehiro 23561da177e4SLinus Torvalds /* 23571da177e4SLinus Torvalds * /proc/self: 23581da177e4SLinus Torvalds */ 23591da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 23601da177e4SLinus Torvalds int buflen) 23611da177e4SLinus Torvalds { 2362488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2363b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 23648578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2365b55fcb22SAndrew Morton if (!tgid) 2366488e5bc4SEric W. Biederman return -ENOENT; 2367b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 23681da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 23691da177e4SLinus Torvalds } 23701da177e4SLinus Torvalds 2371008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 23721da177e4SLinus Torvalds { 2373488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2374b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 23757fee4868SAl Viro char *name = ERR_PTR(-ENOENT); 23767fee4868SAl Viro if (tgid) { 23777fee4868SAl Viro name = __getname(); 23787fee4868SAl Viro if (!name) 23797fee4868SAl Viro name = ERR_PTR(-ENOMEM); 23807fee4868SAl Viro else 23817fee4868SAl Viro sprintf(name, "%d", tgid); 23827fee4868SAl Viro } 23837fee4868SAl Viro nd_set_link(nd, name); 23847fee4868SAl Viro return NULL; 23857fee4868SAl Viro } 23867fee4868SAl Viro 23877fee4868SAl Viro static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd, 23887fee4868SAl Viro void *cookie) 23897fee4868SAl Viro { 23907fee4868SAl Viro char *s = nd_get_link(nd); 23917fee4868SAl Viro if (!IS_ERR(s)) 23927fee4868SAl Viro __putname(s); 23931da177e4SLinus Torvalds } 23941da177e4SLinus Torvalds 2395c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 23961da177e4SLinus Torvalds .readlink = proc_self_readlink, 23971da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 23987fee4868SAl Viro .put_link = proc_self_put_link, 23991da177e4SLinus Torvalds }; 24001da177e4SLinus Torvalds 240128a6d671SEric W. Biederman /* 2402801199ceSEric W. Biederman * proc base 2403801199ceSEric W. Biederman * 2404801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2405801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2406801199ceSEric W. Biederman * describe something that is process related. 2407801199ceSEric W. Biederman */ 2408c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 240961a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2410801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2411801199ceSEric W. Biederman }; 2412801199ceSEric W. Biederman 2413444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2414c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2415801199ceSEric W. Biederman { 2416c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2417801199ceSEric W. Biederman struct inode *inode; 2418801199ceSEric W. Biederman struct proc_inode *ei; 241973d36460SDan Carpenter struct dentry *error; 2420801199ceSEric W. Biederman 2421801199ceSEric W. Biederman /* Allocate the inode */ 2422801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2423801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2424801199ceSEric W. Biederman if (!inode) 2425801199ceSEric W. Biederman goto out; 2426801199ceSEric W. Biederman 2427801199ceSEric W. Biederman /* Initialize the inode */ 2428801199ceSEric W. Biederman ei = PROC_I(inode); 242985fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 2430801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2431801199ceSEric W. Biederman 2432801199ceSEric W. Biederman /* 2433801199ceSEric W. Biederman * grab the reference to the task. 2434801199ceSEric W. Biederman */ 24351a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2436801199ceSEric W. Biederman if (!ei->pid) 2437801199ceSEric W. Biederman goto out_iput; 2438801199ceSEric W. Biederman 2439801199ceSEric W. Biederman inode->i_mode = p->mode; 2440801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2441bfe86848SMiklos Szeredi set_nlink(inode, 2); 2442801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2443801199ceSEric W. Biederman inode->i_size = 64; 2444801199ceSEric W. Biederman if (p->iop) 2445801199ceSEric W. Biederman inode->i_op = p->iop; 2446801199ceSEric W. Biederman if (p->fop) 2447801199ceSEric W. Biederman inode->i_fop = p->fop; 2448801199ceSEric W. Biederman ei->op = p->op; 2449801199ceSEric W. Biederman d_add(dentry, inode); 2450801199ceSEric W. Biederman error = NULL; 2451801199ceSEric W. Biederman out: 2452801199ceSEric W. Biederman return error; 2453801199ceSEric W. Biederman out_iput: 2454801199ceSEric W. Biederman iput(inode); 2455801199ceSEric W. Biederman goto out; 2456801199ceSEric W. Biederman } 2457801199ceSEric W. Biederman 2458444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2459444ceed8SEric W. Biederman { 2460444ceed8SEric W. Biederman struct dentry *error; 2461444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2462c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2463444ceed8SEric W. Biederman 2464444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2465444ceed8SEric W. Biederman 2466444ceed8SEric W. Biederman if (!task) 2467444ceed8SEric W. Biederman goto out_no_task; 2468444ceed8SEric W. Biederman 2469444ceed8SEric W. Biederman /* Lookup the directory entry */ 24707bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 24717bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2472444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2473444ceed8SEric W. Biederman continue; 2474444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2475444ceed8SEric W. Biederman break; 2476444ceed8SEric W. Biederman } 24777bcd6b0eSEric W. Biederman if (p > last) 2478444ceed8SEric W. Biederman goto out; 2479444ceed8SEric W. Biederman 2480444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2481444ceed8SEric W. Biederman 2482444ceed8SEric W. Biederman out: 2483444ceed8SEric W. Biederman put_task_struct(task); 2484444ceed8SEric W. Biederman out_no_task: 2485444ceed8SEric W. Biederman return error; 2486444ceed8SEric W. Biederman } 2487444ceed8SEric W. Biederman 2488c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2489c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 249061a28784SEric W. Biederman { 249161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 249261a28784SEric W. Biederman proc_base_instantiate, task, p); 249361a28784SEric W. Biederman } 249461a28784SEric W. Biederman 2495aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2496297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2497aba76fdbSAndrew Morton { 2498940389b8SAndrea Righi struct task_io_accounting acct = task->ioac; 2499297c5d92SAndrea Righi unsigned long flags; 2500293eb1e7SVasiliy Kulikov int result; 2501297c5d92SAndrea Righi 2502293eb1e7SVasiliy Kulikov result = mutex_lock_killable(&task->signal->cred_guard_mutex); 2503293eb1e7SVasiliy Kulikov if (result) 2504293eb1e7SVasiliy Kulikov return result; 2505293eb1e7SVasiliy Kulikov 2506293eb1e7SVasiliy Kulikov if (!ptrace_may_access(task, PTRACE_MODE_READ)) { 2507293eb1e7SVasiliy Kulikov result = -EACCES; 2508293eb1e7SVasiliy Kulikov goto out_unlock; 2509293eb1e7SVasiliy Kulikov } 25101d1221f3SVasiliy Kulikov 25115995477aSAndrea Righi if (whole && lock_task_sighand(task, &flags)) { 2512b2d002dbSAndrea Righi struct task_struct *t = task; 2513297c5d92SAndrea Righi 25145995477aSAndrea Righi task_io_accounting_add(&acct, &task->signal->ioac); 25155995477aSAndrea Righi while_each_thread(task, t) 25165995477aSAndrea Righi task_io_accounting_add(&acct, &t->ioac); 2517297c5d92SAndrea Righi 2518297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2519297c5d92SAndrea Righi } 2520293eb1e7SVasiliy Kulikov result = sprintf(buffer, 2521aba76fdbSAndrew Morton "rchar: %llu\n" 2522aba76fdbSAndrew Morton "wchar: %llu\n" 2523aba76fdbSAndrew Morton "syscr: %llu\n" 2524aba76fdbSAndrew Morton "syscw: %llu\n" 2525aba76fdbSAndrew Morton "read_bytes: %llu\n" 2526aba76fdbSAndrew Morton "write_bytes: %llu\n" 2527aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 25287c44319dSAlexander Beregalov (unsigned long long)acct.rchar, 25297c44319dSAlexander Beregalov (unsigned long long)acct.wchar, 25307c44319dSAlexander Beregalov (unsigned long long)acct.syscr, 25317c44319dSAlexander Beregalov (unsigned long long)acct.syscw, 25327c44319dSAlexander Beregalov (unsigned long long)acct.read_bytes, 25337c44319dSAlexander Beregalov (unsigned long long)acct.write_bytes, 25347c44319dSAlexander Beregalov (unsigned long long)acct.cancelled_write_bytes); 2535293eb1e7SVasiliy Kulikov out_unlock: 2536293eb1e7SVasiliy Kulikov mutex_unlock(&task->signal->cred_guard_mutex); 2537293eb1e7SVasiliy Kulikov return result; 2538aba76fdbSAndrew Morton } 2539297c5d92SAndrea Righi 2540297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2541297c5d92SAndrea Righi { 2542297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2543297c5d92SAndrea Righi } 2544297c5d92SAndrea Righi 2545297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2546297c5d92SAndrea Righi { 2547297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2548297c5d92SAndrea Righi } 2549297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2550aba76fdbSAndrew Morton 255122d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 255222d917d8SEric W. Biederman static int proc_id_map_open(struct inode *inode, struct file *file, 255322d917d8SEric W. Biederman struct seq_operations *seq_ops) 255422d917d8SEric W. Biederman { 255522d917d8SEric W. Biederman struct user_namespace *ns = NULL; 255622d917d8SEric W. Biederman struct task_struct *task; 255722d917d8SEric W. Biederman struct seq_file *seq; 255822d917d8SEric W. Biederman int ret = -EINVAL; 255922d917d8SEric W. Biederman 256022d917d8SEric W. Biederman task = get_proc_task(inode); 256122d917d8SEric W. Biederman if (task) { 256222d917d8SEric W. Biederman rcu_read_lock(); 256322d917d8SEric W. Biederman ns = get_user_ns(task_cred_xxx(task, user_ns)); 256422d917d8SEric W. Biederman rcu_read_unlock(); 256522d917d8SEric W. Biederman put_task_struct(task); 256622d917d8SEric W. Biederman } 256722d917d8SEric W. Biederman if (!ns) 256822d917d8SEric W. Biederman goto err; 256922d917d8SEric W. Biederman 257022d917d8SEric W. Biederman ret = seq_open(file, seq_ops); 257122d917d8SEric W. Biederman if (ret) 257222d917d8SEric W. Biederman goto err_put_ns; 257322d917d8SEric W. Biederman 257422d917d8SEric W. Biederman seq = file->private_data; 257522d917d8SEric W. Biederman seq->private = ns; 257622d917d8SEric W. Biederman 257722d917d8SEric W. Biederman return 0; 257822d917d8SEric W. Biederman err_put_ns: 257922d917d8SEric W. Biederman put_user_ns(ns); 258022d917d8SEric W. Biederman err: 258122d917d8SEric W. Biederman return ret; 258222d917d8SEric W. Biederman } 258322d917d8SEric W. Biederman 258422d917d8SEric W. Biederman static int proc_id_map_release(struct inode *inode, struct file *file) 258522d917d8SEric W. Biederman { 258622d917d8SEric W. Biederman struct seq_file *seq = file->private_data; 258722d917d8SEric W. Biederman struct user_namespace *ns = seq->private; 258822d917d8SEric W. Biederman put_user_ns(ns); 258922d917d8SEric W. Biederman return seq_release(inode, file); 259022d917d8SEric W. Biederman } 259122d917d8SEric W. Biederman 259222d917d8SEric W. Biederman static int proc_uid_map_open(struct inode *inode, struct file *file) 259322d917d8SEric W. Biederman { 259422d917d8SEric W. Biederman return proc_id_map_open(inode, file, &proc_uid_seq_operations); 259522d917d8SEric W. Biederman } 259622d917d8SEric W. Biederman 259722d917d8SEric W. Biederman static int proc_gid_map_open(struct inode *inode, struct file *file) 259822d917d8SEric W. Biederman { 259922d917d8SEric W. Biederman return proc_id_map_open(inode, file, &proc_gid_seq_operations); 260022d917d8SEric W. Biederman } 260122d917d8SEric W. Biederman 260222d917d8SEric W. Biederman static const struct file_operations proc_uid_map_operations = { 260322d917d8SEric W. Biederman .open = proc_uid_map_open, 260422d917d8SEric W. Biederman .write = proc_uid_map_write, 260522d917d8SEric W. Biederman .read = seq_read, 260622d917d8SEric W. Biederman .llseek = seq_lseek, 260722d917d8SEric W. Biederman .release = proc_id_map_release, 260822d917d8SEric W. Biederman }; 260922d917d8SEric W. Biederman 261022d917d8SEric W. Biederman static const struct file_operations proc_gid_map_operations = { 261122d917d8SEric W. Biederman .open = proc_gid_map_open, 261222d917d8SEric W. Biederman .write = proc_gid_map_write, 261322d917d8SEric W. Biederman .read = seq_read, 261422d917d8SEric W. Biederman .llseek = seq_lseek, 261522d917d8SEric W. Biederman .release = proc_id_map_release, 261622d917d8SEric W. Biederman }; 261722d917d8SEric W. Biederman #endif /* CONFIG_USER_NS */ 261822d917d8SEric W. Biederman 261947830723SKees Cook static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, 262047830723SKees Cook struct pid *pid, struct task_struct *task) 262147830723SKees Cook { 2622a9712bc1SAl Viro int err = lock_trace(task); 2623a9712bc1SAl Viro if (!err) { 262447830723SKees Cook seq_printf(m, "%08x\n", task->personality); 2625a9712bc1SAl Viro unlock_trace(task); 2626a9712bc1SAl Viro } 2627a9712bc1SAl Viro return err; 262847830723SKees Cook } 262947830723SKees Cook 2630801199ceSEric W. Biederman /* 263128a6d671SEric W. Biederman * Thread groups 263228a6d671SEric W. Biederman */ 263300977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2634c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 263520cdc894SEric W. Biederman 2636c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 2637631f9c18SAlexey Dobriyan DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), 2638631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 2639640708a2SPavel Emelyanov #ifdef CONFIG_CHECKPOINT_RESTORE 2640640708a2SPavel Emelyanov DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations), 2641640708a2SPavel Emelyanov #endif 2642631f9c18SAlexey Dobriyan DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 26436b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 2644b2211a36SAndrew Morton #ifdef CONFIG_NET 2645631f9c18SAlexey Dobriyan DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), 2646b2211a36SAndrew Morton #endif 2647631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 2648631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 2649631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 2650a9712bc1SAl Viro ONE("personality", S_IRUGO, proc_pid_personality), 26513036e7b4SJiri Olsa INF("limits", S_IRUGO, proc_pid_limits), 265243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 2653631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 265443ae34cbSIngo Molnar #endif 26555091faa4SMike Galbraith #ifdef CONFIG_SCHED_AUTOGROUP 26565091faa4SMike Galbraith REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), 26575091faa4SMike Galbraith #endif 26584614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 2659ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 2660a9712bc1SAl Viro INF("syscall", S_IRUGO, proc_pid_syscall), 2661ebcb6734SRoland McGrath #endif 2662631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 2663631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tgid_stat), 2664631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 2665b7643757SSiddhesh Poyarekar REG("maps", S_IRUGO, proc_pid_maps_operations), 266628a6d671SEric W. Biederman #ifdef CONFIG_NUMA 2667b7643757SSiddhesh Poyarekar REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), 266828a6d671SEric W. Biederman #endif 2669631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 2670631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 2671631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 2672631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 2673631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 2674631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 2675631f9c18SAlexey Dobriyan REG("mountstats", S_IRUSR, proc_mountstats_operations), 26761e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2677631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 2678b7643757SSiddhesh Poyarekar REG("smaps", S_IRUGO, proc_pid_smaps_operations), 2679ca6b0bf0SAl Viro REG("pagemap", S_IRUGO, proc_pagemap_operations), 268028a6d671SEric W. Biederman #endif 268128a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 2682631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 268328a6d671SEric W. Biederman #endif 268428a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 2685631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 268628a6d671SEric W. Biederman #endif 26872ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 2688a9712bc1SAl Viro ONE("stack", S_IRUGO, proc_pid_stack), 268928a6d671SEric W. Biederman #endif 269028a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 2691631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 269228a6d671SEric W. Biederman #endif 26939745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 2694631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 26959745512cSArjan van de Ven #endif 26968793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 2697631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 269828a6d671SEric W. Biederman #endif 2699a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2700631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 2701a424316cSPaul Menage #endif 2702631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 2703631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 2704a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 270528a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 2706631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 2707631f9c18SAlexey Dobriyan REG("sessionid", S_IRUGO, proc_sessionid_operations), 270828a6d671SEric W. Biederman #endif 2709f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2710631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 2711f4f154fdSAkinobu Mita #endif 2712698ba7b5SChristoph Hellwig #ifdef CONFIG_ELF_CORE 2713631f9c18SAlexey Dobriyan REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), 27143cb4a0bbSKawai, Hidehiro #endif 2715aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 27161d1221f3SVasiliy Kulikov INF("io", S_IRUSR, proc_tgid_io_accounting), 2717aba76fdbSAndrew Morton #endif 2718f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 2719f133eccaSChris Metcalf INF("hardwall", S_IRUGO, proc_pid_hardwall), 2720f133eccaSChris Metcalf #endif 272122d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 272222d917d8SEric W. Biederman REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), 272322d917d8SEric W. Biederman REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), 272422d917d8SEric W. Biederman #endif 272528a6d671SEric W. Biederman }; 272628a6d671SEric W. Biederman 272728a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 272828a6d671SEric W. Biederman void * dirent, filldir_t filldir) 272928a6d671SEric W. Biederman { 273028a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 273128a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 273228a6d671SEric W. Biederman } 273328a6d671SEric W. Biederman 273400977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 273528a6d671SEric W. Biederman .read = generic_read_dir, 273628a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 27376038f373SArnd Bergmann .llseek = default_llseek, 273828a6d671SEric W. Biederman }; 273928a6d671SEric W. Biederman 274000cd8dd3SAl Viro static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 274100cd8dd3SAl Viro { 27427bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 27437bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 274428a6d671SEric W. Biederman } 274528a6d671SEric W. Biederman 2746c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 274728a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 274828a6d671SEric W. Biederman .getattr = pid_getattr, 274928a6d671SEric W. Biederman .setattr = proc_setattr, 27500499680aSVasiliy Kulikov .permission = proc_pid_permission, 275128a6d671SEric W. Biederman }; 275228a6d671SEric W. Biederman 275360347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 27541da177e4SLinus Torvalds { 275548e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 27568578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 275748e6484dSEric W. Biederman struct qstr name; 27581da177e4SLinus Torvalds 275948e6484dSEric W. Biederman name.name = buf; 276060347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 276160347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 276248e6484dSEric W. Biederman if (dentry) { 276348e6484dSEric W. Biederman shrink_dcache_parent(dentry); 276448e6484dSEric W. Biederman d_drop(dentry); 276548e6484dSEric W. Biederman dput(dentry); 27661da177e4SLinus Torvalds } 27671da177e4SLinus Torvalds 276848e6484dSEric W. Biederman name.name = buf; 276960347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 277060347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 277148e6484dSEric W. Biederman if (!leader) 277248e6484dSEric W. Biederman goto out; 277348e6484dSEric W. Biederman 277448e6484dSEric W. Biederman name.name = "task"; 277548e6484dSEric W. Biederman name.len = strlen(name.name); 277648e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 277748e6484dSEric W. Biederman if (!dir) 277848e6484dSEric W. Biederman goto out_put_leader; 277948e6484dSEric W. Biederman 278048e6484dSEric W. Biederman name.name = buf; 278160347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 278248e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 278348e6484dSEric W. Biederman if (dentry) { 278448e6484dSEric W. Biederman shrink_dcache_parent(dentry); 278548e6484dSEric W. Biederman d_drop(dentry); 278648e6484dSEric W. Biederman dput(dentry); 27871da177e4SLinus Torvalds } 278848e6484dSEric W. Biederman 278948e6484dSEric W. Biederman dput(dir); 279048e6484dSEric W. Biederman out_put_leader: 279148e6484dSEric W. Biederman dput(leader); 279248e6484dSEric W. Biederman out: 279348e6484dSEric W. Biederman return; 27941da177e4SLinus Torvalds } 27951da177e4SLinus Torvalds 27960895e91dSRandy Dunlap /** 27970895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 27980895e91dSRandy Dunlap * @task: task that should be flushed. 27990895e91dSRandy Dunlap * 28000895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 280160347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 28020895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 28030895e91dSRandy Dunlap * 28040895e91dSRandy Dunlap * Looks in the dcache for 28050895e91dSRandy Dunlap * /proc/@pid 28060895e91dSRandy Dunlap * /proc/@tgid/task/@pid 28070895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 28080895e91dSRandy Dunlap * from the dcache. 28090895e91dSRandy Dunlap * 28100895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 28110895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 28120895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 28130895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 28140895e91dSRandy Dunlap * dcache entries at process exit time. 28150895e91dSRandy Dunlap * 28160895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 28170895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 28180895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 281960347f67SPavel Emelyanov */ 282060347f67SPavel Emelyanov 282160347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 282260347f67SPavel Emelyanov { 28239fcc2d15SEric W. Biederman int i; 28249b4d1cbeSOleg Nesterov struct pid *pid, *tgid; 2825130f77ecSPavel Emelyanov struct upid *upid; 2826130f77ecSPavel Emelyanov 2827130f77ecSPavel Emelyanov pid = task_pid(task); 2828130f77ecSPavel Emelyanov tgid = task_tgid(task); 28299fcc2d15SEric W. Biederman 28309fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2831130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2832130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 28339b4d1cbeSOleg Nesterov tgid->numbers[i].nr); 2834130f77ecSPavel Emelyanov } 28356f4e6433SPavel Emelyanov 28366f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 28376f4e6433SPavel Emelyanov if (upid->nr == 1) 28386f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 283960347f67SPavel Emelyanov } 284060347f67SPavel Emelyanov 28419711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 28429711ef99SAdrian Bunk struct dentry * dentry, 2843c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2844444ceed8SEric W. Biederman { 2845444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2846444ceed8SEric W. Biederman struct inode *inode; 2847444ceed8SEric W. Biederman 284861a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2849444ceed8SEric W. Biederman if (!inode) 2850444ceed8SEric W. Biederman goto out; 2851444ceed8SEric W. Biederman 2852444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2853444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2854444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2855444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2856aed54175SVegard Nossum 2857bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff, 2858bfe86848SMiklos Szeredi ARRAY_SIZE(tgid_base_stuff))); 2859444ceed8SEric W. Biederman 2860fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 2861444ceed8SEric W. Biederman 2862444ceed8SEric W. Biederman d_add(dentry, inode); 2863444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 28640b728e19SAl Viro if (pid_revalidate(dentry, 0)) 2865444ceed8SEric W. Biederman error = NULL; 2866444ceed8SEric W. Biederman out: 2867444ceed8SEric W. Biederman return error; 2868444ceed8SEric W. Biederman } 2869444ceed8SEric W. Biederman 287000cd8dd3SAl Viro struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 28711da177e4SLinus Torvalds { 287273d36460SDan Carpenter struct dentry *result; 28731da177e4SLinus Torvalds struct task_struct *task; 28741da177e4SLinus Torvalds unsigned tgid; 2875b488893aSPavel Emelyanov struct pid_namespace *ns; 28761da177e4SLinus Torvalds 2877801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2878801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2879801199ceSEric W. Biederman goto out; 2880801199ceSEric W. Biederman 28811da177e4SLinus Torvalds tgid = name_to_int(dentry); 28821da177e4SLinus Torvalds if (tgid == ~0U) 28831da177e4SLinus Torvalds goto out; 28841da177e4SLinus Torvalds 2885b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2886de758734SEric W. Biederman rcu_read_lock(); 2887b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 28881da177e4SLinus Torvalds if (task) 28891da177e4SLinus Torvalds get_task_struct(task); 2890de758734SEric W. Biederman rcu_read_unlock(); 28911da177e4SLinus Torvalds if (!task) 28921da177e4SLinus Torvalds goto out; 28931da177e4SLinus Torvalds 2894444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 289548e6484dSEric W. Biederman put_task_struct(task); 28961da177e4SLinus Torvalds out: 2897cd6a3ce9SEric W. Biederman return result; 28981da177e4SLinus Torvalds } 28991da177e4SLinus Torvalds 29001da177e4SLinus Torvalds /* 29010804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 29020bc58a91SEric W. Biederman * 29031da177e4SLinus Torvalds */ 290419fd4bb2SEric W. Biederman struct tgid_iter { 290519fd4bb2SEric W. Biederman unsigned int tgid; 29060804ef4bSEric W. Biederman struct task_struct *task; 290719fd4bb2SEric W. Biederman }; 290819fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 290919fd4bb2SEric W. Biederman { 29100804ef4bSEric W. Biederman struct pid *pid; 29111da177e4SLinus Torvalds 291219fd4bb2SEric W. Biederman if (iter.task) 291319fd4bb2SEric W. Biederman put_task_struct(iter.task); 29140804ef4bSEric W. Biederman rcu_read_lock(); 29150804ef4bSEric W. Biederman retry: 291619fd4bb2SEric W. Biederman iter.task = NULL; 291719fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 29180804ef4bSEric W. Biederman if (pid) { 291919fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 292019fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 29210804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 29220804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 29230804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 29240804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 29250804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 29260804ef4bSEric W. Biederman * 29270804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 29280804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 29290804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 29300804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 29310804ef4bSEric W. Biederman * As we don't care in the case of readdir. 29320bc58a91SEric W. Biederman */ 293319fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 293419fd4bb2SEric W. Biederman iter.tgid += 1; 29350804ef4bSEric W. Biederman goto retry; 293619fd4bb2SEric W. Biederman } 293719fd4bb2SEric W. Biederman get_task_struct(iter.task); 29381da177e4SLinus Torvalds } 2939454cc105SEric W. Biederman rcu_read_unlock(); 294019fd4bb2SEric W. Biederman return iter; 29411da177e4SLinus Torvalds } 29421da177e4SLinus Torvalds 29437bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 29441da177e4SLinus Torvalds 294561a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 294619fd4bb2SEric W. Biederman struct tgid_iter iter) 294761a28784SEric W. Biederman { 294861a28784SEric W. Biederman char name[PROC_NUMBUF]; 294919fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 295061a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 295119fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 295261a28784SEric W. Biederman } 295361a28784SEric W. Biederman 29540499680aSVasiliy Kulikov static int fake_filldir(void *buf, const char *name, int namelen, 29550499680aSVasiliy Kulikov loff_t offset, u64 ino, unsigned d_type) 29560499680aSVasiliy Kulikov { 29570499680aSVasiliy Kulikov return 0; 29580499680aSVasiliy Kulikov } 29590499680aSVasiliy Kulikov 29601da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 29611da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 29621da177e4SLinus Torvalds { 2963d8bdc59fSLinus Torvalds unsigned int nr; 2964d8bdc59fSLinus Torvalds struct task_struct *reaper; 296519fd4bb2SEric W. Biederman struct tgid_iter iter; 2966b488893aSPavel Emelyanov struct pid_namespace *ns; 29670499680aSVasiliy Kulikov filldir_t __filldir; 29681da177e4SLinus Torvalds 2969d8bdc59fSLinus Torvalds if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET) 2970d8bdc59fSLinus Torvalds goto out_no_task; 2971d8bdc59fSLinus Torvalds nr = filp->f_pos - FIRST_PROCESS_ENTRY; 2972d8bdc59fSLinus Torvalds 2973d8bdc59fSLinus Torvalds reaper = get_proc_task(filp->f_path.dentry->d_inode); 297461a28784SEric W. Biederman if (!reaper) 297561a28784SEric W. Biederman goto out_no_task; 297661a28784SEric W. Biederman 29777bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2978c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 297961a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2980801199ceSEric W. Biederman goto out; 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds 2983b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 298419fd4bb2SEric W. Biederman iter.task = NULL; 298519fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 298619fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 298719fd4bb2SEric W. Biederman iter.task; 298819fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 29890499680aSVasiliy Kulikov if (has_pid_permissions(ns, iter.task, 2)) 29900499680aSVasiliy Kulikov __filldir = filldir; 29910499680aSVasiliy Kulikov else 29920499680aSVasiliy Kulikov __filldir = fake_filldir; 29930499680aSVasiliy Kulikov 299419fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 29950499680aSVasiliy Kulikov if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) { 299619fd4bb2SEric W. Biederman put_task_struct(iter.task); 29970804ef4bSEric W. Biederman goto out; 29981da177e4SLinus Torvalds } 29991da177e4SLinus Torvalds } 30000804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 30010804ef4bSEric W. Biederman out: 300261a28784SEric W. Biederman put_task_struct(reaper); 300361a28784SEric W. Biederman out_no_task: 30041da177e4SLinus Torvalds return 0; 30051da177e4SLinus Torvalds } 30061da177e4SLinus Torvalds 30070bc58a91SEric W. Biederman /* 300828a6d671SEric W. Biederman * Tasks 300928a6d671SEric W. Biederman */ 3010c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 3011631f9c18SAlexey Dobriyan DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), 30123835541dSJerome Marchand DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), 30136b4e306aSEric W. Biederman DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), 3014631f9c18SAlexey Dobriyan REG("environ", S_IRUSR, proc_environ_operations), 3015631f9c18SAlexey Dobriyan INF("auxv", S_IRUSR, proc_pid_auxv), 3016631f9c18SAlexey Dobriyan ONE("status", S_IRUGO, proc_pid_status), 3017a9712bc1SAl Viro ONE("personality", S_IRUGO, proc_pid_personality), 30183036e7b4SJiri Olsa INF("limits", S_IRUGO, proc_pid_limits), 301943ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 3020631f9c18SAlexey Dobriyan REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), 302143ae34cbSIngo Molnar #endif 30224614a696Sjohn stultz REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), 3023ebcb6734SRoland McGrath #ifdef CONFIG_HAVE_ARCH_TRACEHOOK 3024a9712bc1SAl Viro INF("syscall", S_IRUGO, proc_pid_syscall), 3025ebcb6734SRoland McGrath #endif 3026631f9c18SAlexey Dobriyan INF("cmdline", S_IRUGO, proc_pid_cmdline), 3027631f9c18SAlexey Dobriyan ONE("stat", S_IRUGO, proc_tid_stat), 3028631f9c18SAlexey Dobriyan ONE("statm", S_IRUGO, proc_pid_statm), 3029b7643757SSiddhesh Poyarekar REG("maps", S_IRUGO, proc_tid_maps_operations), 303081841161SCyrill Gorcunov #ifdef CONFIG_CHECKPOINT_RESTORE 303181841161SCyrill Gorcunov REG("children", S_IRUGO, proc_tid_children_operations), 303281841161SCyrill Gorcunov #endif 303328a6d671SEric W. Biederman #ifdef CONFIG_NUMA 3034b7643757SSiddhesh Poyarekar REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations), 303528a6d671SEric W. Biederman #endif 3036631f9c18SAlexey Dobriyan REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), 3037631f9c18SAlexey Dobriyan LNK("cwd", proc_cwd_link), 3038631f9c18SAlexey Dobriyan LNK("root", proc_root_link), 3039631f9c18SAlexey Dobriyan LNK("exe", proc_exe_link), 3040631f9c18SAlexey Dobriyan REG("mounts", S_IRUGO, proc_mounts_operations), 3041631f9c18SAlexey Dobriyan REG("mountinfo", S_IRUGO, proc_mountinfo_operations), 30421e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 3043631f9c18SAlexey Dobriyan REG("clear_refs", S_IWUSR, proc_clear_refs_operations), 3044b7643757SSiddhesh Poyarekar REG("smaps", S_IRUGO, proc_tid_smaps_operations), 3045ca6b0bf0SAl Viro REG("pagemap", S_IRUGO, proc_pagemap_operations), 304628a6d671SEric W. Biederman #endif 304728a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 3048631f9c18SAlexey Dobriyan DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), 304928a6d671SEric W. Biederman #endif 305028a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 3051631f9c18SAlexey Dobriyan INF("wchan", S_IRUGO, proc_pid_wchan), 305228a6d671SEric W. Biederman #endif 30532ec220e2SKen Chen #ifdef CONFIG_STACKTRACE 3054a9712bc1SAl Viro ONE("stack", S_IRUGO, proc_pid_stack), 305528a6d671SEric W. Biederman #endif 305628a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 3057631f9c18SAlexey Dobriyan INF("schedstat", S_IRUGO, proc_pid_schedstat), 305828a6d671SEric W. Biederman #endif 30599745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3060631f9c18SAlexey Dobriyan REG("latency", S_IRUGO, proc_lstats_operations), 30619745512cSArjan van de Ven #endif 30628793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 3063631f9c18SAlexey Dobriyan REG("cpuset", S_IRUGO, proc_cpuset_operations), 306428a6d671SEric W. Biederman #endif 3065a424316cSPaul Menage #ifdef CONFIG_CGROUPS 3066631f9c18SAlexey Dobriyan REG("cgroup", S_IRUGO, proc_cgroup_operations), 3067a424316cSPaul Menage #endif 3068631f9c18SAlexey Dobriyan INF("oom_score", S_IRUGO, proc_oom_score), 3069631f9c18SAlexey Dobriyan REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations), 3070a63d83f4SDavid Rientjes REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), 307128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 3072631f9c18SAlexey Dobriyan REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), 307326ec3c64SAl Viro REG("sessionid", S_IRUGO, proc_sessionid_operations), 307428a6d671SEric W. Biederman #endif 3075f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 3076631f9c18SAlexey Dobriyan REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), 3077f4f154fdSAkinobu Mita #endif 3078297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 30791d1221f3SVasiliy Kulikov INF("io", S_IRUSR, proc_tid_io_accounting), 3080297c5d92SAndrea Righi #endif 3081f133eccaSChris Metcalf #ifdef CONFIG_HARDWALL 3082f133eccaSChris Metcalf INF("hardwall", S_IRUGO, proc_pid_hardwall), 3083f133eccaSChris Metcalf #endif 308422d917d8SEric W. Biederman #ifdef CONFIG_USER_NS 308522d917d8SEric W. Biederman REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), 308622d917d8SEric W. Biederman REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), 308722d917d8SEric W. Biederman #endif 308828a6d671SEric W. Biederman }; 308928a6d671SEric W. Biederman 309028a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 309128a6d671SEric W. Biederman void * dirent, filldir_t filldir) 309228a6d671SEric W. Biederman { 309328a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 309428a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 309528a6d671SEric W. Biederman } 309628a6d671SEric W. Biederman 309700cd8dd3SAl Viro static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 309800cd8dd3SAl Viro { 30997bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 31007bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 310128a6d671SEric W. Biederman } 310228a6d671SEric W. Biederman 310300977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 310428a6d671SEric W. Biederman .read = generic_read_dir, 310528a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 31066038f373SArnd Bergmann .llseek = default_llseek, 310728a6d671SEric W. Biederman }; 310828a6d671SEric W. Biederman 3109c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 311028a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 311128a6d671SEric W. Biederman .getattr = pid_getattr, 311228a6d671SEric W. Biederman .setattr = proc_setattr, 311328a6d671SEric W. Biederman }; 311428a6d671SEric W. Biederman 3115444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 3116c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 3117444ceed8SEric W. Biederman { 3118444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 3119444ceed8SEric W. Biederman struct inode *inode; 312061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 3121444ceed8SEric W. Biederman 3122444ceed8SEric W. Biederman if (!inode) 3123444ceed8SEric W. Biederman goto out; 3124444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 3125444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 3126444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 3127444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 3128aed54175SVegard Nossum 3129bfe86848SMiklos Szeredi set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff, 3130bfe86848SMiklos Szeredi ARRAY_SIZE(tid_base_stuff))); 3131444ceed8SEric W. Biederman 3132fb045adbSNick Piggin d_set_d_op(dentry, &pid_dentry_operations); 3133444ceed8SEric W. Biederman 3134444ceed8SEric W. Biederman d_add(dentry, inode); 3135444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 31360b728e19SAl Viro if (pid_revalidate(dentry, 0)) 3137444ceed8SEric W. Biederman error = NULL; 3138444ceed8SEric W. Biederman out: 3139444ceed8SEric W. Biederman return error; 3140444ceed8SEric W. Biederman } 3141444ceed8SEric W. Biederman 314200cd8dd3SAl Viro static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) 314328a6d671SEric W. Biederman { 314428a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 314528a6d671SEric W. Biederman struct task_struct *task; 314628a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 314728a6d671SEric W. Biederman unsigned tid; 3148b488893aSPavel Emelyanov struct pid_namespace *ns; 314928a6d671SEric W. Biederman 315028a6d671SEric W. Biederman if (!leader) 315128a6d671SEric W. Biederman goto out_no_task; 315228a6d671SEric W. Biederman 315328a6d671SEric W. Biederman tid = name_to_int(dentry); 315428a6d671SEric W. Biederman if (tid == ~0U) 315528a6d671SEric W. Biederman goto out; 315628a6d671SEric W. Biederman 3157b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 315828a6d671SEric W. Biederman rcu_read_lock(); 3159b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 316028a6d671SEric W. Biederman if (task) 316128a6d671SEric W. Biederman get_task_struct(task); 316228a6d671SEric W. Biederman rcu_read_unlock(); 316328a6d671SEric W. Biederman if (!task) 316428a6d671SEric W. Biederman goto out; 3165bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 316628a6d671SEric W. Biederman goto out_drop_task; 316728a6d671SEric W. Biederman 3168444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 316928a6d671SEric W. Biederman out_drop_task: 317028a6d671SEric W. Biederman put_task_struct(task); 317128a6d671SEric W. Biederman out: 317228a6d671SEric W. Biederman put_task_struct(leader); 317328a6d671SEric W. Biederman out_no_task: 317428a6d671SEric W. Biederman return result; 317528a6d671SEric W. Biederman } 317628a6d671SEric W. Biederman 317728a6d671SEric W. Biederman /* 31780bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 31790bc58a91SEric W. Biederman * 31800bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 31810bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 31820bc58a91SEric W. Biederman * directory we have more work todo. 31830bc58a91SEric W. Biederman * 31840bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 31850bc58a91SEric W. Biederman * 31860bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 31870bc58a91SEric W. Biederman * threads past it. 31880bc58a91SEric W. Biederman */ 3189cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 3190b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 31910bc58a91SEric W. Biederman { 3192a872ff0cSOleg Nesterov struct task_struct *pos; 31930bc58a91SEric W. Biederman 3194cc288738SEric W. Biederman rcu_read_lock(); 31950bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 31960bc58a91SEric W. Biederman if (tid && (nr > 0)) { 3197b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 3198a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 3199a872ff0cSOleg Nesterov goto found; 32000bc58a91SEric W. Biederman } 32010bc58a91SEric W. Biederman 32020bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 32030bc58a91SEric W. Biederman pos = NULL; 3204a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 3205a872ff0cSOleg Nesterov goto out; 3206a872ff0cSOleg Nesterov 3207a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 3208a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 3209a872ff0cSOleg Nesterov */ 3210a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 3211a872ff0cSOleg Nesterov pos = next_thread(pos); 3212a872ff0cSOleg Nesterov if (pos == leader) { 3213a872ff0cSOleg Nesterov pos = NULL; 3214a872ff0cSOleg Nesterov goto out; 3215a872ff0cSOleg Nesterov } 3216a872ff0cSOleg Nesterov } 3217a872ff0cSOleg Nesterov found: 3218a872ff0cSOleg Nesterov get_task_struct(pos); 3219a872ff0cSOleg Nesterov out: 3220cc288738SEric W. Biederman rcu_read_unlock(); 32210bc58a91SEric W. Biederman return pos; 32220bc58a91SEric W. Biederman } 32230bc58a91SEric W. Biederman 32240bc58a91SEric W. Biederman /* 32250bc58a91SEric W. Biederman * Find the next thread in the thread list. 32260bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 32270bc58a91SEric W. Biederman * 32280bc58a91SEric W. Biederman * The reference to the input task_struct is released. 32290bc58a91SEric W. Biederman */ 32300bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 32310bc58a91SEric W. Biederman { 3232c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3233cc288738SEric W. Biederman rcu_read_lock(); 3234c1df7fb8SOleg Nesterov if (pid_alive(start)) { 32350bc58a91SEric W. Biederman pos = next_thread(start); 3236c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 32370bc58a91SEric W. Biederman pos = NULL; 3238c1df7fb8SOleg Nesterov else 3239c1df7fb8SOleg Nesterov get_task_struct(pos); 3240c1df7fb8SOleg Nesterov } 3241cc288738SEric W. Biederman rcu_read_unlock(); 32420bc58a91SEric W. Biederman put_task_struct(start); 32430bc58a91SEric W. Biederman return pos; 32440bc58a91SEric W. Biederman } 32450bc58a91SEric W. Biederman 324661a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 324761a28784SEric W. Biederman struct task_struct *task, int tid) 324861a28784SEric W. Biederman { 324961a28784SEric W. Biederman char name[PROC_NUMBUF]; 325061a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 325161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 325261a28784SEric W. Biederman proc_task_instantiate, task, NULL); 325361a28784SEric W. Biederman } 325461a28784SEric W. Biederman 32551da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 32561da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 32571da177e4SLinus Torvalds { 32582fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 32591da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 32607d895244SGuillaume Chazarain struct task_struct *leader = NULL; 32610bc58a91SEric W. Biederman struct task_struct *task; 32621da177e4SLinus Torvalds int retval = -ENOENT; 32631da177e4SLinus Torvalds ino_t ino; 32640bc58a91SEric W. Biederman int tid; 3265b488893aSPavel Emelyanov struct pid_namespace *ns; 32661da177e4SLinus Torvalds 32677d895244SGuillaume Chazarain task = get_proc_task(inode); 32687d895244SGuillaume Chazarain if (!task) 32697d895244SGuillaume Chazarain goto out_no_task; 32707d895244SGuillaume Chazarain rcu_read_lock(); 32717d895244SGuillaume Chazarain if (pid_alive(task)) { 32727d895244SGuillaume Chazarain leader = task->group_leader; 32737d895244SGuillaume Chazarain get_task_struct(leader); 32747d895244SGuillaume Chazarain } 32757d895244SGuillaume Chazarain rcu_read_unlock(); 32767d895244SGuillaume Chazarain put_task_struct(task); 327799f89551SEric W. Biederman if (!leader) 327899f89551SEric W. Biederman goto out_no_task; 32791da177e4SLinus Torvalds retval = 0; 32801da177e4SLinus Torvalds 3281ee568b25SLinus Torvalds switch ((unsigned long)filp->f_pos) { 32821da177e4SLinus Torvalds case 0: 32831da177e4SLinus Torvalds ino = inode->i_ino; 3284ee6f779bSZhang Le if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0) 32851da177e4SLinus Torvalds goto out; 3286ee6f779bSZhang Le filp->f_pos++; 32871da177e4SLinus Torvalds /* fall through */ 32881da177e4SLinus Torvalds case 1: 32891da177e4SLinus Torvalds ino = parent_ino(dentry); 3290ee6f779bSZhang Le if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0) 32911da177e4SLinus Torvalds goto out; 3292ee6f779bSZhang Le filp->f_pos++; 32931da177e4SLinus Torvalds /* fall through */ 32941da177e4SLinus Torvalds } 32951da177e4SLinus Torvalds 32960bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 32970bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 32980bc58a91SEric W. Biederman */ 3299b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 33002b47c361SMathieu Desnoyers tid = (int)filp->f_version; 33010bc58a91SEric W. Biederman filp->f_version = 0; 3302ee6f779bSZhang Le for (task = first_tid(leader, tid, filp->f_pos - 2, ns); 33030bc58a91SEric W. Biederman task; 3304ee6f779bSZhang Le task = next_tid(task), filp->f_pos++) { 3305b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 330661a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 33070bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 33080bc58a91SEric W. Biederman * pid for the next readir call */ 33092b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 33100bc58a91SEric W. Biederman put_task_struct(task); 33111da177e4SLinus Torvalds break; 33120bc58a91SEric W. Biederman } 33131da177e4SLinus Torvalds } 33141da177e4SLinus Torvalds out: 331599f89551SEric W. Biederman put_task_struct(leader); 331699f89551SEric W. Biederman out_no_task: 33171da177e4SLinus Torvalds return retval; 33181da177e4SLinus Torvalds } 33196e66b52bSEric W. Biederman 33206e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 33216e66b52bSEric W. Biederman { 33226e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 332399f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 33246e66b52bSEric W. Biederman generic_fillattr(inode, stat); 33256e66b52bSEric W. Biederman 332699f89551SEric W. Biederman if (p) { 332799f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 332899f89551SEric W. Biederman put_task_struct(p); 33296e66b52bSEric W. Biederman } 33306e66b52bSEric W. Biederman 33316e66b52bSEric W. Biederman return 0; 33326e66b52bSEric W. Biederman } 333328a6d671SEric W. Biederman 3334c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 333528a6d671SEric W. Biederman .lookup = proc_task_lookup, 333628a6d671SEric W. Biederman .getattr = proc_task_getattr, 333728a6d671SEric W. Biederman .setattr = proc_setattr, 33380499680aSVasiliy Kulikov .permission = proc_pid_permission, 333928a6d671SEric W. Biederman }; 334028a6d671SEric W. Biederman 334100977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 334228a6d671SEric W. Biederman .read = generic_read_dir, 334328a6d671SEric W. Biederman .readdir = proc_task_readdir, 33446038f373SArnd Bergmann .llseek = default_llseek, 334528a6d671SEric W. Biederman }; 3346