From patchwork Fri Dec 1 21:19:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10088019 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BEDC66035E for ; Fri, 1 Dec 2017 21:26:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B206E2A779 for ; Fri, 1 Dec 2017 21:26:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A6E902A78B; Fri, 1 Dec 2017 21:26:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5248B2A779 for ; Fri, 1 Dec 2017 21:26:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751265AbdLAV0L (ORCPT ); Fri, 1 Dec 2017 16:26:11 -0500 Received: from mail-wr0-f194.google.com ([209.85.128.194]:45725 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751188AbdLAV0K (ORCPT ); Fri, 1 Dec 2017 16:26:10 -0500 Received: by mail-wr0-f194.google.com with SMTP id h1so11435211wre.12 for ; Fri, 01 Dec 2017 13:26:10 -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=4IWFfyA8qbht0pbAaWWcgZ+mreBLvmqM3d70R4Q8pxQ=; b=PHkA5CT5J4SlaoFfIRk8BTLr98g5VgavB/WQ725JS5coJ2YMunZmkw5cP5wo67AeUq zxLhyrGCYSDu1PHmbwvvNghJQ4uN4hzywxC0Ez2smK87vmQlHqN6QNfCRQp+gLqeXRV3 5ZqEMGlwyIw7kShZHVI7BNIBrxn7rAa9sP6oQ= 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=4IWFfyA8qbht0pbAaWWcgZ+mreBLvmqM3d70R4Q8pxQ=; b=s+4AwA1+WyG0hGTW+rYaOUsF5H0RaD/nR00oQy3qH6PHY9XdxdRbokFbKLvw8apA+n FxWD4/a5PtUOZ6Lney14R2VlqLoFYbMJY1vz2HBUmfSde1nfpN/2omvn2xVEaiXm4yTW ZTVNKStCPRmGvrxJ7qHQdSPZLp3kd3tU5/90j0Zuv0d8JrtJRA+P0Amik2LfY+6Gi1r3 oQANOS4XnLCP/59WWYPeDptJNFbbwNgiSagoHEx/9grF7XfTbw6wghe0gj8DtDpC/W/0 sXj8KxcL3jK2nNdJRFFGPM1lf53YbVMYti9iWTfT0PKJn3MduIw1oacxSvAYIqaiq7xw qUCw== X-Gm-Message-State: AJaThX5mJEMh8HnSVxn/vtmI3mXWfJNvgKG7lwBIJD5aY5b3ScKNOLGb 2cEf8KVccpdzHiHnqU+TjEANcoD9FVU= X-Google-Smtp-Source: AGs4zMYkj7kKO7IExzc1FKjBIiWImlO1HbBEisvuCbVi8Yhzh/HnBVhpTnbOvnPJCTdUNyl2r9horQ== X-Received: by 10.223.142.49 with SMTP id n46mr6279292wrb.279.1512163569448; Fri, 01 Dec 2017 13:26:09 -0800 (PST) Received: from localhost.localdomain ([105.150.171.234]) by smtp.gmail.com with ESMTPSA id p17sm2161682wma.23.2017.12.01.13.26.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Dec 2017 13:26:08 -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 4/5] crypto: arm64/chacha20 - move kernel mode neon en/disable into loop Date: Fri, 1 Dec 2017 21:19:26 +0000 Message-Id: <20171201211927.24653-5-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171201211927.24653-1-ard.biesheuvel@linaro.org> References: <20171201211927.24653-1-ard.biesheuvel@linaro.org> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm64/crypto/chacha20-neon-glue.c b/arch/arm64/crypto/chacha20-neon-glue.c index cbdb75d15cd0..b9ca7bef428a 100644 --- a/arch/arm64/crypto/chacha20-neon-glue.c +++ b/arch/arm64/crypto/chacha20-neon-glue.c @@ -36,6 +36,7 @@ static void chacha20_doneon(u32 *state, u8 *dst, const u8 *src, { u8 buf[CHACHA20_BLOCK_SIZE]; + kernel_neon_begin(); while (bytes >= CHACHA20_BLOCK_SIZE * 4) { chacha20_4block_xor_neon(state, dst, src); bytes -= CHACHA20_BLOCK_SIZE * 4; @@ -55,6 +56,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) @@ -72,7 +74,6 @@ static int chacha20_neon(struct skcipher_request *req) crypto_chacha20_init(state, ctx, walk.iv); - kernel_neon_begin(); while (walk.nbytes > 0) { unsigned int nbytes = walk.nbytes; @@ -83,7 +84,6 @@ static int chacha20_neon(struct skcipher_request *req) nbytes); err = skcipher_walk_done(&walk, walk.nbytes - nbytes); } - kernel_neon_end(); return err; }