file.c 8.1 KB

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