file.c 14 KB

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