Skip to contentSkip to conversation

hwrng: bcm2835: Fix hwrng throughput regression

PATCH In review Stefan Wahren 4 participants 6 messages Sep 5, 2023 – Oct 4, 2023

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hwrng: bcm2835: Fix hwrng throughput regression

The last RCU stall fix caused a massive throughput regression of the hwrng on Raspberry Pi 0 - 3. hwrng_msleep doesn't sleep precisely enough and usleep_range doesn't allow scheduling. So try to restore the best possible throughput by introducing hwrng_yield which interruptable sleeps for one jiffy.

Some performance measurements on Raspberry Pi 3B+ (arm64/defconfig):

sudo dd if=/dev/hwrng of=/dev/null count=1 bs=10000

cpu_relax ~138025 Bytes / sec
hwrng_msleep(1000) ~13 Bytes / sec
hwrng_yield ~2510 Bytes / sec

Fixes 96cb9d055445 ("hwrng: bcm2835 - use hwrng_msleep() instead of cpu_relax()")

---

Changes in V2:
- introduce hwrng_yield and use it

 drivers/char/hw_random/bcm2835-rng.c | 2 +-
 drivers/char/hw_random/core.c        | 6 ++++++
 include/linux/hw_random.h            | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/char/hw_random/bcm2835-rng.c +1 -1
index e98fcac578d6..634eab4776f3 100644
@@ -71,7 +71,7 @@ static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
 	while ((rng_readl(priv, RNG_STATUS) >> 24) == 0) {
 		if (!wait)
 			return 0;
-		hwrng_msleep(rng, 1000);
+		hwrng_yield(rng);
 	}
 
 	num_words = rng_readl(priv, RNG_STATUS) >> 24;
drivers/char/hw_random/core.c +6 -0
index f34d356fe2c0..599a4bc2c548 100644
@@ -679,6 +679,12 @@ long hwrng_msleep(struct hwrng *rng, unsigned int msecs)
 }
 EXPORT_SYMBOL_GPL(hwrng_msleep);
 
+long hwrng_yield(struct hwrng *rng)
+{
+	return wait_for_completion_interruptible_timeout(&rng->dying, 1);
+}
+EXPORT_SYMBOL_GPL(hwrng_yield);
+
 static int __init hwrng_modinit(void)
 {
 	int ret;
include/linux/hw_random.h +1 -0
index 8a3115516a1b..136e9842120e 100644
@@ -63,5 +63,6 @@ extern void hwrng_unregister(struct hwrng *rng);
 extern void devm_hwrng_unregister(struct device *dve, struct hwrng *rng);
 
 extern long hwrng_msleep(struct hwrng *rng, unsigned int msecs);
+extern long hwrng_yield(struct hwrng *rng);
 
 #endif /* LINUX_HWRANDOM_H_ */

--
2.34.1

Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines · Stefan Wahren

The last RCU stall fix caused a massive throughput regression of the hwrng on Raspberry Pi 0 - 3. hwrng_msleep doesn't sleep precisely enough and usleep_range doesn't allow scheduling. So try to restore the best possible throughput by introducing hwrng_yield which interruptable sleeps for one jiffy.

Some performance measurements on Raspberry Pi 3B+ (arm64/defconfig):

sudo dd if=/dev/hwrng of=/dev/null count=1 bs=10000

Quoted 7 lines

cpu_relax ~138025 Bytes / sec
hwrng_msleep(1000) ~13 Bytes / sec
hwrng_yield ~2510 Bytes / sec

Fixes 96cb9d055445 ("hwrng: bcm2835 - use hwrng_msleep() instead of cpu_relax()")

Thanks, looks good to me.

maintainer
#2 lore raw
In reply to Stefan Wahren
Quoted 19 lines · Stefan Wahren

The last RCU stall fix caused a massive throughput regression of the hwrng on Raspberry Pi 0 - 3. hwrng_msleep doesn't sleep precisely enough and usleep_range doesn't allow scheduling. So try to restore the best possible throughput by introducing hwrng_yield which interruptable sleeps for one jiffy.

Some performance measurements on Raspberry Pi 3B+ (arm64/defconfig):

sudo dd if=/dev/hwrng of=/dev/null count=1 bs=10000

cpu_relax ~138025 Bytes / sec
hwrng_msleep(1000) ~13 Bytes / sec
hwrng_yield ~2510 Bytes / sec

Fixes 96cb9d055445 ("hwrng: bcm2835 - use hwrng_msleep() instead of cpu_relax()")

---

Quoted 7 lines

Changes in V2:
- introduce hwrng_yield and use it

 drivers/char/hw_random/bcm2835-rng.c | 2 +-
 drivers/char/hw_random/core.c        | 6 ++++++
 include/linux/hw_random.h            | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw
Quoted 23 lines · Herbert Xu
Quoted 22 lines · Stefan Wahren

The last RCU stall fix caused a massive throughput regression of the hwrng on Raspberry Pi 0 - 3. hwrng_msleep doesn't sleep precisely enough and usleep_range doesn't allow scheduling. So try to restore the best possible throughput by introducing hwrng_yield which interruptable sleeps for one jiffy.

Some performance measurements on Raspberry Pi 3B+ (arm64/defconfig):

sudo dd if=/dev/hwrng of=/dev/null count=1 bs=10000

cpu_relax ~138025 Bytes / sec
hwrng_msleep(1000) ~13 Bytes / sec
hwrng_yield ~2510 Bytes / sec

Fixes 96cb9d055445 ("hwrng: bcm2835 - use hwrng_msleep() instead of cpu_relax()")

---

Changes in V2:
- introduce hwrng_yield and use it

Quoted 6 lines
Quoted 4 lines
 drivers/char/hw_random/bcm2835-rng.c | 2 +-
 drivers/char/hw_random/core.c        | 6 ++++++
 include/linux/hw_random.h            | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)

Patch applied. Thanks.

Hi Herbert, I there a strong reason why you merged this to what from here looks like the branch that targets the next merge window? The patch fixes a regression introduced during the last 12 months and thus normally should not wait for the next merge window. For details see "Expectations and best practices for fixing regressions" in Documentation/process/handling-regressions.rst; or if you want to hear it from Linus directly, check these out:
https://lore.kernel.org/all/CAHk-=wis_qQy4oDNynNKi5b7Qhosmxtoj1jxo5wmB6SRUwQUBQ@mail.gmail.com/
https://lore.kernel.org/all/CAHk-=wgD98pmSK3ZyHk_d9kZ2bhgN6DuNZMAJaV0WTtbkf=RDw@mail.gmail.com/

Ciao, Thorsten (wearing his 'the Linux kernel's regression tracker' hat) --
Everything you wanna know about Linux kernel regression tracking:
https://linux-regtracking.leemhuis.info/about/#tldr
If I did something stupid, please tell me, as explained on that page.

maintainer
#4 lore raw
Quoted 10 lines · Linux regression tracking (Thorsten Leemhuis)

Hi Herbert, I there a strong reason why you merged this to what from here looks like the branch that targets the next merge window? The patch fixes a regression introduced during the last 12 months and thus normally should not wait for the next merge window. For details see "Expectations and best practices for fixing regressions" in Documentation/process/handling-regressions.rst; or if you want to hear it from Linus directly, check these out:
https://lore.kernel.org/all/CAHk-=wis_qQy4oDNynNKi5b7Qhosmxtoj1jxo5wmB6SRUwQUBQ@mail.gmail.com/
https://lore.kernel.org/all/CAHk-=wgD98pmSK3ZyHk_d9kZ2bhgN6DuNZMAJaV0WTtbkf=RDw@mail.gmail.com/

This is a one-year-old regression, and the fix is not a trivial reversion. So there is no urgency in pushing this out.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#5 lore raw

[CCing Linus in case he wants to voice in to clarify how fixes for regressions that are nearly a year old should be handled]

Quoted 12 lines · Herbert Xu
Quoted 10 lines · Linux regression tracking (Thorsten Leemhuis)

Hi Herbert, I there a strong reason why you merged this to what from here looks like the branch that targets the next merge window? The patch fixes a regression introduced during the last 12 months and thus normally should not wait for the next merge window. For details see "Expectations and best practices for fixing regressions" in Documentation/process/handling-regressions.rst; or if you want to hear it from Linus directly, check these out:
https://lore.kernel.org/all/CAHk-=wis_qQy4oDNynNKi5b7Qhosmxtoj1jxo5wmB6SRUwQUBQ@mail.gmail.com/
https://lore.kernel.org/all/CAHk-=wgD98pmSK3ZyHk_d9kZ2bhgN6DuNZMAJaV0WTtbkf=RDw@mail.gmail.com/

Quoted 1 line

This is a one-year-old regression,

From day the culprit was authored and merged to mainline: yes, nearly; but it's only roughly 9 months from the release of 6.1 till the report and the posting of the fix you applied.

But that's splitting hairs; pretty sure Linus meant those 12 months just as a rough estimate. Other things also matter, among them how many people are affected and how risky the fix is; but to my untrained eyes this one doesn't look very dangerous (but I of course might be totally wrong with that).

Quoted 1 line

and the fix is not a trivial reversion.

What makes you think the strategy outlined earlier is only relevant for reverts? Yes, one of those links above points to a discussion about a revert, but the other is not about it.

Quoted 1 line

So there is no urgency in pushing this out.

I don't care to much about this particular case, but it would help my work to know how Linus think about it. That's why I CCed him, that way he can comment on this if he wants to.

Linus, FWIW, the thread starts here with the fix for the regression:
https://lore.kernel.org/all/[email protected]/

Is also in -next for a while as b58a36008bfa1a. Quoting from there below for convenience.

Ciao, Thorsten

"""> From b58a36008bfa1aadf55f516bcbfae40c779eb54b Mon Sep 17 00:00:00 2001

Quoted 64 lines

From: Stefan Wahren <[email protected]>
Date: Wed, 6 Sep 2023 01:27:57 +0200
Subject: hwrng: bcm2835 - Fix hwrng throughput regression

The last RCU stall fix caused a massive throughput regression of the hwrng on Raspberry Pi 0 - 3. hwrng_msleep doesn't sleep precisely enough and usleep_range doesn't allow scheduling. So try to restore the best possible throughput by introducing hwrng_yield which interruptable sleeps for one jiffy.

Some performance measurements on Raspberry Pi 3B+ (arm64/defconfig):

sudo dd if=/dev/hwrng of=/dev/null count=1 bs=10000

cpu_relax ~138025 Bytes / sec
hwrng_msleep(1000) ~13 Bytes / sec
hwrng_yield ~2510 Bytes / sec

Fixes 96cb9d055445 ("hwrng: bcm2835 - use hwrng_msleep() instead of cpu_relax()")
---
 drivers/char/hw_random/bcm2835-rng.c | 2 +-
 drivers/char/hw_random/core.c        | 6 ++++++
 include/linux/hw_random.h            | 1 +
 3 files changed, 8 insertions(+), 1 deletion(-)
drivers/char/hw_random/bcm2835-rng.c +1 -1
index eb04b12f9f01b..b03e803006275 100644
@@ -70,7 +70,7 @@ static int bcm2835_rng_read(struct hwrng *rng, void *buf, size_t max,
 	while ((rng_readl(priv, RNG_STATUS) >> 24) == 0) {
 		if (!wait)
 			return 0;
-		hwrng_msleep(rng, 1000);
+		hwrng_yield(rng);
 	}
 
 	num_words = rng_readl(priv, RNG_STATUS) >> 24;
drivers/char/hw_random/core.c +6 -0
index e3598ec9cfca8..420f155d251fb 100644
@@ -678,6 +678,12 @@ long hwrng_msleep(struct hwrng *rng, unsigned int msecs)
 }
 EXPORT_SYMBOL_GPL(hwrng_msleep);
 
+long hwrng_yield(struct hwrng *rng)
+{
+	return wait_for_completion_interruptible_timeout(&rng->dying, 1);
+}
+EXPORT_SYMBOL_GPL(hwrng_yield);
+
 static int __init hwrng_modinit(void)
 {
 	int ret;
include/linux/hw_random.h +0 -0
index 8a3115516a1ba..136e9842120e8 100644

include/linux/hw_random.h extern void hwrng_unregister(struct hwrng *rng); · line 68

Quoted 6 lines
include/linux/hw_random.h +1 -0
@@ -63,5 +63,6 @@ extern void hwrng_unregister(struct hwrng *rng);
 extern void devm_hwrng_unregister(struct device *dve, struct hwrng *rng);
 
 extern long hwrng_msleep(struct hwrng *rng, unsigned int msecs);
+extern long hwrng_yield(struct hwrng *rng);
 
 #endif /* LINUX_HWRANDOM_H_ */

"""