Skip to contentSkip to conversation

RISC-V: hwprobe: Introduce which-cpus

PATCH Superseded Andrew Jones 3 participants 18 messages Oct 11, 2023 – Oct 19, 2023

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover RISC-V: hwprobe: Introduce which-cpus

This series introduces a flag for the hwprobe syscall which effectively reverses its behavior from getting the values of keys for a set of cpus to getting the cpus for a set of key-value pairs. The series was originally posted as an RFC[1] and based on another series. v1 is now only based on a single patch pointed out with the tag below.

Based-on: https://lore.kernel.org/all/[email protected]/

[1] https://lore.kernel.org/all/[email protected]/

Changes since the RFC:
- Split hwprobe out of sys_riscv.c into its own file [Palmer]
- Split the which-cpus functionality out of do_riscv_hwprobe() [Palmer]
- Rename hwprobe_key_is_map to hwprobe_key_is_bitmask [Evan]
- Move the homogeneous_cpus logic into the vDSO function [Evan]
- Rework logic to not need to allocate any memory
- Honor cpu affinity in the which-cpus selftests utility
- Picked up some r-b's

Andrew Jones (6):
RISC-V: hwprobe: Clarify cpus size parameter
RISC-V: Move the hwprobe syscall to its own file
RISC-V: hwprobe: Introduce which-cpus flag
RISC-V: selftests: Statically link hwprobe test
RISC-V: selftests: Convert hwprobe test to kselftest API
RISC-V: selftests: Add which-cpus hwprobe test

 Documentation/riscv/hwprobe.rst               |  27 +-
 arch/riscv/include/asm/hwprobe.h              |  24 ++
 arch/riscv/include/uapi/asm/hwprobe.h         |   3 +
 arch/riscv/kernel/Makefile                    |   1 +
 arch/riscv/kernel/sys_hwprobe.c               | 361 ++++++++++++++++++
 arch/riscv/kernel/sys_riscv.c                 | 267 -------------
 arch/riscv/kernel/vdso/hwprobe.c              |  74 +++-
 .../testing/selftests/riscv/hwprobe/Makefile  |   7 +-
 .../testing/selftests/riscv/hwprobe/hwprobe.c |  64 +---
 .../testing/selftests/riscv/hwprobe/hwprobe.h |  15 +
 .../selftests/riscv/hwprobe/which-cpus.c      | 154 ++++++++
 .../selftests/riscv/vector/vstate_prctl.c     |  10 +-
 12 files changed, 667 insertions(+), 340 deletions(-)
 create mode 100644 arch/riscv/kernel/sys_hwprobe.c
 create mode 100644 tools/testing/selftests/riscv/hwprobe/hwprobe.h
 create mode 100644 tools/testing/selftests/riscv/hwprobe/which-cpus.c
Signature · 7 lines
2.41.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#3 lore raw

PATCH 2/6 RISC-V: Move the hwprobe syscall to its own file

As Palmer says, hwprobe is "sort of its own thing now, and it's only going to get bigger..."

---
 arch/riscv/kernel/Makefile      |   1 +
 arch/riscv/kernel/sys_hwprobe.c | 276 ++++++++++++++++++++++++++++++++
 arch/riscv/kernel/sys_riscv.c   | 267 ------------------------------
 3 files changed, 277 insertions(+), 267 deletions(-)
 create mode 100644 arch/riscv/kernel/sys_hwprobe.c
arch/riscv/kernel/Makefile +1 -0
index 95cf25d48405..db7a43211b30 100644
@@ -50,6 +50,7 @@ obj-y	+= setup.o
 obj-y	+= signal.o
 obj-y	+= syscall_table.o
 obj-y	+= sys_riscv.o
+obj-y	+= sys_hwprobe.o
 obj-y	+= time.o
 obj-y	+= traps.o
 obj-y	+= riscv_ksyms.o
arch/riscv/kernel/sys_hwprobe.c new file +276 -0

276 lines not on this page · raw message

arch/riscv/kernel/sys_riscv.c +0 -267

280 lines not on this page · raw message

Signature · 7 lines
2.41.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#11 lore raw

PATCH 3/6 RISC-V: hwprobe: Introduce which-cpus flag

In reply to Andrew Jones

Introduce the first flag for the hwprobe syscall. The flag basically reverses its behavior, i.e. instead of populating the values of keys for a given set of cpus, the set of cpus after the call is the result of finding a set which supports the values of the keys. In order to do this, we implement a pair compare function which takes the type of value (a single value vs. a bitmask of booleans) into consideration. We also implement vdso support for the new flag.

---
 Documentation/riscv/hwprobe.rst       | 16 ++++-
 arch/riscv/include/asm/hwprobe.h      | 24 +++++++
 arch/riscv/include/uapi/asm/hwprobe.h |  3 +
 arch/riscv/kernel/sys_hwprobe.c       | 93 +++++++++++++++++++++++++--
 arch/riscv/kernel/vdso/hwprobe.c      | 68 +++++++++++++++++---
 5 files changed, 190 insertions(+), 14 deletions(-)
Documentation/riscv/hwprobe.rst +14 -2
index c57437e40ffb..576aa03f56bb 100644
@@ -25,8 +25,20 @@ arch, impl), the returned value will only be valid if all CPUs in the given set
 have the same value. Otherwise -1 will be returned. For boolean-like keys, the
 value returned will be a logical AND of the values for the specified CPUs.
 Usermode can supply NULL for ``cpus`` and 0 for ``cpusetsize`` as a shortcut for
-all online CPUs. There are currently no flags, this value must be zero for
-future compatibility.
+all online CPUs. The currently supported flags are:
+
+* :c:macro:`RISCV_HWPROBE_WHICH_CPUS`: This flag basically reverses the behavior
+  of sys_riscv_hwprobe().  Instead of populating the values of keys for a given
+  set of CPUs, the set of CPUs is initially all unset and the values of each key
+  are given.  Upon return, the CPUs which all match each of the given key-value
+  pairs are set in ``cpus``.  How matching is done depends on the key type.  For
+  value-like keys, matching means to be the exact same as the value.  For
+  boolean-like keys, matching means the result of a logical AND of the pair's
+  value with the CPU's value is exactly the same as the pair's value.  ``cpus``
+  may also initially have set bits, in which case the bits of any CPUs which do
+  not match the pairs will be cleared, but no other bits will be set.
+
+All other flags are reserved for future compatibility and must be zero.
 
 On success 0 is returned, on failure a negative error code is returned.
 
arch/riscv/include/asm/hwprobe.h +24 -0
index 7cad513538d8..a68764149e51 100644
@@ -15,4 +15,28 @@ static inline bool riscv_hwprobe_key_is_valid(__s64 key)
 	return key >= 0 && key <= RISCV_HWPROBE_MAX_KEY;
 }
 
+static inline bool hwprobe_key_is_bitmask(__s64 key)
+{
+	switch (key) {
+	case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
+	case RISCV_HWPROBE_KEY_IMA_EXT_0:
+	case RISCV_HWPROBE_KEY_CPUPERF_0:
+		return true;
+	}
+
+	return false;
+}
+
+static inline bool riscv_hwprobe_pair_cmp(struct riscv_hwprobe *pair,
+					  struct riscv_hwprobe *other_pair)
+{
+	if (pair->key != other_pair->key)
+		return false;
+
+	if (hwprobe_key_is_bitmask(pair->key))
+		return (pair->value & other_pair->value) == other_pair->value;
+
+	return pair->value == other_pair->value;
+}
+
 #endif
arch/riscv/include/uapi/asm/hwprobe.h +3 -0
index 006bfb48343d..1d4134befc48 100644
@@ -38,4 +38,7 @@ struct riscv_hwprobe {
 #define		RISCV_HWPROBE_MISALIGNED_MASK		(7 << 0)
 /* Increase RISCV_HWPROBE_MAX_KEY when adding items. */
 
+/* Flags */
+#define RISCV_HWPROBE_WHICH_CPUS	(1 << 0)
+
 #endif
arch/riscv/kernel/sys_hwprobe.c +89 -4

105 lines not on this page · raw message

arch/riscv/kernel/vdso/hwprobe.c +60 -8

83 lines not on this page · raw message

Signature · 7 lines
2.41.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#15 lore raw

PATCH 4/6 RISC-V: selftests: Statically link hwprobe test

In reply to Andrew Jones

Statically linking makes it more convenient to copy the test to a minimal busybox environment.

---
 tools/testing/selftests/riscv/hwprobe/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/riscv/hwprobe/Makefile +1 -1
index ebdbb3c22e54..5f614c3ba598 100644
@@ -7,4 +7,4 @@ TEST_GEN_PROGS := hwprobe
 include ../../lib.mk
 
 $(OUTPUT)/hwprobe: hwprobe.c sys_hwprobe.S
-	$(CC) -o$@ $(CFLAGS) $(LDFLAGS) $^
+	$(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^
Signature · 7 lines
2.41.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#16 lore raw

PATCH 5/6 RISC-V: selftests: Convert hwprobe test to kselftest API

In reply to Andrew Jones

Returning (exiting with) negative exit codes isn't user friendly, because the user must output the exit code with the shell, convert it from its unsigned 8-bit value back to the negative value, and then look up where that comes from in the code (which may be multiple places). Use the kselftests TAP interface, instead.

---
 .../testing/selftests/riscv/hwprobe/hwprobe.c | 54 +++++++------------
 1 file changed, 20 insertions(+), 34 deletions(-)
tools/testing/selftests/riscv/hwprobe/hwprobe.c +20 -34
index 09f290a67420..56cdca53f54a 100644
@@ -2,6 +2,8 @@
 #include <stddef.h>
 #include <asm/hwprobe.h>
 
+#include "../../kselftest.h"
+
 /*
  * Rather than relying on having a new enough libc to define this, just do it
  * ourselves.  This way we don't need to be coupled to a new-enough libc to
@@ -16,6 +18,9 @@ int main(int argc, char **argv)
 	unsigned long cpus;
 	long out;
 
+	ksft_print_header();
+	ksft_set_plan(5);
+
 	/* Fake the CPU_SET ops. */
 	cpus = -1;
 
@@ -25,13 +30,16 @@ int main(int argc, char **argv)
 	 */
 	for (long i = 0; i < 8; i++)
 		pairs[i].key = i;
+
 	out = riscv_hwprobe(pairs, 8, 1, &cpus, 0);
 	if (out != 0)
-		return -1;
+		ksft_exit_fail_msg("hwprobe() failed with %ld\n", out);
+
 	for (long i = 0; i < 4; ++i) {
 		/* Fail if the kernel claims not to recognize a base key. */
 		if ((i < 4) && (pairs[i].key != i))
-			return -2;
+			ksft_exit_fail_msg("Failed to recognize base key: key != i, "
+					   "key=%ld, i=%ld\n", pairs[i].key, i);
 
 		if (pairs[i].key != RISCV_HWPROBE_KEY_BASE_BEHAVIOR)
 			continue;
@@ -39,52 +47,30 @@ int main(int argc, char **argv)
 		if (pairs[i].value & RISCV_HWPROBE_BASE_BEHAVIOR_IMA)
 			continue;
 
-		return -3;
+		ksft_exit_fail_msg("Unexpected pair: (%ld, %ld)\n", pairs[i].key, pairs[i].value);
 	}
 
-	/*
-	 * This should also work with a NULL CPU set, but should not work
-	 * with an improperly supplied CPU set.
-	 */
 	out = riscv_hwprobe(pairs, 8, 0, 0, 0);
-	if (out != 0)
-		return -4;
+	ksft_test_result(out == 0, "NULL CPU set\n");
 
 	out = riscv_hwprobe(pairs, 8, 0, &cpus, 0);
-	if (out == 0)
-		return -5;
+	ksft_test_result(out != 0, "Bad CPU set\n");
 
 	out = riscv_hwprobe(pairs, 8, 1, 0, 0);
-	if (out == 0)
-		return -6;
+	ksft_test_result(out != 0, "NULL CPU set with non-zero size\n");
 
-	/*
-	 * Check that keys work by providing one that we know exists, and
-	 * checking to make sure the resultig pair is what we asked for.
-	 */
 	pairs[0].key = RISCV_HWPROBE_KEY_BASE_BEHAVIOR;
 	out = riscv_hwprobe(pairs, 1, 1, &cpus, 0);
-	if (out != 0)
-		return -7;
-	if (pairs[0].key != RISCV_HWPROBE_KEY_BASE_BEHAVIOR)
-		return -8;
+	ksft_test_result(out == 0 && pairs[0].key == RISCV_HWPROBE_KEY_BASE_BEHAVIOR,
+			 "Existing key is maintained\n");
 
-	/*
-	 * Check that an unknown key gets overwritten with -1,
-	 * but doesn't block elements after it.
-	 */
 	pairs[0].key = 0x5555;
 	pairs[1].key = 1;
 	pairs[1].value = 0xAAAA;
 	out = riscv_hwprobe(pairs, 2, 0, 0, 0);
-	if (out != 0)
-		return -9;
-
-	if (pairs[0].key != -1)
-		return -10;
-
-	if ((pairs[1].key != 1) || (pairs[1].value == 0xAAAA))
-		return -11;
+	ksft_test_result(out == 0 && pairs[0].key == -1 &&
+			 pairs[1].key == 1 && pairs[1].value != 0xAAAA,
+			 "Unknown key overwritten with -1 and doesn't block other elements\n");
 
-	return 0;
+	ksft_finished();
 }
Signature · 7 lines
2.41.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw

PATCH 1/6 RISC-V: hwprobe: Clarify cpus size parameter

In reply to Andrew Jones

The "count" parameter associated with the 'cpus' parameter of the hwprobe syscall is the size in bytes of 'cpus'. Naming it 'cpu_count' may mislead users (it did me) to think it's the number of CPUs that are or can be represented by 'cpus' instead. This is particularly easy (IMO) to get wrong since 'cpus' is documented to be defined by CPU_SET(3) and CPU_SET(3) also documents a CPU_COUNT() (the number of CPUs in set) macro. CPU_SET(3) refers to the size of cpu sets with 'setsize'. Adopt 'cpusetsize' for the hwprobe parameter and specifically state it is in bytes in Documentation/riscv/hwprobe.rst to clarify.

---
 Documentation/riscv/hwprobe.rst  | 15 ++++++++-------
 arch/riscv/kernel/sys_riscv.c    | 14 +++++++-------
 arch/riscv/kernel/vdso/hwprobe.c | 10 +++++-----
 3 files changed, 20 insertions(+), 19 deletions(-)
Documentation/riscv/hwprobe.rst +8 -7
index a52996b22f75..c57437e40ffb 100644
@@ -12,7 +12,7 @@ is defined in <asm/hwprobe.h>::
     };
 
     long sys_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
-                           size_t cpu_count, cpu_set_t *cpus,
+                           size_t cpusetsize, cpu_set_t *cpus,
                            unsigned int flags);
 
 The arguments are split into three groups: an array of key-value pairs, a CPU
@@ -20,12 +20,13 @@ set, and some flags. The key-value pairs are supplied with a count. Userspace
 must prepopulate the key field for each element, and the kernel will fill in the
 value if the key is recognized. If a key is unknown to the kernel, its key field
 will be cleared to -1, and its value set to 0. The CPU set is defined by
-CPU_SET(3). For value-like keys (eg. vendor/arch/impl), the returned value will
-be only be valid if all CPUs in the given set have the same value. Otherwise -1
-will be returned. For boolean-like keys, the value returned will be a logical
-AND of the values for the specified CPUs. Usermode can supply NULL for cpus and
-0 for cpu_count as a shortcut for all online CPUs. There are currently no flags,
-this value must be zero for future compatibility.
+CPU_SET(3) with size ``cpusetsize`` bytes. For value-like keys (eg. vendor,
+arch, impl), the returned value will only be valid if all CPUs in the given set
+have the same value. Otherwise -1 will be returned. For boolean-like keys, the
+value returned will be a logical AND of the values for the specified CPUs.
+Usermode can supply NULL for ``cpus`` and 0 for ``cpusetsize`` as a shortcut for
+all online CPUs. There are currently no flags, this value must be zero for
+future compatibility.
 
 On success 0 is returned, on failure a negative error code is returned.
 
arch/riscv/kernel/sys_riscv.c +7 -7
index 473159b5f303..ed3545eb1b2b 100644
@@ -228,7 +228,7 @@ static void hwprobe_one_pair(struct riscv_hwprobe *pair,
 }
 
 static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs,
-			    size_t pair_count, size_t cpu_count,
+			    size_t pair_count, size_t cpusetsize,
 			    unsigned long __user *cpus_user,
 			    unsigned int flags)
 {
@@ -246,13 +246,13 @@ static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs,
 	 * 0 as a shortcut to all online CPUs.
 	 */
 	cpumask_clear(&cpus);
-	if (!cpu_count && !cpus_user) {
+	if (!cpusetsize && !cpus_user) {
 		cpumask_copy(&cpus, cpu_online_mask);
 	} else {
-		if (cpu_count > cpumask_size())
-			cpu_count = cpumask_size();
+		if (cpusetsize > cpumask_size())
+			cpusetsize = cpumask_size();
 
-		ret = copy_from_user(&cpus, cpus_user, cpu_count);
+		ret = copy_from_user(&cpus, cpus_user, cpusetsize);
 		if (ret)
 			return -EFAULT;
 
@@ -329,10 +329,10 @@ arch_initcall_sync(init_hwprobe_vdso_data);
 #endif /* CONFIG_MMU */
 
 SYSCALL_DEFINE5(riscv_hwprobe, struct riscv_hwprobe __user *, pairs,
-		size_t, pair_count, size_t, cpu_count, unsigned long __user *,
+		size_t, pair_count, size_t, cpusetsize, unsigned long __user *,
 		cpus, unsigned int, flags)
 {
-	return do_riscv_hwprobe(pairs, pair_count, cpu_count,
+	return do_riscv_hwprobe(pairs, pair_count, cpusetsize,
 				cpus, flags);
 }
 
arch/riscv/kernel/vdso/hwprobe.c +5 -5
index cadf725ef798..026b7645c5ab 100644
@@ -8,21 +8,21 @@
 #include <vdso/helpers.h>
 
 extern int riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
-			 size_t cpu_count, unsigned long *cpus,
+			 size_t cpusetsize, 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,
+			 size_t cpusetsize, 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,
+			 size_t cpusetsize, 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;
+	bool all_cpus = !cpusetsize && !cpus;
 	struct riscv_hwprobe *p = pairs;
 	struct riscv_hwprobe *end = pairs + pair_count;
 
@@ -33,7 +33,7 @@ int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
 	 * masks.
 	 */
 	if ((flags != 0) || (!all_cpus && !avd->homogeneous_cpus))
-		return riscv_hwprobe(pairs, pair_count, cpu_count, cpus, flags);
+		return riscv_hwprobe(pairs, pair_count, cpusetsize, cpus, flags);
 
 	/* This is something we can handle, fill out the pairs. */
 	while (p < end) {
Signature · 7 lines
2.41.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#17 lore raw

PATCH 6/6 RISC-V: selftests: Add which-cpus hwprobe test

In reply to Andrew Jones

Test the RISCV_HWPROBE_WHICH_CPUS flag of hwprobe. The test also has a command line interface in order to get the cpu list for arbitrary hwprobe pairs.

---
 .../testing/selftests/riscv/hwprobe/Makefile  |   5 +-
 .../testing/selftests/riscv/hwprobe/hwprobe.c |  12 +-
 .../testing/selftests/riscv/hwprobe/hwprobe.h |  15 ++
 .../selftests/riscv/hwprobe/which-cpus.c      | 154 ++++++++++++++++++
 .../selftests/riscv/vector/vstate_prctl.c     |  10 +-
 5 files changed, 175 insertions(+), 21 deletions(-)
 create mode 100644 tools/testing/selftests/riscv/hwprobe/hwprobe.h
 create mode 100644 tools/testing/selftests/riscv/hwprobe/which-cpus.c
tools/testing/selftests/riscv/hwprobe/Makefile +4 -1
index 5f614c3ba598..210a8d7325b0 100644
@@ -2,9 +2,12 @@
 # Copyright (C) 2021 ARM Limited
 # Originally tools/testing/arm64/abi/Makefile
 
-TEST_GEN_PROGS := hwprobe
+TEST_GEN_PROGS := hwprobe which-cpus
 
 include ../../lib.mk
 
 $(OUTPUT)/hwprobe: hwprobe.c sys_hwprobe.S
 	$(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^
+
+$(OUTPUT)/which-cpus: which-cpus.c sys_hwprobe.S
+	$(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^
tools/testing/selftests/riscv/hwprobe/hwprobe.c +1 -11
index 56cdca53f54a..d53e0889b59e 100644
@@ -1,17 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
-#include <stddef.h>
-#include <asm/hwprobe.h>
-
+#include "hwprobe.h"
 #include "../../kselftest.h"
 
-/*
- * Rather than relying on having a new enough libc to define this, just do it
- * ourselves.  This way we don't need to be coupled to a new-enough libc to
- * contain the call.
- */
-long riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
-		   size_t cpu_count, unsigned long *cpus, unsigned int flags);
-
 int main(int argc, char **argv)
 {
 	struct riscv_hwprobe pairs[8];
tools/testing/selftests/riscv/hwprobe/hwprobe.h new file +15 -0
new file mode 100644
index 000000000000..e3fccb390c4d
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef SELFTEST_RISCV_HWPROBE_H
+#define SELFTEST_RISCV_HWPROBE_H
+#include <stddef.h>
+#include <asm/hwprobe.h>
+
+/*
+ * Rather than relying on having a new enough libc to define this, just do it
+ * ourselves.  This way we don't need to be coupled to a new-enough libc to
+ * contain the call.
+ */
+long riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
+		   size_t cpusetsize, unsigned long *cpus, unsigned int flags);
+
+#endif
tools/testing/selftests/riscv/hwprobe/which-cpus.c new file +154 -0

154 lines not on this page · raw message

tools/testing/selftests/riscv/vector/vstate_prctl.c +1 -9
index b348b475be57..8dcd399ef7fc 100644
@@ -1,20 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0-only
 #include <sys/prctl.h>
 #include <unistd.h>
-#include <asm/hwprobe.h>
 #include <errno.h>
 #include <sys/wait.h>
 
+#include "../hwprobe/hwprobe.h"
 #include "../../kselftest.h"
 
-/*
- * Rather than relying on having a new enough libc to define this, just do it
- * ourselves.  This way we don't need to be coupled to a new-enough libc to
- * contain the call.
- */
-long riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
-		   size_t cpu_count, unsigned long *cpus, unsigned int flags);
-
 #define NEXT_PROGRAM "./vstate_exec_nolibc"
 static int launch_test(int test_inherit)
 {
Signature · 7 lines
2.41.0


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH v1 1/6] RISC-V: hwprobe: Clarify cpus size parameter

In reply to Andrew Jones
Quoted 11 lines · Andrew Jones

The "count" parameter associated with the 'cpus' parameter of the hwprobe syscall is the size in bytes of 'cpus'. Naming it 'cpu_count' may mislead users (it did me) to think it's the number of CPUs that are or can be represented by 'cpus' instead. This is particularly easy (IMO) to get wrong since 'cpus' is documented to be defined by CPU_SET(3) and CPU_SET(3) also documents a CPU_COUNT() (the number of CPUs in set) macro. CPU_SET(3) refers to the size of cpu sets with 'setsize'. Adopt 'cpusetsize' for the hwprobe parameter and specifically state it is in bytes in Documentation/riscv/hwprobe.rst to clarify.

Quoted 2 lines

Thanks,
Conor.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#4 lore raw

Re: [PATCH v1 2/6] RISC-V: Move the hwprobe syscall to its own file

In reply to Andrew Jones

arch/riscv/kernel/sys_hwprobe.c line 9

Quoted 11 lines · Andrew Jones

+++ b/arch/riscv/kernel/sys_hwprobe.c

arch/riscv/kernel/sys_hwprobe.c new file +9 -0
@@ -0,0 +1,276 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * The hwprobe interface, for allowing userspace to probe to see which features
+ * are supported by the hardware.  See Documentation/riscv/hwprobe.rst for more
+ * details.
+ *
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2014 Darius Rad <[email protected]>
+ * Copyright (C) 2017 SiFive

So uh, this is all new(ish) code, originally written last September, that is being moved, right?

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#5 lore raw

Re: [PATCH v1 2/6] RISC-V: Move the hwprobe syscall to its own file

Quoted 16 lines · Conor Dooley

arch/riscv/kernel/sys_hwprobe.c line 9

Quoted 11 lines · Andrew Jones

+++ b/arch/riscv/kernel/sys_hwprobe.c

arch/riscv/kernel/sys_hwprobe.c new file +9 -0
@@ -0,0 +1,276 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * The hwprobe interface, for allowing userspace to probe to see which features
+ * are supported by the hardware.  See Documentation/riscv/hwprobe.rst for more
+ * details.
+ *
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2014 Darius Rad <[email protected]>
+ * Copyright (C) 2017 SiFive

So uh, this is all new(ish) code, originally written last September, that is being moved, right?

Yeah, I just pulled the Copyrights over by standard practice of code movement, but I agree they don't make much sense for the code I moved. I suck at copyright management and would be happy for suggestions here. If Rivos would like to put one here for the work Evan did, then I'll be happy to add it. Or, if people prefer, I could add a Ventana copyright.

For me, whatever protects the open source code best is best :-)

Thanks,
drew

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#6 lore raw

Re: [PATCH v1 2/6] RISC-V: Move the hwprobe syscall to its own file

Quoted 19 lines · Andrew Jones
Quoted 16 lines · Conor Dooley

arch/riscv/kernel/sys_hwprobe.c line 9

Quoted 11 lines · Andrew Jones

+++ b/arch/riscv/kernel/sys_hwprobe.c

arch/riscv/kernel/sys_hwprobe.c new file +9 -0
@@ -0,0 +1,276 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * The hwprobe interface, for allowing userspace to probe to see which features
+ * are supported by the hardware.  See Documentation/riscv/hwprobe.rst for more
+ * details.
+ *
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2014 Darius Rad <[email protected]>
+ * Copyright (C) 2017 SiFive

So uh, this is all new(ish) code, originally written last September, that is being moved, right?

Quoted 5 lines

Yeah, I just pulled the Copyrights over by standard practice of code movement, but I agree they don't make much sense for the code I moved. I suck at copyright management and would be happy for suggestions here. If Rivos would like to put one here for the work Evan did, then I'll be happy to add it. Or, if people prefer, I could add a Ventana copyright.

I probably would have stuck a Rivos banner on it if I had written it as a new file myself, as I think they "own" the work I do. But it all feels a bit like vanity license plates given it's GPL, so I imagine whatever you do will get no real complaints.

-Evan

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#7 lore raw

Re: [PATCH v1 2/6] RISC-V: Move the hwprobe syscall to its own file

Quoted 26 lines · Evan Green
Quoted 19 lines · Andrew Jones
Quoted 16 lines · Conor Dooley

arch/riscv/kernel/sys_hwprobe.c line 9

Quoted 11 lines · Andrew Jones

+++ b/arch/riscv/kernel/sys_hwprobe.c

arch/riscv/kernel/sys_hwprobe.c new file +9 -0
@@ -0,0 +1,276 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * The hwprobe interface, for allowing userspace to probe to see which features
+ * are supported by the hardware.  See Documentation/riscv/hwprobe.rst for more
+ * details.
+ *
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2014 Darius Rad <[email protected]>
+ * Copyright (C) 2017 SiFive

So uh, this is all new(ish) code, originally written last September, that is being moved, right?

Quoted 5 lines

Yeah, I just pulled the Copyrights over by standard practice of code movement, but I agree they don't make much sense for the code I moved. I suck at copyright management and would be happy for suggestions here. If Rivos would like to put one here for the work Evan did, then I'll be happy to add it. Or, if people prefer, I could add a Ventana copyright.

Quoted 4 lines

I probably would have stuck a Rivos banner on it if I had written it as a new file myself, as I think they "own" the work I do. But it all feels a bit like vanity license plates given it's GPL, so I imagine whatever you do will get no real complaints.

Aye, that's how I see it too. I've not bothered adding that stuff where I could help it. The git history shows who made copyrightable changes to the file anyway, but obv. I am no lawyer.
Just in this case, since as far as I could tell this code was written from LPC onwards, it made little sense to copy over some 2012 era copyright information.

Thanks,
Conor.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#12 lore raw

Re: [PATCH v1 3/6] RISC-V: hwprobe: Introduce which-cpus flag

In reply to Andrew Jones
Quoted 23 lines · Andrew Jones

Introduce the first flag for the hwprobe syscall. The flag basically reverses its behavior, i.e. instead of populating the values of keys for a given set of cpus, the set of cpus after the call is the result of finding a set which supports the values of the keys. In order to do this, we implement a pair compare function which takes the type of value (a single value vs. a bitmask of booleans) into consideration. We also implement vdso support for the new flag.

---
 Documentation/riscv/hwprobe.rst       | 16 ++++-
 arch/riscv/include/asm/hwprobe.h      | 24 +++++++
 arch/riscv/include/uapi/asm/hwprobe.h |  3 +
 arch/riscv/kernel/sys_hwprobe.c       | 93 +++++++++++++++++++++++++--
 arch/riscv/kernel/vdso/hwprobe.c      | 68 +++++++++++++++++---
 5 files changed, 190 insertions(+), 14 deletions(-)
Documentation/riscv/hwprobe.rst +0 -0
index c57437e40ffb..576aa03f56bb 100644

Documentation/riscv/hwprobe.rst arch, impl), the returned value will only be valid if all CPUs in the given set · line 32

Quoted 10 lines
Documentation/riscv/hwprobe.rst +5 -2
@@ -25,8 +25,20 @@ arch, impl), the returned value will only be valid if all CPUs in the given set
 have the same value. Otherwise -1 will be returned. For boolean-like keys, the
 value returned will be a logical AND of the values for the specified CPUs.
 Usermode can supply NULL for ``cpus`` and 0 for ``cpusetsize`` as a shortcut for
-all online CPUs. There are currently no flags, this value must be zero for
-future compatibility.
+all online CPUs. The currently supported flags are:
+
+* :c:macro:`RISCV_HWPROBE_WHICH_CPUS`: This flag basically reverses the behavior
+  of sys_riscv_hwprobe().  Instead of populating the values of keys for a given
+  set of CPUs, the set of CPUs is initially all unset and the values of each key

This isn't quite right anymore, I reckon. The cpuset passed in is used as an initial set, and this function removes CPUs from the set that have differing values from those given in the array of pairs. If an empty cpuset is passed in, then the initial set used is all online cpus.

Quoted 82 lines
Documentation/riscv/hwprobe.rst +9 -0
+  are given.  Upon return, the CPUs which all match each of the given key-value
+  pairs are set in ``cpus``.  How matching is done depends on the key type.  For
+  value-like keys, matching means to be the exact same as the value.  For
+  boolean-like keys, matching means the result of a logical AND of the pair's
+  value with the CPU's value is exactly the same as the pair's value.  ``cpus``
+  may also initially have set bits, in which case the bits of any CPUs which do
+  not match the pairs will be cleared, but no other bits will be set.
+
+All other flags are reserved for future compatibility and must be zero.
 
 On success 0 is returned, on failure a negative error code is returned.
 
arch/riscv/include/asm/hwprobe.h +24 -0
index 7cad513538d8..a68764149e51 100644
@@ -15,4 +15,28 @@ static inline bool riscv_hwprobe_key_is_valid(__s64 key)
        return key >= 0 && key <= RISCV_HWPROBE_MAX_KEY;
 }
 
+static inline bool hwprobe_key_is_bitmask(__s64 key)
+{
+       switch (key) {
+       case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
+       case RISCV_HWPROBE_KEY_IMA_EXT_0:
+       case RISCV_HWPROBE_KEY_CPUPERF_0:
+               return true;
+       }
+
+       return false;
+}
+
+static inline bool riscv_hwprobe_pair_cmp(struct riscv_hwprobe *pair,
+                                         struct riscv_hwprobe *other_pair)
+{
+       if (pair->key != other_pair->key)
+               return false;
+
+       if (hwprobe_key_is_bitmask(pair->key))
+               return (pair->value & other_pair->value) == other_pair->value;
+
+       return pair->value == other_pair->value;
+}
+
 #endif
arch/riscv/include/uapi/asm/hwprobe.h +3 -0
index 006bfb48343d..1d4134befc48 100644
@@ -38,4 +38,7 @@ struct riscv_hwprobe {
 #define                RISCV_HWPROBE_MISALIGNED_MASK           (7 << 0)
 /* Increase RISCV_HWPROBE_MAX_KEY when adding items. */
 
+/* Flags */
+#define RISCV_HWPROBE_WHICH_CPUS       (1 << 0)
+
 #endif
arch/riscv/kernel/sys_hwprobe.c +6 -4
index 69ad5f793374..de294538ca25 100644
@@ -166,10 +166,10 @@ static void hwprobe_one_pair(struct riscv_hwprobe *pair,
        }
 }
 
-static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs,
-                           size_t pair_count, size_t cpusetsize,
-                           unsigned long __user *cpus_user,
-                           unsigned int flags)
+static int hwprobe_get_values(struct riscv_hwprobe __user *pairs,
+                             size_t pair_count, size_t cpusetsize,
+                             unsigned long __user *cpus_user,
+                             unsigned int flags)
 {
        size_t out;
        int ret;
@@ -223,6 +223,91 @@ static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs,
        return 0;
 }
 
+static int hwprobe_get_cpus(struct riscv_hwprobe __user *pairs,
+                           size_t pair_count, size_t cpusetsize,

arch/riscv/kernel/sys_hwprobe.c static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs, · line 237

Quoted 10 lines
arch/riscv/kernel/sys_hwprobe.c +10 -0
+                           unsigned long __user *cpus_user,
+                           unsigned int flags)
+{
+       cpumask_t cpus, one_cpu;
+       bool clear_all = false;
+       size_t i;
+       int ret;
+
+       if (flags != RISCV_HWPROBE_WHICH_CPUS)
+               return -EINVAL;

We'll have to be careful if we add another flag to deal with how it behaves here too. I think the choice you made here is correct as it's the most defensive. It's (usually) easy to change a failure to a non-failure, but an ABI compatibility issue to change a weird-but-nonfailing behavior to a different behavior. This might be worth a comment, but I also tend to love comments more than most, so up to you.

Quoted 4 lines
arch/riscv/kernel/sys_hwprobe.c +4 -0
+
+       if (!cpusetsize || !cpus_user)
+               return -EINVAL;
+

arch/riscv/kernel/sys_hwprobe.c static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs, · line 251

Quoted 10 lines
arch/riscv/kernel/sys_hwprobe.c +10 -0
+       if (cpusetsize > cpumask_size())
+               cpusetsize = cpumask_size();
+
+       ret = copy_from_user(&cpus, cpus_user, cpusetsize);
+       if (ret)
+               return -EFAULT;
+
+       cpumask_and(&cpus, &cpus, cpu_online_mask);
+       if (cpumask_empty(&cpus))
+               cpumask_copy(&cpus, cpu_online_mask);

I worry this is accident prone. If the caller asks for some set of CPUs that don't happen to be online right now, they get an answer for a completely different set of CPUs. I'm all for having a shorthand for "all online CPUs", but I think it should be more explicit. If you moved the cpumask_and() below the if(cpumask_empty()), then IMO it would be a clean shorthand: pass in an empty set to get all online CPUs.

Quoted 103 lines
arch/riscv/kernel/sys_hwprobe.c +59 -0
+
+       cpumask_clear(&one_cpu);
+
+       for (i = 0; i < pair_count; i++) {
+               struct riscv_hwprobe pair, tmp;
+               int cpu;
+
+               ret = copy_from_user(&pair, &pairs[i], sizeof(pair));
+               if (ret)
+                       return -EFAULT;
+
+               if (!riscv_hwprobe_key_is_valid(pair.key)) {
+                       clear_all = true;
+                       pair = (struct riscv_hwprobe){ .key = -1, };
+                       ret = copy_to_user(&pairs[i], &pair, sizeof(pair));
+                       if (ret)
+                               return -EFAULT;
+               }
+
+               if (clear_all)
+                       continue;
+
+               tmp = (struct riscv_hwprobe){ .key = pair.key, };
+
+               for_each_cpu(cpu, &cpus) {
+                       cpumask_set_cpu(cpu, &one_cpu);
+
+                       hwprobe_one_pair(&tmp, &one_cpu);
+
+                       if (!riscv_hwprobe_pair_cmp(&tmp, &pair))
+                               cpumask_clear_cpu(cpu, &cpus);
+
+                       cpumask_clear_cpu(cpu, &one_cpu);
+               }
+       }
+
+       if (clear_all)
+               cpumask_clear(&cpus);
+
+       ret = copy_to_user(cpus_user, &cpus, cpusetsize);
+       if (ret)
+               return -EFAULT;
+
+       return 0;
+}
+
+static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs,
+                           size_t pair_count, size_t cpusetsize,
+                           unsigned long __user *cpus_user,
+                           unsigned int flags)
+{
+       if (flags & RISCV_HWPROBE_WHICH_CPUS)
+               return hwprobe_get_cpus(pairs, pair_count, cpusetsize,
+                                       cpus_user, flags);
+
+       return hwprobe_get_values(pairs, pair_count, cpusetsize,
+                                 cpus_user, flags);
+}
+
 #ifdef CONFIG_MMU
 
 static int __init init_hwprobe_vdso_data(void)
arch/riscv/kernel/vdso/hwprobe.c +11 -8
index 026b7645c5ab..e6c324d64544 100644
@@ -3,6 +3,7 @@
  * Copyright 2023 Rivos, Inc
  */
 
+#include <linux/string.h>
 #include <linux/types.h>
 #include <vdso/datapage.h>
 #include <vdso/helpers.h>
@@ -11,14 +12,9 @@ extern int riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
                         size_t cpusetsize, 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 cpusetsize, unsigned long *cpus,
-                        unsigned int flags);
-
-int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
-                        size_t cpusetsize, unsigned long *cpus,
-                        unsigned int flags)
+static int riscv_vdso_get_values(struct riscv_hwprobe *pairs, size_t pair_count,
+                                size_t cpusetsize, unsigned long *cpus,
+                                unsigned int flags)
 {
        const struct vdso_data *vd = __arch_get_vdso_data();
        const struct arch_vdso_data *avd = &vd->arch_data;
@@ -50,3 +46,59 @@ int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
 
        return 0;
 }
+
+static int riscv_vdso_get_cpus(struct riscv_hwprobe *pairs, size_t pair_count,
+                              size_t cpusetsize, unsigned long *cpus,
+                              unsigned int flags)
+{
+       const struct vdso_data *vd = __arch_get_vdso_data();
+       const struct arch_vdso_data *avd = &vd->arch_data;

arch/riscv/kernel/vdso/hwprobe.c int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count, · line 65

Quoted 10 lines
arch/riscv/kernel/vdso/hwprobe.c +10 -0
+       struct riscv_hwprobe *p = pairs;
+       struct riscv_hwprobe *end = pairs + pair_count;
+       bool clear_all = false;
+
+       if (!cpusetsize || !cpus)
+               return -EINVAL;
+
+       if (flags != RISCV_HWPROBE_WHICH_CPUS || !avd->homogeneous_cpus)
+               return riscv_hwprobe(pairs, pair_count, cpusetsize, cpus, flags);
+

Hm, I realize now that doing the shorthand of "empty set == all online cpus" leaves the VDSO in a slight lurch, as we now have to figure out how to come up with the cpu_online_mask in usermode. It'd be a bummer if the shorthand always bounced to the system call, as I think this will be the most common case. Is there a way we could stash the cpu_online_mask in the vDSO data without it going stale? Any other ideas?

Quoted 40 lines
arch/riscv/kernel/vdso/hwprobe.c +39 -1
+       while (p < end) {
+               if (riscv_hwprobe_key_is_valid(p->key)) {
+                       struct riscv_hwprobe t = {
+                               .key = p->key,
+                               .value = avd->all_cpu_hwprobe_values[p->key],
+                       };
+
+                       if (!riscv_hwprobe_pair_cmp(&t, p))
+                               clear_all = true;
+               } else {
+                       clear_all = true;
+                       p->key = -1;
+                       p->value = 0;
+               }
+               p++;
+       }
+
+       if (clear_all)
+               memset(cpus, 0, cpusetsize);
+
+       return 0;
+}
+
+/* Add a prototype to avoid -Wmissing-prototypes warning. */
+int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
+                        size_t cpusetsize, unsigned long *cpus,
+                        unsigned int flags);
+
+int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
+                        size_t cpusetsize, unsigned long *cpus,
+                        unsigned int flags)
+{
+       if (flags & RISCV_HWPROBE_WHICH_CPUS)
+               return riscv_vdso_get_cpus(pairs, pair_count, cpusetsize,
+                                          cpus, flags);
+
+       return riscv_vdso_get_values(pairs, pair_count, cpusetsize,
+                                    cpus, flags);
+}
--
Quoted 2 lines

2.41.0

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#8 lore raw

Re: [PATCH v1 2/6] RISC-V: Move the hwprobe syscall to its own file

In reply to Conor Dooley
Quoted 32 lines · Conor Dooley
Quoted 26 lines · Evan Green
Quoted 19 lines · Andrew Jones
Quoted 16 lines · Conor Dooley

arch/riscv/kernel/sys_hwprobe.c line 9

Quoted 11 lines · Andrew Jones

+++ b/arch/riscv/kernel/sys_hwprobe.c

arch/riscv/kernel/sys_hwprobe.c new file +9 -0
@@ -0,0 +1,276 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * The hwprobe interface, for allowing userspace to probe to see which features
+ * are supported by the hardware.  See Documentation/riscv/hwprobe.rst for more
+ * details.
+ *
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2014 Darius Rad <[email protected]>
+ * Copyright (C) 2017 SiFive

So uh, this is all new(ish) code, originally written last September, that is being moved, right?

Quoted 5 lines

Yeah, I just pulled the Copyrights over by standard practice of code movement, but I agree they don't make much sense for the code I moved. I suck at copyright management and would be happy for suggestions here. If Rivos would like to put one here for the work Evan did, then I'll be happy to add it. Or, if people prefer, I could add a Ventana copyright.

Quoted 4 lines

I probably would have stuck a Rivos banner on it if I had written it as a new file myself, as I think they "own" the work I do. But it all feels a bit like vanity license plates given it's GPL, so I imagine whatever you do will get no real complaints.

Quoted 7 lines

Aye, that's how I see it too. I've not bothered adding that stuff where I could help it. The git history shows who made copyrightable changes to the file anyway, but obv. I am no lawyer.
Just in this case, since as far as I could tell this code was written from LPC onwards, it made little sense to copy over some 2012 era copyright information.

So what's the final plan? I feel a bit strange adding a Rivos copyright while copying the Rivos code over since I don't consider myself "authorized" to do so. I also don't want to add a Ventana one for just code movement. Do we need a copyright on this file at all? Should I move it without anything and then add a Ventana copyright when adding the which-cpus stuff?

Thanks,
drew

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#13 lore raw

Re: [PATCH v1 3/6] RISC-V: hwprobe: Introduce which-cpus flag

In reply to Evan Green
Quoted 32 lines · Evan Green
Quoted 31 lines · Andrew Jones

Introduce the first flag for the hwprobe syscall. The flag basically reverses its behavior, i.e. instead of populating the values of keys for a given set of cpus, the set of cpus after the call is the result of finding a set which supports the values of the keys. In order to do this, we implement a pair compare function which takes the type of value (a single value vs. a bitmask of booleans) into consideration. We also implement vdso support for the new flag.

---
 Documentation/riscv/hwprobe.rst       | 16 ++++-
 arch/riscv/include/asm/hwprobe.h      | 24 +++++++
 arch/riscv/include/uapi/asm/hwprobe.h |  3 +
 arch/riscv/kernel/sys_hwprobe.c       | 93 +++++++++++++++++++++++++--
 arch/riscv/kernel/vdso/hwprobe.c      | 68 +++++++++++++++++---
 5 files changed, 190 insertions(+), 14 deletions(-)
Documentation/riscv/hwprobe.rst +3 -2
index c57437e40ffb..576aa03f56bb 100644
@@ -25,8 +25,20 @@ arch, impl), the returned value will only be valid if all CPUs in the given set
 have the same value. Otherwise -1 will be returned. For boolean-like keys, the
 value returned will be a logical AND of the values for the specified CPUs.
 Usermode can supply NULL for ``cpus`` and 0 for ``cpusetsize`` as a shortcut for
-all online CPUs. There are currently no flags, this value must be zero for
-future compatibility.
+all online CPUs. The currently supported flags are:
+
+* :c:macro:`RISCV_HWPROBE_WHICH_CPUS`: This flag basically reverses the behavior
Quoted 8 lines

Documentation/riscv/hwprobe.rst arch, impl), the returned value will only be valid if all CPUs in the given set · line 32

Quoted 2 lines
Documentation/riscv/hwprobe.rst +2 -0
+  of sys_riscv_hwprobe().  Instead of populating the values of keys for a given
+  set of CPUs, the set of CPUs is initially all unset and the values of each key

This isn't quite right anymore, I reckon. The cpuset passed in is used as an initial set, and this function removes CPUs from the set that have differing values from those given in the array of pairs. If an empty cpuset is passed in, then the initial set used is all online cpus.

Indeed. I'll add more text describing the "which cpus of this cpuset" behavior for v2.

Quoted 94 lines
Quoted 83 lines
Documentation/riscv/hwprobe.rst +9 -0
+  are given.  Upon return, the CPUs which all match each of the given key-value
+  pairs are set in ``cpus``.  How matching is done depends on the key type.  For
+  value-like keys, matching means to be the exact same as the value.  For
+  boolean-like keys, matching means the result of a logical AND of the pair's
+  value with the CPU's value is exactly the same as the pair's value.  ``cpus``
+  may also initially have set bits, in which case the bits of any CPUs which do
+  not match the pairs will be cleared, but no other bits will be set.
+
+All other flags are reserved for future compatibility and must be zero.
 
 On success 0 is returned, on failure a negative error code is returned.
 
arch/riscv/include/asm/hwprobe.h +24 -0
index 7cad513538d8..a68764149e51 100644
@@ -15,4 +15,28 @@ static inline bool riscv_hwprobe_key_is_valid(__s64 key)
        return key >= 0 && key <= RISCV_HWPROBE_MAX_KEY;
 }
 
+static inline bool hwprobe_key_is_bitmask(__s64 key)
+{
+       switch (key) {
+       case RISCV_HWPROBE_KEY_BASE_BEHAVIOR:
+       case RISCV_HWPROBE_KEY_IMA_EXT_0:
+       case RISCV_HWPROBE_KEY_CPUPERF_0:
+               return true;
+       }
+
+       return false;
+}
+
+static inline bool riscv_hwprobe_pair_cmp(struct riscv_hwprobe *pair,
+                                         struct riscv_hwprobe *other_pair)
+{
+       if (pair->key != other_pair->key)
+               return false;
+
+       if (hwprobe_key_is_bitmask(pair->key))
+               return (pair->value & other_pair->value) == other_pair->value;
+
+       return pair->value == other_pair->value;
+}
+
 #endif
arch/riscv/include/uapi/asm/hwprobe.h +3 -0
index 006bfb48343d..1d4134befc48 100644
@@ -38,4 +38,7 @@ struct riscv_hwprobe {
 #define                RISCV_HWPROBE_MISALIGNED_MASK           (7 << 0)
 /* Increase RISCV_HWPROBE_MAX_KEY when adding items. */
 
+/* Flags */
+#define RISCV_HWPROBE_WHICH_CPUS       (1 << 0)
+
 #endif
arch/riscv/kernel/sys_hwprobe.c +6 -4
index 69ad5f793374..de294538ca25 100644
@@ -166,10 +166,10 @@ static void hwprobe_one_pair(struct riscv_hwprobe *pair,
        }
 }
 
-static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs,
-                           size_t pair_count, size_t cpusetsize,
-                           unsigned long __user *cpus_user,
-                           unsigned int flags)
+static int hwprobe_get_values(struct riscv_hwprobe __user *pairs,
+                             size_t pair_count, size_t cpusetsize,
+                             unsigned long __user *cpus_user,
+                             unsigned int flags)
 {
        size_t out;
        int ret;
@@ -223,6 +223,91 @@ static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs,
        return 0;
 }
 
+static int hwprobe_get_cpus(struct riscv_hwprobe __user *pairs,
+                           size_t pair_count, size_t cpusetsize,

arch/riscv/kernel/sys_hwprobe.c static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs, · line 237

Quoted 10 lines
arch/riscv/kernel/sys_hwprobe.c +10 -0
+                           unsigned long __user *cpus_user,
+                           unsigned int flags)
+{
+       cpumask_t cpus, one_cpu;
+       bool clear_all = false;
+       size_t i;
+       int ret;
+
+       if (flags != RISCV_HWPROBE_WHICH_CPUS)
+               return -EINVAL;
Quoted 7 lines

We'll have to be careful if we add another flag to deal with how it behaves here too. I think the choice you made here is correct as it's the most defensive. It's (usually) easy to change a failure to a non-failure, but an ABI compatibility issue to change a weird-but-nonfailing behavior to a different behavior. This might be worth a comment, but I also tend to love comments more than most, so up to you.

I'll probably leave it without a comment. My thinking is that if we add another flag which applies to which-cpus, then the selftests, which should get updated with new tests for the new flag, should quickly point this out. I prefer avoiding comments when possible, since neither the compiler nor test cases test them, allowing them to easily go stale.

Quoted 16 lines
Quoted 5 lines
arch/riscv/kernel/sys_hwprobe.c +4 -0
+
+       if (!cpusetsize || !cpus_user)
+               return -EINVAL;
+

arch/riscv/kernel/sys_hwprobe.c static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs, · line 251

Quoted 10 lines
arch/riscv/kernel/sys_hwprobe.c +10 -0
+       if (cpusetsize > cpumask_size())
+               cpusetsize = cpumask_size();
+
+       ret = copy_from_user(&cpus, cpus_user, cpusetsize);
+       if (ret)
+               return -EFAULT;
+
+       cpumask_and(&cpus, &cpus, cpu_online_mask);
+       if (cpumask_empty(&cpus))
+               cpumask_copy(&cpus, cpu_online_mask);
Quoted 7 lines

I worry this is accident prone. If the caller asks for some set of CPUs that don't happen to be online right now, they get an answer for a completely different set of CPUs. I'm all for having a shorthand for "all online CPUs", but I think it should be more explicit. If you moved the cpumask_and() below the if(cpumask_empty()), then IMO it would be a clean shorthand: pass in an empty set to get all online CPUs.

Good catch. The way it is now is indeed a bug and your suggestion is the fix. Will do for v2.

Quoted 111 lines
Quoted 111 lines
arch/riscv/kernel/sys_hwprobe.c +59 -0
+
+       cpumask_clear(&one_cpu);
+
+       for (i = 0; i < pair_count; i++) {
+               struct riscv_hwprobe pair, tmp;
+               int cpu;
+
+               ret = copy_from_user(&pair, &pairs[i], sizeof(pair));
+               if (ret)
+                       return -EFAULT;
+
+               if (!riscv_hwprobe_key_is_valid(pair.key)) {
+                       clear_all = true;
+                       pair = (struct riscv_hwprobe){ .key = -1, };
+                       ret = copy_to_user(&pairs[i], &pair, sizeof(pair));
+                       if (ret)
+                               return -EFAULT;
+               }
+
+               if (clear_all)
+                       continue;
+
+               tmp = (struct riscv_hwprobe){ .key = pair.key, };
+
+               for_each_cpu(cpu, &cpus) {
+                       cpumask_set_cpu(cpu, &one_cpu);
+
+                       hwprobe_one_pair(&tmp, &one_cpu);
+
+                       if (!riscv_hwprobe_pair_cmp(&tmp, &pair))
+                               cpumask_clear_cpu(cpu, &cpus);
+
+                       cpumask_clear_cpu(cpu, &one_cpu);
+               }
+       }
+
+       if (clear_all)
+               cpumask_clear(&cpus);
+
+       ret = copy_to_user(cpus_user, &cpus, cpusetsize);
+       if (ret)
+               return -EFAULT;
+
+       return 0;
+}
+
+static int do_riscv_hwprobe(struct riscv_hwprobe __user *pairs,
+                           size_t pair_count, size_t cpusetsize,
+                           unsigned long __user *cpus_user,
+                           unsigned int flags)
+{
+       if (flags & RISCV_HWPROBE_WHICH_CPUS)
+               return hwprobe_get_cpus(pairs, pair_count, cpusetsize,
+                                       cpus_user, flags);
+
+       return hwprobe_get_values(pairs, pair_count, cpusetsize,
+                                 cpus_user, flags);
+}
+
 #ifdef CONFIG_MMU
 
 static int __init init_hwprobe_vdso_data(void)
arch/riscv/kernel/vdso/hwprobe.c +18 -8
index 026b7645c5ab..e6c324d64544 100644
@@ -3,6 +3,7 @@
  * Copyright 2023 Rivos, Inc
  */
 
+#include <linux/string.h>
 #include <linux/types.h>
 #include <vdso/datapage.h>
 #include <vdso/helpers.h>
@@ -11,14 +12,9 @@ extern int riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
                         size_t cpusetsize, 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 cpusetsize, unsigned long *cpus,
-                        unsigned int flags);
-
-int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
-                        size_t cpusetsize, unsigned long *cpus,
-                        unsigned int flags)
+static int riscv_vdso_get_values(struct riscv_hwprobe *pairs, size_t pair_count,
+                                size_t cpusetsize, unsigned long *cpus,
+                                unsigned int flags)
 {
        const struct vdso_data *vd = __arch_get_vdso_data();
        const struct arch_vdso_data *avd = &vd->arch_data;
@@ -50,3 +46,59 @@ int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
 
        return 0;
 }
+
+static int riscv_vdso_get_cpus(struct riscv_hwprobe *pairs, size_t pair_count,
+                              size_t cpusetsize, unsigned long *cpus,
+                              unsigned int flags)
+{
+       const struct vdso_data *vd = __arch_get_vdso_data();
+       const struct arch_vdso_data *avd = &vd->arch_data;
+       struct riscv_hwprobe *p = pairs;
+       struct riscv_hwprobe *end = pairs + pair_count;
+       bool clear_all = false;
+
+       if (!cpusetsize || !cpus)
+               return -EINVAL;
+
Quoted 8 lines

arch/riscv/kernel/vdso/hwprobe.c int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count, · line 65

Quoted 3 lines
arch/riscv/kernel/vdso/hwprobe.c +3 -0
+       if (flags != RISCV_HWPROBE_WHICH_CPUS || !avd->homogeneous_cpus)
+               return riscv_hwprobe(pairs, pair_count, cpusetsize, cpus, flags);
+

Hm, I realize now that doing the shorthand of "empty set == all online cpus" leaves the VDSO in a slight lurch, as we now have to figure out how to come up with the cpu_online_mask in usermode. It'd be a bummer if the shorthand always bounced to the system call,

Huh, I recall thinking about this while working on it, but now, looking at the code I wrote, I see that I apparently forgot to address it. I had intended to bounce to the system call to handle the empty set case, but I'm missing the check for that!

Quoted 4 lines

as I think this
will be the most common case. Is there a way we could stash the cpu_online_mask in the vDSO data without it going stale? Any other ideas?

I don't think we can stash the data and keep it synchronized and, while the empty set case may be useful for platform description utilities, I actually suspect applications which want to set the affinity of their threads based on cpu features will want to start by setting their set to the result of sched_getaffinity(). This is because it wouldn't be useful for an application to know that cpu A has the extension they need if their process has been confined to a set excluding cpu A with cgroups.

I'll fix this lack of empty set check for v2.

Thanks,
drew

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#9 lore raw

Re: [PATCH v1 2/6] RISC-V: Move the hwprobe syscall to its own file

In reply to Andrew Jones
Quoted 41 lines · Andrew Jones
Quoted 32 lines · Conor Dooley
Quoted 26 lines · Evan Green
Quoted 19 lines · Andrew Jones
Quoted 16 lines · Conor Dooley

arch/riscv/kernel/sys_hwprobe.c line 9

Quoted 11 lines · Andrew Jones

+++ b/arch/riscv/kernel/sys_hwprobe.c

arch/riscv/kernel/sys_hwprobe.c new file +9 -0
@@ -0,0 +1,276 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * The hwprobe interface, for allowing userspace to probe to see which features
+ * are supported by the hardware.  See Documentation/riscv/hwprobe.rst for more
+ * details.
+ *
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2014 Darius Rad <[email protected]>
+ * Copyright (C) 2017 SiFive

So uh, this is all new(ish) code, originally written last September, that is being moved, right?

Quoted 5 lines

Yeah, I just pulled the Copyrights over by standard practice of code movement, but I agree they don't make much sense for the code I moved. I suck at copyright management and would be happy for suggestions here. If Rivos would like to put one here for the work Evan did, then I'll be happy to add it. Or, if people prefer, I could add a Ventana copyright.

Quoted 4 lines

I probably would have stuck a Rivos banner on it if I had written it as a new file myself, as I think they "own" the work I do. But it all feels a bit like vanity license plates given it's GPL, so I imagine whatever you do will get no real complaints.

Quoted 7 lines

Aye, that's how I see it too. I've not bothered adding that stuff where I could help it. The git history shows who made copyrightable changes to the file anyway, but obv. I am no lawyer.
Just in this case, since as far as I could tell this code was written from LPC onwards, it made little sense to copy over some 2012 era copyright information.

Quoted 3 lines

So what's the final plan? I feel a bit strange adding a Rivos copyright while copying the Rivos code over since I don't consider myself "authorized" to do so.

They didn't add one when they made the changes, so I wouldn't.

Quoted 4 lines

I also don't want to add a Ventana one for just
code movement. Do we need a copyright on this file at all? Should I move it without anything and then add a Ventana copyright when adding the which-cpus stuff?

I would move it without anything.
Apologies for derailing things with copyright notice crap :/

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#10 lore raw

Re: [PATCH v1 2/6] RISC-V: Move the hwprobe syscall to its own file

Quoted 49 lines · Conor Dooley
Quoted 41 lines · Andrew Jones
Quoted 32 lines · Conor Dooley
Quoted 26 lines · Evan Green
Quoted 19 lines · Andrew Jones
Quoted 16 lines · Conor Dooley

Review line 9

Quoted 11 lines · Andrew Jones

+++ b/arch/riscv/kernel/sys_hwprobe.c

(quoted diff) +9 -0
@@ -0,0 +1,276 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * The hwprobe interface, for allowing userspace to probe to see which features
+ * are supported by the hardware.  See Documentation/riscv/hwprobe.rst for more
+ * details.
+ *
+ * Copyright (C) 2012 Regents of the University of California
+ * Copyright (C) 2014 Darius Rad <[email protected]>
+ * Copyright (C) 2017 SiFive

So uh, this is all new(ish) code, originally written last September, that is being moved, right?

Quoted 5 lines

Yeah, I just pulled the Copyrights over by standard practice of code movement, but I agree they don't make much sense for the code I moved. I suck at copyright management and would be happy for suggestions here. If Rivos would like to put one here for the work Evan did, then I'll be happy to add it. Or, if people prefer, I could add a Ventana copyright.

Quoted 4 lines

I probably would have stuck a Rivos banner on it if I had written it as a new file myself, as I think they "own" the work I do. But it all feels a bit like vanity license plates given it's GPL, so I imagine whatever you do will get no real complaints.

Quoted 7 lines

Aye, that's how I see it too. I've not bothered adding that stuff where I could help it. The git history shows who made copyrightable changes to the file anyway, but obv. I am no lawyer.
Just in this case, since as far as I could tell this code was written from LPC onwards, it made little sense to copy over some 2012 era copyright information.

Quoted 3 lines

So what's the final plan? I feel a bit strange adding a Rivos copyright while copying the Rivos code over since I don't consider myself "authorized" to do so.

They didn't add one when they made the changes, so I wouldn't.

Quoted 7 lines
Quoted 4 lines

I also don't want to add a Ventana one for just
code movement. Do we need a copyright on this file at all? Should I move it without anything and then add a Ventana copyright when adding the which-cpus stuff?

I would move it without anything.
Apologies for derailing things with copyright notice crap :/

Works for me, too.
-Evan

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#14 lore raw

Re: [PATCH v1 3/6] RISC-V: hwprobe: Introduce which-cpus flag

In reply to Andrew Jones

On Wed, Oct 11, 2023 at 03:56:14PM +0200, Andrew Jones wrote:
...

Quoted 60 lines
arch/riscv/kernel/vdso/hwprobe.c +30 -8
index 026b7645c5ab..e6c324d64544 100644
@@ -3,6 +3,7 @@
  * Copyright 2023 Rivos, Inc
  */
 
+#include <linux/string.h>
 #include <linux/types.h>
 #include <vdso/datapage.h>
 #include <vdso/helpers.h>
@@ -11,14 +12,9 @@ extern int riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
 			 size_t cpusetsize, 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 cpusetsize, unsigned long *cpus,
-			 unsigned int flags);
-
-int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
-			 size_t cpusetsize, unsigned long *cpus,
-			 unsigned int flags)
+static int riscv_vdso_get_values(struct riscv_hwprobe *pairs, size_t pair_count,
+				 size_t cpusetsize, unsigned long *cpus,
+				 unsigned int flags)
 {
 	const struct vdso_data *vd = __arch_get_vdso_data();
 	const struct arch_vdso_data *avd = &vd->arch_data;
@@ -50,3 +46,59 @@ int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
 
 	return 0;
 }
+
+static int riscv_vdso_get_cpus(struct riscv_hwprobe *pairs, size_t pair_count,
+			       size_t cpusetsize, unsigned long *cpus,
+			       unsigned int flags)
+{
+	const struct vdso_data *vd = __arch_get_vdso_data();
+	const struct arch_vdso_data *avd = &vd->arch_data;
+	struct riscv_hwprobe *p = pairs;
+	struct riscv_hwprobe *end = pairs + pair_count;
+	bool clear_all = false;
+
+	if (!cpusetsize || !cpus)
+		return -EINVAL;
+
+	if (flags != RISCV_HWPROBE_WHICH_CPUS || !avd->homogeneous_cpus)
+		return riscv_hwprobe(pairs, pair_count, cpusetsize, cpus, flags);
+
+	while (p < end) {
+		if (riscv_hwprobe_key_is_valid(p->key)) {
+			struct riscv_hwprobe t = {
+				.key = p->key,
+				.value = avd->all_cpu_hwprobe_values[p->key],
+			};
+
+			if (!riscv_hwprobe_pair_cmp(&t, p))
+				clear_all = true;

arch/riscv/kernel/vdso/hwprobe.c int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count, · line 84

Quoted 10 lines
arch/riscv/kernel/vdso/hwprobe.c +10 -0
+		} else {
+			clear_all = true;
+			p->key = -1;
+			p->value = 0;
+		}
+		p++;
+	}
+
+	if (clear_all)
+		memset(cpus, 0, cpusetsize);

This memset will go away in v2. It wasn't very smart of me to put it there in the first place as there's no memset available to vdso code, which means it would segfault when attempting to use it. My initial testing failed to find this, because I had forgotten that avd->homogeneous_cpus would be false on a default QEMU machine since it doesn't set mvendorid to anything. I actually found this problem when trying to add a memcmp which wasn't skipped with !avd->homogeneous_cpus, and that promptly segfaulted. (I won't be adding that memcmp either :-)

Thanks,
drew

Quoted 23 lines
arch/riscv/kernel/vdso/hwprobe.c +20 -0
+
+	return 0;
+}
+
+/* Add a prototype to avoid -Wmissing-prototypes warning. */
+int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
+			 size_t cpusetsize, unsigned long *cpus,
+			 unsigned int flags);
+
+int __vdso_riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count,
+			 size_t cpusetsize, unsigned long *cpus,
+			 unsigned int flags)
+{
+	if (flags & RISCV_HWPROBE_WHICH_CPUS)
+		return riscv_vdso_get_cpus(pairs, pair_count, cpusetsize,
+					   cpus, flags);
+
+	return riscv_vdso_get_values(pairs, pair_count, cpusetsize,
+				     cpus, flags);
+}
Signature · 1 line
2.41.0

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv