file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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/fdtable.h>
  15. #include <linux/bitops.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/rcupdate.h>
  19. #include <linux/workqueue.h>
  20. struct fdtable_defer {
  21. spinlock_t lock;
  22. struct work_struct wq;
  23. struct fdtable *next;
  24. };
  25. int sysctl_nr_open __read_mostly = 1024*1024;
  26. int sysctl_nr_open_min = BITS_PER_LONG;
  27. int sysctl_nr_open_max = 1024 * 1024; /* raised later */
  28. /*
  29. * We use this list to defer free fdtables that have vmalloced
  30. * sets/arrays. By keeping a per-cpu list, we avoid having to embed
  31. * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
  32. * this per-task structure.
  33. */
  34. static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
  35. static inline void * alloc_fdmem(unsigned int size)
  36. {
  37. if (size <= PAGE_SIZE)
  38. return kmalloc(size, GFP_KERNEL);
  39. else
  40. return vmalloc(size);
  41. }
  42. static inline void free_fdarr(struct fdtable *fdt)
  43. {
  44. if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *)))
  45. kfree(fdt->fd);
  46. else
  47. vfree(fdt->fd);
  48. }
  49. static inline void free_fdset(struct fdtable *fdt)
  50. {
  51. if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2))
  52. kfree(fdt->open_fds);
  53. else
  54. vfree(fdt->open_fds);
  55. }
  56. static void free_fdtable_work(struct work_struct *work)
  57. {
  58. struct fdtable_defer *f =
  59. container_of(work, struct fdtable_defer, wq);
  60. struct fdtable *fdt;
  61. spin_lock_bh(&f->lock);
  62. fdt = f->next;
  63. f->next = NULL;
  64. spin_unlock_bh(&f->lock);
  65. while(fdt) {
  66. struct fdtable *next = fdt->next;
  67. vfree(fdt->fd);
  68. free_fdset(fdt);
  69. kfree(fdt);
  70. fdt = next;
  71. }
  72. }
  73. void free_fdtable_rcu(struct rcu_head *rcu)
  74. {
  75. struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
  76. struct fdtable_defer *fddef;
  77. BUG_ON(!fdt);
  78. if (fdt->max_fds <= NR_OPEN_DEFAULT) {
  79. /*
  80. * This fdtable is embedded in the files structure and that
  81. * structure itself is getting destroyed.
  82. */
  83. kmem_cache_free(files_cachep,
  84. container_of(fdt, struct files_struct, fdtab));
  85. return;
  86. }
  87. if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) {
  88. kfree(fdt->fd);
  89. kfree(fdt->open_fds);
  90. kfree(fdt);
  91. } else {
  92. fddef = &get_cpu_var(fdtable_defer_list);
  93. spin_lock(&fddef->lock);
  94. fdt->next = fddef->next;
  95. fddef->next = fdt;
  96. /* vmallocs are handled from the workqueue context */
  97. schedule_work(&fddef->wq);
  98. spin_unlock(&fddef->lock);
  99. put_cpu_var(fdtable_defer_list);
  100. }
  101. }
  102. /*
  103. * Expand the fdset in the files_struct. Called with the files spinlock
  104. * held for write.
  105. */
  106. static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
  107. {
  108. unsigned int cpy, set;
  109. BUG_ON(nfdt->max_fds < ofdt->max_fds);
  110. cpy = ofdt->max_fds * sizeof(struct file *);
  111. set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
  112. memcpy(nfdt->fd, ofdt->fd, cpy);
  113. memset((char *)(nfdt->fd) + cpy, 0, set);
  114. cpy = ofdt->max_fds / BITS_PER_BYTE;
  115. set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
  116. memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
  117. memset((char *)(nfdt->open_fds) + cpy, 0, set);
  118. memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
  119. memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
  120. }
  121. static struct fdtable * alloc_fdtable(unsigned int nr)
  122. {
  123. struct fdtable *fdt;
  124. char *data;
  125. /*
  126. * Figure out how many fds we actually want to support in this fdtable.
  127. * Allocation steps are keyed to the size of the fdarray, since it
  128. * grows far faster than any of the other dynamic data. We try to fit
  129. * the fdarray into comfortable page-tuned chunks: starting at 1024B
  130. * and growing in powers of two from there on.
  131. */
  132. nr /= (1024 / sizeof(struct file *));
  133. nr = roundup_pow_of_two(nr + 1);
  134. nr *= (1024 / sizeof(struct file *));
  135. /*
  136. * Note that this can drive nr *below* what we had passed if sysctl_nr_open
  137. * had been set lower between the check in expand_files() and here. Deal
  138. * with that in caller, it's cheaper that way.
  139. *
  140. * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
  141. * bitmaps handling below becomes unpleasant, to put it mildly...
  142. */
  143. if (unlikely(nr > sysctl_nr_open))
  144. nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
  145. fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
  146. if (!fdt)
  147. goto out;
  148. fdt->max_fds = nr;
  149. data = alloc_fdmem(nr * sizeof(struct file *));
  150. if (!data)
  151. goto out_fdt;
  152. fdt->fd = (struct file **)data;
  153. data = alloc_fdmem(max_t(unsigned int,
  154. 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
  155. if (!data)
  156. goto out_arr;
  157. fdt->open_fds = (fd_set *)data;
  158. data += nr / BITS_PER_BYTE;
  159. fdt->close_on_exec = (fd_set *)data;
  160. INIT_RCU_HEAD(&fdt->rcu);
  161. fdt->next = NULL;
  162. return fdt;
  163. out_arr:
  164. free_fdarr(fdt);
  165. out_fdt:
  166. kfree(fdt);
  167. out:
  168. return NULL;
  169. }
  170. /*
  171. * Expand the file descriptor table.
  172. * This function will allocate a new fdtable and both fd array and fdset, of
  173. * the given size.
  174. * Return <0 error code on error; 1 on successful completion.
  175. * The files->file_lock should be held on entry, and will be held on exit.
  176. */
  177. static int expand_fdtable(struct files_struct *files, int nr)
  178. __releases(files->file_lock)
  179. __acquires(files->file_lock)
  180. {
  181. struct fdtable *new_fdt, *cur_fdt;
  182. spin_unlock(&files->file_lock);
  183. new_fdt = alloc_fdtable(nr);
  184. spin_lock(&files->file_lock);
  185. if (!new_fdt)
  186. return -ENOMEM;
  187. /*
  188. * extremely unlikely race - sysctl_nr_open decreased between the check in
  189. * caller and alloc_fdtable(). Cheaper to catch it here...
  190. */
  191. if (unlikely(new_fdt->max_fds <= nr)) {
  192. free_fdarr(new_fdt);
  193. free_fdset(new_fdt);
  194. kfree(new_fdt);
  195. return -EMFILE;
  196. }
  197. /*
  198. * Check again since another task may have expanded the fd table while
  199. * we dropped the lock
  200. */
  201. cur_fdt = files_fdtable(files);
  202. if (nr >= cur_fdt->max_fds) {
  203. /* Continue as planned */
  204. copy_fdtable(new_fdt, cur_fdt);
  205. rcu_assign_pointer(files->fdt, new_fdt);
  206. if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
  207. free_fdtable(cur_fdt);
  208. } else {
  209. /* Somebody else expanded, so undo our attempt */
  210. free_fdarr(new_fdt);
  211. free_fdset(new_fdt);
  212. kfree(new_fdt);
  213. }
  214. return 1;
  215. }
  216. /*
  217. * Expand files.
  218. * This function will expand the file structures, if the requested size exceeds
  219. * the current capacity and there is room for expansion.
  220. * Return <0 error code on error; 0 when nothing done; 1 when files were
  221. * expanded and execution may have blocked.
  222. * The files->file_lock should be held on entry, and will be held on exit.
  223. */
  224. int expand_files(struct files_struct *files, int nr)
  225. {
  226. struct fdtable *fdt;
  227. fdt = files_fdtable(files);
  228. /* Do we need to expand? */
  229. if (nr < fdt->max_fds)
  230. return 0;
  231. /* Can we expand? */
  232. if (nr >= sysctl_nr_open)
  233. return -EMFILE;
  234. /* All good, so we try */
  235. return expand_fdtable(files, nr);
  236. }
  237. static int count_open_files(struct fdtable *fdt)
  238. {
  239. int size = fdt->max_fds;
  240. int i;
  241. /* Find the last open fd */
  242. for (i = size/(8*sizeof(long)); i > 0; ) {
  243. if (fdt->open_fds->fds_bits[--i])
  244. break;
  245. }
  246. i = (i+1) * 8 * sizeof(long);
  247. return i;
  248. }
  249. /*
  250. * Allocate a new files structure and copy contents from the
  251. * passed in files structure.
  252. * errorp will be valid only when the returned files_struct is NULL.
  253. */
  254. struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
  255. {
  256. struct files_struct *newf;
  257. struct file **old_fds, **new_fds;
  258. int open_files, size, i;
  259. struct fdtable *old_fdt, *new_fdt;
  260. *errorp = -ENOMEM;
  261. newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
  262. if (!newf)
  263. goto out;
  264. atomic_set(&newf->count, 1);
  265. spin_lock_init(&newf->file_lock);
  266. newf->next_fd = 0;
  267. new_fdt = &newf->fdtab;
  268. new_fdt->max_fds = NR_OPEN_DEFAULT;
  269. new_fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
  270. new_fdt->open_fds = (fd_set *)&newf->open_fds_init;
  271. new_fdt->fd = &newf->fd_array[0];
  272. INIT_RCU_HEAD(&new_fdt->rcu);
  273. new_fdt->next = NULL;
  274. spin_lock(&oldf->file_lock);
  275. old_fdt = files_fdtable(oldf);
  276. open_files = count_open_files(old_fdt);
  277. /*
  278. * Check whether we need to allocate a larger fd array and fd set.
  279. */
  280. while (unlikely(open_files > new_fdt->max_fds)) {
  281. spin_unlock(&oldf->file_lock);
  282. if (new_fdt != &newf->fdtab) {
  283. free_fdarr(new_fdt);
  284. free_fdset(new_fdt);
  285. kfree(new_fdt);
  286. }
  287. new_fdt = alloc_fdtable(open_files - 1);
  288. if (!new_fdt) {
  289. *errorp = -ENOMEM;
  290. goto out_release;
  291. }
  292. /* beyond sysctl_nr_open; nothing to do */
  293. if (unlikely(new_fdt->max_fds < open_files)) {
  294. free_fdarr(new_fdt);
  295. free_fdset(new_fdt);
  296. kfree(new_fdt);
  297. *errorp = -EMFILE;
  298. goto out_release;
  299. }
  300. /*
  301. * Reacquire the oldf lock and a pointer to its fd table
  302. * who knows it may have a new bigger fd table. We need
  303. * the latest pointer.
  304. */
  305. spin_lock(&oldf->file_lock);
  306. old_fdt = files_fdtable(oldf);
  307. open_files = count_open_files(old_fdt);
  308. }
  309. old_fds = old_fdt->fd;
  310. new_fds = new_fdt->fd;
  311. memcpy(new_fdt->open_fds->fds_bits,
  312. old_fdt->open_fds->fds_bits, open_files/8);
  313. memcpy(new_fdt->close_on_exec->fds_bits,
  314. old_fdt->close_on_exec->fds_bits, open_files/8);
  315. for (i = open_files; i != 0; i--) {
  316. struct file *f = *old_fds++;
  317. if (f) {
  318. get_file(f);
  319. } else {
  320. /*
  321. * The fd may be claimed in the fd bitmap but not yet
  322. * instantiated in the files array if a sibling thread
  323. * is partway through open(). So make sure that this
  324. * fd is available to the new process.
  325. */
  326. FD_CLR(open_files - i, new_fdt->open_fds);
  327. }
  328. rcu_assign_pointer(*new_fds++, f);
  329. }
  330. spin_unlock(&oldf->file_lock);
  331. /* compute the remainder to be cleared */
  332. size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
  333. /* This is long word aligned thus could use a optimized version */
  334. memset(new_fds, 0, size);
  335. if (new_fdt->max_fds > open_files) {
  336. int left = (new_fdt->max_fds-open_files)/8;
  337. int start = open_files / (8 * sizeof(unsigned long));
  338. memset(&new_fdt->open_fds->fds_bits[start], 0, left);
  339. memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
  340. }
  341. rcu_assign_pointer(newf->fdt, new_fdt);
  342. return newf;
  343. out_release:
  344. kmem_cache_free(files_cachep, newf);
  345. out:
  346. return NULL;
  347. }
  348. static void __devinit fdtable_defer_list_init(int cpu)
  349. {
  350. struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
  351. spin_lock_init(&fddef->lock);
  352. INIT_WORK(&fddef->wq, free_fdtable_work);
  353. fddef->next = NULL;
  354. }
  355. void __init files_defer_init(void)
  356. {
  357. int i;
  358. for_each_possible_cpu(i)
  359. fdtable_defer_list_init(i);
  360. sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
  361. -BITS_PER_LONG;
  362. }
  363. struct files_struct init_files = {
  364. .count = ATOMIC_INIT(1),
  365. .fdt = &init_files.fdtab,
  366. .fdtab = {
  367. .max_fds = NR_OPEN_DEFAULT,
  368. .fd = &init_files.fd_array[0],
  369. .close_on_exec = (fd_set *)&init_files.close_on_exec_init,
  370. .open_fds = (fd_set *)&init_files.open_fds_init,
  371. .rcu = RCU_HEAD_INIT,
  372. },
  373. .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
  374. };