mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Kan Liang <kan.liang@linux.intel.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: Re: [RFC PATCH v1] perf build: Fix build for clang's -Wunreachable-code
Date: Fri, 30 May 2025 15:33:33 -0700	[thread overview]
Message-ID: <aDoyPbQ38vZTCaNn@google.com> (raw)
In-Reply-To: <CAP-5=fW0g_bhmHm-uyVRja7dhkw47K-PLAx5iubQEm4X+vTSAQ@mail.gmail.com>

On Wed, May 28, 2025 at 04:18:16PM -0700, Ian Rogers wrote:
> On Wed, May 28, 2025 at 2:59 PM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > On Wed, May 28, 2025 at 01:32:10PM -0700, Ian Rogers wrote:
> > > On Wed, May 28, 2025 at 12:56 PM Namhyung Kim <namhyung@kernel.org> wrote:
> > > >
> > > > On Wed, May 28, 2025 at 11:35:00AM -0700, Ian Rogers wrote:
> > > > > On Wed, May 28, 2025 at 11:24 AM Namhyung Kim <namhyung@kernel.org> wrote:
> > > > > >
> > > > > > On Tue, May 27, 2025 at 01:53:37PM -0700, Ian Rogers wrote:
> > > > > > > On Fri, Apr 11, 2025 at 3:14 PM Ian Rogers <irogers@google.com> wrote:
> > > > > > > >
> > > > > > > > On Fri, Apr 11, 2025 at 2:34 PM Namhyung Kim <namhyung@kernel.org> wrote:
> > > > > > > > >
> > > > > > > > > Hi Ian,
> > > > > > > > >
> > > > > > > > > On Thu, Apr 10, 2025 at 01:26:47PM -0700, Ian Rogers wrote:
> > > > > > > > > > Clang's unreachable code warning is able to catch bugs like the famous
> > > > > > > > > > "goto fail" which gcc's unreachable code warning fails to warn about
> > > > > > > > > > (it will complain about misleading indent). The changes here are
> > > > > > > > > > sufficient to get perf building with clang with -Wunreachable code,
> > > > > > > > > > but they don't really fix any bugs. Posting as an RFC to see if anyone
> > > > > > > > > > things this is worth pursuing.
> > > > > > > > >
> > > > > > > > > I'm not sure if it's useful and don't see what kind of bugs it can
> > > > > > > > > address.  The proposed changes don't look like an improvement.
> > > > > > > >
> > > > > > > > The goto fail case was in OpenSSL the code from a bad merge:
> > > > > > > > ```
> > > > > > > > if (...)
> > > > > > > >   goto fail;
> > > > > > > >   goto fail;
> > > > > > > > ```
> > > > > > > > Meaning the fail path was always taken and checking on the non-fail
> > > > > > > > code never executed. Newer GCCs will warn of this because of the
> > > > > > > > "misleading indent" but  clang won't. It is easy to imagine similar
> > > > > > > > mistakes creeping in, so using compiler warnings to avoid the bug
> > > > > > > > could be useful.
> > > > > >
> > > > > > It doesn't look very convincing to me but it might be valuable in some
> > > > > > rare cases.  But the proposed changes - basically replace exit() to
> > > > > > __builtin_unreachable() - seem weird.  Why is calling it a problem?  I
> > > > > > guess it already has some kind of annotation like "noreturn"?
> > > > >
> > > > > Yep. The exit is incorrect (depending on your notion of correct, I'd
> > > > > go with clang's notion as they've had to consider this for a while) as
> > > > > it can never be executed. I've added the __builtin_unreachable() to
> > > > > document that you can never get to that statement, as otherwise it can
> > > > > make the code readability harder with the code looking like it will
> > > > > fall through after calling something like usage_with_options (which is
> > > > > noreturn). In unoptimized builds __builtin_unreachable() will fail if
> > > > > executed, so it is a bit more active than just a comment.
> > > >
> > > > Oh I see, usage_with_options() calls exit() inside so any code after
> > > > that won't be executed.  Hmm.. isn't it better to remove those codes
> > > > then?
> > >
> > > Not sure I follow. The patch does remove the code but it replaces it
> > > with __builtin_unreachable() to basically state that the code here and
> > > below can never be reached. Do you mean remove the exit from
> > > usage_with_options? Then we'd need to fix all the callers, which would
> > > be a larger patch. Perhaps it should be usage_with_options_and_exit()
> > > to make it clearer that the code doesn't return. I was after doing
> > > what was minimal for -Wunreachable-code but while trying to keep the
> > > code clear.
> >
> > No, I meant we may not need the __builtin_unreachable() at the callsites.
> >
> > Would it complain this code?
> >
> >   if (some_bad_option_use)
> >     usage_with_options(...);
> >
> >   /* normal code path */
> 
> Right that would fix -Wunreachable, but the existing code would be:
> 
>   if (some_bad_option_use) {
>     usage_with_options(...);
>     exit(..);
>   }
>   /* normal code path */
> 
> Letting you know that "normal code path" couldn't be fallen into after
> usage_with_options(...). To make the behavior more obvious we could
> rename usage_with_options():
> 
>   if (some_bad_option_use)
>     usage_with_options_and_exit(...);
> 
>   /* normal code path */
> 
> What I've done is:
> 
>   if (some_bad_option_use) {
>     usage_with_options(...);
>     __builtin_unreachable();
>   }
>   /* normal code path */
> 
> My reasoning is that usage_with_options() doesn't obviously on the
> face of it call exit and never return. To make that clear we could add
> a comment:
> 
>   if (some_bad_option_use) {
>     usage_with_options(...);
>     /* usage_with_options never returns as it always calls exit */
>   }
>   /* normal code path */
> 
> But my preference is to use __builtin_unreachable as that is the same
> as the comment but is also something the compiler can trap on were it
> not true.

I see, thanks for the explanation.  That part looks ok then.

Thanks,
Namhyung


  reply	other threads:[~2025-05-30 22:33 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-10 20:26 Ian Rogers
2025-04-11 21:34 ` Namhyung Kim
2025-04-11 22:14   ` Ian Rogers
2025-05-27 20:53     ` Ian Rogers
2025-05-28 18:24       ` Namhyung Kim
2025-05-28 18:35         ` Ian Rogers
2025-05-28 19:56           ` Namhyung Kim
2025-05-28 20:32             ` Ian Rogers
2025-05-28 21:59               ` Namhyung Kim
2025-05-28 23:18                 ` Ian Rogers
2025-05-30 22:33                   ` Namhyung Kim [this message]
2025-05-31 18:22                     ` David Laight

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=aDoyPbQ38vZTCaNn@google.com \
    --to=namhyung@kernel.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    /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

all inboxes | Powered by JetHome®