* [PATCH] net: phy: seed PRNG from PHY link up jitter
@ 2025-04-29 8:50 Ahmad Fatoum
0 siblings, 0 replies; only message in thread
From: Ahmad Fatoum @ 2025-04-29 8:50 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
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.
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))
--
2.39.5
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2025-04-29 8:55 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-29 8:50 [PATCH] net: phy: seed PRNG from PHY link up jitter Ahmad Fatoum
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox