Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
| 15 | #include <linux/bitops.h> |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/rcupdate.h> |
| 19 | #include <linux/workqueue.h> |
| 20 | |
| 21 | struct fdtable_defer { |
| 22 | spinlock_t lock; |
| 23 | struct work_struct wq; |
| 24 | struct timer_list timer; |
| 25 | struct fdtable *next; |
| 26 | }; |
| 27 | |
| 28 | /* |
| 29 | * We use this list to defer free fdtables that have vmalloced |
| 30 | * sets/arrays. By keeping a per-cpu list, we avoid having to embed |
| 31 | * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in |
| 32 | * this per-task structure. |
| 33 | */ |
| 34 | static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | |
| 37 | /* |
| 38 | * Allocate an fd array, using kmalloc or vmalloc. |
| 39 | * Note: the array isn't cleared at allocation time. |
| 40 | */ |
| 41 | struct file ** alloc_fd_array(int num) |
| 42 | { |
| 43 | struct file **new_fds; |
| 44 | int size = num * sizeof(struct file *); |
| 45 | |
| 46 | if (size <= PAGE_SIZE) |
| 47 | new_fds = (struct file **) kmalloc(size, GFP_KERNEL); |
| 48 | else |
| 49 | new_fds = (struct file **) vmalloc(size); |
| 50 | return new_fds; |
| 51 | } |
| 52 | |
| 53 | void free_fd_array(struct file **array, int num) |
| 54 | { |
| 55 | int size = num * sizeof(struct file *); |
| 56 | |
| 57 | if (!array) { |
| 58 | printk (KERN_ERR "free_fd_array: array = 0 (num = %d)\n", num); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if (num <= NR_OPEN_DEFAULT) /* Don't free the embedded fd array! */ |
| 63 | return; |
| 64 | else if (size <= PAGE_SIZE) |
| 65 | kfree(array); |
| 66 | else |
| 67 | vfree(array); |
| 68 | } |
| 69 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 70 | static void __free_fdtable(struct fdtable *fdt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Dipankar Sarma | 0b175a7 | 2005-09-15 00:48:42 +0530 | [diff] [blame] | 72 | free_fdset(fdt->open_fds, fdt->max_fdset); |
| 73 | free_fdset(fdt->close_on_exec, fdt->max_fdset); |
| 74 | free_fd_array(fdt->fd, fdt->max_fds); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 75 | kfree(fdt); |
| 76 | } |
| 77 | |
| 78 | static void fdtable_timer(unsigned long data) |
| 79 | { |
| 80 | struct fdtable_defer *fddef = (struct fdtable_defer *)data; |
| 81 | |
| 82 | spin_lock(&fddef->lock); |
| 83 | /* |
| 84 | * If someone already emptied the queue return. |
| 85 | */ |
| 86 | if (!fddef->next) |
| 87 | goto out; |
| 88 | if (!schedule_work(&fddef->wq)) |
| 89 | mod_timer(&fddef->timer, 5); |
| 90 | out: |
| 91 | spin_unlock(&fddef->lock); |
| 92 | } |
| 93 | |
| 94 | static void free_fdtable_work(struct fdtable_defer *f) |
| 95 | { |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 96 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 98 | spin_lock_bh(&f->lock); |
| 99 | fdt = f->next; |
| 100 | f->next = NULL; |
| 101 | spin_unlock_bh(&f->lock); |
| 102 | while(fdt) { |
| 103 | struct fdtable *next = fdt->next; |
| 104 | __free_fdtable(fdt); |
| 105 | fdt = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void free_fdtable_rcu(struct rcu_head *rcu) |
| 110 | { |
| 111 | struct fdtable *fdt = container_of(rcu, struct fdtable, rcu); |
| 112 | int fdset_size, fdarray_size; |
| 113 | struct fdtable_defer *fddef; |
| 114 | |
| 115 | BUG_ON(!fdt); |
| 116 | fdset_size = fdt->max_fdset / 8; |
| 117 | fdarray_size = fdt->max_fds * sizeof(struct file *); |
| 118 | |
| 119 | if (fdt->free_files) { |
| 120 | /* |
| 121 | * The this fdtable was embedded in the files structure |
| 122 | * and the files structure itself was getting destroyed. |
| 123 | * It is now safe to free the files structure. |
| 124 | */ |
| 125 | kmem_cache_free(files_cachep, fdt->free_files); |
| 126 | return; |
| 127 | } |
Eric Dumazet | 0c9e63f | 2006-03-23 03:00:12 -0800 | [diff] [blame] | 128 | if (fdt->max_fdset <= EMBEDDED_FD_SET_SIZE && |
| 129 | fdt->max_fds <= NR_OPEN_DEFAULT) { |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 130 | /* |
| 131 | * The fdtable was embedded |
| 132 | */ |
| 133 | return; |
| 134 | } |
| 135 | if (fdset_size <= PAGE_SIZE && fdarray_size <= PAGE_SIZE) { |
| 136 | kfree(fdt->open_fds); |
| 137 | kfree(fdt->close_on_exec); |
| 138 | kfree(fdt->fd); |
| 139 | kfree(fdt); |
| 140 | } else { |
| 141 | fddef = &get_cpu_var(fdtable_defer_list); |
| 142 | spin_lock(&fddef->lock); |
| 143 | fdt->next = fddef->next; |
| 144 | fddef->next = fdt; |
| 145 | /* |
| 146 | * vmallocs are handled from the workqueue context. |
| 147 | * If the per-cpu workqueue is running, then we |
| 148 | * defer work scheduling through a timer. |
| 149 | */ |
| 150 | if (!schedule_work(&fddef->wq)) |
| 151 | mod_timer(&fddef->timer, 5); |
| 152 | spin_unlock(&fddef->lock); |
| 153 | put_cpu_var(fdtable_defer_list); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void free_fdtable(struct fdtable *fdt) |
| 158 | { |
Eric Dumazet | 0c9e63f | 2006-03-23 03:00:12 -0800 | [diff] [blame] | 159 | if (fdt->free_files || |
| 160 | fdt->max_fdset > EMBEDDED_FD_SET_SIZE || |
| 161 | fdt->max_fds > NR_OPEN_DEFAULT) |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 162 | call_rcu(&fdt->rcu, free_fdtable_rcu); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Expand the fdset in the files_struct. Called with the files spinlock |
| 167 | * held for write. |
| 168 | */ |
| 169 | static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt) |
| 170 | { |
| 171 | int i; |
| 172 | int count; |
| 173 | |
| 174 | BUG_ON(nfdt->max_fdset < fdt->max_fdset); |
| 175 | BUG_ON(nfdt->max_fds < fdt->max_fds); |
| 176 | /* Copy the existing tables and install the new pointers */ |
| 177 | |
| 178 | i = fdt->max_fdset / (sizeof(unsigned long) * 8); |
| 179 | count = (nfdt->max_fdset - fdt->max_fdset) / 8; |
| 180 | |
| 181 | /* |
| 182 | * Don't copy the entire array if the current fdset is |
| 183 | * not yet initialised. |
| 184 | */ |
| 185 | if (i) { |
| 186 | memcpy (nfdt->open_fds, fdt->open_fds, |
| 187 | fdt->max_fdset/8); |
| 188 | memcpy (nfdt->close_on_exec, fdt->close_on_exec, |
| 189 | fdt->max_fdset/8); |
| 190 | memset (&nfdt->open_fds->fds_bits[i], 0, count); |
| 191 | memset (&nfdt->close_on_exec->fds_bits[i], 0, count); |
| 192 | } |
| 193 | |
| 194 | /* Don't copy/clear the array if we are creating a new |
| 195 | fd array for fork() */ |
| 196 | if (fdt->max_fds) { |
| 197 | memcpy(nfdt->fd, fdt->fd, |
| 198 | fdt->max_fds * sizeof(struct file *)); |
| 199 | /* clear the remainder of the array */ |
| 200 | memset(&nfdt->fd[fdt->max_fds], 0, |
| 201 | (nfdt->max_fds - fdt->max_fds) * |
| 202 | sizeof(struct file *)); |
| 203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Allocate an fdset array, using kmalloc or vmalloc. |
| 208 | * Note: the array isn't cleared at allocation time. |
| 209 | */ |
| 210 | fd_set * alloc_fdset(int num) |
| 211 | { |
| 212 | fd_set *new_fdset; |
| 213 | int size = num / 8; |
| 214 | |
| 215 | if (size <= PAGE_SIZE) |
| 216 | new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL); |
| 217 | else |
| 218 | new_fdset = (fd_set *) vmalloc(size); |
| 219 | return new_fdset; |
| 220 | } |
| 221 | |
| 222 | void free_fdset(fd_set *array, int num) |
| 223 | { |
Eric Dumazet | 0c9e63f | 2006-03-23 03:00:12 -0800 | [diff] [blame] | 224 | if (num <= EMBEDDED_FD_SET_SIZE) /* Don't free an embedded fdset */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | return; |
Eric Dumazet | 0c9e63f | 2006-03-23 03:00:12 -0800 | [diff] [blame] | 226 | else if (num <= 8 * PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | kfree(array); |
| 228 | else |
| 229 | vfree(array); |
| 230 | } |
| 231 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 232 | static struct fdtable *alloc_fdtable(int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 234 | struct fdtable *fdt = NULL; |
| 235 | int nfds = 0; |
| 236 | fd_set *new_openset = NULL, *new_execset = NULL; |
| 237 | struct file **new_fds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Eric Dumazet | 0c9e63f | 2006-03-23 03:00:12 -0800 | [diff] [blame] | 239 | fdt = kzalloc(sizeof(*fdt), GFP_KERNEL); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 240 | if (!fdt) |
| 241 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Andrew Morton | a29b0b7 | 2006-07-12 09:03:08 -0700 | [diff] [blame] | 243 | nfds = max_t(int, 8 * L1_CACHE_BYTES, roundup_pow_of_two(nr + 1)); |
Andrew Morton | 92eb7a2 | 2006-07-10 04:45:31 -0700 | [diff] [blame] | 244 | if (nfds > NR_OPEN) |
| 245 | nfds = NR_OPEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 247 | new_openset = alloc_fdset(nfds); |
| 248 | new_execset = alloc_fdset(nfds); |
| 249 | if (!new_openset || !new_execset) |
| 250 | goto out; |
| 251 | fdt->open_fds = new_openset; |
| 252 | fdt->close_on_exec = new_execset; |
| 253 | fdt->max_fdset = nfds; |
| 254 | |
| 255 | nfds = NR_OPEN_DEFAULT; |
| 256 | /* |
| 257 | * Expand to the max in easy steps, and keep expanding it until |
| 258 | * we have enough for the requested fd array size. |
| 259 | */ |
| 260 | do { |
| 261 | #if NR_OPEN_DEFAULT < 256 |
| 262 | if (nfds < 256) |
| 263 | nfds = 256; |
| 264 | else |
| 265 | #endif |
| 266 | if (nfds < (PAGE_SIZE / sizeof(struct file *))) |
| 267 | nfds = PAGE_SIZE / sizeof(struct file *); |
| 268 | else { |
| 269 | nfds = nfds * 2; |
| 270 | if (nfds > NR_OPEN) |
| 271 | nfds = NR_OPEN; |
| 272 | } |
| 273 | } while (nfds <= nr); |
| 274 | new_fds = alloc_fd_array(nfds); |
| 275 | if (!new_fds) |
Kirill Korotaev | d579091 | 2006-07-12 09:03:05 -0700 | [diff] [blame] | 276 | goto out2; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 277 | fdt->fd = new_fds; |
| 278 | fdt->max_fds = nfds; |
| 279 | fdt->free_files = NULL; |
| 280 | return fdt; |
Kirill Korotaev | d579091 | 2006-07-12 09:03:05 -0700 | [diff] [blame] | 281 | out2: |
| 282 | nfds = fdt->max_fdset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | out: |
Andrew Morton | 8b0e330 | 2006-09-27 01:51:02 -0700 | [diff] [blame] | 284 | free_fdset(new_openset, nfds); |
| 285 | free_fdset(new_execset, nfds); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 286 | kfree(fdt); |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | /* |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 291 | * Expand the file descriptor table. |
| 292 | * This function will allocate a new fdtable and both fd array and fdset, of |
| 293 | * the given size. |
| 294 | * Return <0 error code on error; 1 on successful completion. |
| 295 | * The files->file_lock should be held on entry, and will be held on exit. |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 296 | */ |
| 297 | static int expand_fdtable(struct files_struct *files, int nr) |
| 298 | __releases(files->file_lock) |
| 299 | __acquires(files->file_lock) |
| 300 | { |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 301 | struct fdtable *new_fdt, *cur_fdt; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | spin_unlock(&files->file_lock); |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 304 | new_fdt = alloc_fdtable(nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | spin_lock(&files->file_lock); |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 306 | if (!new_fdt) |
| 307 | return -ENOMEM; |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 308 | /* |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 309 | * Check again since another task may have expanded the fd table while |
| 310 | * we dropped the lock |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 311 | */ |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 312 | cur_fdt = files_fdtable(files); |
| 313 | if (nr >= cur_fdt->max_fds || nr >= cur_fdt->max_fdset) { |
| 314 | /* Continue as planned */ |
| 315 | copy_fdtable(new_fdt, cur_fdt); |
| 316 | rcu_assign_pointer(files->fdt, new_fdt); |
| 317 | free_fdtable(cur_fdt); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 318 | } else { |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 319 | /* Somebody else expanded, so undo our attempt */ |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 320 | __free_fdtable(new_fdt); |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 321 | } |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 322 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Expand files. |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 327 | * This function will expand the file structures, if the requested size exceeds |
| 328 | * the current capacity and there is room for expansion. |
| 329 | * Return <0 error code on error; 0 when nothing done; 1 when files were |
| 330 | * expanded and execution may have blocked. |
| 331 | * The files->file_lock should be held on entry, and will be held on exit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | */ |
| 333 | int expand_files(struct files_struct *files, int nr) |
| 334 | { |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 335 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Dipankar Sarma | badf166 | 2005-09-09 13:04:10 -0700 | [diff] [blame] | 337 | fdt = files_fdtable(files); |
Vadim Lobanov | 74d392a | 2006-09-29 02:01:43 -0700 | [diff] [blame] | 338 | /* Do we need to expand? */ |
| 339 | if (nr < fdt->max_fdset && nr < fdt->max_fds) |
| 340 | return 0; |
| 341 | /* Can we expand? */ |
| 342 | if (fdt->max_fdset >= NR_OPEN || fdt->max_fds >= NR_OPEN || |
| 343 | nr >= NR_OPEN) |
| 344 | return -EMFILE; |
| 345 | |
| 346 | /* All good, so we try */ |
| 347 | return expand_fdtable(files, nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 349 | |
| 350 | static void __devinit fdtable_defer_list_init(int cpu) |
| 351 | { |
| 352 | struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu); |
| 353 | spin_lock_init(&fddef->lock); |
| 354 | INIT_WORK(&fddef->wq, (void (*)(void *))free_fdtable_work, fddef); |
| 355 | init_timer(&fddef->timer); |
| 356 | fddef->timer.data = (unsigned long)fddef; |
| 357 | fddef->timer.function = fdtable_timer; |
| 358 | fddef->next = NULL; |
| 359 | } |
| 360 | |
| 361 | void __init files_defer_init(void) |
| 362 | { |
| 363 | int i; |
KAMEZAWA Hiroyuki | 0a94502 | 2006-03-28 01:56:37 -0800 | [diff] [blame] | 364 | for_each_possible_cpu(i) |
Dipankar Sarma | ab2af1f | 2005-09-09 13:04:13 -0700 | [diff] [blame] | 365 | fdtable_defer_list_init(i); |
| 366 | } |