Message ID | 20230919-optimize_checksum-v7-2-06c7d0ddd5d6@rivosinc.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp3789055vqi; Tue, 19 Sep 2023 18:01:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHTzZWxx40M7A0rUHoLS47RMFibootfCck6TX1AQ4UQdi7Qf6l24qqG0bblWtKntw6FQN/+ X-Received: by 2002:a05:6a20:2594:b0:15a:290:d833 with SMTP id k20-20020a056a20259400b0015a0290d833mr1104144pzd.53.1695171673229; Tue, 19 Sep 2023 18:01:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695171673; cv=none; d=google.com; s=arc-20160816; b=0Vt2WQksSWkFTy5yZeqisGDNMCvkb/F0fN9h6YGwS1BT9Vu3WSS9xyClfikEtKn3/q eAi1IDlLKyxtCGPlpPHGucV+NaCRHUCjSwq0Y0idPOt7tZrZTyJTGA6/4JAMFNdhK0cl 1qdiRfKz+d1Qn3B0Siq9Jh7wWbJFpJBj7klkAeMrquslY1Jni0FlvW/WPlmTNQ1c/x0a Vnyq+OFgAlFWJVKY0KoeRyTaPPUBzeU+QkPLw8iCmH1HQbOdwtoKrzxdMif4OEKfxRmC 0qfybYpNzXdYpXndqh2OQY4c+ieh2QZsgxouN8ZIHMezAxY1O90O1yKPDD76pEetzofO yk6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=1k75qxIvoyjoXO7i5f1b9o5ew6JdXdHy21zFkpaW4Nc=; fh=OdLM5KLdAYlMPhaKzudfbUgmxJEOsES1wczB6MEdoso=; b=Un0Oovxa47pExqJUHY3ieAz7/k8XJtkpcLPPXnYSHUqJ8VMv1/IxVUsRyO8k0LW/23 77AjsdqQ2mJbYD7ufOsBOw8m//h9EeNRRQWjpKKn6ncCkcLT7aqLB0dAwB2IV7iZbOTZ AAI+Lfwu255BuJz72NiTjPH8yq6EHSaZztpi+r7+1IZ2FN4KLulEunOUAydwyxQj4j5n UjB4t5x/s5aTuyCqRZMIN3PHCHvc8g6MHWI+pI7G6DiAjquREcfE21qOM8gNr8Sq5kW/ M1KZWJcY+9B/bqBN2Y1IxUocncxYkOtRSMTL2Bce8CzeVzTv+kyO9rEztDYDXFi/Yv4W CFpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=IFhhoGu+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id t16-20020a056a00139000b00690204af234si11006983pfg.378.2023.09.19.18.01.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 18:01:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=IFhhoGu+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 02A0181E9A72; Tue, 19 Sep 2023 11:45:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232441AbjISSpM (ORCPT <rfc822;toshivichauhan@gmail.com> + 26 others); Tue, 19 Sep 2023 14:45:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232103AbjISSpG (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 19 Sep 2023 14:45:06 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 073B59D for <linux-kernel@vger.kernel.org>; Tue, 19 Sep 2023 11:45:01 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-565e54cb93aso3526368a12.3 for <linux-kernel@vger.kernel.org>; Tue, 19 Sep 2023 11:45:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1695149100; x=1695753900; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1k75qxIvoyjoXO7i5f1b9o5ew6JdXdHy21zFkpaW4Nc=; b=IFhhoGu+M64abnPeiZDAzqKOKytYu/j/DOCTSfKK3hqT/ryiV1Vs33YRGUxrnXgiLT XWuHBzhPUUNEE+ke4klLvt7lJMpVlMDFxdyXdCp1zXIZq4xvBQjUT9B/u0GsQZbd/l6D BczNw4C9Vq3McNMhhZ7I3MSPJQXvqcmYJaLSiiioJsL3kcx6525uEh380j52n0uxj+D6 tQjO6HCb0jMVHgiXUVwp+5k1NuhIcB9Dpyyjo8n8og+4UVBXrpjNi/9tT5pujVwRZ9qj 3RUpKesaV5hDu47nYMxAcSDhjb2ZCrtWmp+EEdZylptKWT/5/Kgb255jxo/889OVQYa4 HPcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695149100; x=1695753900; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1k75qxIvoyjoXO7i5f1b9o5ew6JdXdHy21zFkpaW4Nc=; b=h4XdOX9boHyRT8uSDC/L7FVmfwV1EvzGRczdYQTWsnjZoOo/mLhphISSCqnI86msuC qhHFeJVMn20GlAXSlv+4QpnXYxvDywLDEgJAkxlj2pm7SO9XOP20yMghmOznaGjBnqBx hXDRU4lMd1YVVYun8UgHVPmXDqKn3Km39e1tMVcDXVdPF7lgUATbWq5pKulsIEy9JEB7 ZSLCHqD4FgwnocuZQylXvTKnWIvjgbFT9FP5W5ahZBzFwjhDbBKPVG1inYVcV3QCOg9c /kEScW1NQI84umTsoi51oAlegen/b09CV/HCBS908sWDZ+KcJKCbpf2tbc972ocGZJiS eNKw== X-Gm-Message-State: AOJu0Yybpj2qEtglDBrv0cEqPR49HXx6Vgg6iAKmj02WvWHTkhyyVYNo ymLw9uv0nWhNigkV+tyhZBosCg== X-Received: by 2002:a17:90a:f308:b0:26f:4685:5b69 with SMTP id ca8-20020a17090af30800b0026f46855b69mr617613pjb.7.1695149100481; Tue, 19 Sep 2023 11:45:00 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id m5-20020a17090b068500b0026309d57724sm3876846pjz.39.2023.09.19.11.44.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 11:45:00 -0700 (PDT) From: Charlie Jenkins <charlie@rivosinc.com> Date: Tue, 19 Sep 2023 11:44:31 -0700 Subject: [PATCH v7 2/4] riscv: Checksum header MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230919-optimize_checksum-v7-2-06c7d0ddd5d6@rivosinc.com> References: <20230919-optimize_checksum-v7-0-06c7d0ddd5d6@rivosinc.com> In-Reply-To: <20230919-optimize_checksum-v7-0-06c7d0ddd5d6@rivosinc.com> To: Charlie Jenkins <charlie@rivosinc.com>, Palmer Dabbelt <palmer@dabbelt.com>, Conor Dooley <conor@kernel.org>, Samuel Holland <samuel.holland@sifive.com>, David Laight <David.Laight@aculab.com>, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Paul Walmsley <paul.walmsley@sifive.com>, Albert Ou <aou@eecs.berkeley.edu>, Arnd Bergmann <arnd@arndb.de> X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 19 Sep 2023 11:45:11 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777516332517167148 X-GMAIL-MSGID: 1777516332517167148 |
Series | riscv: Add fine-tuned checksum functions | |
Commit Message
Charlie Jenkins
Sept. 19, 2023, 6:44 p.m. UTC
Provide checksum algorithms that have been designed to leverage riscv
instructions such as rotate. In 64-bit, can take advantage of the larger
register to avoid some overflow checking.
Signed-off-by: Charlie Jenkins <charlie@rivosinc.com>
---
arch/riscv/include/asm/checksum.h | 79 +++++++++++++++++++++++++++++++++++++++
1 file changed, 79 insertions(+)
Comments
On Tue, Sep 19, 2023 at 11:44:31AM -0700, Charlie Jenkins wrote: > Provide checksum algorithms that have been designed to leverage riscv > instructions such as rotate. In 64-bit, can take advantage of the larger > register to avoid some overflow checking. > > Signed-off-by: Charlie Jenkins <charlie@rivosinc.com> Same here, Acked-by: Conor Dooley <conor.dooley@microchip.com> I think there are some "issues" attributed to this patch by the automation - but they spurious. This diff here could not cause a drivers/block/drbd/drbd_bitmap.c:1271: warning: Function parameter or member 'peer_device' not described in 'drbd_bm_write_copy_pages' after all. Cheers, Conor. > --- > arch/riscv/include/asm/checksum.h | 79 +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 79 insertions(+) > > diff --git a/arch/riscv/include/asm/checksum.h b/arch/riscv/include/asm/checksum.h > new file mode 100644 > index 000000000000..dc0dd89f2a13 > --- /dev/null > +++ b/arch/riscv/include/asm/checksum.h > @@ -0,0 +1,79 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * IP checksum routines > + * > + * Copyright (C) 2023 Rivos Inc. > + */ > +#ifndef __ASM_RISCV_CHECKSUM_H > +#define __ASM_RISCV_CHECKSUM_H > + > +#include <linux/in6.h> > +#include <linux/uaccess.h> > + > +#define ip_fast_csum ip_fast_csum > + > +#include <asm-generic/checksum.h> > + > +/* > + * Quickly compute an IP checksum with the assumption that IPv4 headers will > + * always be in multiples of 32-bits, and have an ihl of at least 5. > + * @ihl is the number of 32 bit segments and must be greater than or equal to 5. > + * @iph is assumed to be word aligned. > + */ > +static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl) > +{ > + unsigned long csum = 0; > + int pos = 0; > + > + do { > + csum += ((const unsigned int *)iph)[pos]; > + if (IS_ENABLED(CONFIG_32BIT)) > + csum += csum < ((const unsigned int *)iph)[pos]; > + } while (++pos < ihl); > + > + /* > + * ZBB only saves three instructions on 32-bit and five on 64-bit so not > + * worth checking if supported without Alternatives. > + */ > + if (IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && > + IS_ENABLED(CONFIG_RISCV_ALTERNATIVE)) { > + unsigned long fold_temp; > + > + asm_volatile_goto(ALTERNATIVE("j %l[no_zbb]", "nop", 0, > + RISCV_ISA_EXT_ZBB, 1) > + : > + : > + : > + : no_zbb); > + > + if (IS_ENABLED(CONFIG_32BIT)) { > + asm(".option push \n\ > + .option arch,+zbb \n\ > + not %[fold_temp], %[csum] \n\ > + rori %[csum], %[csum], 16 \n\ > + sub %[csum], %[fold_temp], %[csum] \n\ > + .option pop" > + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); > + } else { > + asm(".option push \n\ > + .option arch,+zbb \n\ > + rori %[fold_temp], %[csum], 32 \n\ > + add %[csum], %[fold_temp], %[csum] \n\ > + srli %[csum], %[csum], 32 \n\ > + not %[fold_temp], %[csum] \n\ > + roriw %[csum], %[csum], 16 \n\ > + subw %[csum], %[fold_temp], %[csum] \n\ > + .option pop" > + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); > + } > + return csum >> 16; > + } > +no_zbb: > +#ifndef CONFIG_32BIT > + csum += (csum >> 32) | (csum << 32); > + csum >>= 32; > +#endif > + return csum_fold((__force __wsum)csum); > +} > + > +#endif // __ASM_RISCV_CHECKSUM_H > > -- > 2.42.0 >
Hi Charlie, > -----Original Message----- > From: linux-riscv <linux-riscv-bounces@lists.infradead.org> On Behalf Of > Charlie Jenkins > Sent: Wednesday, September 20, 2023 2:45 AM > To: Charlie Jenkins <charlie@rivosinc.com>; Palmer Dabbelt > <palmer@dabbelt.com>; Conor Dooley <conor@kernel.org>; Samuel Holland > <samuel.holland@sifive.com>; David Laight <David.Laight@aculab.com>; > linux-riscv@lists.infradead.org; linux-kernel@vger.kernel.org; linux- > arch@vger.kernel.org > Cc: Paul Walmsley <paul.walmsley@sifive.com>; Albert Ou > <aou@eecs.berkeley.edu>; Arnd Bergmann <arnd@arndb.de> > Subject: [PATCH v7 2/4] riscv: Checksum header > > Provide checksum algorithms that have been designed to leverage riscv > instructions such as rotate. In 64-bit, can take advantage of the larger > register to avoid some overflow checking. > > Signed-off-by: Charlie Jenkins <charlie@rivosinc.com> > --- > arch/riscv/include/asm/checksum.h | 79 > +++++++++++++++++++++++++++++++++++++++ > 1 file changed, 79 insertions(+) > > diff --git a/arch/riscv/include/asm/checksum.h > b/arch/riscv/include/asm/checksum.h > new file mode 100644 > index 000000000000..dc0dd89f2a13 > --- /dev/null > +++ b/arch/riscv/include/asm/checksum.h > @@ -0,0 +1,79 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * IP checksum routines > + * > + * Copyright (C) 2023 Rivos Inc. > + */ > +#ifndef __ASM_RISCV_CHECKSUM_H > +#define __ASM_RISCV_CHECKSUM_H > + > +#include <linux/in6.h> > +#include <linux/uaccess.h> > + > +#define ip_fast_csum ip_fast_csum > + > +#include <asm-generic/checksum.h> > + > +/* > + * Quickly compute an IP checksum with the assumption that IPv4 headers > will > + * always be in multiples of 32-bits, and have an ihl of at least 5. > + * @ihl is the number of 32 bit segments and must be greater than or equal > to 5. > + * @iph is assumed to be word aligned. Not sure if the assumption is always true. It looks the implementation in "lib/checksum.c" doesn't take this assumption. The ip header can comes after a 14-Byte ether header, which may start from a word-aligned or DMA friendly address. > + */ > +static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl) > +{ > + unsigned long csum = 0; > + int pos = 0; > + > + do { > + csum += ((const unsigned int *)iph)[pos]; > + if (IS_ENABLED(CONFIG_32BIT)) > + csum += csum < ((const unsigned int *)iph)[pos]; > + } while (++pos < ihl); > + > + /* > + * ZBB only saves three instructions on 32-bit and five on 64-bit so not > + * worth checking if supported without Alternatives. > + */ > + if (IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && > + IS_ENABLED(CONFIG_RISCV_ALTERNATIVE)) { > + unsigned long fold_temp; > + > + asm_volatile_goto(ALTERNATIVE("j %l[no_zbb]", "nop", 0, > + RISCV_ISA_EXT_ZBB, 1) > + : > + : > + : > + : no_zbb); > + > + if (IS_ENABLED(CONFIG_32BIT)) { > + asm(".option push \n\ > + .option arch,+zbb \n\ > + not %[fold_temp], %[csum] > \n\ > + rori %[csum], %[csum], 16 \n\ > + sub %[csum], %[fold_temp], %[csum] > \n\ > + .option pop" > + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); > + } else { > + asm(".option push \n\ > + .option arch,+zbb \n\ > + rori %[fold_temp], %[csum], 32 \n\ > + add %[csum], %[fold_temp], %[csum] > \n\ > + srli %[csum], %[csum], 32 \n\ > + not %[fold_temp], %[csum] > \n\ > + roriw %[csum], %[csum], 16 \n\ > + subw %[csum], %[fold_temp], %[csum] > \n\ > + .option pop" > + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); > + } > + return csum >> 16; > + } > +no_zbb: > +#ifndef CONFIG_32BIT > + csum += (csum >> 32) | (csum << 32); Just like patch 3/4 does, we can call ror64(csum, 32). BRs, Xiao > + csum >>= 32; > +#endif > + return csum_fold((__force __wsum)csum); > +} > + > +#endif // __ASM_RISCV_CHECKSUM_H > > -- > 2.42.0 > > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv
On Wed, Oct 25, 2023 at 06:50:05AM +0000, Wang, Xiao W wrote: > Hi Charlie, > > > -----Original Message----- > > From: linux-riscv <linux-riscv-bounces@lists.infradead.org> On Behalf Of > > Charlie Jenkins > > Sent: Wednesday, September 20, 2023 2:45 AM > > To: Charlie Jenkins <charlie@rivosinc.com>; Palmer Dabbelt > > <palmer@dabbelt.com>; Conor Dooley <conor@kernel.org>; Samuel Holland > > <samuel.holland@sifive.com>; David Laight <David.Laight@aculab.com>; > > linux-riscv@lists.infradead.org; linux-kernel@vger.kernel.org; linux- > > arch@vger.kernel.org > > Cc: Paul Walmsley <paul.walmsley@sifive.com>; Albert Ou > > <aou@eecs.berkeley.edu>; Arnd Bergmann <arnd@arndb.de> > > Subject: [PATCH v7 2/4] riscv: Checksum header > > > > Provide checksum algorithms that have been designed to leverage riscv > > instructions such as rotate. In 64-bit, can take advantage of the larger > > register to avoid some overflow checking. > > > > Signed-off-by: Charlie Jenkins <charlie@rivosinc.com> > > --- > > arch/riscv/include/asm/checksum.h | 79 > > +++++++++++++++++++++++++++++++++++++++ > > 1 file changed, 79 insertions(+) > > > > diff --git a/arch/riscv/include/asm/checksum.h > > b/arch/riscv/include/asm/checksum.h > > new file mode 100644 > > index 000000000000..dc0dd89f2a13 > > --- /dev/null > > +++ b/arch/riscv/include/asm/checksum.h > > @@ -0,0 +1,79 @@ > > +/* SPDX-License-Identifier: GPL-2.0 */ > > +/* > > + * IP checksum routines > > + * > > + * Copyright (C) 2023 Rivos Inc. > > + */ > > +#ifndef __ASM_RISCV_CHECKSUM_H > > +#define __ASM_RISCV_CHECKSUM_H > > + > > +#include <linux/in6.h> > > +#include <linux/uaccess.h> > > + > > +#define ip_fast_csum ip_fast_csum > > + > > +#include <asm-generic/checksum.h> > > + > > +/* > > + * Quickly compute an IP checksum with the assumption that IPv4 headers > > will > > + * always be in multiples of 32-bits, and have an ihl of at least 5. > > + * @ihl is the number of 32 bit segments and must be greater than or equal > > to 5. > > + * @iph is assumed to be word aligned. > > Not sure if the assumption is always true. It looks the implementation in "lib/checksum.c" doesn't take this assumption. > The ip header can comes after a 14-Byte ether header, which may start from a word-aligned or DMA friendly address. While lib/checksum.c does not make this assumption, other architectures (x86, ARM, powerpc, mips, arc) do make this assumption. Architectures seem to only align the header on a word boundary in do_csum. I worry that the benefit of aligning iph in this "fast" csum function would disproportionately impact hardware that has fast misaligned accesses. - Charlie > > > + */ > > +static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl) > > +{ > > + unsigned long csum = 0; > > + int pos = 0; > > + > > + do { > > + csum += ((const unsigned int *)iph)[pos]; > > + if (IS_ENABLED(CONFIG_32BIT)) > > + csum += csum < ((const unsigned int *)iph)[pos]; > > + } while (++pos < ihl); > > + > > + /* > > + * ZBB only saves three instructions on 32-bit and five on 64-bit so not > > + * worth checking if supported without Alternatives. > > + */ > > + if (IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && > > + IS_ENABLED(CONFIG_RISCV_ALTERNATIVE)) { > > + unsigned long fold_temp; > > + > > + asm_volatile_goto(ALTERNATIVE("j %l[no_zbb]", "nop", 0, > > + RISCV_ISA_EXT_ZBB, 1) > > + : > > + : > > + : > > + : no_zbb); > > + > > + if (IS_ENABLED(CONFIG_32BIT)) { > > + asm(".option push \n\ > > + .option arch,+zbb \n\ > > + not %[fold_temp], %[csum] > > \n\ > > + rori %[csum], %[csum], 16 \n\ > > + sub %[csum], %[fold_temp], %[csum] > > \n\ > > + .option pop" > > + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); > > + } else { > > + asm(".option push \n\ > > + .option arch,+zbb \n\ > > + rori %[fold_temp], %[csum], 32 \n\ > > + add %[csum], %[fold_temp], %[csum] > > \n\ > > + srli %[csum], %[csum], 32 \n\ > > + not %[fold_temp], %[csum] > > \n\ > > + roriw %[csum], %[csum], 16 \n\ > > + subw %[csum], %[fold_temp], %[csum] > > \n\ > > + .option pop" > > + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); > > + } > > + return csum >> 16; > > + } > > +no_zbb: > > +#ifndef CONFIG_32BIT > > + csum += (csum >> 32) | (csum << 32); > > Just like patch 3/4 does, we can call ror64(csum, 32). > > BRs, > Xiao > > > + csum >>= 32; > > +#endif > > + return csum_fold((__force __wsum)csum); > > +} > > + > > +#endif // __ASM_RISCV_CHECKSUM_H > > > > -- > > 2.42.0 > > > > > > _______________________________________________ > > linux-riscv mailing list > > linux-riscv@lists.infradead.org > > http://lists.infradead.org/mailman/listinfo/linux-riscv
On Wed, Oct 25, 2023, at 22:37, Charlie Jenkins wrote: > On Wed, Oct 25, 2023 at 06:50:05AM +0000, Wang, Xiao W wrote: >> > + >> > +/* >> > + * Quickly compute an IP checksum with the assumption that IPv4 headers >> > will >> > + * always be in multiples of 32-bits, and have an ihl of at least 5. >> > + * @ihl is the number of 32 bit segments and must be greater than or equal >> > to 5. >> > + * @iph is assumed to be word aligned. >> >> Not sure if the assumption is always true. It looks the implementation in "lib/checksum.c" doesn't take this assumption. >> The ip header can comes after a 14-Byte ether header, which may start from a word-aligned or DMA friendly address. > > While lib/checksum.c does not make this assumption, other architectures > (x86, ARM, powerpc, mips, arc) do make this assumption. Architectures > seem to only align the header on a word boundary in do_csum. I worry > that the benefit of aligning iph in this "fast" csum function would > disproportionately impact hardware that has fast misaligned accesses. Most architectures set NET_IP_ALIGN to '2', which is intended to have the IP header at a 32-bit aligned address, though some other targets don't bother: arch/arm64/include/asm/processor.h:#define NET_IP_ALIGN 0 arch/powerpc/include/asm/processor.h:#define NET_IP_ALIGN 0 arch/x86/include/asm/processor.h:#define NET_IP_ALIGN 0 include/linux/skbuff.h:#define NET_IP_ALIGN 2 I think it's considered a driver bug if an SKB ends up with a misaligned IP header, but it's also something that some of the more obscure drivers get wrong. Arnd
On Wed, Oct 25, 2023 at 10:52:22PM +0200, Arnd Bergmann wrote: > On Wed, Oct 25, 2023, at 22:37, Charlie Jenkins wrote: > > On Wed, Oct 25, 2023 at 06:50:05AM +0000, Wang, Xiao W wrote: > > >> > + > >> > +/* > >> > + * Quickly compute an IP checksum with the assumption that IPv4 headers > >> > will > >> > + * always be in multiples of 32-bits, and have an ihl of at least 5. > >> > + * @ihl is the number of 32 bit segments and must be greater than or equal > >> > to 5. > >> > + * @iph is assumed to be word aligned. > >> > >> Not sure if the assumption is always true. It looks the implementation in "lib/checksum.c" doesn't take this assumption. > >> The ip header can comes after a 14-Byte ether header, which may start from a word-aligned or DMA friendly address. > > > > While lib/checksum.c does not make this assumption, other architectures > > (x86, ARM, powerpc, mips, arc) do make this assumption. Architectures > > seem to only align the header on a word boundary in do_csum. I worry > > that the benefit of aligning iph in this "fast" csum function would > > disproportionately impact hardware that has fast misaligned accesses. > > Most architectures set NET_IP_ALIGN to '2', which is intended > to have the IP header at a 32-bit aligned address, though > some other targets don't bother: > > arch/arm64/include/asm/processor.h:#define NET_IP_ALIGN 0 > arch/powerpc/include/asm/processor.h:#define NET_IP_ALIGN 0 > arch/x86/include/asm/processor.h:#define NET_IP_ALIGN 0 > include/linux/skbuff.h:#define NET_IP_ALIGN 2 > > I think it's considered a driver bug if an SKB ends up > with a misaligned IP header, but it's also something that > some of the more obscure drivers get wrong. > > Arnd Thank you for pointing that out, I had not realized that macro existed. Since riscv keeps NET_IP_ALIGN at 0 it should be expected that ip_fast_csum is only called with 32-bit aligned addresses. I will update the comment and refer to that macro. riscv supports misaligned accesses but there are no guarantees of speed. - Charlie
On Wed, Oct 25, 2023, at 23:11, Charlie Jenkins wrote: > > Thank you for pointing that out, I had not realized that macro existed. > Since riscv keeps NET_IP_ALIGN at 0 it should be expected that > ip_fast_csum is only called with 32-bit aligned addresses. I will update > the comment and refer to that macro. riscv supports misaligned accesses > but there are no guarantees of speed. Just to clarify for your comment: riscv gets the default value of '2', which is the one that makes the header aligned. Arnd
On Wed, Oct 25, 2023 at 11:18:40PM +0200, Arnd Bergmann wrote: > On Wed, Oct 25, 2023, at 23:11, Charlie Jenkins wrote: > > > > Thank you for pointing that out, I had not realized that macro existed. > > Since riscv keeps NET_IP_ALIGN at 0 it should be expected that > > ip_fast_csum is only called with 32-bit aligned addresses. I will update > > the comment and refer to that macro. riscv supports misaligned accesses > > but there are no guarantees of speed. > > Just to clarify for your comment: riscv gets the default value of '2', > which is the one that makes the header aligned. > > Arnd Oops, typo. I meant to write 2. - Charlie
diff --git a/arch/riscv/include/asm/checksum.h b/arch/riscv/include/asm/checksum.h new file mode 100644 index 000000000000..dc0dd89f2a13 --- /dev/null +++ b/arch/riscv/include/asm/checksum.h @@ -0,0 +1,79 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * IP checksum routines + * + * Copyright (C) 2023 Rivos Inc. + */ +#ifndef __ASM_RISCV_CHECKSUM_H +#define __ASM_RISCV_CHECKSUM_H + +#include <linux/in6.h> +#include <linux/uaccess.h> + +#define ip_fast_csum ip_fast_csum + +#include <asm-generic/checksum.h> + +/* + * Quickly compute an IP checksum with the assumption that IPv4 headers will + * always be in multiples of 32-bits, and have an ihl of at least 5. + * @ihl is the number of 32 bit segments and must be greater than or equal to 5. + * @iph is assumed to be word aligned. + */ +static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl) +{ + unsigned long csum = 0; + int pos = 0; + + do { + csum += ((const unsigned int *)iph)[pos]; + if (IS_ENABLED(CONFIG_32BIT)) + csum += csum < ((const unsigned int *)iph)[pos]; + } while (++pos < ihl); + + /* + * ZBB only saves three instructions on 32-bit and five on 64-bit so not + * worth checking if supported without Alternatives. + */ + if (IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && + IS_ENABLED(CONFIG_RISCV_ALTERNATIVE)) { + unsigned long fold_temp; + + asm_volatile_goto(ALTERNATIVE("j %l[no_zbb]", "nop", 0, + RISCV_ISA_EXT_ZBB, 1) + : + : + : + : no_zbb); + + if (IS_ENABLED(CONFIG_32BIT)) { + asm(".option push \n\ + .option arch,+zbb \n\ + not %[fold_temp], %[csum] \n\ + rori %[csum], %[csum], 16 \n\ + sub %[csum], %[fold_temp], %[csum] \n\ + .option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); + } else { + asm(".option push \n\ + .option arch,+zbb \n\ + rori %[fold_temp], %[csum], 32 \n\ + add %[csum], %[fold_temp], %[csum] \n\ + srli %[csum], %[csum], 32 \n\ + not %[fold_temp], %[csum] \n\ + roriw %[csum], %[csum], 16 \n\ + subw %[csum], %[fold_temp], %[csum] \n\ + .option pop" + : [csum] "+r" (csum), [fold_temp] "=&r" (fold_temp)); + } + return csum >> 16; + } +no_zbb: +#ifndef CONFIG_32BIT + csum += (csum >> 32) | (csum << 32); + csum >>= 32; +#endif + return csum_fold((__force __wsum)csum); +} + +#endif // __ASM_RISCV_CHECKSUM_H