file.c 11 KB

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