file.c 6.3 KB

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