[PATCH] SysRq-X: show blocked tasks
[linux-block.git] / fs / file.c
CommitLineData
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>
15#include <linux/bitops.h>
ab2af1f5
DS
16#include <linux/interrupt.h>
17#include <linux/spinlock.h>
18#include <linux/rcupdate.h>
19#include <linux/workqueue.h>
20
21struct 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 */
34static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
1da177e4
LT
35
36
37/*
38 * Allocate an fd array, using kmalloc or vmalloc.
39 * Note: the array isn't cleared at allocation time.
40 */
41struct 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
53void 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
ab2af1f5 70static void __free_fdtable(struct fdtable *fdt)
1da177e4 71{
0b175a7e
DS
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);
ab2af1f5
DS
75 kfree(fdt);
76}
1da177e4 77
ab2af1f5
DS
78static void fdtable_timer(unsigned long data)
79{
80 struct fdtable_defer *fddef = (struct fdtable_defer *)data;
1da177e4 81
ab2af1f5
DS
82 spin_lock(&fddef->lock);
83 /*
84 * If someone already emptied the queue return.
1da177e4 85 */
ab2af1f5
DS
86 if (!fddef->next)
87 goto out;
88 if (!schedule_work(&fddef->wq))
89 mod_timer(&fddef->timer, 5);
90out:
91 spin_unlock(&fddef->lock);
92}
1da177e4 93
65f27f38 94static void free_fdtable_work(struct work_struct *work)
ab2af1f5 95{
65f27f38
DH
96 struct fdtable_defer *f =
97 container_of(work, struct fdtable_defer, wq);
ab2af1f5 98 struct fdtable *fdt;
1da177e4 99
ab2af1f5
DS
100 spin_lock_bh(&f->lock);
101 fdt = f->next;
102 f->next = NULL;
103 spin_unlock_bh(&f->lock);
104 while(fdt) {
105 struct fdtable *next = fdt->next;
106 __free_fdtable(fdt);
107 fdt = next;
108 }
109}
1da177e4 110
ab2af1f5
DS
111static void free_fdtable_rcu(struct rcu_head *rcu)
112{
113 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
114 int fdset_size, fdarray_size;
115 struct fdtable_defer *fddef;
1da177e4 116
ab2af1f5
DS
117 BUG_ON(!fdt);
118 fdset_size = fdt->max_fdset / 8;
119 fdarray_size = fdt->max_fds * sizeof(struct file *);
120
121 if (fdt->free_files) {
122 /*
123 * The this fdtable was embedded in the files structure
124 * and the files structure itself was getting destroyed.
125 * It is now safe to free the files structure.
126 */
127 kmem_cache_free(files_cachep, fdt->free_files);
128 return;
129 }
0c9e63fd
ED
130 if (fdt->max_fdset <= EMBEDDED_FD_SET_SIZE &&
131 fdt->max_fds <= NR_OPEN_DEFAULT) {
ab2af1f5
DS
132 /*
133 * The fdtable was embedded
134 */
135 return;
136 }
137 if (fdset_size <= PAGE_SIZE && fdarray_size <= PAGE_SIZE) {
138 kfree(fdt->open_fds);
139 kfree(fdt->close_on_exec);
140 kfree(fdt->fd);
141 kfree(fdt);
1da177e4 142 } else {
ab2af1f5
DS
143 fddef = &get_cpu_var(fdtable_defer_list);
144 spin_lock(&fddef->lock);
145 fdt->next = fddef->next;
146 fddef->next = fdt;
147 /*
148 * vmallocs are handled from the workqueue context.
149 * If the per-cpu workqueue is running, then we
150 * defer work scheduling through a timer.
151 */
152 if (!schedule_work(&fddef->wq))
153 mod_timer(&fddef->timer, 5);
154 spin_unlock(&fddef->lock);
155 put_cpu_var(fdtable_defer_list);
1da177e4 156 }
ab2af1f5
DS
157}
158
159void free_fdtable(struct fdtable *fdt)
160{
0c9e63fd
ED
161 if (fdt->free_files ||
162 fdt->max_fdset > EMBEDDED_FD_SET_SIZE ||
163 fdt->max_fds > NR_OPEN_DEFAULT)
ab2af1f5
DS
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 */
171static 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 }
1da177e4
LT
206}
207
208/*
209 * Allocate an fdset array, using kmalloc or vmalloc.
210 * Note: the array isn't cleared at allocation time.
211 */
212fd_set * alloc_fdset(int num)
213{
214 fd_set *new_fdset;
215 int size = num / 8;
216
217 if (size <= PAGE_SIZE)
218 new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
219 else
220 new_fdset = (fd_set *) vmalloc(size);
221 return new_fdset;
222}
223
224void free_fdset(fd_set *array, int num)
225{
0c9e63fd 226 if (num <= EMBEDDED_FD_SET_SIZE) /* Don't free an embedded fdset */
1da177e4 227 return;
0c9e63fd 228 else if (num <= 8 * PAGE_SIZE)
1da177e4
LT
229 kfree(array);
230 else
231 vfree(array);
232}
233
ab2af1f5 234static struct fdtable *alloc_fdtable(int nr)
1da177e4 235{
ab2af1f5
DS
236 struct fdtable *fdt = NULL;
237 int nfds = 0;
238 fd_set *new_openset = NULL, *new_execset = NULL;
239 struct file **new_fds;
1da177e4 240
0c9e63fd 241 fdt = kzalloc(sizeof(*fdt), GFP_KERNEL);
ab2af1f5
DS
242 if (!fdt)
243 goto out;
1da177e4 244
a29b0b74 245 nfds = max_t(int, 8 * L1_CACHE_BYTES, roundup_pow_of_two(nr + 1));
92eb7a2f
AM
246 if (nfds > NR_OPEN)
247 nfds = NR_OPEN;
1da177e4 248
ab2af1f5
DS
249 new_openset = alloc_fdset(nfds);
250 new_execset = alloc_fdset(nfds);
251 if (!new_openset || !new_execset)
252 goto out;
253 fdt->open_fds = new_openset;
254 fdt->close_on_exec = new_execset;
255 fdt->max_fdset = nfds;
256
257 nfds = NR_OPEN_DEFAULT;
258 /*
259 * Expand to the max in easy steps, and keep expanding it until
260 * we have enough for the requested fd array size.
261 */
262 do {
263#if NR_OPEN_DEFAULT < 256
264 if (nfds < 256)
265 nfds = 256;
266 else
267#endif
268 if (nfds < (PAGE_SIZE / sizeof(struct file *)))
269 nfds = PAGE_SIZE / sizeof(struct file *);
270 else {
271 nfds = nfds * 2;
272 if (nfds > NR_OPEN)
273 nfds = NR_OPEN;
274 }
275 } while (nfds <= nr);
276 new_fds = alloc_fd_array(nfds);
277 if (!new_fds)
d579091b 278 goto out2;
ab2af1f5
DS
279 fdt->fd = new_fds;
280 fdt->max_fds = nfds;
281 fdt->free_files = NULL;
282 return fdt;
d579091b
KK
283out2:
284 nfds = fdt->max_fdset;
ab2af1f5 285out:
8b0e330b
AM
286 free_fdset(new_openset, nfds);
287 free_fdset(new_execset, nfds);
ab2af1f5
DS
288 kfree(fdt);
289 return NULL;
290}
1da177e4 291
ab2af1f5 292/*
74d392aa
VL
293 * Expand the file descriptor table.
294 * This function will allocate a new fdtable and both fd array and fdset, of
295 * the given size.
296 * Return <0 error code on error; 1 on successful completion.
297 * The files->file_lock should be held on entry, and will be held on exit.
ab2af1f5
DS
298 */
299static int expand_fdtable(struct files_struct *files, int nr)
300 __releases(files->file_lock)
301 __acquires(files->file_lock)
302{
74d392aa 303 struct fdtable *new_fdt, *cur_fdt;
ab2af1f5
DS
304
305 spin_unlock(&files->file_lock);
74d392aa 306 new_fdt = alloc_fdtable(nr);
ab2af1f5 307 spin_lock(&files->file_lock);
74d392aa
VL
308 if (!new_fdt)
309 return -ENOMEM;
ab2af1f5 310 /*
74d392aa
VL
311 * Check again since another task may have expanded the fd table while
312 * we dropped the lock
ab2af1f5 313 */
74d392aa
VL
314 cur_fdt = files_fdtable(files);
315 if (nr >= cur_fdt->max_fds || nr >= cur_fdt->max_fdset) {
316 /* Continue as planned */
317 copy_fdtable(new_fdt, cur_fdt);
318 rcu_assign_pointer(files->fdt, new_fdt);
319 free_fdtable(cur_fdt);
ab2af1f5 320 } else {
74d392aa 321 /* Somebody else expanded, so undo our attempt */
74d392aa 322 __free_fdtable(new_fdt);
ab2af1f5 323 }
74d392aa 324 return 1;
1da177e4
LT
325}
326
327/*
328 * Expand files.
74d392aa
VL
329 * This function will expand the file structures, if the requested size exceeds
330 * the current capacity and there is room for expansion.
331 * Return <0 error code on error; 0 when nothing done; 1 when files were
332 * expanded and execution may have blocked.
333 * The files->file_lock should be held on entry, and will be held on exit.
1da177e4
LT
334 */
335int expand_files(struct files_struct *files, int nr)
336{
badf1662 337 struct fdtable *fdt;
1da177e4 338
badf1662 339 fdt = files_fdtable(files);
74d392aa
VL
340 /* Do we need to expand? */
341 if (nr < fdt->max_fdset && nr < fdt->max_fds)
342 return 0;
343 /* Can we expand? */
344 if (fdt->max_fdset >= NR_OPEN || fdt->max_fds >= NR_OPEN ||
345 nr >= NR_OPEN)
346 return -EMFILE;
347
348 /* All good, so we try */
349 return expand_fdtable(files, nr);
1da177e4 350}
ab2af1f5
DS
351
352static void __devinit fdtable_defer_list_init(int cpu)
353{
354 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
355 spin_lock_init(&fddef->lock);
65f27f38 356 INIT_WORK(&fddef->wq, free_fdtable_work);
ab2af1f5
DS
357 init_timer(&fddef->timer);
358 fddef->timer.data = (unsigned long)fddef;
359 fddef->timer.function = fdtable_timer;
360 fddef->next = NULL;
361}
362
363void __init files_defer_init(void)
364{
365 int i;
0a945022 366 for_each_possible_cpu(i)
ab2af1f5
DS
367 fdtable_defer_list_init(i);
368}