file.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. static inline void * alloc_fdmem(unsigned int size)
  32. {
  33. if (size <= PAGE_SIZE)
  34. return kmalloc(size, GFP_KERNEL);
  35. else
  36. return vmalloc(size);
  37. }
  38. static inline void free_fdarr(struct fdtable *fdt)
  39. {
  40. if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *)))
  41. kfree(fdt->fd);
  42. else
  43. vfree(fdt->fd);
  44. }
  45. static inline void free_fdset(struct fdtable *fdt)
  46. {
  47. if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2))
  48. kfree(fdt->open_fds);
  49. else
  50. vfree(fdt->open_fds);
  51. }
  52. static void free_fdtable_work(struct work_struct *work)
  53. {
  54. struct fdtable_defer *f =
  55. container_of(work, struct fdtable_defer, wq);
  56. struct fdtable *fdt;
  57. spin_lock_bh(&f->lock);
  58. fdt = f->next;
  59. f->next = NULL;
  60. spin_unlock_bh(&f->lock);
  61. while(fdt) {
  62. struct fdtable *next = fdt->next;
  63. vfree(fdt->fd);
  64. free_fdset(fdt);
  65. kfree(fdt);
  66. fdt = next;
  67. }
  68. }
  69. void free_fdtable_rcu(struct rcu_head *rcu)
  70. {
  71. struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
  72. struct fdtable_defer *fddef;
  73. BUG_ON(!fdt);
  74. if (fdt->max_fds <= NR_OPEN_DEFAULT) {
  75. /*
  76. * This fdtable is embedded in the files structure and that
  77. * structure itself is getting destroyed.
  78. */
  79. kmem_cache_free(files_cachep,
  80. container_of(fdt, struct files_struct, fdtab));
  81. return;
  82. }
  83. if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) {
  84. kfree(fdt->fd);
  85. kfree(fdt->open_fds);
  86. kfree(fdt);
  87. } else {
  88. fddef = &get_cpu_var(fdtable_defer_list);
  89. spin_lock(&fddef->lock);
  90. fdt->next = fddef->next;
  91. fddef->next = fdt;
  92. /* vmallocs are handled from the workqueue context */
  93. schedule_work(&fddef->wq);
  94. spin_unlock(&fddef->lock);
  95. put_cpu_var(fdtable_defer_list);
  96. }
  97. }
  98. /*
  99. * Expand the fdset in the files_struct. Called with the files spinlock
  100. * held for write.
  101. */
  102. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
  103. {
  104. unsigned int cpy, set;
  105. BUG_ON(nfdt->max_fds < ofdt->max_fds);
  106. if (ofdt->max_fds == 0)
  107. return;
  108. cpy = ofdt->max_fds * sizeof(struct file *);
  109. set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
  110. memcpy(nfdt->fd, ofdt->fd, cpy);
  111. memset((char *)(nfdt->fd) + cpy, 0, set);
  112. cpy = ofdt->max_fds / BITS_PER_BYTE;
  113. set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
  114. memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
  115. memset((char *)(nfdt->open_fds) + cpy, 0, set);
  116. memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
  117. memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
  118. }
  119. static struct fdtable * alloc_fdtable(unsigned int nr)
  120. {
  121. struct fdtable *fdt;
  122. char *data;
  123. /*
  124. * Figure out how many fds we actually want to support in this fdtable.
  125. * Allocation steps are keyed to the size of the fdarray, since it
  126. * grows far faster than any of the other dynamic data. We try to fit
  127. * the fdarray into comfortable page-tuned chunks: starting at 1024B
  128. * and growing in powers of two from there on.
  129. */
  130. nr /= (1024 / sizeof(struct file *));
  131. nr = roundup_pow_of_two(nr + 1);
  132. nr *= (1024 / sizeof(struct file *));
  133. if (nr > NR_OPEN)
  134. nr = NR_OPEN;
  135. fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
  136. if (!fdt)
  137. goto out;
  138. fdt->max_fds = nr;
  139. data = alloc_fdmem(nr * sizeof(struct file *));
  140. if (!data)
  141. goto out_fdt;
  142. fdt->fd = (struct file **)data;
  143. data = alloc_fdmem(max_t(unsigned int,
  144. 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
  145. if (!data)
  146. goto out_arr;
  147. fdt->open_fds = (fd_set *)data;
  148. data += nr / BITS_PER_BYTE;
  149. fdt->close_on_exec = (fd_set *)data;
  150. INIT_RCU_HEAD(&fdt->rcu);
  151. fdt->next = NULL;
  152. return fdt;
  153. out_arr:
  154. free_fdarr(fdt);
  155. out_fdt:
  156. kfree(fdt);
  157. out:
  158. return NULL;
  159. }
  160. /*
  161. * Expand the file descriptor table.
  162. * This function will allocate a new fdtable and both fd array and fdset, of
  163. * the given size.
  164. * Return <0 error code on error; 1 on successful completion.
  165. * The files->file_lock should be held on entry, and will be held on exit.
  166. */
  167. static int expand_fdtable(struct files_struct *files, int nr)
  168. __releases(files->file_lock)
  169. __acquires(files->file_lock)
  170. {
  171. struct fdtable *new_fdt, *cur_fdt;
  172. spin_unlock(&files->file_lock);
  173. new_fdt = alloc_fdtable(nr);
  174. spin_lock(&files->file_lock);
  175. if (!new_fdt)
  176. return -ENOMEM;
  177. /*
  178. * Check again since another task may have expanded the fd table while
  179. * we dropped the lock
  180. */
  181. cur_fdt = files_fdtable(files);
  182. if (nr >= cur_fdt->max_fds) {
  183. /* Continue as planned */
  184. copy_fdtable(new_fdt, cur_fdt);
  185. rcu_assign_pointer(files->fdt, new_fdt);
  186. if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
  187. free_fdtable(cur_fdt);
  188. } else {
  189. /* Somebody else expanded, so undo our attempt */
  190. free_fdarr(new_fdt);
  191. free_fdset(new_fdt);
  192. kfree(new_fdt);
  193. }
  194. return 1;
  195. }
  196. /*
  197. * Expand files.
  198. * This function will expand the file structures, if the requested size exceeds
  199. * the current capacity and there is room for expansion.
  200. * Return <0 error code on error; 0 when nothing done; 1 when files were
  201. * expanded and execution may have blocked.
  202. * The files->file_lock should be held on entry, and will be held on exit.
  203. */
  204. int expand_files(struct files_struct *files, int nr)
  205. {
  206. struct fdtable *fdt;
  207. fdt = files_fdtable(files);
  208. /* Do we need to expand? */
  209. if (nr < fdt->max_fds)
  210. return 0;
  211. /* Can we expand? */
  212. if (nr >= NR_OPEN)
  213. return -EMFILE;
  214. /* All good, so we try */
  215. return expand_fdtable(files, nr);
  216. }
  217. static void __devinit fdtable_defer_list_init(int cpu)
  218. {
  219. struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
  220. spin_lock_init(&fddef->lock);
  221. INIT_WORK(&fddef->wq, free_fdtable_work);
  222. fddef->next = NULL;
  223. }
  224. void __init files_defer_init(void)
  225. {
  226. int i;
  227. for_each_possible_cpu(i)
  228. fdtable_defer_list_init(i);
  229. }