2020-01-31 11:49:36

by Radhey Shyam Pandey

[permalink] [raw]
Subject: [PATCH v3 -next 4/4] net: emaclite: Fix restricted cast warning of sparse

Explicitly cast xemaclite_readl return value when it's passed to ntohl.
Fixes below reported sparse warnings:

xilinx_emaclite.c:411:24: sparse: sparse: cast to restricted __be32
xilinx_emaclite.c:420:36: sparse: sparse: cast to restricted __be32

Signed-off-by: Radhey Shyam Pandey <[email protected]>
Reported-by: kbuild test robot <[email protected]>
---
drivers/net/ethernet/xilinx/xilinx_emaclite.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/xilinx/xilinx_emaclite.c b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
index 96e9d21..3273d4f 100644
--- a/drivers/net/ethernet/xilinx/xilinx_emaclite.c
+++ b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
@@ -408,7 +408,8 @@ static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data, int maxlen)

/* Get the protocol type of the ethernet frame that arrived
*/
- proto_type = ((ntohl(xemaclite_readl(addr + XEL_HEADER_OFFSET +
+ proto_type = ((ntohl((__force __be32)xemaclite_readl(addr +
+ XEL_HEADER_OFFSET +
XEL_RXBUFF_OFFSET)) >> XEL_HEADER_SHIFT) &
XEL_RPLR_LENGTH_MASK);

@@ -417,7 +418,7 @@ static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data, int maxlen)
*/
if (proto_type > ETH_DATA_LEN) {
if (proto_type == ETH_P_IP) {
- length = ((ntohl(xemaclite_readl(addr +
+ length = ((ntohl((__force __be32)xemaclite_readl(addr +
XEL_HEADER_IP_LENGTH_OFFSET +
XEL_RXBUFF_OFFSET)) >>
XEL_HEADER_SHIFT) &
--
2.7.4


2020-01-31 13:50:56

by Andrew Lunn

[permalink] [raw]
Subject: Re: [PATCH v3 -next 4/4] net: emaclite: Fix restricted cast warning of sparse

On Fri, Jan 31, 2020 at 05:17:50PM +0530, Radhey Shyam Pandey wrote:
> Explicitly cast xemaclite_readl return value when it's passed to ntohl.
> Fixes below reported sparse warnings:
>
> xilinx_emaclite.c:411:24: sparse: sparse: cast to restricted __be32
> xilinx_emaclite.c:420:36: sparse: sparse: cast to restricted __be32
>
> Signed-off-by: Radhey Shyam Pandey <[email protected]>
> Reported-by: kbuild test robot <[email protected]>
> ---
> drivers/net/ethernet/xilinx/xilinx_emaclite.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/net/ethernet/xilinx/xilinx_emaclite.c b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> index 96e9d21..3273d4f 100644
> --- a/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> +++ b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> @@ -408,7 +408,8 @@ static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data, int maxlen)
>
> /* Get the protocol type of the ethernet frame that arrived
> */
> - proto_type = ((ntohl(xemaclite_readl(addr + XEL_HEADER_OFFSET +
> + proto_type = ((ntohl((__force __be32)xemaclite_readl(addr +
> + XEL_HEADER_OFFSET +
> XEL_RXBUFF_OFFSET)) >> XEL_HEADER_SHIFT) &
> XEL_RPLR_LENGTH_MASK);
>
> @@ -417,7 +418,7 @@ static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data, int maxlen)
> */
> if (proto_type > ETH_DATA_LEN) {
> if (proto_type == ETH_P_IP) {
> - length = ((ntohl(xemaclite_readl(addr +
> + length = ((ntohl((__force __be32)xemaclite_readl(addr +
> XEL_HEADER_IP_LENGTH_OFFSET +
> XEL_RXBUFF_OFFSET)) >>
> XEL_HEADER_SHIFT) &

If i understand this code correctly, you need the ntohl because you
are poking around inside the packet. All the other uses of
xemaclite_readl() are for descriptors etc.

It would be cleaner if you defined a xemaclite_readlbe32. If you use
ioread32be() it will do the endinness swap for you, so you don't need
the ntohl() and the horrible cast.

Andrew

2020-02-10 14:55:48

by Radhey Shyam Pandey

[permalink] [raw]
Subject: RE: [PATCH v3 -next 4/4] net: emaclite: Fix restricted cast warning of sparse

> -----Original Message-----
> From: Andrew Lunn <[email protected]>
> Sent: Friday, January 31, 2020 7:19 PM
> To: Radhey Shyam Pandey <[email protected]>
> Cc: [email protected]; [email protected]; linux-
> [email protected]; Anirudha Sarangi <[email protected]>; Michal Simek
> <[email protected]>; [email protected];
> [email protected]; John Linn <[email protected]>; linux-arm-
> [email protected]
> Subject: Re: [PATCH v3 -next 4/4] net: emaclite: Fix restricted cast warning of
> sparse
>
> On Fri, Jan 31, 2020 at 05:17:50PM +0530, Radhey Shyam Pandey wrote:
> > Explicitly cast xemaclite_readl return value when it's passed to ntohl.
> > Fixes below reported sparse warnings:
> >
> > xilinx_emaclite.c:411:24: sparse: sparse: cast to restricted __be32
> > xilinx_emaclite.c:420:36: sparse: sparse: cast to restricted __be32
> >
> > Signed-off-by: Radhey Shyam Pandey <[email protected]>
> > Reported-by: kbuild test robot <[email protected]>
> > ---
> > drivers/net/ethernet/xilinx/xilinx_emaclite.c | 5 +++--
> > 1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> > index 96e9d21..3273d4f 100644
> > --- a/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> > +++ b/drivers/net/ethernet/xilinx/xilinx_emaclite.c
> > @@ -408,7 +408,8 @@ static u16 xemaclite_recv_data(struct net_local
> *drvdata, u8 *data, int maxlen)
> >
> > /* Get the protocol type of the ethernet frame that arrived
> > */
> > - proto_type = ((ntohl(xemaclite_readl(addr + XEL_HEADER_OFFSET +
> > + proto_type = ((ntohl((__force __be32)xemaclite_readl(addr +
> > + XEL_HEADER_OFFSET +
> > XEL_RXBUFF_OFFSET)) >> XEL_HEADER_SHIFT) &
> > XEL_RPLR_LENGTH_MASK);
> >
> > @@ -417,7 +418,7 @@ static u16 xemaclite_recv_data(struct net_local
> *drvdata, u8 *data, int maxlen)
> > */
> > if (proto_type > ETH_DATA_LEN) {
> > if (proto_type == ETH_P_IP) {
> > - length = ((ntohl(xemaclite_readl(addr +
> > + length = ((ntohl((__force __be32)xemaclite_readl(addr
> +
> > XEL_HEADER_IP_LENGTH_OFFSET +
> > XEL_RXBUFF_OFFSET)) >>
> > XEL_HEADER_SHIFT) &
>
> If i understand this code correctly, you need the ntohl because you
> are poking around inside the packet. All the other uses of
> xemaclite_readl() are for descriptors etc.
>
> It would be cleaner if you defined a xemaclite_readlbe32. If you use
> ioread32be() it will do the endinness swap for you, so you don't need
> the ntohl() and the horrible cast.
Thanks for the review. Yes, defining xemaclite_readlbe32 would eliminate the
cast need. I will address it in the next version.

>
> Andrew