1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/proc/inode.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/time.h>
9 #include <linux/proc_fs.h>
10 #include <linux/kernel.h>
11 #include <linux/pid_namespace.h>
13 #include <linux/string.h>
14 #include <linux/stat.h>
15 #include <linux/completion.h>
16 #include <linux/poll.h>
17 #include <linux/printk.h>
18 #include <linux/file.h>
19 #include <linux/limits.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/sysctl.h>
23 #include <linux/seq_file.h>
24 #include <linux/slab.h>
25 #include <linux/mount.h>
26 #include <linux/magic.h>
28 #include <linux/uaccess.h>
32 static void proc_evict_inode(struct inode *inode)
34 struct proc_dir_entry *de;
35 struct ctl_table_header *head;
37 truncate_inode_pages_final(&inode->i_data);
40 /* Stop tracking associated processes */
41 put_pid(PROC_I(inode)->pid);
43 /* Let go of any associated proc directory entry */
48 head = PROC_I(inode)->sysctl;
50 RCU_INIT_POINTER(PROC_I(inode)->sysctl, NULL);
51 proc_sys_evict_inode(inode, head);
55 static struct kmem_cache * proc_inode_cachep;
57 static struct inode *proc_alloc_inode(struct super_block *sb)
59 struct proc_inode *ei;
62 ei = kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
67 ei->op.proc_get_link = NULL;
70 ei->sysctl_entry = NULL;
72 inode = &ei->vfs_inode;
76 static void proc_i_callback(struct rcu_head *head)
78 struct inode *inode = container_of(head, struct inode, i_rcu);
79 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
82 static void proc_destroy_inode(struct inode *inode)
84 call_rcu(&inode->i_rcu, proc_i_callback);
87 static void init_once(void *foo)
89 struct proc_inode *ei = (struct proc_inode *) foo;
91 inode_init_once(&ei->vfs_inode);
94 void __init proc_init_inodecache(void)
96 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
97 sizeof(struct proc_inode),
98 0, (SLAB_RECLAIM_ACCOUNT|
99 SLAB_MEM_SPREAD|SLAB_ACCOUNT|
104 static int proc_show_options(struct seq_file *seq, struct dentry *root)
106 struct super_block *sb = root->d_sb;
107 struct pid_namespace *pid = sb->s_fs_info;
109 if (!gid_eq(pid->pid_gid, GLOBAL_ROOT_GID))
110 seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
111 if (pid->hide_pid != HIDEPID_OFF)
112 seq_printf(seq, ",hidepid=%u", pid->hide_pid);
117 static const struct super_operations proc_sops = {
118 .alloc_inode = proc_alloc_inode,
119 .destroy_inode = proc_destroy_inode,
120 .drop_inode = generic_delete_inode,
121 .evict_inode = proc_evict_inode,
122 .statfs = simple_statfs,
123 .remount_fs = proc_remount,
124 .show_options = proc_show_options,
127 enum {BIAS = -1U<<31};
129 static inline int use_pde(struct proc_dir_entry *pde)
131 return atomic_inc_unless_negative(&pde->in_use);
134 static void unuse_pde(struct proc_dir_entry *pde)
136 if (atomic_dec_return(&pde->in_use) == BIAS)
137 complete(pde->pde_unload_completion);
141 static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
144 * close() (proc_reg_release()) can't delete an entry and proceed:
145 * ->release hook needs to be available at the right moment.
147 * rmmod (remove_proc_entry() et al) can't delete an entry and proceed:
148 * "struct file" needs to be available at the right moment.
150 * Therefore, first process to enter this function does ->release() and
151 * signals its completion to the other process which does nothing.
154 /* somebody else is doing that, just wait */
155 DECLARE_COMPLETION_ONSTACK(c);
157 spin_unlock(&pde->pde_unload_lock);
158 wait_for_completion(&c);
159 spin_lock(&pde->pde_unload_lock);
162 pdeo->closing = true;
163 spin_unlock(&pde->pde_unload_lock);
165 pde->proc_fops->release(file_inode(file), file);
166 spin_lock(&pde->pde_unload_lock);
167 /* After ->release. */
175 void proc_entry_rundown(struct proc_dir_entry *de)
177 DECLARE_COMPLETION_ONSTACK(c);
178 /* Wait until all existing callers into module are done. */
179 de->pde_unload_completion = &c;
180 if (atomic_add_return(BIAS, &de->in_use) != BIAS)
181 wait_for_completion(&c);
183 /* ->pde_openers list can't grow from now on. */
185 spin_lock(&de->pde_unload_lock);
186 while (!list_empty(&de->pde_openers)) {
187 struct pde_opener *pdeo;
188 pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
189 close_pdeo(de, pdeo);
191 spin_unlock(&de->pde_unload_lock);
194 static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
196 struct proc_dir_entry *pde = PDE(file_inode(file));
199 loff_t (*llseek)(struct file *, loff_t, int);
200 llseek = pde->proc_fops->llseek;
202 llseek = default_llseek;
203 rv = llseek(file, offset, whence);
209 static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
211 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
212 struct proc_dir_entry *pde = PDE(file_inode(file));
215 read = pde->proc_fops->read;
217 rv = read(file, buf, count, ppos);
223 static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
225 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
226 struct proc_dir_entry *pde = PDE(file_inode(file));
229 write = pde->proc_fops->write;
231 rv = write(file, buf, count, ppos);
237 static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
239 struct proc_dir_entry *pde = PDE(file_inode(file));
240 unsigned int rv = DEFAULT_POLLMASK;
241 unsigned int (*poll)(struct file *, struct poll_table_struct *);
243 poll = pde->proc_fops->poll;
245 rv = poll(file, pts);
251 static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
253 struct proc_dir_entry *pde = PDE(file_inode(file));
255 long (*ioctl)(struct file *, unsigned int, unsigned long);
257 ioctl = pde->proc_fops->unlocked_ioctl;
259 rv = ioctl(file, cmd, arg);
266 static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
268 struct proc_dir_entry *pde = PDE(file_inode(file));
270 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
272 compat_ioctl = pde->proc_fops->compat_ioctl;
274 rv = compat_ioctl(file, cmd, arg);
281 static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
283 struct proc_dir_entry *pde = PDE(file_inode(file));
285 int (*mmap)(struct file *, struct vm_area_struct *);
287 mmap = pde->proc_fops->mmap;
289 rv = mmap(file, vma);
296 proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
297 unsigned long len, unsigned long pgoff,
300 struct proc_dir_entry *pde = PDE(file_inode(file));
301 unsigned long rv = -EIO;
304 typeof(proc_reg_get_unmapped_area) *get_area;
306 get_area = pde->proc_fops->get_unmapped_area;
309 get_area = current->mm->get_unmapped_area;
313 rv = get_area(file, orig_addr, len, pgoff, flags);
321 static int proc_reg_open(struct inode *inode, struct file *file)
323 struct proc_dir_entry *pde = PDE(inode);
325 int (*open)(struct inode *, struct file *);
326 int (*release)(struct inode *, struct file *);
327 struct pde_opener *pdeo;
331 * 1) PDE's ->release hook will be called no matter what
332 * either normally by close()/->release, or forcefully by
333 * rmmod/remove_proc_entry.
335 * 2) rmmod isn't blocked by opening file in /proc and sitting on
336 * the descriptor (including "rmmod foo </proc/foo" scenario).
338 * Save every "struct file" with custom ->release hook.
340 pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
348 open = pde->proc_fops->open;
349 release = pde->proc_fops->release;
352 rv = open(inode, file);
354 if (rv == 0 && release) {
355 /* To know what to release. */
357 pdeo->closing = false;
359 spin_lock(&pde->pde_unload_lock);
360 list_add(&pdeo->lh, &pde->pde_openers);
361 spin_unlock(&pde->pde_unload_lock);
369 static int proc_reg_release(struct inode *inode, struct file *file)
371 struct proc_dir_entry *pde = PDE(inode);
372 struct pde_opener *pdeo;
373 spin_lock(&pde->pde_unload_lock);
374 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
375 if (pdeo->file == file) {
376 close_pdeo(pde, pdeo);
380 spin_unlock(&pde->pde_unload_lock);
384 static const struct file_operations proc_reg_file_ops = {
385 .llseek = proc_reg_llseek,
386 .read = proc_reg_read,
387 .write = proc_reg_write,
388 .poll = proc_reg_poll,
389 .unlocked_ioctl = proc_reg_unlocked_ioctl,
391 .compat_ioctl = proc_reg_compat_ioctl,
393 .mmap = proc_reg_mmap,
394 .get_unmapped_area = proc_reg_get_unmapped_area,
395 .open = proc_reg_open,
396 .release = proc_reg_release,
400 static const struct file_operations proc_reg_file_ops_no_compat = {
401 .llseek = proc_reg_llseek,
402 .read = proc_reg_read,
403 .write = proc_reg_write,
404 .poll = proc_reg_poll,
405 .unlocked_ioctl = proc_reg_unlocked_ioctl,
406 .mmap = proc_reg_mmap,
407 .get_unmapped_area = proc_reg_get_unmapped_area,
408 .open = proc_reg_open,
409 .release = proc_reg_release,
413 static void proc_put_link(void *p)
418 static const char *proc_get_link(struct dentry *dentry,
420 struct delayed_call *done)
422 struct proc_dir_entry *pde = PDE(inode);
423 if (unlikely(!use_pde(pde)))
424 return ERR_PTR(-EINVAL);
425 set_delayed_call(done, proc_put_link, pde);
429 const struct inode_operations proc_link_inode_operations = {
430 .get_link = proc_get_link,
433 struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
435 struct inode *inode = new_inode(sb);
438 inode->i_ino = de->low_ino;
439 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
440 PROC_I(inode)->pde = de;
442 if (is_empty_pde(de)) {
443 make_empty_dir_inode(inode);
447 inode->i_mode = de->mode;
448 inode->i_uid = de->uid;
449 inode->i_gid = de->gid;
452 inode->i_size = de->size;
454 set_nlink(inode, de->nlink);
455 WARN_ON(!de->proc_iops);
456 inode->i_op = de->proc_iops;
458 if (S_ISREG(inode->i_mode)) {
460 if (!de->proc_fops->compat_ioctl)
462 &proc_reg_file_ops_no_compat;
465 inode->i_fop = &proc_reg_file_ops;
467 inode->i_fop = de->proc_fops;
475 int proc_fill_super(struct super_block *s, void *data, int silent)
477 struct pid_namespace *ns = get_pid_ns(s->s_fs_info);
478 struct inode *root_inode;
481 if (!proc_parse_options(data, ns))
484 /* User space would break if executables or devices appear on proc */
485 s->s_iflags |= SB_I_USERNS_VISIBLE | SB_I_NOEXEC | SB_I_NODEV;
486 s->s_flags |= MS_NODIRATIME | MS_NOSUID | MS_NOEXEC;
487 s->s_blocksize = 1024;
488 s->s_blocksize_bits = 10;
489 s->s_magic = PROC_SUPER_MAGIC;
490 s->s_op = &proc_sops;
494 * procfs isn't actually a stacking filesystem; however, there is
495 * too much magic going on inside it to permit stacking things on
498 s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
501 root_inode = proc_get_inode(s, &proc_root);
503 pr_err("proc_fill_super: get root inode failed\n");
507 s->s_root = d_make_root(root_inode);
509 pr_err("proc_fill_super: allocate dentry failed\n");
513 ret = proc_setup_self(s);
517 return proc_setup_thread_self(s);