Merge tag 'x86-apic-2024-03-12' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / fs / file_table.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * linux/fs/file_table.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
7 */
8
9#include <linux/string.h>
10#include <linux/slab.h>
11#include <linux/file.h>
9f3acc31 12#include <linux/fdtable.h>
1da177e4
LT
13#include <linux/init.h>
14#include <linux/module.h>
1da177e4 15#include <linux/fs.h>
5970e15d 16#include <linux/filelock.h>
1da177e4 17#include <linux/security.h>
5b825c3a 18#include <linux/cred.h>
1da177e4 19#include <linux/eventpoll.h>
ab2af1f5 20#include <linux/rcupdate.h>
1da177e4 21#include <linux/mount.h>
16f7e0fe 22#include <linux/capability.h>
1da177e4 23#include <linux/cdev.h>
0eeca283 24#include <linux/fsnotify.h>
529bf6be
DS
25#include <linux/sysctl.h>
26#include <linux/percpu_counter.h>
6416ccb7 27#include <linux/percpu.h>
4a9d4b02 28#include <linux/task_work.h>
0552f879 29#include <linux/ima.h>
4248b0da 30#include <linux/swap.h>
a3580ac9 31#include <linux/kmemleak.h>
529bf6be 32
60063497 33#include <linux/atomic.h>
1da177e4 34
e81e3f4d
EP
35#include "internal.h"
36
1da177e4 37/* sysctl tunables... */
204d5a24 38static struct files_stat_struct files_stat = {
1da177e4
LT
39 .max_files = NR_FILE
40};
41
b6b3fdea 42/* SLAB cache for file structures */
68279f9c 43static struct kmem_cache *filp_cachep __ro_after_init;
b6b3fdea 44
529bf6be 45static struct percpu_counter nr_files __cacheline_aligned_in_smp;
1da177e4 46
def3ae83 47/* Container for backing file with optional user path */
62d53c4a
AG
48struct backing_file {
49 struct file file;
def3ae83 50 struct path user_path;
62d53c4a
AG
51};
52
53static inline struct backing_file *backing_file(struct file *f)
54{
55 return container_of(f, struct backing_file, file);
56}
57
def3ae83 58struct path *backing_file_user_path(struct file *f)
62d53c4a 59{
def3ae83 60 return &backing_file(f)->user_path;
62d53c4a 61}
def3ae83 62EXPORT_SYMBOL_GPL(backing_file_user_path);
62d53c4a 63
529bf6be 64static inline void file_free(struct file *f)
1da177e4 65{
e8cff84f 66 security_file_free(f);
62d53c4a 67 if (likely(!(f->f_mode & FMODE_NOACCOUNT)))
d3b1084d 68 percpu_counter_dec(&nr_files);
0ede61d8
CB
69 put_cred(f->f_cred);
70 if (unlikely(f->f_mode & FMODE_BACKING)) {
def3ae83 71 path_put(backing_file_user_path(f));
6cf41fcf 72 kfree(backing_file(f));
0ede61d8
CB
73 } else {
74 kmem_cache_free(filp_cachep, f);
75 }
1da177e4
LT
76}
77
529bf6be
DS
78/*
79 * Return the total number of open files in the system
80 */
518de9b3 81static long get_nr_files(void)
1da177e4 82{
529bf6be 83 return percpu_counter_read_positive(&nr_files);
1da177e4
LT
84}
85
529bf6be
DS
86/*
87 * Return the maximum number of open files in the system
88 */
518de9b3 89unsigned long get_max_files(void)
ab2af1f5 90{
529bf6be 91 return files_stat.max_files;
ab2af1f5 92}
529bf6be
DS
93EXPORT_SYMBOL_GPL(get_max_files);
94
204d5a24
LC
95#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
96
529bf6be
DS
97/*
98 * Handle nr_files sysctl
99 */
204d5a24
LC
100static int proc_nr_files(struct ctl_table *table, int write, void *buffer,
101 size_t *lenp, loff_t *ppos)
529bf6be
DS
102{
103 files_stat.nr_files = get_nr_files();
518de9b3 104 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
529bf6be 105}
204d5a24
LC
106
107static struct ctl_table fs_stat_sysctls[] = {
108 {
109 .procname = "file-nr",
110 .data = &files_stat,
111 .maxlen = sizeof(files_stat),
112 .mode = 0444,
113 .proc_handler = proc_nr_files,
114 },
115 {
116 .procname = "file-max",
117 .data = &files_stat.max_files,
118 .maxlen = sizeof(files_stat.max_files),
119 .mode = 0644,
120 .proc_handler = proc_doulongvec_minmax,
121 .extra1 = SYSCTL_LONG_ZERO,
122 .extra2 = SYSCTL_LONG_MAX,
123 },
124 {
125 .procname = "nr_open",
126 .data = &sysctl_nr_open,
127 .maxlen = sizeof(unsigned int),
128 .mode = 0644,
129 .proc_handler = proc_dointvec_minmax,
130 .extra1 = &sysctl_nr_open_min,
131 .extra2 = &sysctl_nr_open_max,
132 },
204d5a24
LC
133};
134
135static int __init init_fs_stat_sysctls(void)
529bf6be 136{
204d5a24 137 register_sysctl_init("fs", fs_stat_sysctls);
a3580ac9
LC
138 if (IS_ENABLED(CONFIG_BINFMT_MISC)) {
139 struct ctl_table_header *hdr;
140 hdr = register_sysctl_mount_point("fs/binfmt_misc");
141 kmemleak_not_leak(hdr);
142 }
204d5a24 143 return 0;
529bf6be 144}
204d5a24 145fs_initcall(init_fs_stat_sysctls);
529bf6be 146#endif
ab2af1f5 147
8a05a8c3 148static int init_file(struct file *f, int flags, const struct cred *cred)
d3b1084d 149{
d3b1084d
MS
150 int error;
151
d3b1084d
MS
152 f->f_cred = get_cred(cred);
153 error = security_file_alloc(f);
154 if (unlikely(error)) {
dff745c1 155 put_cred(f->f_cred);
8a05a8c3 156 return error;
d3b1084d
MS
157 }
158
d3b1084d
MS
159 rwlock_init(&f->f_owner.lock);
160 spin_lock_init(&f->f_lock);
161 mutex_init(&f->f_pos_lock);
d3b1084d
MS
162 f->f_flags = flags;
163 f->f_mode = OPEN_FMODE(flags);
164 /* f->f_version: 0 */
165
0ede61d8
CB
166 /*
167 * We're SLAB_TYPESAFE_BY_RCU so initialize f_count last. While
168 * fget-rcu pattern users need to be able to handle spurious
169 * refcount bumps we should reinitialize the reused file first.
170 */
171 atomic_long_set(&f->f_count, 1);
8a05a8c3 172 return 0;
d3b1084d
MS
173}
174
1da177e4 175/* Find an unused file structure and return a pointer to it.
1afc99be
AV
176 * Returns an error pointer if some error happend e.g. we over file
177 * structures limit, run out of memory or operation is not permitted.
430e285e
DH
178 *
179 * Be very careful using this. You are responsible for
180 * getting write access to any mount that you might assign
181 * to this filp, if it is opened for write. If this is not
182 * done, you will imbalance int the mount's writer count
183 * and a warning at __fput() time.
1da177e4 184 */
ea73ea72 185struct file *alloc_empty_file(int flags, const struct cred *cred)
1da177e4 186{
518de9b3 187 static long old_max;
1afc99be 188 struct file *f;
8a05a8c3 189 int error;
1da177e4
LT
190
191 /*
192 * Privileged users can go above max_files
193 */
529bf6be
DS
194 if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
195 /*
196 * percpu_counters are inaccurate. Do an expensive check before
197 * we go and fail.
198 */
52d9f3b4 199 if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
529bf6be
DS
200 goto over;
201 }
af4d2ecb 202
8a05a8c3
AG
203 f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
204 if (unlikely(!f))
205 return ERR_PTR(-ENOMEM);
206
207 error = init_file(f, flags, cred);
dff745c1
AG
208 if (unlikely(error)) {
209 kmem_cache_free(filp_cachep, f);
8a05a8c3 210 return ERR_PTR(error);
dff745c1 211 }
8a05a8c3
AG
212
213 percpu_counter_inc(&nr_files);
1da177e4 214
af4d2ecb
KK
215 return f;
216
217over:
1da177e4 218 /* Ran out of filps - report that */
529bf6be 219 if (get_nr_files() > old_max) {
518de9b3 220 pr_info("VFS: file-max limit %lu reached\n", get_max_files());
529bf6be 221 old_max = get_nr_files();
1da177e4 222 }
1afc99be 223 return ERR_PTR(-ENFILE);
1da177e4
LT
224}
225
d3b1084d
MS
226/*
227 * Variant of alloc_empty_file() that doesn't check and modify nr_files.
228 *
8a05a8c3
AG
229 * This is only for kernel internal use, and the allocate file must not be
230 * installed into file tables or such.
d3b1084d
MS
231 */
232struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
233{
8a05a8c3
AG
234 struct file *f;
235 int error;
236
237 f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
238 if (unlikely(!f))
239 return ERR_PTR(-ENOMEM);
240
241 error = init_file(f, flags, cred);
dff745c1
AG
242 if (unlikely(error)) {
243 kmem_cache_free(filp_cachep, f);
8a05a8c3 244 return ERR_PTR(error);
dff745c1 245 }
d3b1084d 246
8a05a8c3 247 f->f_mode |= FMODE_NOACCOUNT;
d3b1084d
MS
248
249 return f;
250}
251
62d53c4a
AG
252/*
253 * Variant of alloc_empty_file() that allocates a backing_file container
254 * and doesn't check and modify nr_files.
255 *
256 * This is only for kernel internal use, and the allocate file must not be
257 * installed into file tables or such.
258 */
259struct file *alloc_empty_backing_file(int flags, const struct cred *cred)
260{
261 struct backing_file *ff;
262 int error;
263
264 ff = kzalloc(sizeof(struct backing_file), GFP_KERNEL);
265 if (unlikely(!ff))
266 return ERR_PTR(-ENOMEM);
267
268 error = init_file(&ff->file, flags, cred);
dff745c1
AG
269 if (unlikely(error)) {
270 kfree(ff);
62d53c4a 271 return ERR_PTR(error);
dff745c1 272 }
62d53c4a
AG
273
274 ff->file.f_mode |= FMODE_BACKING | FMODE_NOACCOUNT;
275 return &ff->file;
276}
277
ce8d2cdf 278/**
0873add0 279 * file_init_path - initialize a 'struct file' based on path
a457606a 280 *
0873add0 281 * @file: the file to set up
a457606a 282 * @path: the (dentry, vfsmount) pair for the new file
ce8d2cdf 283 * @fop: the 'struct file_operations' for the new file
ce8d2cdf 284 */
0873add0
CB
285static void file_init_path(struct file *file, const struct path *path,
286 const struct file_operations *fop)
ce8d2cdf 287{
2c48b9c4 288 file->f_path = *path;
dd37978c 289 file->f_inode = path->dentry->d_inode;
2c48b9c4 290 file->f_mapping = path->dentry->d_inode->i_mapping;
5660e13d 291 file->f_wb_err = filemap_sample_wb_err(file->f_mapping);
735e4ae5 292 file->f_sb_err = file_sample_sb_err(file);
868941b1 293 if (fop->llseek)
e7478158 294 file->f_mode |= FMODE_LSEEK;
c9c554f2 295 if ((file->f_mode & FMODE_READ) &&
84363182 296 likely(fop->read || fop->read_iter))
c9c554f2
AV
297 file->f_mode |= FMODE_CAN_READ;
298 if ((file->f_mode & FMODE_WRITE) &&
84363182 299 likely(fop->write || fop->write_iter))
c9c554f2 300 file->f_mode |= FMODE_CAN_WRITE;
164f4064 301 file->f_iocb_flags = iocb_flags(file);
f5d11409 302 file->f_mode |= FMODE_OPENED;
ce8d2cdf 303 file->f_op = fop;
c9c554f2 304 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
890275b5 305 i_readcount_inc(path->dentry->d_inode);
0873add0
CB
306}
307
308/**
309 * alloc_file - allocate and initialize a 'struct file'
310 *
311 * @path: the (dentry, vfsmount) pair for the new file
312 * @flags: O_... flags with which the new file will be opened
313 * @fop: the 'struct file_operations' for the new file
314 */
315static struct file *alloc_file(const struct path *path, int flags,
316 const struct file_operations *fop)
317{
318 struct file *file;
319
320 file = alloc_empty_file(flags, current_cred());
321 if (!IS_ERR(file))
322 file_init_path(file, path, fop);
3d1e4631 323 return file;
ce8d2cdf 324}
ce8d2cdf 325
0873add0
CB
326static inline int alloc_path_pseudo(const char *name, struct inode *inode,
327 struct vfsmount *mnt, struct path *path)
d93aa9d8 328{
d93aa9d8 329 struct qstr this = QSTR_INIT(name, strlen(name));
0873add0
CB
330
331 path->dentry = d_alloc_pseudo(mnt->mnt_sb, &this);
332 if (!path->dentry)
333 return -ENOMEM;
334 path->mnt = mntget(mnt);
335 d_instantiate(path->dentry, inode);
336 return 0;
337}
338
339struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
340 const char *name, int flags,
341 const struct file_operations *fops)
342{
343 int ret;
d93aa9d8
AV
344 struct path path;
345 struct file *file;
346
0873add0
CB
347 ret = alloc_path_pseudo(name, inode, mnt, &path);
348 if (ret)
349 return ERR_PTR(ret);
350
b6509f6a 351 file = alloc_file(&path, flags, fops);
d93aa9d8
AV
352 if (IS_ERR(file)) {
353 ihold(inode);
354 path_put(&path);
355 }
356 return file;
357}
358EXPORT_SYMBOL(alloc_file_pseudo);
359
bac0a9e5
CB
360struct file *alloc_file_pseudo_noaccount(struct inode *inode,
361 struct vfsmount *mnt, const char *name,
362 int flags,
363 const struct file_operations *fops)
364{
365 int ret;
366 struct path path;
367 struct file *file;
368
369 ret = alloc_path_pseudo(name, inode, mnt, &path);
370 if (ret)
371 return ERR_PTR(ret);
372
373 file = alloc_empty_file_noaccount(flags, current_cred());
374 if (IS_ERR(file)) {
375 ihold(inode);
376 path_put(&path);
377 return file;
378 }
379 file_init_path(file, &path, fops);
380 return file;
381}
382EXPORT_SYMBOL_GPL(alloc_file_pseudo_noaccount);
383
183266f2
AV
384struct file *alloc_file_clone(struct file *base, int flags,
385 const struct file_operations *fops)
386{
387 struct file *f = alloc_file(&base->f_path, flags, fops);
388 if (!IS_ERR(f)) {
389 path_get(&f->f_path);
390 f->f_mapping = base->f_mapping;
391 }
392 return f;
393}
394
d7065da0 395/* the real guts of fput() - releasing the last reference to file
1da177e4 396 */
d7065da0 397static void __fput(struct file *file)
1da177e4 398{
0f7fc9e4
JJS
399 struct dentry *dentry = file->f_path.dentry;
400 struct vfsmount *mnt = file->f_path.mnt;
c77cecee 401 struct inode *inode = file->f_inode;
a07b2000 402 fmode_t mode = file->f_mode;
1da177e4 403
4d27f326
AV
404 if (unlikely(!(file->f_mode & FMODE_OPENED)))
405 goto out;
406
1da177e4 407 might_sleep();
0eeca283
RL
408
409 fsnotify_close(file);
1da177e4
LT
410 /*
411 * The function eventpoll_release() should be the first called
412 * in the file cleanup chain.
413 */
414 eventpoll_release(file);
78ed8a13 415 locks_remove_file(file);
1da177e4 416
bb02b186 417 ima_file_free(file);
233e70f4 418 if (unlikely(file->f_flags & FASYNC)) {
72c2d531 419 if (file->f_op->fasync)
233e70f4
AV
420 file->f_op->fasync(-1, file, 0);
421 }
72c2d531 422 if (file->f_op->release)
1da177e4 423 file->f_op->release(inode, file);
60ed8cf7 424 if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
a07b2000 425 !(mode & FMODE_PATH))) {
1da177e4 426 cdev_put(inode->i_cdev);
60ed8cf7 427 }
1da177e4 428 fops_put(file->f_op);
609d7fa9 429 put_pid(file->f_owner.pid);
d6da19c9 430 put_file_access(file);
1da177e4 431 dput(dentry);
a07b2000
AV
432 if (unlikely(mode & FMODE_NEED_UNMOUNT))
433 dissolve_on_fput(mnt);
1da177e4 434 mntput(mnt);
4d27f326
AV
435out:
436 file_free(file);
1da177e4
LT
437}
438
4f5e65a1 439static LLIST_HEAD(delayed_fput_list);
4a9d4b02
AV
440static void delayed_fput(struct work_struct *unused)
441{
4f5e65a1 442 struct llist_node *node = llist_del_all(&delayed_fput_list);
b9ea557e 443 struct file *f, *t;
4f5e65a1 444
e87f2c26 445 llist_for_each_entry_safe(f, t, node, f_llist)
b9ea557e 446 __fput(f);
4a9d4b02
AV
447}
448
449static void ____fput(struct callback_head *work)
450{
372a34e6 451 __fput(container_of(work, struct file, f_task_work));
4a9d4b02
AV
452}
453
454/*
455 * If kernel thread really needs to have the final fput() it has done
456 * to complete, call this. The only user right now is the boot - we
457 * *do* need to make sure our writes to binaries on initramfs has
458 * not left us with opened struct file waiting for __fput() - execve()
459 * won't work without that. Please, don't add more callers without
460 * very good reasons; in particular, never call that with locks
461 * held and never call that from a thread that might need to do
462 * some work on any kind of umount.
463 */
464void flush_delayed_fput(void)
465{
466 delayed_fput(NULL);
467}
7239a40c 468EXPORT_SYMBOL_GPL(flush_delayed_fput);
4a9d4b02 469
c7314d74 470static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
4a9d4b02 471
81132a39 472void fput(struct file *file)
d7065da0 473{
81132a39 474 if (atomic_long_dec_and_test(&file->f_count)) {
4a9d4b02 475 struct task_struct *task = current;
e7b2c406 476
7cb537b6
AV
477 if (unlikely(!(file->f_mode & (FMODE_BACKING | FMODE_OPENED)))) {
478 file_free(file);
479 return;
480 }
e7b2c406 481 if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
372a34e6
CB
482 init_task_work(&file->f_task_work, ____fput);
483 if (!task_work_add(task, &file->f_task_work, TWA_RESUME))
e7b2c406 484 return;
64372501
AM
485 /*
486 * After this task has run exit_task_work(),
be49b30a 487 * task_work_add() will fail. Fall through to delayed
64372501
AM
488 * fput to avoid leaking *file.
489 */
4a9d4b02 490 }
4f5e65a1 491
e87f2c26 492 if (llist_add(&file->f_llist, &delayed_fput_list))
c7314d74 493 schedule_delayed_work(&delayed_fput_work, 1);
4a9d4b02
AV
494 }
495}
496
497/*
498 * synchronous analog of fput(); for kernel threads that might be needed
499 * in some umount() (and thus can't use flush_delayed_fput() without
500 * risking deadlocks), need to wait for completion of __fput() and know
501 * for this specific struct file it won't involve anything that would
502 * need them. Use only if you really need it - at the very least,
503 * don't blindly convert fput() by kernel thread to that.
504 */
505void __fput_sync(struct file *file)
506{
021a160a 507 if (atomic_long_dec_and_test(&file->f_count))
d7065da0
AV
508 __fput(file);
509}
510
511EXPORT_SYMBOL(fput);
f0043206 512EXPORT_SYMBOL(__fput_sync);
d7065da0 513
4248b0da 514void __init files_init(void)
b9ea557e 515{
b6b3fdea 516 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
0ede61d8
CB
517 SLAB_TYPESAFE_BY_RCU | SLAB_HWCACHE_ALIGN |
518 SLAB_PANIC | SLAB_ACCOUNT, NULL);
4248b0da
MG
519 percpu_counter_init(&nr_files, 0, GFP_KERNEL);
520}
b6b3fdea 521
4248b0da
MG
522/*
523 * One file with associated inode and dcache is very roughly 1K. Per default
524 * do not use more than 10% of our memory for files.
525 */
526void __init files_maxfiles_init(void)
527{
528 unsigned long n;
ca79b0c2 529 unsigned long nr_pages = totalram_pages();
3d6357de 530 unsigned long memreserve = (nr_pages - nr_free_pages()) * 3/2;
4248b0da 531
3d6357de
AK
532 memreserve = min(memreserve, nr_pages - 1);
533 n = ((nr_pages - memreserve) * (PAGE_SIZE / 1024)) / 10;
1da177e4 534
518de9b3 535 files_stat.max_files = max_t(unsigned long, n, NR_FILE);
b9ea557e 536}