Skip to content

Commit bbea9f6

Browse files
Vadim LobanovLinus Torvalds
authored andcommitted
[PATCH] fdtable: Make fdarray and fdsets equal in size
Currently, each fdtable supports three dynamically-sized arrays of data: the fdarray and two fdsets. The code allows the number of fds supported by the fdarray (fdtable->max_fds) to differ from the number of fds supported by each of the fdsets (fdtable->max_fdset). In practice, it is wasteful for these two sizes to differ: whenever we hit a limit on the smaller-capacity structure, we will reallocate the entire fdtable and all the dynamic arrays within it, so any delta in the memory used by the larger-capacity structure will never be touched at all. Rather than hogging this excess, we shouldn't even allocate it in the first place, and keep the capacities of the fdarray and the fdsets equal. This patch removes fdtable->max_fdset. As an added bonus, most of the supporting code becomes simpler. Signed-off-by: Vadim Lobanov <vlobanov@speakeasy.net> Cc: Christoph Hellwig <hch@lst.de> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Dipankar Sarma <dipankar@in.ibm.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent f3d19c9 commit bbea9f6

13 files changed

Lines changed: 48 additions & 81 deletions

File tree

‎arch/alpha/kernel/osf_sys.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ osf_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp,
979979
long timeout;
980980
int ret = -EINVAL;
981981
struct fdtable *fdt;
982-
int max_fdset;
982+
int max_fds;
983983

984984
timeout = MAX_SCHEDULE_TIMEOUT;
985985
if (tvp) {
@@ -1003,9 +1003,9 @@ osf_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp,
10031003

10041004
rcu_read_lock();
10051005
fdt = files_fdtable(current->files);
1006-
max_fdset = fdt->max_fdset;
1006+
max_fds = fdt->max_fds;
10071007
rcu_read_unlock();
1008-
if (n < 0 || n > max_fdset)
1008+
if (n < 0 || n > max_fds)
10091009
goto out_nofds;
10101010

10111011
/*

‎arch/mips/kernel/kspd.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static void sp_cleanup(void)
301301
for (;;) {
302302
unsigned long set;
303303
i = j * __NFDBITS;
304-
if (i >= fdt->max_fdset || i >= fdt->max_fds)
304+
if (i >= fdt->max_fds)
305305
break;
306306
set = fdt->open_fds->fds_bits[j++];
307307
while (set) {

‎fs/compat.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,19 +1679,19 @@ int compat_core_sys_select(int n, compat_ulong_t __user *inp,
16791679
{
16801680
fd_set_bits fds;
16811681
char *bits;
1682-
int size, max_fdset, ret = -EINVAL;
1682+
int size, max_fds, ret = -EINVAL;
16831683
struct fdtable *fdt;
16841684

16851685
if (n < 0)
16861686
goto out_nofds;
16871687

1688-
/* max_fdset can increase, so grab it once to avoid race */
1688+
/* max_fds can increase, so grab it once to avoid race */
16891689
rcu_read_lock();
16901690
fdt = files_fdtable(current->files);
1691-
max_fdset = fdt->max_fdset;
1691+
max_fds = fdt->max_fds;
16921692
rcu_read_unlock();
1693-
if (n > max_fdset)
1694-
n = max_fdset;
1693+
if (n > max_fds)
1694+
n = max_fds;
16951695

16961696
/*
16971697
* We need 6 bitmaps (in/out/ex for both incoming and outgoing),

‎fs/exec.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ static void flush_old_files(struct files_struct * files)
783783
j++;
784784
i = j * __NFDBITS;
785785
fdt = files_fdtable(files);
786-
if (i >= fdt->max_fds || i >= fdt->max_fdset)
786+
if (i >= fdt->max_fds)
787787
break;
788788
set = fdt->close_on_exec->fds_bits[j];
789789
if (!set)

‎fs/fcntl.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ static int locate_fd(struct files_struct *files,
7777
start = files->next_fd;
7878

7979
newfd = start;
80-
if (start < fdt->max_fdset) {
80+
if (start < fdt->max_fds)
8181
newfd = find_next_zero_bit(fdt->open_fds->fds_bits,
82-
fdt->max_fdset, start);
83-
}
82+
fdt->max_fds, start);
8483

8584
error = -EMFILE;
8685
if (newfd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)

‎fs/file.c‎

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ void free_fd_array(struct file **array, int num)
6868

6969
static void __free_fdtable(struct fdtable *fdt)
7070
{
71-
free_fdset(fdt->open_fds, fdt->max_fdset);
72-
free_fdset(fdt->close_on_exec, fdt->max_fdset);
71+
free_fdset(fdt->open_fds, fdt->max_fds);
72+
free_fdset(fdt->close_on_exec, fdt->max_fds);
7373
free_fd_array(fdt->fd, fdt->max_fds);
7474
kfree(fdt);
7575
}
@@ -98,7 +98,7 @@ static void free_fdtable_rcu(struct rcu_head *rcu)
9898
struct fdtable_defer *fddef;
9999

100100
BUG_ON(!fdt);
101-
fdset_size = fdt->max_fdset / 8;
101+
fdset_size = fdt->max_fds / 8;
102102
fdarray_size = fdt->max_fds * sizeof(struct file *);
103103

104104
if (fdt->free_files) {
@@ -110,13 +110,11 @@ static void free_fdtable_rcu(struct rcu_head *rcu)
110110
kmem_cache_free(files_cachep, fdt->free_files);
111111
return;
112112
}
113-
if (fdt->max_fdset <= EMBEDDED_FD_SET_SIZE &&
114-
fdt->max_fds <= NR_OPEN_DEFAULT) {
113+
if (fdt->max_fds <= NR_OPEN_DEFAULT)
115114
/*
116115
* The fdtable was embedded
117116
*/
118117
return;
119-
}
120118
if (fdset_size <= PAGE_SIZE && fdarray_size <= PAGE_SIZE) {
121119
kfree(fdt->open_fds);
122120
kfree(fdt->close_on_exec);
@@ -136,9 +134,7 @@ static void free_fdtable_rcu(struct rcu_head *rcu)
136134

137135
void free_fdtable(struct fdtable *fdt)
138136
{
139-
if (fdt->free_files ||
140-
fdt->max_fdset > EMBEDDED_FD_SET_SIZE ||
141-
fdt->max_fds > NR_OPEN_DEFAULT)
137+
if (fdt->free_files || fdt->max_fds > NR_OPEN_DEFAULT)
142138
call_rcu(&fdt->rcu, free_fdtable_rcu);
143139
}
144140

@@ -151,22 +147,21 @@ static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt)
151147
int i;
152148
int count;
153149

154-
BUG_ON(nfdt->max_fdset < fdt->max_fdset);
155150
BUG_ON(nfdt->max_fds < fdt->max_fds);
156151
/* Copy the existing tables and install the new pointers */
157152

158-
i = fdt->max_fdset / (sizeof(unsigned long) * 8);
159-
count = (nfdt->max_fdset - fdt->max_fdset) / 8;
153+
i = fdt->max_fds / (sizeof(unsigned long) * 8);
154+
count = (nfdt->max_fds - fdt->max_fds) / 8;
160155

161156
/*
162157
* Don't copy the entire array if the current fdset is
163158
* not yet initialised.
164159
*/
165160
if (i) {
166161
memcpy (nfdt->open_fds, fdt->open_fds,
167-
fdt->max_fdset/8);
162+
fdt->max_fds/8);
168163
memcpy (nfdt->close_on_exec, fdt->close_on_exec,
169-
fdt->max_fdset/8);
164+
fdt->max_fds/8);
170165
memset (&nfdt->open_fds->fds_bits[i], 0, count);
171166
memset (&nfdt->close_on_exec->fds_bits[i], 0, count);
172167
}
@@ -201,7 +196,7 @@ fd_set * alloc_fdset(int num)
201196

202197
void free_fdset(fd_set *array, int num)
203198
{
204-
if (num <= EMBEDDED_FD_SET_SIZE) /* Don't free an embedded fdset */
199+
if (num <= NR_OPEN_DEFAULT) /* Don't free an embedded fdset */
205200
return;
206201
else if (num <= 8 * PAGE_SIZE)
207202
kfree(array);
@@ -220,18 +215,6 @@ static struct fdtable *alloc_fdtable(int nr)
220215
if (!fdt)
221216
goto out;
222217

223-
nfds = max_t(int, 8 * L1_CACHE_BYTES, roundup_pow_of_two(nr + 1));
224-
if (nfds > NR_OPEN)
225-
nfds = NR_OPEN;
226-
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-
235218
nfds = NR_OPEN_DEFAULT;
236219
/*
237220
* Expand to the max in easy steps, and keep expanding it until
@@ -251,15 +234,21 @@ static struct fdtable *alloc_fdtable(int nr)
251234
nfds = NR_OPEN;
252235
}
253236
} while (nfds <= nr);
237+
238+
new_openset = alloc_fdset(nfds);
239+
new_execset = alloc_fdset(nfds);
240+
if (!new_openset || !new_execset)
241+
goto out;
242+
fdt->open_fds = new_openset;
243+
fdt->close_on_exec = new_execset;
244+
254245
new_fds = alloc_fd_array(nfds);
255246
if (!new_fds)
256-
goto out2;
247+
goto out;
257248
fdt->fd = new_fds;
258249
fdt->max_fds = nfds;
259250
fdt->free_files = NULL;
260251
return fdt;
261-
out2:
262-
nfds = fdt->max_fdset;
263252
out:
264253
free_fdset(new_openset, nfds);
265254
free_fdset(new_execset, nfds);
@@ -290,7 +279,7 @@ static int expand_fdtable(struct files_struct *files, int nr)
290279
* we dropped the lock
291280
*/
292281
cur_fdt = files_fdtable(files);
293-
if (nr >= cur_fdt->max_fds || nr >= cur_fdt->max_fdset) {
282+
if (nr >= cur_fdt->max_fds) {
294283
/* Continue as planned */
295284
copy_fdtable(new_fdt, cur_fdt);
296285
rcu_assign_pointer(files->fdt, new_fdt);
@@ -316,11 +305,10 @@ int expand_files(struct files_struct *files, int nr)
316305

317306
fdt = files_fdtable(files);
318307
/* Do we need to expand? */
319-
if (nr < fdt->max_fdset && nr < fdt->max_fds)
308+
if (nr < fdt->max_fds)
320309
return 0;
321310
/* Can we expand? */
322-
if (fdt->max_fdset >= NR_OPEN || fdt->max_fds >= NR_OPEN ||
323-
nr >= NR_OPEN)
311+
if (nr >= NR_OPEN)
324312
return -EMFILE;
325313

326314
/* All good, so we try */

‎fs/open.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,8 +864,7 @@ int get_unused_fd(void)
864864

865865
repeat:
866866
fdt = files_fdtable(files);
867-
fd = find_next_zero_bit(fdt->open_fds->fds_bits,
868-
fdt->max_fdset,
867+
fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
869868
files->next_fd);
870869

871870
/*

‎fs/select.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
311311
{
312312
fd_set_bits fds;
313313
void *bits;
314-
int ret, max_fdset;
314+
int ret, max_fds;
315315
unsigned int size;
316316
struct fdtable *fdt;
317317
/* Allocate small arguments on the stack to save memory and be faster */
@@ -321,13 +321,13 @@ static int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
321321
if (n < 0)
322322
goto out_nofds;
323323

324-
/* max_fdset can increase, so grab it once to avoid race */
324+
/* max_fds can increase, so grab it once to avoid race */
325325
rcu_read_lock();
326326
fdt = files_fdtable(current->files);
327-
max_fdset = fdt->max_fdset;
327+
max_fds = fdt->max_fds;
328328
rcu_read_unlock();
329-
if (n > max_fdset)
330-
n = max_fdset;
329+
if (n > max_fds)
330+
n = max_fds;
331331

332332
/*
333333
* We need 6 bitmaps (in/out/ex for both incoming and outgoing),

‎include/linux/file.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,8 @@ struct embedded_fd_set {
2626
unsigned long fds_bits[1];
2727
};
2828

29-
/*
30-
* More than this number of fds: we use a separately allocated fd_set
31-
*/
32-
#define EMBEDDED_FD_SET_SIZE (BITS_PER_BYTE * sizeof(struct embedded_fd_set))
33-
3429
struct fdtable {
3530
unsigned int max_fds;
36-
int max_fdset;
3731
struct file ** fd; /* current fd array */
3832
fd_set *close_on_exec;
3933
fd_set *open_fds;

‎include/linux/init_task.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#define INIT_FDTABLE \
1313
{ \
1414
.max_fds = NR_OPEN_DEFAULT, \
15-
.max_fdset = EMBEDDED_FD_SET_SIZE, \
1615
.fd = &init_files.fd_array[0], \
1716
.close_on_exec = (fd_set *)&init_files.close_on_exec_init, \
1817
.open_fds = (fd_set *)&init_files.open_fds_init, \

0 commit comments

Comments
 (0)