From patchwork Tue Dec 12 16:36:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lukasz Stelmach X-Patchwork-Id: 10107857 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 84E03602B3 for ; Tue, 12 Dec 2017 16:37:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7544E2947B for ; Tue, 12 Dec 2017 16:37:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 67F07294AF; Tue, 12 Dec 2017 16:37:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0639B2947B for ; Tue, 12 Dec 2017 16:37:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752204AbdLLQhD (ORCPT ); Tue, 12 Dec 2017 11:37:03 -0500 Received: from mailout2.w1.samsung.com ([210.118.77.12]:40528 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752464AbdLLQgR (ORCPT ); Tue, 12 Dec 2017 11:36:17 -0500 Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20171212163614euoutp0259e5db59f87d05fae89c62d27349b7f7~-mVFlVAjd3187731877euoutp02Y; Tue, 12 Dec 2017 16:36:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20171212163614euoutp0259e5db59f87d05fae89c62d27349b7f7~-mVFlVAjd3187731877euoutp02Y DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1513096574; bh=+ghKX4Gq5hgDMRvLskO1aiaD20HiblHctRc3aoAl3vY=; h=From:To:Cc:Subject:Date:In-reply-to:In-reply-to:References:From; b=gAONdqVKDdNUVCbzrxlA4Q6X6Fx4NkvTwUy93A7v+9EvRWDYym2b9luxu8nc+K/gW aZDp3Zv5ppyw0W75FV9I+GLVq5ReBh9f7n27ESfWsS/mnVRJh/d/16glNsjjrNnPAq ZmFIJVO3pf0f2IHOQsjG0SP1XKVqxi9aMAhWPdR8= Received: from eusmges2.samsung.com (unknown [203.254.199.241]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20171212163613eucas1p2e0d47271c7c0a870abc57df384ad2e7b~-mVE3vpN52507925079eucas1p2H; Tue, 12 Dec 2017 16:36:13 +0000 (GMT) Received: from eucas1p2.samsung.com ( [182.198.249.207]) by eusmges2.samsung.com (EUCPMTA) with SMTP id F1.CB.12907.D75003A5; Tue, 12 Dec 2017 16:36:13 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20171212163613eucas1p2f27c9c0924f94ad9a26b6870fcf5fe45~-mVEL00ML0378903789eucas1p2O; Tue, 12 Dec 2017 16:36:13 +0000 (GMT) X-AuditID: cbfec7f1-f793a6d00000326b-6b-5a30057dc540 Received: from eusync4.samsung.com ( [203.254.199.214]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id 3C.2D.20118.D75003A5; Tue, 12 Dec 2017 16:36:13 +0000 (GMT) MIME-version: 1.0 Content-transfer-encoding: 8BIT Content-type: text/plain; charset="UTF-8" Received: from localhost ([106.116.147.110]) by eusync4.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0P0U00B1PWSCJH60@eusync4.samsung.com>; Tue, 12 Dec 2017 16:36:13 +0000 (GMT) From: =?UTF-8?q?=C5=81ukasz=20Stelmach?= To: Krzysztof Kozlowski , robh+dt@kernel.org, Stephan Mueller , Herbert Xu , "David S . Miller" , Kukjin Kim , linux-crypto@vger.kernel.org, linux-samsung-soc@vger.kernel.org, linux-kernel@vger.kernel.org Cc: =?UTF-8?q?=C5=81ukasz=20Stelmach?= , Marek Szyprowski , Bartlomiej Zolnierkiewicz Subject: [PATCH v3 4/4] crypto: exynos - Introduce mutex to prevent concurrent access to hardware Date: Tue, 12 Dec 2017 17:36:07 +0100 Message-id: <20171212163607.2985-5-l.stelmach@samsung.com> X-Mailer: git-send-email 2.11.0 In-reply-to: <20171212163607.2985-1-l.stelmach@samsung.com> In-reply-to: <20171211140623.7673-1-l.stelmach@samsung.com> Organization: Samsung R&D Institute Poland X-Brightmail-Tracker: H4sIAAAAAAAAA01SbUgTYRzvubvdzuHqmiv/mGSNLApMjT4cWqGSOOhLRF8yoy47lrhN2U2Z wUglVypMzV7WiF4ss1a2XDbSaOYcTQtcOiM1NF+qDxaVaVSglreb1Lff//97eZ7nx0PhijZJ DJWnN3IGPatVkTLC/fx3IMEsScpO+jK4iWmxOSXM5cApgqmeimVqJj/hTCDwQMoMeW8j5t3o b4wJtl8mGVvAgzHNvhEpU/HUJ2XOv2wm0iLV3wcGMHXrnSFM7X4Wr3Y5Kkm1tdWB1DOutXvJ bNmOY5w2r5gzJO46Ijve4xmWFNbGmJrffiZKkXNVFaIooLfDrRGsCkUswtXwatRJViEZpaAb EczbbJg4zCCobxwmRNV2aOgYChO3EMy6LpECIadXwq/6UUJIxek48PXnC2uc3gyPnjilon4W QW+wIqQn6XSoaeyWCISS9mAwWXceCQMupHa6e0KqKDoXnDcmcQETdDy872hAwglyOgWeuSXi jeKg3dIXwhF0Kswu3MeWcKX/dChmBb0F7pW/IYR8oGukMP+1jhTNu2GityX8tCiY8rdKRRwL wfr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6Bu5R7vXP873njO7yPFpgdqZrNooyef+75yZ+yKDS7keFX/9c3PRsfssNlt3HK16Fmva3fwl /rLTirfm6pET9T2Usv8EpUreiG2cceysf5ei3555+1flefsd2ushJ7uUR66o5EQRo/OhnCVM Zu8uFLe9nCfx/arp2m6Nc0u7r+XVrWZb0fVPiaU4I9FQi7moOBEA0Lim860CAAA= X-CMS-MailID: 20171212163613eucas1p2f27c9c0924f94ad9a26b6870fcf5fe45 X-Msg-Generator: CA CMS-TYPE: 201P X-CMS-RootMailID: 20171212163613eucas1p2f27c9c0924f94ad9a26b6870fcf5fe45 X-RootMTR: 20171212163613eucas1p2f27c9c0924f94ad9a26b6870fcf5fe45 References: <20171212163607.2985-1-l.stelmach@samsung.com> Sender: linux-samsung-soc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-samsung-soc@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Hardware operations like reading random numbers and setting a seed need to be conducted in a single thread. Therefore a mutex is required to prevent multiple threads (processes) from accessing the hardware at the same time. The sequence of mutex_lock() and mutex_unlock() in the exynos_rng_reseed() function enables switching between different threads waiting for the driver to generate random numbers for them. Signed-off-by: Ɓukasz Stelmach Reviewed-by: Krzysztof Kozlowski --- drivers/crypto/exynos-rng.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/drivers/crypto/exynos-rng.c b/drivers/crypto/exynos-rng.c index 825ed7bfd881..4a06092074b9 100644 --- a/drivers/crypto/exynos-rng.c +++ b/drivers/crypto/exynos-rng.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -79,6 +80,7 @@ struct exynos_rng_dev { enum exynos_prng_type type; void __iomem *mem; struct clk *clk; + struct mutex lock; /* Generated numbers stored for seeding during resume */ u8 seed_save[EXYNOS_RNG_SEED_SIZE]; unsigned int seed_save_len; @@ -191,6 +193,10 @@ static void exynos_rng_reseed(struct exynos_rng_dev *rng) return; exynos_rng_set_seed(rng, seed, read); + + /* Let others do some of their job. */ + mutex_unlock(&rng->lock); + mutex_lock(&rng->lock); } static int exynos_rng_generate(struct crypto_rng *tfm, @@ -206,6 +212,7 @@ static int exynos_rng_generate(struct crypto_rng *tfm, if (ret) return ret; + mutex_lock(&rng->lock); do { ret = exynos_rng_get_random(rng, dst, dlen, &read); if (ret) @@ -216,6 +223,7 @@ static int exynos_rng_generate(struct crypto_rng *tfm, exynos_rng_reseed(rng); } while (dlen > 0); + mutex_unlock(&rng->lock); clk_disable_unprepare(rng->clk); @@ -233,7 +241,9 @@ static int exynos_rng_seed(struct crypto_rng *tfm, const u8 *seed, if (ret) return ret; + mutex_lock(&rng->lock); ret = exynos_rng_set_seed(ctx->rng, seed, slen); + mutex_unlock(&rng->lock); clk_disable_unprepare(rng->clk); @@ -278,6 +288,8 @@ static int exynos_rng_probe(struct platform_device *pdev) rng->type = (enum exynos_prng_type)of_device_get_match_data(&pdev->dev); + mutex_init(&rng->lock); + rng->dev = &pdev->dev; rng->clk = devm_clk_get(&pdev->dev, "secss"); if (IS_ERR(rng->clk)) { @@ -328,9 +340,14 @@ static int __maybe_unused exynos_rng_suspend(struct device *dev) if (ret) return ret; + mutex_lock(&rng->lock); + /* Get new random numbers and store them for seeding on resume. */ exynos_rng_get_random(rng, rng->seed_save, sizeof(rng->seed_save), &(rng->seed_save_len)); + + mutex_unlock(&rng->lock); + dev_dbg(rng->dev, "Stored %u bytes for seeding on system resume\n", rng->seed_save_len); @@ -353,8 +370,12 @@ static int __maybe_unused exynos_rng_resume(struct device *dev) if (ret) return ret; + mutex_lock(&rng->lock); + ret = exynos_rng_set_seed(rng, rng->seed_save, rng->seed_save_len); + mutex_unlock(&rng->lock); + clk_disable_unprepare(rng->clk); return ret;