Re: [PATCH V4] net: phy: Add sysfs attribute for PHY c45 identifiers.

From: Russell King (Oracle)
Date: Fri Jun 16 2023 - 10:11:20 EST


On Fri, Jun 16, 2023 at 09:54:55PM +0800, Jianhui Zhao wrote:
> >+ struct phy_c45_devid_attribute *devattr =
> >+ container_of(attr, struct phy_c45_devid_attribute, attr);
>
> >+ struct phy_c45_devid_attribute *devattr =
> >+ (struct phy_c45_devid_attribute *)container_of(attr, struct device_attribute, attr);
>
> The two conversions is not same.

You're right...

> One is convert "struct device_attribute" to "struct phy_c45_devid_attribute",
> and another one is convert "struct attribute" to "struct phy_c45_devid_attribute".
> The second one must cast "struct device_attribute" returned from container_of
> to "struct phy_c45_devid_attribute".

... but this isn't entirely correct.

Doing it properly:

struct phy_c45_devid_attribute *devattr =
container_of(attr, struct phy_c45_devid_attribute, attr.attr);

Number one rule with C programming: if you have to cast, you're
probably doing something wrong, so always look to see if there's an
alternative solution that doesn't involve casts.

Casts are one of the reasons why programming errors happen - it stops
the compiler being able to perform proper type checking. Always avoid
them as much as possible.

--
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!