Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3277880rdb; Wed, 13 Sep 2023 07:30:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEaBQKRBdREM1AZBD02x2/L99L4ww6qJWMvvVGkLPaoO0njdJidEd+W5s3nvmZRTK1dPfQN X-Received: by 2002:a05:6a20:3955:b0:13e:b58a:e3e8 with SMTP id r21-20020a056a20395500b0013eb58ae3e8mr2905326pzg.50.1694615457656; Wed, 13 Sep 2023 07:30:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694615457; cv=none; d=google.com; s=arc-20160816; b=GaPFzT59d0BfwrDXZiTnbBedIMPlOmPfrM4IuYezn2hMHuYZc8bJ2TzOZ/trseA0IN 4NP5fgsf9iSC9cbOL/EFPDIaFQl0+EsjKlOkg2jYE9K5xvHAuqrqHWNc3CYyds36Q+C0 WrwIx3iz3XaXvKiKeUW3SzTVvJ00Vca9cO+MK8UiSyKO3Z3rPgzh9LUyC+BcZvgRJQGm giSBnI6leuSmSIM/O+LY/vZT/3xDZwWjt+pLdP0ac/cIOlYkttMtFTVy+i8iM+wextvW F1bw4gIzwl+bDRNtR2wj3wxWrp5Y5wEdGEG8ajU/hR+fRqzGINQJytlSMNiWgU+1QzJt LltA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:mime-version :references:in-reply-to:from:dkim-signature; bh=QtCA0UtRzCSjKL8jQ8LuLqlWRxghJL7WuWVY8knUOsQ=; fh=VlEF95Unitr+HgG2WgpzBxDYkVA5XYhRPTDGDVePOd4=; b=dU69TSqS5/pjkPGemWYqYgA9JvhK1a7tSlc/TPrZm1qGQGU66SFbaw2a5hKIWalEKB 6/oe9zNUCzYSu8bqgRM/rnKyn4RSs0LqiqjaoVWVWLU0bt8t7rL0RwudM8893uotqfXh OXFMwLAKbuEW4ncvi/5mSc8d14pLIgqaq1u3/RjwY6vbbY3bevT+Ie+6wX4r15BhU8n2 r7+NnNsO1dQjjbRP/b/4qIG+P+4dqu2GnKQf62jScYTiLIg3Uxk8C5uBy6wtPWkMgJAz HcqRXuxs8sFxoKm1nJ+JOAKnTAl303No0Kwg0SjeDONZYGZAiMVoFTs91LbxvEsIRC6c Do+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=X5Fny7FY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id f6-20020a056a000b0600b0068e486a14e1si2674822pfu.163.2023.09.13.07.30.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 07:30:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@canonical.com header.s=20210705 header.b=X5Fny7FY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=canonical.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 220B7832C2E5; Wed, 13 Sep 2023 02:19:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239259AbjIMJT4 (ORCPT + 99 others); Wed, 13 Sep 2023 05:19:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238104AbjIMJTv (ORCPT ); Wed, 13 Sep 2023 05:19:51 -0400 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0739F19B4 for ; Wed, 13 Sep 2023 02:19:47 -0700 (PDT) Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id D8AF93F674 for ; Wed, 13 Sep 2023 09:19:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1694596785; bh=QtCA0UtRzCSjKL8jQ8LuLqlWRxghJL7WuWVY8knUOsQ=; h=From:In-Reply-To:References:Mime-Version:Date:Message-ID:Subject: To:Cc:Content-Type; b=X5Fny7FYuObTIPO3NToF+rc3XSp3Rnty3cIBwb78GkwpNkJ2QYFa2ijHLelNrU4WT T9Ltewab6+841dU0xHDGK9nHpm7wU7pM/7t5z1gmsCtjEMfjVMhJcJiSBs1s3nC78U suvLXt9AiKCE3YCxvuGNWHL5PCwu6kvJQBGjBOl1HuvDtYlNEL+ENyQXa2b6cm4ljG UEY1lb1xMQ4hvmKoiakBfA9i1zYnek9i/272Dr0HvpfVFN4kTldmx4bp8I+7ll7vQM eCsxmxT7zuwgEIUxUwiIdrxg8eGgAnWU8FVfJNTR5LBZw9+gHqvaO5X0TIVnrYzgn5 QPVaixAI8+DoQ== Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-4131d4bc82dso79989241cf.3 for ; Wed, 13 Sep 2023 02:19:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694596784; x=1695201584; h=cc:to:subject:message-id:date:mime-version:references:in-reply-to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QtCA0UtRzCSjKL8jQ8LuLqlWRxghJL7WuWVY8knUOsQ=; b=bc/YawEnnYDo0nhT98KTgzy+Zqh76wcGY8s1yU/AY1rQhokgVrHzSqXcN93AGtBo1+ Lu+y4gdwW9FI6n4t6j05xF9mRzsUG4SRajIWJKb/ENhBaeRCFALyCHCwlxZekfeePFV7 GdaAcn9vAHTyt3QcqEEHSHuXV4UGr+66oPUrTtzr4KDBtBP1+aSVhJHs+LKsT2t6dEMi xAJR3kB7Gu6cPcStk5muYCpIMNuuKMzXWaPfJm+tmDJtDuLdjPf/dNPbWgnHrLH+lcum IpDIAOy++JXIhAD7Wea3af8D5IztfOHwGblYsCEn6EqNbpt0qtv+1ilFsT6ZXq3O5QOF Y1Rg== X-Gm-Message-State: AOJu0YzQxO0a8c+5SjrGH4wdbU8JdOHMVqOn8UzQSurR3Wof+gCSodbo F49stRNxj5Nl4ud0lyOfIQK/GU7pXnvVqkfcu1uNl7D24S/AYC9mjaBDyAVlvkzdogkr9l6DsuQ GW4YeJZeU/6Rq8DaAAzd0A2WpRcgQ2uEFJUn7r+UgkqQ73MjAaKF6E/5z6w== X-Received: by 2002:ac8:5e50:0:b0:410:8873:aca with SMTP id i16-20020ac85e50000000b0041088730acamr1946438qtx.52.1694596784429; Wed, 13 Sep 2023 02:19:44 -0700 (PDT) X-Received: by 2002:ac8:5e50:0:b0:410:8873:aca with SMTP id i16-20020ac85e50000000b0041088730acamr1946425qtx.52.1694596784130; Wed, 13 Sep 2023 02:19:44 -0700 (PDT) Received: from 348282803490 named unknown by gmailapi.google.com with HTTPREST; Wed, 13 Sep 2023 02:19:43 -0700 From: Emil Renner Berthing In-Reply-To: References: <20230911-optimize_checksum-v4-0-77cc2ad9e9d7@rivosinc.com> <20230911-optimize_checksum-v4-1-77cc2ad9e9d7@rivosinc.com> Mime-Version: 1.0 Date: Wed, 13 Sep 2023 02:19:43 -0700 Message-ID: Subject: Re: [PATCH v4 1/5] riscv: Checksum header To: Charlie Jenkins , Emil Renner Berthing Cc: Palmer Dabbelt , Conor Dooley , Samuel Holland , David Laight , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Paul Walmsley , Albert Ou Content-Type: text/plain; charset="UTF-8" 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 (howler.vger.email [0.0.0.0]); Wed, 13 Sep 2023 02:19:55 -0700 (PDT) Charlie Jenkins wrote: > On Tue, Sep 12, 2023 at 03:24:29AM -0700, Emil Renner Berthing wrote: > > 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 > > > --- > > > arch/riscv/include/asm/checksum.h | 95 +++++++++++++++++++++++++++++++++++++++ > > > 1 file changed, 95 insertions(+) > > > > > > diff --git a/arch/riscv/include/asm/checksum.h b/arch/riscv/include/asm/checksum.h > > > new file mode 100644 > > > index 000000000000..0d7fc8275a5e > > > --- /dev/null > > > +++ b/arch/riscv/include/asm/checksum.h > > > @@ -0,0 +1,95 @@ > > > +/* 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 > > > +#include > > > + > > > +#ifdef CONFIG_32BIT > > > +typedef unsigned int csum_t; > > > +#else > > > +typedef unsigned long csum_t; > > > +#endif > > > > Hi Charlie, > > > > Isn't unsigned long already 32bit on 32bit RISC-V, so why is this #ifdef > > needed? > Oh, I wasn't sure so I ran sizeof(long) in qemu-system-riscv32 and it > gave me 8 so assumed a long was 8 bytes. Do you think it would make what > is going on more clear if I use u32 and u64 or would you recommend just > using long? Yeah, it doesn't seem like csum_t is used anywhere else, so I'd just use unsigned long if all you want is a register sized unsigned value. It'll be more familiar and easier to read for most people. > > > > > + > > > +/* > > > + * Fold a partial checksum without adding pseudo headers > > > + */ > > > +static inline __sum16 csum_fold(__wsum sum) > > > +{ > > > + return (~sum - ror32(sum, 16)) >> 16; > > > +} > > > + > > > +#define csum_fold csum_fold > > > + > > > +/* > > > + * 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) > > > +{ > > > + csum_t 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)) { > > > + csum_t 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; > > > > The indentation seems off here. > > > > /Emil > > > > > +#endif > > > + return csum_fold((__force __wsum)csum); > > > +} > > > + > > > +#define ip_fast_csum ip_fast_csum > > > + > > > +#include > > > + > > > +#endif // __ASM_RISCV_CHECKSUM_H > > > > > > -- > > > 2.42.0 > > > > > > > > > _______________________________________________ > > > linux-riscv mailing list > > > linux-riscv@lists.infradead.org > > > http://lists.inradead.org/mailman/listinfo/linux-riscv