From patchwork Mon Dec 4 12:26: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: 120515 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp4365519qgn; Mon, 4 Dec 2017 04:27:15 -0800 (PST) X-Google-Smtp-Source: AGs4zMaDkZEdwfa26B867vGmKgHF+Xad/5MG6554nt1l1QwUTJQq4XNyYlB2IX6L7GJO6j2XWQLC X-Received: by 10.159.204.146 with SMTP id t18mr11375075plo.237.1512390435411; Mon, 04 Dec 2017 04:27:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512390435; cv=none; d=google.com; s=arc-20160816; b=o01Ag549QScbxEj+Za02rQjlSS67tyT5Af03hvQm0tv02yHmWqX61OBuflo+L4U6dt zTch7HFm1suUhIMAY9WD/3CnZIxHgEU/VIuC0/YqvQHwsu4By9YkelxR84TyKU47Wu20 Xjgz20QAqb1oUBouNEbOq9XZsHfb1VUTbXKhSuAa7OgvQ1PPHV1dfdkFe54Bk/asiP/J pvv23ksYU1Q25ytGOCBUBVh+5JRRAd710fRZfr6WcNuvrsuq9BvzG+qqIyD5lte4Z+s8 +qfIzv4b+dNb0VX135m67YN0LUfjfxIRk9n4d7NJcgiTtQl40k+9Cm9JRwLFJVe7k+Y/ +FFg== 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=AW+iYHbvWFCddi4/vLn/Li4cwn2FKZLOZ5Xu4L2o7sEFjywgpbZMfvpCbz7AZlxeiV WS5guVZy+yO4jIbSG7Djob1DXwUMqjPlR79vkyXSXTVtWH7lvLZy1Ln9l8fTEKuitQXD EGY6WBezGCuGEztFd2piNIRrNSZOw3b21Z+5VtMHbOu69G+5gJNXNBef9U+B+VP39wQ7 +98a6MLB1We/UzZ7FXcSxo+HihG0Kkc/CTmrPWUDqvaQcQxm/4hdtTcUqefNgPKQ8ScA gjdiVXQMSRrm6lxnlrD1rKFpgeMvao6JMM6j6GuiDVyfQ2/gG8CwoCKHPezFRqyIfNju LzFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SgyXGla/; 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 u144si9409634pgb.226.2017.12.04.04.27.15; Mon, 04 Dec 2017 04:27:15 -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=SgyXGla/; 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 S1753138AbdLDM1O (ORCPT + 1 other); Mon, 4 Dec 2017 07:27:14 -0500 Received: from mail-wm0-f65.google.com ([74.125.82.65]:44482 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753142AbdLDM1L (ORCPT ); Mon, 4 Dec 2017 07:27:11 -0500 Received: by mail-wm0-f65.google.com with SMTP id t8so5453293wmc.3 for ; Mon, 04 Dec 2017 04:27:11 -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=SgyXGla/u3x1dLflpupGjmIczL/OalUwN1xEkR1cUiEdQiB8aIqmP0v6T4uIn41pk3 5SQbM6r34qGzHkN4bDwKbMnVDmbtvWi4A92YzbuXVzpeYx50Wa7wb5OGtSwNArVdVnTy eGNoyZ20gDDvKMcfT/RcPAoT1X30H6Np5KX1s= 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=jIilYinuJMmFItGakrvIvgf+eoBswopMbiSPW1aigDs/poaYaYTp1TlOkRpNMODt7t 7NkT8awaiiCTqVIv3p7/2cAfVyDDUGk4666s3p7QldAeGHSTeDvJmvTaZXzUYOq1lzeh +MYpoRqwgpnF37OHhjZW95DmxtlLokntWyuF/EkxsvELY7XMdy8meX4B14dNXkD8bp+v 3B+EADMB+3Sq8uePfMVUR3mUJCPyLOEaJiwRl/5KBcbyKeIYRugeu49YaGyF2lTOTRaS x1+Tk3+PcQUbn5EzSldOSvtiFBdS1Es9Lqu+YFMTdrPpuv31a2fTKLizi9KeqZV8KZvh wrAA== X-Gm-Message-State: AKGB3mK8bBm+oWTaqj8Tq4csnAlE1qbB+wg4Vc/Y+5RMWLWPh907bMzJ TGOavsuNd/Pv+UJo4WNFdDvzW3Hcqtg= X-Received: by 10.28.178.135 with SMTP id b129mr2746569wmf.103.1512390430248; Mon, 04 Dec 2017 04:27:10 -0800 (PST) Received: from localhost.localdomain ([105.150.171.234]) by smtp.gmail.com with ESMTPSA id a8sm7665839wmh.41.2017.12.04.04.27.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 04 Dec 2017 04:27:09 -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 v2 05/19] crypto: arm64/chacha20 - move kernel mode neon en/disable into loop Date: Mon, 4 Dec 2017 12:26:31 +0000 Message-Id: <20171204122645.31535-6-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171204122645.31535-1-ard.biesheuvel@linaro.org> References: <20171204122645.31535-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; }