From patchwork Wed Oct 18 07:00:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gilad Ben-Yossef X-Patchwork-Id: 116231 Delivered-To: patch@linaro.org Received: by 10.140.22.163 with SMTP id 32csp5746548qgn; Wed, 18 Oct 2017 00:06:51 -0700 (PDT) X-Google-Smtp-Source: AOwi7QAK2WJ1n1QU66j/ub7hnp6re7X1Z1vnjZW3YVcivUsA2JlQTxFc093HJhNHplzjlDvg7zhu X-Received: by 10.98.198.195 with SMTP id x64mr14079294pfk.322.1508310411582; Wed, 18 Oct 2017 00:06:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1508310411; cv=none; d=google.com; s=arc-20160816; b=sfheLKUY2qdQZqqckMvsDJm7KK0QItuzjIaJXdj7f0kPVQ2FQGCsGKiKZgWr33EW4B a7ML3WvfNTBgrCQM3eBWJpseScIpi/zmiqNw6tVkJpFTt/CpVLALH5inx+xcCzoOaGCY xjQiUlkJQiNEis5HM2EMJv9hnyb0JtvNvyMzIZ6EF1U9zXRzR6ehAwkMP/BkDn/2DhqS Czxyqt3mJCIsGCpFy1HKMjXPEK59VAc8/2e8tDkKSX0f0AzN9r0vQfTwPmkLbdFf12Kx gOX6b0aJ2oCE7V9xiTKwYPEjyGanDrw1suMIeQC5WvvpMMpleliiTG9e/X5bcUtz2v63 VWmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=GC236wveax5YSzj+Bhh0LelivQd+qYwt8ZP+Zmr+cKs=; b=GJXlFn0CFZEkzYpsA/470gFYOSa8NvyDW8pfP/7k1BkLi9zvOkGpFmjbzjYVTGfvXi h4wO/tMXyw4rUioCowOktFYgEmxCyddKwLl3mGjUPKJcfNU3uwAS11QK2wgv8Y1Z6bTl JXKg+/S8YHfn0rt4cMWhSrMD72/9B7xE5MLy7+VOq3/7+It9FCPV990j51FuCRjcsDXZ RgPUOYpUq5/PKfbTUsSkoAcgQnFadbTDXYpVWlv411RjgPT3cd2fcUi91mCIDsKCc+U8 NFMu67VFtUE1tt0SbxzHvwr1cFtNLmDpxzxncum72H6/Aq9JKwKMgpuKUSl6eK5dRnVh cINw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v187si723783pfv.76.2017.10.18.00.06.51; Wed, 18 Oct 2017 00:06:51 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966926AbdJRHGt (ORCPT + 27 others); Wed, 18 Oct 2017 03:06:49 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:34276 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1761450AbdJRHGp (ORCPT ); Wed, 18 Oct 2017 03:06:45 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 2EC351529; Wed, 18 Oct 2017 00:06:45 -0700 (PDT) Received: from localhost.localdomain (usa-sjc-mx-foss1.foss.arm.com [217.140.101.70]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D7C713F590; Wed, 18 Oct 2017 00:06:36 -0700 (PDT) From: Gilad Ben-Yossef To: Herbert Xu , "David S. Miller" , Jonathan Corbet , David Howells , Tom Lendacky , Gary Hook , Boris Brezillon , Arnaud Ebalard , Matthias Brugger , Alasdair Kergon , Mike Snitzer , dm-devel@redhat.com, Steve French , "Theodore Y. Ts'o" , Jaegeuk Kim , Steffen Klassert , Alexey Kuznetsov , Hideaki YOSHIFUJI , Mimi Zohar , Dmitry Kasatkin , James Morris , "Serge E. Hallyn" Cc: Ofir Drang , linux-crypto@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, keyrings@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-cifs@vger.kernel.org, samba-technical@lists.samba.org, linux-fscrypt@vger.kernel.org, netdev@vger.kernel.org, linux-ima-devel@lists.sourceforge.net, linux-ima-user@lists.sourceforge.net, linux-security-module@vger.kernel.org Subject: [PATCH v10 20/20] crypto: adapt api sample to use async. op wait Date: Wed, 18 Oct 2017 08:00:52 +0100 Message-Id: <1508310057-25369-21-git-send-email-gilad@benyossef.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1508310057-25369-1-git-send-email-gilad@benyossef.com> References: <1508310057-25369-1-git-send-email-gilad@benyossef.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The code sample is waiting for an async. crypto op completion. Adapt sample to use the new generic infrastructure to do the same. This also fixes a possible data coruption bug created by the use of wait_for_completion_interruptible() without dealing correctly with an interrupt aborting the wait prior to the async op finishing. Signed-off-by: Gilad Ben-Yossef --- Documentation/crypto/api-samples.rst | 52 +++++++----------------------------- 1 file changed, 10 insertions(+), 42 deletions(-) -- 2.7.4 diff --git a/Documentation/crypto/api-samples.rst b/Documentation/crypto/api-samples.rst index 2531948..006827e 100644 --- a/Documentation/crypto/api-samples.rst +++ b/Documentation/crypto/api-samples.rst @@ -7,59 +7,27 @@ Code Example For Symmetric Key Cipher Operation :: - struct tcrypt_result { - struct completion completion; - int err; - }; - /* tie all data structures together */ struct skcipher_def { struct scatterlist sg; struct crypto_skcipher *tfm; struct skcipher_request *req; - struct tcrypt_result result; + struct crypto_wait wait; }; - /* Callback function */ - static void test_skcipher_cb(struct crypto_async_request *req, int error) - { - struct tcrypt_result *result = req->data; - - if (error == -EINPROGRESS) - return; - result->err = error; - complete(&result->completion); - pr_info("Encryption finished successfully\n"); - } - /* Perform cipher operation */ static unsigned int test_skcipher_encdec(struct skcipher_def *sk, int enc) { - int rc = 0; + int rc; if (enc) - rc = crypto_skcipher_encrypt(sk->req); + rc = crypto_wait_req(crypto_skcipher_encrypt(sk->req), &sk->wait); else - rc = crypto_skcipher_decrypt(sk->req); - - switch (rc) { - case 0: - break; - case -EINPROGRESS: - case -EBUSY: - rc = wait_for_completion_interruptible( - &sk->result.completion); - if (!rc && !sk->result.err) { - reinit_completion(&sk->result.completion); - break; - } - default: - pr_info("skcipher encrypt returned with %d result %d\n", - rc, sk->result.err); - break; - } - init_completion(&sk->result.completion); + rc = crypto_wait_req(crypto_skcipher_decrypt(sk->req), &sk->wait); + + if (rc) + pr_info("skcipher encrypt returned with result %d\n", rc); return rc; } @@ -89,8 +57,8 @@ Code Example For Symmetric Key Cipher Operation } skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, - test_skcipher_cb, - &sk.result); + crypto_req_done, + &sk.wait); /* AES 256 with random key */ get_random_bytes(&key, 32); @@ -122,7 +90,7 @@ Code Example For Symmetric Key Cipher Operation /* We encrypt one block */ sg_init_one(&sk.sg, scratchpad, 16); skcipher_request_set_crypt(req, &sk.sg, &sk.sg, 16, ivdata); - init_completion(&sk.result.completion); + crypto_init_wait(&sk.wait); /* encrypt data */ ret = test_skcipher_encdec(&sk, 1);