Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4717930rdb; Fri, 15 Sep 2023 10:06:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHZgAe1JRHmCeOGJCD0EG3oSpp9rEo0GARs2On8sYBepf9a8R3BfXNF10hcO75HJhE8aRIg X-Received: by 2002:a17:90a:2a8a:b0:274:84f3:d73a with SMTP id j10-20020a17090a2a8a00b0027484f3d73amr2153658pjd.28.1694797566091; Fri, 15 Sep 2023 10:06:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694797566; cv=none; d=google.com; s=arc-20160816; b=VWM3/P1rOccjRmuHMC1Q0g3l7YUAJZcxrc8oTh7FkcPEYLUbvwVUyYsYRk6rdbTa9R FiOqFOk3x3lFkepoLXDlls3u0ms31vIzZhNjgGWtKlGlzrMuMGul0HoI0usDgCEEgDii IkQPzJOK0JH8fBEhIStyswGU9t6LLg0Tkt8B9qCULwjpEOajOxLUTcQOvnvSGe5kTtg6 a5wIts2wUg9fM0mfuCvQVQjHf7E5T8Wl0Z9O9DbKjuNgsd7Pp9uvVfTmyfVmly01tInu KDg0Y1zPemr6GRVssTh8uTaf6piGsHI3cEl4EaVHrmr5TCCymaxpPYiy7hVJsMPzbFeJ AXMw== 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:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id kk1-20020a17090b4a0100b0027450695a78si5294245pjb.172.2023.09.15.10.06.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 10:06:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; 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:2 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 agentk.vger.email (Postfix) with ESMTP id 6D5CD820AA99; Fri, 15 Sep 2023 10:03:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235184AbjIORDD (ORCPT + 99 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 ); 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 ; Fri, 15 Sep 2023 10:02:30 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1c35ee3b0d2so18909995ad.2 for ; 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 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 , Palmer Dabbelt , Conor Dooley , Samuel Holland , David Laight , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Paul Walmsley , Albert Ou , Arnd Bergmann , David Laight 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 agentk.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 (agentk.vger.email [0.0.0.0]); Fri, 15 Sep 2023 10:03:31 -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. 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 Cc: Albert Ou Cc: Arnd Bergmann To: Charlie Jenkins To: Palmer Dabbelt To: Conor Dooley To: Samuel Holland To: David Laight 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 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 -- - Charlie