[PATCH net v5 5/5] net: macb: avoid dealing with endianness in macb_set_hwaddr()

Théo Lebrun posted 5 patches 5 months ago
There is a newer version of this series
[PATCH net v5 5/5] net: macb: avoid dealing with endianness in macb_set_hwaddr()
Posted by Théo Lebrun 5 months ago
bp->dev->dev_addr is of type `unsigned char *`. Casting it to a u32
pointer and dereferencing implies dealing manually with endianness,
which is error-prone.

Replace by calls to get_unaligned_le32|le16() helpers.

This was found using sparse:
   ⟩ make C=2 drivers/net/ethernet/cadence/macb_main.o
   warning: incorrect type in assignment (different base types)
      expected unsigned int [usertype] bottom
      got restricted __le32 [usertype]
   warning: incorrect type in assignment (different base types)
      expected unsigned short [usertype] top
      got restricted __le16 [usertype]
   ...

Reviewed-by: Sean Anderson <sean.anderson@linux.dev>
Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
---
 drivers/net/ethernet/cadence/macb_main.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
index fc082a7a5a313be3d58a008533c3815cb1b1639a..c16d60048185b4cb473ddfcf4633fa2f6dea20cc 100644
--- a/drivers/net/ethernet/cadence/macb_main.c
+++ b/drivers/net/ethernet/cadence/macb_main.c
@@ -271,12 +271,10 @@ static bool hw_is_gem(void __iomem *addr, bool native_io)
 
 static void macb_set_hwaddr(struct macb *bp)
 {
-	u32 bottom;
-	u16 top;
+	u32 bottom = get_unaligned_le32(bp->dev->dev_addr);
+	u16 top = get_unaligned_le16(bp->dev->dev_addr + 4);
 
-	bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
 	macb_or_gem_writel(bp, SA1B, bottom);
-	top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
 	macb_or_gem_writel(bp, SA1T, top);
 
 	if (gem_has_ptp(bp)) {

-- 
2.51.0

Re: [PATCH net v5 5/5] net: macb: avoid dealing with endianness in macb_set_hwaddr()
Posted by Karumanchi, Vineeth 5 months ago
Hi Theo,

On 9/10/2025 9:45 PM, Théo Lebrun wrote:
> bp->dev->dev_addr is of type `unsigned char *`. Casting it to a u32
> pointer and dereferencing implies dealing manually with endianness,
> which is error-prone.
> 
> Replace by calls to get_unaligned_le32|le16() helpers.
> 
> This was found using sparse:
>     ⟩ make C=2 drivers/net/ethernet/cadence/macb_main.o
>     warning: incorrect type in assignment (different base types)
>        expected unsigned int [usertype] bottom
>        got restricted __le32 [usertype]
>     warning: incorrect type in assignment (different base types)
>        expected unsigned short [usertype] top
>        got restricted __le16 [usertype]
>     ...
> 
> Reviewed-by: Sean Anderson <sean.anderson@linux.dev>
> Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
> ---
>   drivers/net/ethernet/cadence/macb_main.c | 6 ++----
>   1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
> index fc082a7a5a313be3d58a008533c3815cb1b1639a..c16d60048185b4cb473ddfcf4633fa2f6dea20cc 100644
> --- a/drivers/net/ethernet/cadence/macb_main.c
> +++ b/drivers/net/ethernet/cadence/macb_main.c
> @@ -271,12 +271,10 @@ static bool hw_is_gem(void __iomem *addr, bool native_io)
>   
>   static void macb_set_hwaddr(struct macb *bp)
>   {
> -	u32 bottom;
> -	u16 top;
> +	u32 bottom = get_unaligned_le32(bp->dev->dev_addr);
> +	u16 top = get_unaligned_le16(bp->dev->dev_addr + 4);
>   

please change the order as per reverse xmas tree.

> -	bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
>   	macb_or_gem_writel(bp, SA1B, bottom);
> -	top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
>   	macb_or_gem_writel(bp, SA1T, top);
>   
>   	if (gem_has_ptp(bp)) {
> 

-- 
🙏 Vineeth

Re: [PATCH net v5 5/5] net: macb: avoid dealing with endianness in macb_set_hwaddr()
Posted by Théo Lebrun 5 months ago
On Thu Sep 11, 2025 at 5:13 AM CEST, Karumanchi, Vineeth wrote:
> On 9/10/2025 9:45 PM, Théo Lebrun wrote:
>> @@ -271,12 +271,10 @@ static bool hw_is_gem(void __iomem *addr, bool native_io)
>>   
>>   static void macb_set_hwaddr(struct macb *bp)
>>   {
>> -	u32 bottom;
>> -	u16 top;
>> +	u32 bottom = get_unaligned_le32(bp->dev->dev_addr);
>> +	u16 top = get_unaligned_le16(bp->dev->dev_addr + 4);
>
> please change the order as per reverse xmas tree.

I had realised this before sending the patch but preferred keeping the
ordering as-is to access dev_addr+0 first then dev_addr+4.

RCT is a strict rule in net so I'll fix it in the next revision. Some
sneaky options were also considered: a spare space in the `u32 bottom`
line, express bottom using `dev_addr + 0`, or renaming variables. :-)

Thanks Vineeth,

--
Théo Lebrun, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com