file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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/time.h>
  12. #include <linux/slab.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/file.h>
  15. #include <linux/fdtable.h>
  16. #include <linux/bitops.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/workqueue.h>
  21. struct fdtable_defer {
  22. spinlock_t lock;
  23. struct work_struct wq;
  24. struct fdtable *next;
  25. };
  26. int sysctl_nr_open __read_mostly = 1024*1024;
  27. int sysctl_nr_open_min = BITS_PER_LONG;
  28. int sysctl_nr_open_max = 1024 * 1024; /* raised later */
  29. /*
  30. * We use this list to defer free fdtables that have vmalloced
  31. * sets/arrays. By keeping a per-cpu list, we avoid having to embed
  32. * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
  33. * this per-task structure.
  34. */
  35. static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
  36. static inline void * alloc_fdmem(unsigned int size)
  37. {
  38. if (size <= PAGE_SIZE)
  39. return kmalloc(size, GFP_KERNEL);
  40. else
  41. return vmalloc(size);
  42. }
  43. static inline void free_fdarr(struct fdtable *fdt)
  44. {
  45. if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *)))
  46. kfree(fdt->fd);
  47. else
  48. vfree(fdt->fd);
  49. }
  50. static inline void free_fdset(struct fdtable *fdt)
  51. {
  52. if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2))
  53. kfree(fdt->open_fds);
  54. else
  55. vfree(fdt->open_fds);
  56. }
  57. static void free_fdtable_work(struct work_struct *work)
  58. {
  59. struct fdtable_defer *f =
  60. container_of(work, struct fdtable_defer, wq);
  61. struct fdtable *fdt;
  62. spin_lock_bh(&f->lock);
  63. fdt = f->next;
  64. f->next = NULL;
  65. spin_unlock_bh(&f->lock);
  66. while(fdt) {
  67. struct fdtable *next = fdt->next;
  68. vfree(fdt->fd);
  69. free_fdset(fdt);
  70. kfree(fdt);
  71. fdt = next;
  72. }
  73. }
  74. void free_fdtable_rcu(struct rcu_head *rcu)
  75. {
  76. struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
  77. struct fdtable_defer *fddef;
  78. BUG_ON(!fdt);
  79. if (fdt->max_fds <= NR_OPEN_DEFAULT) {
  80. /*
  81. * This fdtable is embedded in the files structure and that
  82. * structure itself is getting destroyed.
  83. */
  84. kmem_cache_free(files_cachep,
  85. container_of(fdt, struct files_struct, fdtab));
  86. return;
  87. }
  88. if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) {
  89. kfree(fdt->fd);
  90. kfree(fdt->open_fds);
  91. kfree(fdt);
  92. } else {
  93. fddef = &get_cpu_var(fdtable_defer_list);
  94. spin_lock(&fddef->lock);
  95. fdt->next = fddef->next;
  96. fddef->next = fdt;
  97. /* vmallocs are handled from the workqueue context */
  98. schedule_work(&fddef->wq);
  99. spin_unlock(&fddef->lock);
  100. put_cpu_var(fdtable_defer_list);
  101. }
  102. }
  103. /*
  104. * Expand the fdset in the files_struct. Called with the files spinlock
  105. * held for write.
  106. */
  107. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
  108. {
  109. unsigned int cpy, set;
  110. BUG_ON(nfdt->max_fds < ofdt->max_fds);
  111. cpy = ofdt->max_fds * sizeof(struct file *);
  112. set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
  113. memcpy(nfdt->fd, ofdt->fd, cpy);
  114. memset((char *)(nfdt->fd) + cpy, 0, set);
  115. cpy = ofdt->max_fds / BITS_PER_BYTE;
  116. set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
  117. memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
  118. memset((char *)(nfdt->open_fds) + cpy, 0, set);
  119. memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
  120. memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
  121. }
  122. static struct fdtable * alloc_fdtable(unsigned int nr)
  123. {
  124. struct fdtable *fdt;
  125. char *data;
  126. /*
  127. * Figure out how many fds we actually want to support in this fdtable.
  128. * Allocation steps are keyed to the size of the fdarray, since it
  129. * grows far faster than any of the other dynamic data. We try to fit
  130. * the fdarray into comfortable page-tuned chunks: starting at 1024B
  131. * and growing in powers of two from there on.
  132. */
  133. nr /= (1024 / sizeof(struct file *));
  134. nr = roundup_pow_of_two(nr + 1);
  135. nr *= (1024 / sizeof(struct file *));
  136. /*
  137. * Note that this can drive nr *below* what we had passed if sysctl_nr_open
  138. * had been set lower between the check in expand_files() and here. Deal
  139. * with that in caller, it's cheaper that way.
  140. *
  141. * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
  142. * bitmaps handling below becomes unpleasant, to put it mildly...
  143. */
  144. if (unlikely(nr > sysctl_nr_open))
  145. nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
  146. fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
  147. if (!fdt)
  148. goto out;
  149. fdt->max_fds = nr;
  150. data = alloc_fdmem(nr * sizeof(struct file *));
  151. if (!data)
  152. goto out_fdt;
  153. fdt->fd = (struct file **)data;
  154. data = alloc_fdmem(max_t(unsigned int,
  155. 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
  156. if (!data)
  157. goto out_arr;
  158. fdt->open_fds = (fd_set *)data;
  159. data += nr / BITS_PER_BYTE;
  160. fdt->close_on_exec = (fd_set *)data;
  161. INIT_RCU_HEAD(&fdt->rcu);
  162. fdt->next = NULL;
  163. return fdt;
  164. out_arr:
  165. free_fdarr(fdt);
  166. out_fdt:
  167. kfree(fdt);
  168. out:
  169. return NULL;
  170. }
  171. /*
  172. * Expand the file descriptor table.
  173. * This function will allocate a new fdtable and both fd array and fdset, of
  174. * the given size.
  175. * Return <0 error code on error; 1 on successful completion.
  176. * The files->file_lock should be held on entry, and will be held on exit.
  177. */
  178. static int expand_fdtable(struct files_struct *files, int nr)
  179. __releases(files->file_lock)
  180. __acquires(files->file_lock)
  181. {
  182. struct fdtable *new_fdt, *cur_fdt;
  183. spin_unlock(&files->file_lock);
  184. new_fdt = alloc_fdtable(nr);
  185. spin_lock(&files->file_lock);
  186. if (!new_fdt)
  187. return -ENOMEM;
  188. /*
  189. * extremely unlikely race - sysctl_nr_open decreased between the check in
  190. * caller and alloc_fdtable(). Cheaper to catch it here...
  191. */
  192. if (unlikely(new_fdt->max_fds <= nr)) {
  193. free_fdarr(new_fdt);
  194. free_fdset(new_fdt);
  195. kfree(new_fdt);
  196. return -EMFILE;
  197. }
  198. /*
  199. * Check again since another task may have expanded the fd table while
  200. * we dropped the lock
  201. */
  202. cur_fdt = files_fdtable(files);
  203. if (nr >= cur_fdt->max_fds) {
  204. /* Continue as planned */
  205. copy_fdtable(new_fdt, cur_fdt);
  206. rcu_assign_pointer(files->fdt, new_fdt);
  207. if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
  208. free_fdtable(cur_fdt);
  209. } else {
  210. /* Somebody else expanded, so undo our attempt */
  211. free_fdarr(new_fdt);
  212. free_fdset(new_fdt);
  213. kfree(new_fdt);
  214. }
  215. return 1;
  216. }
  217. /*
  218. * Expand files.
  219. * This function will expand the file structures, if the requested size exceeds
  220. * the current capacity and there is room for expansion.
  221. * Return <0 error code on error; 0 when nothing done; 1 when files were
  222. * expanded and execution may have blocked.
  223. * The files->file_lock should be held on entry, and will be held on exit.
  224. */
  225. int expand_files(struct files_struct *files, int nr)
  226. {
  227. struct fdtable *fdt;
  228. fdt = files_fdtable(files);
  229. /*
  230. * N.B. For clone tasks sharing a files structure, this test
  231. * will limit the total number of files that can be opened.
  232. */
  233. if (nr >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  234. return -EMFILE;
  235. /* Do we need to expand? */
  236. if (nr < fdt->max_fds)
  237. return 0;
  238. /* Can we expand? */
  239. if (nr >= sysctl_nr_open)
  240. return -EMFILE;
  241. /* All good, so we try */
  242. return expand_fdtable(files, nr);
  243. }
  244. static int count_open_files(struct fdtable *fdt)
  245. {
  246. int size = fdt->max_fds;
  247. int i;
  248. /* Find the last open fd */
  249. for (i = size/(8*sizeof(long)); i > 0; ) {
  250. if (fdt->open_fds->fds_bits[--i])
  251. break;
  252. }
  253. i = (i+1) * 8 * sizeof(long);
  254. return i;
  255. }
  256. /*
  257. * Allocate a new files structure and copy contents from the
  258. * passed in files structure.
  259. * errorp will be valid only when the returned files_struct is NULL.
  260. */
  261. struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
  262. {
  263. struct files_struct *newf;
  264. struct file **old_fds, **new_fds;
  265. int open_files, size, i;
  266. struct fdtable *old_fdt, *new_fdt;
  267. *errorp = -ENOMEM;
  268. newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
  269. if (!newf)
  270. goto out;
  271. atomic_set(&newf->count, 1);
  272. spin_lock_init(&newf->file_lock);
  273. newf->next_fd = 0;
  274. new_fdt = &newf->fdtab;
  275. new_fdt->max_fds = NR_OPEN_DEFAULT;
  276. new_fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
  277. new_fdt->open_fds = (fd_set *)&newf->open_fds_init;
  278. new_fdt->fd = &newf->fd_array[0];
  279. INIT_RCU_HEAD(&new_fdt->rcu);
  280. new_fdt->next = NULL;
  281. spin_lock(&oldf->file_lock);
  282. old_fdt = files_fdtable(oldf);
  283. open_files = count_open_files(old_fdt);
  284. /*
  285. * Check whether we need to allocate a larger fd array and fd set.
  286. */
  287. while (unlikely(open_files > new_fdt->max_fds)) {
  288. spin_unlock(&oldf->file_lock);
  289. if (new_fdt != &newf->fdtab) {
  290. free_fdarr(new_fdt);
  291. free_fdset(new_fdt);
  292. kfree(new_fdt);
  293. }
  294. new_fdt = alloc_fdtable(open_files - 1);
  295. if (!new_fdt) {
  296. *errorp = -ENOMEM;
  297. goto out_release;
  298. }
  299. /* beyond sysctl_nr_open; nothing to do */
  300. if (unlikely(new_fdt->max_fds < open_files)) {
  301. free_fdarr(new_fdt);
  302. free_fdset(new_fdt);
  303. kfree(new_fdt);
  304. *errorp = -EMFILE;
  305. goto out_release;
  306. }
  307. /*
  308. * Reacquire the oldf lock and a pointer to its fd table
  309. * who knows it may have a new bigger fd table. We need
  310. * the latest pointer.
  311. */
  312. spin_lock(&oldf->file_lock);
  313. old_fdt = files_fdtable(oldf);
  314. open_files = count_open_files(old_fdt);
  315. }
  316. old_fds = old_fdt->fd;
  317. new_fds = new_fdt->fd;
  318. memcpy(new_fdt->open_fds->fds_bits,
  319. old_fdt->open_fds->fds_bits, open_files/8);
  320. memcpy(new_fdt->close_on_exec->fds_bits,
  321. old_fdt->close_on_exec->fds_bits, open_files/8);
  322. for (i = open_files; i != 0; i--) {
  323. struct file *f = *old_fds++;
  324. if (f) {
  325. get_file(f);
  326. } else {
  327. /*
  328. * The fd may be claimed in the fd bitmap but not yet
  329. * instantiated in the files array if a sibling thread
  330. * is partway through open(). So make sure that this
  331. * fd is available to the new process.
  332. */
  333. FD_CLR(open_files - i, new_fdt->open_fds);
  334. }
  335. rcu_assign_pointer(*new_fds++, f);
  336. }
  337. spin_unlock(&oldf->file_lock);
  338. /* compute the remainder to be cleared */
  339. size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
  340. /* This is long word aligned thus could use a optimized version */
  341. memset(new_fds, 0, size);
  342. if (new_fdt->max_fds > open_files) {
  343. int left = (new_fdt->max_fds-open_files)/8;
  344. int start = open_files / (8 * sizeof(unsigned long));
  345. memset(&new_fdt->open_fds->fds_bits[start], 0, left);
  346. memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
  347. }
  348. rcu_assign_pointer(newf->fdt, new_fdt);
  349. return newf;
  350. out_release:
  351. kmem_cache_free(files_cachep, newf);
  352. out:
  353. return NULL;
  354. }
  355. static void __devinit fdtable_defer_list_init(int cpu)
  356. {
  357. struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
  358. spin_lock_init(&fddef->lock);
  359. INIT_WORK(&fddef->wq, free_fdtable_work);
  360. fddef->next = NULL;
  361. }
  362. void __init files_defer_init(void)
  363. {
  364. int i;
  365. for_each_possible_cpu(i)
  366. fdtable_defer_list_init(i);
  367. sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
  368. -BITS_PER_LONG;
  369. }
  370. struct files_struct init_files = {
  371. .count = ATOMIC_INIT(1),
  372. .fdt = &init_files.fdtab,
  373. .fdtab = {
  374. .max_fds = NR_OPEN_DEFAULT,
  375. .fd = &init_files.fd_array[0],
  376. .close_on_exec = (fd_set *)&init_files.close_on_exec_init,
  377. .open_fds = (fd_set *)&init_files.open_fds_init,
  378. .rcu = RCU_HEAD_INIT,
  379. },
  380. .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
  381. };
  382. /*
  383. * allocate a file descriptor, mark it busy.
  384. */
  385. int alloc_fd(unsigned start, unsigned flags)
  386. {
  387. struct files_struct *files = current->files;
  388. unsigned int fd;
  389. int error;
  390. struct fdtable *fdt;
  391. spin_lock(&files->file_lock);
  392. repeat:
  393. fdt = files_fdtable(files);
  394. fd = start;
  395. if (fd < files->next_fd)
  396. fd = files->next_fd;
  397. if (fd < fdt->max_fds)
  398. fd = find_next_zero_bit(fdt->open_fds->fds_bits,
  399. fdt->max_fds, fd);
  400. error = expand_files(files, fd);
  401. if (error < 0)
  402. goto out;
  403. /*
  404. * If we needed to expand the fs array we
  405. * might have blocked - try again.
  406. */
  407. if (error)
  408. goto repeat;
  409. if (start <= files->next_fd)
  410. files->next_fd = fd + 1;
  411. FD_SET(fd, fdt->open_fds);
  412. if (flags & O_CLOEXEC)
  413. FD_SET(fd, fdt->close_on_exec);
  414. else
  415. FD_CLR(fd, fdt->close_on_exec);
  416. error = fd;
  417. #if 1
  418. /* Sanity check */
  419. if (rcu_dereference(fdt->fd[fd]) != NULL) {
  420. printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
  421. rcu_assign_pointer(fdt->fd[fd], NULL);
  422. }
  423. #endif
  424. out:
  425. spin_unlock(&files->file_lock);
  426. return error;
  427. }
  428. int get_unused_fd(void)
  429. {
  430. return alloc_fd(0, 0);
  431. }
  432. EXPORT_SYMBOL(get_unused_fd);