All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lukas Bulwahn <lukas.bulwahn@gmail.com>
To: Martin Fernandez <martin.fernandez@eclypsium.com>
Cc: "open list:DOCUMENTATION" <linux-doc@vger.kernel.org>,
	Dwaipayan Ray <dwaipayanray1@gmail.com>,
	Joe Perches <joe@perches.com>
Subject: Re: [PATCH v3] doc/checkpatch: Add description to MACRO_ARG_REUSE
Date: Wed, 6 Jul 2022 16:21:37 +0200	[thread overview]
Message-ID: <CAKXUXMyem+n2NySP2smrZLyjvVDWeSAM3Fst4QejLiCCH_gO8g@mail.gmail.com> (raw)
In-Reply-To: <CAKgze5ZoYEcPDwDgPkNehuM431z7SdF03308N0EuRRBWr68T1Q@mail.gmail.com>

On Wed, Jul 6, 2022 at 3:26 PM Martin Fernandez
<martin.fernandez@eclypsium.com> wrote:
>
> On 7/6/22, Lukas Bulwahn <lukas.bulwahn@gmail.com> wrote:
> > On Tue, Jul 5, 2022 at 1:00 AM Martin Fernandez
> > <martin.fernandez@eclypsium.com> wrote:
> >>
> >> Add a description, an example and a possible workaround to the
> >> MACRO_ARG_REUSE check.
> >>
> >> Signed-off-by: Martin Fernandez <martin.fernandez@eclypsium.com>
> >> Acked-by: Dwaipayan Ray <dwaipayanray1@gmail.com>
> >> ---
> >>  Documentation/dev-tools/checkpatch.rst | 20 ++++++++++++++++++++
> >>  1 file changed, 20 insertions(+)
> >>
> >> diff --git a/Documentation/dev-tools/checkpatch.rst
> >> b/Documentation/dev-tools/checkpatch.rst
> >> index b52452bc2963..86545c65cf7b 100644
> >> --- a/Documentation/dev-tools/checkpatch.rst
> >> +++ b/Documentation/dev-tools/checkpatch.rst
> >> @@ -759,6 +759,26 @@ Indentation and Line Breaks
> >>  Macros, Attributes and Symbols
> >>  ------------------------------
> >>
> >> +  **ARG_REUSE**
> >
> > The name of this checkpatch type is actually "MACRO_ARG_REUSE".
>
> You are right.
>
> >> +    Using the same argument multiple times in the macro definition
> >> +    would lead to unwanted side-effects.
> >
> > how about "... may lead to unwanted side effects"?
> >
> > Rationale: it does only lead to side effects if there are multiple
> > computations involved.
>
> Good point.
>
> > just on spelling:
> > s/side-effects/side effects/
> >
> >> +
> >> +    For example, given a `min` macro defined like::
> >> +
> >> +      #define min(x, y)  ((x) < (y) ? (x) : (y))
> >> +
> >> +    If you call it with `min(foo(x), 0)`, it would expand to::
> >> +
> >> +      foo(x) < 0 ? foo(x) : 0
> >> +
> >> +    If `foo` has side-effects or it's an expensive calculation the
> >> +    results might not be what the user intended.
> >> +
> >
> > s/side-effects/side effects/
> >
> >> +    For a workaround the idea is to define local variables to hold the
> >> +    macro's arguments. Checkout the actual implementation of `min` in
> >> +    include/linux/minmax.h for the full implementation of the
> >> +    workaround.
> >> +
> >
> > I ran checkpatch on all commits from v5.17..v5.18 and looked for all
> > check warnings with MACRO_ARG_REUSE.
> >
> > There were 35 warnings in 15 commits, touching 16 different files (4
> > in drivers/staging, 5 in drivers/net/wireless/, 5 in
> > drivers/net/ethernet/, 1 in drivers/net/dsa/, 1 in drivers/net/can/).
> >
> > As far as I see it from those commits, the more common way to address
> > this is to check that a macro is only used locally in some file and
> > that all uses of that macro pass a constant value as macro argument.
> >
> > Maybe we add these two as equally good alternatives?
>
> Yes, that's what I did on my patch that triggered this patch. But I
> don't think that's a workaround. You still have the issue there, just
> that the uses of the macros are "good".
>
> I think that falls better into the "I know what I'm doing, I'm ok with
> the warning" scenario, than a proper workaround.
>

Well, the purpose of the checkpatch documentation is to provide some
more background information on the warning (e.g., the historic
motivation, what to consider when judging its validity) and any hints
on possible resolutions. So, I would expect to see the documentation
cover explaining the most common (reusable) resolutions. A valid
argument why a check warning can be ignored falls into such a
resolution. In fact, the category "CHECK" in checkpatch.pl already
suggests that often the resolution may be to "inspect some code, but
not modify the code and then further 'ignore' the reported warning",
as some rules in checkpatch are checking something with just some
quite weak heuristics.

So, for this patch here: How about avoiding the word "workaround" and
just state these to options as resolution, e.g., a text like this:

Here are two possible options:
- Check the macro arguments of all uses of this macro to be free of
unintended side effects. Passing a constant value is usually fine, as
the compiler will use constant propagation and further optimizations
to produce acceptable code.
- If needed, define local variables in the macro to hold the macro's
argument. See the implementation of `min` in include/linux/minmax.h as
one example of this option.

What do you think?

I really appreciate you providing some documentation for this rule. I
also appreciate the rules that checkpatch.pl checks being better
explained to all of us in the kernel community. That avoids that we
all, especially newcomers, individually wonder about what checkpatch
intends to warn us about.

Lukas

  reply	other threads:[~2022-07-06 14:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-04 22:57 [PATCH v3] doc/checkpatch: Add description to MACRO_ARG_REUSE Martin Fernandez
2022-07-06  7:30 ` Bagas Sanjaya
2022-07-06 13:19   ` Martin Fernandez
2022-07-06 14:03     ` Akira Yokosawa
2022-07-06 14:07       ` Martin Fernandez
2022-07-06 11:29 ` Lukas Bulwahn
2022-07-06 13:26   ` Martin Fernandez
2022-07-06 14:21     ` Lukas Bulwahn [this message]
2022-07-06 17:19       ` Martin Fernandez
2022-07-06 15:09 ` Akira Yokosawa
2022-07-06 17:24   ` Martin Fernandez

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=CAKXUXMyem+n2NySP2smrZLyjvVDWeSAM3Fst4QejLiCCH_gO8g@mail.gmail.com \
    --to=lukas.bulwahn@gmail.com \
    --cc=dwaipayanray1@gmail.com \
    --cc=joe@perches.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=martin.fernandez@eclypsium.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.