From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: Re: [PATCH] net: phy: seed PRNG from PHY link up jitter
Date: Wed, 30 Apr 2025 10:36:42 +0200 [thread overview]
Message-ID: <ca2da684-fa88-45da-a16d-6c11b5842009@pengutronix.de> (raw)
In-Reply-To: <20250429085005.3114496-1-a.fatoum@pengutronix.de>
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@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 |
prev parent reply other threads:[~2025-04-30 9:46 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-29 8:50 Ahmad Fatoum
2025-04-30 8:36 ` Ahmad Fatoum [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ca2da684-fa88-45da-a16d-6c11b5842009@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox