file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * linux/fs/file.c
  3. *
  4. * Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
  5. *
  6. * Manage the dynamic fd arrays in the process files_struct.
  7. */
  8. #include <linux/export.h>
  9. #include <linux/fs.h>
  10. #include <linux/mm.h>
  11. #include <linux/mmzone.h>
  12. #include <linux/time.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/file.h>
  17. #include <linux/fdtable.h>
  18. #include <linux/bitops.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/rcupdate.h>
  22. #include <linux/workqueue.h>
  23. struct fdtable_defer {
  24. spinlock_t lock;
  25. struct work_struct wq;
  26. struct fdtable *next;
  27. };
  28. int sysctl_nr_open __read_mostly = 1024*1024;
  29. int sysctl_nr_open_min = BITS_PER_LONG;
  30. int sysctl_nr_open_max = 1024 * 1024; /* raised later */
  31. /*
  32. * We use this list to defer free fdtables that have vmalloced
  33. * sets/arrays. By keeping a per-cpu list, we avoid having to embed
  34. * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
  35. * this per-task structure.
  36. */
  37. static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
  38. static void *alloc_fdmem(size_t size)
  39. {
  40. /*
  41. * Very large allocations can stress page reclaim, so fall back to
  42. * vmalloc() if the allocation size will be considered "large" by the VM.
  43. */
  44. if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
  45. void *data = kmalloc(size, GFP_KERNEL|__GFP_NOWARN);
  46. if (data != NULL)
  47. return data;
  48. }
  49. return vmalloc(size);
  50. }
  51. static void free_fdmem(void *ptr)
  52. {
  53. is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
  54. }
  55. static void __free_fdtable(struct fdtable *fdt)
  56. {
  57. free_fdmem(fdt->fd);
  58. free_fdmem(fdt->open_fds);
  59. kfree(fdt);
  60. }
  61. static void free_fdtable_work(struct work_struct *work)
  62. {
  63. struct fdtable_defer *f =
  64. container_of(work, struct fdtable_defer, wq);
  65. struct fdtable *fdt;
  66. spin_lock_bh(&f->lock);
  67. fdt = f->next;
  68. f->next = NULL;
  69. spin_unlock_bh(&f->lock);
  70. while(fdt) {
  71. struct fdtable *next = fdt->next;
  72. __free_fdtable(fdt);
  73. fdt = next;
  74. }
  75. }
  76. static void free_fdtable_rcu(struct rcu_head *rcu)
  77. {
  78. struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
  79. struct fdtable_defer *fddef;
  80. BUG_ON(!fdt);
  81. BUG_ON(fdt->max_fds <= NR_OPEN_DEFAULT);
  82. if (!is_vmalloc_addr(fdt->fd) && !is_vmalloc_addr(fdt->open_fds)) {
  83. kfree(fdt->fd);
  84. kfree(fdt->open_fds);
  85. kfree(fdt);
  86. } else {
  87. fddef = &get_cpu_var(fdtable_defer_list);
  88. spin_lock(&fddef->lock);
  89. fdt->next = fddef->next;
  90. fddef->next = fdt;
  91. /* vmallocs are handled from the workqueue context */
  92. schedule_work(&fddef->wq);
  93. spin_unlock(&fddef->lock);
  94. put_cpu_var(fdtable_defer_list);
  95. }
  96. }
  97. /*
  98. * Expand the fdset in the files_struct. Called with the files spinlock
  99. * held for write.
  100. */
  101. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
  102. {
  103. unsigned int cpy, set;
  104. BUG_ON(nfdt->max_fds < ofdt->max_fds);
  105. cpy = ofdt->max_fds * sizeof(struct file *);
  106. set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
  107. memcpy(nfdt->fd, ofdt->fd, cpy);
  108. memset((char *)(nfdt->fd) + cpy, 0, set);
  109. cpy = ofdt->max_fds / BITS_PER_BYTE;
  110. set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
  111. memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
  112. memset((char *)(nfdt->open_fds) + cpy, 0, set);
  113. memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
  114. memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
  115. }
  116. static struct fdtable * alloc_fdtable(unsigned int nr)
  117. {
  118. struct fdtable *fdt;
  119. void *data;
  120. /*
  121. * Figure out how many fds we actually want to support in this fdtable.
  122. * Allocation steps are keyed to the size of the fdarray, since it
  123. * grows far faster than any of the other dynamic data. We try to fit
  124. * the fdarray into comfortable page-tuned chunks: starting at 1024B
  125. * and growing in powers of two from there on.
  126. */
  127. nr /= (1024 / sizeof(struct file *));
  128. nr = roundup_pow_of_two(nr + 1);
  129. nr *= (1024 / sizeof(struct file *));
  130. /*
  131. * Note that this can drive nr *below* what we had passed if sysctl_nr_open
  132. * had been set lower between the check in expand_files() and here. Deal
  133. * with that in caller, it's cheaper that way.
  134. *
  135. * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
  136. * bitmaps handling below becomes unpleasant, to put it mildly...
  137. */
  138. if (unlikely(nr > sysctl_nr_open))
  139. nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
  140. fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
  141. if (!fdt)
  142. goto out;
  143. fdt->max_fds = nr;
  144. data = alloc_fdmem(nr * sizeof(struct file *));
  145. if (!data)
  146. goto out_fdt;
  147. fdt->fd = data;
  148. data = alloc_fdmem(max_t(size_t,
  149. 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
  150. if (!data)
  151. goto out_arr;
  152. fdt->open_fds = data;
  153. data += nr / BITS_PER_BYTE;
  154. fdt->close_on_exec = data;
  155. fdt->next = NULL;
  156. return fdt;
  157. out_arr:
  158. free_fdmem(fdt->fd);
  159. out_fdt:
  160. kfree(fdt);
  161. out:
  162. return NULL;
  163. }
  164. /*
  165. * Expand the file descriptor table.
  166. * This function will allocate a new fdtable and both fd array and fdset, of
  167. * the given size.
  168. * Return <0 error code on error; 1 on successful completion.
  169. * The files->file_lock should be held on entry, and will be held on exit.
  170. */
  171. static int expand_fdtable(struct files_struct *files, int nr)
  172. __releases(files->file_lock)
  173. __acquires(files->file_lock)
  174. {
  175. struct fdtable *new_fdt, *cur_fdt;
  176. spin_unlock(&files->file_lock);
  177. new_fdt = alloc_fdtable(nr);
  178. spin_lock(&files->file_lock);
  179. if (!new_fdt)
  180. return -ENOMEM;
  181. /*
  182. * extremely unlikely race - sysctl_nr_open decreased between the check in
  183. * caller and alloc_fdtable(). Cheaper to catch it here...
  184. */
  185. if (unlikely(new_fdt->max_fds <= nr)) {
  186. __free_fdtable(new_fdt);
  187. return -EMFILE;
  188. }
  189. /*
  190. * Check again since another task may have expanded the fd table while
  191. * we dropped the lock
  192. */
  193. cur_fdt = files_fdtable(files);
  194. if (nr >= cur_fdt->max_fds) {
  195. /* Continue as planned */
  196. copy_fdtable(new_fdt, cur_fdt);
  197. rcu_assign_pointer(files->fdt, new_fdt);
  198. if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
  199. call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
  200. } else {
  201. /* Somebody else expanded, so undo our attempt */
  202. __free_fdtable(new_fdt);
  203. }
  204. return 1;
  205. }
  206. /*
  207. * Expand files.
  208. * This function will expand the file structures, if the requested size exceeds
  209. * the current capacity and there is room for expansion.
  210. * Return <0 error code on error; 0 when nothing done; 1 when files were
  211. * expanded and execution may have blocked.
  212. * The files->file_lock should be held on entry, and will be held on exit.
  213. */
  214. int expand_files(struct files_struct *files, int nr)
  215. {
  216. struct fdtable *fdt;
  217. fdt = files_fdtable(files);
  218. /* Do we need to expand? */
  219. if (nr < fdt->max_fds)
  220. return 0;
  221. /* Can we expand? */
  222. if (nr >= sysctl_nr_open)
  223. return -EMFILE;
  224. /* All good, so we try */
  225. return expand_fdtable(files, nr);
  226. }
  227. static int count_open_files(struct fdtable *fdt)
  228. {
  229. int size = fdt->max_fds;
  230. int i;
  231. /* Find the last open fd */
  232. for (i = size / BITS_PER_LONG; i > 0; ) {
  233. if (fdt->open_fds[--i])
  234. break;
  235. }
  236. i = (i + 1) * BITS_PER_LONG;
  237. return i;
  238. }
  239. /*
  240. * Allocate a new files structure and copy contents from the
  241. * passed in files structure.
  242. * errorp will be valid only when the returned files_struct is NULL.
  243. */
  244. struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
  245. {
  246. struct files_struct *newf;
  247. struct file **old_fds, **new_fds;
  248. int open_files, size, i;
  249. struct fdtable *old_fdt, *new_fdt;
  250. *errorp = -ENOMEM;
  251. newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
  252. if (!newf)
  253. goto out;
  254. atomic_set(&newf->count, 1);
  255. spin_lock_init(&newf->file_lock);
  256. newf->next_fd = 0;
  257. new_fdt = &newf->fdtab;
  258. new_fdt->max_fds = NR_OPEN_DEFAULT;
  259. new_fdt->close_on_exec = newf->close_on_exec_init;
  260. new_fdt->open_fds = newf->open_fds_init;
  261. new_fdt->fd = &newf->fd_array[0];
  262. new_fdt->next = NULL;
  263. spin_lock(&oldf->file_lock);
  264. old_fdt = files_fdtable(oldf);
  265. open_files = count_open_files(old_fdt);
  266. /*
  267. * Check whether we need to allocate a larger fd array and fd set.
  268. */
  269. while (unlikely(open_files > new_fdt->max_fds)) {
  270. spin_unlock(&oldf->file_lock);
  271. if (new_fdt != &newf->fdtab)
  272. __free_fdtable(new_fdt);
  273. new_fdt = alloc_fdtable(open_files - 1);
  274. if (!new_fdt) {
  275. *errorp = -ENOMEM;
  276. goto out_release;
  277. }
  278. /* beyond sysctl_nr_open; nothing to do */
  279. if (unlikely(new_fdt->max_fds < open_files)) {
  280. __free_fdtable(new_fdt);
  281. *errorp = -EMFILE;
  282. goto out_release;
  283. }
  284. /*
  285. * Reacquire the oldf lock and a pointer to its fd table
  286. * who knows it may have a new bigger fd table. We need
  287. * the latest pointer.
  288. */
  289. spin_lock(&oldf->file_lock);
  290. old_fdt = files_fdtable(oldf);
  291. open_files = count_open_files(old_fdt);
  292. }
  293. old_fds = old_fdt->fd;
  294. new_fds = new_fdt->fd;
  295. memcpy(new_fdt->open_fds, old_fdt->open_fds, open_files / 8);
  296. memcpy(new_fdt->close_on_exec, old_fdt->close_on_exec, open_files / 8);
  297. for (i = open_files; i != 0; i--) {
  298. struct file *f = *old_fds++;
  299. if (f) {
  300. get_file(f);
  301. } else {
  302. /*
  303. * The fd may be claimed in the fd bitmap but not yet
  304. * instantiated in the files array if a sibling thread
  305. * is partway through open(). So make sure that this
  306. * fd is available to the new process.
  307. */
  308. __clear_open_fd(open_files - i, new_fdt);
  309. }
  310. rcu_assign_pointer(*new_fds++, f);
  311. }
  312. spin_unlock(&oldf->file_lock);
  313. /* compute the remainder to be cleared */
  314. size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
  315. /* This is long word aligned thus could use a optimized version */
  316. memset(new_fds, 0, size);
  317. if (new_fdt->max_fds > open_files) {
  318. int left = (new_fdt->max_fds - open_files) / 8;
  319. int start = open_files / BITS_PER_LONG;
  320. memset(&new_fdt->open_fds[start], 0, left);
  321. memset(&new_fdt->close_on_exec[start], 0, left);
  322. }
  323. rcu_assign_pointer(newf->fdt, new_fdt);
  324. return newf;
  325. out_release:
  326. kmem_cache_free(files_cachep, newf);
  327. out:
  328. return NULL;
  329. }
  330. static void close_files(struct files_struct * files)
  331. {
  332. int i, j;
  333. struct fdtable *fdt;
  334. j = 0;
  335. /*
  336. * It is safe to dereference the fd table without RCU or
  337. * ->file_lock because this is the last reference to the
  338. * files structure. But use RCU to shut RCU-lockdep up.
  339. */
  340. rcu_read_lock();
  341. fdt = files_fdtable(files);
  342. rcu_read_unlock();
  343. for (;;) {
  344. unsigned long set;
  345. i = j * BITS_PER_LONG;
  346. if (i >= fdt->max_fds)
  347. break;
  348. set = fdt->open_fds[j++];
  349. while (set) {
  350. if (set & 1) {
  351. struct file * file = xchg(&fdt->fd[i], NULL);
  352. if (file) {
  353. filp_close(file, files);
  354. cond_resched();
  355. }
  356. }
  357. i++;
  358. set >>= 1;
  359. }
  360. }
  361. }
  362. struct files_struct *get_files_struct(struct task_struct *task)
  363. {
  364. struct files_struct *files;
  365. task_lock(task);
  366. files = task->files;
  367. if (files)
  368. atomic_inc(&files->count);
  369. task_unlock(task);
  370. return files;
  371. }
  372. void put_files_struct(struct files_struct *files)
  373. {
  374. struct fdtable *fdt;
  375. if (atomic_dec_and_test(&files->count)) {
  376. close_files(files);
  377. /* not really needed, since nobody can see us */
  378. rcu_read_lock();
  379. fdt = files_fdtable(files);
  380. rcu_read_unlock();
  381. /* free the arrays if they are not embedded */
  382. if (fdt != &files->fdtab)
  383. __free_fdtable(fdt);
  384. kmem_cache_free(files_cachep, files);
  385. }
  386. }
  387. void reset_files_struct(struct files_struct *files)
  388. {
  389. struct task_struct *tsk = current;
  390. struct files_struct *old;
  391. old = tsk->files;
  392. task_lock(tsk);
  393. tsk->files = files;
  394. task_unlock(tsk);
  395. put_files_struct(old);
  396. }
  397. void exit_files(struct task_struct *tsk)
  398. {
  399. struct files_struct * files = tsk->files;
  400. if (files) {
  401. task_lock(tsk);
  402. tsk->files = NULL;
  403. task_unlock(tsk);
  404. put_files_struct(files);
  405. }
  406. }
  407. static void __devinit fdtable_defer_list_init(int cpu)
  408. {
  409. struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
  410. spin_lock_init(&fddef->lock);
  411. INIT_WORK(&fddef->wq, free_fdtable_work);
  412. fddef->next = NULL;
  413. }
  414. void __init files_defer_init(void)
  415. {
  416. int i;
  417. for_each_possible_cpu(i)
  418. fdtable_defer_list_init(i);
  419. sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
  420. -BITS_PER_LONG;
  421. }
  422. struct files_struct init_files = {
  423. .count = ATOMIC_INIT(1),
  424. .fdt = &init_files.fdtab,
  425. .fdtab = {
  426. .max_fds = NR_OPEN_DEFAULT,
  427. .fd = &init_files.fd_array[0],
  428. .close_on_exec = init_files.close_on_exec_init,
  429. .open_fds = init_files.open_fds_init,
  430. },
  431. .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
  432. };
  433. /*
  434. * allocate a file descriptor, mark it busy.
  435. */
  436. int __alloc_fd(struct files_struct *files,
  437. unsigned start, unsigned end, unsigned flags)
  438. {
  439. unsigned int fd;
  440. int error;
  441. struct fdtable *fdt;
  442. spin_lock(&files->file_lock);
  443. repeat:
  444. fdt = files_fdtable(files);
  445. fd = start;
  446. if (fd < files->next_fd)
  447. fd = files->next_fd;
  448. if (fd < fdt->max_fds)
  449. fd = find_next_zero_bit(fdt->open_fds, fdt->max_fds, fd);
  450. /*
  451. * N.B. For clone tasks sharing a files structure, this test
  452. * will limit the total number of files that can be opened.
  453. */
  454. error = -EMFILE;
  455. if (fd >= end)
  456. goto out;
  457. error = expand_files(files, fd);
  458. if (error < 0)
  459. goto out;
  460. /*
  461. * If we needed to expand the fs array we
  462. * might have blocked - try again.
  463. */
  464. if (error)
  465. goto repeat;
  466. if (start <= files->next_fd)
  467. files->next_fd = fd + 1;
  468. __set_open_fd(fd, fdt);
  469. if (flags & O_CLOEXEC)
  470. __set_close_on_exec(fd, fdt);
  471. else
  472. __clear_close_on_exec(fd, fdt);
  473. error = fd;
  474. #if 1
  475. /* Sanity check */
  476. if (rcu_dereference_raw(fdt->fd[fd]) != NULL) {
  477. printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
  478. rcu_assign_pointer(fdt->fd[fd], NULL);
  479. }
  480. #endif
  481. out:
  482. spin_unlock(&files->file_lock);
  483. return error;
  484. }
  485. int alloc_fd(unsigned start, unsigned flags)
  486. {
  487. return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags);
  488. }
  489. int get_unused_fd_flags(unsigned flags)
  490. {
  491. return __alloc_fd(current->files, 0, rlimit(RLIMIT_NOFILE), flags);
  492. }
  493. EXPORT_SYMBOL(get_unused_fd_flags);
  494. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  495. {
  496. struct fdtable *fdt = files_fdtable(files);
  497. __clear_open_fd(fd, fdt);
  498. if (fd < files->next_fd)
  499. files->next_fd = fd;
  500. }
  501. void put_unused_fd(unsigned int fd)
  502. {
  503. struct files_struct *files = current->files;
  504. spin_lock(&files->file_lock);
  505. __put_unused_fd(files, fd);
  506. spin_unlock(&files->file_lock);
  507. }
  508. EXPORT_SYMBOL(put_unused_fd);
  509. /*
  510. * Install a file pointer in the fd array.
  511. *
  512. * The VFS is full of places where we drop the files lock between
  513. * setting the open_fds bitmap and installing the file in the file
  514. * array. At any such point, we are vulnerable to a dup2() race
  515. * installing a file in the array before us. We need to detect this and
  516. * fput() the struct file we are about to overwrite in this case.
  517. *
  518. * It should never happen - if we allow dup2() do it, _really_ bad things
  519. * will follow.
  520. */
  521. void fd_install(unsigned int fd, struct file *file)
  522. {
  523. struct files_struct *files = current->files;
  524. struct fdtable *fdt;
  525. spin_lock(&files->file_lock);
  526. fdt = files_fdtable(files);
  527. BUG_ON(fdt->fd[fd] != NULL);
  528. rcu_assign_pointer(fdt->fd[fd], file);
  529. spin_unlock(&files->file_lock);
  530. }
  531. EXPORT_SYMBOL(fd_install);