Root/
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 | |
9 | #include <linux/module.h> |
10 | #include <linux/fs.h> |
11 | #include <linux/mm.h> |
12 | #include <linux/time.h> |
13 | #include <linux/sched.h> |
14 | #include <linux/slab.h> |
15 | #include <linux/vmalloc.h> |
16 | #include <linux/file.h> |
17 | #include <linux/fdtable.h> |
18 | #include <linux/bitops.h> |
19 | #include <linux/interrupt.h> |
20 | #include <linux/spinlock.h> |
21 | #include <linux/rcupdate.h> |
22 | #include <linux/workqueue.h> |
23 | |
24 | struct fdtable_defer { |
25 | spinlock_t lock; |
26 | struct work_struct wq; |
27 | struct fdtable *next; |
28 | }; |
29 | |
30 | int sysctl_nr_open __read_mostly = 1024*1024; |
31 | int sysctl_nr_open_min = BITS_PER_LONG; |
32 | int sysctl_nr_open_max = 1024 * 1024; /* raised later */ |
33 | |
34 | /* |
35 | * We use this list to defer free fdtables that have vmalloced |
36 | * sets/arrays. By keeping a per-cpu list, we avoid having to embed |
37 | * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in |
38 | * this per-task structure. |
39 | */ |
40 | static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list); |
41 | |
42 | static inline void * alloc_fdmem(unsigned int size) |
43 | { |
44 | if (size <= PAGE_SIZE) |
45 | return kmalloc(size, GFP_KERNEL); |
46 | else |
47 | return vmalloc(size); |
48 | } |
49 | |
50 | static inline void free_fdarr(struct fdtable *fdt) |
51 | { |
52 | if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) |
53 | kfree(fdt->fd); |
54 | else |
55 | vfree(fdt->fd); |
56 | } |
57 | |
58 | static inline void free_fdset(struct fdtable *fdt) |
59 | { |
60 | if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2)) |
61 | kfree(fdt->open_fds); |
62 | else |
63 | vfree(fdt->open_fds); |
64 | } |
65 | |
66 | static void free_fdtable_work(struct work_struct *work) |
67 | { |
68 | struct fdtable_defer *f = |
69 | container_of(work, struct fdtable_defer, wq); |
70 | struct fdtable *fdt; |
71 | |
72 | spin_lock_bh(&f->lock); |
73 | fdt = f->next; |
74 | f->next = NULL; |
75 | spin_unlock_bh(&f->lock); |
76 | while(fdt) { |
77 | struct fdtable *next = fdt->next; |
78 | vfree(fdt->fd); |
79 | free_fdset(fdt); |
80 | kfree(fdt); |
81 | fdt = next; |
82 | } |
83 | } |
84 | |
85 | void free_fdtable_rcu(struct rcu_head *rcu) |
86 | { |
87 | struct fdtable *fdt = container_of(rcu, struct fdtable, rcu); |
88 | struct fdtable_defer *fddef; |
89 | |
90 | BUG_ON(!fdt); |
91 | |
92 | if (fdt->max_fds <= NR_OPEN_DEFAULT) { |
93 | /* |
94 | * This fdtable is embedded in the files structure and that |
95 | * structure itself is getting destroyed. |
96 | */ |
97 | kmem_cache_free(files_cachep, |
98 | container_of(fdt, struct files_struct, fdtab)); |
99 | return; |
100 | } |
101 | if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) { |
102 | kfree(fdt->fd); |
103 | kfree(fdt->open_fds); |
104 | kfree(fdt); |
105 | } else { |
106 | fddef = &get_cpu_var(fdtable_defer_list); |
107 | spin_lock(&fddef->lock); |
108 | fdt->next = fddef->next; |
109 | fddef->next = fdt; |
110 | /* vmallocs are handled from the workqueue context */ |
111 | schedule_work(&fddef->wq); |
112 | spin_unlock(&fddef->lock); |
113 | put_cpu_var(fdtable_defer_list); |
114 | } |
115 | } |
116 | |
117 | /* |
118 | * Expand the fdset in the files_struct. Called with the files spinlock |
119 | * held for write. |
120 | */ |
121 | static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt) |
122 | { |
123 | unsigned int cpy, set; |
124 | |
125 | BUG_ON(nfdt->max_fds < ofdt->max_fds); |
126 | |
127 | cpy = ofdt->max_fds * sizeof(struct file *); |
128 | set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *); |
129 | memcpy(nfdt->fd, ofdt->fd, cpy); |
130 | memset((char *)(nfdt->fd) + cpy, 0, set); |
131 | |
132 | cpy = ofdt->max_fds / BITS_PER_BYTE; |
133 | set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE; |
134 | memcpy(nfdt->open_fds, ofdt->open_fds, cpy); |
135 | memset((char *)(nfdt->open_fds) + cpy, 0, set); |
136 | memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy); |
137 | memset((char *)(nfdt->close_on_exec) + cpy, 0, set); |
138 | } |
139 | |
140 | static struct fdtable * alloc_fdtable(unsigned int nr) |
141 | { |
142 | struct fdtable *fdt; |
143 | char *data; |
144 | |
145 | /* |
146 | * Figure out how many fds we actually want to support in this fdtable. |
147 | * Allocation steps are keyed to the size of the fdarray, since it |
148 | * grows far faster than any of the other dynamic data. We try to fit |
149 | * the fdarray into comfortable page-tuned chunks: starting at 1024B |
150 | * and growing in powers of two from there on. |
151 | */ |
152 | nr /= (1024 / sizeof(struct file *)); |
153 | nr = roundup_pow_of_two(nr + 1); |
154 | nr *= (1024 / sizeof(struct file *)); |
155 | /* |
156 | * Note that this can drive nr *below* what we had passed if sysctl_nr_open |
157 | * had been set lower between the check in expand_files() and here. Deal |
158 | * with that in caller, it's cheaper that way. |
159 | * |
160 | * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise |
161 | * bitmaps handling below becomes unpleasant, to put it mildly... |
162 | */ |
163 | if (unlikely(nr > sysctl_nr_open)) |
164 | nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1; |
165 | |
166 | fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL); |
167 | if (!fdt) |
168 | goto out; |
169 | fdt->max_fds = nr; |
170 | data = alloc_fdmem(nr * sizeof(struct file *)); |
171 | if (!data) |
172 | goto out_fdt; |
173 | fdt->fd = (struct file **)data; |
174 | data = alloc_fdmem(max_t(unsigned int, |
175 | 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES)); |
176 | if (!data) |
177 | goto out_arr; |
178 | fdt->open_fds = (fd_set *)data; |
179 | data += nr / BITS_PER_BYTE; |
180 | fdt->close_on_exec = (fd_set *)data; |
181 | INIT_RCU_HEAD(&fdt->rcu); |
182 | fdt->next = NULL; |
183 | |
184 | return fdt; |
185 | |
186 | out_arr: |
187 | free_fdarr(fdt); |
188 | out_fdt: |
189 | kfree(fdt); |
190 | out: |
191 | return NULL; |
192 | } |
193 | |
194 | /* |
195 | * Expand the file descriptor table. |
196 | * This function will allocate a new fdtable and both fd array and fdset, of |
197 | * the given size. |
198 | * Return <0 error code on error; 1 on successful completion. |
199 | * The files->file_lock should be held on entry, and will be held on exit. |
200 | */ |
201 | static int expand_fdtable(struct files_struct *files, int nr) |
202 | __releases(files->file_lock) |
203 | __acquires(files->file_lock) |
204 | { |
205 | struct fdtable *new_fdt, *cur_fdt; |
206 | |
207 | spin_unlock(&files->file_lock); |
208 | new_fdt = alloc_fdtable(nr); |
209 | spin_lock(&files->file_lock); |
210 | if (!new_fdt) |
211 | return -ENOMEM; |
212 | /* |
213 | * extremely unlikely race - sysctl_nr_open decreased between the check in |
214 | * caller and alloc_fdtable(). Cheaper to catch it here... |
215 | */ |
216 | if (unlikely(new_fdt->max_fds <= nr)) { |
217 | free_fdarr(new_fdt); |
218 | free_fdset(new_fdt); |
219 | kfree(new_fdt); |
220 | return -EMFILE; |
221 | } |
222 | /* |
223 | * Check again since another task may have expanded the fd table while |
224 | * we dropped the lock |
225 | */ |
226 | cur_fdt = files_fdtable(files); |
227 | if (nr >= cur_fdt->max_fds) { |
228 | /* Continue as planned */ |
229 | copy_fdtable(new_fdt, cur_fdt); |
230 | rcu_assign_pointer(files->fdt, new_fdt); |
231 | if (cur_fdt->max_fds > NR_OPEN_DEFAULT) |
232 | free_fdtable(cur_fdt); |
233 | } else { |
234 | /* Somebody else expanded, so undo our attempt */ |
235 | free_fdarr(new_fdt); |
236 | free_fdset(new_fdt); |
237 | kfree(new_fdt); |
238 | } |
239 | return 1; |
240 | } |
241 | |
242 | /* |
243 | * Expand files. |
244 | * This function will expand the file structures, if the requested size exceeds |
245 | * the current capacity and there is room for expansion. |
246 | * Return <0 error code on error; 0 when nothing done; 1 when files were |
247 | * expanded and execution may have blocked. |
248 | * The files->file_lock should be held on entry, and will be held on exit. |
249 | */ |
250 | int expand_files(struct files_struct *files, int nr) |
251 | { |
252 | struct fdtable *fdt; |
253 | |
254 | fdt = files_fdtable(files); |
255 | |
256 | /* |
257 | * N.B. For clone tasks sharing a files structure, this test |
258 | * will limit the total number of files that can be opened. |
259 | */ |
260 | if (nr >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) |
261 | return -EMFILE; |
262 | |
263 | /* Do we need to expand? */ |
264 | if (nr < fdt->max_fds) |
265 | return 0; |
266 | |
267 | /* Can we expand? */ |
268 | if (nr >= sysctl_nr_open) |
269 | return -EMFILE; |
270 | |
271 | /* All good, so we try */ |
272 | return expand_fdtable(files, nr); |
273 | } |
274 | EXPORT_SYMBOL_GPL(expand_files); |
275 | |
276 | static int count_open_files(struct fdtable *fdt) |
277 | { |
278 | int size = fdt->max_fds; |
279 | int i; |
280 | |
281 | /* Find the last open fd */ |
282 | for (i = size/(8*sizeof(long)); i > 0; ) { |
283 | if (fdt->open_fds->fds_bits[--i]) |
284 | break; |
285 | } |
286 | i = (i+1) * 8 * sizeof(long); |
287 | return i; |
288 | } |
289 | |
290 | /* |
291 | * Allocate a new files structure and copy contents from the |
292 | * passed in files structure. |
293 | * errorp will be valid only when the returned files_struct is NULL. |
294 | */ |
295 | struct files_struct *dup_fd(struct files_struct *oldf, int *errorp) |
296 | { |
297 | struct files_struct *newf; |
298 | struct file **old_fds, **new_fds; |
299 | int open_files, size, i; |
300 | struct fdtable *old_fdt, *new_fdt; |
301 | |
302 | *errorp = -ENOMEM; |
303 | newf = kmem_cache_alloc(files_cachep, GFP_KERNEL); |
304 | if (!newf) |
305 | goto out; |
306 | |
307 | atomic_set(&newf->count, 1); |
308 | |
309 | spin_lock_init(&newf->file_lock); |
310 | newf->next_fd = 0; |
311 | new_fdt = &newf->fdtab; |
312 | new_fdt->max_fds = NR_OPEN_DEFAULT; |
313 | new_fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init; |
314 | new_fdt->open_fds = (fd_set *)&newf->open_fds_init; |
315 | new_fdt->fd = &newf->fd_array[0]; |
316 | INIT_RCU_HEAD(&new_fdt->rcu); |
317 | new_fdt->next = NULL; |
318 | |
319 | spin_lock(&oldf->file_lock); |
320 | old_fdt = files_fdtable(oldf); |
321 | open_files = count_open_files(old_fdt); |
322 | |
323 | /* |
324 | * Check whether we need to allocate a larger fd array and fd set. |
325 | */ |
326 | while (unlikely(open_files > new_fdt->max_fds)) { |
327 | spin_unlock(&oldf->file_lock); |
328 | |
329 | if (new_fdt != &newf->fdtab) { |
330 | free_fdarr(new_fdt); |
331 | free_fdset(new_fdt); |
332 | kfree(new_fdt); |
333 | } |
334 | |
335 | new_fdt = alloc_fdtable(open_files - 1); |
336 | if (!new_fdt) { |
337 | *errorp = -ENOMEM; |
338 | goto out_release; |
339 | } |
340 | |
341 | /* beyond sysctl_nr_open; nothing to do */ |
342 | if (unlikely(new_fdt->max_fds < open_files)) { |
343 | free_fdarr(new_fdt); |
344 | free_fdset(new_fdt); |
345 | kfree(new_fdt); |
346 | *errorp = -EMFILE; |
347 | goto out_release; |
348 | } |
349 | |
350 | /* |
351 | * Reacquire the oldf lock and a pointer to its fd table |
352 | * who knows it may have a new bigger fd table. We need |
353 | * the latest pointer. |
354 | */ |
355 | spin_lock(&oldf->file_lock); |
356 | old_fdt = files_fdtable(oldf); |
357 | open_files = count_open_files(old_fdt); |
358 | } |
359 | |
360 | old_fds = old_fdt->fd; |
361 | new_fds = new_fdt->fd; |
362 | |
363 | memcpy(new_fdt->open_fds->fds_bits, |
364 | old_fdt->open_fds->fds_bits, open_files/8); |
365 | memcpy(new_fdt->close_on_exec->fds_bits, |
366 | old_fdt->close_on_exec->fds_bits, open_files/8); |
367 | |
368 | for (i = open_files; i != 0; i--) { |
369 | struct file *f = *old_fds++; |
370 | if (f) { |
371 | get_file(f); |
372 | } else { |
373 | /* |
374 | * The fd may be claimed in the fd bitmap but not yet |
375 | * instantiated in the files array if a sibling thread |
376 | * is partway through open(). So make sure that this |
377 | * fd is available to the new process. |
378 | */ |
379 | FD_CLR(open_files - i, new_fdt->open_fds); |
380 | } |
381 | rcu_assign_pointer(*new_fds++, f); |
382 | } |
383 | spin_unlock(&oldf->file_lock); |
384 | |
385 | /* compute the remainder to be cleared */ |
386 | size = (new_fdt->max_fds - open_files) * sizeof(struct file *); |
387 | |
388 | /* This is long word aligned thus could use a optimized version */ |
389 | memset(new_fds, 0, size); |
390 | |
391 | if (new_fdt->max_fds > open_files) { |
392 | int left = (new_fdt->max_fds-open_files)/8; |
393 | int start = open_files / (8 * sizeof(unsigned long)); |
394 | |
395 | memset(&new_fdt->open_fds->fds_bits[start], 0, left); |
396 | memset(&new_fdt->close_on_exec->fds_bits[start], 0, left); |
397 | } |
398 | |
399 | rcu_assign_pointer(newf->fdt, new_fdt); |
400 | |
401 | return newf; |
402 | |
403 | out_release: |
404 | kmem_cache_free(files_cachep, newf); |
405 | out: |
406 | return NULL; |
407 | } |
408 | |
409 | static void __devinit fdtable_defer_list_init(int cpu) |
410 | { |
411 | struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu); |
412 | spin_lock_init(&fddef->lock); |
413 | INIT_WORK(&fddef->wq, free_fdtable_work); |
414 | fddef->next = NULL; |
415 | } |
416 | |
417 | void __init files_defer_init(void) |
418 | { |
419 | int i; |
420 | for_each_possible_cpu(i) |
421 | fdtable_defer_list_init(i); |
422 | sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) & |
423 | -BITS_PER_LONG; |
424 | } |
425 | |
426 | struct files_struct init_files = { |
427 | .count = ATOMIC_INIT(1), |
428 | .fdt = &init_files.fdtab, |
429 | .fdtab = { |
430 | .max_fds = NR_OPEN_DEFAULT, |
431 | .fd = &init_files.fd_array[0], |
432 | .close_on_exec = (fd_set *)&init_files.close_on_exec_init, |
433 | .open_fds = (fd_set *)&init_files.open_fds_init, |
434 | .rcu = RCU_HEAD_INIT, |
435 | }, |
436 | .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock), |
437 | }; |
438 | |
439 | /* |
440 | * allocate a file descriptor, mark it busy. |
441 | */ |
442 | int alloc_fd(unsigned start, unsigned flags) |
443 | { |
444 | struct files_struct *files = current->files; |
445 | unsigned int fd; |
446 | int error; |
447 | struct fdtable *fdt; |
448 | |
449 | spin_lock(&files->file_lock); |
450 | repeat: |
451 | fdt = files_fdtable(files); |
452 | fd = start; |
453 | if (fd < files->next_fd) |
454 | fd = files->next_fd; |
455 | |
456 | if (fd < fdt->max_fds) |
457 | fd = find_next_zero_bit(fdt->open_fds->fds_bits, |
458 | fdt->max_fds, fd); |
459 | |
460 | error = expand_files(files, fd); |
461 | if (error < 0) |
462 | goto out; |
463 | |
464 | /* |
465 | * If we needed to expand the fs array we |
466 | * might have blocked - try again. |
467 | */ |
468 | if (error) |
469 | goto repeat; |
470 | |
471 | if (start <= files->next_fd) |
472 | files->next_fd = fd + 1; |
473 | |
474 | FD_SET(fd, fdt->open_fds); |
475 | if (flags & O_CLOEXEC) |
476 | FD_SET(fd, fdt->close_on_exec); |
477 | else |
478 | FD_CLR(fd, fdt->close_on_exec); |
479 | error = fd; |
480 | #if 1 |
481 | /* Sanity check */ |
482 | if (rcu_dereference(fdt->fd[fd]) != NULL) { |
483 | printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd); |
484 | rcu_assign_pointer(fdt->fd[fd], NULL); |
485 | } |
486 | #endif |
487 | |
488 | out: |
489 | spin_unlock(&files->file_lock); |
490 | return error; |
491 | } |
492 | |
493 | int get_unused_fd(void) |
494 | { |
495 | return alloc_fd(0, 0); |
496 | } |
497 | EXPORT_SYMBOL(get_unused_fd); |
498 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9