1 // SPDX-License-Identifier: GPL-2.0-only 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> 12 #include <linux/fdtable.h> 13 #include <linux/init.h> 14 #include <linux/module.h> 15 #include <linux/fs.h> 16 #include <linux/filelock.h> 17 #include <linux/security.h> 18 #include <linux/cred.h> 19 #include <linux/eventpoll.h> 20 #include <linux/rcupdate.h> 21 #include <linux/mount.h> 22 #include <linux/capability.h> 23 #include <linux/cdev.h> 24 #include <linux/fsnotify.h> 25 #include <linux/sysctl.h> 26 #include <linux/percpu_counter.h> 27 #include <linux/percpu.h> 28 #include <linux/task_work.h> 29 #include <linux/ima.h> 30 #include <linux/swap.h> 31 #include <linux/kmemleak.h> 32 33 #include <linux/atomic.h> 34 35 #include "internal.h" 36 37 /* sysctl tunables... */ 38 static struct files_stat_struct files_stat = { 39 .max_files = NR_FILE 40 }; 41 42 /* SLAB cache for file structures */ 43 static struct kmem_cache *filp_cachep __ro_after_init; 44 45 static struct percpu_counter nr_files __cacheline_aligned_in_smp; 46 47 /* Container for backing file with optional user path */ 48 struct backing_file { 49 struct file file; 50 struct path user_path; 51 }; 52 53 static inline struct backing_file *backing_file(struct file *f) 54 { 55 return container_of(f, struct backing_file, file); 56 } 57 58 struct path *backing_file_user_path(struct file *f) 59 { 60 return &backing_file(f)->user_path; 61 } 62 EXPORT_SYMBOL_GPL(backing_file_user_path); 63 64 static inline void file_free(struct file *f) 65 { 66 security_file_free(f); 67 if (likely(!(f->f_mode & FMODE_NOACCOUNT))) 68 percpu_counter_dec(&nr_files); 69 put_cred(f->f_cred); 70 if (unlikely(f->f_mode & FMODE_BACKING)) { 71 path_put(backing_file_user_path(f)); 72 kfree(backing_file(f)); 73 } else { 74 kmem_cache_free(filp_cachep, f); 75 } 76 } 77 78 /* 79 * Return the total number of open files in the system 80 */ 81 static long get_nr_files(void) 82 { 83 return percpu_counter_read_positive(&nr_files); 84 } 85 86 /* 87 * Return the maximum number of open files in the system 88 */ 89 unsigned long get_max_files(void) 90 { 91 return files_stat.max_files; 92 } 93 EXPORT_SYMBOL_GPL(get_max_files); 94 95 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS) 96 97 /* 98 * Handle nr_files sysctl 99 */ 100 static int proc_nr_files(struct ctl_table *table, int write, void *buffer, 101 size_t *lenp, loff_t *ppos) 102 { 103 files_stat.nr_files = get_nr_files(); 104 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos); 105 } 106 107 static 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 }, 133 }; 134 135 static int __init init_fs_stat_sysctls(void) 136 { 137 register_sysctl_init("fs", fs_stat_sysctls); 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 } 143 return 0; 144 } 145 fs_initcall(init_fs_stat_sysctls); 146 #endif 147 148 static int init_file(struct file *f, int flags, const struct cred *cred) 149 { 150 int error; 151 152 f->f_cred = get_cred(cred); 153 error = security_file_alloc(f); 154 if (unlikely(error)) { 155 put_cred(f->f_cred); 156 return error; 157 } 158 159 rwlock_init(&f->f_owner.lock); 160 spin_lock_init(&f->f_lock); 161 mutex_init(&f->f_pos_lock); 162 f->f_flags = flags; 163 f->f_mode = OPEN_FMODE(flags); 164 /* f->f_version: 0 */ 165 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); 172 return 0; 173 } 174 175 /* Find an unused file structure and return a pointer to it. 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. 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. 184 */ 185 struct file *alloc_empty_file(int flags, const struct cred *cred) 186 { 187 static long old_max; 188 struct file *f; 189 int error; 190 191 /* 192 * Privileged users can go above max_files 193 */ 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 */ 199 if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files) 200 goto over; 201 } 202 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); 208 if (unlikely(error)) { 209 kmem_cache_free(filp_cachep, f); 210 return ERR_PTR(error); 211 } 212 213 percpu_counter_inc(&nr_files); 214 215 return f; 216 217 over: 218 /* Ran out of filps - report that */ 219 if (get_nr_files() > old_max) { 220 pr_info("VFS: file-max limit %lu reached\n", get_max_files()); 221 old_max = get_nr_files(); 222 } 223 return ERR_PTR(-ENFILE); 224 } 225 226 /* 227 * Variant of alloc_empty_file() that doesn't check and modify nr_files. 228 * 229 * This is only for kernel internal use, and the allocate file must not be 230 * installed into file tables or such. 231 */ 232 struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred) 233 { 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); 242 if (unlikely(error)) { 243 kmem_cache_free(filp_cachep, f); 244 return ERR_PTR(error); 245 } 246 247 f->f_mode |= FMODE_NOACCOUNT; 248 249 return f; 250 } 251 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 */ 259 struct 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); 269 if (unlikely(error)) { 270 kfree(ff); 271 return ERR_PTR(error); 272 } 273 274 ff->file.f_mode |= FMODE_BACKING | FMODE_NOACCOUNT; 275 return &ff->file; 276 } 277 278 /** 279 * alloc_file - allocate and initialize a 'struct file' 280 * 281 * @path: the (dentry, vfsmount) pair for the new file 282 * @flags: O_... flags with which the new file will be opened 283 * @fop: the 'struct file_operations' for the new file 284 */ 285 static struct file *alloc_file(const struct path *path, int flags, 286 const struct file_operations *fop) 287 { 288 struct file *file; 289 290 file = alloc_empty_file(flags, current_cred()); 291 if (IS_ERR(file)) 292 return file; 293 294 file->f_path = *path; 295 file->f_inode = path->dentry->d_inode; 296 file->f_mapping = path->dentry->d_inode->i_mapping; 297 file->f_wb_err = filemap_sample_wb_err(file->f_mapping); 298 file->f_sb_err = file_sample_sb_err(file); 299 if (fop->llseek) 300 file->f_mode |= FMODE_LSEEK; 301 if ((file->f_mode & FMODE_READ) && 302 likely(fop->read || fop->read_iter)) 303 file->f_mode |= FMODE_CAN_READ; 304 if ((file->f_mode & FMODE_WRITE) && 305 likely(fop->write || fop->write_iter)) 306 file->f_mode |= FMODE_CAN_WRITE; 307 file->f_iocb_flags = iocb_flags(file); 308 file->f_mode |= FMODE_OPENED; 309 file->f_op = fop; 310 if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ) 311 i_readcount_inc(path->dentry->d_inode); 312 return file; 313 } 314 315 struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt, 316 const char *name, int flags, 317 const struct file_operations *fops) 318 { 319 struct qstr this = QSTR_INIT(name, strlen(name)); 320 struct path path; 321 struct file *file; 322 323 path.dentry = d_alloc_pseudo(mnt->mnt_sb, &this); 324 if (!path.dentry) 325 return ERR_PTR(-ENOMEM); 326 path.mnt = mntget(mnt); 327 d_instantiate(path.dentry, inode); 328 file = alloc_file(&path, flags, fops); 329 if (IS_ERR(file)) { 330 ihold(inode); 331 path_put(&path); 332 } 333 return file; 334 } 335 EXPORT_SYMBOL(alloc_file_pseudo); 336 337 struct file *alloc_file_clone(struct file *base, int flags, 338 const struct file_operations *fops) 339 { 340 struct file *f = alloc_file(&base->f_path, flags, fops); 341 if (!IS_ERR(f)) { 342 path_get(&f->f_path); 343 f->f_mapping = base->f_mapping; 344 } 345 return f; 346 } 347 348 /* the real guts of fput() - releasing the last reference to file 349 */ 350 static void __fput(struct file *file) 351 { 352 struct dentry *dentry = file->f_path.dentry; 353 struct vfsmount *mnt = file->f_path.mnt; 354 struct inode *inode = file->f_inode; 355 fmode_t mode = file->f_mode; 356 357 if (unlikely(!(file->f_mode & FMODE_OPENED))) 358 goto out; 359 360 might_sleep(); 361 362 fsnotify_close(file); 363 /* 364 * The function eventpoll_release() should be the first called 365 * in the file cleanup chain. 366 */ 367 eventpoll_release(file); 368 locks_remove_file(file); 369 370 ima_file_free(file); 371 if (unlikely(file->f_flags & FASYNC)) { 372 if (file->f_op->fasync) 373 file->f_op->fasync(-1, file, 0); 374 } 375 if (file->f_op->release) 376 file->f_op->release(inode, file); 377 if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL && 378 !(mode & FMODE_PATH))) { 379 cdev_put(inode->i_cdev); 380 } 381 fops_put(file->f_op); 382 put_pid(file->f_owner.pid); 383 put_file_access(file); 384 dput(dentry); 385 if (unlikely(mode & FMODE_NEED_UNMOUNT)) 386 dissolve_on_fput(mnt); 387 mntput(mnt); 388 out: 389 file_free(file); 390 } 391 392 static LLIST_HEAD(delayed_fput_list); 393 static void delayed_fput(struct work_struct *unused) 394 { 395 struct llist_node *node = llist_del_all(&delayed_fput_list); 396 struct file *f, *t; 397 398 llist_for_each_entry_safe(f, t, node, f_llist) 399 __fput(f); 400 } 401 402 static void ____fput(struct callback_head *work) 403 { 404 __fput(container_of(work, struct file, f_task_work)); 405 } 406 407 /* 408 * If kernel thread really needs to have the final fput() it has done 409 * to complete, call this. The only user right now is the boot - we 410 * *do* need to make sure our writes to binaries on initramfs has 411 * not left us with opened struct file waiting for __fput() - execve() 412 * won't work without that. Please, don't add more callers without 413 * very good reasons; in particular, never call that with locks 414 * held and never call that from a thread that might need to do 415 * some work on any kind of umount. 416 */ 417 void flush_delayed_fput(void) 418 { 419 delayed_fput(NULL); 420 } 421 EXPORT_SYMBOL_GPL(flush_delayed_fput); 422 423 static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput); 424 425 void fput(struct file *file) 426 { 427 if (atomic_long_dec_and_test(&file->f_count)) { 428 struct task_struct *task = current; 429 430 if (unlikely(!(file->f_mode & (FMODE_BACKING | FMODE_OPENED)))) { 431 file_free(file); 432 return; 433 } 434 if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) { 435 init_task_work(&file->f_task_work, ____fput); 436 if (!task_work_add(task, &file->f_task_work, TWA_RESUME)) 437 return; 438 /* 439 * After this task has run exit_task_work(), 440 * task_work_add() will fail. Fall through to delayed 441 * fput to avoid leaking *file. 442 */ 443 } 444 445 if (llist_add(&file->f_llist, &delayed_fput_list)) 446 schedule_delayed_work(&delayed_fput_work, 1); 447 } 448 } 449 450 /* 451 * synchronous analog of fput(); for kernel threads that might be needed 452 * in some umount() (and thus can't use flush_delayed_fput() without 453 * risking deadlocks), need to wait for completion of __fput() and know 454 * for this specific struct file it won't involve anything that would 455 * need them. Use only if you really need it - at the very least, 456 * don't blindly convert fput() by kernel thread to that. 457 */ 458 void __fput_sync(struct file *file) 459 { 460 if (atomic_long_dec_and_test(&file->f_count)) 461 __fput(file); 462 } 463 464 EXPORT_SYMBOL(fput); 465 EXPORT_SYMBOL(__fput_sync); 466 467 void __init files_init(void) 468 { 469 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0, 470 SLAB_TYPESAFE_BY_RCU | SLAB_HWCACHE_ALIGN | 471 SLAB_PANIC | SLAB_ACCOUNT, NULL); 472 percpu_counter_init(&nr_files, 0, GFP_KERNEL); 473 } 474 475 /* 476 * One file with associated inode and dcache is very roughly 1K. Per default 477 * do not use more than 10% of our memory for files. 478 */ 479 void __init files_maxfiles_init(void) 480 { 481 unsigned long n; 482 unsigned long nr_pages = totalram_pages(); 483 unsigned long memreserve = (nr_pages - nr_free_pages()) * 3/2; 484 485 memreserve = min(memreserve, nr_pages - 1); 486 n = ((nr_pages - memreserve) * (PAGE_SIZE / 1024)) / 10; 487 488 files_stat.max_files = max_t(unsigned long, n, NR_FILE); 489 } 490