aboutsummaryrefslogtreecommitdiffstats
path: root/fs/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/file.c')
-rw-r--r--fs/file.c290
1 files changed, 90 insertions, 200 deletions
diff --git a/fs/file.c b/fs/file.c
index 8e81775c5dc8..c5575de01113 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -21,7 +21,6 @@
21struct fdtable_defer { 21struct fdtable_defer {
22 spinlock_t lock; 22 spinlock_t lock;
23 struct work_struct wq; 23 struct work_struct wq;
24 struct timer_list timer;
25 struct fdtable *next; 24 struct fdtable *next;
26}; 25};
27 26
@@ -33,66 +32,34 @@ struct fdtable_defer {
33 */ 32 */
34static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list); 33static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
35 34
36 35static inline void * alloc_fdmem(unsigned int size)
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{ 36{
43 struct file **new_fds;
44 int size = num * sizeof(struct file *);
45
46 if (size <= PAGE_SIZE) 37 if (size <= PAGE_SIZE)
47 new_fds = (struct file **) kmalloc(size, GFP_KERNEL); 38 return 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 39 else
67 vfree(array); 40 return vmalloc(size);
68} 41}
69 42
70static void __free_fdtable(struct fdtable *fdt) 43static inline void free_fdarr(struct fdtable *fdt)
71{ 44{
72 free_fdset(fdt->open_fds, fdt->max_fdset); 45 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *)))
73 free_fdset(fdt->close_on_exec, fdt->max_fdset); 46 kfree(fdt->fd);
74 free_fd_array(fdt->fd, fdt->max_fds); 47 else
75 kfree(fdt); 48 vfree(fdt->fd);
76} 49}
77 50
78static void fdtable_timer(unsigned long data) 51static inline void free_fdset(struct fdtable *fdt)
79{ 52{
80 struct fdtable_defer *fddef = (struct fdtable_defer *)data; 53 if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2))
81 54 kfree(fdt->open_fds);
82 spin_lock(&fddef->lock); 55 else
83 /* 56 vfree(fdt->open_fds);
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);
90out:
91 spin_unlock(&fddef->lock);
92} 57}
93 58
94static void free_fdtable_work(struct fdtable_defer *f) 59static void free_fdtable_work(struct work_struct *work)
95{ 60{
61 struct fdtable_defer *f =
62 container_of(work, struct fdtable_defer, wq);
96 struct fdtable *fdt; 63 struct fdtable *fdt;
97 64
98 spin_lock_bh(&f->lock); 65 spin_lock_bh(&f->lock);
@@ -101,189 +68,113 @@ static void free_fdtable_work(struct fdtable_defer *f)
101 spin_unlock_bh(&f->lock); 68 spin_unlock_bh(&f->lock);
102 while(fdt) { 69 while(fdt) {
103 struct fdtable *next = fdt->next; 70 struct fdtable *next = fdt->next;
104 __free_fdtable(fdt); 71 vfree(fdt->fd);
72 free_fdset(fdt);
73 kfree(fdt);
105 fdt = next; 74 fdt = next;
106 } 75 }
107} 76}
108 77
109static void free_fdtable_rcu(struct rcu_head *rcu) 78void free_fdtable_rcu(struct rcu_head *rcu)
110{ 79{
111 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu); 80 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
112 int fdset_size, fdarray_size;
113 struct fdtable_defer *fddef; 81 struct fdtable_defer *fddef;
114 82
115 BUG_ON(!fdt); 83 BUG_ON(!fdt);
116 fdset_size = fdt->max_fdset / 8;
117 fdarray_size = fdt->max_fds * sizeof(struct file *);
118 84
119 if (fdt->free_files) { 85 if (fdt->max_fds <= NR_OPEN_DEFAULT) {
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 }
128 if (fdt->max_fdset <= EMBEDDED_FD_SET_SIZE &&
129 fdt->max_fds <= NR_OPEN_DEFAULT) {
130 /* 86 /*
131 * The fdtable was embedded 87 * This fdtable is embedded in the files structure and that
88 * structure itself is getting destroyed.
132 */ 89 */
90 kmem_cache_free(files_cachep,
91 container_of(fdt, struct files_struct, fdtab));
133 return; 92 return;
134 } 93 }
135 if (fdset_size <= PAGE_SIZE && fdarray_size <= PAGE_SIZE) { 94 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) {
136 kfree(fdt->open_fds);
137 kfree(fdt->close_on_exec);
138 kfree(fdt->fd); 95 kfree(fdt->fd);
96 kfree(fdt->open_fds);
139 kfree(fdt); 97 kfree(fdt);
140 } else { 98 } else {
141 fddef = &get_cpu_var(fdtable_defer_list); 99 fddef = &get_cpu_var(fdtable_defer_list);
142 spin_lock(&fddef->lock); 100 spin_lock(&fddef->lock);
143 fdt->next = fddef->next; 101 fdt->next = fddef->next;
144 fddef->next = fdt; 102 fddef->next = fdt;
145 /* 103 /* vmallocs are handled from the workqueue context */
146 * vmallocs are handled from the workqueue context. 104 schedule_work(&fddef->wq);
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); 105 spin_unlock(&fddef->lock);
153 put_cpu_var(fdtable_defer_list); 106 put_cpu_var(fdtable_defer_list);
154 } 107 }
155} 108}
156 109
157void free_fdtable(struct fdtable *fdt)
158{
159 if (fdt->free_files ||
160 fdt->max_fdset > EMBEDDED_FD_SET_SIZE ||
161 fdt->max_fds > NR_OPEN_DEFAULT)
162 call_rcu(&fdt->rcu, free_fdtable_rcu);
163}
164
165/* 110/*
166 * Expand the fdset in the files_struct. Called with the files spinlock 111 * Expand the fdset in the files_struct. Called with the files spinlock
167 * held for write. 112 * held for write.
168 */ 113 */
169static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt) 114static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
170{ 115{
171 int i; 116 unsigned int cpy, set;
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 117
194 /* Don't copy/clear the array if we are creating a new 118 BUG_ON(nfdt->max_fds < ofdt->max_fds);
195 fd array for fork() */ 119 if (ofdt->max_fds == 0)
196 if (fdt->max_fds) { 120 return;
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 }
204}
205
206/*
207 * Allocate an fdset array, using kmalloc or vmalloc.
208 * Note: the array isn't cleared at allocation time.
209 */
210fd_set * alloc_fdset(int num)
211{
212 fd_set *new_fdset;
213 int size = num / 8;
214 121
215 if (size <= PAGE_SIZE) 122 cpy = ofdt->max_fds * sizeof(struct file *);
216 new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL); 123 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
217 else 124 memcpy(nfdt->fd, ofdt->fd, cpy);
218 new_fdset = (fd_set *) vmalloc(size); 125 memset((char *)(nfdt->fd) + cpy, 0, set);
219 return new_fdset; 126
127 cpy = ofdt->max_fds / BITS_PER_BYTE;
128 set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
129 memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
130 memset((char *)(nfdt->open_fds) + cpy, 0, set);
131 memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
132 memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
220} 133}
221 134
222void free_fdset(fd_set *array, int num) 135static struct fdtable * alloc_fdtable(unsigned int nr)
223{ 136{
224 if (num <= EMBEDDED_FD_SET_SIZE) /* Don't free an embedded fdset */ 137 struct fdtable *fdt;
225 return; 138 char *data;
226 else if (num <= 8 * PAGE_SIZE)
227 kfree(array);
228 else
229 vfree(array);
230}
231 139
232static struct fdtable *alloc_fdtable(int nr) 140 /*
233{ 141 * Figure out how many fds we actually want to support in this fdtable.
234 struct fdtable *fdt = NULL; 142 * Allocation steps are keyed to the size of the fdarray, since it
235 int nfds = 0; 143 * grows far faster than any of the other dynamic data. We try to fit
236 fd_set *new_openset = NULL, *new_execset = NULL; 144 * the fdarray into comfortable page-tuned chunks: starting at 1024B
237 struct file **new_fds; 145 * and growing in powers of two from there on.
146 */
147 nr /= (1024 / sizeof(struct file *));
148 nr = roundup_pow_of_two(nr + 1);
149 nr *= (1024 / sizeof(struct file *));
150 if (nr > NR_OPEN)
151 nr = NR_OPEN;
238 152
239 fdt = kzalloc(sizeof(*fdt), GFP_KERNEL); 153 fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
240 if (!fdt) 154 if (!fdt)
241 goto out; 155 goto out;
242 156 fdt->max_fds = nr;
243 nfds = max_t(int, 8 * L1_CACHE_BYTES, roundup_pow_of_two(nr + 1)); 157 data = alloc_fdmem(nr * sizeof(struct file *));
244 if (nfds > NR_OPEN) 158 if (!data)
245 nfds = NR_OPEN; 159 goto out_fdt;
246 160 fdt->fd = (struct file **)data;
247 new_openset = alloc_fdset(nfds); 161 data = alloc_fdmem(max_t(unsigned int,
248 new_execset = alloc_fdset(nfds); 162 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
249 if (!new_openset || !new_execset) 163 if (!data)
250 goto out; 164 goto out_arr;
251 fdt->open_fds = new_openset; 165 fdt->open_fds = (fd_set *)data;
252 fdt->close_on_exec = new_execset; 166 data += nr / BITS_PER_BYTE;
253 fdt->max_fdset = nfds; 167 fdt->close_on_exec = (fd_set *)data;
168 INIT_RCU_HEAD(&fdt->rcu);
169 fdt->next = NULL;
254 170
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)
276 goto out2;
277 fdt->fd = new_fds;
278 fdt->max_fds = nfds;
279 fdt->free_files = NULL;
280 return fdt; 171 return fdt;
281out2: 172
282 nfds = fdt->max_fdset; 173out_arr:
283out: 174 free_fdarr(fdt);
284 free_fdset(new_openset, nfds); 175out_fdt:
285 free_fdset(new_execset, nfds);
286 kfree(fdt); 176 kfree(fdt);
177out:
287 return NULL; 178 return NULL;
288} 179}
289 180
@@ -310,14 +201,17 @@ static int expand_fdtable(struct files_struct *files, int nr)
310 * we dropped the lock 201 * we dropped the lock
311 */ 202 */
312 cur_fdt = files_fdtable(files); 203 cur_fdt = files_fdtable(files);
313 if (nr >= cur_fdt->max_fds || nr >= cur_fdt->max_fdset) { 204 if (nr >= cur_fdt->max_fds) {
314 /* Continue as planned */ 205 /* Continue as planned */
315 copy_fdtable(new_fdt, cur_fdt); 206 copy_fdtable(new_fdt, cur_fdt);
316 rcu_assign_pointer(files->fdt, new_fdt); 207 rcu_assign_pointer(files->fdt, new_fdt);
317 free_fdtable(cur_fdt); 208 if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
209 free_fdtable(cur_fdt);
318 } else { 210 } else {
319 /* Somebody else expanded, so undo our attempt */ 211 /* Somebody else expanded, so undo our attempt */
320 __free_fdtable(new_fdt); 212 free_fdarr(new_fdt);
213 free_fdset(new_fdt);
214 kfree(new_fdt);
321 } 215 }
322 return 1; 216 return 1;
323} 217}
@@ -336,11 +230,10 @@ int expand_files(struct files_struct *files, int nr)
336 230
337 fdt = files_fdtable(files); 231 fdt = files_fdtable(files);
338 /* Do we need to expand? */ 232 /* Do we need to expand? */
339 if (nr < fdt->max_fdset && nr < fdt->max_fds) 233 if (nr < fdt->max_fds)
340 return 0; 234 return 0;
341 /* Can we expand? */ 235 /* Can we expand? */
342 if (fdt->max_fdset >= NR_OPEN || fdt->max_fds >= NR_OPEN || 236 if (nr >= NR_OPEN)
343 nr >= NR_OPEN)
344 return -EMFILE; 237 return -EMFILE;
345 238
346 /* All good, so we try */ 239 /* All good, so we try */
@@ -351,10 +244,7 @@ static void __devinit fdtable_defer_list_init(int cpu)
351{ 244{
352 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu); 245 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
353 spin_lock_init(&fddef->lock); 246 spin_lock_init(&fddef->lock);
354 INIT_WORK(&fddef->wq, (void (*)(void *))free_fdtable_work, fddef); 247 INIT_WORK(&fddef->wq, free_fdtable_work);
355 init_timer(&fddef->timer);
356 fddef->timer.data = (unsigned long)fddef;
357 fddef->timer.function = fdtable_timer;
358 fddef->next = NULL; 248 fddef->next = NULL;
359} 249}
360 250