From patchwork Wed Jul 5 16:48:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 11654 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f45:0:b0:3ea:f831:8777 with SMTP id v5csp2001611vqx; Wed, 5 Jul 2023 09:54:30 -0700 (PDT) X-Google-Smtp-Source: APBJJlGx506OfTujXDLHjqiYwZSSHGniwaUTCTHIpv9LH3kdv/nS/uuP9r5ZCOxbwTmDUB9bVQ9g X-Received: by 2002:a05:6a21:6d89:b0:12f:382d:2a37 with SMTP id wl9-20020a056a216d8900b0012f382d2a37mr6495732pzb.15.1688576069866; Wed, 05 Jul 2023 09:54:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688576069; cv=none; d=google.com; s=arc-20160816; b=BnR55x7EM0Rc1e4W/ouZqyR+169dS+GVieUIinBYYdsmFfIyOj2TOiFp1wsNxFexfM sjQoDK0rOsLpG7waYPquS3Lpb/zejTn+vtmVhZcYGOTYOs2VyNKchgH2vpAd6GGmiOZd SpqUjqsttKSZaa0TV3hpY9XLJ1mu/zYW4cIOGBPPPQhjsCzs3n9We4p3xRdInyBBqDt2 A3Opn1lRBhP9WnExu9DF5iyj7zkijpnSXVHKuol00lovKOfiaIlsqaMt+AcRYtdJQrG1 egN+fy0juYwFaxy+nBpJNE2kcybgdsYK8PGlZ4jT4bjFnfb58QNLX5nCOCUUc1YLdFvE 7iBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=3hP9DbBA1q7ePE4MjE20bv2ZQsjinvRoBDENG1RHrkc=; fh=EU6Cpa/c/vkgGDg7D9lg6EszDhbrdGb7Zn7wyfQFrAc=; b=XQqV2jmUZ+nn2vm5AG8oK02ysVqHwaTEIDp27+yFpfZlL/gsVligva1A6OnPoNqkEO BbfZMq0NhtmV/u614wPyF9vYp1Tf3DfXvTu41AfvW6SCSQwpmy3w2chCN763BgE0ZTtn heHVK+ehaMOh9Px6DxcS3kcUveZ/fVETBva7gcY8IXdmkezERGY5LLRz/W7J1dSC5W7l npfQ87U4tnr2XdW3ksIfOUL5a4YcFjUgWkXRnCegy2vr/W5FvSnwyNulX0dZEPE/Qlt5 ahJ7tz2fmJsB2OjBvjqiJgAtNgyANieNlCEhJVS6vZzxEOYe/gn+j9RqZCxtAN1NG+42 uxKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=dN16GTJy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fa39-20020a056a002d2700b0068131db9c6esi16626961pfb.138.2023.07.05.09.54.16; Wed, 05 Jul 2023 09:54:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=dN16GTJy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232397AbjGEQsu (ORCPT + 99 others); Wed, 5 Jul 2023 12:48:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232230AbjGEQso (ORCPT ); Wed, 5 Jul 2023 12:48:44 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CBEF10EA for ; Wed, 5 Jul 2023 09:48:41 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-262e839647eso5159378a91.2 for ; Wed, 05 Jul 2023 09:48:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1688575721; x=1691167721; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=3hP9DbBA1q7ePE4MjE20bv2ZQsjinvRoBDENG1RHrkc=; b=dN16GTJyFnx0CAdebrX0ihc9B+hOMoFybI8IewfRhaLSmnhpMkKjk+9jMWpnYCH2nO abZ8bieLyGIv/gBw547X4lutvAnIFkaEJOxHjM7sLCugaoYj1sNtX0sQiPttz9zUtEF4 CjDCCW4autZVoak/I0EriHUxIgDGxz1avBoIhCt2QwLwUPcWABSd61YiMXFhhv6qgVuH D+mpSlhVmLZCk0iGIjRlFGBxcqXh6KlLn9P2lBSvYVEOl8eP9pFD+/Q/qGMFU78VD25T KzdJYFBbOvoWl+GnGsp0LKG4zRhoiTz1MD9oiijr7Q3Fzoo8Ti3Nj3VgDwxn2lTk7iVy shJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688575721; x=1691167721; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3hP9DbBA1q7ePE4MjE20bv2ZQsjinvRoBDENG1RHrkc=; b=jIPmP6eBvuBVAwr6GU4cNHqRrnJfC0zaFlxJEzB/xVHHyOHLAxBVe56DHkdWxgzecO 0zyzdxuOgyF+AZPz65BrlUpiAXz1k9rquPbvai3mi+dzVBPyaFZPdXEIGcZYSQAnfSQA BsUzYT66/8rkTn3vfwpj9ljsvfYjHTOI/+chbhbRp6ySpxYe4pnvaIzgjQI7Qa8xI0E1 /gy1rWwibzT1Et+ZtBI/S9GemjOu9XQ8LMvOzFbbfPNXNt39cFcVLi44iQDD17NtPV2U AtIjp+KWYYsxBWLXmQpLDxNwCKygP0KzgMyxpfyyqsa+XVpB6Hdwy1n0EQiP/S055wFD 6cxw== X-Gm-Message-State: ABy/qLYNu8QcNJTsK1OuAEgIc8FYxUGIJzMY9+OM6ioo67mvyHLXDqHR SNNEO1mLD5VQ8fxetYnvZ1x3Sw== X-Received: by 2002:a17:90b:156:b0:262:ea30:2cc3 with SMTP id em22-20020a17090b015600b00262ea302cc3mr16732080pjb.2.1688575720894; Wed, 05 Jul 2023 09:48:40 -0700 (PDT) Received: from evan.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id f21-20020a17090ace1500b00263f85b6a35sm1311431pju.29.2023.07.05.09.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jul 2023 09:48:40 -0700 (PDT) From: Evan Green To: Palmer Dabbelt Cc: Simon Hosie , David Laight , Evan Green , Albert Ou , Alexandre Ghiti , Andrew Jones , Andy Chiu , Anup Patel , Conor Dooley , Greentime Hu , Guo Ren , Heiko Stuebner , Heiko Stuebner , Jisheng Zhang , Jonathan Corbet , Ley Foon Tan , Li Zhengyu , Marc Zyngier , Masahiro Yamada , Palmer Dabbelt , Paul Walmsley , Randy Dunlap , Samuel Holland , Sunil V L , Xianting Tian , Yangyu Chen , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [PATCH v2 0/2] RISC-V: Probe for misaligned access speed Date: Wed, 5 Jul 2023 09:48:31 -0700 Message-Id: <20230705164833.995516-1-evan@rivosinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770600341280159763?= X-GMAIL-MSGID: =?utf-8?q?1770600341280159763?= The current setting for the hwprobe bit indicating misaligned access speed is controlled by a vendor-specific feature probe function. This is essentially a per-SoC table we have to maintain on behalf of each vendor going forward. Let's convert that instead to something we detect at runtime. We have two assembly routines at the heart of our probe: one that does a bunch of word-sized accesses (without aligning its input buffer), and the other that does byte accesses. If we can move a larger number of bytes using misaligned word accesses than we can with the same amount of time doing byte accesses, then we can declare misaligned accesses as "fast". The tradeoff of reducing this maintenance burden is boot time. We spend 4-6 jiffies per core doing this measurement (0-2 on jiffie edge alignment, and 4 on measurement). The timing loop was based on raid6_choose_gen(), which uses (16+1)*N jiffies (where N is the number of algorithms). By taking only the fastest iteration out of all attempts for use in the comparison, variance between runs is very low. On my THead C906, it looks like this: [ 0.047563] cpu0: Ratio of byte access time to unaligned word access is 4.34, unaligned accesses are fast Several others have chimed in with results on slow machines with the older algorithm, which took all runs into account, including noise like interrupts. Even with this variation, results indicate that in all cases (fast, slow, and emulated) the measured numbers are nowhere near each other (always multiple factors away). Changes in v2: - Explain more in the commit message (Conor) - Use a new algorithm that looks for the fastest run (David) - Clarify documentatin further (David and Conor) - Unify around a single word, "unaligned" (Conor) - Align asm operands, and other misc whitespace changes (Conor) Evan Green (2): RISC-V: Probe for unaligned access speed RISC-V: alternative: Remove feature_probe_func Documentation/riscv/hwprobe.rst | 11 ++- arch/riscv/errata/thead/errata.c | 8 --- arch/riscv/include/asm/alternative.h | 5 -- arch/riscv/include/asm/cpufeature.h | 2 + arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/alternative.c | 19 ----- arch/riscv/kernel/copy-unaligned.S | 71 ++++++++++++++++++ arch/riscv/kernel/copy-unaligned.h | 13 ++++ arch/riscv/kernel/cpufeature.c | 104 +++++++++++++++++++++++++++ arch/riscv/kernel/smpboot.c | 3 +- 10 files changed, 198 insertions(+), 39 deletions(-) create mode 100644 arch/riscv/kernel/copy-unaligned.S create mode 100644 arch/riscv/kernel/copy-unaligned.h