Message ID | 20230915-optimize_checksum-v6-0-14a6cf61c618@rivosinc.com |
---|---|
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 h50csp1270242vqi; Fri, 15 Sep 2023 12:23:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE5L/MilBl2UFMA2EuwoUXjxoEDqk4DLgX4gDV3WGBwNrwn+YT/rwexciPNitYk+QR92tsw X-Received: by 2002:a17:90a:e018:b0:26b:7d8e:edf9 with SMTP id u24-20020a17090ae01800b0026b7d8eedf9mr2078023pjy.49.1694805827632; Fri, 15 Sep 2023 12:23:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694805827; cv=none; d=google.com; s=arc-20160816; b=ZCnvYwLnuoPgQlFeXDNHCv18UDLpOTKGpyzDX1llgcuUfbmcrpglOiiZgUS2uirJu1 EmrBJADtXSHGfrkihE8w0Nz/FrTbbdcbrRkczq6FRcPX2/r24g1u4SnxebO3TwxQBb81 oa+x/pi759VgLJCI5CXgBcJULJbkAh6/HaDMM51VhwRqIABBC1mBaxXkKq4+UgHJKVAE uVA50kd84LcDs1hsDgGqQnXRpUENsKrTIJbSVGrZdHzf+Ob4JM6VuWLUAynbPe57YcyY sttZTn/tUtnew6WQc1NoZw99QNLpUjICKtju+Y6V2N4UAIiJ4Z4+s7KYe8cbIVG4M/VN EKUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:content-transfer-encoding:mime-version :message-id:date:subject:from:dkim-signature; bh=Ln+7rHA69/YQ6l7rezPa2HlFS+4ma5eDvscwKBBdEIw=; fh=Y565EP4XCbbHzyjsLQMNVA5sHkqrC9GPqaO5xfn/wHs=; b=GW24u7MlqfnQWmy18d+uROQ+uuIAUtP+Mel3BvK/zck9TmsgcWpw9RNCuvCFqqzUE7 sHyztT8AGl30NwFV7jZgHexpZix6gPJkHjBgVrfiFdOIpm2kjQtxhIxqukcj3KIqW4AH 4+omiD4RMo45Hbb5LV+hSiq1l4UDVBkYZGjvEguHW45U0YMOsjMN2O57mhpe2Y/As/tN ODLxv3wHtvp7ResyGgr0irhDho5y+DCOOWRJE/ehShtR77/q5Dh2ZDIPTfuNEw4yrxzN 5KTAU20T59Ym6y4wr5doJePZmz7GY6YpI7vJ+QQa4/nz2/i18YUiu8KhCCrZAU7nUGrc bgeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ISknroYH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id cu2-20020a17090afa8200b00274880d2b70si2385923pjb.8.2023.09.15.12.23.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 12:23:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=ISknroYH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (Postfix) with ESMTP id EA8B2838218D; Fri, 15 Sep 2023 10:03:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235290AbjIORDD (ORCPT <rfc822;ruipengqi7@gmail.com> + 30 others); Fri, 15 Sep 2023 13:03:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235429AbjIORCg (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 15 Sep 2023 13:02:36 -0400 Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C156DE66 for <linux-kernel@vger.kernel.org>; Fri, 15 Sep 2023 10:02:30 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1c35ee3b0d2so18909995ad.2 for <linux-kernel@vger.kernel.org>; Fri, 15 Sep 2023 10:02:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1694797350; x=1695402150; darn=vger.kernel.org; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:from:to:cc:subject:date:message-id:reply-to; bh=Ln+7rHA69/YQ6l7rezPa2HlFS+4ma5eDvscwKBBdEIw=; b=ISknroYH8Pez9fMLY0dBDdnB/AAiPDtt3kDdrK9e48FLoWdA/xJ2zaGfPMGpwL3aM+ TZr/VOErl4qkIHraP4tT05KIvsLm/wPr40uva1sqNDWGMrG+wx9h7TbHkbUgzahu7Js1 zMOYgJHBCkKwjMvfw1DksM+6YZQ80Ik3dVqcOcYGod/bzm91oBCcnu0JTKDFhLwVRMA8 qrdXji7Nt8IOiB4B4R5zc3ERkDhZ4zGEP1j70e9vGzofDjeZKYOKxOlP2zf1mfawQUnX 7xoFmb9tbx1fUOhF7pOsraQuRNd0bBFcEc5D6BWikHLEzQkAGYl40rCEer2aPKsRBrYb QBnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694797350; x=1695402150; h=cc:to:content-transfer-encoding:mime-version:message-id:date :subject:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Ln+7rHA69/YQ6l7rezPa2HlFS+4ma5eDvscwKBBdEIw=; b=IDR2hVZqxIJG6rMdOLvDSmpIJbltQW1hJANFNNaSxuFMA81rT31KoiOxiUTzzK58au XMa2wS6voZ+ajeRsfpMtdVEKjOK3YKPKF9MGCRW1vIQFvyoCWJY9VOEvCmLA9D5qETfA X2KLY1KACtZXD7LsP8a3CWoBRQQwdpeaiPI0wSoTFldy0DfOgc9HuuiVwr6UagSrH1ag /7WbEpz1sZc14t8Y9STf2ZUPzCDU2lazgpn5UjGPeeC6nUN/DO800o0L5jJDnSjmv2eB 0AN5+8yZbOLIpGEjbqpZNO87CQe0u8BplP+51s9M6wZx/9CsWwxzSNxxDFvbqzXsWoGL 44ew== X-Gm-Message-State: AOJu0Yy09VF60ryVGn01k4cbzuyjjwUCDjooeHT+CflK5R79/wrDSvfz uCTF5w0Ww7i6DixIsVRTcf3SWVN/89FrDOLNYFY= X-Received: by 2002:a17:902:a40b:b0:1bb:3406:a612 with SMTP id p11-20020a170902a40b00b001bb3406a612mr2116565plq.57.1694797349850; Fri, 15 Sep 2023 10:02:29 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id g22-20020a1709029f9600b001c44c8d857esm34299plq.120.2023.09.15.10.02.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 10:02:29 -0700 (PDT) From: Charlie Jenkins <charlie@rivosinc.com> Subject: [PATCH v6 0/4] riscv: Add fine-tuned checksum functions Date: Fri, 15 Sep 2023 10:01:16 -0700 Message-Id: <20230915-optimize_checksum-v6-0-14a6cf61c618@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-B4-Tracking: v=1; b=H4sIANyNBGUC/23Q30oFIRAG8Fc5eJ2ho67aVe8REf6rHWLXg56kO uy75x6ClvDyG5jfN8yV1FQwVfJwupKSGlbMaw/T3YmE2a1viWLsmQADwQyTNJ8vuOB3eglzCu/ 1Y6HRc6nAGBeAk753LukVP2/m03PPM9ZLLl+3isb36a8G00BrnDJqhVaM+86CeyzYcsU13Ie8k B1s8IdYpkYIdCSEOCkTvZbCDxBxRPQIETuiGEQho41aDBB5QDgfIbIjWocALtrUlQGijsjow03 tl1jlDTjQSqd/yLZtP2bSzwXJAQAA 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>, David Laight <david.laight@aculab.com> X-Mailer: b4 0.12.3 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email 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 (fry.vger.email [0.0.0.0]); Fri, 15 Sep 2023 10:03:41 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777132715623822156 X-GMAIL-MSGID: 1777132715623822156 |
Series |
riscv: Add fine-tuned checksum functions
|
|
Message
Charlie Jenkins
Sept. 15, 2023, 5:01 p.m. UTC
Each architecture generally implements fine-tuned checksum functions to
leverage the instruction set. This patch adds the main checksum
functions that are used in networking.
Vector support is included in this patch to start a discussion on that,
it can probably be optimized more. The vector patches still need some
work as they rely on GCC vector intrinsics types which cannot work in
the kernel since it requires C vector support rather than just assembler
support. I have tested the vector patches as standalone algorithms in QEMU.
This patch takes heavy use of the Zbb extension using alternatives
patching.
To test this patch, enable the configs for KUNIT, then CHECKSUM_KUNIT
and RISCV_CHECKSUM_KUNIT.
I have attempted to make these functions as optimal as possible, but I
have not ran anything on actual riscv hardware. My performance testing
has been limited to inspecting the assembly, running the algorithms on
x86 hardware, and running in QEMU.
ip_fast_csum is a relatively small function so even though it is
possible to read 64 bits at a time on compatible hardware, the
bottleneck becomes the clean up and setup code so loading 32 bits at a
time is actually faster.
---
The algorithm proposed to replace the default csum_fold can be seen to
compute the same result by running all 2^32 possible inputs.
static inline unsigned int ror32(unsigned int word, unsigned int shift)
{
return (word >> (shift & 31)) | (word << ((-shift) & 31));
}
unsigned short csum_fold(unsigned int csum)
{
unsigned int sum = csum;
sum = (sum & 0xffff) + (sum >> 16);
sum = (sum & 0xffff) + (sum >> 16);
return ~sum;
}
unsigned short csum_fold_arc(unsigned int csum)
{
return ((~csum - ror32(csum, 16)) >> 16);
}
int main()
{
unsigned int start = 0x0;
do {
if (csum_fold(start) != csum_fold_arc(start)) {
printf("Not the same %u\n", start);
return -1;
}
start += 1;
} while(start != 0x0);
printf("The same\n");
return 0;
}
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Arnd Bergmann <arnd@arndb.de>
To: Charlie Jenkins <charlie@rivosinc.com>
To: Palmer Dabbelt <palmer@dabbelt.com>
To: Conor Dooley <conor@kernel.org>
To: Samuel Holland <samuel.holland@sifive.com>
To: David Laight <David.Laight@aculab.com>
To: linux-riscv@lists.infradead.org
To: linux-kernel@vger.kernel.org
To: linux-arch@vger.kernel.org
Signed-off-by: Charlie Jenkins <charlie@rivosinc.com>
---
Changes in v6:
- Fix accuracy of commit message for csum_fold
- Fix indentation
- Link to v5: https://lore.kernel.org/r/20230914-optimize_checksum-v5-0-c95b82a2757e@rivosinc.com
Changes in v5:
- Drop vector patches
- Check ZBB enabled before doing any ZBB code (Conor)
- Check endianness in IS_ENABLED
- Revert to the simpler non-tree based version of ipv6_csum_magic since
David pointed out that the tree based version is not better.
- Link to v4: https://lore.kernel.org/r/20230911-optimize_checksum-v4-0-77cc2ad9e9d7@rivosinc.com
Changes in v4:
- Suggestion by David Laight to use an improved checksum used in
arch/arc.
- Eliminates zero-extension on rv32, but not on rv64.
- Reduces data dependency which should improve execution speed on
rv32 and rv64
- Still passes CHECKSUM_KUNIT and RISCV_CHECKSUM_KUNIT on rv32 and
rv64 with and without zbb.
- Link to v3: https://lore.kernel.org/r/20230907-optimize_checksum-v3-0-c502d34d9d73@rivosinc.com
Changes in v3:
- Use riscv_has_extension_likely and has_vector where possible (Conor)
- Reduce ifdefs by using IS_ENABLED where possible (Conor)
- Use kernel_vector_begin in the vector code (Samuel)
- Link to v2: https://lore.kernel.org/r/20230905-optimize_checksum-v2-0-ccd658db743b@rivosinc.com
Changes in v2:
- After more benchmarking, rework functions to improve performance.
- Remove tests that overlapped with the already existing checksum
tests and make tests more extensive.
- Use alternatives to activate code with Zbb and vector extensions
- Link to v1: https://lore.kernel.org/r/20230826-optimize_checksum-v1-0-937501b4522a@rivosinc.com
---
Charlie Jenkins (4):
asm-generic: Improve csum_fold
riscv: Checksum header
riscv: Add checksum library
riscv: Test checksum functions
arch/riscv/Kconfig.debug | 1 +
arch/riscv/include/asm/checksum.h | 91 ++++++++++
arch/riscv/lib/Kconfig.debug | 31 ++++
arch/riscv/lib/Makefile | 3 +
arch/riscv/lib/csum.c | 198 ++++++++++++++++++++
arch/riscv/lib/riscv_checksum_kunit.c | 330 ++++++++++++++++++++++++++++++++++
include/asm-generic/checksum.h | 4 +-
7 files changed, 655 insertions(+), 3 deletions(-)
---
base-commit: af3c30d33476bc2694b0d699173544b07f7ae7de
change-id: 20230804-optimize_checksum-db145288ac21