4 * Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
6 * Manage the dynamic fd arrays in the process files_struct.
9 #include <linux/module.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>
24 struct fdtable_defer
{
26 struct work_struct wq
;
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 */
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.
40 static DEFINE_PER_CPU(struct fdtable_defer
, fdtable_defer_list
);
42 static inline void * alloc_fdmem(unsigned int size
)
44 if (size
<= PAGE_SIZE
)
45 return kmalloc(size
, GFP_KERNEL
);
50 static inline void free_fdarr(struct fdtable
*fdt
)
52 if (fdt
->max_fds
<= (PAGE_SIZE
/ sizeof(struct file
*)))
58 static inline void free_fdset(struct fdtable
*fdt
)
60 if (fdt
->max_fds
<= (PAGE_SIZE
* BITS_PER_BYTE
/ 2))
66 static void free_fdtable_work(struct work_struct
*work
)
68 struct fdtable_defer
*f
=
69 container_of(work
, struct fdtable_defer
, wq
);
72 spin_lock_bh(&f
->lock
);
75 spin_unlock_bh(&f
->lock
);
77 struct fdtable
*next
= fdt
->next
;
85 void free_fdtable_rcu(struct rcu_head
*rcu
)
87 struct fdtable
*fdt
= container_of(rcu
, struct fdtable
, rcu
);
88 struct fdtable_defer
*fddef
;
92 if (fdt
->max_fds
<= NR_OPEN_DEFAULT
) {
94 * This fdtable is embedded in the files structure and that
95 * structure itself is getting destroyed.
97 kmem_cache_free(files_cachep
,
98 container_of(fdt
, struct files_struct
, fdtab
));
101 if (fdt
->max_fds
<= (PAGE_SIZE
/ sizeof(struct file
*))) {
103 kfree(fdt
->open_fds
);
106 fddef
= &get_cpu_var(fdtable_defer_list
);
107 spin_lock(&fddef
->lock
);
108 fdt
->next
= fddef
->next
;
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
);
118 * Expand the fdset in the files_struct. Called with the files spinlock
121 static void copy_fdtable(struct fdtable
*nfdt
, struct fdtable
*ofdt
)
123 unsigned int cpy
, set
;
125 BUG_ON(nfdt
->max_fds
< ofdt
->max_fds
);
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
);
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
);
140 static struct fdtable
* alloc_fdtable(unsigned int nr
)
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.
152 nr
/= (1024 / sizeof(struct file
*));
153 nr
= roundup_pow_of_two(nr
+ 1);
154 nr
*= (1024 / sizeof(struct file
*));
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.
160 * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
161 * bitmaps handling below becomes unpleasant, to put it mildly...
163 if (unlikely(nr
> sysctl_nr_open
))
164 nr
= ((sysctl_nr_open
- 1) | (BITS_PER_LONG
- 1)) + 1;
166 fdt
= kmalloc(sizeof(struct fdtable
), GFP_KERNEL
);
170 data
= alloc_fdmem(nr
* sizeof(struct file
*));
173 fdt
->fd
= (struct file
**)data
;
174 data
= alloc_fdmem(max_t(unsigned int,
175 2 * nr
/ BITS_PER_BYTE
, L1_CACHE_BYTES
));
178 fdt
->open_fds
= (fd_set
*)data
;
179 data
+= nr
/ BITS_PER_BYTE
;
180 fdt
->close_on_exec
= (fd_set
*)data
;
194 * Expand the file descriptor table.
195 * This function will allocate a new fdtable and both fd array and fdset, of
197 * Return <0 error code on error; 1 on successful completion.
198 * The files->file_lock should be held on entry, and will be held on exit.
200 static int expand_fdtable(struct files_struct
*files
, int nr
)
201 __releases(files
->file_lock
)
202 __acquires(files
->file_lock
)
204 struct fdtable
*new_fdt
, *cur_fdt
;
206 spin_unlock(&files
->file_lock
);
207 new_fdt
= alloc_fdtable(nr
);
208 spin_lock(&files
->file_lock
);
212 * extremely unlikely race - sysctl_nr_open decreased between the check in
213 * caller and alloc_fdtable(). Cheaper to catch it here...
215 if (unlikely(new_fdt
->max_fds
<= nr
)) {
222 * Check again since another task may have expanded the fd table while
223 * we dropped the lock
225 cur_fdt
= files_fdtable(files
);
226 if (nr
>= cur_fdt
->max_fds
) {
227 /* Continue as planned */
228 copy_fdtable(new_fdt
, cur_fdt
);
229 rcu_assign_pointer(files
->fdt
, new_fdt
);
230 if (cur_fdt
->max_fds
> NR_OPEN_DEFAULT
)
231 free_fdtable(cur_fdt
);
233 /* Somebody else expanded, so undo our attempt */
243 * This function will expand the file structures, if the requested size exceeds
244 * the current capacity and there is room for expansion.
245 * Return <0 error code on error; 0 when nothing done; 1 when files were
246 * expanded and execution may have blocked.
247 * The files->file_lock should be held on entry, and will be held on exit.
249 int expand_files(struct files_struct
*files
, int nr
)
253 fdt
= files_fdtable(files
);
256 * N.B. For clone tasks sharing a files structure, this test
257 * will limit the total number of files that can be opened.
259 if (nr
>= rlimit(RLIMIT_NOFILE
))
262 /* Do we need to expand? */
263 if (nr
< fdt
->max_fds
)
267 if (nr
>= sysctl_nr_open
)
270 /* All good, so we try */
271 return expand_fdtable(files
, nr
);
274 static int count_open_files(struct fdtable
*fdt
)
276 int size
= fdt
->max_fds
;
279 /* Find the last open fd */
280 for (i
= size
/(8*sizeof(long)); i
> 0; ) {
281 if (fdt
->open_fds
->fds_bits
[--i
])
284 i
= (i
+1) * 8 * sizeof(long);
289 * Allocate a new files structure and copy contents from the
290 * passed in files structure.
291 * errorp will be valid only when the returned files_struct is NULL.
293 struct files_struct
*dup_fd(struct files_struct
*oldf
, int *errorp
)
295 struct files_struct
*newf
;
296 struct file
**old_fds
, **new_fds
;
297 int open_files
, size
, i
;
298 struct fdtable
*old_fdt
, *new_fdt
;
301 newf
= kmem_cache_alloc(files_cachep
, GFP_KERNEL
);
305 atomic_set(&newf
->count
, 1);
307 spin_lock_init(&newf
->file_lock
);
309 new_fdt
= &newf
->fdtab
;
310 new_fdt
->max_fds
= NR_OPEN_DEFAULT
;
311 new_fdt
->close_on_exec
= (fd_set
*)&newf
->close_on_exec_init
;
312 new_fdt
->open_fds
= (fd_set
*)&newf
->open_fds_init
;
313 new_fdt
->fd
= &newf
->fd_array
[0];
314 new_fdt
->next
= NULL
;
316 spin_lock(&oldf
->file_lock
);
317 old_fdt
= files_fdtable(oldf
);
318 open_files
= count_open_files(old_fdt
);
321 * Check whether we need to allocate a larger fd array and fd set.
323 while (unlikely(open_files
> new_fdt
->max_fds
)) {
324 spin_unlock(&oldf
->file_lock
);
326 if (new_fdt
!= &newf
->fdtab
) {
332 new_fdt
= alloc_fdtable(open_files
- 1);
338 /* beyond sysctl_nr_open; nothing to do */
339 if (unlikely(new_fdt
->max_fds
< open_files
)) {
348 * Reacquire the oldf lock and a pointer to its fd table
349 * who knows it may have a new bigger fd table. We need
350 * the latest pointer.
352 spin_lock(&oldf
->file_lock
);
353 old_fdt
= files_fdtable(oldf
);
354 open_files
= count_open_files(old_fdt
);
357 old_fds
= old_fdt
->fd
;
358 new_fds
= new_fdt
->fd
;
360 memcpy(new_fdt
->open_fds
->fds_bits
,
361 old_fdt
->open_fds
->fds_bits
, open_files
/8);
362 memcpy(new_fdt
->close_on_exec
->fds_bits
,
363 old_fdt
->close_on_exec
->fds_bits
, open_files
/8);
365 for (i
= open_files
; i
!= 0; i
--) {
366 struct file
*f
= *old_fds
++;
371 * The fd may be claimed in the fd bitmap but not yet
372 * instantiated in the files array if a sibling thread
373 * is partway through open(). So make sure that this
374 * fd is available to the new process.
376 FD_CLR(open_files
- i
, new_fdt
->open_fds
);
378 rcu_assign_pointer(*new_fds
++, f
);
380 spin_unlock(&oldf
->file_lock
);
382 /* compute the remainder to be cleared */
383 size
= (new_fdt
->max_fds
- open_files
) * sizeof(struct file
*);
385 /* This is long word aligned thus could use a optimized version */
386 memset(new_fds
, 0, size
);
388 if (new_fdt
->max_fds
> open_files
) {
389 int left
= (new_fdt
->max_fds
-open_files
)/8;
390 int start
= open_files
/ (8 * sizeof(unsigned long));
392 memset(&new_fdt
->open_fds
->fds_bits
[start
], 0, left
);
393 memset(&new_fdt
->close_on_exec
->fds_bits
[start
], 0, left
);
396 rcu_assign_pointer(newf
->fdt
, new_fdt
);
401 kmem_cache_free(files_cachep
, newf
);
406 static void __devinit
fdtable_defer_list_init(int cpu
)
408 struct fdtable_defer
*fddef
= &per_cpu(fdtable_defer_list
, cpu
);
409 spin_lock_init(&fddef
->lock
);
410 INIT_WORK(&fddef
->wq
, free_fdtable_work
);
414 void __init
files_defer_init(void)
417 for_each_possible_cpu(i
)
418 fdtable_defer_list_init(i
);
419 sysctl_nr_open_max
= min((size_t)INT_MAX
, ~(size_t)0/sizeof(void *)) &
423 struct files_struct init_files
= {
424 .count
= ATOMIC_INIT(1),
425 .fdt
= &init_files
.fdtab
,
427 .max_fds
= NR_OPEN_DEFAULT
,
428 .fd
= &init_files
.fd_array
[0],
429 .close_on_exec
= (fd_set
*)&init_files
.close_on_exec_init
,
430 .open_fds
= (fd_set
*)&init_files
.open_fds_init
,
432 .file_lock
= __SPIN_LOCK_UNLOCKED(init_task
.file_lock
),
436 * allocate a file descriptor, mark it busy.
438 int alloc_fd(unsigned start
, unsigned flags
)
440 struct files_struct
*files
= current
->files
;
445 spin_lock(&files
->file_lock
);
447 fdt
= files_fdtable(files
);
449 if (fd
< files
->next_fd
)
452 if (fd
< fdt
->max_fds
)
453 fd
= find_next_zero_bit(fdt
->open_fds
->fds_bits
,
456 error
= expand_files(files
, fd
);
461 * If we needed to expand the fs array we
462 * might have blocked - try again.
467 if (start
<= files
->next_fd
)
468 files
->next_fd
= fd
+ 1;
470 FD_SET(fd
, fdt
->open_fds
);
471 if (flags
& O_CLOEXEC
)
472 FD_SET(fd
, fdt
->close_on_exec
);
474 FD_CLR(fd
, fdt
->close_on_exec
);
478 if (rcu_dereference_raw(fdt
->fd
[fd
]) != NULL
) {
479 printk(KERN_WARNING
"alloc_fd: slot %d not NULL!\n", fd
);
480 rcu_assign_pointer(fdt
->fd
[fd
], NULL
);
485 spin_unlock(&files
->file_lock
);
489 int get_unused_fd(void)
491 return alloc_fd(0, 0);
493 EXPORT_SYMBOL(get_unused_fd
);