From: Johannes Berg <johannes@sipsolutions.net>
To: Hongbo Li <lihongbo22@huawei.com>,
davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
pabeni@redhat.com, kvalo@kernel.org
Cc: linux-wireless@vger.kernel.org, netdev@vger.kernel.org
Subject: Re: [PATCH -next v2] wifi: mac80211: use max to simplify the code
Date: Tue, 27 Aug 2024 09:39:00 +0200 [thread overview]
Message-ID: <4967a09d4c00532148aacd7dc4b5e21902ef7721.camel@sipsolutions.net> (raw)
In-Reply-To: <82523993-61c8-4c03-8826-61da9081d3ba@huawei.com>
On Tue, 2024-08-27 at 15:29 +0800, Hongbo Li wrote:
>
> On 2024/8/27 15:25, Johannes Berg wrote:
> > On Tue, 2024-08-27 at 11:03 +0800, Hongbo Li wrote:
> > > The following Coccinelle/coccicheck warning reported by
> > > minmax.cocci:
> > > WARNING opportunity for max()
> >
> > Yeah well, maybe sometimes we shouldn't blindly follow tools ...
> >
> > > Let's use max() to simplify the code and fix the warning.
> >
> > You should explain why.
> >
> > I think only one out of four changes in this patch is correct,
> > semantically.
> >
> You mean sometimes we should keep the variable type in comparison?
No, I just don't think these are semantically calculations of a maximum,
even if they look that way.
That's why I asked: Why are you making this change? It looks like you're
making this change just because you want coccicheck to be silent here.
But that's *really* not a good reason! Don't do that, ever, *think*
about the changes you're making too.
We should consider the primary consumer of the code to be *people*, not
the compiler or tools like coccicheck. And for *people*, applying max()
to a link ID makes no sense. It's a link ID, not any kind of value that
applying max() to makes any sense.
In contrast, for the timeout value there that you changed, that _does_
make sense: it clearly wants to take the longer of the two durations.
So then why do we have patterns that look like max(0, link_id)? That's
because we treat -1 as a special value indicating "no link, but for the
whole sta/vif/...", "don't care about the link" or "MLD not used"
(depending on the context). Internally in the code, however, we use 0
for non-MLD to simplify older drivers and internal logic.
That's why we end up with "link_id >= 0 ? link_id : 0" in some places.
But it's fundamentally not max() even though it looks like it. Replacing
it with max() does this a disservice.
Now arguably open-coding it often (though three perhaps isn't often, but
I'm surprised it's only three times) maybe isn't a great idea either,
but then that should be solved differently.
So yeah, please think about changes, don't make them blindly.
johannes
prev parent reply other threads:[~2024-08-27 7:39 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-27 3:03 [PATCH -next v2] wifi: mac80211: use max to simplify the code Hongbo Li
2024-08-27 7:25 ` Johannes Berg
2024-08-27 7:29 ` Hongbo Li
2024-08-27 7:39 ` Johannes Berg [this message]
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=4967a09d4c00532148aacd7dc4b5e21902ef7721.camel@sipsolutions.net \
--to=johannes@sipsolutions.net \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=kuba@kernel.org \
--cc=kvalo@kernel.org \
--cc=lihongbo22@huawei.com \
--cc=linux-wireless@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.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