Skip to content

Commit d888c83

Browse files
committed
fs: fix fd table size alignment properly
Jason Donenfeld reports that my commit 1c24a18 ("fs: fd tables have to be multiples of BITS_PER_LONG") doesn't work, and the reason is an embarrassing brown-paper-bag bug. Yes, we want to align the number of fds to BITS_PER_LONG, and yes, the reason they might not be aligned is because the incoming 'max_fd' argument might not be aligned. But aligining the argument - while simple - will cause a "infinitely big" maxfd (eg NR_OPEN_MAX) to just overflow to zero. Which most definitely isn't what we want either. The obvious fix was always just to do the alignment last, but I had moved it earlier just to make the patch smaller and the code look simpler. Duh. It certainly made _me_ look simple. Fixes: 1c24a18 ("fs: fd tables have to be multiples of BITS_PER_LONG") Reported-and-tested-by: Jason A. Donenfeld <Jason@zx2c4.com> Cc: Fedor Pchelkin <aissur0002@gmail.com> Cc: Alexey Khoroshilov <khoroshilov@ispras.ru> Cc: Christian Brauner <brauner@kernel.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 965181d commit d888c83

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

fs/file.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,9 @@ static unsigned int sane_fdtable_size(struct fdtable *fdt, unsigned int max_fds)
303303
unsigned int count;
304304

305305
count = count_open_files(fdt);
306-
max_fds = ALIGN(max_fds, BITS_PER_LONG);
307306
if (max_fds < NR_OPEN_DEFAULT)
308307
max_fds = NR_OPEN_DEFAULT;
309-
return min(count, max_fds);
308+
return ALIGN(min(count, max_fds), BITS_PER_LONG);
310309
}
311310

312311
/*

0 commit comments

Comments
 (0)