4 * Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
6 * Manage the dynamic fd arrays in the process files_struct.
11 #include <linux/time.h>
12 #include <linux/slab.h>
13 #include <linux/vmalloc.h>
14 #include <linux/file.h>
15 #include <linux/fdtable.h>
16 #include <linux/bitops.h>
17 #include <linux/interrupt.h>
18 #include <linux/spinlock.h>
19 #include <linux/rcupdate.h>
20 #include <linux/workqueue.h>
22 struct fdtable_defer {
24 struct work_struct wq;
28 int sysctl_nr_open __read_mostly = 1024*1024;
31 * We use this list to defer free fdtables that have vmalloced
32 * sets/arrays. By keeping a per-cpu list, we avoid having to embed
33 * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
34 * this per-task structure.
36 static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
38 static inline void * alloc_fdmem(unsigned int size)
40 if (size <= PAGE_SIZE)
41 return kmalloc(size, GFP_KERNEL);
46 static inline void free_fdarr(struct fdtable *fdt)
48 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *)))
54 static inline void free_fdset(struct fdtable *fdt)
56 if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2))
62 static void free_fdtable_work(struct work_struct *work)
64 struct fdtable_defer *f =
65 container_of(work, struct fdtable_defer, wq);
68 spin_lock_bh(&f->lock);
71 spin_unlock_bh(&f->lock);
73 struct fdtable *next = fdt->next;
81 void free_fdtable_rcu(struct rcu_head *rcu)
83 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
84 struct fdtable_defer *fddef;
88 if (fdt->max_fds <= NR_OPEN_DEFAULT) {
90 * This fdtable is embedded in the files structure and that
91 * structure itself is getting destroyed.
93 kmem_cache_free(files_cachep,
94 container_of(fdt, struct files_struct, fdtab));
97 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) {
102 fddef = &get_cpu_var(fdtable_defer_list);
103 spin_lock(&fddef->lock);
104 fdt->next = fddef->next;
106 /* vmallocs are handled from the workqueue context */
107 schedule_work(&fddef->wq);
108 spin_unlock(&fddef->lock);
109 put_cpu_var(fdtable_defer_list);
114 * Expand the fdset in the files_struct. Called with the files spinlock
117 static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
119 unsigned int cpy, set;
121 BUG_ON(nfdt->max_fds < ofdt->max_fds);
122 if (ofdt->max_fds == 0)
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);
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);
138 static struct fdtable * alloc_fdtable(unsigned int nr)
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.
150 nr /= (1024 / sizeof(struct file *));
151 nr = roundup_pow_of_two(nr + 1);
152 nr *= (1024 / sizeof(struct file *));
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.
158 * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
159 * bitmaps handling below becomes unpleasant, to put it mildly...
161 if (unlikely(nr > sysctl_nr_open))
162 nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
164 fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
168 data = alloc_fdmem(nr * sizeof(struct file *));
171 fdt->fd = (struct file **)data;
172 data = alloc_fdmem(max_t(unsigned int,
173 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
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);
193 * Expand the file descriptor table.
194 * This function will allocate a new fdtable and both fd array and fdset, of
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.
199 static int expand_fdtable(struct files_struct *files, int nr)
200 __releases(files->file_lock)
201 __acquires(files->file_lock)
203 struct fdtable *new_fdt, *cur_fdt;
205 spin_unlock(&files->file_lock);
206 new_fdt = alloc_fdtable(nr);
207 spin_lock(&files->file_lock);
211 * extremely unlikely race - sysctl_nr_open decreased between the check in
212 * caller and alloc_fdtable(). Cheaper to catch it here...
214 if (unlikely(new_fdt->max_fds <= nr)) {
221 * Check again since another task may have expanded the fd table while
222 * we dropped the lock
224 cur_fdt = files_fdtable(files);
225 if (nr >= cur_fdt->max_fds) {
226 /* Continue as planned */
227 copy_fdtable(new_fdt, cur_fdt);
228 rcu_assign_pointer(files->fdt, new_fdt);
229 if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
230 free_fdtable(cur_fdt);
232 /* Somebody else expanded, so undo our attempt */
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.
248 int expand_files(struct files_struct *files, int nr)
252 fdt = files_fdtable(files);
253 /* Do we need to expand? */
254 if (nr < fdt->max_fds)
257 if (nr >= sysctl_nr_open)
260 /* All good, so we try */
261 return expand_fdtable(files, nr);
264 static int count_open_files(struct fdtable *fdt)
266 int size = fdt->max_fds;
269 /* Find the last open fd */
270 for (i = size/(8*sizeof(long)); i > 0; ) {
271 if (fdt->open_fds->fds_bits[--i])
274 i = (i+1) * 8 * sizeof(long);
278 static struct files_struct *alloc_files(void)
280 struct files_struct *newf;
283 newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
287 atomic_set(&newf->count, 1);
289 spin_lock_init(&newf->file_lock);
292 fdt->max_fds = NR_OPEN_DEFAULT;
293 fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
294 fdt->open_fds = (fd_set *)&newf->open_fds_init;
295 fdt->fd = &newf->fd_array[0];
296 INIT_RCU_HEAD(&fdt->rcu);
298 rcu_assign_pointer(newf->fdt, fdt);
304 * Allocate a new files structure and copy contents from the
305 * passed in files structure.
306 * errorp will be valid only when the returned files_struct is NULL.
308 struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
310 struct files_struct *newf;
311 struct file **old_fds, **new_fds;
312 int open_files, size, i;
313 struct fdtable *old_fdt, *new_fdt;
316 newf = alloc_files();
320 spin_lock(&oldf->file_lock);
321 old_fdt = files_fdtable(oldf);
322 new_fdt = files_fdtable(newf);
323 open_files = count_open_files(old_fdt);
326 * Check whether we need to allocate a larger fd array and fd set.
327 * Note: we're not a clone task, so the open count won't change.
329 if (open_files > new_fdt->max_fds) {
330 new_fdt->max_fds = 0;
331 spin_unlock(&oldf->file_lock);
332 spin_lock(&newf->file_lock);
333 *errorp = expand_files(newf, open_files-1);
334 spin_unlock(&newf->file_lock);
337 new_fdt = files_fdtable(newf);
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.
343 spin_lock(&oldf->file_lock);
344 old_fdt = files_fdtable(oldf);
347 old_fds = old_fdt->fd;
348 new_fds = new_fdt->fd;
350 memcpy(new_fdt->open_fds->fds_bits,
351 old_fdt->open_fds->fds_bits, open_files/8);
352 memcpy(new_fdt->close_on_exec->fds_bits,
353 old_fdt->close_on_exec->fds_bits, open_files/8);
355 for (i = open_files; i != 0; i--) {
356 struct file *f = *old_fds++;
361 * The fd may be claimed in the fd bitmap but not yet
362 * instantiated in the files array if a sibling thread
363 * is partway through open(). So make sure that this
364 * fd is available to the new process.
366 FD_CLR(open_files - i, new_fdt->open_fds);
368 rcu_assign_pointer(*new_fds++, f);
370 spin_unlock(&oldf->file_lock);
372 /* compute the remainder to be cleared */
373 size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
375 /* This is long word aligned thus could use a optimized version */
376 memset(new_fds, 0, size);
378 if (new_fdt->max_fds > open_files) {
379 int left = (new_fdt->max_fds-open_files)/8;
380 int start = open_files / (8 * sizeof(unsigned long));
382 memset(&new_fdt->open_fds->fds_bits[start], 0, left);
383 memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
389 kmem_cache_free(files_cachep, newf);
394 static void __devinit fdtable_defer_list_init(int cpu)
396 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
397 spin_lock_init(&fddef->lock);
398 INIT_WORK(&fddef->wq, free_fdtable_work);
402 void __init files_defer_init(void)
405 for_each_possible_cpu(i)
406 fdtable_defer_list_init(i);
409 struct files_struct init_files = {
410 .count = ATOMIC_INIT(1),
411 .fdt = &init_files.fdtab,
413 .max_fds = NR_OPEN_DEFAULT,
414 .fd = &init_files.fd_array[0],
415 .close_on_exec = (fd_set *)&init_files.close_on_exec_init,
416 .open_fds = (fd_set *)&init_files.open_fds_init,
417 .rcu = RCU_HEAD_INIT,
419 .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),