]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/proc/inode.c
fs/proc/inode.c: annotate close_pdeo() for sparse
[mirror_ubuntu-jammy-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
efb1a57d 8#include <linux/cache.h>
1da177e4
LT
9#include <linux/time.h>
10#include <linux/proc_fs.h>
11#include <linux/kernel.h>
97412950 12#include <linux/pid_namespace.h>
1da177e4
LT
13#include <linux/mm.h>
14#include <linux/string.h>
15#include <linux/stat.h>
786d7e16 16#include <linux/completion.h>
dd23aae4 17#include <linux/poll.h>
87ebdc00 18#include <linux/printk.h>
1da177e4
LT
19#include <linux/file.h>
20#include <linux/limits.h>
21#include <linux/init.h>
22#include <linux/module.h>
9043476f 23#include <linux/sysctl.h>
97412950 24#include <linux/seq_file.h>
5a0e3ad6 25#include <linux/slab.h>
97412950 26#include <linux/mount.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;
71448011 36 struct proc_inode *ei = PROC_I(inode);
1da177e4 37
91b0abe3 38 truncate_inode_pages_final(&inode->i_data);
dbd5768f 39 clear_inode(inode);
fef26658 40
99f89551 41 /* Stop tracking associated processes */
71448011 42 if (ei->pid) {
7bc3e6e5 43 proc_pid_evict_inode(ei);
71448011
EB
44 ei->pid = NULL;
45 }
1da177e4
LT
46
47 /* Let go of any associated proc directory entry */
71448011
EB
48 de = ei->pde;
49 if (de) {
135d5655 50 pde_put(de);
71448011
EB
51 ei->pde = NULL;
52 }
d6cffbbe 53
71448011 54 head = ei->sysctl;
dfef6dcd 55 if (head) {
71448011 56 RCU_INIT_POINTER(ei->sysctl, NULL);
d6cffbbe 57 proc_sys_evict_inode(inode, head);
dfef6dcd 58 }
1da177e4
LT
59}
60
efb1a57d 61static struct kmem_cache *proc_inode_cachep __ro_after_init;
195b8cf0 62static struct kmem_cache *pde_opener_cache __ro_after_init;
1da177e4
LT
63
64static struct inode *proc_alloc_inode(struct super_block *sb)
65{
66 struct proc_inode *ei;
1da177e4 67
f245e1c1 68 ei = kmem_cache_alloc(proc_inode_cachep, GFP_KERNEL);
1da177e4
LT
69 if (!ei)
70 return NULL;
13b41b09 71 ei->pid = NULL;
aed7a6c4 72 ei->fd = 0;
1da177e4
LT
73 ei->op.proc_get_link = NULL;
74 ei->pde = NULL;
9043476f
AV
75 ei->sysctl = NULL;
76 ei->sysctl_entry = NULL;
0afa5ca8 77 INIT_HLIST_NODE(&ei->sibling_inodes);
3d3d35b1 78 ei->ns_ops = NULL;
230f72e9 79 return &ei->vfs_inode;
1da177e4
LT
80}
81
4aa6b55c 82static void proc_free_inode(struct inode *inode)
1da177e4
LT
83{
84 kmem_cache_free(proc_inode_cachep, PROC_I(inode));
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
195b8cf0 94void __init proc_init_kmemcache(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);
195b8cf0
AD
102 pde_opener_cache =
103 kmem_cache_create("pde_opener", sizeof(struct pde_opener), 0,
2acddbe8 104 SLAB_ACCOUNT|SLAB_PANIC, NULL);
b4884f23 105 proc_dir_entry_cache = kmem_cache_create_usercopy(
2d6e4e82
AD
106 "proc_dir_entry", SIZEOF_PDE, 0, SLAB_PANIC,
107 offsetof(struct proc_dir_entry, inline_name),
108 SIZEOF_PDE_INLINE_NAME, NULL);
109 BUILD_BUG_ON(sizeof(struct proc_dir_entry) >= SIZEOF_PDE);
1da177e4
LT
110}
111
f90f3caf 112void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock)
26dbc60f
EB
113{
114 struct inode *inode;
115 struct proc_inode *ei;
116 struct hlist_node *node;
080f6276 117 struct super_block *old_sb = NULL;
26dbc60f
EB
118
119 rcu_read_lock();
120 for (;;) {
080f6276 121 struct super_block *sb;
26dbc60f
EB
122 node = hlist_first_rcu(inodes);
123 if (!node)
124 break;
125 ei = hlist_entry(node, struct proc_inode, sibling_inodes);
126 spin_lock(lock);
127 hlist_del_init_rcu(&ei->sibling_inodes);
128 spin_unlock(lock);
129
130 inode = &ei->vfs_inode;
131 sb = inode->i_sb;
080f6276 132 if ((sb != old_sb) && !atomic_inc_not_zero(&sb->s_active))
26dbc60f
EB
133 continue;
134 inode = igrab(inode);
135 rcu_read_unlock();
080f6276
EB
136 if (sb != old_sb) {
137 if (old_sb)
138 deactivate_super(old_sb);
139 old_sb = sb;
140 }
26dbc60f 141 if (unlikely(!inode)) {
26dbc60f
EB
142 rcu_read_lock();
143 continue;
144 }
145
f90f3caf
EB
146 if (S_ISDIR(inode->i_mode)) {
147 struct dentry *dir = d_find_any_alias(inode);
148 if (dir) {
149 d_invalidate(dir);
150 dput(dir);
151 }
152 } else {
153 struct dentry *dentry;
154 while ((dentry = d_find_alias(inode))) {
155 d_invalidate(dentry);
156 dput(dentry);
157 }
158 }
26dbc60f 159 iput(inode);
26dbc60f
EB
160
161 rcu_read_lock();
162 }
163 rcu_read_unlock();
080f6276
EB
164 if (old_sb)
165 deactivate_super(old_sb);
26dbc60f
EB
166}
167
97412950
VK
168static int proc_show_options(struct seq_file *seq, struct dentry *root)
169{
0499680a
VK
170 struct super_block *sb = root->d_sb;
171 struct pid_namespace *pid = sb->s_fs_info;
172
dcb0f222
EB
173 if (!gid_eq(pid->pid_gid, GLOBAL_ROOT_GID))
174 seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, pid->pid_gid));
796f571b 175 if (pid->hide_pid != HIDEPID_OFF)
0499680a
VK
176 seq_printf(seq, ",hidepid=%u", pid->hide_pid);
177
97412950
VK
178 return 0;
179}
180
60a3c3a5 181const struct super_operations proc_sops = {
1da177e4 182 .alloc_inode = proc_alloc_inode,
4aa6b55c 183 .free_inode = proc_free_inode,
1da177e4 184 .drop_inode = generic_delete_inode,
8267952b 185 .evict_inode = proc_evict_inode,
1da177e4 186 .statfs = simple_statfs,
97412950 187 .show_options = proc_show_options,
1da177e4
LT
188};
189
866ad9a7
AV
190enum {BIAS = -1U<<31};
191
192static inline int use_pde(struct proc_dir_entry *pde)
193{
15b158b4 194 return likely(atomic_inc_unless_negative(&pde->in_use));
881adb85
AD
195}
196
866ad9a7 197static void unuse_pde(struct proc_dir_entry *pde)
881adb85 198{
15b158b4 199 if (unlikely(atomic_dec_return(&pde->in_use) == BIAS))
05c0ae21 200 complete(pde->pde_unload_completion);
786d7e16
AD
201}
202
2f897424 203/* pde is locked on entry, unlocked on exit */
ca469f35 204static void close_pdeo(struct proc_dir_entry *pde, struct pde_opener *pdeo)
904f394e 205 __releases(&pde->pde_unload_lock)
ca469f35 206{
492b2da6
AD
207 /*
208 * close() (proc_reg_release()) can't delete an entry and proceed:
209 * ->release hook needs to be available at the right moment.
210 *
211 * rmmod (remove_proc_entry() et al) can't delete an entry and proceed:
212 * "struct file" needs to be available at the right moment.
213 *
214 * Therefore, first process to enter this function does ->release() and
215 * signals its completion to the other process which does nothing.
216 */
05c0ae21 217 if (pdeo->closing) {
ca469f35 218 /* somebody else is doing that, just wait */
05c0ae21
AV
219 DECLARE_COMPLETION_ONSTACK(c);
220 pdeo->c = &c;
ca469f35 221 spin_unlock(&pde->pde_unload_lock);
05c0ae21 222 wait_for_completion(&c);
ca469f35
AV
223 } else {
224 struct file *file;
2f897424
AD
225 struct completion *c;
226
f5887c71 227 pdeo->closing = true;
ca469f35
AV
228 spin_unlock(&pde->pde_unload_lock);
229 file = pdeo->file;
d56c0d45 230 pde->proc_ops->proc_release(file_inode(file), file);
ca469f35 231 spin_lock(&pde->pde_unload_lock);
492b2da6 232 /* After ->release. */
06a0c417 233 list_del(&pdeo->lh);
2f897424
AD
234 c = pdeo->c;
235 spin_unlock(&pde->pde_unload_lock);
236 if (unlikely(c))
237 complete(c);
195b8cf0 238 kmem_cache_free(pde_opener_cache, pdeo);
05c0ae21 239 }
ca469f35
AV
240}
241
866ad9a7 242void proc_entry_rundown(struct proc_dir_entry *de)
786d7e16 243{
05c0ae21 244 DECLARE_COMPLETION_ONSTACK(c);
866ad9a7 245 /* Wait until all existing callers into module are done. */
05c0ae21
AV
246 de->pde_unload_completion = &c;
247 if (atomic_add_return(BIAS, &de->in_use) != BIAS)
248 wait_for_completion(&c);
786d7e16 249
492b2da6
AD
250 /* ->pde_openers list can't grow from now on. */
251
05c0ae21 252 spin_lock(&de->pde_unload_lock);
866ad9a7
AV
253 while (!list_empty(&de->pde_openers)) {
254 struct pde_opener *pdeo;
866ad9a7 255 pdeo = list_first_entry(&de->pde_openers, struct pde_opener, lh);
ca469f35 256 close_pdeo(de, pdeo);
2f897424 257 spin_lock(&de->pde_unload_lock);
866ad9a7
AV
258 }
259 spin_unlock(&de->pde_unload_lock);
786d7e16
AD
260}
261
866ad9a7
AV
262static loff_t proc_reg_llseek(struct file *file, loff_t offset, int whence)
263{
264 struct proc_dir_entry *pde = PDE(file_inode(file));
265 loff_t rv = -EINVAL;
266 if (use_pde(pde)) {
d56c0d45 267 typeof_member(struct proc_ops, proc_lseek) lseek;
9af27b28 268
d56c0d45
AD
269 lseek = pde->proc_ops->proc_lseek;
270 if (!lseek)
271 lseek = default_llseek;
272 rv = lseek(file, offset, whence);
866ad9a7
AV
273 unuse_pde(pde);
274 }
786d7e16
AD
275 return rv;
276}
277
866ad9a7 278static ssize_t proc_reg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
786d7e16 279{
496ad9aa 280 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 281 ssize_t rv = -EIO;
866ad9a7 282 if (use_pde(pde)) {
d56c0d45 283 typeof_member(struct proc_ops, proc_read) read;
9af27b28 284
d56c0d45 285 read = pde->proc_ops->proc_read;
866ad9a7
AV
286 if (read)
287 rv = read(file, buf, count, ppos);
288 unuse_pde(pde);
786d7e16 289 }
866ad9a7
AV
290 return rv;
291}
786d7e16 292
866ad9a7
AV
293static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
294{
866ad9a7
AV
295 struct proc_dir_entry *pde = PDE(file_inode(file));
296 ssize_t rv = -EIO;
297 if (use_pde(pde)) {
d56c0d45 298 typeof_member(struct proc_ops, proc_write) write;
9af27b28 299
d56c0d45 300 write = pde->proc_ops->proc_write;
866ad9a7
AV
301 if (write)
302 rv = write(file, buf, count, ppos);
303 unuse_pde(pde);
304 }
786d7e16
AD
305 return rv;
306}
307
076ccb76 308static __poll_t proc_reg_poll(struct file *file, struct poll_table_struct *pts)
786d7e16 309{
496ad9aa 310 struct proc_dir_entry *pde = PDE(file_inode(file));
e6c8adca 311 __poll_t rv = DEFAULT_POLLMASK;
866ad9a7 312 if (use_pde(pde)) {
d56c0d45 313 typeof_member(struct proc_ops, proc_poll) poll;
9af27b28 314
d56c0d45 315 poll = pde->proc_ops->proc_poll;
866ad9a7
AV
316 if (poll)
317 rv = poll(file, pts);
318 unuse_pde(pde);
786d7e16 319 }
786d7e16
AD
320 return rv;
321}
322
323static long proc_reg_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
324{
496ad9aa 325 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 326 long rv = -ENOTTY;
866ad9a7 327 if (use_pde(pde)) {
d56c0d45 328 typeof_member(struct proc_ops, proc_ioctl) ioctl;
9af27b28 329
d56c0d45 330 ioctl = pde->proc_ops->proc_ioctl;
866ad9a7
AV
331 if (ioctl)
332 rv = ioctl(file, cmd, arg);
333 unuse_pde(pde);
786d7e16 334 }
786d7e16
AD
335 return rv;
336}
337
338#ifdef CONFIG_COMPAT
339static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
340{
496ad9aa 341 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 342 long rv = -ENOTTY;
866ad9a7 343 if (use_pde(pde)) {
d56c0d45 344 typeof_member(struct proc_ops, proc_compat_ioctl) compat_ioctl;
9af27b28 345
d56c0d45 346 compat_ioctl = pde->proc_ops->proc_compat_ioctl;
866ad9a7
AV
347 if (compat_ioctl)
348 rv = compat_ioctl(file, cmd, arg);
349 unuse_pde(pde);
786d7e16 350 }
786d7e16
AD
351 return rv;
352}
353#endif
354
355static int proc_reg_mmap(struct file *file, struct vm_area_struct *vma)
356{
496ad9aa 357 struct proc_dir_entry *pde = PDE(file_inode(file));
786d7e16 358 int rv = -EIO;
866ad9a7 359 if (use_pde(pde)) {
d56c0d45 360 typeof_member(struct proc_ops, proc_mmap) mmap;
9af27b28 361
d56c0d45 362 mmap = pde->proc_ops->proc_mmap;
866ad9a7
AV
363 if (mmap)
364 rv = mmap(file, vma);
365 unuse_pde(pde);
786d7e16 366 }
786d7e16
AD
367 return rv;
368}
369
5721cf84
HD
370static unsigned long
371proc_reg_get_unmapped_area(struct file *file, unsigned long orig_addr,
372 unsigned long len, unsigned long pgoff,
373 unsigned long flags)
c4fe2448
AD
374{
375 struct proc_dir_entry *pde = PDE(file_inode(file));
2cbe3b0a 376 unsigned long rv = -EIO;
ae5758a1 377
c4fe2448 378 if (use_pde(pde)) {
d56c0d45 379 typeof_member(struct proc_ops, proc_get_unmapped_area) get_area;
ae5758a1 380
d56c0d45 381 get_area = pde->proc_ops->proc_get_unmapped_area;
fad1a86e 382#ifdef CONFIG_MMU
ae5758a1
JB
383 if (!get_area)
384 get_area = current->mm->get_unmapped_area;
fad1a86e 385#endif
ae5758a1 386
5721cf84
HD
387 if (get_area)
388 rv = get_area(file, orig_addr, len, pgoff, flags);
ae5758a1
JB
389 else
390 rv = orig_addr;
c4fe2448
AD
391 unuse_pde(pde);
392 }
393 return rv;
394}
395
786d7e16
AD
396static int proc_reg_open(struct inode *inode, struct file *file)
397{
398 struct proc_dir_entry *pde = PDE(inode);
399 int rv = 0;
d56c0d45
AD
400 typeof_member(struct proc_ops, proc_open) open;
401 typeof_member(struct proc_ops, proc_release) release;
881adb85
AD
402 struct pde_opener *pdeo;
403
404 /*
492b2da6
AD
405 * Ensure that
406 * 1) PDE's ->release hook will be called no matter what
407 * either normally by close()/->release, or forcefully by
408 * rmmod/remove_proc_entry.
409 *
410 * 2) rmmod isn't blocked by opening file in /proc and sitting on
411 * the descriptor (including "rmmod foo </proc/foo" scenario).
881adb85 412 *
492b2da6 413 * Save every "struct file" with custom ->release hook.
881adb85 414 */
e7a6e291 415 if (!use_pde(pde))
d2857e79 416 return -ENOENT;
e7a6e291 417
d56c0d45 418 release = pde->proc_ops->proc_release;
e7a6e291 419 if (release) {
195b8cf0 420 pdeo = kmem_cache_alloc(pde_opener_cache, GFP_KERNEL);
e7a6e291
AD
421 if (!pdeo) {
422 rv = -ENOMEM;
423 goto out_unuse;
424 }
425 }
786d7e16 426
d56c0d45 427 open = pde->proc_ops->proc_open;
786d7e16
AD
428 if (open)
429 rv = open(inode, file);
430
e7a6e291
AD
431 if (release) {
432 if (rv == 0) {
433 /* To know what to release. */
434 pdeo->file = file;
435 pdeo->closing = false;
436 pdeo->c = NULL;
437 spin_lock(&pde->pde_unload_lock);
438 list_add(&pdeo->lh, &pde->pde_openers);
439 spin_unlock(&pde->pde_unload_lock);
440 } else
195b8cf0 441 kmem_cache_free(pde_opener_cache, pdeo);
e7a6e291 442 }
05c0ae21 443
e7a6e291 444out_unuse:
05c0ae21 445 unuse_pde(pde);
786d7e16
AD
446 return rv;
447}
448
449static int proc_reg_release(struct inode *inode, struct file *file)
450{
451 struct proc_dir_entry *pde = PDE(inode);
881adb85 452 struct pde_opener *pdeo;
786d7e16 453 spin_lock(&pde->pde_unload_lock);
ca469f35
AV
454 list_for_each_entry(pdeo, &pde->pde_openers, lh) {
455 if (pdeo->file == file) {
456 close_pdeo(pde, pdeo);
2f897424 457 return 0;
ca469f35 458 }
881adb85 459 }
786d7e16 460 spin_unlock(&pde->pde_unload_lock);
ca469f35 461 return 0;
786d7e16
AD
462}
463
464static const struct file_operations proc_reg_file_ops = {
465 .llseek = proc_reg_llseek,
466 .read = proc_reg_read,
467 .write = proc_reg_write,
468 .poll = proc_reg_poll,
469 .unlocked_ioctl = proc_reg_unlocked_ioctl,
470#ifdef CONFIG_COMPAT
471 .compat_ioctl = proc_reg_compat_ioctl,
472#endif
473 .mmap = proc_reg_mmap,
c4fe2448 474 .get_unmapped_area = proc_reg_get_unmapped_area,
786d7e16
AD
475 .open = proc_reg_open,
476 .release = proc_reg_release,
477};
478
778f3dd5
DM
479#ifdef CONFIG_COMPAT
480static const struct file_operations proc_reg_file_ops_no_compat = {
481 .llseek = proc_reg_llseek,
482 .read = proc_reg_read,
483 .write = proc_reg_write,
484 .poll = proc_reg_poll,
485 .unlocked_ioctl = proc_reg_unlocked_ioctl,
486 .mmap = proc_reg_mmap,
c4fe2448 487 .get_unmapped_area = proc_reg_get_unmapped_area,
778f3dd5
DM
488 .open = proc_reg_open,
489 .release = proc_reg_release,
490};
491#endif
492
fceef393
AV
493static void proc_put_link(void *p)
494{
495 unuse_pde(p);
496}
497
6b255391 498static const char *proc_get_link(struct dentry *dentry,
fceef393
AV
499 struct inode *inode,
500 struct delayed_call *done)
7e0e953b 501{
6b255391 502 struct proc_dir_entry *pde = PDE(inode);
15b158b4 503 if (!use_pde(pde))
7e0e953b 504 return ERR_PTR(-EINVAL);
fceef393 505 set_delayed_call(done, proc_put_link, pde);
680baacb 506 return pde->data;
7e0e953b
AV
507}
508
7e0e953b 509const struct inode_operations proc_link_inode_operations = {
6b255391 510 .get_link = proc_get_link,
7e0e953b
AV
511};
512
6d1b6e4e 513struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
1da177e4 514{
51f0885e 515 struct inode *inode = new_inode_pseudo(sb);
1da177e4 516
51f0885e
LT
517 if (inode) {
518 inode->i_ino = de->low_ino;
078cd827 519 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
a1d4aebb 520 PROC_I(inode)->pde = de;
5e971dce 521
eb6d38d5
EB
522 if (is_empty_pde(de)) {
523 make_empty_dir_inode(inode);
524 return inode;
525 }
5e971dce
AD
526 if (de->mode) {
527 inode->i_mode = de->mode;
528 inode->i_uid = de->uid;
529 inode->i_gid = de->gid;
530 }
531 if (de->size)
532 inode->i_size = de->size;
533 if (de->nlink)
bfe86848 534 set_nlink(inode, de->nlink);
d56c0d45
AD
535
536 if (S_ISREG(inode->i_mode)) {
537 inode->i_op = de->proc_iops;
538 inode->i_fop = &proc_reg_file_ops;
778f3dd5 539#ifdef CONFIG_COMPAT
d56c0d45
AD
540 if (!de->proc_ops->proc_compat_ioctl) {
541 inode->i_fop = &proc_reg_file_ops_no_compat;
778f3dd5 542 }
d56c0d45
AD
543#endif
544 } else if (S_ISDIR(inode->i_mode)) {
545 inode->i_op = de->proc_iops;
546 inode->i_fop = de->proc_dir_ops;
547 } else if (S_ISLNK(inode->i_mode)) {
548 inode->i_op = de->proc_iops;
549 inode->i_fop = NULL;
550 } else
551 BUG();
99b76233 552 } else
135d5655 553 pde_put(de);
1da177e4 554 return inode;
d3d009cb 555}