From patchwork Tue Mar 14 18:32:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 69772 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1920235wrd; Tue, 14 Mar 2023 11:36:36 -0700 (PDT) X-Google-Smtp-Source: AK7set8CPD/o3wVbGSAl3+Nv9Xe4iVlhr58ddNM61JbYXqnAxIIsY+NudcCo1/wQLdBSBiuWLsrq X-Received: by 2002:a17:90b:38c3:b0:239:fd9b:85bd with SMTP id nn3-20020a17090b38c300b00239fd9b85bdmr39795947pjb.27.1678818995762; Tue, 14 Mar 2023 11:36:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678818995; cv=none; d=google.com; s=arc-20160816; b=SGWJV/8q9F5FjvEZGouJvDOO4dGgHAwZH0Tg4SxSkaGHqPjg2W5tFrsmdlVxygaUG8 t1infgo7OPiMLrPpbsQdA9to4VW6HI5Tf23P/O9YRlHvJlR770nYmYyZvmtA3oNTw/yN k37NzSqMU1ZZcv5+NYU1Mspgvl1Kr1flb4pH4PJoQEZHKFMBwP1KCvXEQ4opKuM3slrP /ws2mqAmWjf6JnzX/ThoyPuvTjyIyMlFKdT2pNrNnqDqvsU2UDtVe2qpbCFr2YNWQH/3 QNycfaYo5pB3YSjQlDr38DVF/gv2j4M3qVzSahdgX96VdQs3SRodEFf6z5hwbLbPWS0N Jb9g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=L2I6pzb9mfcEjWOn9wUkhHEqFGs76E697ygZBc6Zpec=; b=MFAkL5refn2tdHLDRrtGU2NhwKUnamOPGE7pub6JcCWj2Y5jgGSxJ3vnvUXfqJalON eZtzola5+B3E9DfMUVHUnEwrP6yER6bY0n8tHoSDXGxpLr6pbzHhuI1DOoomRPfjUgvP ct+z1M4YZLG1zVDctmTFWjWFTOq7DcBKM/GAte+Kl+7HZATKnMUSCvlg+ty9sZ3GhHXm Y/OGTD1R2YN5M6hW9PCJLYZHChwbGl2iqhGoLUcSlxmk9CNLEaVAbfl5NtVF7xDKevM+ 47ASFvps4QXXmc6RzxKkw+GgqD3HeFROyiw+EPOk+Dat+Tu1LwDw2Ae/MiQn+ghu/H/y LdxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=SJ9H+8cq; 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 e125-20020a636983000000b0050bd83f0243si362551pgc.16.2023.03.14.11.36.17; Tue, 14 Mar 2023 11:36:35 -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.20210112.gappssmtp.com header.s=20210112 header.b=SJ9H+8cq; 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 S230394AbjCNSfN (ORCPT + 99 others); Tue, 14 Mar 2023 14:35:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231173AbjCNSeq (ORCPT ); Tue, 14 Mar 2023 14:34:46 -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 5579BA8816 for ; Tue, 14 Mar 2023 11:34:19 -0700 (PDT) Received: by mail-pl1-x62d.google.com with SMTP id i5so17575293pla.2 for ; Tue, 14 Mar 2023 11:34:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1678818776; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=L2I6pzb9mfcEjWOn9wUkhHEqFGs76E697ygZBc6Zpec=; b=SJ9H+8cq/o5ePemMP/iGfo9qWAMMQSokbVTQyJh1hPK9R7xoxRxMQQSac1ZYoj07/8 pvDG8QHI83PX5z7VVQ1YwGApZcYNJqC8X6ESSRAJvN1fBkQUaFXbHvPn6RQMJ/K2AKdc aP+b5nA5kS8n+CfDsdhKgILlZ0kRDkOArahw1NOe08u+PJ5ne7RG6MthaMWDm/cSJkL0 6FgrsW5i1yBDZJ0A19M+ouwzIzVFcwU17NYY9MVNWUmBSHO5Gajt6TCqDvcKeU6PG9Vo DS3DXTgiXRiPvMu//X4+L/eL1YXX8s0GeOMQwpegAA4nfh+vw5t9vo3kAnNf3UfEPWSm vXoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678818776; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=L2I6pzb9mfcEjWOn9wUkhHEqFGs76E697ygZBc6Zpec=; b=e8R0+sq2y+VQ9OGgPsJJzZEtXhNtpXm8IUtIyMBafLU4MorPWlVTn9jmzBj9vyyMCD S14OpZnvjcIq3QOjpgxuK7uLzJDkl4JCbitsfoGsDpjUrt6MmyAYtp1ORoP8LsYuTFWo WMjEt5YhDF8h2zkFWGzqRGobjDddC3d+sVpux2SYUKGykfFy+olRLDUHFDzJwRwKyNRX XruZcq6Z+EQHAtvF3UDgA7aomyDKG0qFN01vIjer03YxKw+7KFUttvsIFGzWJcARmXy+ imsuEsJ5TMY+IVTuQHY+jSwqDs3DfVZ/g/VH7ZmR0vgDX0JwQE5qxfenH70f5D2AQYC2 XEXA== X-Gm-Message-State: AO0yUKWavYMPK5lYKXqA4zksHsWgA9GzLiJIGLA6xPXjz448CzL7r8vy SSHqa7l7QubeoUCCoqmfM+wziA== X-Received: by 2002:a17:902:e847:b0:19a:81c1:e743 with SMTP id t7-20020a170902e84700b0019a81c1e743mr92302plg.2.1678818775814; Tue, 14 Mar 2023 11:32:55 -0700 (PDT) Received: from evan.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id kn3-20020a170903078300b0019d397b0f18sm2072019plb.214.2023.03.14.11.32.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Mar 2023 11:32:55 -0700 (PDT) From: Evan Green To: Palmer Dabbelt Cc: slewis@rivosinc.com, heiko@sntech.de, Conor Dooley , vineetg@rivosinc.com, Evan Green , Albert Ou , Andrew Bresticker , Atish Patra , Celeste Liu , Conor Dooley , Davidlohr Bueso , Guo Ren , Jann Horn , Jisheng Zhang , "Liam R. Howlett" , Nathan Chancellor , Palmer Dabbelt , Paul Walmsley , linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [PATCH v4 6/6] RISC-V: Add hwprobe vDSO function and data Date: Tue, 14 Mar 2023 11:32:20 -0700 Message-Id: <20230314183220.513101-7-evan@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230314183220.513101-1-evan@rivosinc.com> References: <20230314183220.513101-1-evan@rivosinc.com> 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 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?1760369307534330266?= X-GMAIL-MSGID: =?utf-8?q?1760369307534330266?= Add a vDSO function __vdso_riscv_hwprobe, which can sit in front of the riscv_hwprobe syscall and answer common queries. We stash a copy of static answers for the "all CPUs" case in the vDSO data page. This data is private to the vDSO, so we can decide later to change what's stored there or under what conditions we defer to the syscall. Currently all data can be discovered at boot, so the vDSO function answers all queries when the cpumask is set to the "all CPUs" hint. There's also a boolean in the data that lets the vDSO function know that all CPUs are the same. In that case, the vDSO will also answer queries for arbitrary CPU masks in addition to the "all CPUs" hint. Signed-off-by: Evan Green --- Changes in v4: - Added a prototype to fix -Wmissing-prototype warning (lkp@intel.com) - Fixed rv32 build failure (lkp@intel.com) - Make vdso prototype match syscall types update Changes in v3: - Introduced vDSO function One aspect of this that's less than perfect is that there are two copies of the arch_vdso_data in the data page. This stems from the fact that vdso_data is arrayed by CS_BASES. Since we're very far away from filling up the page, and this is not ABI, I left it for now. If things get full we can move this data out to its own page, or refactor vdso_data tree-wide to allow for non-arrayed data. --- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/vdso/data.h | 17 +++++++ arch/riscv/include/asm/vdso/gettimeofday.h | 8 ++++ arch/riscv/kernel/sys_riscv.c | 41 +++++++++++++++++ arch/riscv/kernel/vdso.c | 6 --- arch/riscv/kernel/vdso/Makefile | 2 + arch/riscv/kernel/vdso/hwprobe.c | 52 ++++++++++++++++++++++ arch/riscv/kernel/vdso/sys_hwprobe.S | 15 +++++++ arch/riscv/kernel/vdso/vdso.lds.S | 1 + 9 files changed, 137 insertions(+), 6 deletions(-) create mode 100644 arch/riscv/include/asm/vdso/data.h create mode 100644 arch/riscv/kernel/vdso/hwprobe.c create mode 100644 arch/riscv/kernel/vdso/sys_hwprobe.S diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index e2b656043abf..218e75975a15 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -33,6 +33,7 @@ config RISCV select ARCH_HAS_STRICT_MODULE_RWX if MMU && !XIP_KERNEL select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN_SANITIZE_ALL + select ARCH_HAS_VDSO_DATA select ARCH_OPTIONAL_KERNEL_RWX if ARCH_HAS_STRICT_KERNEL_RWX select ARCH_OPTIONAL_KERNEL_RWX_DEFAULT select ARCH_STACKWALK diff --git a/arch/riscv/include/asm/vdso/data.h b/arch/riscv/include/asm/vdso/data.h new file mode 100644 index 000000000000..dc2f76f58b76 --- /dev/null +++ b/arch/riscv/include/asm/vdso/data.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __RISCV_ASM_VDSO_DATA_H +#define __RISCV_ASM_VDSO_DATA_H + +#include +#include +#include + +struct arch_vdso_data { + /* Stash static answers to the hwprobe queries when all CPUs are selected. */ + __u64 all_cpu_hwprobe_values[RISCV_HWPROBE_MAX_KEY + 1]; + + /* Boolean indicating all CPUs have the same static hwprobe values. */ + __u8 homogeneous_cpus; +}; + +#endif /* __RISCV_ASM_VDSO_DATA_H */ diff --git a/arch/riscv/include/asm/vdso/gettimeofday.h b/arch/riscv/include/asm/vdso/gettimeofday.h index 77d9c2f721c4..ba3283cf7acc 100644 --- a/arch/riscv/include/asm/vdso/gettimeofday.h +++ b/arch/riscv/include/asm/vdso/gettimeofday.h @@ -9,6 +9,12 @@ #include #include +/* + * 32-bit land is lacking generic time vsyscalls as well as the legacy 32-bit + * time syscalls like gettimeofday. Skip these definitions since on 32-bit. + */ +#ifdef CONFIG_GENERIC_TIME_VSYSCALL + #define VDSO_HAS_CLOCK_GETRES 1 static __always_inline @@ -60,6 +66,8 @@ int clock_getres_fallback(clockid_t _clkid, struct __kernel_timespec *_ts) return ret; } +#endif /* CONFIG_GENERIC_TIME_VSYSCALL */ + static __always_inline u64 __arch_get_hw_counter(s32 clock_mode, const struct vdso_data *vd) { diff --git a/arch/riscv/kernel/sys_riscv.c b/arch/riscv/kernel/sys_riscv.c index 76d5b468914c..0711dc4afe73 100644 --- a/arch/riscv/kernel/sys_riscv.c +++ b/arch/riscv/kernel/sys_riscv.c @@ -14,6 +14,7 @@ #include #include #include +#include static long riscv_sys_mmap(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, @@ -244,6 +245,46 @@ static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs, } +static int __init init_hwprobe_vdso_data(void) +{ + struct vdso_data *vd = __arch_get_k_vdso_data(); + struct arch_vdso_data *avd = &vd->arch_data; + u64 id_bitsmash = 0; + struct riscv_hwprobe pair; + int key; + + /* + * Initialize vDSO data with the answers for the "all CPUs" case, to + * save a syscall in the common case. + */ + for (key = 0; key <= RISCV_HWPROBE_MAX_KEY; key++) { + pair.key = key; + hwprobe_one_pair(&pair, cpu_online_mask); + + WARN_ON_ONCE(pair.key < 0); + + avd->all_cpu_hwprobe_values[key] = pair.value; + /* + * Smash together the vendor, arch, and impl IDs to see if + * they're all 0 or any negative. + */ + if (key <= RISCV_HWPROBE_KEY_MIMPID) + id_bitsmash |= pair.value; + } + + /* + * If the arch, vendor, and implementation ID are all the same across + * all harts, then assume all CPUs are the same, and allow the vDSO to + * answer queries for arbitrary masks. However if all values are 0 (not + * populated) or any value returns -1 (varies across CPUs), then the + * vDSO should defer to the kernel for exotic cpu masks. + */ + avd->homogeneous_cpus = (id_bitsmash > 0); + return 0; +} + +arch_initcall_sync(init_hwprobe_vdso_data); + SYSCALL_DEFINE5(riscv_hwprobe, struct riscv_hwprobe __user *, pairs, size_t, pair_count, size_t, cpu_count, unsigned long __user *, cpus, unsigned int, flags) diff --git a/arch/riscv/kernel/vdso.c b/arch/riscv/kernel/vdso.c index e410275918ac..52a847eacd79 100644 --- a/arch/riscv/kernel/vdso.c +++ b/arch/riscv/kernel/vdso.c @@ -14,13 +14,7 @@ #include #include #include - -#ifdef CONFIG_GENERIC_TIME_VSYSCALL #include -#else -struct vdso_data { -}; -#endif extern char vdso_start[], vdso_end[]; #ifdef CONFIG_COMPAT diff --git a/arch/riscv/kernel/vdso/Makefile b/arch/riscv/kernel/vdso/Makefile index 06e6b27f3bcc..107fd2162cdf 100644 --- a/arch/riscv/kernel/vdso/Makefile +++ b/arch/riscv/kernel/vdso/Makefile @@ -12,6 +12,8 @@ vdso-syms += vgettimeofday endif vdso-syms += getcpu vdso-syms += flush_icache +vdso-syms += hwprobe +vdso-syms += sys_hwprobe # Files to link into the vdso obj-vdso = $(patsubst %, %.o, $(vdso-syms)) note.o diff --git a/arch/riscv/kernel/vdso/hwprobe.c b/arch/riscv/kernel/vdso/hwprobe.c new file mode 100644 index 000000000000..d40bec6ac078 --- /dev/null +++ b/arch/riscv/kernel/vdso/hwprobe.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright 2023 Rivos, Inc + */ + +#include +#include +#include + +extern int riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count, + size_t cpu_count, unsigned long *cpus, + unsigned int flags); + +/* Add a prototype to avoid -Wmissing-prototypes warning. */ +int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count, + size_t cpu_count, unsigned long *cpus, + unsigned int flags); + +int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count, + size_t cpu_count, unsigned long *cpus, + unsigned int flags) +{ + const struct vdso_data *vd = __arch_get_vdso_data(); + const struct arch_vdso_data *avd = &vd->arch_data; + bool all_cpus = !cpu_count && !cpus; + struct riscv_hwprobe *p = pairs; + struct riscv_hwprobe *end = pairs + pair_count; + + /* + * Defer to the syscall for exotic requests. The vdso has answers + * stashed away only for the "all cpus" case. If all CPUs are + * homogeneous, then this function can handle requests for arbitrary + * masks. + */ + if ((flags != 0) || (!all_cpus && !avd->homogeneous_cpus)) + return riscv_hwprobe(pairs, pair_count, cpu_count, cpus, flags); + + /* This is something we can handle, fill out the pairs. */ + while (p < end) { + if (p->key <= RISCV_HWPROBE_MAX_KEY) { + p->value = avd->all_cpu_hwprobe_values[p->key]; + + } else { + p->key = -1; + p->value = 0; + } + + p++; + } + + return 0; +} diff --git a/arch/riscv/kernel/vdso/sys_hwprobe.S b/arch/riscv/kernel/vdso/sys_hwprobe.S new file mode 100644 index 000000000000..2511c8ba8f86 --- /dev/null +++ b/arch/riscv/kernel/vdso/sys_hwprobe.S @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2022 Rivos, Inc */ + +#include +#include + +.text +ENTRY(riscv_hwprobe) + .cfi_startproc + li a7, __NR_riscv_hwprobe + ecall + ret + + .cfi_endproc +ENDPROC(riscv_hwprobe) diff --git a/arch/riscv/kernel/vdso/vdso.lds.S b/arch/riscv/kernel/vdso/vdso.lds.S index 150b1a572e61..dfe63fb71041 100644 --- a/arch/riscv/kernel/vdso/vdso.lds.S +++ b/arch/riscv/kernel/vdso/vdso.lds.S @@ -75,6 +75,7 @@ VERSION #endif __vdso_getcpu; __vdso_flush_icache; + __vdso_riscv_hwprobe; local: *; }; }