Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3785815pxb; Tue, 19 Apr 2022 09:46:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzEQMCbOtjDBYZA9gzf5a52na1FkXkVtOHVYsSJIu+JsdInxmviOOfpOF59u5RSuIDeGCTq X-Received: by 2002:a17:902:cacb:b0:158:694f:23ff with SMTP id y11-20020a170902cacb00b00158694f23ffmr16678449pld.119.1650386787090; Tue, 19 Apr 2022 09:46:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650386787; cv=none; d=google.com; s=arc-20160816; b=DFl9bH1tHMhHlcyo0wERxUO+Zt1dKkQ7cbA6/t60tun/1lpTkORlPC5cC7CUlaDq84 0odlawNzMAqB6Ehgj8M+8IxJ+KuqlqMPwdtvQ6k1PIrcic7C0EIseFxpOQR+YsGzM5WQ I5EwiFXdyPRaukbE8ZuwZ+yY4HuUy4BOPjQ3ceRkykJ0DifH5qYhslIIh4xXfCchLYa1 7ky0om9FUGesRqFJCp1kXAKEbLsSNzrj04cLboGzBceNsxUabPj0pNdErRAMeo0zQjJy cuOvgj27z+S4AP8WOCZx/TsOCkrhI54hyOB5Y5EOwIRCy38AI8HeyfAosZ5TwpEUK6Nb aM6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=WvPm2+jw1IMNs3yzXrQKNPDIHO0oTw/mFv5RdwfwKug=; b=DqPuZWk9xvdOVnR5Lj3rBTmjE/183Pdhqiq9L3VSVvqWaQ+f/Xc0FrOWLHXfjFjeiW G4Kt85q1+Gcw+lsLXKHtT5t9rLwTn4TAHwrphR2TtPEc1QJdYuXYVyjpslP/OK3q2XUc xR0+CdvBwBR6Ev5G/hKuekqQ/0k8tKfDe/Bd5QCnGag/kESFP2fkyYAfVCiazfeG/sim pnyJhxWscbwz3ISxhzPkMQ4JbyZ5doPE+xb9viBXTEqA+sb4DL/30/TfwHMQjzxUvAKM +8mDzflMFj/G6s4f0GP5HwrKUZYuz3WX6aR5/imvaiV1Nr27Lccoapqans05U2fbWjcj clhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jvYSrcHT; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o18-20020a056a0015d200b004fac1f3d5f8si12618220pfu.264.2022.04.19.09.46.11; Tue, 19 Apr 2022 09:46:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jvYSrcHT; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235707AbiDRXrH (ORCPT + 99 others); Mon, 18 Apr 2022 19:47:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229666AbiDRXrG (ORCPT ); Mon, 18 Apr 2022 19:47:06 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18D7B1BEBD for ; Mon, 18 Apr 2022 16:44:26 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A1BE261269 for ; Mon, 18 Apr 2022 23:44:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B0FF2C385A7; Mon, 18 Apr 2022 23:44:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1650325465; bh=iYBJn49zqBQzPWYiNRTt8PA6tBTUPYfQbLPnh2SubZU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=jvYSrcHT38rD8JU7fCnaeHIETnfJHb8pTe+bHzj3MO71TfR4/nHzoQnbWZD+95ReD pwnMIXwKUJTfbojKaDBdZzkb0Ewdxm3llOkRLvwssULPnCGL4+DUW0bhhsfhOtesRH s7vqmTfxLjv9lb1byIhdZ2tEiKtFCwp7EtrhlsC/mjBDuiiMge6oDpchoIW4qMoq9B zQnooZN8DEUcJgOIneY3voB7eHWKkc5fsJTYautsb+cc9BMcNtb4+gPC0C2Q+Qzmuu +JPcDSx0a26tNwD+Mk9jA4GGhFIrkdmOaProYoglKcKOi5+zrfRRct84cie2RHEJBs 3OxxrDstVLIJA== Date: Mon, 18 Apr 2022 16:44:23 -0700 From: Eric Biggers To: Nathan Huckleberry Cc: linux-crypto@vger.kernel.org, Herbert Xu , "David S. Miller" , linux-arm-kernel@lists.infradead.org, Paul Crowley , Sami Tolvanen , Ard Biesheuvel Subject: Re: [PATCH v4 4/8] crypto: x86/aesni-xctr: Add accelerated implementation of XCTR Message-ID: References: <20220412172816.917723-1-nhuck@google.com> <20220412172816.917723-5-nhuck@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220412172816.917723-5-nhuck@google.com> X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Tue, Apr 12, 2022 at 05:28:12PM +0000, Nathan Huckleberry wrote: > diff --git a/arch/x86/crypto/aes_ctrby8_avx-x86_64.S b/arch/x86/crypto/aes_ctrby8_avx-x86_64.S > index 43852ba6e19c..9e20d7d3d6da 100644 > --- a/arch/x86/crypto/aes_ctrby8_avx-x86_64.S > +++ b/arch/x86/crypto/aes_ctrby8_avx-x86_64.S > @@ -53,6 +53,10 @@ > #define KEY_192 2 > #define KEY_256 3 > > +// XCTR mode only > +#define counter %r9 > +#define xiv %xmm8 > + It would be helpful if the registers were listed in order, and if the CTR-specific ones were marked as being specific to CTR. This would make it easy to verify that there are no collisions in register allocation. I.e.: [...] #define xdata7 %xmm7 #define xcounter %xmm8 // CTR mode only #define xiv %xmm8 // XCTR mode only #define xbyteswap %xmm9 // CTR mode only #define xkey0 %xmm10 [...] #define num_bytes %r8 #define counter %r9 // XCTR mode only #define tmp %r10 [...] I'm also not a fan of the naming, with "xcounter" being used by CTR only and "counter" being used by XCTR only... I see why you did it, though, as the existing code uses the "x" prefix to mean "this is an xmm register". It could at least use a comment that makes this super clear, though: // Note: the "x" prefix in these aliases means "this is an xmm register". // No relation to XCTR where the "X" prefix means "XOR counter". #define xdata0 %xmm0 > + .if (\xctr == 1) As \xctr is either 0 or 1, this can be written as simply '.if \xctr' > + .set i, 0 > + .rept (by) > + club XDATA, i > + movq counter, var_xdata > + .set i, (i +1) > + .endr > + .endif > + Since the 3-operand add instruction (vpaddq) is available here, and in fact is being used already, it isn't necessary to move 'counter' into all (up to 8) of the var_xdata registers. Just move it into the last var_xdata register, or into a temporary register, and use it as a source operand for all the additions. > - vpshufb xbyteswap, xcounter, xdata0 > - > - .set i, 1 > - .rept (by - 1) > - club XDATA, i > - vpaddq (ddq_add_1 + 16 * (i - 1))(%rip), xcounter, var_xdata > - vptest ddq_low_msk(%rip), var_xdata > - jnz 1f > - vpaddq ddq_high_add_1(%rip), var_xdata, var_xdata > - vpaddq ddq_high_add_1(%rip), xcounter, xcounter > - 1: > - vpshufb xbyteswap, var_xdata, var_xdata > - .set i, (i +1) > - .endr > + .if (\xctr == 0) > + vpshufb xbyteswap, xcounter, xdata0 > + .set i, 1 > + .rept (by - 1) > + club XDATA, i > + vpaddq (ddq_add_1 + 16 * (i - 1))(%rip), xcounter, var_xdata > + vptest ddq_low_msk(%rip), var_xdata > + jnz 1f > + vpaddq ddq_high_add_1(%rip), var_xdata, var_xdata > + vpaddq ddq_high_add_1(%rip), xcounter, xcounter > + 1: > + vpshufb xbyteswap, var_xdata, var_xdata > + .set i, (i +1) > + .endr > + .endif > + .if (\xctr == 1) > + .set i, 0 > + .rept (by) > + club XDATA, i > + vpaddq (ddq_add_1 + 16 * i)(%rip), var_xdata, var_xdata > + .set i, (i +1) > + .endr > + .set i, 0 > + .rept (by) > + club XDATA, i > + vpxor xiv, var_xdata, var_xdata > + .set i, (i +1) > + .endr > + .endif This can be written as: .if \xctr [second part above] .else [first part above] .endif > - vpaddq (ddq_add_1 + 16 * (by - 1))(%rip), xcounter, xcounter > - vptest ddq_low_msk(%rip), xcounter > - jnz 1f > - vpaddq ddq_high_add_1(%rip), xcounter, xcounter > - 1: > + .if (\xctr == 0) > + vpaddq (ddq_add_1 + 16 * (by - 1))(%rip), xcounter, xcounter > + vptest ddq_low_msk(%rip), xcounter > + jnz 1f > + vpaddq ddq_high_add_1(%rip), xcounter, xcounter > + 1: > + .endif > + .if (\xctr == 1) > + add $by, counter > + .endif Likewise here. > +.macro do_aes_ctrmain key_len, xctr > cmp $16, num_bytes > - jb .Ldo_return2\key_len > + jb .Ldo_return2\xctr\key_len > > vmovdqa byteswap_const(%rip), xbyteswap > - vmovdqu (p_iv), xcounter > - vpshufb xbyteswap, xcounter, xcounter > + .if (\xctr == 0) > + vmovdqu (p_iv), xcounter > + vpshufb xbyteswap, xcounter, xcounter > + .endif > + .if (\xctr == 1) > + andq $(~0xf), num_bytes > + shr $4, counter > + vmovdqu (p_iv), xiv > + .endif And likewise here. Also, the load of byteswap_const can be moved into the !\xctr block. - Eric