From patchwork Mon Oct 8 21:15:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10631463 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B6F3914DB for ; Mon, 8 Oct 2018 21:17:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A838129816 for ; Mon, 8 Oct 2018 21:17:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9C15629839; Mon, 8 Oct 2018 21:17:14 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 22E5929816 for ; Mon, 8 Oct 2018 21:17:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=ot/3QYCOhm+tsyCtzwpPbSMJ1Q0CLp/643mivo0eCrc=; b=MMciefsToV4buYYspwFq3rt8qo z2wLMas/HS7MZCzIcNLw8oAOnFS4JvpCkiSRRsWsnpru4LaYs+0f3W1rB0Pz2t/tUJQF9uDLZiTqN KonExiDWfui07nHAhd8s4aRqQAzDfFHJ5M1XLrzZvvHZlzTmhqvs6PSshConymM4AySRm1OkSQM5M 3mWShv5UrtC2ftKYkfGPMo7Ex0yqbz5b8Xy6PwzLUJkPTJ0QtfBDGRWDoF73F2U86V0IrRheMHoy4 ad8Yqg66UnQFxMt4aBw/kfnHRSgckXXuMGUgB4CKHo5XqewKtEBIqL2gbE8CMmjuueJ9fWWfeEOd7 f77/FgOg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1g9cte-0007BC-Pt; Mon, 08 Oct 2018 21:16:58 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1g9ct1-0006qo-Eq for linux-arm-kernel@lists.infradead.org; Mon, 08 Oct 2018 21:16:22 +0000 Received: by mail-wm1-x341.google.com with SMTP id 193-v6so9571834wme.3 for ; Mon, 08 Oct 2018 14:16:08 -0700 (PDT) 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=yVyxxH1ii59qYZF1tTV85yOwfr9+lABlveFtUiVcJ1o=; b=PBqbW6uTfN5iffbPRE1SIC5r3SoXgOP4NpaaUNKHUlamM5mbc7Y7g5XrgIAlvLYQrN lL+nGpFu9zOhdoBJsSE4TIyUcq5aX+0vRITgY4CXvXiWROA5n6RutE5HQ7av3uDRu4PJ CrcyaB/LWlQR5HTRec71Ei5nAmBeBTtuqjJEA= 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=yVyxxH1ii59qYZF1tTV85yOwfr9+lABlveFtUiVcJ1o=; b=jofpUFWdXgmoUtSJLuGcgIhlT0y7mQ8dtforAdKVsp0L5SRtpSIlUn1z4PoWoK4VeQ 0TB3tIP134I7EodgpkMJfEHtcRvSyHppRKH2A5FM+D3o/Tel7Lv3+TKtqrfefI3nG7MV ioQfXXxIvFTDTL1k7vDv6dJPvX5JQn+bL1hLDjlwS+tiRvABkadRZQBlfromYp+1psQ9 fM/IBsyYSzWmXOYUnSLkYO5rPB/2ZG6zPGdpHONh31OwtcGNgwanZmVqtjh+v0p5FkXf iEryNTSE/WC+Lg5CaRu8ZuazY7gpJS7vufyoIlmxgfoVdxPjCug7X9SFlDS4vnOBIKUv yDHQ== X-Gm-Message-State: ABuFfohs6wCKAEN5YYiCIevnCbo0STfXQ0Uj/ckrYGNJ+S1yJxF2vcee DoRR/s23VJt1+nhgyG9260ta6g== X-Google-Smtp-Source: ACcGV60McTrF/fI3Rnaz5sjOeV9Vv/ZrB+wioCZHa2apvq4TL1E4IJ+kPiD8+yzSxpDDwoscRc/B4A== X-Received: by 2002:a1c:b58e:: with SMTP id e136-v6mr15619838wmf.114.1539033366609; Mon, 08 Oct 2018 14:16:06 -0700 (PDT) Received: from localhost.localdomain ([2a01:cb1d:112:6f00:8084:9715:d038:c67d]) by smtp.gmail.com with ESMTPSA id s24-v6sm7563308wmc.7.2018.10.08.14.16.05 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Oct 2018 14:16:05 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Subject: [PATCH 1/3] crypto: memneq - use unaligned accessors for aligned fast path Date: Mon, 8 Oct 2018 23:15:52 +0200 Message-Id: <20181008211554.5355-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181008211554.5355-1-ard.biesheuvel@linaro.org> References: <20181008211554.5355-1-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181008_141619_956819_5C760AB3 X-CRM114-Status: GOOD ( 14.95 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jason@zx2c4.com, herbert@gondor.apana.org.au, arnd@arndb.de, Ard Biesheuvel , ebiggers@google.com, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP On ARM v6 and later, we define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS because the ordinary load/store instructions (ldr, ldrh, ldrb) can tolerate any misalignment of the memory address. However, load/store double and load/store multiple instructions (ldrd, ldm) may still only be used on memory addresses that are 32-bit aligned, and so we have to use the CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS macro with care, or we may end up with a severe performance hit due to alignment traps that require fixups by the kernel. Fortunately, the get_unaligned() accessors do the right thing: when building for ARMv6 or later, the compiler will emit unaligned accesses using the ordinary load/store instructions (but avoid the ones that require 32-bit alignment). When building for older ARM, those accessors will emit the appropriate sequence of ldrb/mov/orr instructions. And on architectures that can truly tolerate any kind of misalignment, the get_unaligned() accessors resolve to the leXX_to_cpup accessors that operate on aligned addresses. So switch to the unaligned accessors for the aligned fast path. This will create the exact same code on architectures that can really tolerate any kind of misalignment, and generate code for ARMv6+ that avoids load/store instructions that trigger alignment faults. Signed-off-by: Ard Biesheuvel --- crypto/memneq.c | 24 ++++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/crypto/memneq.c b/crypto/memneq.c index afed1bd16aee..0f46a6150f22 100644 --- a/crypto/memneq.c +++ b/crypto/memneq.c @@ -60,6 +60,7 @@ */ #include +#include #ifndef __HAVE_ARCH_CRYPTO_MEMNEQ @@ -71,7 +72,10 @@ __crypto_memneq_generic(const void *a, const void *b, size_t size) #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) while (size >= sizeof(unsigned long)) { - neq |= *(unsigned long *)a ^ *(unsigned long *)b; + unsigned long const *p = a; + unsigned long const *q = b; + + neq |= get_unaligned(p) ^ get_unaligned(q); OPTIMIZER_HIDE_VAR(neq); a += sizeof(unsigned long); b += sizeof(unsigned long); @@ -95,18 +99,24 @@ static inline unsigned long __crypto_memneq_16(const void *a, const void *b) #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS if (sizeof(unsigned long) == 8) { - neq |= *(unsigned long *)(a) ^ *(unsigned long *)(b); + unsigned long const *p = a; + unsigned long const *q = b; + + neq |= get_unaligned(p++) ^ get_unaligned(q++); OPTIMIZER_HIDE_VAR(neq); - neq |= *(unsigned long *)(a+8) ^ *(unsigned long *)(b+8); + neq |= get_unaligned(p) ^ get_unaligned(q); OPTIMIZER_HIDE_VAR(neq); } else if (sizeof(unsigned int) == 4) { - neq |= *(unsigned int *)(a) ^ *(unsigned int *)(b); + unsigned int const *p = a; + unsigned int const *q = b; + + neq |= get_unaligned(p++) ^ get_unaligned(q++); OPTIMIZER_HIDE_VAR(neq); - neq |= *(unsigned int *)(a+4) ^ *(unsigned int *)(b+4); + neq |= get_unaligned(p++) ^ get_unaligned(q++); OPTIMIZER_HIDE_VAR(neq); - neq |= *(unsigned int *)(a+8) ^ *(unsigned int *)(b+8); + neq |= get_unaligned(p++) ^ get_unaligned(q++); OPTIMIZER_HIDE_VAR(neq); - neq |= *(unsigned int *)(a+12) ^ *(unsigned int *)(b+12); + neq |= get_unaligned(p) ^ get_unaligned(q); OPTIMIZER_HIDE_VAR(neq); } else #endif /* CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS */ From patchwork Mon Oct 8 21:15:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10631465 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3CA0015E8 for ; Mon, 8 Oct 2018 21:17:42 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2D2F229816 for ; Mon, 8 Oct 2018 21:17:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1F68229839; Mon, 8 Oct 2018 21:17:42 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BCCA429816 for ; Mon, 8 Oct 2018 21:17:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=0dYIJjG+RVvVcb5JgxRW4IUI8DRNOy6kNloGSMhrfmw=; b=ENzVDJnHrx6kr5LqQ5CZVrisqD ehuGplx8aC7grlL0XaymxslRAJ92zwJz1EdEiZY71GPHrVd75SXoBQpR4CgMGdzUxAAMFH8oUqoWl 5LwOi5HxMyllcXCD/Tr47fJ/De6gMsRkWa4vMWjqEmOsOW3rtat2h/YHVO+n3O3V6CFZ82IevWmVB eF3zBaTJblCu3F9hurahxUwOS8QvaRicQ7ngPh5JzjgAg3hFJQ9+QoB4+U8pNAvO9LdhHoqvo1aRW xUGEqt1SLhACOIzcUuFIPyRzG0pL8UxpdRweFwjnjtsifbW8Cne/kxuN7mknnf9tbd/4SLeDy5V6R UGyufOkQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1g9cuG-0007Sb-CQ; Mon, 08 Oct 2018 21:17:36 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1g9ct1-0006qt-Eo for linux-arm-kernel@lists.infradead.org; Mon, 08 Oct 2018 21:16:23 +0000 Received: by mail-wm1-x341.google.com with SMTP id 143-v6so9382025wmf.1 for ; Mon, 08 Oct 2018 14:16:09 -0700 (PDT) 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=J0aEQXXrkbEOrgPdvQLuNJVqzwZkLxj5YarnobqFpmY=; b=QICMMtwn3ZdSKPwUMQdTRIHUAtlJNyjNIadf/MSj58IyxcxqKwNtGER7cJEyvzXeXa V37zUin8m75tjh26a9BCszmlTBQYd1uXX/aRuTK0ZiMiQNBQCWZeIFEODgXxmwtabViZ ZRFxj+5OCxE215VNU4ZKErCM+4Qlujm6sNcqE= 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=J0aEQXXrkbEOrgPdvQLuNJVqzwZkLxj5YarnobqFpmY=; b=tGGuo/huL4oMKa6XnwZfROBoLU+Vs+BSn9a+7MTsNKVxNJjWoWLReo+SAuk17tECjm UeugQTeGW/IoyueeYXxQeupyiKlQ8loBDGx274zybpXAqU8M8vqORdX7sj5ZpBwBpmI+ De94et0swa8IuTguomve/SUNzukn9tP/VPbowMDYVynsayTfM8JSCEcyxvpipJW6UwK1 CgHYrmQdYplMwW7SlbdQagkGXRYhSsxki162BiDgBbgs3cbGR1hTTvXlHZKqH54kC3N0 aa6fvj+g/S2Po9bR+raS9iolHbxuy4ME3A4//GYI/esEvkHDs5R2VjDLW7/oC2xruCz4 +BfQ== X-Gm-Message-State: ABuFfojJHqYp/yqMfloXpWrddgVidk139b4PTZypbqU9t4Zs1HeaBMUx ZQF76kERd3WsSrBUB/91GDa/9w== X-Google-Smtp-Source: ACcGV61mPfy4v5RRayAL9q+XSEC/bETR7tsY/X+jYoraSK8/5s+/I5nPa26wrFIDsmrbVFkmtHoJJA== X-Received: by 2002:a1c:bce:: with SMTP id 197-v6mr15370424wml.15.1539033367816; Mon, 08 Oct 2018 14:16:07 -0700 (PDT) Received: from localhost.localdomain ([2a01:cb1d:112:6f00:8084:9715:d038:c67d]) by smtp.gmail.com with ESMTPSA id s24-v6sm7563308wmc.7.2018.10.08.14.16.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Oct 2018 14:16:07 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Subject: [PATCH 2/3] crypto: crypto_xor - use unaligned accessors for aligned fast path Date: Mon, 8 Oct 2018 23:15:53 +0200 Message-Id: <20181008211554.5355-3-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181008211554.5355-1-ard.biesheuvel@linaro.org> References: <20181008211554.5355-1-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181008_141619_960416_C4FA2462 X-CRM114-Status: GOOD ( 16.05 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jason@zx2c4.com, herbert@gondor.apana.org.au, arnd@arndb.de, Ard Biesheuvel , ebiggers@google.com, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP On ARM v6 and later, we define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS because the ordinary load/store instructions (ldr, ldrh, ldrb) can tolerate any misalignment of the memory address. However, load/store double and load/store multiple instructions (ldrd, ldm) may still only be used on memory addresses that are 32-bit aligned, and so we have to use the CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS macro with care, or we may end up with a severe performance hit due to alignment traps that require fixups by the kernel. Fortunately, the get_unaligned() accessors do the right thing: when building for ARMv6 or later, the compiler will emit unaligned accesses using the ordinary load/store instructions (but avoid the ones that require 32-bit alignment). When building for older ARM, those accessors will emit the appropriate sequence of ldrb/mov/orr instructions. And on architectures that can truly tolerate any kind of misalignment, the get_unaligned() accessors resolve to the leXX_to_cpup accessors that operate on aligned addresses. So switch to the unaligned accessors for the aligned fast path. This will create the exact same code on architectures that can really tolerate any kind of misalignment, and generate code for ARMv6+ that avoids load/store instructions that trigger alignment faults. Signed-off-by: Ard Biesheuvel --- crypto/algapi.c | 7 +++---- include/crypto/algapi.h | 11 +++++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/crypto/algapi.c b/crypto/algapi.c index 2545c5f89c4c..52ce3c5a0499 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -988,11 +988,10 @@ void crypto_inc(u8 *a, unsigned int size) __be32 *b = (__be32 *)(a + size); u32 c; - if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || - IS_ALIGNED((unsigned long)b, __alignof__(*b))) + if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) for (; size >= 4; size -= 4) { - c = be32_to_cpu(*--b) + 1; - *b = cpu_to_be32(c); + c = get_unaligned_be32(--b) + 1; + put_unaligned_be32(c, b); if (likely(c)) return; } diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h index 4a5ad10e75f0..86267c232f34 100644 --- a/include/crypto/algapi.h +++ b/include/crypto/algapi.h @@ -17,6 +17,8 @@ #include #include +#include + /* * Maximum values for blocksize and alignmask, used to allocate * static buffers that are big enough for any combination of @@ -212,7 +214,9 @@ static inline void crypto_xor(u8 *dst, const u8 *src, unsigned int size) unsigned long *s = (unsigned long *)src; while (size > 0) { - *d++ ^= *s++; + put_unaligned(get_unaligned(d) ^ get_unaligned(s), d); + d++; + s++; size -= sizeof(unsigned long); } } else { @@ -231,7 +235,10 @@ static inline void crypto_xor_cpy(u8 *dst, const u8 *src1, const u8 *src2, unsigned long *s2 = (unsigned long *)src2; while (size > 0) { - *d++ = *s1++ ^ *s2++; + put_unaligned(get_unaligned(s1) ^ get_unaligned(s2), d); + d++; + s1++; + s2++; size -= sizeof(unsigned long); } } else { From patchwork Mon Oct 8 21:15:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10631467 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 200F114DB for ; Mon, 8 Oct 2018 21:18:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2333D296AA for ; Mon, 8 Oct 2018 21:18:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 142D7296BE; Mon, 8 Oct 2018 21:18:22 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2CEE0296AA for ; Mon, 8 Oct 2018 21:18:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=pPht+0xYmXceszZ4buZHEIBgsdWhj36fjHgonq+5aq4=; b=rkzunyWcImjholyZylUWvuaFey p0eUbMg6AGBiSOwZeLXqvTrPhaUWD2bm9ru0din7KZlW105U0Goo78Rv6OrBFgrKwjc92wbp3jBAb /JcsN8lv5cUq5p9wOqB8tPRFeEtnsf6ErhBs/yEBI08xQ8RvaIdCLaVRqknL4pQfEwj4fn1oUT43R eJQrgEMx2enjTSoiXjXigRiBS7RE8FPbqoxIceCdVtzY91dUgyV0Qa+cBiXocECQWrt8zpur08418 /qWXXsMRdHdzazrahPn4Z4igtSDXbCYtEpeUfpjOGK9BmMW8X4npB4cCoPK9GXqnPS5pXlfsEswnU 8c2+ZIXQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1g9cuq-0007i3-4D; Mon, 08 Oct 2018 21:18:12 +0000 Received: from mail-wm1-x342.google.com ([2a00:1450:4864:20::342]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1g9ct3-0006qx-TK for linux-arm-kernel@lists.infradead.org; Mon, 08 Oct 2018 21:16:44 +0000 Received: by mail-wm1-x342.google.com with SMTP id 185-v6so9631120wmt.2 for ; Mon, 08 Oct 2018 14:16:10 -0700 (PDT) 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=gav2KcErAu5oEWztCs050y15OhvmwcN8SK23NHcd/0Q=; b=IklIDQ37fH9qB/852NY84rDYYN6XPnb3cTOS8BbxQpapijNSMREM3JGnd7xCAl9PcM KDwmXpYeKnR6kQ8ZN6GcHD3uCNU65Mqb6yHf4/M1COcfjFLloBBd1WI/9z/RDav8pamO CzjSg2jvzOBRSH0qDqh49OsAmnZEguKWzRlHQ= 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=gav2KcErAu5oEWztCs050y15OhvmwcN8SK23NHcd/0Q=; b=OsP6USdq0TR9jIgblNDl4WWHVf1acLF+ZfZSDymjhtqJzD6hQgdxqAAYt76edXNtRJ JkHcRECvC2CHxPYB1v8RBHGmWcWtmlzCqAcU6t4I6kgywT8DvQVghC452oPpsCo2cK9N yeeE87Vjby0qpRwy8/CvfRHbevCyPHYVuBIfwN6jIfVa7IrVaPh975P7FLQhDTnrv7t3 5fTjERp9WRUrM+Jk4vygETGxeR3JqZPihyZT5YtJ+Ye9KwWivpBYlivYPUrqRPoAPa9o TsE2Qh4PnC/jGZFEmlsaCQ4X30NFtgCO3CqO7MmMhVmyzZKw9d1FLAhG1YMRL8dlmoPH UxDQ== X-Gm-Message-State: ABuFfoiqxbg+eEgO1D20fkjQGellHOaMmrthG3ByACLZA6NET43bzFge WylAZm9tjb5WwntOcL+8V0c3Vg== X-Google-Smtp-Source: ACcGV63YWTC/jq+TuutDAGf9ZZmP+YVDQD4nQaJbnzlJQnZIVJ90cfdudwpcrBq+RQQ4LEnjPhKkqA== X-Received: by 2002:a1c:4887:: with SMTP id v129-v6mr15717607wma.139.1539033369088; Mon, 08 Oct 2018 14:16:09 -0700 (PDT) Received: from localhost.localdomain ([2a01:cb1d:112:6f00:8084:9715:d038:c67d]) by smtp.gmail.com with ESMTPSA id s24-v6sm7563308wmc.7.2018.10.08.14.16.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Oct 2018 14:16:08 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Subject: [PATCH 3/3] crypto: siphash - drop _aligned variants Date: Mon, 8 Oct 2018 23:15:54 +0200 Message-Id: <20181008211554.5355-4-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181008211554.5355-1-ard.biesheuvel@linaro.org> References: <20181008211554.5355-1-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181008_141622_013511_57395D7E X-CRM114-Status: GOOD ( 18.07 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jason@zx2c4.com, herbert@gondor.apana.org.au, arnd@arndb.de, Ard Biesheuvel , ebiggers@google.com, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP On ARM v6 and later, we define CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS because the ordinary load/store instructions (ldr, ldrh, ldrb) can tolerate any misalignment of the memory address. However, load/store double and load/store multiple instructions (ldrd, ldm) may still only be used on memory addresses that are 32-bit aligned, and so we have to use the CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS macro with care, or we may end up with a severe performance hit due to alignment traps that require fixups by the kernel. Fortunately, the get_unaligned() accessors do the right thing: when building for ARMv6 or later, the compiler will emit unaligned accesses using the ordinary load/store instructions (but avoid the ones that require 32-bit alignment). When building for older ARM, those accessors will emit the appropriate sequence of ldrb/mov/orr instructions. And on architectures that can truly tolerate any kind of misalignment, the get_unaligned() accessors resolve to the leXX_to_cpup accessors that operate on aligned addresses. Since the compiler will in fact emit ldrd or ldm instructions when building this code for ARM v6 or later, the solution is to use the unaligned accessors on the aligned code paths. Given the above, this either produces the same code, or better in the ARMv6+ case. However, since that removes the only difference between the aligned and unaligned variants, we can drop the aligned variant entirely. Signed-off-by: Ard Biesheuvel --- include/linux/siphash.h | 106 +++++++++----------- lib/siphash.c | 103 ++----------------- 2 files changed, 54 insertions(+), 155 deletions(-) diff --git a/include/linux/siphash.h b/include/linux/siphash.h index fa7a6b9cedbf..ef3c36b0ae0f 100644 --- a/include/linux/siphash.h +++ b/include/linux/siphash.h @@ -15,16 +15,14 @@ #include #include +#include #define SIPHASH_ALIGNMENT __alignof__(u64) typedef struct { u64 key[2]; } siphash_key_t; -u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key); -#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS -u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key); -#endif +u64 __siphash(const void *data, size_t len, const siphash_key_t *key); u64 siphash_1u64(const u64 a, const siphash_key_t *key); u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key); @@ -48,26 +46,6 @@ static inline u64 siphash_4u32(const u32 a, const u32 b, const u32 c, } -static inline u64 ___siphash_aligned(const __le64 *data, size_t len, - const siphash_key_t *key) -{ - if (__builtin_constant_p(len) && len == 4) - return siphash_1u32(le32_to_cpup((const __le32 *)data), key); - if (__builtin_constant_p(len) && len == 8) - return siphash_1u64(le64_to_cpu(data[0]), key); - if (__builtin_constant_p(len) && len == 16) - return siphash_2u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), - key); - if (__builtin_constant_p(len) && len == 24) - return siphash_3u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), - le64_to_cpu(data[2]), key); - if (__builtin_constant_p(len) && len == 32) - return siphash_4u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), - le64_to_cpu(data[2]), le64_to_cpu(data[3]), - key); - return __siphash_aligned(data, len, key); -} - /** * siphash - compute 64-bit siphash PRF value * @data: buffer to hash @@ -77,11 +55,30 @@ static inline u64 ___siphash_aligned(const __le64 *data, size_t len, static inline u64 siphash(const void *data, size_t len, const siphash_key_t *key) { -#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS - if (!IS_ALIGNED((unsigned long)data, SIPHASH_ALIGNMENT)) - return __siphash_unaligned(data, len, key); -#endif - return ___siphash_aligned(data, len, key); + if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { + if (__builtin_constant_p(len) && len == 4) + return siphash_1u32(get_unaligned_le32(data), + key); + if (__builtin_constant_p(len) && len == 8) + return siphash_1u64(get_unaligned_le64(data), + key); + if (__builtin_constant_p(len) && len == 16) + return siphash_2u64(get_unaligned_le64(data), + get_unaligned_le64(data + 8), + key); + if (__builtin_constant_p(len) && len == 24) + return siphash_3u64(get_unaligned_le64(data), + get_unaligned_le64(data + 8), + get_unaligned_le64(data + 16), + key); + if (__builtin_constant_p(len) && len == 32) + return siphash_4u64(get_unaligned_le64(data), + get_unaligned_le64(data + 8), + get_unaligned_le64(data + 16), + get_unaligned_le64(data + 24), + key); + } + return __siphash(data, len, key); } #define HSIPHASH_ALIGNMENT __alignof__(unsigned long) @@ -89,12 +86,7 @@ typedef struct { unsigned long key[2]; } hsiphash_key_t; -u32 __hsiphash_aligned(const void *data, size_t len, - const hsiphash_key_t *key); -#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS -u32 __hsiphash_unaligned(const void *data, size_t len, - const hsiphash_key_t *key); -#endif +u32 __hsiphash(const void *data, size_t len, const hsiphash_key_t *key); u32 hsiphash_1u32(const u32 a, const hsiphash_key_t *key); u32 hsiphash_2u32(const u32 a, const u32 b, const hsiphash_key_t *key); @@ -103,24 +95,6 @@ u32 hsiphash_3u32(const u32 a, const u32 b, const u32 c, u32 hsiphash_4u32(const u32 a, const u32 b, const u32 c, const u32 d, const hsiphash_key_t *key); -static inline u32 ___hsiphash_aligned(const __le32 *data, size_t len, - const hsiphash_key_t *key) -{ - if (__builtin_constant_p(len) && len == 4) - return hsiphash_1u32(le32_to_cpu(data[0]), key); - if (__builtin_constant_p(len) && len == 8) - return hsiphash_2u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), - key); - if (__builtin_constant_p(len) && len == 12) - return hsiphash_3u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), - le32_to_cpu(data[2]), key); - if (__builtin_constant_p(len) && len == 16) - return hsiphash_4u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), - le32_to_cpu(data[2]), le32_to_cpu(data[3]), - key); - return __hsiphash_aligned(data, len, key); -} - /** * hsiphash - compute 32-bit hsiphash PRF value * @data: buffer to hash @@ -130,11 +104,27 @@ static inline u32 ___hsiphash_aligned(const __le32 *data, size_t len, static inline u32 hsiphash(const void *data, size_t len, const hsiphash_key_t *key) { -#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS - if (!IS_ALIGNED((unsigned long)data, HSIPHASH_ALIGNMENT)) - return __hsiphash_unaligned(data, len, key); -#endif - return ___hsiphash_aligned(data, len, key); + if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) { + if (__builtin_constant_p(len) && len == 4) + return hsiphash_1u32(get_unaligned_le32(data), + key); + if (__builtin_constant_p(len) && len == 8) + return hsiphash_2u32(get_unaligned_le32(data), + get_unaligned_le32(data + 4), + key); + if (__builtin_constant_p(len) && len == 12) + return hsiphash_3u32(get_unaligned_le32(data), + get_unaligned_le32(data + 4), + get_unaligned_le32(data + 8), + key); + if (__builtin_constant_p(len) && len == 16) + return hsiphash_4u32(get_unaligned_le32(data), + get_unaligned_le32(data + 4), + get_unaligned_le32(data + 8), + get_unaligned_le32(data + 12), + key); + } + return __hsiphash(data, len, key); } #endif /* _LINUX_SIPHASH_H */ diff --git a/lib/siphash.c b/lib/siphash.c index 3ae58b4edad6..3b2ba1a10ad9 100644 --- a/lib/siphash.c +++ b/lib/siphash.c @@ -49,40 +49,7 @@ SIPROUND; \ return (v0 ^ v1) ^ (v2 ^ v3); -u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key) -{ - const u8 *end = data + len - (len % sizeof(u64)); - const u8 left = len & (sizeof(u64) - 1); - u64 m; - PREAMBLE(len) - for (; data != end; data += sizeof(u64)) { - m = le64_to_cpup(data); - v3 ^= m; - SIPROUND; - SIPROUND; - v0 ^= m; - } -#if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64 - if (left) - b |= le64_to_cpu((__force __le64)(load_unaligned_zeropad(data) & - bytemask_from_count(left))); -#else - switch (left) { - case 7: b |= ((u64)end[6]) << 48; - case 6: b |= ((u64)end[5]) << 40; - case 5: b |= ((u64)end[4]) << 32; - case 4: b |= le32_to_cpup(data); break; - case 3: b |= ((u64)end[2]) << 16; - case 2: b |= le16_to_cpup(data); break; - case 1: b |= end[0]; - } -#endif - POSTAMBLE -} -EXPORT_SYMBOL(__siphash_aligned); - -#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS -u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key) +u64 __siphash(const void *data, size_t len, const siphash_key_t *key) { const u8 *end = data + len - (len % sizeof(u64)); const u8 left = len & (sizeof(u64) - 1); @@ -112,8 +79,7 @@ u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key) #endif POSTAMBLE } -EXPORT_SYMBOL(__siphash_unaligned); -#endif +EXPORT_SYMBOL(__siphash); /** * siphash_1u64 - compute 64-bit siphash PRF value of a u64 @@ -250,39 +216,7 @@ EXPORT_SYMBOL(siphash_3u32); HSIPROUND; \ return (v0 ^ v1) ^ (v2 ^ v3); -u32 __hsiphash_aligned(const void *data, size_t len, const hsiphash_key_t *key) -{ - const u8 *end = data + len - (len % sizeof(u64)); - const u8 left = len & (sizeof(u64) - 1); - u64 m; - HPREAMBLE(len) - for (; data != end; data += sizeof(u64)) { - m = le64_to_cpup(data); - v3 ^= m; - HSIPROUND; - v0 ^= m; - } -#if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64 - if (left) - b |= le64_to_cpu((__force __le64)(load_unaligned_zeropad(data) & - bytemask_from_count(left))); -#else - switch (left) { - case 7: b |= ((u64)end[6]) << 48; - case 6: b |= ((u64)end[5]) << 40; - case 5: b |= ((u64)end[4]) << 32; - case 4: b |= le32_to_cpup(data); break; - case 3: b |= ((u64)end[2]) << 16; - case 2: b |= le16_to_cpup(data); break; - case 1: b |= end[0]; - } -#endif - HPOSTAMBLE -} -EXPORT_SYMBOL(__hsiphash_aligned); - -#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS -u32 __hsiphash_unaligned(const void *data, size_t len, +u32 __hsiphash(const void *data, size_t len, const hsiphash_key_t *key) { const u8 *end = data + len - (len % sizeof(u64)); @@ -312,8 +246,7 @@ u32 __hsiphash_unaligned(const void *data, size_t len, #endif HPOSTAMBLE } -EXPORT_SYMBOL(__hsiphash_unaligned); -#endif +EXPORT_SYMBOL(__hsiphash); /** * hsiphash_1u32 - compute 64-bit hsiphash PRF value of a u32 @@ -418,30 +351,7 @@ EXPORT_SYMBOL(hsiphash_4u32); HSIPROUND; \ return v1 ^ v3; -u32 __hsiphash_aligned(const void *data, size_t len, const hsiphash_key_t *key) -{ - const u8 *end = data + len - (len % sizeof(u32)); - const u8 left = len & (sizeof(u32) - 1); - u32 m; - HPREAMBLE(len) - for (; data != end; data += sizeof(u32)) { - m = le32_to_cpup(data); - v3 ^= m; - HSIPROUND; - v0 ^= m; - } - switch (left) { - case 3: b |= ((u32)end[2]) << 16; - case 2: b |= le16_to_cpup(data); break; - case 1: b |= end[0]; - } - HPOSTAMBLE -} -EXPORT_SYMBOL(__hsiphash_aligned); - -#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS -u32 __hsiphash_unaligned(const void *data, size_t len, - const hsiphash_key_t *key) +u32 __hsiphash(const void *data, size_t len, const hsiphash_key_t *key) { const u8 *end = data + len - (len % sizeof(u32)); const u8 left = len & (sizeof(u32) - 1); @@ -460,8 +370,7 @@ u32 __hsiphash_unaligned(const void *data, size_t len, } HPOSTAMBLE } -EXPORT_SYMBOL(__hsiphash_unaligned); -#endif +EXPORT_SYMBOL(__hsiphash); /** * hsiphash_1u32 - compute 32-bit hsiphash PRF value of a u32