file.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/fs.h>
  9. #include <linux/mm.h>
  10. #include <linux/time.h>
  11. #include <linux/slab.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/file.h>
  14. #include <linux/bitops.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/workqueue.h>
  19. struct fdtable_defer {
  20. spinlock_t lock;
  21. struct work_struct wq;
  22. struct timer_list timer;
  23. struct fdtable *next;
  24. };
  25. /*
  26. * We use this list to defer free fdtables that have vmalloced
  27. * sets/arrays. By keeping a per-cpu list, we avoid having to embed
  28. * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
  29. * this per-task structure.
  30. */
  31. static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
  32. /*
  33. * Allocate an fd array, using kmalloc or vmalloc.
  34. * Note: the array isn't cleared at allocation time.
  35. */
  36. struct file ** alloc_fd_array(int num)
  37. {
  38. struct file **new_fds;
  39. int size = num * sizeof(struct file *);
  40. if (size <= PAGE_SIZE)
  41. new_fds = (struct file **) kmalloc(size, GFP_KERNEL);
  42. else
  43. new_fds = (struct file **) vmalloc(size);
  44. return new_fds;
  45. }
  46. void free_fd_array(struct file **array, int num)
  47. {
  48. int size = num * sizeof(struct file *);
  49. if (!array) {
  50. printk (KERN_ERR "free_fd_array: array = 0 (num = %d)\n", num);
  51. return;
  52. }
  53. if (num <= NR_OPEN_DEFAULT) /* Don't free the embedded fd array! */
  54. return;
  55. else if (size <= PAGE_SIZE)
  56. kfree(array);
  57. else
  58. vfree(array);
  59. }
  60. static void __free_fdtable(struct fdtable *fdt)
  61. {
  62. free_fdset(fdt->open_fds, fdt->max_fdset);
  63. free_fdset(fdt->close_on_exec, fdt->max_fdset);
  64. free_fd_array(fdt->fd, fdt->max_fds);
  65. kfree(fdt);
  66. }
  67. static void fdtable_timer(unsigned long data)
  68. {
  69. struct fdtable_defer *fddef = (struct fdtable_defer *)data;
  70. spin_lock(&fddef->lock);
  71. /*
  72. * If someone already emptied the queue return.
  73. */
  74. if (!fddef->next)
  75. goto out;
  76. if (!schedule_work(&fddef->wq))
  77. mod_timer(&fddef->timer, 5);
  78. out:
  79. spin_unlock(&fddef->lock);
  80. }
  81. static void free_fdtable_work(struct work_struct *work)
  82. {
  83. struct fdtable_defer *f =
  84. container_of(work, struct fdtable_defer, wq);
  85. struct fdtable *fdt;
  86. spin_lock_bh(&f->lock);
  87. fdt = f->next;
  88. f->next = NULL;
  89. spin_unlock_bh(&f->lock);
  90. while(fdt) {
  91. struct fdtable *next = fdt->next;
  92. __free_fdtable(fdt);
  93. fdt = next;
  94. }
  95. }
  96. static void free_fdtable_rcu(struct rcu_head *rcu)
  97. {
  98. struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
  99. int fdset_size, fdarray_size;
  100. struct fdtable_defer *fddef;
  101. BUG_ON(!fdt);
  102. fdset_size = fdt->max_fdset / 8;
  103. fdarray_size = fdt->max_fds * sizeof(struct file *);
  104. if (fdt->free_files) {
  105. /*
  106. * The this fdtable was embedded in the files structure
  107. * and the files structure itself was getting destroyed.
  108. * It is now safe to free the files structure.
  109. */
  110. kmem_cache_free(files_cachep, fdt->free_files);
  111. return;
  112. }
  113. if (fdt->max_fdset <= EMBEDDED_FD_SET_SIZE &&
  114. fdt->max_fds <= NR_OPEN_DEFAULT) {
  115. /*
  116. * The fdtable was embedded
  117. */
  118. return;
  119. }
  120. if (fdset_size <= PAGE_SIZE && fdarray_size <= PAGE_SIZE) {
  121. kfree(fdt->open_fds);
  122. kfree(fdt->close_on_exec);
  123. kfree(fdt->fd);
  124. kfree(fdt);
  125. } else {
  126. fddef = &get_cpu_var(fdtable_defer_list);
  127. spin_lock(&fddef->lock);
  128. fdt->next = fddef->next;
  129. fddef->next = fdt;
  130. /*
  131. * vmallocs are handled from the workqueue context.
  132. * If the per-cpu workqueue is running, then we
  133. * defer work scheduling through a timer.
  134. */
  135. if (!schedule_work(&fddef->wq))
  136. mod_timer(&fddef->timer, 5);
  137. spin_unlock(&fddef->lock);
  138. put_cpu_var(fdtable_defer_list);
  139. }
  140. }
  141. void free_fdtable(struct fdtable *fdt)
  142. {
  143. if (fdt->free_files ||
  144. fdt->max_fdset > EMBEDDED_FD_SET_SIZE ||
  145. fdt->max_fds > NR_OPEN_DEFAULT)
  146. call_rcu(&fdt->rcu, free_fdtable_rcu);
  147. }
  148. /*
  149. * Expand the fdset in the files_struct. Called with the files spinlock
  150. * held for write.
  151. */
  152. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt)
  153. {
  154. int i;
  155. int count;
  156. BUG_ON(nfdt->max_fdset < fdt->max_fdset);
  157. BUG_ON(nfdt->max_fds < fdt->max_fds);
  158. /* Copy the existing tables and install the new pointers */
  159. i = fdt->max_fdset / (sizeof(unsigned long) * 8);
  160. count = (nfdt->max_fdset - fdt->max_fdset) / 8;
  161. /*
  162. * Don't copy the entire array if the current fdset is
  163. * not yet initialised.
  164. */
  165. if (i) {
  166. memcpy (nfdt->open_fds, fdt->open_fds,
  167. fdt->max_fdset/8);
  168. memcpy (nfdt->close_on_exec, fdt->close_on_exec,
  169. fdt->max_fdset/8);
  170. memset (&nfdt->open_fds->fds_bits[i], 0, count);
  171. memset (&nfdt->close_on_exec->fds_bits[i], 0, count);
  172. }
  173. /* Don't copy/clear the array if we are creating a new
  174. fd array for fork() */
  175. if (fdt->max_fds) {
  176. memcpy(nfdt->fd, fdt->fd,
  177. fdt->max_fds * sizeof(struct file *));
  178. /* clear the remainder of the array */
  179. memset(&nfdt->fd[fdt->max_fds], 0,
  180. (nfdt->max_fds - fdt->max_fds) *
  181. sizeof(struct file *));
  182. }
  183. }
  184. /*
  185. * Allocate an fdset array, using kmalloc or vmalloc.
  186. * Note: the array isn't cleared at allocation time.
  187. */
  188. fd_set * alloc_fdset(int num)
  189. {
  190. fd_set *new_fdset;
  191. int size = num / 8;
  192. if (size <= PAGE_SIZE)
  193. new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
  194. else
  195. new_fdset = (fd_set *) vmalloc(size);
  196. return new_fdset;
  197. }
  198. void free_fdset(fd_set *array, int num)
  199. {
  200. if (num <= EMBEDDED_FD_SET_SIZE) /* Don't free an embedded fdset */
  201. return;
  202. else if (num <= 8 * PAGE_SIZE)
  203. kfree(array);
  204. else
  205. vfree(array);
  206. }
  207. static struct fdtable *alloc_fdtable(int nr)
  208. {
  209. struct fdtable *fdt = NULL;
  210. int nfds = 0;
  211. fd_set *new_openset = NULL, *new_execset = NULL;
  212. struct file **new_fds;
  213. fdt = kzalloc(sizeof(*fdt), GFP_KERNEL);
  214. if (!fdt)
  215. goto out;
  216. nfds = max_t(int, 8 * L1_CACHE_BYTES, roundup_pow_of_two(nr + 1));
  217. if (nfds > NR_OPEN)
  218. nfds = NR_OPEN;
  219. new_openset = alloc_fdset(nfds);
  220. new_execset = alloc_fdset(nfds);
  221. if (!new_openset || !new_execset)
  222. goto out;
  223. fdt->open_fds = new_openset;
  224. fdt->close_on_exec = new_execset;
  225. fdt->max_fdset = nfds;
  226. nfds = NR_OPEN_DEFAULT;
  227. /*
  228. * Expand to the max in easy steps, and keep expanding it until
  229. * we have enough for the requested fd array size.
  230. */
  231. do {
  232. #if NR_OPEN_DEFAULT < 256
  233. if (nfds < 256)
  234. nfds = 256;
  235. else
  236. #endif
  237. if (nfds < (PAGE_SIZE / sizeof(struct file *)))
  238. nfds = PAGE_SIZE / sizeof(struct file *);
  239. else {
  240. nfds = nfds * 2;
  241. if (nfds > NR_OPEN)
  242. nfds = NR_OPEN;
  243. }
  244. } while (nfds <= nr);
  245. new_fds = alloc_fd_array(nfds);
  246. if (!new_fds)
  247. goto out2;
  248. fdt->fd = new_fds;
  249. fdt->max_fds = nfds;
  250. fdt->free_files = NULL;
  251. return fdt;
  252. out2:
  253. nfds = fdt->max_fdset;
  254. out:
  255. free_fdset(new_openset, nfds);
  256. free_fdset(new_execset, nfds);
  257. kfree(fdt);
  258. return NULL;
  259. }
  260. /*
  261. * Expand the file descriptor table.
  262. * This function will allocate a new fdtable and both fd array and fdset, of
  263. * the given size.
  264. * Return <0 error code on error; 1 on successful completion.
  265. * The files->file_lock should be held on entry, and will be held on exit.
  266. */
  267. static int expand_fdtable(struct files_struct *files, int nr)
  268. __releases(files->file_lock)
  269. __acquires(files->file_lock)
  270. {
  271. struct fdtable *new_fdt, *cur_fdt;
  272. spin_unlock(&files->file_lock);
  273. new_fdt = alloc_fdtable(nr);
  274. spin_lock(&files->file_lock);
  275. if (!new_fdt)
  276. return -ENOMEM;
  277. /*
  278. * Check again since another task may have expanded the fd table while
  279. * we dropped the lock
  280. */
  281. cur_fdt = files_fdtable(files);
  282. if (nr >= cur_fdt->max_fds || nr >= cur_fdt->max_fdset) {
  283. /* Continue as planned */
  284. copy_fdtable(new_fdt, cur_fdt);
  285. rcu_assign_pointer(files->fdt, new_fdt);
  286. free_fdtable(cur_fdt);
  287. } else {
  288. /* Somebody else expanded, so undo our attempt */
  289. __free_fdtable(new_fdt);
  290. }
  291. return 1;
  292. }
  293. /*
  294. * Expand files.
  295. * This function will expand the file structures, if the requested size exceeds
  296. * the current capacity and there is room for expansion.
  297. * Return <0 error code on error; 0 when nothing done; 1 when files were
  298. * expanded and execution may have blocked.
  299. * The files->file_lock should be held on entry, and will be held on exit.
  300. */
  301. int expand_files(struct files_struct *files, int nr)
  302. {
  303. struct fdtable *fdt;
  304. fdt = files_fdtable(files);
  305. /* Do we need to expand? */
  306. if (nr < fdt->max_fdset && nr < fdt->max_fds)
  307. return 0;
  308. /* Can we expand? */
  309. if (fdt->max_fdset >= NR_OPEN || fdt->max_fds >= NR_OPEN ||
  310. nr >= NR_OPEN)
  311. return -EMFILE;
  312. /* All good, so we try */
  313. return expand_fdtable(files, nr);
  314. }
  315. static void __devinit fdtable_defer_list_init(int cpu)
  316. {
  317. struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
  318. spin_lock_init(&fddef->lock);
  319. INIT_WORK(&fddef->wq, free_fdtable_work);
  320. init_timer(&fddef->timer);
  321. fddef->timer.data = (unsigned long)fddef;
  322. fddef->timer.function = fdtable_timer;
  323. fddef->next = NULL;
  324. }
  325. void __init files_defer_init(void)
  326. {
  327. int i;
  328. for_each_possible_cpu(i)
  329. fdtable_defer_list_init(i);
  330. }