aboutsummaryrefslogtreecommitdiffstats
path: root/fs/file.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/file.c')
-rw-r--r--fs/file.c255
1 files changed, 84 insertions, 171 deletions
diff --git a/fs/file.c b/fs/file.c
index 51aef675470f..857fa49e984c 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -32,46 +32,28 @@ struct fdtable_defer {
32 */ 32 */
33static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list); 33static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
34 34
35 35static inline void * alloc_fdmem(unsigned int size)
36/*
37 * Allocate an fd array, using kmalloc or vmalloc.
38 * Note: the array isn't cleared at allocation time.
39 */
40struct file ** alloc_fd_array(int num)
41{ 36{
42 struct file **new_fds;
43 int size = num * sizeof(struct file *);
44
45 if (size <= PAGE_SIZE) 37 if (size <= PAGE_SIZE)
46 new_fds = (struct file **) kmalloc(size, GFP_KERNEL); 38 return kmalloc(size, GFP_KERNEL);
47 else 39 else
48 new_fds = (struct file **) vmalloc(size); 40 return vmalloc(size);
49 return new_fds;
50} 41}
51 42
52void free_fd_array(struct file **array, int num) 43static inline void free_fdarr(struct fdtable *fdt)
53{ 44{
54 int size = num * sizeof(struct file *); 45 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *)))
55 46 kfree(fdt->fd);
56 if (!array) {
57 printk (KERN_ERR "free_fd_array: array = 0 (num = %d)\n", num);
58 return;
59 }
60
61 if (num <= NR_OPEN_DEFAULT) /* Don't free the embedded fd array! */
62 return;
63 else if (size <= PAGE_SIZE)
64 kfree(array);
65 else 47 else
66 vfree(array); 48 vfree(fdt->fd);
67} 49}
68 50
69static void __free_fdtable(struct fdtable *fdt) 51static inline void free_fdset(struct fdtable *fdt)
70{ 52{
71 free_fdset(fdt->open_fds, fdt->max_fdset); 53 if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2))
72 free_fdset(fdt->close_on_exec, fdt->max_fdset); 54 kfree(fdt->open_fds);
73 free_fd_array(fdt->fd, fdt->max_fds); 55 else
74 kfree(fdt); 56 vfree(fdt->open_fds);
75} 57}
76 58
77static void free_fdtable_work(struct work_struct *work) 59static void free_fdtable_work(struct work_struct *work)
@@ -86,41 +68,32 @@ static void free_fdtable_work(struct work_struct *work)
86 spin_unlock_bh(&f->lock); 68 spin_unlock_bh(&f->lock);
87 while(fdt) { 69 while(fdt) {
88 struct fdtable *next = fdt->next; 70 struct fdtable *next = fdt->next;
89 __free_fdtable(fdt); 71 vfree(fdt->fd);
72 free_fdset(fdt);
73 kfree(fdt);
90 fdt = next; 74 fdt = next;
91 } 75 }
92} 76}
93 77
94static void free_fdtable_rcu(struct rcu_head *rcu) 78void free_fdtable_rcu(struct rcu_head *rcu)
95{ 79{
96 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu); 80 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
97 int fdset_size, fdarray_size;
98 struct fdtable_defer *fddef; 81 struct fdtable_defer *fddef;
99 82
100 BUG_ON(!fdt); 83 BUG_ON(!fdt);
101 fdset_size = fdt->max_fdset / 8;
102 fdarray_size = fdt->max_fds * sizeof(struct file *);
103 84
104 if (fdt->free_files) { 85 if (fdt->max_fds <= NR_OPEN_DEFAULT) {
105 /* 86 /*
106 * The this fdtable was embedded in the files structure 87 * This fdtable is embedded in the files structure and that
107 * and the files structure itself was getting destroyed. 88 * structure itself is getting destroyed.
108 * It is now safe to free the files structure.
109 */ 89 */
110 kmem_cache_free(files_cachep, fdt->free_files); 90 kmem_cache_free(files_cachep,
91 container_of(fdt, struct files_struct, fdtab));
111 return; 92 return;
112 } 93 }
113 if (fdt->max_fdset <= EMBEDDED_FD_SET_SIZE && 94 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) {
114 fdt->max_fds <= NR_OPEN_DEFAULT) {
115 /*
116 * The fdtable was embedded
117 */
118 return;
119 }
120 if (fdset_size <= PAGE_SIZE && fdarray_size <= PAGE_SIZE) {
121 kfree(fdt->open_fds);
122 kfree(fdt->close_on_exec);
123 kfree(fdt->fd); 95 kfree(fdt->fd);
96 kfree(fdt->open_fds);
124 kfree(fdt); 97 kfree(fdt);
125 } else { 98 } else {
126 fddef = &get_cpu_var(fdtable_defer_list); 99 fddef = &get_cpu_var(fdtable_defer_list);
@@ -134,136 +107,74 @@ static void free_fdtable_rcu(struct rcu_head *rcu)
134 } 107 }
135} 108}
136 109
137void free_fdtable(struct fdtable *fdt)
138{
139 if (fdt->free_files ||
140 fdt->max_fdset > EMBEDDED_FD_SET_SIZE ||
141 fdt->max_fds > NR_OPEN_DEFAULT)
142 call_rcu(&fdt->rcu, free_fdtable_rcu);
143}
144
145/* 110/*
146 * 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
147 * held for write. 112 * held for write.
148 */ 113 */
149static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt) 114static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
150{ 115{
151 int i; 116 unsigned int cpy, set;
152 int count;
153
154 BUG_ON(nfdt->max_fdset < fdt->max_fdset);
155 BUG_ON(nfdt->max_fds < fdt->max_fds);
156 /* Copy the existing tables and install the new pointers */
157
158 i = fdt->max_fdset / (sizeof(unsigned long) * 8);
159 count = (nfdt->max_fdset - fdt->max_fdset) / 8;
160 117
161 /* 118 BUG_ON(nfdt->max_fds < ofdt->max_fds);
162 * Don't copy the entire array if the current fdset is 119 if (ofdt->max_fds == 0)
163 * not yet initialised. 120 return;
164 */
165 if (i) {
166 memcpy (nfdt->open_fds, fdt->open_fds,
167 fdt->max_fdset/8);
168 memcpy (nfdt->close_on_exec, fdt->close_on_exec,
169 fdt->max_fdset/8);
170 memset (&nfdt->open_fds->fds_bits[i], 0, count);
171 memset (&nfdt->close_on_exec->fds_bits[i], 0, count);
172 }
173
174 /* Don't copy/clear the array if we are creating a new
175 fd array for fork() */
176 if (fdt->max_fds) {
177 memcpy(nfdt->fd, fdt->fd,
178 fdt->max_fds * sizeof(struct file *));
179 /* clear the remainder of the array */
180 memset(&nfdt->fd[fdt->max_fds], 0,
181 (nfdt->max_fds - fdt->max_fds) *
182 sizeof(struct file *));
183 }
184}
185
186/*
187 * Allocate an fdset array, using kmalloc or vmalloc.
188 * Note: the array isn't cleared at allocation time.
189 */
190fd_set * alloc_fdset(int num)
191{
192 fd_set *new_fdset;
193 int size = num / 8;
194 121
195 if (size <= PAGE_SIZE) 122 cpy = ofdt->max_fds * sizeof(struct file *);
196 new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL); 123 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
197 else 124 memcpy(nfdt->fd, ofdt->fd, cpy);
198 new_fdset = (fd_set *) vmalloc(size); 125 memset((char *)(nfdt->fd) + cpy, 0, set);
199 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);
200} 133}
201 134
202void free_fdset(fd_set *array, int num) 135static struct fdtable * alloc_fdtable(unsigned int nr)
203{ 136{
204 if (num <= EMBEDDED_FD_SET_SIZE) /* Don't free an embedded fdset */ 137 struct fdtable *fdt;
205 return; 138 char *data;
206 else if (num <= 8 * PAGE_SIZE)
207 kfree(array);
208 else
209 vfree(array);
210}
211 139
212static struct fdtable *alloc_fdtable(int nr) 140 /*
213{ 141 * Figure out how many fds we actually want to support in this fdtable.
214 struct fdtable *fdt = NULL; 142 * Allocation steps are keyed to the size of the fdarray, since it
215 int nfds = 0; 143 * grows far faster than any of the other dynamic data. We try to fit
216 fd_set *new_openset = NULL, *new_execset = NULL; 144 * the fdarray into comfortable page-tuned chunks: starting at 1024B
217 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;
218 152
219 fdt = kzalloc(sizeof(*fdt), GFP_KERNEL); 153 fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
220 if (!fdt) 154 if (!fdt)
221 goto out; 155 goto out;
222 156 fdt->max_fds = nr;
223 nfds = max_t(int, 8 * L1_CACHE_BYTES, roundup_pow_of_two(nr + 1)); 157 data = alloc_fdmem(nr * sizeof(struct file *));
224 if (nfds > NR_OPEN) 158 if (!data)
225 nfds = NR_OPEN; 159 goto out_fdt;
160 fdt->fd = (struct file **)data;
161 data = alloc_fdmem(max_t(unsigned int,
162 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
163 if (!data)
164 goto out_arr;
165 fdt->open_fds = (fd_set *)data;
166 data += nr / BITS_PER_BYTE;
167 fdt->close_on_exec = (fd_set *)data;
168 INIT_RCU_HEAD(&fdt->rcu);
169 fdt->next = NULL;
226 170
227 new_openset = alloc_fdset(nfds);
228 new_execset = alloc_fdset(nfds);
229 if (!new_openset || !new_execset)
230 goto out;
231 fdt->open_fds = new_openset;
232 fdt->close_on_exec = new_execset;
233 fdt->max_fdset = nfds;
234
235 nfds = NR_OPEN_DEFAULT;
236 /*
237 * Expand to the max in easy steps, and keep expanding it until
238 * we have enough for the requested fd array size.
239 */
240 do {
241#if NR_OPEN_DEFAULT < 256
242 if (nfds < 256)
243 nfds = 256;
244 else
245#endif
246 if (nfds < (PAGE_SIZE / sizeof(struct file *)))
247 nfds = PAGE_SIZE / sizeof(struct file *);
248 else {
249 nfds = nfds * 2;
250 if (nfds > NR_OPEN)
251 nfds = NR_OPEN;
252 }
253 } while (nfds <= nr);
254 new_fds = alloc_fd_array(nfds);
255 if (!new_fds)
256 goto out2;
257 fdt->fd = new_fds;
258 fdt->max_fds = nfds;
259 fdt->free_files = NULL;
260 return fdt; 171 return fdt;
261out2: 172
262 nfds = fdt->max_fdset; 173out_arr:
263out: 174 free_fdarr(fdt);
264 free_fdset(new_openset, nfds); 175out_fdt:
265 free_fdset(new_execset, nfds);
266 kfree(fdt); 176 kfree(fdt);
177out:
267 return NULL; 178 return NULL;
268} 179}
269 180
@@ -290,14 +201,17 @@ static int expand_fdtable(struct files_struct *files, int nr)
290 * we dropped the lock 201 * we dropped the lock
291 */ 202 */
292 cur_fdt = files_fdtable(files); 203 cur_fdt = files_fdtable(files);
293 if (nr >= cur_fdt->max_fds || nr >= cur_fdt->max_fdset) { 204 if (nr >= cur_fdt->max_fds) {
294 /* Continue as planned */ 205 /* Continue as planned */
295 copy_fdtable(new_fdt, cur_fdt); 206 copy_fdtable(new_fdt, cur_fdt);
296 rcu_assign_pointer(files->fdt, new_fdt); 207 rcu_assign_pointer(files->fdt, new_fdt);
297 free_fdtable(cur_fdt); 208 if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
209 call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
298 } else { 210 } else {
299 /* Somebody else expanded, so undo our attempt */ 211 /* Somebody else expanded, so undo our attempt */
300 __free_fdtable(new_fdt); 212 free_fdarr(new_fdt);
213 free_fdset(new_fdt);
214 kfree(new_fdt);
301 } 215 }
302 return 1; 216 return 1;
303} 217}
@@ -316,11 +230,10 @@ int expand_files(struct files_struct *files, int nr)
316 230
317 fdt = files_fdtable(files); 231 fdt = files_fdtable(files);
318 /* Do we need to expand? */ 232 /* Do we need to expand? */
319 if (nr < fdt->max_fdset && nr < fdt->max_fds) 233 if (nr < fdt->max_fds)
320 return 0; 234 return 0;
321 /* Can we expand? */ 235 /* Can we expand? */
322 if (fdt->max_fdset >= NR_OPEN || fdt->max_fds >= NR_OPEN || 236 if (nr >= NR_OPEN)
323 nr >= NR_OPEN)
324 return -EMFILE; 237 return -EMFILE;
325 238
326 /* All good, so we try */ 239 /* All good, so we try */