fs/select: rework stack allocation hack for clang
authorArnd Bergmann <arnd@arndb.de>
Fri, 16 Feb 2024 20:23:34 +0000 (21:23 +0100)
committerChristian Brauner <brauner@kernel.org>
Tue, 20 Feb 2024 08:23:52 +0000 (09:23 +0100)
A while ago, we changed the way that select() and poll() preallocate
a temporary buffer just under the size of the static warning limit of
1024 bytes, as clang was frequently going slightly above that limit.

The warnings have recently returned and I took another look. As it turns
out, clang is not actually inherently worse at reserving stack space,
it just happens to inline do_select() into core_sys_select(), while gcc
never inlines it.

Annotate do_select() to never be inlined and in turn remove the special
case for the allocation size. This should give the same behavior for
both clang and gcc all the time and once more avoids those warnings.

Fixes: ad312f95d41c ("fs/select: avoid clang stack usage warning")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/r/20240216202352.2492798-1-arnd@kernel.org
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Andi Kleen <ak@linux.intel.com>
Reviewed-by: Jan Kara <jack@suse.cz>
Signed-off-by: Christian Brauner <brauner@kernel.org>
fs/select.c
include/linux/poll.h

index 11a3b1312abeff485f520f665859a86c10e216e8..9515c3fa1a03e8a8576f90f8a4dee46509dd1954 100644 (file)
@@ -476,7 +476,7 @@ static inline void wait_key_set(poll_table *wait, unsigned long in,
                wait->_key |= POLLOUT_SET;
 }
 
-static int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
+static noinline_for_stack int do_select(int n, fd_set_bits *fds, struct timespec64 *end_time)
 {
        ktime_t expire, *to = NULL;
        struct poll_wqueues table;
index a9e0e1c2d1f2ff89d99a9377a1de7d0dc78483cd..d1ea4f3714a8485677e89a4c6c07a657e53a15ca 100644 (file)
 
 /* ~832 bytes of stack space used max in sys_select/sys_poll before allocating
    additional memory. */
-#ifdef __clang__
-#define MAX_STACK_ALLOC 768
-#else
 #define MAX_STACK_ALLOC 832
-#endif
 #define FRONTEND_STACK_ALLOC   256
 #define SELECT_STACK_ALLOC     FRONTEND_STACK_ALLOC
 #define POLL_STACK_ALLOC       FRONTEND_STACK_ALLOC