From patchwork Thu Aug 24 14:19:06 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: 110930 Delivered-To: patch@linaro.org Received: by 10.37.128.210 with SMTP id c18csp8564748ybm; Thu, 24 Aug 2017 07:23:34 -0700 (PDT) X-Received: by 10.84.217.132 with SMTP id p4mr7002434pli.297.1503584614510; Thu, 24 Aug 2017 07:23:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503584614; cv=none; d=google.com; s=arc-20160816; b=UHgopnefinXyaxF6vijv+9iADrfnWvBffIVP8bbBNt6N815dp8N6aRYvpRFZqWgcyh Qb4CRrczlMdL8YNYWkXVvHmzOUYV5pKoe6omq84m1wlFcCUIjlzn6s5k5NOu1T8fi3T/ BGYuCYb0MyEhY77V6578aX9JDWPVVwVHWK7ASfAZT/+BBYZNYkzIkzN+YXSV6bvu7yco 8ixpsyNGQv7Bf4lBxmrM3Kk7OGGiKqeEcplp+Kd6H977N3qYBgVSZY23Z6PfqR3kHqH2 esJkS/KRVMysc2rC5mvFPTSMg7Isps+BuAtX9iS34G0AA2SIC7VeFIBjDPRGAE7YuMTF s9jA== 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=jYyqhJ9iw99eEE548FbhZSszxZztW62njpLMz3/1uCY=; b=hYhQaNosPqNKauBiDZ54W17eGaC97nwCgzvGJbiMGCIjKPnd7JsnTYeTpSNuFAjSun CjVMxj/iHB33SvwoC5cemoeSPJfJ/pwlOXk5oTckdtPOyv5h6Qwcz71cPx2ynZ5HepTx p9M2vWO3JbDzdpVk0dJHq3zDTamnS6kc6uz4w7A70m5rJ1HEvSLcWeUGKibdw9T1eJRU +OVIrX4J/vCTVTQoJCflSRYfTPHHYuJsccXc8cWub1hRjcIwBW8Cj0bwTMiHp4yYco8U qUX4DVcYyD5GXU4VrpUlod0gvYMcfgLcYBrLSqBY1iEjvdFcNfODF+yxvNrYWCyXiFnO 8T5Q== 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 n34si3080651pld.382.2017.08.24.07.23.33; Thu, 24 Aug 2017 07:23:34 -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 S1754003AbdHXOXb (ORCPT + 26 others); Thu, 24 Aug 2017 10:23:31 -0400 Received: from foss.arm.com ([217.140.101.70]:42506 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753203AbdHXOX0 (ORCPT ); Thu, 24 Aug 2017 10:23:26 -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 B41691B8A; Thu, 24 Aug 2017 07:23:25 -0700 (PDT) Received: from gby.kfn.arm.com (unknown [10.45.48.132]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id BD3F93F540; Thu, 24 Aug 2017 07:23:17 -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, Shaohua Li , Steve French , "Theodore Y. Ts'o" , Jaegeuk Kim , Mimi Zohar , Dmitry Kasatkin , James Morris , "Serge E. Hallyn" , 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-raid@vger.kernel.org, linux-cifs@vger.kernel.org, samba-technical@lists.samba.org, linux-fscrypt@vger.kernel.org, linux-ima-devel@lists.sourceforge.net, linux-ima-user@lists.sourceforge.net, linux-security-module@vger.kernel.org Cc: Ofir Drang Subject: [PATCH v7 19/19] crypto: adapt api sample to use async. op wait Date: Thu, 24 Aug 2017 17:19:06 +0300 Message-Id: <1503584350-7831-20-git-send-email-gilad@benyossef.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1503584350-7831-1-git-send-email-gilad@benyossef.com> References: <1503584350-7831-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.1.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);