From patchwork Tue Sep 5 23:27:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Wahren X-Patchwork-Id: 13375186 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5DB99CCF9EA for ; Tue, 5 Sep 2023 23:29:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=J2FmUlyA3fCiezqCGIuu4PeGTXO2f5UL9YUaiFnlL4A=; b=dntTUDsLesYeJj FoEePQ/Uh7zNW7wvlI0YOst/IcP8uEUPvlHKmE74p11cseyoNJqmZYnv3IJS2SMTaRkh2eIYrw+Qz 22fONjNFyViGY9R3wDwcugqAkLtgl4H2wjvsv/fXsDP9AbcL1NcKP6cri7Vn9rEPPK77/73y1Ryop 5itm2UzEij36PnQWsgrLTKmYCBNqIJq+pX7Wcerh1Z6b0iA0ujO1AvmeoYOuNwE8510RmUHDwDGZH Ia5MOCPQnysneUs/L3Rxj2lSjs/CnAMSeik5ShcPmqI5AYlnc2vicDkMXg/ZeVjLfCOAs8+BZJlRu RQ2cr34I5h1PjBju5DBQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qdfTM-006tWX-1N; Tue, 05 Sep 2023 23:28:40 +0000 Received: from mout.gmx.net ([212.227.15.19]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qdfTJ-006tVt-0u for linux-arm-kernel@lists.infradead.org; Tue, 05 Sep 2023 23:28:39 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=s31663417; t=1693956502; x=1694561302; i=wahrenst@gmx.net; bh=GY+cKU9ZqUyJgDRu4Y/gsgI1xUTfX+iEeHDS9Zf7BZk=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date; b=ZqGSu1W6FS9RIsoJ+oa6rHJbQukBFXZomkdTj/gzJgMh7myuxLLl2v+5ncrZgKY6fHtmL5F /uSLOkcjgjzYUKFnY6t9X1eLqXzsGFQ6risU1gZ+hW9bZwVohVdQokupkvvNFG/brZyNOfNLl /rBPU68fTqsal+b29FMa90CLkkUMZM+oBCScmNr8AgXA0bMBBGUvcwkMOW6sMGVxWrZ3BX8/p aRehpiBTg4syOPN5Yw9aZ655EEScKxwGyzKO9vbc190KJrQOSKI4vUNMrTiYF1UbLOp0+/6Ym RrApSXYo7Bb9lIL1rG4i/V7ZTJnrWlyeS5r79ewoYYbUNerqFNhw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from stefanw-SCHENKER ([37.4.248.43]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MTzf6-1qCkxo2U42-00R2rh; Wed, 06 Sep 2023 01:28:22 +0200 From: Stefan Wahren To: Olivia Mackall , Herbert Xu , Florian Fainelli , Ray Jui , Scott Branden Cc: "Jason A. Donenfeld" , Mark Brown , linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, bcm-kernel-feedback-list@broadcom.com, Stefan Wahren Subject: [PATCH V2] hwrng: bcm2835: Fix hwrng throughput regression Date: Wed, 6 Sep 2023 01:27:57 +0200 Message-Id: <20230905232757.36459-1-wahrenst@gmx.net> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Provags-ID: V03:K1:PN+1I8AC+BmOLLuSKVSIMR5owJvnEYSRbHlwT7DLRg+pkbPVCYl 9OQEjbhXHMgVeBFSiIstVfiiFpGkxncyeF+MaHnz5biVmCv3Drcs8Ksn1RROhcYX9mNXVeh aOXUEMbEy0zoIVFn8zxuJrt+O3IzHZ8WLjDUSK94fCaTIQ7G6Bu1tQXedm8SWPZtL7VdMxE BdRIN8JPn21wB5Gu3uqWg== UI-OutboundReport: notjunk:1;M01:P0:0DPy7gFzeOo=;CqUeXnEDfQBdqmK8N2x6XvUbPBo TL7TobCWBQPKl/IBZjEWBINdbd5K47cocJ5rjLQmrTjKqGUu811fj8WlMP6KznccwluDo3s+y ovXw7m5QgSA2qaDizUVuknCJMiZKVOSsIfSftHrqcPLbAqYwfyyVkEHXB6qSeENlmE4qUv2gz 6XJgyiTzpTMNL0B0FUYBCn7V0P/hZU10rVOZZ2Zqs3ByDMmlGj62rNnMO5tVLOs4gXerEZr2z UNdapVA32kQIScXBaxARXUPMIqcsGAYFmx+JDkv05p0gujQ0PcrBJoTk/zzhnOByJ8s5lWSht mD7/r4AC1M9DpS9Lr1KlEuqQyzRs4pLqvKm3DBZTblkPytpN66KtV28T6h56/5oUqxvlbtTkV ZaA4ybqG9OjiMiJJLPcY9KJRIQIbmTrHGPaMDj/oowHnfJNmhMRuJQqvVxVlsXyQK39/99EjQ 0P5J0c5k8stWWGijlHhAfJOFf5jKm49+7Dm+Wuaei7v54DOOuPFPzeueRFKJAl+jvXTtigjOX ojIG93sbV+CkA8439gcdM5AyjlYYODM8731499tHfMvROd0v1UOLJHMQIGEGd2rDSXkw1QL2v eHyhJIcKZIrjBFFvNkxc4lrG8rF2+Of+qklbqP6qh6KNgcKprXs48cRpa3aDZDXhYYI0XPG8x gsUEawe65qfxUPZQD1nEUWqtiyj5mAVy8FY/Ovq6k981Np4GINUb0XnqSiCi8ptVaqq6xMbmR gah+ha3EZl2GMhPVVFlCuq2AP3ccF29udrdGH+tRV1sEcS5sU16vUD1Db1pH30wsr8rn5DlQQ s3zRl1fouMwsdJcK5TH/jDIREJWy45RVKmpm6dwP8/CsVX44AkIkBVXnTwVn+w//P2ZPTsB/l 2n6s8DYcPDeDYmyESfi5/WHlAsBLlnqhO4Owz0O2/4GCbdisl+/jwUW1vKPXVizSwbo0YDU+3 7aEX9A== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230905_162837_757444_C9B4E0A0 X-CRM114-Status: GOOD ( 13.92 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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()") Link: https://lore.kernel.org/linux-arm-kernel/bc97ece5-44a3-4c4e-77da-2db3eb66b128@gmx.net/ Signed-off-by: Stefan Wahren Reviewed-by: Jason A. Donenfeld --- 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(-) -- 2.34.1 diff --git a/drivers/char/hw_random/bcm2835-rng.c b/drivers/char/hw_random/bcm2835-rng.c index e98fcac578d6..634eab4776f3 100644 --- a/drivers/char/hw_random/bcm2835-rng.c +++ b/drivers/char/hw_random/bcm2835-rng.c @@ -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; diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c index f34d356fe2c0..599a4bc2c548 100644 --- a/drivers/char/hw_random/core.c +++ b/drivers/char/hw_random/core.c @@ -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; diff --git a/include/linux/hw_random.h b/include/linux/hw_random.h index 8a3115516a1b..136e9842120e 100644 --- a/include/linux/hw_random.h +++ b/include/linux/hw_random.h @@ -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_ */