]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - fs/proc/inode.c
Merge remote-tracking branch 'asoc/topic/pcm512x' into asoc-next
[mirror_ubuntu-focal-kernel.git] / fs / proc / inode.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * linux/fs/proc/inode.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8#include <linux/time.h>
9#include <linux/proc_fs.h>
10#include <linux/kernel.h>
97412950 11#include <linux/pid_namespace.h>
1da177e4
LT
12#include <linux/mm.h>
13#include <linux/string.h>
14#include <linux/stat.h>
786d7e16 15#include <linux/completion.h>
dd23aae4 16#include <linux/poll.h>
87ebdc00 17#include <linux/printk.h>
1da177e4
LT
18#include <linux/file.h>
19#include <linux/limits.h>
20#include <linux/init.h>
21#include <linux/module.h>
9043476f 22#include <linux/sysctl.h>
97412950 23#include <linux/seq_file.h>
5a0e3ad6 24#include <linux/slab.h>
97412950 25#include <linux/mount.h>
303eb7e2 26#include <linux/magic.h>
1da177e4 27
7c0f6ba6 28#include <linux/uaccess.h>
1da177e4 29
fee781e6 30#include "internal.h"
1da177e4 31
8267952b 32static void proc_evict_inode(struct inode *inode)
1da177e4
LT
33{
34 struct proc_dir_entry *de;
dfef6dcd 35 struct ctl_table_header *head;
1da177e4 36
91b0abe3 37 truncate_inode_pages_final(&inode->i_data);
dbd5768f 38 clear_inode(inode);
fef26658 39
99f89551 40 /* Stop tracking associated processes */
13b41b09 41 put_pid(PROC_I(inode)->pid);
1da177e4
LT
42
43 /* Let go of any associated proc directory entry */
6bee55f9 44 de = PDE(inode);
99b76233 45 if (de)
135d5655 46 pde_put(de);
d6cffbbe 47
dfef6dcd
AV
48 head = PROC_I(inode)->sysctl;
49 if (head) {
1c44dbc8 50 RCU_INIT_POINTER(PROC_I(inode)->sysctl, NULL);
d6cffbbe 51 proc_sys_evict_inode(inode, head);
dfef6dcd 52 }
1da177e4
LT
53}
54
e18b890b 55static struct kmem_cache * proc_inode_cachep;
1da177e4
LT
56
57static struct inode *proc_alloc_inode(struct super_block *sb)
58{
59 struct proc_inode *ei;
60 struct inode *inode;
61
f245e1c1 62 ei = kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
1da177e4
LT
63 if (!ei)
64 return NULL;
13b41b09 65 ei->pid = NULL;
aed7a6c4 66 ei->fd = 0;
1da177e4
LT
67 ei->op.proc_get_link = NULL;
68 ei->pde = NULL;
9043476f
AV
69 ei->sysctl = NULL;
70 ei->sysctl_entry = NULL;
3d3d35b1 71 ei->ns_ops = NULL;
1da177e4 72 inode = &ei->vfs_inode;
1da177e4
LT
73 return inode;
74}
75
fa0d7e3d 76static void proc_i_callback(struct rcu_head *head)
1da177e4 77{
fa0d7e3d 78 struct inode *inode = container_of(head, struct inode, i_rcu);
1da177e4
LT
79 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
80}
81
fa0d7e3d
NP
82static void proc_destroy_inode(struct inode *inode)
83{
84 call_rcu(&inode->i_rcu, proc_i_callback);
85}
86
51cc5068 87static void init_once(void *foo)
1da177e4
LT
88{
89 struct proc_inode *ei = (struct proc_inode *) foo;
90
a35afb83 91 inode_init_once(&ei->vfs_inode);
1da177e4 92}
20c2df83 93
5bcd7ff9 94void __init proc_init_inodecache(void)
1da177e4
LT
95{
96 proc_inode_cachep = kmem_cache_create("proc_inode_cache",
97 sizeof(struct proc_inode),
fffb60f9 98 0, (SLAB_RECLAIM_ACCOUNT|
5d097056
VD
99 SLAB_MEM_SPREAD|SLAB_ACCOUNT|
100 SLAB_PANIC),
20c2df83 101 init_once);
1da177e4
LT
102}
103
97412950
VK
104static int proc_show_options(struct seq_file *seq, struct dentry *root)
105{
0499680a
VK
106 struct super_block *sb = root->d_sb;
107 struct pid_namespace *pid = sb->s_fs_info;
108
dcb0f222
EB
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));
796f571b 111 if (pid->hide_pid != HIDEPID_OFF)
0499680a
VK
112 seq_printf(seq, ",hidepid=%u", pid->hide_pid);
113
97412950
VK
114 return 0;
115}
116
ee9b6d61 117static const struct super_operations proc_sops = {
1da177e4
LT
118 .alloc_inode = proc_alloc_inode,
119 .destroy_inode = proc_destroy_inode,
1da177e4 120 .drop_inode = generic_delete_inode,
8267952b 121 .evict_inode = proc_evict_inode,
1da177e4 122 .statfs = simple_statfs,
97412950
VK
123 .remount_fs = proc_remount,
124 .show_options = proc_show_options,
1da177e4
LT
125};
126
866ad9a7
AV
127enum {BIAS = -1U<<31};
128
129static inline int use_pde(struct proc_dir_entry *pde)
130{
05c0ae21 131 return atomic_inc_unless_negative(&pde->in_use);
881adb85
AD
132}
133
866ad9a7 134static void unuse_pde(struct proc_dir_entry *pde)
881adb85 135{
05c0ae21
AV
136 if (atomic_dec_return(&pde->in_use) == BIAS)
137 complete(pde->pde_unload_completion);
786d7e16
AD
138}
139
ca469f35
AV
140/* pde is locked */
141static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
142{
492b2da6
AD
143 /*
144 * close() (proc_reg_release()) can't delete an entry and proceed:
145 * ->release hook needs to be available at the right moment.
146 *
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.
149 *
150 * Therefore, first process to enter this function does ->release() and
151 * signals its completion to the other process which does nothing.
152 */
05c0ae21 153 if (pdeo->closing) {
ca469f35 154 /* somebody else is doing that, just wait */
05c0ae21
AV
155 DECLARE_COMPLETION_ONSTACK(c);
156 pdeo->c = &c;
ca469f35 157 spin_unlock(&pde->pde_unload_lock);
05c0ae21 158 wait_for_completion(&c);
ca469f35 159 spin_lock(&pde->pde_unload_lock);
ca469f35
AV
160 } else {
161 struct file *file;
f5887c71 162 pdeo->closing = true;
ca469f35
AV
163 spin_unlock(&pde->pde_unload_lock);
164 file = pdeo->file;
165 pde->proc_fops->release(file_inode(file), file);
166 spin_lock(&pde->pde_unload_lock);
492b2da6 167 /* After ->release. */
06a0c417 168 list_del(&pdeo->lh);
05c0ae21
AV
169 if (pdeo->c)
170 complete(pdeo->c);
ca469f35 171 kfree(pdeo);
05c0ae21 172 }
ca469f35
AV
173}
174
866ad9a7 175void proc_entry_rundown(struct proc_dir_entry *de)
786d7e16 176{
05c0ae21 177 DECLARE_COMPLETION_ONSTACK(c);
866ad9a7 178 /* Wait until all existing callers into module are done. */
05c0ae21
AV
179 de->pde_unload_completion = &c;
180 if (atomic_add_return(BIAS, &de->in_use) != BIAS)
181 wait_for_completion(&c);
786d7e16 182
492b2da6
AD
183 /* ->pde_openers list can't grow from now on. */
184
05c0ae21 185 spin_lock(&de->pde_unload_lock);
866ad9a7
AV
186 while (!list_empty(&de->pde_openers)) {
187 struct pde_opener *pdeo;
866ad9a7 188 pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
ca469f35 189 close_pdeo(de, pdeo);
866ad9a7
AV
190 }
191 spin_unlock(&de->pde_unload_lock);
786d7e16
AD
192}
193
866ad9a7
AV
194static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
195{
196 struct proc_dir_entry *pde = PDE(file_inode(file));
197 loff_t rv = -EINVAL;
198 if (use_pde(pde)) {
199 loff_t (*llseek)(struct file *, loff_t, int);
200 llseek = pde->proc_fops->llseek;
201 if (!llseek)
202 llseek = default_llseek;
203 rv = llseek(file, offset, whence);
204 unuse_pde(pde);
205 }
786d7e16
AD
206 return rv;
207}
208
866ad9a7 209static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
786d7e16 210{
866ad9a7 211 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
496ad9aa 212 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 213 ssize_t rv = -EIO;
866ad9a7
AV
214 if (use_pde(pde)) {
215 read = pde->proc_fops->read;
216 if (read)
217 rv = read(file, buf, count, ppos);
218 unuse_pde(pde);
786d7e16 219 }
866ad9a7
AV
220 return rv;
221}
786d7e16 222
866ad9a7
AV
223static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
224{
225 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
226 struct proc_dir_entry *pde = PDE(file_inode(file));
227 ssize_t rv = -EIO;
228 if (use_pde(pde)) {
229 write = pde->proc_fops->write;
230 if (write)
231 rv = write(file, buf, count, ppos);
232 unuse_pde(pde);
233 }
786d7e16
AD
234 return rv;
235}
236
237static unsigned int proc_reg_poll(struct file *file, struct poll_table_struct *pts)
238{
496ad9aa 239 struct proc_dir_entry *pde = PDE(file_inode(file));
dd23aae4 240 unsigned int rv = DEFAULT_POLLMASK;
786d7e16 241 unsigned int (*poll)(struct file *, struct poll_table_struct *);
866ad9a7
AV
242 if (use_pde(pde)) {
243 poll = pde->proc_fops->poll;
244 if (poll)
245 rv = poll(file, pts);
246 unuse_pde(pde);
786d7e16 247 }
786d7e16
AD
248 return rv;
249}
250
251static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
252{
496ad9aa 253 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 254 long rv = -ENOTTY;
b19dd42f 255 long (*ioctl)(struct file *, unsigned int, unsigned long);
866ad9a7
AV
256 if (use_pde(pde)) {
257 ioctl = pde->proc_fops->unlocked_ioctl;
258 if (ioctl)
259 rv = ioctl(file, cmd, arg);
260 unuse_pde(pde);
786d7e16 261 }
786d7e16
AD
262 return rv;
263}
264
265#ifdef CONFIG_COMPAT
266static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
267{
496ad9aa 268 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16
AD
269 long rv = -ENOTTY;
270 long (*compat_ioctl)(struct file *, unsigned int, unsigned long);
866ad9a7
AV
271 if (use_pde(pde)) {
272 compat_ioctl = pde->proc_fops->compat_ioctl;
273 if (compat_ioctl)
274 rv = compat_ioctl(file, cmd, arg);
275 unuse_pde(pde);
786d7e16 276 }
786d7e16
AD
277 return rv;
278}
279#endif
280
281static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
282{
496ad9aa 283 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16
AD
284 int rv = -EIO;
285 int (*mmap)(struct file *, struct vm_area_struct *);
866ad9a7
AV
286 if (use_pde(pde)) {
287 mmap = pde->proc_fops->mmap;
288 if (mmap)
289 rv = mmap(file, vma);
290 unuse_pde(pde);
786d7e16 291 }
786d7e16
AD
292 return rv;
293}
294
5721cf84
HD
295static unsigned long
296proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
297 unsigned long len, unsigned long pgoff,
298 unsigned long flags)
c4fe2448
AD
299{
300 struct proc_dir_entry *pde = PDE(file_inode(file));
2cbe3b0a 301 unsigned long rv = -EIO;
ae5758a1 302
c4fe2448 303 if (use_pde(pde)) {
ae5758a1
JB
304 typeof(proc_reg_get_unmapped_area) *get_area;
305
306 get_area = pde->proc_fops->get_unmapped_area;
fad1a86e 307#ifdef CONFIG_MMU
ae5758a1
JB
308 if (!get_area)
309 get_area = current->mm->get_unmapped_area;
fad1a86e 310#endif
ae5758a1 311
5721cf84
HD
312 if (get_area)
313 rv = get_area(file, orig_addr, len, pgoff, flags);
ae5758a1
JB
314 else
315 rv = orig_addr;
c4fe2448
AD
316 unuse_pde(pde);
317 }
318 return rv;
319}
320
786d7e16
AD
321static int proc_reg_open(struct inode *inode, struct file *file)
322{
323 struct proc_dir_entry *pde = PDE(inode);
324 int rv = 0;
325 int (*open)(struct inode *, struct file *);
881adb85
AD
326 int (*release)(struct inode *, struct file *);
327 struct pde_opener *pdeo;
328
329 /*
492b2da6
AD
330 * Ensure that
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.
334 *
335 * 2) rmmod isn't blocked by opening file in /proc and sitting on
336 * the descriptor (including "rmmod foo </proc/foo" scenario).
881adb85 337 *
492b2da6 338 * Save every "struct file" with custom ->release hook.
881adb85 339 */
39a10ac2 340 pdeo = kmalloc(sizeof(struct pde_opener), GFP_KERNEL);
881adb85
AD
341 if (!pdeo)
342 return -ENOMEM;
786d7e16 343
866ad9a7 344 if (!use_pde(pde)) {
881adb85 345 kfree(pdeo);
d2857e79 346 return -ENOENT;
786d7e16 347 }
786d7e16 348 open = pde->proc_fops->open;
881adb85 349 release = pde->proc_fops->release;
786d7e16
AD
350
351 if (open)
352 rv = open(inode, file);
353
881adb85
AD
354 if (rv == 0 && release) {
355 /* To know what to release. */
881adb85 356 pdeo->file = file;
39a10ac2
AD
357 pdeo->closing = false;
358 pdeo->c = NULL;
05c0ae21 359 spin_lock(&pde->pde_unload_lock);
881adb85 360 list_add(&pdeo->lh, &pde->pde_openers);
05c0ae21 361 spin_unlock(&pde->pde_unload_lock);
881adb85
AD
362 } else
363 kfree(pdeo);
05c0ae21
AV
364
365 unuse_pde(pde);
786d7e16
AD
366 return rv;
367}
368
369static int proc_reg_release(struct inode *inode, struct file *file)
370{
371 struct proc_dir_entry *pde = PDE(inode);
881adb85 372 struct pde_opener *pdeo;
786d7e16 373 spin_lock(&pde->pde_unload_lock);
ca469f35
AV
374 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
375 if (pdeo->file == file) {
376 close_pdeo(pde, pdeo);
377 break;
378 }
881adb85 379 }
786d7e16 380 spin_unlock(&pde->pde_unload_lock);
ca469f35 381 return 0;
786d7e16
AD
382}
383
384static 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,
390#ifdef CONFIG_COMPAT
391 .compat_ioctl = proc_reg_compat_ioctl,
392#endif
393 .mmap = proc_reg_mmap,
c4fe2448 394 .get_unmapped_area = proc_reg_get_unmapped_area,
786d7e16
AD
395 .open = proc_reg_open,
396 .release = proc_reg_release,
397};
398
778f3dd5
DM
399#ifdef CONFIG_COMPAT
400static 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,
c4fe2448 407 .get_unmapped_area = proc_reg_get_unmapped_area,
778f3dd5
DM
408 .open = proc_reg_open,
409 .release = proc_reg_release,
410};
411#endif
412
fceef393
AV
413static void proc_put_link(void *p)
414{
415 unuse_pde(p);
416}
417
6b255391 418static const char *proc_get_link(struct dentry *dentry,
fceef393
AV
419 struct inode *inode,
420 struct delayed_call *done)
7e0e953b 421{
6b255391 422 struct proc_dir_entry *pde = PDE(inode);
7e0e953b
AV
423 if (unlikely(!use_pde(pde)))
424 return ERR_PTR(-EINVAL);
fceef393 425 set_delayed_call(done, proc_put_link, pde);
680baacb 426 return pde->data;
7e0e953b
AV
427}
428
7e0e953b 429const struct inode_operations proc_link_inode_operations = {
6b255391 430 .get_link = proc_get_link,
7e0e953b
AV
431};
432
6d1b6e4e 433struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
1da177e4 434{
51f0885e 435 struct inode *inode = new_inode_pseudo(sb);
1da177e4 436
51f0885e
LT
437 if (inode) {
438 inode->i_ino = de->low_ino;
078cd827 439 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
a1d4aebb 440 PROC_I(inode)->pde = de;
5e971dce 441
eb6d38d5
EB
442 if (is_empty_pde(de)) {
443 make_empty_dir_inode(inode);
444 return inode;
445 }
5e971dce
AD
446 if (de->mode) {
447 inode->i_mode = de->mode;
448 inode->i_uid = de->uid;
449 inode->i_gid = de->gid;
450 }
451 if (de->size)
452 inode->i_size = de->size;
453 if (de->nlink)
bfe86848 454 set_nlink(inode, de->nlink);
b6cdc731
AV
455 WARN_ON(!de->proc_iops);
456 inode->i_op = de->proc_iops;
5e971dce
AD
457 if (de->proc_fops) {
458 if (S_ISREG(inode->i_mode)) {
778f3dd5 459#ifdef CONFIG_COMPAT
5e971dce
AD
460 if (!de->proc_fops->compat_ioctl)
461 inode->i_fop =
462 &proc_reg_file_ops_no_compat;
463 else
778f3dd5 464#endif
5e971dce
AD
465 inode->i_fop = &proc_reg_file_ops;
466 } else {
467 inode->i_fop = de->proc_fops;
778f3dd5 468 }
786d7e16 469 }
99b76233 470 } else
135d5655 471 pde_put(de);
1da177e4 472 return inode;
d3d009cb 473}
1da177e4 474
e94591d0 475int proc_fill_super(struct super_block *s, void *data, int silent)
1da177e4 476{
e94591d0 477 struct pid_namespace *ns = get_pid_ns(s->s_fs_info);
87e0aab3 478 struct inode *root_inode;
0097875b 479 int ret;
87e0aab3 480
e94591d0
EB
481 if (!proc_parse_options(data, ns))
482 return -EINVAL;
483
a2982cc9
EB
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;
1751e8a6 486 s->s_flags |= SB_NODIRATIME | SB_NOSUID | SB_NOEXEC;
1da177e4
LT
487 s->s_blocksize = 1024;
488 s->s_blocksize_bits = 10;
489 s->s_magic = PROC_SUPER_MAGIC;
490 s->s_op = &proc_sops;
491 s->s_time_gran = 1;
a867d734
LT
492
493 /*
494 * procfs isn't actually a stacking filesystem; however, there is
495 * too much magic going on inside it to permit stacking things on
496 * top of it
497 */
498 s->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH;
1da177e4 499
135d5655 500 pde_get(&proc_root);
87e0aab3
MP
501 root_inode = proc_get_inode(s, &proc_root);
502 if (!root_inode) {
87ebdc00 503 pr_err("proc_fill_super: get root inode failed\n");
87e0aab3
MP
504 return -ENOMEM;
505 }
1da177e4 506
87e0aab3
MP
507 s->s_root = d_make_root(root_inode);
508 if (!s->s_root) {
87ebdc00 509 pr_err("proc_fill_super: allocate dentry failed\n");
87e0aab3
MP
510 return -ENOMEM;
511 }
512
0097875b
EB
513 ret = proc_setup_self(s);
514 if (ret) {
515 return ret;
516 }
517 return proc_setup_thread_self(s);
1da177e4 518}