From patchwork Wed Dec 6 19:43:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 120888 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp7468563qgn; Wed, 6 Dec 2017 11:44:14 -0800 (PST) X-Google-Smtp-Source: AGs4zMb47KBVQfFaIcO98Byt3UwJzqWd1sKyiw4xNXaEhCTVx8iqSWzmtVPX14IMZMHrqlndEB80 X-Received: by 10.159.204.147 with SMTP id t19mr23387469plo.222.1512589454040; Wed, 06 Dec 2017 11:44:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512589454; cv=none; d=google.com; s=arc-20160816; b=bfMofwvvJOYOPEjAeaHSIIfMbY6We+DOyNBq0pxBwUvou2SFmpfdhGETJHOfd9gEGn 90PIX3sedAMiduiM4zCvoIB4OlNeOuWxfkp9H2eYTlvAI7BO39NJwHKU/uHZS0lfhkW2 g0IctZV4+YzX/gG8JCwc9LalUaIQ1HOfmR7M9qC+8JYOUfkr0j1lj6vAmr6662LZ7vWS azfZ0ZJkyhrfSghBfX+zaDucs+ivnC1mj74JKLtVKlq+VWr6TI+gnPw1uG0wjisb7/tB BGxYUPvEk8RXSpuDYhkj0vxcngIvRliXcMkSG9ibGPsJ+vDc7a/5vY6PMdIZlXhXq+XS hLTg== 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:dkim-signature:arc-authentication-results; bh=IhTik8BmpN/IjyhBdu2dYZZ72sUFaQrQeiEa5qMgGQk=; b=EqWSGYs34YAf9V952lcYI5hKq//If+0YrJZCAfFLTr4hm/A4+7bpQd+ySBiKFD2EPJ zvjfmXKMhXfhQ5lzwu6dReisGwk9OWiFyR4dtVFmLf13Jy1aPE42rwox/BagsrAdJ+zH lq02Rshk5r/5hRUWvTrS7k996UWJIldnfvWPjXOM8xCnUkY8Pgx08kq68zKXu5J24/Tv MsXfAuosrBuca1CkcI9O766qhS4ZPOcUs+NCDPSdyZuoBz5KNrZwNQTmhQKYIpOvRDee 5h3IqobMs1S8QU4y6Zchhc9V+ZO/30Bwb/UL2QlRuPefs93MLER42LXGpwPIBXPHemo/ vWbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KahvDAbK; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i16si2382524pgv.496.2017.12.06.11.44.13; Wed, 06 Dec 2017 11:44:14 -0800 (PST) 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; dkim=pass header.i=@linaro.org header.s=google header.b=KahvDAbK; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752073AbdLFToL (ORCPT + 1 other); Wed, 6 Dec 2017 14:44:11 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:44331 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752126AbdLFToK (ORCPT ); Wed, 6 Dec 2017 14:44:10 -0500 Received: by mail-wr0-f195.google.com with SMTP id l22so5100091wrc.11 for ; Wed, 06 Dec 2017 11:44:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=IhTik8BmpN/IjyhBdu2dYZZ72sUFaQrQeiEa5qMgGQk=; b=KahvDAbKzrmVdWRAx9S5eOMsUg9W+vaTK1AbWDMXcoQDIWwHiKCqRmVeGs0n7cMXV4 AwXIWnL6lEq2LJzUqOB8FUaHzoa/s57S38x+XRfxqJQXms+u71ok1pAHK/f1Sva1cYTF GTl+Q4JAiFPJl2bXCL3XHZgJ8Ke4hgL6fsgok= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=IhTik8BmpN/IjyhBdu2dYZZ72sUFaQrQeiEa5qMgGQk=; b=FDyrX1sM5uId9DQQa1GF9D/pvf8tMGJzUEZmTmYiQYOkUmboGuQKgGVdk2qfGO6vFV l46gy+H2PEZEdovU+LrgYlr/xrwEDeHc41h8lq/vlchnYaBJ/OFECf/9fwIQ2otJX8Bi zEIkQb4/Gd54mJcHwS0YJat1cG+Z7MPc5SJbv/dGVgjS37mE2tnKntiMIBFBRFrFfDOk f118HrscXyKcHetlFkV0HH+GxaXYz5jC8onQ63j/RqWJM1OILAu/axJmE3Vp72MR9cD2 EfQSXb3rlBFs2PYfgtGRNAPIOGQviJrIw9e67NlWrTvBvgYC+bffzUOrRslvCiufg15T BOuA== X-Gm-Message-State: AJaThX5+NxJ+0xhIJoVmmQbcrB9gc93h/r+Msvl14PifLHjKR5fWiDqv eqDH9piOKRH6FvYSB2xk0i9PLbCSEi8= X-Received: by 10.223.156.146 with SMTP id d18mr20927914wre.161.1512589448764; Wed, 06 Dec 2017 11:44:08 -0800 (PST) Received: from localhost.localdomain ([105.150.171.234]) by smtp.gmail.com with ESMTPSA id b66sm3596594wmh.32.2017.12.06.11.44.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Dec 2017 11:44:07 -0800 (PST) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, linux-arm-kernel@lists.infradead.org, Ard Biesheuvel , Dave Martin , Russell King - ARM Linux , Sebastian Andrzej Siewior , Mark Rutland , linux-rt-users@vger.kernel.org, Peter Zijlstra , Catalin Marinas , Will Deacon , Steven Rostedt , Thomas Gleixner Subject: [PATCH v3 05/20] crypto: arm64/chacha20 - move kernel mode neon en/disable into loop Date: Wed, 6 Dec 2017 19:43:31 +0000 Message-Id: <20171206194346.24393-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171206194346.24393-1-ard.biesheuvel@linaro.org> References: <20171206194346.24393-1-ard.biesheuvel@linaro.org> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When kernel mode NEON was first introduced on arm64, the preserve and restore of the userland NEON state was completely unoptimized, and involved saving all registers on each call to kernel_neon_begin(), and restoring them on each call to kernel_neon_end(). For this reason, the NEON crypto code that was introduced at the time keeps the NEON enabled throughout the execution of the crypto API methods, which may include calls back into the crypto API that could result in memory allocation or other actions that we should avoid when running with preemption disabled. Since then, we have optimized the kernel mode NEON handling, which now restores lazily (upon return to userland), and so the preserve action is only costly the first time it is called after entering the kernel. So let's put the kernel_neon_begin() and kernel_neon_end() calls around the actual invocations of the NEON crypto code, and run the remainder of the code with kernel mode NEON disabled (and preemption enabled) Signed-off-by: Ard Biesheuvel --- arch/arm64/crypto/chacha20-neon-glue.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) -- 2.11.0 diff --git a/arch/arm64/crypto/chacha20-neon-glue.c b/arch/arm64/crypto/chacha20-neon-glue.c index cbdb75d15cd0..727579c93ded 100644 --- a/arch/arm64/crypto/chacha20-neon-glue.c +++ b/arch/arm64/crypto/chacha20-neon-glue.c @@ -37,12 +37,19 @@ static void chacha20_doneon(u32 *state, u8 *dst, const u8 *src, u8 buf[CHACHA20_BLOCK_SIZE]; while (bytes >= CHACHA20_BLOCK_SIZE * 4) { + kernel_neon_begin(); chacha20_4block_xor_neon(state, dst, src); + kernel_neon_end(); bytes -= CHACHA20_BLOCK_SIZE * 4; src += CHACHA20_BLOCK_SIZE * 4; dst += CHACHA20_BLOCK_SIZE * 4; state[12] += 4; } + + if (!bytes) + return; + + kernel_neon_begin(); while (bytes >= CHACHA20_BLOCK_SIZE) { chacha20_block_xor_neon(state, dst, src); bytes -= CHACHA20_BLOCK_SIZE; @@ -55,6 +62,7 @@ static void chacha20_doneon(u32 *state, u8 *dst, const u8 *src, chacha20_block_xor_neon(state, buf, buf); memcpy(dst, buf, bytes); } + kernel_neon_end(); } static int chacha20_neon(struct skcipher_request *req) @@ -68,11 +76,10 @@ static int chacha20_neon(struct skcipher_request *req) if (!may_use_simd() || req->cryptlen <= CHACHA20_BLOCK_SIZE) return crypto_chacha20_crypt(req); - err = skcipher_walk_virt(&walk, req, true); + err = skcipher_walk_virt(&walk, req, false); crypto_chacha20_init(state, ctx, walk.iv); - kernel_neon_begin(); while (walk.nbytes > 0) { unsigned int nbytes = walk.nbytes; @@ -83,7 +90,6 @@ static int chacha20_neon(struct skcipher_request *req) nbytes); err = skcipher_walk_done(&walk, walk.nbytes - nbytes); } - kernel_neon_end(); return err; }