[PATCH] net: phy: seed PRNG from PHY link up jitter

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Apr 30 01:36:42 PDT 2025


Hi,

On 4/29/25 10:50, Ahmad Fatoum wrote:
> The new Xorshift* RNG is seeded only from the cycle counter at startup,
> which is much less entropy for the MAC address generation use case,
> which used the nanosecond timestamp at generation time before the switch
> to Xorshift*.
> 
> The nice thing about our new PRNG implementation is that we can call
> srand_xor multiple times to keep adding entropy, so let's do that after
> each PHY link up.
> 
> This should ensure that MAC addresses are sufficiently random again.

I talked off-list with Sascha about this and there are other callsites
to random_ether_addr that can happen before Ethernet link is up.

I will revisit this patch later.

Cheers,
Ahmad

> 
> Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
> ---
> I think this is a candidate for master, because especially network
> booted barebox can now easily end up with the same seed on two
> susbequent runs (observed on i.MX6).
> ---
>  common/clock.c        | 7 +++++++
>  drivers/net/phy/phy.c | 7 +++++--
>  include/clock.h       | 8 ++++++++
>  3 files changed, 20 insertions(+), 2 deletions(-)
> 
> diff --git a/common/clock.c b/common/clock.c
> index 517116e3b9a3..931a96865140 100644
> --- a/common/clock.c
> +++ b/common/clock.c
> @@ -207,6 +207,13 @@ void mdelay_non_interruptible(unsigned long msecs)
>  }
>  EXPORT_SYMBOL(mdelay_non_interruptible);
>  
> +#if IN_PROPER
> +void clocksource_srand(void)
> +{
> +	srand_xor(current_clock->read() & current_clock->mask);
> +}
> +#endif
> +
>  int init_clock(struct clocksource *cs)
>  {
>  	if (current_clock && cs->priority <= current_clock->priority)
> diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
> index 2795d09918d5..d9f50f45f441 100644
> --- a/drivers/net/phy/phy.c
> +++ b/drivers/net/phy/phy.c
> @@ -68,11 +68,14 @@ int phy_update_status(struct phy_device *phydev)
>  	if (phydev->adjust_link)
>  		phydev->adjust_link(edev);
>  
> -	if (phydev->link)
> +	if (phydev->link) {
>  		dev_info(&edev->dev, "%dMbps %s duplex link detected\n",
>  				phydev->speed, phydev->duplex ? "full" : "half");
> -	else if (oldlink)
> +		/* Add time to establish link up to RNG state */
> +		clocksource_srand();
> +	} else if (oldlink) {
>  		dev_info(&edev->dev, "link down\n");
> +	}
>  
>  	return 0;
>  }
> diff --git a/include/clock.h b/include/clock.h
> index 03a38911a7a7..e28870b1e84b 100644
> --- a/include/clock.h
> +++ b/include/clock.h
> @@ -43,6 +43,14 @@ void udelay(unsigned long usecs);
>  void mdelay(unsigned long msecs);
>  void mdelay_non_interruptible(unsigned long msecs);
>  
> +#if IN_PROPER
> +void clocksource_srand(void);
> +#else
> +static inline void clocksource_srand(void)
> +{
> +}
> +#endif
> +
>  #define SECOND ((uint64_t)(1000 * 1000 * 1000))
>  #define MSECOND ((uint64_t)(1000 * 1000))
>  #define USECOND ((uint64_t)(1000))

-- 
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |




More information about the barebox mailing list