From patchwork Sun Jul 2 14:42:10 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: 106867 Delivered-To: patch@linaro.org Received: by 10.182.135.102 with SMTP id pr6csp8296581obb; Sun, 2 Jul 2017 07:49:00 -0700 (PDT) X-Received: by 10.99.65.196 with SMTP id o187mr5625105pga.193.1499006940651; Sun, 02 Jul 2017 07:49:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1499006940; cv=none; d=google.com; s=arc-20160816; b=hhrAeyTBdXooZFoY9z8A7ncLdGA4pa8l5xU3/4bBd2ErwTp9yzc8wj/6G9SaZ08sfx 5qHYsldrAWn9vaP+TaxRcT2vXuLrH2Cb81eyl7XzySvsTslm4hu+x1oNKB/a50HUA3TS lQ1CWls79ogWw0NMPj0T5A+RbcU8J5TDGHe4Ezi5T6PkcFMP5fzymCjtgrAiFT3X2j8Y hrlX8LcpRpViF2L9wvhqnbJS0VHNschZapHNjgiFGZ3S4umMLDFNTIaNesvweCqp7xtp OOLGB+lYHgEuGOgYS1dHLCTTMSxiYzE1yrrIpqewChaseMW2GSmErH22AQ26Ngb1TIr+ EbrA== 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=7C1aAJSgYY01SDIvfXS6Jxsz2JTGLabAgoM2YxxlpuA=; b=oNPgxxRbCPp/EP5ZNeR69Q0zfdTKYKyuMN8/FPEK6Yi8co6xpBmPbVVWeNBT+8pakn F5pj2OQFrwwZvwNQd8X+0rDGwbTF6mGI7y1NhGeiP5K++Qgv87fCNdCI7wL5MpV5jZ3u SMXEk0C/pLKN5vwv2twaNIi9pHVR6vsVOWmsoFHtEQfkjQEV//T7emu5kkLadH8PlqiC CU+ED0roERm925FIPW/zmhOutCh7D9NXfOUaZWNhzn0Tcp3BnrqX2THxeBG1AeSG41wZ jDI0lAkEvBxME3Qt8yI7ksXHeczJk3D93ZNTnVRf9Zmz7kGpmxgKqWtj/+nVIZTAbnyo HJaw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-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 p3si10208060pfg.45.2017.07.02.07.49.00; Sun, 02 Jul 2017 07:49:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-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-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752463AbdGBOs6 (ORCPT + 1 other); Sun, 2 Jul 2017 10:48:58 -0400 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:54890 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752638AbdGBOsz (ORCPT ); Sun, 2 Jul 2017 10:48:55 -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 5168115B2; Sun, 2 Jul 2017 07:48:44 -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 781043F578; Sun, 2 Jul 2017 07:48: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, 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 v3 28/28] crypto: adapt api sample to use async. op wait Date: Sun, 2 Jul 2017 17:42:10 +0300 Message-Id: <1499006535-19760-29-git-send-email-gilad@benyossef.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1499006535-19760-1-git-send-email-gilad@benyossef.com> References: <1499006535-19760-1-git-send-email-gilad@benyossef.com> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@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 c6aa4ba..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 -EIOCBQUEUED: - 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);