file_table.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 ([email protected])
  7. */
  8. #include <linux/string.h>
  9. #include <linux/slab.h>
  10. #include <linux/file.h>
  11. #include <linux/fdtable.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/fs.h>
  15. #include <linux/security.h>
  16. #include <linux/cred.h>
  17. #include <linux/eventpoll.h>
  18. #include <linux/rcupdate.h>
  19. #include <linux/mount.h>
  20. #include <linux/capability.h>
  21. #include <linux/cdev.h>
  22. #include <linux/fsnotify.h>
  23. #include <linux/sysctl.h>
  24. #include <linux/percpu_counter.h>
  25. #include <linux/percpu.h>
  26. #include <linux/task_work.h>
  27. #include <linux/ima.h>
  28. #include <linux/swap.h>
  29. #include <linux/kmemleak.h>
  30. #include <linux/atomic.h>
  31. #include "internal.h"
  32. /* sysctl tunables... */
  33. static struct files_stat_struct files_stat = {
  34. .max_files = NR_FILE
  35. };
  36. /* SLAB cache for file structures */
  37. static struct kmem_cache *filp_cachep __read_mostly;
  38. static struct percpu_counter nr_files __cacheline_aligned_in_smp;
  39. static void file_free_rcu(struct rcu_head *head)
  40. {
  41. struct file *f = container_of(head, struct file, f_rcuhead);
  42. put_cred(f->f_cred);
  43. kmem_cache_free(filp_cachep, f);
  44. }
  45. static inline void file_free(struct file *f)
  46. {
  47. security_file_free(f);
  48. if (!(f->f_mode & FMODE_NOACCOUNT))
  49. percpu_counter_dec(&nr_files);
  50. call_rcu(&f->f_rcuhead, file_free_rcu);
  51. }
  52. /*
  53. * Return the total number of open files in the system
  54. */
  55. static long get_nr_files(void)
  56. {
  57. return percpu_counter_read_positive(&nr_files);
  58. }
  59. /*
  60. * Return the maximum number of open files in the system
  61. */
  62. unsigned long get_max_files(void)
  63. {
  64. return files_stat.max_files;
  65. }
  66. EXPORT_SYMBOL_GPL(get_max_files);
  67. #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
  68. /*
  69. * Handle nr_files sysctl
  70. */
  71. static int proc_nr_files(struct ctl_table *table, int write, void *buffer,
  72. size_t *lenp, loff_t *ppos)
  73. {
  74. files_stat.nr_files = get_nr_files();
  75. return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
  76. }
  77. static struct ctl_table fs_stat_sysctls[] = {
  78. {
  79. .procname = "file-nr",
  80. .data = &files_stat,
  81. .maxlen = sizeof(files_stat),
  82. .mode = 0444,
  83. .proc_handler = proc_nr_files,
  84. },
  85. {
  86. .procname = "file-max",
  87. .data = &files_stat.max_files,
  88. .maxlen = sizeof(files_stat.max_files),
  89. .mode = 0644,
  90. .proc_handler = proc_doulongvec_minmax,
  91. .extra1 = SYSCTL_LONG_ZERO,
  92. .extra2 = SYSCTL_LONG_MAX,
  93. },
  94. {
  95. .procname = "nr_open",
  96. .data = &sysctl_nr_open,
  97. .maxlen = sizeof(unsigned int),
  98. .mode = 0644,
  99. .proc_handler = proc_dointvec_minmax,
  100. .extra1 = &sysctl_nr_open_min,
  101. .extra2 = &sysctl_nr_open_max,
  102. },
  103. { }
  104. };
  105. static int __init init_fs_stat_sysctls(void)
  106. {
  107. register_sysctl_init("fs", fs_stat_sysctls);
  108. if (IS_ENABLED(CONFIG_BINFMT_MISC)) {
  109. struct ctl_table_header *hdr;
  110. hdr = register_sysctl_mount_point("fs/binfmt_misc");
  111. kmemleak_not_leak(hdr);
  112. }
  113. return 0;
  114. }
  115. fs_initcall(init_fs_stat_sysctls);
  116. #endif
  117. static struct file *__alloc_file(int flags, const struct cred *cred)
  118. {
  119. struct file *f;
  120. int error;
  121. f = kmem_cache_zalloc(filp_cachep, GFP_KERNEL);
  122. if (unlikely(!f))
  123. return ERR_PTR(-ENOMEM);
  124. f->f_cred = get_cred(cred);
  125. error = security_file_alloc(f);
  126. if (unlikely(error)) {
  127. file_free_rcu(&f->f_rcuhead);
  128. return ERR_PTR(error);
  129. }
  130. atomic_long_set(&f->f_count, 1);
  131. rwlock_init(&f->f_owner.lock);
  132. spin_lock_init(&f->f_lock);
  133. mutex_init(&f->f_pos_lock);
  134. f->f_flags = flags;
  135. f->f_mode = OPEN_FMODE(flags);
  136. /* f->f_version: 0 */
  137. return f;
  138. }
  139. /* Find an unused file structure and return a pointer to it.
  140. * Returns an error pointer if some error happend e.g. we over file
  141. * structures limit, run out of memory or operation is not permitted.
  142. *
  143. * Be very careful using this. You are responsible for
  144. * getting write access to any mount that you might assign
  145. * to this filp, if it is opened for write. If this is not
  146. * done, you will imbalance int the mount's writer count
  147. * and a warning at __fput() time.
  148. */
  149. struct file *alloc_empty_file(int flags, const struct cred *cred)
  150. {
  151. static long old_max;
  152. struct file *f;
  153. /*
  154. * Privileged users can go above max_files
  155. */
  156. if (get_nr_files() >= files_stat.max_files && !capable(CAP_SYS_ADMIN)) {
  157. /*
  158. * percpu_counters are inaccurate. Do an expensive check before
  159. * we go and fail.
  160. */
  161. if (percpu_counter_sum_positive(&nr_files) >= files_stat.max_files)
  162. goto over;
  163. }
  164. f = __alloc_file(flags, cred);
  165. if (!IS_ERR(f))
  166. percpu_counter_inc(&nr_files);
  167. return f;
  168. over:
  169. /* Ran out of filps - report that */
  170. if (get_nr_files() > old_max) {
  171. pr_info("VFS: file-max limit %lu reached\n", get_max_files());
  172. old_max = get_nr_files();
  173. }
  174. return ERR_PTR(-ENFILE);
  175. }
  176. /*
  177. * Variant of alloc_empty_file() that doesn't check and modify nr_files.
  178. *
  179. * Should not be used unless there's a very good reason to do so.
  180. */
  181. struct file *alloc_empty_file_noaccount(int flags, const struct cred *cred)
  182. {
  183. struct file *f = __alloc_file(flags, cred);
  184. if (!IS_ERR(f))
  185. f->f_mode |= FMODE_NOACCOUNT;
  186. return f;
  187. }
  188. /**
  189. * alloc_file - allocate and initialize a 'struct file'
  190. *
  191. * @path: the (dentry, vfsmount) pair for the new file
  192. * @flags: O_... flags with which the new file will be opened
  193. * @fop: the 'struct file_operations' for the new file
  194. */
  195. static struct file *alloc_file(const struct path *path, int flags,
  196. const struct file_operations *fop)
  197. {
  198. struct file *file;
  199. file = alloc_empty_file(flags, current_cred());
  200. if (IS_ERR(file))
  201. return file;
  202. file->f_path = *path;
  203. file->f_inode = path->dentry->d_inode;
  204. file->f_mapping = path->dentry->d_inode->i_mapping;
  205. file->f_wb_err = filemap_sample_wb_err(file->f_mapping);
  206. file->f_sb_err = file_sample_sb_err(file);
  207. if (fop->llseek)
  208. file->f_mode |= FMODE_LSEEK;
  209. if ((file->f_mode & FMODE_READ) &&
  210. likely(fop->read || fop->read_iter))
  211. file->f_mode |= FMODE_CAN_READ;
  212. if ((file->f_mode & FMODE_WRITE) &&
  213. likely(fop->write || fop->write_iter))
  214. file->f_mode |= FMODE_CAN_WRITE;
  215. file->f_iocb_flags = iocb_flags(file);
  216. file->f_mode |= FMODE_OPENED;
  217. file->f_op = fop;
  218. if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  219. i_readcount_inc(path->dentry->d_inode);
  220. return file;
  221. }
  222. struct file *alloc_file_pseudo(struct inode *inode, struct vfsmount *mnt,
  223. const char *name, int flags,
  224. const struct file_operations *fops)
  225. {
  226. static const struct dentry_operations anon_ops = {
  227. .d_dname = simple_dname
  228. };
  229. struct qstr this = QSTR_INIT(name, strlen(name));
  230. struct path path;
  231. struct file *file;
  232. path.dentry = d_alloc_pseudo(mnt->mnt_sb, &this);
  233. if (!path.dentry)
  234. return ERR_PTR(-ENOMEM);
  235. if (!mnt->mnt_sb->s_d_op)
  236. d_set_d_op(path.dentry, &anon_ops);
  237. path.mnt = mntget(mnt);
  238. d_instantiate(path.dentry, inode);
  239. file = alloc_file(&path, flags, fops);
  240. if (IS_ERR(file)) {
  241. ihold(inode);
  242. path_put(&path);
  243. }
  244. return file;
  245. }
  246. EXPORT_SYMBOL(alloc_file_pseudo);
  247. struct file *alloc_file_clone(struct file *base, int flags,
  248. const struct file_operations *fops)
  249. {
  250. struct file *f = alloc_file(&base->f_path, flags, fops);
  251. if (!IS_ERR(f)) {
  252. path_get(&f->f_path);
  253. f->f_mapping = base->f_mapping;
  254. }
  255. return f;
  256. }
  257. /* the real guts of fput() - releasing the last reference to file
  258. */
  259. static void __fput(struct file *file)
  260. {
  261. struct dentry *dentry = file->f_path.dentry;
  262. struct vfsmount *mnt = file->f_path.mnt;
  263. struct inode *inode = file->f_inode;
  264. fmode_t mode = file->f_mode;
  265. if (unlikely(!(file->f_mode & FMODE_OPENED)))
  266. goto out;
  267. might_sleep();
  268. fsnotify_close(file);
  269. /*
  270. * The function eventpoll_release() should be the first called
  271. * in the file cleanup chain.
  272. */
  273. eventpoll_release(file);
  274. locks_remove_file(file);
  275. ima_file_free(file);
  276. if (unlikely(file->f_flags & FASYNC)) {
  277. if (file->f_op->fasync)
  278. file->f_op->fasync(-1, file, 0);
  279. }
  280. if (file->f_op->release)
  281. file->f_op->release(inode, file);
  282. if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL &&
  283. !(mode & FMODE_PATH))) {
  284. cdev_put(inode->i_cdev);
  285. }
  286. fops_put(file->f_op);
  287. put_pid(file->f_owner.pid);
  288. put_file_access(file);
  289. dput(dentry);
  290. if (unlikely(mode & FMODE_NEED_UNMOUNT))
  291. dissolve_on_fput(mnt);
  292. mntput(mnt);
  293. out:
  294. file_free(file);
  295. }
  296. static LLIST_HEAD(delayed_fput_list);
  297. static void delayed_fput(struct work_struct *unused)
  298. {
  299. struct llist_node *node = llist_del_all(&delayed_fput_list);
  300. struct file *f, *t;
  301. llist_for_each_entry_safe(f, t, node, f_llist)
  302. __fput(f);
  303. }
  304. static void ____fput(struct callback_head *work)
  305. {
  306. __fput(container_of(work, struct file, f_rcuhead));
  307. }
  308. /*
  309. * If kernel thread really needs to have the final fput() it has done
  310. * to complete, call this. The only user right now is the boot - we
  311. * *do* need to make sure our writes to binaries on initramfs has
  312. * not left us with opened struct file waiting for __fput() - execve()
  313. * won't work without that. Please, don't add more callers without
  314. * very good reasons; in particular, never call that with locks
  315. * held and never call that from a thread that might need to do
  316. * some work on any kind of umount.
  317. */
  318. void flush_delayed_fput(void)
  319. {
  320. delayed_fput(NULL);
  321. }
  322. EXPORT_SYMBOL_GPL(flush_delayed_fput);
  323. static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
  324. void fput(struct file *file)
  325. {
  326. if (atomic_long_dec_and_test(&file->f_count)) {
  327. struct task_struct *task = current;
  328. if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
  329. init_task_work(&file->f_rcuhead, ____fput);
  330. if (!task_work_add(task, &file->f_rcuhead, TWA_RESUME))
  331. return;
  332. /*
  333. * After this task has run exit_task_work(),
  334. * task_work_add() will fail. Fall through to delayed
  335. * fput to avoid leaking *file.
  336. */
  337. }
  338. if (llist_add(&file->f_llist, &delayed_fput_list))
  339. schedule_delayed_work(&delayed_fput_work, 1);
  340. }
  341. }
  342. /*
  343. * synchronous analog of fput(); for kernel threads that might be needed
  344. * in some umount() (and thus can't use flush_delayed_fput() without
  345. * risking deadlocks), need to wait for completion of __fput() and know
  346. * for this specific struct file it won't involve anything that would
  347. * need them. Use only if you really need it - at the very least,
  348. * don't blindly convert fput() by kernel thread to that.
  349. */
  350. void __fput_sync(struct file *file)
  351. {
  352. if (atomic_long_dec_and_test(&file->f_count)) {
  353. struct task_struct *task = current;
  354. BUG_ON(!(task->flags & PF_KTHREAD));
  355. __fput(file);
  356. }
  357. }
  358. EXPORT_SYMBOL(fput);
  359. EXPORT_SYMBOL(__fput_sync);
  360. void __init files_init(void)
  361. {
  362. filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
  363. SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT, NULL);
  364. percpu_counter_init(&nr_files, 0, GFP_KERNEL);
  365. }
  366. /*
  367. * One file with associated inode and dcache is very roughly 1K. Per default
  368. * do not use more than 10% of our memory for files.
  369. */
  370. void __init files_maxfiles_init(void)
  371. {
  372. unsigned long n;
  373. unsigned long nr_pages = totalram_pages();
  374. unsigned long memreserve = (nr_pages - nr_free_pages()) * 3/2;
  375. memreserve = min(memreserve, nr_pages - 1);
  376. n = ((nr_pages - memreserve) * (PAGE_SIZE / 1024)) / 10;
  377. files_stat.max_files = max_t(unsigned long, n, NR_FILE);
  378. }