file.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 fdtable_defer *f)
  82. {
  83. struct fdtable *fdt;
  84. spin_lock_bh(&f->lock);
  85. fdt = f->next;
  86. f->next = NULL;
  87. spin_unlock_bh(&f->lock);
  88. while(fdt) {
  89. struct fdtable *next = fdt->next;
  90. __free_fdtable(fdt);
  91. fdt = next;
  92. }
  93. }
  94. static void free_fdtable_rcu(struct rcu_head *rcu)
  95. {
  96. struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
  97. int fdset_size, fdarray_size;
  98. struct fdtable_defer *fddef;
  99. BUG_ON(!fdt);
  100. fdset_size = fdt->max_fdset / 8;
  101. fdarray_size = fdt->max_fds * sizeof(struct file *);
  102. if (fdt->free_files) {
  103. /*
  104. * The this fdtable was embedded in the files structure
  105. * and the files structure itself was getting destroyed.
  106. * It is now safe to free the files structure.
  107. */
  108. kmem_cache_free(files_cachep, fdt->free_files);
  109. return;
  110. }
  111. if (fdt->max_fdset <= EMBEDDED_FD_SET_SIZE &&
  112. fdt->max_fds <= NR_OPEN_DEFAULT) {
  113. /*
  114. * The fdtable was embedded
  115. */
  116. return;
  117. }
  118. if (fdset_size <= PAGE_SIZE && fdarray_size <= PAGE_SIZE) {
  119. kfree(fdt->open_fds);
  120. kfree(fdt->close_on_exec);
  121. kfree(fdt->fd);
  122. kfree(fdt);
  123. } else {
  124. fddef = &get_cpu_var(fdtable_defer_list);
  125. spin_lock(&fddef->lock);
  126. fdt->next = fddef->next;
  127. fddef->next = fdt;
  128. /*
  129. * vmallocs are handled from the workqueue context.
  130. * If the per-cpu workqueue is running, then we
  131. * defer work scheduling through a timer.
  132. */
  133. if (!schedule_work(&fddef->wq))
  134. mod_timer(&fddef->timer, 5);
  135. spin_unlock(&fddef->lock);
  136. put_cpu_var(fdtable_defer_list);
  137. }
  138. }
  139. void free_fdtable(struct fdtable *fdt)
  140. {
  141. if (fdt->free_files ||
  142. fdt->max_fdset > EMBEDDED_FD_SET_SIZE ||
  143. fdt->max_fds > NR_OPEN_DEFAULT)
  144. call_rcu(&fdt->rcu, free_fdtable_rcu);
  145. }
  146. /*
  147. * Expand the fdset in the files_struct. Called with the files spinlock
  148. * held for write.
  149. */
  150. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt)
  151. {
  152. int i;
  153. int count;
  154. BUG_ON(nfdt->max_fdset < fdt->max_fdset);
  155. BUG_ON(nfdt->max_fds < fdt->max_fds);
  156. /* Copy the existing tables and install the new pointers */
  157. i = fdt->max_fdset / (sizeof(unsigned long) * 8);
  158. count = (nfdt->max_fdset - fdt->max_fdset) / 8;
  159. /*
  160. * Don't copy the entire array if the current fdset is
  161. * not yet initialised.
  162. */
  163. if (i) {
  164. memcpy (nfdt->open_fds, fdt->open_fds,
  165. fdt->max_fdset/8);
  166. memcpy (nfdt->close_on_exec, fdt->close_on_exec,
  167. fdt->max_fdset/8);
  168. memset (&nfdt->open_fds->fds_bits[i], 0, count);
  169. memset (&nfdt->close_on_exec->fds_bits[i], 0, count);
  170. }
  171. /* Don't copy/clear the array if we are creating a new
  172. fd array for fork() */
  173. if (fdt->max_fds) {
  174. memcpy(nfdt->fd, fdt->fd,
  175. fdt->max_fds * sizeof(struct file *));
  176. /* clear the remainder of the array */
  177. memset(&nfdt->fd[fdt->max_fds], 0,
  178. (nfdt->max_fds - fdt->max_fds) *
  179. sizeof(struct file *));
  180. }
  181. }
  182. /*
  183. * Allocate an fdset array, using kmalloc or vmalloc.
  184. * Note: the array isn't cleared at allocation time.
  185. */
  186. fd_set * alloc_fdset(int num)
  187. {
  188. fd_set *new_fdset;
  189. int size = num / 8;
  190. if (size <= PAGE_SIZE)
  191. new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
  192. else
  193. new_fdset = (fd_set *) vmalloc(size);
  194. return new_fdset;
  195. }
  196. void free_fdset(fd_set *array, int num)
  197. {
  198. if (num <= EMBEDDED_FD_SET_SIZE) /* Don't free an embedded fdset */
  199. return;
  200. else if (num <= 8 * PAGE_SIZE)
  201. kfree(array);
  202. else
  203. vfree(array);
  204. }
  205. static struct fdtable *alloc_fdtable(int nr)
  206. {
  207. struct fdtable *fdt = NULL;
  208. int nfds = 0;
  209. fd_set *new_openset = NULL, *new_execset = NULL;
  210. struct file **new_fds;
  211. fdt = kzalloc(sizeof(*fdt), GFP_KERNEL);
  212. if (!fdt)
  213. goto out;
  214. nfds = max_t(int, 8 * L1_CACHE_BYTES, roundup_pow_of_two(nr + 1));
  215. if (nfds > NR_OPEN)
  216. nfds = NR_OPEN;
  217. new_openset = alloc_fdset(nfds);
  218. new_execset = alloc_fdset(nfds);
  219. if (!new_openset || !new_execset)
  220. goto out;
  221. fdt->open_fds = new_openset;
  222. fdt->close_on_exec = new_execset;
  223. fdt->max_fdset = nfds;
  224. nfds = NR_OPEN_DEFAULT;
  225. /*
  226. * Expand to the max in easy steps, and keep expanding it until
  227. * we have enough for the requested fd array size.
  228. */
  229. do {
  230. #if NR_OPEN_DEFAULT < 256
  231. if (nfds < 256)
  232. nfds = 256;
  233. else
  234. #endif
  235. if (nfds < (PAGE_SIZE / sizeof(struct file *)))
  236. nfds = PAGE_SIZE / sizeof(struct file *);
  237. else {
  238. nfds = nfds * 2;
  239. if (nfds > NR_OPEN)
  240. nfds = NR_OPEN;
  241. }
  242. } while (nfds <= nr);
  243. new_fds = alloc_fd_array(nfds);
  244. if (!new_fds)
  245. goto out2;
  246. fdt->fd = new_fds;
  247. fdt->max_fds = nfds;
  248. fdt->free_files = NULL;
  249. return fdt;
  250. out2:
  251. nfds = fdt->max_fdset;
  252. out:
  253. free_fdset(new_openset, nfds);
  254. free_fdset(new_execset, nfds);
  255. kfree(fdt);
  256. return NULL;
  257. }
  258. /*
  259. * Expands the file descriptor table - it will allocate a new fdtable and
  260. * both fd array and fdset. It is expected to be called with the
  261. * files_lock held.
  262. */
  263. static int expand_fdtable(struct files_struct *files, int nr)
  264. __releases(files->file_lock)
  265. __acquires(files->file_lock)
  266. {
  267. int error = 0;
  268. struct fdtable *fdt;
  269. struct fdtable *nfdt = NULL;
  270. spin_unlock(&files->file_lock);
  271. nfdt = alloc_fdtable(nr);
  272. if (!nfdt) {
  273. error = -ENOMEM;
  274. spin_lock(&files->file_lock);
  275. goto out;
  276. }
  277. spin_lock(&files->file_lock);
  278. fdt = files_fdtable(files);
  279. /*
  280. * Check again since another task may have expanded the
  281. * fd table while we dropped the lock
  282. */
  283. if (nr >= fdt->max_fds || nr >= fdt->max_fdset) {
  284. copy_fdtable(nfdt, fdt);
  285. } else {
  286. /* Somebody expanded while we dropped file_lock */
  287. spin_unlock(&files->file_lock);
  288. __free_fdtable(nfdt);
  289. spin_lock(&files->file_lock);
  290. goto out;
  291. }
  292. rcu_assign_pointer(files->fdt, nfdt);
  293. free_fdtable(fdt);
  294. out:
  295. return error;
  296. }
  297. /*
  298. * Expand files.
  299. * Return <0 on error; 0 nothing done; 1 files expanded, we may have blocked.
  300. * Should be called with the files->file_lock spinlock held for write.
  301. */
  302. int expand_files(struct files_struct *files, int nr)
  303. {
  304. int err, expand = 0;
  305. struct fdtable *fdt;
  306. fdt = files_fdtable(files);
  307. if (nr >= fdt->max_fdset || nr >= fdt->max_fds) {
  308. if (fdt->max_fdset >= NR_OPEN ||
  309. fdt->max_fds >= NR_OPEN || nr >= NR_OPEN) {
  310. err = -EMFILE;
  311. goto out;
  312. }
  313. expand = 1;
  314. if ((err = expand_fdtable(files, nr)))
  315. goto out;
  316. }
  317. err = expand;
  318. out:
  319. return err;
  320. }
  321. static void __devinit fdtable_defer_list_init(int cpu)
  322. {
  323. struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
  324. spin_lock_init(&fddef->lock);
  325. INIT_WORK(&fddef->wq, (void (*)(void *))free_fdtable_work, fddef);
  326. init_timer(&fddef->timer);
  327. fddef->timer.data = (unsigned long)fddef;
  328. fddef->timer.function = fdtable_timer;
  329. fddef->next = NULL;
  330. }
  331. void __init files_defer_init(void)
  332. {
  333. int i;
  334. for_each_possible_cpu(i)
  335. fdtable_defer_list_init(i);
  336. }