diff options
Diffstat (limited to 'fs/file.c')
| -rw-r--r-- | fs/file.c | 387 |
1 files changed, 261 insertions, 126 deletions
| @@ -13,6 +13,25 @@ | |||
| 13 | #include <linux/vmalloc.h> | 13 | #include <linux/vmalloc.h> |
| 14 | #include <linux/file.h> | 14 | #include <linux/file.h> |
| 15 | #include <linux/bitops.h> | 15 | #include <linux/bitops.h> |
| 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); | ||
| 16 | 35 | ||
| 17 | 36 | ||
| 18 | /* | 37 | /* |
| @@ -48,82 +67,143 @@ void free_fd_array(struct file **array, int num) | |||
| 48 | vfree(array); | 67 | vfree(array); |
| 49 | } | 68 | } |
| 50 | 69 | ||
| 51 | /* | 70 | static void __free_fdtable(struct fdtable *fdt) |
| 52 | * Expand the fd array in the files_struct. Called with the files | 71 | { |
| 53 | * spinlock held for write. | 72 | int fdset_size, fdarray_size; |
| 54 | */ | ||
| 55 | 73 | ||
| 56 | static int expand_fd_array(struct files_struct *files, int nr) | 74 | fdset_size = fdt->max_fdset / 8; |
| 57 | __releases(files->file_lock) | 75 | fdarray_size = fdt->max_fds * sizeof(struct file *); |
| 58 | __acquires(files->file_lock) | 76 | free_fdset(fdt->open_fds, fdset_size); |
| 77 | free_fdset(fdt->close_on_exec, fdset_size); | ||
| 78 | free_fd_array(fdt->fd, fdarray_size); | ||
| 79 | kfree(fdt); | ||
| 80 | } | ||
| 81 | |||
| 82 | static void fdtable_timer(unsigned long data) | ||
| 59 | { | 83 | { |
| 60 | struct file **new_fds; | 84 | struct fdtable_defer *fddef = (struct fdtable_defer *)data; |
| 61 | int error, nfds; | ||
| 62 | 85 | ||
| 63 | 86 | spin_lock(&fddef->lock); | |
| 64 | error = -EMFILE; | 87 | /* |
| 65 | if (files->max_fds >= NR_OPEN || nr >= NR_OPEN) | 88 | * If someone already emptied the queue return. |
| 89 | */ | ||
| 90 | if (!fddef->next) | ||
| 66 | goto out; | 91 | goto out; |
| 92 | if (!schedule_work(&fddef->wq)) | ||
| 93 | mod_timer(&fddef->timer, 5); | ||
| 94 | out: | ||
| 95 | spin_unlock(&fddef->lock); | ||
| 96 | } | ||
| 67 | 97 | ||
| 68 | nfds = files->max_fds; | 98 | static void free_fdtable_work(struct fdtable_defer *f) |
| 69 | spin_unlock(&files->file_lock); | 99 | { |
| 100 | struct fdtable *fdt; | ||
| 70 | 101 | ||
| 71 | /* | 102 | spin_lock_bh(&f->lock); |
| 72 | * Expand to the max in easy steps, and keep expanding it until | 103 | fdt = f->next; |
| 73 | * we have enough for the requested fd array size. | 104 | f->next = NULL; |
| 74 | */ | 105 | spin_unlock_bh(&f->lock); |
| 106 | while(fdt) { | ||
| 107 | struct fdtable *next = fdt->next; | ||
| 108 | __free_fdtable(fdt); | ||
| 109 | fdt = next; | ||
| 110 | } | ||
| 111 | } | ||
| 75 | 112 | ||
| 76 | do { | 113 | static void free_fdtable_rcu(struct rcu_head *rcu) |
| 77 | #if NR_OPEN_DEFAULT < 256 | 114 | { |
| 78 | if (nfds < 256) | 115 | struct fdtable *fdt = container_of(rcu, struct fdtable, rcu); |
| 79 | nfds = 256; | 116 | int fdset_size, fdarray_size; |
| 80 | else | 117 | struct fdtable_defer *fddef; |
| 81 | #endif | ||
| 82 | if (nfds < (PAGE_SIZE / sizeof(struct file *))) | ||
| 83 | nfds = PAGE_SIZE / sizeof(struct file *); | ||
| 84 | else { | ||
| 85 | nfds = nfds * 2; | ||
| 86 | if (nfds > NR_OPEN) | ||
| 87 | nfds = NR_OPEN; | ||
| 88 | } | ||
| 89 | } while (nfds <= nr); | ||
| 90 | 118 | ||
| 91 | error = -ENOMEM; | 119 | BUG_ON(!fdt); |
| 92 | new_fds = alloc_fd_array(nfds); | 120 | fdset_size = fdt->max_fdset / 8; |
| 93 | spin_lock(&files->file_lock); | 121 | fdarray_size = fdt->max_fds * sizeof(struct file *); |
| 94 | if (!new_fds) | ||
| 95 | goto out; | ||
| 96 | 122 | ||
| 97 | /* Copy the existing array and install the new pointer */ | 123 | if (fdt->free_files) { |
| 98 | 124 | /* | |
| 99 | if (nfds > files->max_fds) { | 125 | * The this fdtable was embedded in the files structure |
| 100 | struct file **old_fds; | 126 | * and the files structure itself was getting destroyed. |
| 101 | int i; | 127 | * It is now safe to free the files structure. |
| 102 | 128 | */ | |
| 103 | old_fds = xchg(&files->fd, new_fds); | 129 | kmem_cache_free(files_cachep, fdt->free_files); |
| 104 | i = xchg(&files->max_fds, nfds); | 130 | return; |
| 105 | 131 | } | |
| 106 | /* Don't copy/clear the array if we are creating a new | 132 | if (fdt->max_fdset <= __FD_SETSIZE && fdt->max_fds <= NR_OPEN_DEFAULT) { |
| 107 | fd array for fork() */ | 133 | /* |
| 108 | if (i) { | 134 | * The fdtable was embedded |
| 109 | memcpy(new_fds, old_fds, i * sizeof(struct file *)); | 135 | */ |
| 110 | /* clear the remainder of the array */ | 136 | return; |
| 111 | memset(&new_fds[i], 0, | 137 | } |
| 112 | (nfds-i) * sizeof(struct file *)); | 138 | if (fdset_size <= PAGE_SIZE && fdarray_size <= PAGE_SIZE) { |
| 113 | 139 | kfree(fdt->open_fds); | |
| 114 | spin_unlock(&files->file_lock); | 140 | kfree(fdt->close_on_exec); |
| 115 | free_fd_array(old_fds, i); | 141 | kfree(fdt->fd); |
| 116 | spin_lock(&files->file_lock); | 142 | kfree(fdt); |
| 117 | } | ||
| 118 | } else { | 143 | } else { |
| 119 | /* Somebody expanded the array while we slept ... */ | 144 | fddef = &get_cpu_var(fdtable_defer_list); |
| 120 | spin_unlock(&files->file_lock); | 145 | spin_lock(&fddef->lock); |
| 121 | free_fd_array(new_fds, nfds); | 146 | fdt->next = fddef->next; |
| 122 | spin_lock(&files->file_lock); | 147 | fddef->next = fdt; |
| 148 | /* | ||
| 149 | * vmallocs are handled from the workqueue context. | ||
| 150 | * If the per-cpu workqueue is running, then we | ||
| 151 | * defer work scheduling through a timer. | ||
| 152 | */ | ||
| 153 | if (!schedule_work(&fddef->wq)) | ||
| 154 | mod_timer(&fddef->timer, 5); | ||
| 155 | spin_unlock(&fddef->lock); | ||
| 156 | put_cpu_var(fdtable_defer_list); | ||
| 123 | } | 157 | } |
| 124 | error = 0; | 158 | } |
| 125 | out: | 159 | |
| 126 | return error; | 160 | void free_fdtable(struct fdtable *fdt) |
| 161 | { | ||
| 162 | if (fdt->free_files || fdt->max_fdset > __FD_SETSIZE || | ||
| 163 | fdt->max_fds > NR_OPEN_DEFAULT) | ||
| 164 | call_rcu(&fdt->rcu, free_fdtable_rcu); | ||
| 165 | } | ||
| 166 | |||
| 167 | /* | ||
| 168 | * Expand the fdset in the files_struct. Called with the files spinlock | ||
| 169 | * held for write. | ||
| 170 | */ | ||
| 171 | static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt) | ||
| 172 | { | ||
| 173 | int i; | ||
| 174 | int count; | ||
| 175 | |||
| 176 | BUG_ON(nfdt->max_fdset < fdt->max_fdset); | ||
| 177 | BUG_ON(nfdt->max_fds < fdt->max_fds); | ||
| 178 | /* Copy the existing tables and install the new pointers */ | ||
| 179 | |||
| 180 | i = fdt->max_fdset / (sizeof(unsigned long) * 8); | ||
| 181 | count = (nfdt->max_fdset - fdt->max_fdset) / 8; | ||
| 182 | |||
| 183 | /* | ||
| 184 | * Don't copy the entire array if the current fdset is | ||
| 185 | * not yet initialised. | ||
| 186 | */ | ||
| 187 | if (i) { | ||
| 188 | memcpy (nfdt->open_fds, fdt->open_fds, | ||
| 189 | fdt->max_fdset/8); | ||
| 190 | memcpy (nfdt->close_on_exec, fdt->close_on_exec, | ||
| 191 | fdt->max_fdset/8); | ||
| 192 | memset (&nfdt->open_fds->fds_bits[i], 0, count); | ||
| 193 | memset (&nfdt->close_on_exec->fds_bits[i], 0, count); | ||
| 194 | } | ||
| 195 | |||
| 196 | /* Don't copy/clear the array if we are creating a new | ||
| 197 | fd array for fork() */ | ||
| 198 | if (fdt->max_fds) { | ||
| 199 | memcpy(nfdt->fd, fdt->fd, | ||
| 200 | fdt->max_fds * sizeof(struct file *)); | ||
| 201 | /* clear the remainder of the array */ | ||
| 202 | memset(&nfdt->fd[fdt->max_fds], 0, | ||
| 203 | (nfdt->max_fds - fdt->max_fds) * | ||
| 204 | sizeof(struct file *)); | ||
| 205 | } | ||
| 206 | nfdt->next_fd = fdt->next_fd; | ||
| 127 | } | 207 | } |
| 128 | 208 | ||
| 129 | /* | 209 | /* |
| @@ -154,26 +234,21 @@ void free_fdset(fd_set *array, int num) | |||
| 154 | vfree(array); | 234 | vfree(array); |
| 155 | } | 235 | } |
| 156 | 236 | ||
| 157 | /* | 237 | static struct fdtable *alloc_fdtable(int nr) |
| 158 | * Expand the fdset in the files_struct. Called with the files spinlock | ||
| 159 | * held for write. | ||
| 160 | */ | ||
| 161 | static int expand_fdset(struct files_struct *files, int nr) | ||
| 162 | __releases(file->file_lock) | ||
| 163 | __acquires(file->file_lock) | ||
| 164 | { | 238 | { |
| 165 | fd_set *new_openset = NULL, *new_execset = NULL; | 239 | struct fdtable *fdt = NULL; |
| 166 | int error, nfds = 0; | 240 | int nfds = 0; |
| 167 | 241 | fd_set *new_openset = NULL, *new_execset = NULL; | |
| 168 | error = -EMFILE; | 242 | struct file **new_fds; |
| 169 | if (files->max_fdset >= NR_OPEN || nr >= NR_OPEN) | ||
| 170 | goto out; | ||
| 171 | 243 | ||
| 172 | nfds = files->max_fdset; | 244 | fdt = kmalloc(sizeof(*fdt), GFP_KERNEL); |
| 173 | spin_unlock(&files->file_lock); | 245 | if (!fdt) |
| 246 | goto out; | ||
| 247 | memset(fdt, 0, sizeof(*fdt)); | ||
| 174 | 248 | ||
| 175 | /* Expand to the max in easy steps */ | 249 | nfds = __FD_SETSIZE; |
| 176 | do { | 250 | /* Expand to the max in easy steps */ |
| 251 | do { | ||
| 177 | if (nfds < (PAGE_SIZE * 8)) | 252 | if (nfds < (PAGE_SIZE * 8)) |
| 178 | nfds = PAGE_SIZE * 8; | 253 | nfds = PAGE_SIZE * 8; |
| 179 | else { | 254 | else { |
| @@ -183,49 +258,88 @@ static int expand_fdset(struct files_struct *files, int nr) | |||
| 183 | } | 258 | } |
| 184 | } while (nfds <= nr); | 259 | } while (nfds <= nr); |
| 185 | 260 | ||
| 186 | error = -ENOMEM; | 261 | new_openset = alloc_fdset(nfds); |
| 187 | new_openset = alloc_fdset(nfds); | 262 | new_execset = alloc_fdset(nfds); |
| 188 | new_execset = alloc_fdset(nfds); | 263 | if (!new_openset || !new_execset) |
| 189 | spin_lock(&files->file_lock); | 264 | goto out; |
| 190 | if (!new_openset || !new_execset) | 265 | fdt->open_fds = new_openset; |
| 266 | fdt->close_on_exec = new_execset; | ||
| 267 | fdt->max_fdset = nfds; | ||
| 268 | |||
| 269 | nfds = NR_OPEN_DEFAULT; | ||
| 270 | /* | ||
| 271 | * Expand to the max in easy steps, and keep expanding it until | ||
| 272 | * we have enough for the requested fd array size. | ||
| 273 | */ | ||
| 274 | do { | ||
| 275 | #if NR_OPEN_DEFAULT < 256 | ||
| 276 | if (nfds < 256) | ||
| 277 | nfds = 256; | ||
| 278 | else | ||
| 279 | #endif | ||
| 280 | if (nfds < (PAGE_SIZE / sizeof(struct file *))) | ||
| 281 | nfds = PAGE_SIZE / sizeof(struct file *); | ||
| 282 | else { | ||
| 283 | nfds = nfds * 2; | ||
| 284 | if (nfds > NR_OPEN) | ||
| 285 | nfds = NR_OPEN; | ||
| 286 | } | ||
| 287 | } while (nfds <= nr); | ||
| 288 | new_fds = alloc_fd_array(nfds); | ||
| 289 | if (!new_fds) | ||
| 290 | goto out; | ||
| 291 | fdt->fd = new_fds; | ||
| 292 | fdt->max_fds = nfds; | ||
| 293 | fdt->free_files = NULL; | ||
| 294 | return fdt; | ||
| 295 | out: | ||
| 296 | if (new_openset) | ||
| 297 | free_fdset(new_openset, nfds); | ||
| 298 | if (new_execset) | ||
| 299 | free_fdset(new_execset, nfds); | ||
| 300 | kfree(fdt); | ||
| 301 | return NULL; | ||
| 302 | } | ||
| 303 | |||
| 304 | /* | ||
| 305 | * Expands the file descriptor table - it will allocate a new fdtable and | ||
| 306 | * both fd array and fdset. It is expected to be called with the | ||
| 307 | * files_lock held. | ||
| 308 | */ | ||
| 309 | static int expand_fdtable(struct files_struct *files, int nr) | ||
| 310 | __releases(files->file_lock) | ||
| 311 | __acquires(files->file_lock) | ||
| 312 | { | ||
| 313 | int error = 0; | ||
| 314 | struct fdtable *fdt; | ||
| 315 | struct fdtable *nfdt = NULL; | ||
| 316 | |||
| 317 | spin_unlock(&files->file_lock); | ||
| 318 | nfdt = alloc_fdtable(nr); | ||
| 319 | if (!nfdt) { | ||
| 320 | error = -ENOMEM; | ||
| 321 | spin_lock(&files->file_lock); | ||
| 191 | goto out; | 322 | goto out; |
| 323 | } | ||
| 192 | 324 | ||
| 193 | error = 0; | 325 | spin_lock(&files->file_lock); |
| 194 | 326 | fdt = files_fdtable(files); | |
| 195 | /* Copy the existing tables and install the new pointers */ | 327 | /* |
| 196 | if (nfds > files->max_fdset) { | 328 | * Check again since another task may have expanded the |
| 197 | int i = files->max_fdset / (sizeof(unsigned long) * 8); | 329 | * fd table while we dropped the lock |
| 198 | int count = (nfds - files->max_fdset) / 8; | 330 | */ |
| 199 | 331 | if (nr >= fdt->max_fds || nr >= fdt->max_fdset) { | |
| 200 | /* | 332 | copy_fdtable(nfdt, fdt); |
| 201 | * Don't copy the entire array if the current fdset is | 333 | } else { |
| 202 | * not yet initialised. | 334 | /* Somebody expanded while we dropped file_lock */ |
| 203 | */ | ||
| 204 | if (i) { | ||
| 205 | memcpy (new_openset, files->open_fds, files->max_fdset/8); | ||
| 206 | memcpy (new_execset, files->close_on_exec, files->max_fdset/8); | ||
| 207 | memset (&new_openset->fds_bits[i], 0, count); | ||
| 208 | memset (&new_execset->fds_bits[i], 0, count); | ||
| 209 | } | ||
| 210 | |||
| 211 | nfds = xchg(&files->max_fdset, nfds); | ||
| 212 | new_openset = xchg(&files->open_fds, new_openset); | ||
| 213 | new_execset = xchg(&files->close_on_exec, new_execset); | ||
| 214 | spin_unlock(&files->file_lock); | 335 | spin_unlock(&files->file_lock); |
| 215 | free_fdset (new_openset, nfds); | 336 | __free_fdtable(nfdt); |
| 216 | free_fdset (new_execset, nfds); | ||
| 217 | spin_lock(&files->file_lock); | 337 | spin_lock(&files->file_lock); |
| 218 | return 0; | 338 | goto out; |
| 219 | } | 339 | } |
| 220 | /* Somebody expanded the array while we slept ... */ | 340 | rcu_assign_pointer(files->fdt, nfdt); |
| 221 | 341 | free_fdtable(fdt); | |
| 222 | out: | 342 | out: |
| 223 | spin_unlock(&files->file_lock); | ||
| 224 | if (new_openset) | ||
| 225 | free_fdset(new_openset, nfds); | ||
| 226 | if (new_execset) | ||
| 227 | free_fdset(new_execset, nfds); | ||
| 228 | spin_lock(&files->file_lock); | ||
| 229 | return error; | 343 | return error; |
| 230 | } | 344 | } |
| 231 | 345 | ||
| @@ -237,18 +351,39 @@ out: | |||
| 237 | int expand_files(struct files_struct *files, int nr) | 351 | int expand_files(struct files_struct *files, int nr) |
| 238 | { | 352 | { |
| 239 | int err, expand = 0; | 353 | int err, expand = 0; |
| 354 | struct fdtable *fdt; | ||
| 240 | 355 | ||
| 241 | if (nr >= files->max_fdset) { | 356 | fdt = files_fdtable(files); |
| 242 | expand = 1; | 357 | if (nr >= fdt->max_fdset || nr >= fdt->max_fds) { |
| 243 | if ((err = expand_fdset(files, nr))) | 358 | if (fdt->max_fdset >= NR_OPEN || |
| 359 | fdt->max_fds >= NR_OPEN || nr >= NR_OPEN) { | ||
| 360 | err = -EMFILE; | ||
| 244 | goto out; | 361 | goto out; |
| 245 | } | 362 | } |
| 246 | if (nr >= files->max_fds) { | ||
| 247 | expand = 1; | 363 | expand = 1; |
| 248 | if ((err = expand_fd_array(files, nr))) | 364 | if ((err = expand_fdtable(files, nr))) |
| 249 | goto out; | 365 | goto out; |
| 250 | } | 366 | } |
| 251 | err = expand; | 367 | err = expand; |
| 252 | out: | 368 | out: |
| 253 | return err; | 369 | return err; |
| 254 | } | 370 | } |
| 371 | |||
| 372 | static void __devinit fdtable_defer_list_init(int cpu) | ||
| 373 | { | ||
| 374 | struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu); | ||
| 375 | spin_lock_init(&fddef->lock); | ||
| 376 | INIT_WORK(&fddef->wq, (void (*)(void *))free_fdtable_work, fddef); | ||
| 377 | init_timer(&fddef->timer); | ||
| 378 | fddef->timer.data = (unsigned long)fddef; | ||
| 379 | fddef->timer.function = fdtable_timer; | ||
| 380 | fddef->next = NULL; | ||
| 381 | } | ||
| 382 | |||
| 383 | void __init files_defer_init(void) | ||
| 384 | { | ||
| 385 | int i; | ||
| 386 | /* Really early - can't use for_each_cpu */ | ||
| 387 | for (i = 0; i < NR_CPUS; i++) | ||
| 388 | fdtable_defer_list_init(i); | ||
| 389 | } | ||
