1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * linux/fs/proc/inode.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1991, 1992 Linus Torvalds
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/cache.h>
9*4882a593Smuzhiyun #include <linux/time.h>
10*4882a593Smuzhiyun #include <linux/proc_fs.h>
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/pid_namespace.h>
13*4882a593Smuzhiyun #include <linux/mm.h>
14*4882a593Smuzhiyun #include <linux/string.h>
15*4882a593Smuzhiyun #include <linux/stat.h>
16*4882a593Smuzhiyun #include <linux/completion.h>
17*4882a593Smuzhiyun #include <linux/poll.h>
18*4882a593Smuzhiyun #include <linux/printk.h>
19*4882a593Smuzhiyun #include <linux/file.h>
20*4882a593Smuzhiyun #include <linux/limits.h>
21*4882a593Smuzhiyun #include <linux/init.h>
22*4882a593Smuzhiyun #include <linux/module.h>
23*4882a593Smuzhiyun #include <linux/sysctl.h>
24*4882a593Smuzhiyun #include <linux/seq_file.h>
25*4882a593Smuzhiyun #include <linux/slab.h>
26*4882a593Smuzhiyun #include <linux/mount.h>
27*4882a593Smuzhiyun #include <linux/bug.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include <linux/uaccess.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include "internal.h"
32*4882a593Smuzhiyun
proc_evict_inode(struct inode * inode)33*4882a593Smuzhiyun static void proc_evict_inode(struct inode *inode)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun struct proc_dir_entry *de;
36*4882a593Smuzhiyun struct ctl_table_header *head;
37*4882a593Smuzhiyun struct proc_inode *ei = PROC_I(inode);
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun truncate_inode_pages_final(&inode->i_data);
40*4882a593Smuzhiyun clear_inode(inode);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* Stop tracking associated processes */
43*4882a593Smuzhiyun if (ei->pid) {
44*4882a593Smuzhiyun proc_pid_evict_inode(ei);
45*4882a593Smuzhiyun ei->pid = NULL;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /* Let go of any associated proc directory entry */
49*4882a593Smuzhiyun de = ei->pde;
50*4882a593Smuzhiyun if (de) {
51*4882a593Smuzhiyun pde_put(de);
52*4882a593Smuzhiyun ei->pde = NULL;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun head = ei->sysctl;
56*4882a593Smuzhiyun if (head) {
57*4882a593Smuzhiyun RCU_INIT_POINTER(ei->sysctl, NULL);
58*4882a593Smuzhiyun proc_sys_evict_inode(inode, head);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun static struct kmem_cache *proc_inode_cachep __ro_after_init;
63*4882a593Smuzhiyun static struct kmem_cache *pde_opener_cache __ro_after_init;
64*4882a593Smuzhiyun
proc_alloc_inode(struct super_block * sb)65*4882a593Smuzhiyun static struct inode *proc_alloc_inode(struct super_block *sb)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun struct proc_inode *ei;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun ei = kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
70*4882a593Smuzhiyun if (!ei)
71*4882a593Smuzhiyun return NULL;
72*4882a593Smuzhiyun ei->pid = NULL;
73*4882a593Smuzhiyun ei->fd = 0;
74*4882a593Smuzhiyun ei->op.proc_get_link = NULL;
75*4882a593Smuzhiyun ei->pde = NULL;
76*4882a593Smuzhiyun ei->sysctl = NULL;
77*4882a593Smuzhiyun ei->sysctl_entry = NULL;
78*4882a593Smuzhiyun INIT_HLIST_NODE(&ei->sibling_inodes);
79*4882a593Smuzhiyun ei->ns_ops = NULL;
80*4882a593Smuzhiyun return &ei->vfs_inode;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
proc_free_inode(struct inode * inode)83*4882a593Smuzhiyun static void proc_free_inode(struct inode *inode)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun kmem_cache_free(proc_inode_cachep, PROC_I(inode));
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
init_once(void * foo)88*4882a593Smuzhiyun static void init_once(void *foo)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun struct proc_inode *ei = (struct proc_inode *) foo;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun inode_init_once(&ei->vfs_inode);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
proc_init_kmemcache(void)95*4882a593Smuzhiyun void __init proc_init_kmemcache(void)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun proc_inode_cachep = kmem_cache_create("proc_inode_cache",
98*4882a593Smuzhiyun sizeof(struct proc_inode),
99*4882a593Smuzhiyun 0, (SLAB_RECLAIM_ACCOUNT|
100*4882a593Smuzhiyun SLAB_MEM_SPREAD|SLAB_ACCOUNT|
101*4882a593Smuzhiyun SLAB_PANIC),
102*4882a593Smuzhiyun init_once);
103*4882a593Smuzhiyun pde_opener_cache =
104*4882a593Smuzhiyun kmem_cache_create("pde_opener", sizeof(struct pde_opener), 0,
105*4882a593Smuzhiyun SLAB_ACCOUNT|SLAB_PANIC, NULL);
106*4882a593Smuzhiyun proc_dir_entry_cache = kmem_cache_create_usercopy(
107*4882a593Smuzhiyun "proc_dir_entry", SIZEOF_PDE, 0, SLAB_PANIC,
108*4882a593Smuzhiyun offsetof(struct proc_dir_entry, inline_name),
109*4882a593Smuzhiyun SIZEOF_PDE_INLINE_NAME, NULL);
110*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct proc_dir_entry) >= SIZEOF_PDE);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
proc_invalidate_siblings_dcache(struct hlist_head * inodes,spinlock_t * lock)113*4882a593Smuzhiyun void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun struct inode *inode;
116*4882a593Smuzhiyun struct proc_inode *ei;
117*4882a593Smuzhiyun struct hlist_node *node;
118*4882a593Smuzhiyun struct super_block *old_sb = NULL;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun rcu_read_lock();
121*4882a593Smuzhiyun for (;;) {
122*4882a593Smuzhiyun struct super_block *sb;
123*4882a593Smuzhiyun node = hlist_first_rcu(inodes);
124*4882a593Smuzhiyun if (!node)
125*4882a593Smuzhiyun break;
126*4882a593Smuzhiyun ei = hlist_entry(node, struct proc_inode, sibling_inodes);
127*4882a593Smuzhiyun spin_lock(lock);
128*4882a593Smuzhiyun hlist_del_init_rcu(&ei->sibling_inodes);
129*4882a593Smuzhiyun spin_unlock(lock);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun inode = &ei->vfs_inode;
132*4882a593Smuzhiyun sb = inode->i_sb;
133*4882a593Smuzhiyun if ((sb != old_sb) && !atomic_inc_not_zero(&sb->s_active))
134*4882a593Smuzhiyun continue;
135*4882a593Smuzhiyun inode = igrab(inode);
136*4882a593Smuzhiyun rcu_read_unlock();
137*4882a593Smuzhiyun if (sb != old_sb) {
138*4882a593Smuzhiyun if (old_sb)
139*4882a593Smuzhiyun deactivate_super(old_sb);
140*4882a593Smuzhiyun old_sb = sb;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun if (unlikely(!inode)) {
143*4882a593Smuzhiyun rcu_read_lock();
144*4882a593Smuzhiyun continue;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun if (S_ISDIR(inode->i_mode)) {
148*4882a593Smuzhiyun struct dentry *dir = d_find_any_alias(inode);
149*4882a593Smuzhiyun if (dir) {
150*4882a593Smuzhiyun d_invalidate(dir);
151*4882a593Smuzhiyun dput(dir);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun } else {
154*4882a593Smuzhiyun struct dentry *dentry;
155*4882a593Smuzhiyun while ((dentry = d_find_alias(inode))) {
156*4882a593Smuzhiyun d_invalidate(dentry);
157*4882a593Smuzhiyun dput(dentry);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun iput(inode);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun rcu_read_lock();
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun rcu_read_unlock();
165*4882a593Smuzhiyun if (old_sb)
166*4882a593Smuzhiyun deactivate_super(old_sb);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
hidepid2str(enum proc_hidepid v)169*4882a593Smuzhiyun static inline const char *hidepid2str(enum proc_hidepid v)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun switch (v) {
172*4882a593Smuzhiyun case HIDEPID_OFF: return "off";
173*4882a593Smuzhiyun case HIDEPID_NO_ACCESS: return "noaccess";
174*4882a593Smuzhiyun case HIDEPID_INVISIBLE: return "invisible";
175*4882a593Smuzhiyun case HIDEPID_NOT_PTRACEABLE: return "ptraceable";
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun WARN_ONCE(1, "bad hide_pid value: %d\n", v);
178*4882a593Smuzhiyun return "unknown";
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
proc_show_options(struct seq_file * seq,struct dentry * root)181*4882a593Smuzhiyun static int proc_show_options(struct seq_file *seq, struct dentry *root)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun struct proc_fs_info *fs_info = proc_sb_info(root->d_sb);
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun if (!gid_eq(fs_info->pid_gid, GLOBAL_ROOT_GID))
186*4882a593Smuzhiyun seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, fs_info->pid_gid));
187*4882a593Smuzhiyun if (fs_info->hide_pid != HIDEPID_OFF)
188*4882a593Smuzhiyun seq_printf(seq, ",hidepid=%s", hidepid2str(fs_info->hide_pid));
189*4882a593Smuzhiyun if (fs_info->pidonly != PROC_PIDONLY_OFF)
190*4882a593Smuzhiyun seq_printf(seq, ",subset=pid");
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun return 0;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun const struct super_operations proc_sops = {
196*4882a593Smuzhiyun .alloc_inode = proc_alloc_inode,
197*4882a593Smuzhiyun .free_inode = proc_free_inode,
198*4882a593Smuzhiyun .drop_inode = generic_delete_inode,
199*4882a593Smuzhiyun .evict_inode = proc_evict_inode,
200*4882a593Smuzhiyun .statfs = simple_statfs,
201*4882a593Smuzhiyun .show_options = proc_show_options,
202*4882a593Smuzhiyun };
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun enum {BIAS = -1U<<31};
205*4882a593Smuzhiyun
use_pde(struct proc_dir_entry * pde)206*4882a593Smuzhiyun static inline int use_pde(struct proc_dir_entry *pde)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun return likely(atomic_inc_unless_negative(&pde->in_use));
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
unuse_pde(struct proc_dir_entry * pde)211*4882a593Smuzhiyun static void unuse_pde(struct proc_dir_entry *pde)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun if (unlikely(atomic_dec_return(&pde->in_use) == BIAS))
214*4882a593Smuzhiyun complete(pde->pde_unload_completion);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /* pde is locked on entry, unlocked on exit */
close_pdeo(struct proc_dir_entry * pde,struct pde_opener * pdeo)218*4882a593Smuzhiyun static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
219*4882a593Smuzhiyun __releases(&pde->pde_unload_lock)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun * close() (proc_reg_release()) can't delete an entry and proceed:
223*4882a593Smuzhiyun * ->release hook needs to be available at the right moment.
224*4882a593Smuzhiyun *
225*4882a593Smuzhiyun * rmmod (remove_proc_entry() et al) can't delete an entry and proceed:
226*4882a593Smuzhiyun * "struct file" needs to be available at the right moment.
227*4882a593Smuzhiyun *
228*4882a593Smuzhiyun * Therefore, first process to enter this function does ->release() and
229*4882a593Smuzhiyun * signals its completion to the other process which does nothing.
230*4882a593Smuzhiyun */
231*4882a593Smuzhiyun if (pdeo->closing) {
232*4882a593Smuzhiyun /* somebody else is doing that, just wait */
233*4882a593Smuzhiyun DECLARE_COMPLETION_ONSTACK(c);
234*4882a593Smuzhiyun pdeo->c = &c;
235*4882a593Smuzhiyun spin_unlock(&pde->pde_unload_lock);
236*4882a593Smuzhiyun wait_for_completion(&c);
237*4882a593Smuzhiyun } else {
238*4882a593Smuzhiyun struct file *file;
239*4882a593Smuzhiyun struct completion *c;
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun pdeo->closing = true;
242*4882a593Smuzhiyun spin_unlock(&pde->pde_unload_lock);
243*4882a593Smuzhiyun file = pdeo->file;
244*4882a593Smuzhiyun pde->proc_ops->proc_release(file_inode(file), file);
245*4882a593Smuzhiyun spin_lock(&pde->pde_unload_lock);
246*4882a593Smuzhiyun /* After ->release. */
247*4882a593Smuzhiyun list_del(&pdeo->lh);
248*4882a593Smuzhiyun c = pdeo->c;
249*4882a593Smuzhiyun spin_unlock(&pde->pde_unload_lock);
250*4882a593Smuzhiyun if (unlikely(c))
251*4882a593Smuzhiyun complete(c);
252*4882a593Smuzhiyun kmem_cache_free(pde_opener_cache, pdeo);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun
proc_entry_rundown(struct proc_dir_entry * de)256*4882a593Smuzhiyun void proc_entry_rundown(struct proc_dir_entry *de)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun DECLARE_COMPLETION_ONSTACK(c);
259*4882a593Smuzhiyun /* Wait until all existing callers into module are done. */
260*4882a593Smuzhiyun de->pde_unload_completion = &c;
261*4882a593Smuzhiyun if (atomic_add_return(BIAS, &de->in_use) != BIAS)
262*4882a593Smuzhiyun wait_for_completion(&c);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /* ->pde_openers list can't grow from now on. */
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun spin_lock(&de->pde_unload_lock);
267*4882a593Smuzhiyun while (!list_empty(&de->pde_openers)) {
268*4882a593Smuzhiyun struct pde_opener *pdeo;
269*4882a593Smuzhiyun pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
270*4882a593Smuzhiyun close_pdeo(de, pdeo);
271*4882a593Smuzhiyun spin_lock(&de->pde_unload_lock);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun spin_unlock(&de->pde_unload_lock);
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
pde_lseek(struct proc_dir_entry * pde,struct file * file,loff_t offset,int whence)276*4882a593Smuzhiyun static loff_t pde_lseek(struct proc_dir_entry *pde, struct file *file, loff_t offset, int whence)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun typeof_member(struct proc_ops, proc_lseek) lseek;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun lseek = pde->proc_ops->proc_lseek;
281*4882a593Smuzhiyun if (!lseek)
282*4882a593Smuzhiyun lseek = default_llseek;
283*4882a593Smuzhiyun return lseek(file, offset, whence);
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
proc_reg_llseek(struct file * file,loff_t offset,int whence)286*4882a593Smuzhiyun static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(file_inode(file));
289*4882a593Smuzhiyun loff_t rv = -EINVAL;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun if (pde_is_permanent(pde)) {
292*4882a593Smuzhiyun return pde_lseek(pde, file, offset, whence);
293*4882a593Smuzhiyun } else if (use_pde(pde)) {
294*4882a593Smuzhiyun rv = pde_lseek(pde, file, offset, whence);
295*4882a593Smuzhiyun unuse_pde(pde);
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun return rv;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
proc_reg_read_iter(struct kiocb * iocb,struct iov_iter * iter)300*4882a593Smuzhiyun static ssize_t proc_reg_read_iter(struct kiocb *iocb, struct iov_iter *iter)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(file_inode(iocb->ki_filp));
303*4882a593Smuzhiyun ssize_t ret;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun if (pde_is_permanent(pde))
306*4882a593Smuzhiyun return pde->proc_ops->proc_read_iter(iocb, iter);
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun if (!use_pde(pde))
309*4882a593Smuzhiyun return -EIO;
310*4882a593Smuzhiyun ret = pde->proc_ops->proc_read_iter(iocb, iter);
311*4882a593Smuzhiyun unuse_pde(pde);
312*4882a593Smuzhiyun return ret;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
pde_read(struct proc_dir_entry * pde,struct file * file,char __user * buf,size_t count,loff_t * ppos)315*4882a593Smuzhiyun static ssize_t pde_read(struct proc_dir_entry *pde, struct file *file, char __user *buf, size_t count, loff_t *ppos)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun typeof_member(struct proc_ops, proc_read) read;
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun read = pde->proc_ops->proc_read;
320*4882a593Smuzhiyun if (read)
321*4882a593Smuzhiyun return read(file, buf, count, ppos);
322*4882a593Smuzhiyun return -EIO;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
proc_reg_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)325*4882a593Smuzhiyun static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(file_inode(file));
328*4882a593Smuzhiyun ssize_t rv = -EIO;
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun if (pde_is_permanent(pde)) {
331*4882a593Smuzhiyun return pde_read(pde, file, buf, count, ppos);
332*4882a593Smuzhiyun } else if (use_pde(pde)) {
333*4882a593Smuzhiyun rv = pde_read(pde, file, buf, count, ppos);
334*4882a593Smuzhiyun unuse_pde(pde);
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun return rv;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun
pde_write(struct proc_dir_entry * pde,struct file * file,const char __user * buf,size_t count,loff_t * ppos)339*4882a593Smuzhiyun static ssize_t pde_write(struct proc_dir_entry *pde, struct file *file, const char __user *buf, size_t count, loff_t *ppos)
340*4882a593Smuzhiyun {
341*4882a593Smuzhiyun typeof_member(struct proc_ops, proc_write) write;
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun write = pde->proc_ops->proc_write;
344*4882a593Smuzhiyun if (write)
345*4882a593Smuzhiyun return write(file, buf, count, ppos);
346*4882a593Smuzhiyun return -EIO;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun
proc_reg_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)349*4882a593Smuzhiyun static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(file_inode(file));
352*4882a593Smuzhiyun ssize_t rv = -EIO;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun if (pde_is_permanent(pde)) {
355*4882a593Smuzhiyun return pde_write(pde, file, buf, count, ppos);
356*4882a593Smuzhiyun } else if (use_pde(pde)) {
357*4882a593Smuzhiyun rv = pde_write(pde, file, buf, count, ppos);
358*4882a593Smuzhiyun unuse_pde(pde);
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun return rv;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun
pde_poll(struct proc_dir_entry * pde,struct file * file,struct poll_table_struct * pts)363*4882a593Smuzhiyun static __poll_t pde_poll(struct proc_dir_entry *pde, struct file *file, struct poll_table_struct *pts)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun typeof_member(struct proc_ops, proc_poll) poll;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun poll = pde->proc_ops->proc_poll;
368*4882a593Smuzhiyun if (poll)
369*4882a593Smuzhiyun return poll(file, pts);
370*4882a593Smuzhiyun return DEFAULT_POLLMASK;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
proc_reg_poll(struct file * file,struct poll_table_struct * pts)373*4882a593Smuzhiyun static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(file_inode(file));
376*4882a593Smuzhiyun __poll_t rv = DEFAULT_POLLMASK;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun if (pde_is_permanent(pde)) {
379*4882a593Smuzhiyun return pde_poll(pde, file, pts);
380*4882a593Smuzhiyun } else if (use_pde(pde)) {
381*4882a593Smuzhiyun rv = pde_poll(pde, file, pts);
382*4882a593Smuzhiyun unuse_pde(pde);
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun return rv;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun
pde_ioctl(struct proc_dir_entry * pde,struct file * file,unsigned int cmd,unsigned long arg)387*4882a593Smuzhiyun static long pde_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned int cmd, unsigned long arg)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun typeof_member(struct proc_ops, proc_ioctl) ioctl;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun ioctl = pde->proc_ops->proc_ioctl;
392*4882a593Smuzhiyun if (ioctl)
393*4882a593Smuzhiyun return ioctl(file, cmd, arg);
394*4882a593Smuzhiyun return -ENOTTY;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
proc_reg_unlocked_ioctl(struct file * file,unsigned int cmd,unsigned long arg)397*4882a593Smuzhiyun static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(file_inode(file));
400*4882a593Smuzhiyun long rv = -ENOTTY;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun if (pde_is_permanent(pde)) {
403*4882a593Smuzhiyun return pde_ioctl(pde, file, cmd, arg);
404*4882a593Smuzhiyun } else if (use_pde(pde)) {
405*4882a593Smuzhiyun rv = pde_ioctl(pde, file, cmd, arg);
406*4882a593Smuzhiyun unuse_pde(pde);
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun return rv;
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
pde_compat_ioctl(struct proc_dir_entry * pde,struct file * file,unsigned int cmd,unsigned long arg)412*4882a593Smuzhiyun static long pde_compat_ioctl(struct proc_dir_entry *pde, struct file *file, unsigned int cmd, unsigned long arg)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun typeof_member(struct proc_ops, proc_compat_ioctl) compat_ioctl;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun compat_ioctl = pde->proc_ops->proc_compat_ioctl;
417*4882a593Smuzhiyun if (compat_ioctl)
418*4882a593Smuzhiyun return compat_ioctl(file, cmd, arg);
419*4882a593Smuzhiyun return -ENOTTY;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
proc_reg_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)422*4882a593Smuzhiyun static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(file_inode(file));
425*4882a593Smuzhiyun long rv = -ENOTTY;
426*4882a593Smuzhiyun if (pde_is_permanent(pde)) {
427*4882a593Smuzhiyun return pde_compat_ioctl(pde, file, cmd, arg);
428*4882a593Smuzhiyun } else if (use_pde(pde)) {
429*4882a593Smuzhiyun rv = pde_compat_ioctl(pde, file, cmd, arg);
430*4882a593Smuzhiyun unuse_pde(pde);
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun return rv;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun #endif
435*4882a593Smuzhiyun
pde_mmap(struct proc_dir_entry * pde,struct file * file,struct vm_area_struct * vma)436*4882a593Smuzhiyun static int pde_mmap(struct proc_dir_entry *pde, struct file *file, struct vm_area_struct *vma)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun typeof_member(struct proc_ops, proc_mmap) mmap;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun mmap = pde->proc_ops->proc_mmap;
441*4882a593Smuzhiyun if (mmap)
442*4882a593Smuzhiyun return mmap(file, vma);
443*4882a593Smuzhiyun return -EIO;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun
proc_reg_mmap(struct file * file,struct vm_area_struct * vma)446*4882a593Smuzhiyun static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(file_inode(file));
449*4882a593Smuzhiyun int rv = -EIO;
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun if (pde_is_permanent(pde)) {
452*4882a593Smuzhiyun return pde_mmap(pde, file, vma);
453*4882a593Smuzhiyun } else if (use_pde(pde)) {
454*4882a593Smuzhiyun rv = pde_mmap(pde, file, vma);
455*4882a593Smuzhiyun unuse_pde(pde);
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun return rv;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun static unsigned long
pde_get_unmapped_area(struct proc_dir_entry * pde,struct file * file,unsigned long orig_addr,unsigned long len,unsigned long pgoff,unsigned long flags)461*4882a593Smuzhiyun pde_get_unmapped_area(struct proc_dir_entry *pde, struct file *file, unsigned long orig_addr,
462*4882a593Smuzhiyun unsigned long len, unsigned long pgoff,
463*4882a593Smuzhiyun unsigned long flags)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun typeof_member(struct proc_ops, proc_get_unmapped_area) get_area;
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun get_area = pde->proc_ops->proc_get_unmapped_area;
468*4882a593Smuzhiyun #ifdef CONFIG_MMU
469*4882a593Smuzhiyun if (!get_area)
470*4882a593Smuzhiyun get_area = current->mm->get_unmapped_area;
471*4882a593Smuzhiyun #endif
472*4882a593Smuzhiyun if (get_area)
473*4882a593Smuzhiyun return get_area(file, orig_addr, len, pgoff, flags);
474*4882a593Smuzhiyun return orig_addr;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun static unsigned long
proc_reg_get_unmapped_area(struct file * file,unsigned long orig_addr,unsigned long len,unsigned long pgoff,unsigned long flags)478*4882a593Smuzhiyun proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
479*4882a593Smuzhiyun unsigned long len, unsigned long pgoff,
480*4882a593Smuzhiyun unsigned long flags)
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(file_inode(file));
483*4882a593Smuzhiyun unsigned long rv = -EIO;
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun if (pde_is_permanent(pde)) {
486*4882a593Smuzhiyun return pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags);
487*4882a593Smuzhiyun } else if (use_pde(pde)) {
488*4882a593Smuzhiyun rv = pde_get_unmapped_area(pde, file, orig_addr, len, pgoff, flags);
489*4882a593Smuzhiyun unuse_pde(pde);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun return rv;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
proc_reg_open(struct inode * inode,struct file * file)494*4882a593Smuzhiyun static int proc_reg_open(struct inode *inode, struct file *file)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
497*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(inode);
498*4882a593Smuzhiyun int rv = 0;
499*4882a593Smuzhiyun typeof_member(struct proc_ops, proc_open) open;
500*4882a593Smuzhiyun typeof_member(struct proc_ops, proc_release) release;
501*4882a593Smuzhiyun struct pde_opener *pdeo;
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun if (pde_is_permanent(pde)) {
504*4882a593Smuzhiyun open = pde->proc_ops->proc_open;
505*4882a593Smuzhiyun if (open)
506*4882a593Smuzhiyun rv = open(inode, file);
507*4882a593Smuzhiyun return rv;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun if (fs_info->pidonly == PROC_PIDONLY_ON)
511*4882a593Smuzhiyun return -ENOENT;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun /*
514*4882a593Smuzhiyun * Ensure that
515*4882a593Smuzhiyun * 1) PDE's ->release hook will be called no matter what
516*4882a593Smuzhiyun * either normally by close()/->release, or forcefully by
517*4882a593Smuzhiyun * rmmod/remove_proc_entry.
518*4882a593Smuzhiyun *
519*4882a593Smuzhiyun * 2) rmmod isn't blocked by opening file in /proc and sitting on
520*4882a593Smuzhiyun * the descriptor (including "rmmod foo </proc/foo" scenario).
521*4882a593Smuzhiyun *
522*4882a593Smuzhiyun * Save every "struct file" with custom ->release hook.
523*4882a593Smuzhiyun */
524*4882a593Smuzhiyun if (!use_pde(pde))
525*4882a593Smuzhiyun return -ENOENT;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun release = pde->proc_ops->proc_release;
528*4882a593Smuzhiyun if (release) {
529*4882a593Smuzhiyun pdeo = kmem_cache_alloc(pde_opener_cache, GFP_KERNEL);
530*4882a593Smuzhiyun if (!pdeo) {
531*4882a593Smuzhiyun rv = -ENOMEM;
532*4882a593Smuzhiyun goto out_unuse;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun open = pde->proc_ops->proc_open;
537*4882a593Smuzhiyun if (open)
538*4882a593Smuzhiyun rv = open(inode, file);
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun if (release) {
541*4882a593Smuzhiyun if (rv == 0) {
542*4882a593Smuzhiyun /* To know what to release. */
543*4882a593Smuzhiyun pdeo->file = file;
544*4882a593Smuzhiyun pdeo->closing = false;
545*4882a593Smuzhiyun pdeo->c = NULL;
546*4882a593Smuzhiyun spin_lock(&pde->pde_unload_lock);
547*4882a593Smuzhiyun list_add(&pdeo->lh, &pde->pde_openers);
548*4882a593Smuzhiyun spin_unlock(&pde->pde_unload_lock);
549*4882a593Smuzhiyun } else
550*4882a593Smuzhiyun kmem_cache_free(pde_opener_cache, pdeo);
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun out_unuse:
554*4882a593Smuzhiyun unuse_pde(pde);
555*4882a593Smuzhiyun return rv;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun
proc_reg_release(struct inode * inode,struct file * file)558*4882a593Smuzhiyun static int proc_reg_release(struct inode *inode, struct file *file)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(inode);
561*4882a593Smuzhiyun struct pde_opener *pdeo;
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun if (pde_is_permanent(pde)) {
564*4882a593Smuzhiyun typeof_member(struct proc_ops, proc_release) release;
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun release = pde->proc_ops->proc_release;
567*4882a593Smuzhiyun if (release) {
568*4882a593Smuzhiyun return release(inode, file);
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun return 0;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun spin_lock(&pde->pde_unload_lock);
574*4882a593Smuzhiyun list_for_each_entry(pdeo, &pde->pde_openers, lh) {
575*4882a593Smuzhiyun if (pdeo->file == file) {
576*4882a593Smuzhiyun close_pdeo(pde, pdeo);
577*4882a593Smuzhiyun return 0;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun spin_unlock(&pde->pde_unload_lock);
581*4882a593Smuzhiyun return 0;
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun static const struct file_operations proc_reg_file_ops = {
585*4882a593Smuzhiyun .llseek = proc_reg_llseek,
586*4882a593Smuzhiyun .read = proc_reg_read,
587*4882a593Smuzhiyun .write = proc_reg_write,
588*4882a593Smuzhiyun .poll = proc_reg_poll,
589*4882a593Smuzhiyun .unlocked_ioctl = proc_reg_unlocked_ioctl,
590*4882a593Smuzhiyun .mmap = proc_reg_mmap,
591*4882a593Smuzhiyun .get_unmapped_area = proc_reg_get_unmapped_area,
592*4882a593Smuzhiyun .open = proc_reg_open,
593*4882a593Smuzhiyun .release = proc_reg_release,
594*4882a593Smuzhiyun };
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun static const struct file_operations proc_iter_file_ops = {
597*4882a593Smuzhiyun .llseek = proc_reg_llseek,
598*4882a593Smuzhiyun .read_iter = proc_reg_read_iter,
599*4882a593Smuzhiyun .write = proc_reg_write,
600*4882a593Smuzhiyun .splice_read = generic_file_splice_read,
601*4882a593Smuzhiyun .poll = proc_reg_poll,
602*4882a593Smuzhiyun .unlocked_ioctl = proc_reg_unlocked_ioctl,
603*4882a593Smuzhiyun .mmap = proc_reg_mmap,
604*4882a593Smuzhiyun .get_unmapped_area = proc_reg_get_unmapped_area,
605*4882a593Smuzhiyun .open = proc_reg_open,
606*4882a593Smuzhiyun .release = proc_reg_release,
607*4882a593Smuzhiyun };
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
610*4882a593Smuzhiyun static const struct file_operations proc_reg_file_ops_compat = {
611*4882a593Smuzhiyun .llseek = proc_reg_llseek,
612*4882a593Smuzhiyun .read = proc_reg_read,
613*4882a593Smuzhiyun .write = proc_reg_write,
614*4882a593Smuzhiyun .poll = proc_reg_poll,
615*4882a593Smuzhiyun .unlocked_ioctl = proc_reg_unlocked_ioctl,
616*4882a593Smuzhiyun .compat_ioctl = proc_reg_compat_ioctl,
617*4882a593Smuzhiyun .mmap = proc_reg_mmap,
618*4882a593Smuzhiyun .get_unmapped_area = proc_reg_get_unmapped_area,
619*4882a593Smuzhiyun .open = proc_reg_open,
620*4882a593Smuzhiyun .release = proc_reg_release,
621*4882a593Smuzhiyun };
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun static const struct file_operations proc_iter_file_ops_compat = {
624*4882a593Smuzhiyun .llseek = proc_reg_llseek,
625*4882a593Smuzhiyun .read_iter = proc_reg_read_iter,
626*4882a593Smuzhiyun .splice_read = generic_file_splice_read,
627*4882a593Smuzhiyun .write = proc_reg_write,
628*4882a593Smuzhiyun .poll = proc_reg_poll,
629*4882a593Smuzhiyun .unlocked_ioctl = proc_reg_unlocked_ioctl,
630*4882a593Smuzhiyun .compat_ioctl = proc_reg_compat_ioctl,
631*4882a593Smuzhiyun .mmap = proc_reg_mmap,
632*4882a593Smuzhiyun .get_unmapped_area = proc_reg_get_unmapped_area,
633*4882a593Smuzhiyun .open = proc_reg_open,
634*4882a593Smuzhiyun .release = proc_reg_release,
635*4882a593Smuzhiyun };
636*4882a593Smuzhiyun #endif
637*4882a593Smuzhiyun
proc_put_link(void * p)638*4882a593Smuzhiyun static void proc_put_link(void *p)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun unuse_pde(p);
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun
proc_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)643*4882a593Smuzhiyun static const char *proc_get_link(struct dentry *dentry,
644*4882a593Smuzhiyun struct inode *inode,
645*4882a593Smuzhiyun struct delayed_call *done)
646*4882a593Smuzhiyun {
647*4882a593Smuzhiyun struct proc_dir_entry *pde = PDE(inode);
648*4882a593Smuzhiyun if (!use_pde(pde))
649*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
650*4882a593Smuzhiyun set_delayed_call(done, proc_put_link, pde);
651*4882a593Smuzhiyun return pde->data;
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun const struct inode_operations proc_link_inode_operations = {
655*4882a593Smuzhiyun .get_link = proc_get_link,
656*4882a593Smuzhiyun };
657*4882a593Smuzhiyun
proc_get_inode(struct super_block * sb,struct proc_dir_entry * de)658*4882a593Smuzhiyun struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
659*4882a593Smuzhiyun {
660*4882a593Smuzhiyun struct inode *inode = new_inode(sb);
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun if (!inode) {
663*4882a593Smuzhiyun pde_put(de);
664*4882a593Smuzhiyun return NULL;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun
667*4882a593Smuzhiyun inode->i_ino = de->low_ino;
668*4882a593Smuzhiyun inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
669*4882a593Smuzhiyun PROC_I(inode)->pde = de;
670*4882a593Smuzhiyun if (is_empty_pde(de)) {
671*4882a593Smuzhiyun make_empty_dir_inode(inode);
672*4882a593Smuzhiyun return inode;
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun if (de->mode) {
676*4882a593Smuzhiyun inode->i_mode = de->mode;
677*4882a593Smuzhiyun inode->i_uid = de->uid;
678*4882a593Smuzhiyun inode->i_gid = de->gid;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun if (de->size)
681*4882a593Smuzhiyun inode->i_size = de->size;
682*4882a593Smuzhiyun if (de->nlink)
683*4882a593Smuzhiyun set_nlink(inode, de->nlink);
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun if (S_ISREG(inode->i_mode)) {
686*4882a593Smuzhiyun inode->i_op = de->proc_iops;
687*4882a593Smuzhiyun if (de->proc_ops->proc_read_iter)
688*4882a593Smuzhiyun inode->i_fop = &proc_iter_file_ops;
689*4882a593Smuzhiyun else
690*4882a593Smuzhiyun inode->i_fop = &proc_reg_file_ops;
691*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
692*4882a593Smuzhiyun if (de->proc_ops->proc_compat_ioctl) {
693*4882a593Smuzhiyun if (de->proc_ops->proc_read_iter)
694*4882a593Smuzhiyun inode->i_fop = &proc_iter_file_ops_compat;
695*4882a593Smuzhiyun else
696*4882a593Smuzhiyun inode->i_fop = &proc_reg_file_ops_compat;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun #endif
699*4882a593Smuzhiyun } else if (S_ISDIR(inode->i_mode)) {
700*4882a593Smuzhiyun inode->i_op = de->proc_iops;
701*4882a593Smuzhiyun inode->i_fop = de->proc_dir_ops;
702*4882a593Smuzhiyun } else if (S_ISLNK(inode->i_mode)) {
703*4882a593Smuzhiyun inode->i_op = de->proc_iops;
704*4882a593Smuzhiyun inode->i_fop = NULL;
705*4882a593Smuzhiyun } else {
706*4882a593Smuzhiyun BUG();
707*4882a593Smuzhiyun }
708*4882a593Smuzhiyun return inode;
709*4882a593Smuzhiyun }
710