From: David Laight <david.laight.linux@gmail.com>
To: Jacek Kowalski <jacek@jacekk.info>
Cc: Simon Horman <horms@kernel.org>,
Tony Nguyen <anthony.l.nguyen@intel.com>,
Przemek Kitszel <przemyslaw.kitszel@intel.com>,
Andrew Lunn <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
intel-wired-lan@lists.osuosl.org, netdev@vger.kernel.org
Subject: Re: [PATCH iwl-next v2 1/5] e1000: drop unnecessary constant casts to u16
Date: Mon, 14 Jul 2025 13:21:14 +0100 [thread overview]
Message-ID: <20250714132114.70feff08@pumpkin> (raw)
In-Reply-To: <522a1e9d-0453-447b-b541-86b76fa245bd@jacekk.info>
On Tue, 8 Jul 2025 21:40:12 +0200
Jacek Kowalski <jacek@jacekk.info> wrote:
> >> - if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
> >> + if ((old_vid != E1000_MNG_VLAN_NONE) &&
> >
> > Ditto.
> >
> > But more importantly, both Clang 20.1.7 W=1 builds (or at any rate,
> > builds with -Wtautological-constant-out-of-range-compare), and Smatch
> > complain that the comparison above is now always true because
> > E1000_MNG_VLAN_NONE is -1, while old_vid is unsigned.
I'm guessing 'old_vid' is actually u16 (or the compiler knows the
value came from a u16).
In either case the compiler can 'know' that the condition is always
true - but a 'u16 old_vid' is promoted to 'signed int' prior to
the compare with -1, whereas if a 'u32 old_vid' is known to contain
a 16bit value it is the -1 that is converted to ~0u.
>
> You are right - I have missed that E1000_MNG_VLAN_NONE is negative.
> Therefore (u16)E1000_MNG_VLAN_NONE has a side effect of causing a
> wraparound.
>
> It's even more interesting that (inadvertently) I have not made a
> similar change in e1000e:
>
> ./drivers/net/ethernet/intel/e1000e/netdev.c:
> if (adapter->mng_vlan_id != (u16)E1000_MNG_VLAN_NONE) {
>
>
> > Perhaps E1000_MNG_VLAN_NONE should be updated to be UINT16_MAX?
>
> There's no UINT16_MAX in kernel as far as I know. I'd rather leave it as
> it was or, if you insist on further refactoring, use either one of:
>
> #define E1000_MNG_VLAN_NONE (u16)(~((u16) 0))
> mimick ACPI: #define ACPI_UINT16_MAX (u16)(~((u16) 0))
>
> #define E1000_MNG_VLAN_NONE ((u16)-1)
> move the cast into the constant
>
> #define E1000_MNG_VLAN_NONE 0xFFFF
> use ready-made value
Possibly better is 0xFFFFu.
Then 'u16 old_val' is first promoted to 'signed int' and then implicitly
cast to 'unsigned int' prior to the comparison.
Remember C does all maths as [un]signed int (except for types bigger than int).
Local variables, function parameters and function results should really
be [un]signed int provided the domain of value doesn't exceed that of int.
Otherwise the compile is forced to generate explicit instructions to mask
the result of arithmetic operations to the smaller type.
David
>
> (parentheses left only due to the constant being "(-1)" and not "-1").
>
next prev parent reply other threads:[~2025-07-14 12:21 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-08 8:16 [PATCH iwl-next v2 0/5] drop unnecessary constant casts to u16 Jacek Kowalski
2025-07-08 8:16 ` [PATCH iwl-next v2 1/5] e1000: " Jacek Kowalski
2025-07-08 19:06 ` Simon Horman
2025-07-08 19:40 ` Jacek Kowalski
2025-07-14 12:21 ` David Laight [this message]
2025-07-09 3:03 ` kernel test robot
2025-07-11 17:25 ` David Laight
2025-07-08 8:17 ` [PATCH iwl-next v2 2/5] e1000e: " Jacek Kowalski
2025-07-08 19:07 ` Simon Horman
2025-07-08 8:17 ` [PATCH iwl-next v2 3/5] igb: " Jacek Kowalski
2025-07-08 19:08 ` Simon Horman
2025-07-08 8:18 ` [PATCH iwl-next v2 4/5] igc: " Jacek Kowalski
2025-07-08 19:08 ` Simon Horman
2025-07-08 8:18 ` [PATCH iwl-next v2 5/5] ixgbe: " Jacek Kowalski
2025-07-08 8:54 ` [Intel-wired-lan] " Loktionov, Aleksandr
2025-07-08 9:34 ` Jacek Kowalski
2025-07-08 10:26 ` Loktionov, Aleksandr
2025-07-08 11:13 ` Jacek Kowalski
2025-07-08 19:08 ` Simon Horman
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=20250714132114.70feff08@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=andrew+netdev@lunn.ch \
--cc=anthony.l.nguyen@intel.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=intel-wired-lan@lists.osuosl.org \
--cc=jacek@jacekk.info \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=przemyslaw.kitszel@intel.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).