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>
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 fdtable *next;
25 };
26
27 int sysctl_nr_open __read_mostly = 1024*1024;
28
29 /*
30 * We use this list to defer free fdtables that have vmalloced
31 * sets/arrays. By keeping a per-cpu list, we avoid having to embed
32 * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
33 * this per-task structure.
34 */
35 static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
36
37 static inline void * alloc_fdmem(unsigned int size)
38 {
39 if (size <= PAGE_SIZE)
40 return kmalloc(size, GFP_KERNEL);
41 else
42 return vmalloc(size);
43 }
44
45 static inline void free_fdarr(struct fdtable *fdt)
46 {
47 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *)))
48 kfree(fdt->fd);
49 else
50 vfree(fdt->fd);
51 }
52
53 static inline void free_fdset(struct fdtable *fdt)
54 {
55 if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2))
56 kfree(fdt->open_fds);
57 else
58 vfree(fdt->open_fds);
59 }
60
61 static void free_fdtable_work(struct work_struct *work)
62 {
63 struct fdtable_defer *f =
64 container_of(work, struct fdtable_defer, wq);
65 struct fdtable *fdt;
66
67 spin_lock_bh(&f->lock);
68 fdt = f->next;
69 f->next = NULL;
70 spin_unlock_bh(&f->lock);
71 while(fdt) {
72 struct fdtable *next = fdt->next;
73 vfree(fdt->fd);
74 free_fdset(fdt);
75 kfree(fdt);
76 fdt = next;
77 }
78 }
79
80 void free_fdtable_rcu(struct rcu_head *rcu)
81 {
82 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
83 struct fdtable_defer *fddef;
84
85 BUG_ON(!fdt);
86
87 if (fdt->max_fds <= NR_OPEN_DEFAULT) {
88 /*
89 * This fdtable is embedded in the files structure and that
90 * structure itself is getting destroyed.
91 */
92 kmem_cache_free(files_cachep,
93 container_of(fdt, struct files_struct, fdtab));
94 return;
95 }
96 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) {
97 kfree(fdt->fd);
98 kfree(fdt->open_fds);
99 kfree(fdt);
100 } else {
101
102 fddef = &per_cpu(fdtable_defer_list, raw_smp_processor_id());
103
104 spin_lock(&fddef->lock);
105 fdt->next = fddef->next;
106 fddef->next = fdt;
107 /* vmallocs are handled from the workqueue context */
108 schedule_work(&fddef->wq);
109 spin_unlock(&fddef->lock);
110 }
111 }
112
113 /*
114 * Expand the fdset in the files_struct. Called with the files spinlock
115 * held for write.
116 */
117 static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
118 {
119 unsigned int cpy, set;
120
121 BUG_ON(nfdt->max_fds < ofdt->max_fds);
122 if (ofdt->max_fds == 0)
123 return;
124
125 cpy = ofdt->max_fds * sizeof(struct file *);
126 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
127 memcpy(nfdt->fd, ofdt->fd, cpy);
128 memset((char *)(nfdt->fd) + cpy, 0, set);
129
130 cpy = ofdt->max_fds / BITS_PER_BYTE;
131 set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
132 memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
133 memset((char *)(nfdt->open_fds) + cpy, 0, set);
134 memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
135 memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
136 }
137
138 static struct fdtable * alloc_fdtable(unsigned int nr)
139 {
140 struct fdtable *fdt;
141 char *data;
142
143 /*
144 * Figure out how many fds we actually want to support in this fdtable.
145 * Allocation steps are keyed to the size of the fdarray, since it
146 * grows far faster than any of the other dynamic data. We try to fit
147 * the fdarray into comfortable page-tuned chunks: starting at 1024B
148 * and growing in powers of two from there on.
149 */
150 nr /= (1024 / sizeof(struct file *));
151 nr = roundup_pow_of_two(nr + 1);
152 nr *= (1024 / sizeof(struct file *));
153 if (nr > sysctl_nr_open)
154 nr = sysctl_nr_open;
155
156 fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
157 if (!fdt)
158 goto out;
159 fdt->max_fds = nr;
160 data = alloc_fdmem(nr * sizeof(struct file *));
161 if (!data)
162 goto out_fdt;
163 fdt->fd = (struct file **)data;
164 data = alloc_fdmem(max_t(unsigned int,
165 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
166 if (!data)
167 goto out_arr;
168 fdt->open_fds = (fd_set *)data;
169 data += nr / BITS_PER_BYTE;
170 fdt->close_on_exec = (fd_set *)data;
171 INIT_RCU_HEAD(&fdt->rcu);
172 fdt->next = NULL;
173
174 return fdt;
175
176 out_arr:
177 free_fdarr(fdt);
178 out_fdt:
179 kfree(fdt);
180 out:
181 return NULL;
182 }
183
184 /*
185 * Expand the file descriptor table.
186 * This function will allocate a new fdtable and both fd array and fdset, of
187 * the given size.
188 * Return <0 error code on error; 1 on successful completion.
189 * The files->file_lock should be held on entry, and will be held on exit.
190 */
191 static int expand_fdtable(struct files_struct *files, int nr)
192 __releases(files->file_lock)
193 __acquires(files->file_lock)
194 {
195 struct fdtable *new_fdt, *cur_fdt;
196
197 spin_unlock(&files->file_lock);
198 new_fdt = alloc_fdtable(nr);
199 spin_lock(&files->file_lock);
200 if (!new_fdt)
201 return -ENOMEM;
202 /*
203 * Check again since another task may have expanded the fd table while
204 * we dropped the lock
205 */
206 cur_fdt = files_fdtable(files);
207 if (nr >= cur_fdt->max_fds) {
208 /* Continue as planned */
209 copy_fdtable(new_fdt, cur_fdt);
210 rcu_assign_pointer(files->fdt, new_fdt);
211 if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
212 free_fdtable(cur_fdt);
213 } else {
214 /* Somebody else expanded, so undo our attempt */
215 free_fdarr(new_fdt);
216 free_fdset(new_fdt);
217 kfree(new_fdt);
218 }
219 return 1;
220 }
221
222 /*
223 * Expand files.
224 * This function will expand the file structures, if the requested size exceeds
225 * the current capacity and there is room for expansion.
226 * Return <0 error code on error; 0 when nothing done; 1 when files were
227 * expanded and execution may have blocked.
228 * The files->file_lock should be held on entry, and will be held on exit.
229 */
230 int expand_files(struct files_struct *files, int nr)
231 {
232 struct fdtable *fdt;
233
234 fdt = files_fdtable(files);
235 /* Do we need to expand? */
236 if (nr < fdt->max_fds)
237 return 0;
238 /* Can we expand? */
239 if (nr >= sysctl_nr_open)
240 return -EMFILE;
241
242 /* All good, so we try */
243 return expand_fdtable(files, nr);
244 }
245
246 static void __devinit fdtable_defer_list_init(int cpu)
247 {
248 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
249 spin_lock_init(&fddef->lock);
250 INIT_WORK(&fddef->wq, free_fdtable_work);
251 fddef->next = NULL;
252 }
253
254 void __init files_defer_init(void)
255 {
256 int i;
257 for_each_possible_cpu(i)
258 fdtable_defer_list_init(i);
259 }
260
|
This page was automatically generated by the
LXR engine.
|