From patchwork Mon Dec 11 14:06:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lukasz Stelmach X-Patchwork-Id: 10105203 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 9249A602A7 for ; Mon, 11 Dec 2017 14:08:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E81F295FA for ; Mon, 11 Dec 2017 14:08:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9379E2960A; Mon, 11 Dec 2017 14:08:14 +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 10D33295FA for ; Mon, 11 Dec 2017 14:08:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753201AbdLKOH7 (ORCPT ); Mon, 11 Dec 2017 09:07:59 -0500 Received: from mailout2.w1.samsung.com ([210.118.77.12]:33147 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752938AbdLKOGn (ORCPT ); Mon, 11 Dec 2017 09:06:43 -0500 Received: from eucas1p1.samsung.com (unknown [182.198.249.206]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20171211140640euoutp0258f7aa4849b6dbdca7d1c06d0f864d08~-QpN4FjR70716707167euoutp02V; Mon, 11 Dec 2017 14:06:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20171211140640euoutp0258f7aa4849b6dbdca7d1c06d0f864d08~-QpN4FjR70716707167euoutp02V DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1513001201; bh=NoY77MgHG1aq3zHy167qqjvJNyoiklPlc2OtRMCQsvw=; h=From:To:Cc:Subject:Date:In-reply-to:In-reply-to:References:From; b=myJPQ4aDkxzsJgH0ebek1DsJNrQ4mPH75QdnYJUmxTqp01HTSD8kZiWInRkNeu2b8 +mXM3HIEsms47BGobxL9egp0JI+3paXsafz6QvtQnX4CcSfGRHv0RqtZwtwb/nyilw mzNJgCvauF5Ef51TDMJqe1pmvlxa1TNCS/XhpcQM= Received: from eusmges5.samsung.com (unknown [203.254.199.245]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20171211140640eucas1p266eea60a6d97f4d8af6f918a9b096de2~-QpNDsd7Z0507005070eucas1p25; Mon, 11 Dec 2017 14:06:40 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges5.samsung.com (EUCPMTA) with SMTP id 71.C4.12743.FE09E2A5; Mon, 11 Dec 2017 14:06:39 +0000 (GMT) Received: from eusmgms1.samsung.com (unknown [182.198.249.179]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20171211140639eucas1p23232610e264416b74fa4e69c14d85968~-QpMPcx9j2986129861eucas1p2D; Mon, 11 Dec 2017 14:06:39 +0000 (GMT) X-AuditID: cbfec7f5-f79d06d0000031c7-28-5a2e90ef9237 Received: from eusync3.samsung.com ( [203.254.199.213]) by eusmgms1.samsung.com (EUCPMTA) with SMTP id 44.54.18832.FE09E2A5; Mon, 11 Dec 2017 14:06:39 +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 eusync3.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0P0S0076MV72O4B0@eusync3.samsung.com>; Mon, 11 Dec 2017 14:06:39 +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 v2 4/4] crypto: exynos - Introduce mutex to prevent concurrent access to hardware Date: Mon, 11 Dec 2017 15:06:23 +0100 Message-id: <20171211140623.7673-5-l.stelmach@samsung.com> X-Mailer: git-send-email 2.11.0 In-reply-to: <20171211140623.7673-1-l.stelmach@samsung.com> In-reply-to: <20171205123558.31087-1-l.stelmach@samsung.com> Organization: Samsung R&D Institute Poland X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrFKsWRmVeSWpSXmKPExsWy7djPc7rvJ+hFGRz8bGCxccZ6Vos551tY LLpfyVj0P37NbHH+/AZ2i5uHVjBa3L/3k8ni8q45bBYzzu9jslh75C67ReveI+wWU0+vZXHg 8fh05QqTx5aVN5k8th1Q9di0qpPNo2/LKkaPz5vkAtiiuGxSUnMyy1KL9O0SuDLWN/9iL5go VfHu5S+WBsbVol2MnBwSAiYSU+YvZoSwxSQu3FvP1sXIxSEksJRR4vTLr6wQzmdGiY0XHrB0 MXKAdfybmw8RX8Yoce/1TTaQbl4BQYkfk++B1TALyEscuZQNEmYW0JTYuns9O0T9F0aJgx++ M4Ek2AQcJfqXngBbICKwj0ni8cSpjCAOM8jUg9tOgk0VFkiWaFnTBHYfi4CqxMF1i5ghtllJ dBxYyAZxt7zErraLrCA2p4C1ROfxdjYI20Zi4dkZYDa/gJbEmqbrLCALJATmsUs0PD3PBNHs InHkfiM7hC0s8er4FihbRqKz4yATREM/o8Th+d+hElMYJRYvdICwrSX+rJrIBvEon8SkbdOZ IWH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8/qSCGB9MSS1OzU1ILUIpgs EwenVAOjhPE0x9lXZjjPPeGQfLaAPc9YKZtjB98Uztw/Yb2mPA5T/1RWrQ17NU989r6vKl9/ nt38+sqsQwFtqfcDdp6pSD+/eff9H62buadzrTvreo/n+F4/aVON1yGz/l7eP/P/3r+n7Zuv bH/ZxPr20UPtRbEabQGHLtXFftG2FPzx7mPY8nM31yZtEVViKc5INNRiLipOBADjKJoXrAIA AA== X-CMS-MailID: 20171211140639eucas1p23232610e264416b74fa4e69c14d85968 X-Msg-Generator: CA CMS-TYPE: 201P X-CMS-RootMailID: 20171211140639eucas1p23232610e264416b74fa4e69c14d85968 X-RootMTR: 20171211140639eucas1p23232610e264416b74fa4e69c14d85968 References: <20171211140623.7673-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 Cc: Marek Szyprowski , Bartlomiej Zolnierkiewicz 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 c72a838f1932..6209035ca659 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; @@ -192,6 +194,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, @@ -207,6 +213,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) @@ -217,6 +224,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); @@ -234,7 +242,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); @@ -284,6 +294,8 @@ static int exynos_rng_probe(struct platform_device *pdev) return -ENOTSUPP; } + mutex_init(&rng->lock); + rng->dev = &pdev->dev; rng->clk = devm_clk_get(&pdev->dev, "secss"); if (IS_ERR(rng->clk)) { @@ -334,9 +346,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); @@ -359,8 +376,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;