Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp28505rdb; Wed, 1 Nov 2023 15:49:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGBqyLNBkjZoXQ0e/4aZ+nTqPoBGWk73NBmBf8KyNIGmURE503b+xpcl2NAtJf67pw9WCXq X-Received: by 2002:a17:902:f98d:b0:1cc:32df:8ebd with SMTP id ky13-20020a170902f98d00b001cc32df8ebdmr4894029plb.25.1698878951114; Wed, 01 Nov 2023 15:49:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698878951; cv=none; d=google.com; s=arc-20160816; b=yQLv2+S9GQMqm5K2i4sHT3CDjPbvYF81frIyri6gjiop+/WnltsCrOuKB2dHmNMwkS mYzVU6jSTNsf1+xE8YUXOD6bS2NnOcE6Fvec1hQowY7YzJS9J9ab8k1/v5IyStRRU8D0 0mb5uhmQvnEEQTmRAtzFsdXJ1KhvwgT9xYnj1hwamY79wbe33OHXDV2k/m5Jw+SNN3vA q8d9W8zdgbZOnFDB28ezUn6nOVdm0kfhCBkEHgJNgpx+kM9TfXCD6P3Jd/sNVnDUFSkw HPGZTbXHK4+O5iAl+VamW3sHWsejCDz7P53K2U7aWDY7A2c06jSuvQilpzs8GpUMpGMH DyWw== 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=IQCU8CvUd0qtGWMvDD449YAC+H6Mg3XfAOJXkEVCODM=; fh=B38m/x4vysnb+FduGD3Y3Qx6OAV3jidMNwqy8dyeER8=; b=DUO9IC10Dn7sHKifnRdBTsKtu99BRvUBj1xZC0ZrQ5T7GjfcAT2sqmcEShNSaPZ088 wLzNP5gGU5F/SQ+b+3AhlLictswC6m+NV4n+6tYd8cUF8Q1ljDx156Wc0FIRqkiC2Zgp O06Zr5u8/LJcCpLfvJia3VTTixuOoQGPBDZ5vhz/oqDemKgLqUXqrkdfXtzyhj6YTVaw 61xBeFf7cEIMWDKajdTCvmix9Hby4tl2rthOIBdjJ0I/SUcVVpKv7SuMtvM8jIfDOBmW BFARGwGk3jS0VrdBUx7nLbQP0BD7MDp8WoyZzGYmroir10UR30Bjw7bf1xhFnaycm2P5 fGDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=nErgtZcy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id o4-20020a170903300400b001c88fc3c5a1si3643982pla.461.2023.11.01.15.49.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 15:49:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=nErgtZcy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id F288480C256F; Wed, 1 Nov 2023 15:48:57 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347145AbjKAWsV (ORCPT + 99 others); Wed, 1 Nov 2023 18:48:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346976AbjKAWsS (ORCPT ); Wed, 1 Nov 2023 18:48:18 -0400 Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67E01133 for ; Wed, 1 Nov 2023 15:48:14 -0700 (PDT) Received: by mail-oi1-x232.google.com with SMTP id 5614622812f47-3ae5a014d78so703445b6e.1 for ; Wed, 01 Nov 2023 15:48:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698878893; x=1699483693; 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=IQCU8CvUd0qtGWMvDD449YAC+H6Mg3XfAOJXkEVCODM=; b=nErgtZcyFj/BikMTtgAaSFy393NclIpjec7pCmbqRk+Wx63bhFTSHkOtB68zxyefVB bhaQRd4As7M1qbR1pkoJOqsXSWYB9CrzWLGaLV8i1kD6NzxPSh39nTc+VN7zFK7+aLQe 4I0ah5PntbwzBdsBbkCEw/xiH9YtdJS/aO6R9245+5csbAWe2IMOgL2A3TO95rUCRXSR K68kJMs7aWxipqtSaufKKno8Epoik0bDWCbJsUjDOhd3BqIaKa3geI9q7CaC/f6K8k+G KtVV7bR5XwhwGcvofw14l4YwqL+9QisnftuwRxo3PSOhNrP9++nuwA+nB1n3ALwb9T1k cBEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698878893; x=1699483693; 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=IQCU8CvUd0qtGWMvDD449YAC+H6Mg3XfAOJXkEVCODM=; b=P50mvp3cwoJtZR7TtOSsX33i6mrKvDbykTR7jll7sy4xxJEUVG6MkxZBPU+Kiq3VsS Vhc8Mnt14rqXN4etO3mVRcc8frmCez0WrKJB3LPy5o60Cf1Fk1hqlHJLPL3GEhMuXiHI N81Kk6isgol+R7XpQUhi7bFJH2WEazj5rkDuy4vQNCkANPFYSx241SNXAwR81RNWmOzc rABm6dGjQgOW8/kClQnWIWwikjmjAqugp9DViLEFEdFev7td0bFOOeJxxYiWBtvM16Ou +Vo8zn/IWZql8XgGL2HS0zx8sIMY+jkhTv2SaGcgVt4BJ/R/29AL+PCo3rajXqwPxurJ k6Pg== X-Gm-Message-State: AOJu0YxHEW6YohmYBIV1g+8R/HHI5OgJ6NSm5K+D0vSYEhWGG1iUYYts eVY2MNDQBFKEltKqAk9EyIakDw== X-Received: by 2002:a05:6808:f09:b0:3b2:e4b7:2af2 with SMTP id m9-20020a0568080f0900b003b2e4b72af2mr2429154oiw.6.1698878893596; Wed, 01 Nov 2023 15:48:13 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id be24-20020a056808219800b003b274008e46sm376580oib.0.2023.11.01.15.48.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 15:48:13 -0700 (PDT) From: Charlie Jenkins Subject: [PATCH v10 0/5] riscv: Add fine-tuned checksum functions Date: Wed, 01 Nov 2023 15:48:10 -0700 Message-Id: <20231101-optimize_checksum-v10-0-a498577bb969@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-B4-Tracking: v=1; b=H4sIAKvVQmUC/23Q3UrEMBAF4FdZcm0lM/n3yvcQkSSTukG2Xdq1q Evf3XQRLTKXc2C+Gc5VzGWqZRYPh6uYylLnOg5tAHl3EPkYh9fSVWqBQIlKeqm78Xypp/pVXvK x5Lf5/dRRAm3Q+5gRRNs7T6WvHzf06bnNxzpfxunzdmOBLf3R0DLaAp3sgnJGQmosxsepLuNch 3yfx5PYwAX/kCANh2BDciZrPCWnVWIQtUcch6gNMRJJaQrkFIPoHQLAIbohzuWMkUJpCoOYPcI 1vJjtk2CSx4jOuMIgdo+wndiGgI429xayBc8gbo8EDnENkTY7kkRkyDKI/0VAIlusb0hfkgMJB OiJQcIOUWyxoSElSvDFhoQY/iHrun4Dltrtu94CAAA= To: Charlie Jenkins , Palmer Dabbelt , Conor Dooley , Samuel Holland , David Laight , Xiao Wang , Evan Green , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Paul Walmsley , Albert Ou , Arnd Bergmann , David Laight , Conor Dooley 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,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 01 Nov 2023 15:48:58 -0700 (PDT) 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. This patch takes heavy use of the Zbb extension using alternatives patching. To test this patch, enable the configs for KUNIT, then 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. Relies on https://lore.kernel.org/lkml/20230920193801.3035093-1-evan@rivosinc.com/ --- 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 Cc: Albert Ou Cc: Arnd Bergmann To: Charlie Jenkins To: Palmer Dabbelt To: Conor Dooley To: Samuel Holland To: David Laight To: Xiao Wang To: Evan Green To: linux-riscv@lists.infradead.org To: linux-kernel@vger.kernel.org To: linux-arch@vger.kernel.org Signed-off-by: Charlie Jenkins --- Changes in v10: - Move tests that were riscv-specific to be arch agnostic (Arnd) - Link to v9: https://lore.kernel.org/r/20231031-optimize_checksum-v9-0-ea018e69b229@rivosinc.com Changes in v9: - Use ror64 (Xiao) - Move do_csum and csum_ipv6_magic headers to patch 4 (Xiao) - Remove word "IP" from checksum headers (Xiao) - Swap to using ifndef CONFIG_32BIT instead of ifdef CONFIG_64BIT (Xiao) - Run no alignment code when buff is aligned (Xiao) - Consolidate two do_csum implementations overlap into do_csum_common - Link to v8: https://lore.kernel.org/r/20231027-optimize_checksum-v8-0-feb7101d128d@rivosinc.com Changes in v8: - Speedups of 12% without Zbb and 21% with Zbb when cpu supports fast misaligned accesses for do_csum - Various formatting updates - Patch now relies on https://lore.kernel.org/lkml/20230920193801.3035093-1-evan@rivosinc.com/ - Link to v7: https://lore.kernel.org/r/20230919-optimize_checksum-v7-0-06c7d0ddd5d6@rivosinc.com Changes in v7: - Included linux/bitops.h in asm-generic/checksum.h to use ror (Conor) - Optimized loop in do_csum (David) - Used ror instead of shifting (David) - Unfortunately had to reintroduce ifdefs because gcc is not smart enough to not throw warnings on code that will never execute - Use ifdef instead of IS_ENABLED on __LITTLE_ENDIAN because IS_ENABLED does not work on that - Only optimize for zbb when alternatives is enabled in do_csum - Link to v6: https://lore.kernel.org/r/20230915-optimize_checksum-v6-0-14a6cf61c618@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 (5): asm-generic: Improve csum_fold riscv: Add static key for misaligned accesses riscv: Checksum header riscv: Add checksum library kunit: Add tests for csum_ipv6_magic and ip_fast_csum arch/riscv/include/asm/checksum.h | 92 ++++++++++ arch/riscv/include/asm/cpufeature.h | 3 + arch/riscv/kernel/cpufeature.c | 30 ++++ arch/riscv/lib/Makefile | 1 + arch/riscv/lib/csum.c | 326 ++++++++++++++++++++++++++++++++++++ include/asm-generic/checksum.h | 6 +- lib/checksum_kunit.c | 284 ++++++++++++++++++++++++++++++- 7 files changed, 738 insertions(+), 4 deletions(-) --- base-commit: 8d68c506cd34a142331623fd23eb1c4e680e1955 change-id: 20230804-optimize_checksum-db145288ac21 -- - Charlie