Message ID | 20240216202352.2492798-1-arnd@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-69291-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp767145dyb; Fri, 16 Feb 2024 12:24:17 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWX9PhOfH4rOcwHxH1l7rrMXqaolcWtFPDcWRffGod2kmUdyblZ/U7jA5xG4HvKy0g17OeciUSokoqutyJCtbYOH2nNeg== X-Google-Smtp-Source: AGHT+IHvEYqjXa8DeKv4CE2d7F5WqQgEBB1EB4kw6+Ld6AmreBC7Iz48CetXO9nn3hGCIqIEpV+t X-Received: by 2002:a05:620a:468a:b0:787:3c10:f17f with SMTP id bq10-20020a05620a468a00b007873c10f17fmr5609415qkb.61.1708115057683; Fri, 16 Feb 2024 12:24:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708115057; cv=pass; d=google.com; s=arc-20160816; b=vDQu9pBzI/yQiSu3tr2SgWgFCqIsKwg6L6uTLfA8YepF3xugctyK+TNtRn8nDk9xLS GIVZEE0CMZ5CdNLOupTROdfgVK3T6gcL9xxX1Ji1uLJUqnXLgG3hLhQ7CSOaDgFzaW/d IC44CwBC3j+5Q8fihkOb2N6zyWPlBmk3Zy4dO/QI4D/O3hwtpPSw7WKm2uaW25aKrGir 7lYYmXXF1oD9TvsbLmzZtk2ZmpADEtvo0tSqEk1/Tif4B0SzKCZULhZfXtPv1WwJ/HH1 AofV9V83Zd/j5vqEHYxQfwL4Ou9piQHfk4hBOQbXx+FrZApwMcJEd21kNU2i8XJ08Jx4 dMHA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=Oo9HBcLfH1N8g9ihc0+Bym+GK1so/hZIsswt3CwurtQ=; fh=PHw3cwNpb8q3MDTw4QDqnZoZeBb1CxYJNb1mDoQLG3Q=; b=OpkXiK2hInU4drPmeYpSUYP7e9mcWmShkxQnblMQ977PyErP8flykQF6c10l+keiTW tCMtkoC9A60vhjvYDPVkQm0tiSvNQh9WWQKEHejChgPMLtS60AmKXF10r8PNjtPM4r1K MuZc2/XzjJYYbc/pverIsxvn5ww5EwthcnNBv4rV5t1vQAUULqHna7/0nGbHERWmxVmE +QFbOKf8b+/NFO5D6Trzq8vO1arANnVNhkZRqv9AH6BWA0zQJNjnz3tgoegahgbnE+Vm oGyTaWHyhwhcXhigl1g0fDZVoCOWb4OWc5dbyVtMHWFI+FZgKUXN0XoWaqMlMPKWw/Us QxEA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BREFQPUE; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-69291-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69291-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id o24-20020a05620a0d5800b007871de8ec5asi685092qkl.52.2024.02.16.12.24.17 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 12:24:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-69291-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=BREFQPUE; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-69291-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-69291-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 7A39F1C21832 for <ouuuleilei@gmail.com>; Fri, 16 Feb 2024 20:24:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B2A521384B5; Fri, 16 Feb 2024 20:24:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BREFQPUE" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2EF67135A6F; Fri, 16 Feb 2024 20:23:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708115039; cv=none; b=W/ZyNW0UflRYyP/hmXQHFwKevKoSJQBFZd4dYHrOPJYjYpqymjw9GkZzgW6IUMle+qlvhjTNaHqbRtN4kRpq1z4ClT9eEZ9t2oJ08J6FrfRCMbztZ1QiLcwGuh6tMTj5HROpeBLEJtcNk08qnUcL/oJopeVfwM0H/ScVeLTwnMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708115039; c=relaxed/simple; bh=Sq/Mpf4CU1DDxAJaU5smyIB6OI93uNYISckA1rqgEqc=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=iB1ruXJJeSyQXtiVpWlmnoVzK6HCFHMjd4QBpMlOuamQeIvWRME/wgHBBDc17nUNExiTZ0Yc0DrdMtsoYC5p6HwA1LbeP6FVs4d0AQOhqcuH1j1CjmpdgCWEFJOYqNV9KcjGpEBgv/jAHRJ7/KC39Zx79RFpHrBv88yDA6IDe0E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BREFQPUE; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9DD7C433C7; Fri, 16 Feb 2024 20:23:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708115038; bh=Sq/Mpf4CU1DDxAJaU5smyIB6OI93uNYISckA1rqgEqc=; h=From:To:Cc:Subject:Date:From; b=BREFQPUE+Lg3oFG94/XQbjW6/X+N9Fgw8T5AM64uUsoM72l6MQ59lraq2doPeAmw0 gDuJLNl+241uzB2b51z9SyZ4hC6DZvvm8yLd+PNsKAmnUOL68zlMmfjN0bfQWpM0pd Ars2RqyFjk6GCUfQyBKbiffJqgbSCHUu46VLLSCSBmyGaMUALsTwJlygoySG7izBUr 6eivxdVnqLOuY8O6W2m+xBwAjiQN5gdxr7EYCRV6hBsrzlx5tWGshcsXdz0XtpvgtM AepxHs04hMDnSrPQU+THAkdsqYO1fmCnnMVMRLhwc6uL3gfRhk411oeGOh0wolXfkP qZaPK+/EP9M6w== From: Arnd Bergmann <arnd@kernel.org> To: Alexander Viro <viro@zeniv.linux.org.uk>, Christian Brauner <brauner@kernel.org> Cc: Arnd Bergmann <arnd@arndb.de>, Jan Kara <jack@suse.cz>, Nathan Chancellor <nathan@kernel.org>, Nick Desaulniers <ndesaulniers@google.com>, Bill Wendling <morbo@google.com>, Justin Stitt <justinstitt@google.com>, Kees Cook <keescook@chromium.org>, Andrew Morton <akpm@linux-foundation.org>, Andi Kleen <ak@linux.intel.com>, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH] fs/select: rework stack allocation hack for clang Date: Fri, 16 Feb 2024 21:23:34 +0100 Message-Id: <20240216202352.2492798-1-arnd@kernel.org> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791088455032675498 X-GMAIL-MSGID: 1791088455032675498 |
Series |
fs/select: rework stack allocation hack for clang
|
|
Commit Message
Arnd Bergmann
Feb. 16, 2024, 8:23 p.m. UTC
From: Arnd Bergmann <arnd@arndb.de> 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> --- fs/select.c | 2 +- include/linux/poll.h | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-)
Comments
On Fri, Feb 16, 2024 at 09:23:34PM +0100, Arnd Bergmann wrote: > From: Arnd Bergmann <arnd@arndb.de> > > 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> Reviewed-by: Kees Cook <keescook@chromium.org>
I suspect given the larger default stack now we could maybe just increase the
warning limit too, but that should be fine.
Reviewed-by: Andi Kleen <ak@linux.intel.com>
On Sun, Feb 18, 2024, at 11:19, Andi Kleen wrote: > I suspect given the larger default stack now we could maybe just increase the > warning limit too, but that should be fine. I don't think we have increased the default stack size in decades, it's still 8KB on almost all 32-bit architectures (sh, hexagon and m68k still allow 4KB stacks, but that's probably broken). I would actually like to (optionally) reduce the stack size for 64-bit machines from 16KB to 12KB now that it's always vmapped. > Reviewed-by: Andi Kleen <ak@linux.intel.com> Thanks, Arnd
On Sun, Feb 18, 2024 at 11:29:32AM +0100, Arnd Bergmann wrote: > On Sun, Feb 18, 2024, at 11:19, Andi Kleen wrote: > > I suspect given the larger default stack now we could maybe just increase the > > warning limit too, but that should be fine. > > I don't think we have increased the default stack size in decades, > it's still 8KB on almost all 32-bit architectures (sh, hexagon and m68k > still allow 4KB stacks, but that's probably broken). I would actually > like to (optionally) reduce the stack size for 64-bit machines > from 16KB to 12KB now that it's always vmapped. now == after 4/8K. The 1024 warning limit dates back to the 4/8K times. It could be certainly reevaluated for this decade's setup. -Andi
On Fri 16-02-24 21:23:34, Arnd Bergmann wrote: > From: Arnd Bergmann <arnd@arndb.de> > > 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> Looks good (if this indeed works with clang ;). Feel free to add: Reviewed-by: Jan Kara <jack@suse.cz> Honza > --- > fs/select.c | 2 +- > include/linux/poll.h | 4 ---- > 2 files changed, 1 insertion(+), 5 deletions(-) > > diff --git a/fs/select.c b/fs/select.c > index 11a3b1312abe..9515c3fa1a03 100644 > --- a/fs/select.c > +++ b/fs/select.c > @@ -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; > diff --git a/include/linux/poll.h b/include/linux/poll.h > index a9e0e1c2d1f2..d1ea4f3714a8 100644 > --- a/include/linux/poll.h > +++ b/include/linux/poll.h > @@ -14,11 +14,7 @@ > > /* ~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 > -- > 2.39.2 >
From: Arnd Bergmann > Sent: 18 February 2024 10:30 > > On Sun, Feb 18, 2024, at 11:19, Andi Kleen wrote: > > I suspect given the larger default stack now we could maybe just increase the > > warning limit too, but that should be fine. > > I don't think we have increased the default stack size in decades, > it's still 8KB on almost all 32-bit architectures (sh, hexagon and m68k > still allow 4KB stacks, but that's probably broken). I would actually > like to (optionally) reduce the stack size for 64-bit machines > from 16KB to 12KB now that it's always vmapped. Hasn't the stack for (some) ppc64 been increased to 32k to try to stop some recursive network code (of some kind) exploding the stack. (This causes grief when the stack size is doubled for kasan!) I really don't understand why the change isn't deemed necessary for some cpu types (I think ones that are likely to have less processors and less memory) because a small amount of the same workload would explode a the smaller stack. OTOH more pressure really ought to be applied to remove the recursion. Unless you are trying to calculate the ackerman function (don't!) it is usually not to difficult to convert recursion to a loop. More than one level is really asking for trouble. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
On Fri, 16 Feb 2024 21:23:34 +0100, Arnd Bergmann wrote: > 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. > > [...] Applied to the vfs.misc branch of the vfs/vfs.git tree. Patches in the vfs.misc branch should appear in linux-next soon. Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it. It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated. Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch. tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.misc [1/1] fs/select: rework stack allocation hack for clang https://git.kernel.org/vfs/vfs/c/f3dd8c812c24
diff --git a/fs/select.c b/fs/select.c index 11a3b1312abe..9515c3fa1a03 100644 --- a/fs/select.c +++ b/fs/select.c @@ -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; diff --git a/include/linux/poll.h b/include/linux/poll.h index a9e0e1c2d1f2..d1ea4f3714a8 100644 --- a/include/linux/poll.h +++ b/include/linux/poll.h @@ -14,11 +14,7 @@ /* ~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