From: Arnd Bergmann <arnd@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Tom Rix <trix@redhat.com>,
Zheng Zengkai <zhengzengkai@huawei.com>,
Randy Dunlap <rdunlap@infradead.org>,
David Howells <dhowells@redhat.com>,
Hulk Robot <hulkci@huawei.com>,
linux-afs@lists.infradead.org,
Marc Dionne <marc.dionne@auristor.com>,
linux-fsdevel <linux-fsdevel@vger.kernel.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] afs: fix no return statement in function returning non-void
Date: Fri, 18 Jun 2021 17:23:02 +0200 [thread overview]
Message-ID: <CAK8P3a3L6B9HXsOXSu9_c6pz1kN91Vig6EPsetLuYVW=M72XaQ@mail.gmail.com> (raw)
In-Reply-To: <CAHk-=wgW6yfsLUtepANX2PVkADR_7WDzk05YVhtw1ZBmDEGT2Q@mail.gmail.com>
On Thu, Jun 17, 2021 at 12:51 AM Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>
> On Wed, Jun 16, 2021 at 9:22 AM Tom Rix <trix@redhat.com> wrote:
> >
> > to fix, add an unreachable() to the generic BUG()
> >
> > diff --git a/include/asm-generic/bug.h b/include/asm-generic/bug.h
> > index f152b9bb916f..b250e06d7de2 100644
> > --- a/include/asm-generic/bug.h
> > +++ b/include/asm-generic/bug.h
> > @@ -177,7 +177,10 @@ void __warn(const char *file, int line, void
> > *caller, unsigned taint,
> >
> > #else /* !CONFIG_BUG */
> > #ifndef HAVE_ARCH_BUG
> > -#define BUG() do {} while (1)
> > +#define BUG() do { \
> > + do {} while (1); \
> > + unreachable(); \
> > + } while (0)
> > #endif
>
> I'm a bit surprised that the compiler doesn't make that code after an
> infinite loop automatically be marked "unreachable". But at the same I
> can imagine the compiler doing some checks without doing real flow
> analysis, and doing "oh, that conditional branch is unconditional".
>
> So this patch at least makes sense to me and I have no objections to
> it, even if it makes me go "silly compiler, we shouldn't have to tell
> you this".
>
> So Ack from me on this.
I've tried to figure out what the compiler is trying to do here, and it's
still weird. When I saw the patch posted, I misread it as having just
unreachable() without the loop, and that would have been bad
because it triggers undefined behavior.
What I found is a minimal test case of
static int f(void)
{
do {} while (1);
}
to trigger the warning with any version of gcc (not clang), but none of
these other variations cause a warning:
// not static -> no warning!
int f(void)
{
do {} while (1);
}
// some return statement anywhere in the function, no warning
static int f(int i)
{
if (i)
return 0;
do {} while (1);
}
// annotated as never returning, as discussed in this thread
static int __attribute__((noreturn)) f(void)
{
do {} while (1);
}
// unreachable annotation, as suggested by Tom
static int f(void)
{
do {} while (1);
__builtin_unreachable();
}
The last three are obviously intentional, as the warning is only for functions
that can *never* return but lack an annotation. I have no idea why the warning
is only for static functions though.
All my randconfig builds for arm/arm64/x86 missed this problem since those
architectures have a custom BUG() implementation with an inline asm.
I've taken them out now and found only two other instances of the issue so far:
arbitrary_virt_to_machine() and ppc64 get_hugepd_cache_index(). My preference
would be to annotate these as __noreturn, but change to the asm-generic
BUG() is probably better.
Arnd
next prev parent reply other threads:[~2021-06-18 15:27 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-15 11:55 [PATCH] afs: fix no return statement in function returning non-void David Howells
2021-06-15 12:03 ` David Howells
2021-06-15 14:49 ` Linus Torvalds
2021-06-15 23:58 ` Randy Dunlap
2021-06-16 0:32 ` Linus Torvalds
2021-06-16 1:38 ` Randy Dunlap
2021-06-16 2:19 ` Randy Dunlap
2021-06-16 3:15 ` Zheng Zengkai
2021-06-16 12:56 ` Tom Rix
2021-06-16 14:34 ` Linus Torvalds
2021-06-16 16:22 ` Tom Rix
2021-06-16 16:29 ` Linus Torvalds
2021-06-18 15:23 ` Arnd Bergmann [this message]
2021-06-16 13:41 ` David Howells
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAK8P3a3L6B9HXsOXSu9_c6pz1kN91Vig6EPsetLuYVW=M72XaQ@mail.gmail.com' \
--to=arnd@kernel.org \
--cc=dhowells@redhat.com \
--cc=hulkci@huawei.com \
--cc=linux-afs@lists.infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=marc.dionne@auristor.com \
--cc=rdunlap@infradead.org \
--cc=torvalds@linux-foundation.org \
--cc=trix@redhat.com \
--cc=zhengzengkai@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).