file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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/module.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(unsigned int 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. 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. /*
  106. * Expand the fdset in the files_struct. Called with the files spinlock
  107. * held for write.
  108. */
  109. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
  110. {
  111. unsigned int cpy, set;
  112. BUG_ON(nfdt->max_fds < ofdt->max_fds);
  113. cpy = ofdt->max_fds * sizeof(struct file *);
  114. set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
  115. memcpy(nfdt->fd, ofdt->fd, cpy);
  116. memset((char *)(nfdt->fd) + cpy, 0, set);
  117. cpy = ofdt->max_fds / BITS_PER_BYTE;
  118. set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
  119. memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
  120. memset((char *)(nfdt->open_fds) + cpy, 0, set);
  121. memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
  122. memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
  123. }
  124. static struct fdtable * alloc_fdtable(unsigned int nr)
  125. {
  126. struct fdtable *fdt;
  127. char *data;
  128. /*
  129. * Figure out how many fds we actually want to support in this fdtable.
  130. * Allocation steps are keyed to the size of the fdarray, since it
  131. * grows far faster than any of the other dynamic data. We try to fit
  132. * the fdarray into comfortable page-tuned chunks: starting at 1024B
  133. * and growing in powers of two from there on.
  134. */
  135. nr /= (1024 / sizeof(struct file *));
  136. nr = roundup_pow_of_two(nr + 1);
  137. nr *= (1024 / sizeof(struct file *));
  138. /*
  139. * Note that this can drive nr *below* what we had passed if sysctl_nr_open
  140. * had been set lower between the check in expand_files() and here. Deal
  141. * with that in caller, it's cheaper that way.
  142. *
  143. * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
  144. * bitmaps handling below becomes unpleasant, to put it mildly...
  145. */
  146. if (unlikely(nr > sysctl_nr_open))
  147. nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
  148. fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
  149. if (!fdt)
  150. goto out;
  151. fdt->max_fds = nr;
  152. data = alloc_fdmem(nr * sizeof(struct file *));
  153. if (!data)
  154. goto out_fdt;
  155. fdt->fd = (struct file **)data;
  156. data = alloc_fdmem(max_t(unsigned int,
  157. 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
  158. if (!data)
  159. goto out_arr;
  160. fdt->open_fds = (fd_set *)data;
  161. data += nr / BITS_PER_BYTE;
  162. fdt->close_on_exec = (fd_set *)data;
  163. fdt->next = NULL;
  164. return fdt;
  165. out_arr:
  166. free_fdmem(fdt->fd);
  167. out_fdt:
  168. kfree(fdt);
  169. out:
  170. return NULL;
  171. }
  172. /*
  173. * Expand the file descriptor table.
  174. * This function will allocate a new fdtable and both fd array and fdset, of
  175. * the given size.
  176. * Return <0 error code on error; 1 on successful completion.
  177. * The files->file_lock should be held on entry, and will be held on exit.
  178. */
  179. static int expand_fdtable(struct files_struct *files, int nr)
  180. __releases(files->file_lock)
  181. __acquires(files->file_lock)
  182. {
  183. struct fdtable *new_fdt, *cur_fdt;
  184. spin_unlock(&files->file_lock);
  185. new_fdt = alloc_fdtable(nr);
  186. spin_lock(&files->file_lock);
  187. if (!new_fdt)
  188. return -ENOMEM;
  189. /*
  190. * extremely unlikely race - sysctl_nr_open decreased between the check in
  191. * caller and alloc_fdtable(). Cheaper to catch it here...
  192. */
  193. if (unlikely(new_fdt->max_fds <= nr)) {
  194. __free_fdtable(new_fdt);
  195. return -EMFILE;
  196. }
  197. /*
  198. * Check again since another task may have expanded the fd table while
  199. * we dropped the lock
  200. */
  201. cur_fdt = files_fdtable(files);
  202. if (nr >= cur_fdt->max_fds) {
  203. /* Continue as planned */
  204. copy_fdtable(new_fdt, cur_fdt);
  205. rcu_assign_pointer(files->fdt, new_fdt);
  206. if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
  207. free_fdtable(cur_fdt);
  208. } else {
  209. /* Somebody else expanded, so undo our attempt */
  210. __free_fdtable(new_fdt);
  211. }
  212. return 1;
  213. }
  214. /*
  215. * Expand files.
  216. * This function will expand the file structures, if the requested size exceeds
  217. * the current capacity and there is room for expansion.
  218. * Return <0 error code on error; 0 when nothing done; 1 when files were
  219. * expanded and execution may have blocked.
  220. * The files->file_lock should be held on entry, and will be held on exit.
  221. */
  222. int expand_files(struct files_struct *files, int nr)
  223. {
  224. struct fdtable *fdt;
  225. fdt = files_fdtable(files);
  226. /*
  227. * N.B. For clone tasks sharing a files structure, this test
  228. * will limit the total number of files that can be opened.
  229. */
  230. if (nr >= rlimit(RLIMIT_NOFILE))
  231. return -EMFILE;
  232. /* Do we need to expand? */
  233. if (nr < fdt->max_fds)
  234. return 0;
  235. /* Can we expand? */
  236. if (nr >= sysctl_nr_open)
  237. return -EMFILE;
  238. /* All good, so we try */
  239. return expand_fdtable(files, nr);
  240. }
  241. static int count_open_files(struct fdtable *fdt)
  242. {
  243. int size = fdt->max_fds;
  244. int i;
  245. /* Find the last open fd */
  246. for (i = size/(8*sizeof(long)); i > 0; ) {
  247. if (fdt->open_fds->fds_bits[--i])
  248. break;
  249. }
  250. i = (i+1) * 8 * sizeof(long);
  251. return i;
  252. }
  253. /*
  254. * Allocate a new files structure and copy contents from the
  255. * passed in files structure.
  256. * errorp will be valid only when the returned files_struct is NULL.
  257. */
  258. struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
  259. {
  260. struct files_struct *newf;
  261. struct file **old_fds, **new_fds;
  262. int open_files, size, i;
  263. struct fdtable *old_fdt, *new_fdt;
  264. *errorp = -ENOMEM;
  265. newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
  266. if (!newf)
  267. goto out;
  268. atomic_set(&newf->count, 1);
  269. spin_lock_init(&newf->file_lock);
  270. newf->next_fd = 0;
  271. new_fdt = &newf->fdtab;
  272. new_fdt->max_fds = NR_OPEN_DEFAULT;
  273. new_fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
  274. new_fdt->open_fds = (fd_set *)&newf->open_fds_init;
  275. new_fdt->fd = &newf->fd_array[0];
  276. new_fdt->next = NULL;
  277. spin_lock(&oldf->file_lock);
  278. old_fdt = files_fdtable(oldf);
  279. open_files = count_open_files(old_fdt);
  280. /*
  281. * Check whether we need to allocate a larger fd array and fd set.
  282. */
  283. while (unlikely(open_files > new_fdt->max_fds)) {
  284. spin_unlock(&oldf->file_lock);
  285. if (new_fdt != &newf->fdtab)
  286. __free_fdtable(new_fdt);
  287. new_fdt = alloc_fdtable(open_files - 1);
  288. if (!new_fdt) {
  289. *errorp = -ENOMEM;
  290. goto out_release;
  291. }
  292. /* beyond sysctl_nr_open; nothing to do */
  293. if (unlikely(new_fdt->max_fds < open_files)) {
  294. __free_fdtable(new_fdt);
  295. *errorp = -EMFILE;
  296. goto out_release;
  297. }
  298. /*
  299. * Reacquire the oldf lock and a pointer to its fd table
  300. * who knows it may have a new bigger fd table. We need
  301. * the latest pointer.
  302. */
  303. spin_lock(&oldf->file_lock);
  304. old_fdt = files_fdtable(oldf);
  305. open_files = count_open_files(old_fdt);
  306. }
  307. old_fds = old_fdt->fd;
  308. new_fds = new_fdt->fd;
  309. memcpy(new_fdt->open_fds->fds_bits,
  310. old_fdt->open_fds->fds_bits, open_files/8);
  311. memcpy(new_fdt->close_on_exec->fds_bits,
  312. old_fdt->close_on_exec->fds_bits, open_files/8);
  313. for (i = open_files; i != 0; i--) {
  314. struct file *f = *old_fds++;
  315. if (f) {
  316. get_file(f);
  317. } else {
  318. /*
  319. * The fd may be claimed in the fd bitmap but not yet
  320. * instantiated in the files array if a sibling thread
  321. * is partway through open(). So make sure that this
  322. * fd is available to the new process.
  323. */
  324. FD_CLR(open_files - i, new_fdt->open_fds);
  325. }
  326. rcu_assign_pointer(*new_fds++, f);
  327. }
  328. spin_unlock(&oldf->file_lock);
  329. /* compute the remainder to be cleared */
  330. size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
  331. /* This is long word aligned thus could use a optimized version */
  332. memset(new_fds, 0, size);
  333. if (new_fdt->max_fds > open_files) {
  334. int left = (new_fdt->max_fds-open_files)/8;
  335. int start = open_files / (8 * sizeof(unsigned long));
  336. memset(&new_fdt->open_fds->fds_bits[start], 0, left);
  337. memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
  338. }
  339. rcu_assign_pointer(newf->fdt, new_fdt);
  340. return newf;
  341. out_release:
  342. kmem_cache_free(files_cachep, newf);
  343. out:
  344. return NULL;
  345. }
  346. static void __devinit fdtable_defer_list_init(int cpu)
  347. {
  348. struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
  349. spin_lock_init(&fddef->lock);
  350. INIT_WORK(&fddef->wq, free_fdtable_work);
  351. fddef->next = NULL;
  352. }
  353. void __init files_defer_init(void)
  354. {
  355. int i;
  356. for_each_possible_cpu(i)
  357. fdtable_defer_list_init(i);
  358. sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
  359. -BITS_PER_LONG;
  360. }
  361. struct files_struct init_files = {
  362. .count = ATOMIC_INIT(1),
  363. .fdt = &init_files.fdtab,
  364. .fdtab = {
  365. .max_fds = NR_OPEN_DEFAULT,
  366. .fd = &init_files.fd_array[0],
  367. .close_on_exec = (fd_set *)&init_files.close_on_exec_init,
  368. .open_fds = (fd_set *)&init_files.open_fds_init,
  369. },
  370. .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
  371. };
  372. /*
  373. * allocate a file descriptor, mark it busy.
  374. */
  375. int alloc_fd(unsigned start, unsigned flags)
  376. {
  377. struct files_struct *files = current->files;
  378. unsigned int fd;
  379. int error;
  380. struct fdtable *fdt;
  381. spin_lock(&files->file_lock);
  382. repeat:
  383. fdt = files_fdtable(files);
  384. fd = start;
  385. if (fd < files->next_fd)
  386. fd = files->next_fd;
  387. if (fd < fdt->max_fds)
  388. fd = find_next_zero_bit(fdt->open_fds->fds_bits,
  389. fdt->max_fds, fd);
  390. error = expand_files(files, fd);
  391. if (error < 0)
  392. goto out;
  393. /*
  394. * If we needed to expand the fs array we
  395. * might have blocked - try again.
  396. */
  397. if (error)
  398. goto repeat;
  399. if (start <= files->next_fd)
  400. files->next_fd = fd + 1;
  401. FD_SET(fd, fdt->open_fds);
  402. if (flags & O_CLOEXEC)
  403. FD_SET(fd, fdt->close_on_exec);
  404. else
  405. FD_CLR(fd, fdt->close_on_exec);
  406. error = fd;
  407. #if 1
  408. /* Sanity check */
  409. if (rcu_dereference_raw(fdt->fd[fd]) != NULL) {
  410. printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
  411. rcu_assign_pointer(fdt->fd[fd], NULL);
  412. }
  413. #endif
  414. out:
  415. spin_unlock(&files->file_lock);
  416. return error;
  417. }
  418. int get_unused_fd(void)
  419. {
  420. return alloc_fd(0, 0);
  421. }
  422. EXPORT_SYMBOL(get_unused_fd);