file_table.c 10 KB

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