Subject: Re: [PATCH net-next] r8169: fix ntohs/htons sparse warnings

From: Al Viro <[email-masked]>
Date: Mon, 1 Jul 2019 20:56:21 +0100

On Mon, Jul 01, 2019 at 09:35:28PM +0200, Heiner Kallweit wrote:
> Sparse complains about casting to/from restricted __be16. Fix this.

Fix what, exactly?  Force-cast is not a fix - it's "STFU, I know
better, it's really correct" to sparse.  Which may or may not
match the reality, but it definitely requires more in way of
commit message than "sparse says it's wrong; shut it up".

>  static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
> @@ -1537,7 +1537,7 @@ static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
>  
>  	if (opts2 & RxVlanTag)
>  		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
> -				       ntohs(opts2 & 0xffff));
> +				       ntohs((__force __be16)(opts2 & 0xffff)));
>  }

Should that be ntohs at all?  What behaviour is correct on big-endian host?

AFAICS, in that code opts2 comes from little-endian 32bit.  It's converted to
host-endian, lower 16 bits (i.e. the first two octets in memory) are then
fed to ntohs.  Suppose we had in-core value stored as A0, A1, A2, A3.
On little-endian that code will yield A0 * 256 + A1, treated as host-endian.
On big-endian the same will yield A1 * 256 + A0.  Is that actually correct?

The code dealing with the value passed to __vlan_hwaccel_put_tag() as the
third argument treats it as a host-endian integer.  So... Has anyone
tested that code on b-e host?  Should that ntohs() actually be swab16(),
yielding (on any host) the same value we currently get for l-e hosts only?