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