Skip to contentSkip to conversation

scripts/runtime: Add ability to mark test as don't run by default

PATCH Superseded Suraj Jitindar Singh 4 participants 17 messages Aug 17, 2016 – Aug 19, 2016 KVM

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/5 scripts/runtime: Add ability to mark test as don't run by default

Invoking run_tests.sh without the -g parameter will by default run all of the tests for a given architecture. This patch series will add a test which has the ability to bring down the host and thus it might be nice if we double check that the user actually wants to run that test instead of them unknowingly bringing down a machine they might not want to.

In order to do this add the option for a tests' group parameter in unittests.cfg to include "nodefault" on order to indicate that it shouldn't be run be default.

When tests are invoked via run_tests.sh those with the nodefault group parameter will be skipped unless explicitly specified by the "-g" command line option. When tests with the nodefault group parameter are built and run standalone the user will be prompted on invocation to confirm that they actually want to run the test.

This allows a developer to mark a test as having potentially adverse effects and thus requires an extra level of confirmation from the user before they are invoked. Existing functionality will be preserved and new tests can choose any group other than "nodefault" if they want to be run by default.

---

Change Log:

V2 -> V3:
- Move checking on standalone invokation into a function "skip_nodefault" in scripts/runtime.bash
V3 -> V4:
- Remove superfluous "STANDALONE=yes"

---
 arm/unittests.cfg     |  3 +++
 powerpc/unittests.cfg |  3 +++
 scripts/runtime.bash  | 27 ++++++++++++++++++++++++++-
 x86/unittests.cfg     |  3 +++
 4 files changed, 35 insertions(+), 1 deletion(-)
arm/unittests.cfg +3 -0
index ffd12e5..3f6fa45 100644
@@ -12,6 +12,9 @@
 #					# specific to only one.
 # groups = <group_name1> <group_name2> ...	# Used to identify test cases
 #						# with run_tests -g ...
+#						# Specify group_name=nodefault
+#						# to have test not run by
+#						# default
 # accel = kvm|tcg		# Optionally specify if test must run with
 #				# kvm or tcg. If not specified, then kvm will
 #				# be used when available.
powerpc/unittests.cfg +3 -0
index ed4fdbe..0098cb6 100644
@@ -12,6 +12,9 @@
 #					# specific to only one.
 # groups = <group_name1> <group_name2> ...	# Used to identify test cases
 #						# with run_tests -g ...
+#						# Specify group_name=nodefault
+#						# to have test not run by
+#						# default
 # accel = kvm|tcg		# Optionally specify if test must run with
 #				# kvm or tcg. If not specified, then kvm will
 #				# be used when available.
scripts/runtime.bash +26 -1
index 0503cf0..1ae65c6 100644
@@ -32,6 +32,25 @@ get_cmdline()
     echo "TESTNAME=$testname TIMEOUT=$timeout ACCEL=$accel $RUNTIME_arch_run $kernel -smp $smp $opts"
 }
 
+skip_nodefault()
+{
+    [ "$STANDALONE" != "yes" ] && return 0
+
+    while true; do
+        read -p "Test marked not to be run by default, are you sure (Y/N)? " yn
+        case $yn in
+            "Y" | "y" | "Yes" | "yes")
+                return 1
+                ;;
+            "" | "N" | "n" | "No" | "no" | "q" | "quit" | "exit")
+                return 0
+                ;;
+            *)
+                ;;
+        esac
+    done
+}
+
 function run()
 {
     local testname="$1"
@@ -48,10 +67,16 @@ function run()
         return
     fi
 
-    if [ -n "$only_group" ] && ! grep -q "$only_group" <<<$groups; then
+    if [ -n "$only_group" ] && ! grep -qw "$only_group" <<<$groups; then
         return
     fi
 
+    if [ -z "$only_group" ] && grep -qw "nodefault" <<<$groups &&
+            skip_nodefault; then
+        echo -e "`SKIP` $testname (test marked as manual run only)"
+        return;
+    fi
+
     if [ -n "$arch" ] && [ "$arch" != "$ARCH" ]; then
         echo "`SKIP` $1 ($arch only)"
         return 2
x86/unittests.cfg +3 -0
index 60747cf..4a1f74e 100644
@@ -12,6 +12,9 @@
 #					# specific to only one.
 # groups = <group_name1> <group_name2> ...	# Used to identify test cases
 #						# with run_tests -g ...
+#						# Specify group_name=nodefault
+#						# to have test not run by
+#						# default
 # accel = kvm|tcg		# Optionally specify if test must run with
 #				# kvm or tcg. If not specified, then kvm will
 #				# be used when available.
Signature · 1 line
2.5.5
#1 lore raw

PATCH v4 2/5 lib/powerpc: Add generic decrementer exception handler

Add the lib/powerpc/handlers.c file and associated header files as a place to implement generic exception handler functions for inclusion in tests.

Add a generic exception handler for a decrementer (0x900) interrupt which will reset the decrementer to its maximum value.

---

Change Log:

V2 -> V3:
- Whitespace changes

---
 lib/powerpc/asm/handlers.h |  8 ++++++++
 lib/powerpc/handlers.c     | 22 ++++++++++++++++++++++
 lib/ppc64/asm/handlers.h   |  1 +
 powerpc/Makefile.common    |  1 +
 4 files changed, 32 insertions(+)
 create mode 100644 lib/powerpc/asm/handlers.h
 create mode 100644 lib/powerpc/handlers.c
 create mode 100644 lib/ppc64/asm/handlers.h
lib/powerpc/asm/handlers.h new file +8 -0
new file mode 100644
index 0000000..64ba727
@@ -0,0 +1,8 @@
+#ifndef _ASMPOWERPC_HANDLERS_H_
+#define _ASMPOWERPC_HANDLERS_H_
+
+#include <asm/ptrace.h>
+
+void dec_except_handler(struct pt_regs *regs, void *data);
+
+#endif /* _ASMPOWERPC_HANDLERS_H_ */
lib/powerpc/handlers.c new file +22 -0
new file mode 100644
index 0000000..be8226a
@@ -0,0 +1,22 @@
+/*
+ * Generic exception handlers for registration and use in tests
+ *
+ * Copyright 2016 Suraj Jitindar Singh, IBM.
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+
+#include <libcflat.h>
+#include <asm/handlers.h>
+#include <asm/ptrace.h>
+
+/*
+ * Generic handler for decrementer exceptions (0x900)
+ * Just reset the decrementer back to its maximum value (0x7FFFFFFF)
+ */
+void dec_except_handler(struct pt_regs *regs __unused, void *data __unused)
+{
+	uint32_t dec = 0x7FFFFFFF;
+
+	asm volatile ("mtdec %0" : : "r" (dec));
+}
lib/ppc64/asm/handlers.h new file +1 -0
new file mode 100644
index 0000000..92e6fb2
@@ -0,0 +1 @@
+#include "../../powerpc/asm/handlers.h"
powerpc/Makefile.common +1 -0
index 3f8887d..404194b 100644
@@ -36,6 +36,7 @@ cflatobjs += lib/powerpc/hcall.o
 cflatobjs += lib/powerpc/setup.o
 cflatobjs += lib/powerpc/rtas.o
 cflatobjs += lib/powerpc/processor.o
+cflatobjs += lib/powerpc/handlers.o
 
 FLATLIBS = $(libcflat) $(LIBFDT_archive)
 %.elf: CFLAGS += $(arch_CFLAGS)
Signature · 1 line
2.5.5
#2 lore raw

PATCH v4 3/5 lib/powerpc: Add function to start secondary threads

In reply to Suraj Jitindar Singh

Add the lib/powerpc/smp.c file and associated header files as a place to implement generic smp functionality for inclusion in tests.

Add functions start_all_cpus(), start_cpu() and start_thread() to start all stopped threads of all cpus, all stopped threads of a single cpu or a single stopped thread of a guest at a given execution location, respectively.

---

Change Log:

V2 -> V3:
- start_thread now returns int to reflect error, success or failure to start thread
- start_cpu returns number of threads on cpu and number successfully started
- start_all_cpus checks if number of threads started = total number of threads - 1
V3 -> V4:
- Style changes

---
 lib/powerpc/asm/smp.h   |  22 +++++++++++
 lib/powerpc/smp.c       | 102 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/ppc64/asm/smp.h     |   1 +
 powerpc/Makefile.common |   1 +
 4 files changed, 126 insertions(+)
 create mode 100644 lib/powerpc/asm/smp.h
 create mode 100644 lib/powerpc/smp.c
 create mode 100644 lib/ppc64/asm/smp.h
lib/powerpc/asm/smp.h new file +22 -0
new file mode 100644
index 0000000..21940b4
@@ -0,0 +1,22 @@
+#ifndef _ASMPOWERPC_SMP_H_
+#define _ASMPOWERPC_SMP_H_
+
+#include <libcflat.h>
+
+extern int nr_threads;
+
+struct start_threads {
+	int nr_threads;
+	int nr_started;
+};
+
+typedef void (*secondary_entry_fn)(void);
+
+extern void halt(void);
+
+extern int start_thread(int cpu_id, secondary_entry_fn entry, uint32_t r3);
+extern struct start_threads start_cpu(int cpu_node, secondary_entry_fn entry,
+				      uint32_t r3);
+extern bool start_all_cpus(secondary_entry_fn entry, uint32_t r3);
+
+#endif /* _ASMPOWERPC_SMP_H_ */
lib/powerpc/smp.c new file +102 -0

102 lines not on this page · raw message

lib/ppc64/asm/smp.h new file +1 -0
new file mode 100644
index 0000000..67ced75
@@ -0,0 +1 @@
+#include "../../powerpc/asm/smp.h"
powerpc/Makefile.common +1 -0
index 404194b..677030a 100644
@@ -37,6 +37,7 @@ cflatobjs += lib/powerpc/setup.o
 cflatobjs += lib/powerpc/rtas.o
 cflatobjs += lib/powerpc/processor.o
 cflatobjs += lib/powerpc/handlers.o
+cflatobjs += lib/powerpc/smp.o
 
 FLATLIBS = $(libcflat) $(LIBFDT_archive)
 %.elf: CFLAGS += $(arch_CFLAGS)
Signature · 1 line
2.5.5
#5 lore raw

PATCH v4 4/5 lib/powerpc: Implement generic delay function for use in unit tests

In reply to Suraj Jitindar Singh

It would be nice if we had a generic delay function which could be used in unit tests, add one.

Add the variable tb_hz used to store the time base frequency which is read from the device tree on setup.

Add functions mdelay, udelay and delay in processor.c to delay for a given number of milliseconds, microseconds and time base ticks respectively.

---

Change Log:

V2 -> V3:
- Add patch to series
V3 -> V4:
- Reword sleep->delay
- Move cpu_relax to asm-generic/barrier.h
- Add assert to catch when delay fns called with too large values

---
 lib/asm-generic/barrier.h   |  4 ++++
 lib/powerpc/asm/processor.h | 19 +++++++++++++++++++
 lib/powerpc/asm/setup.h     |  2 ++
 lib/powerpc/processor.c     | 20 ++++++++++++++++++++
 lib/powerpc/setup.c         |  7 +++++++
 5 files changed, 52 insertions(+)
lib/asm-generic/barrier.h +4 -0
index 12ae782..6a990ff 100644
@@ -28,4 +28,8 @@
 #define smp_wmb()	wmb()
 #endif
 
+#ifndef cpu_relax
+#define cpu_relax()	asm volatile ("":::"memory")
+#endif
+
 #endif /* _ASM_BARRIER_H_ */
lib/powerpc/asm/processor.h +19 -0
index 09692bd..ac001e1 100644
@@ -1,6 +1,7 @@
 #ifndef _ASMPOWERPC_PROCESSOR_H_
 #define _ASMPOWERPC_PROCESSOR_H_
 
+#include <libcflat.h>
 #include <asm/ptrace.h>
 
 #ifndef __ASSEMBLY__
@@ -8,4 +9,22 @@ void handle_exception(int trap, void (*func)(struct pt_regs *, void *), void *);
 void do_handle_exception(struct pt_regs *regs);
 #endif /* __ASSEMBLY__ */
 
+static inline uint64_t get_tb(void)
+{
+	uint64_t tb;
+
+	asm volatile ("mfspr %[tb],268" : [tb] "=r" (tb));
+
+	return tb;
+}
+
+extern void delay(uint64_t cycles);
+extern void udelay(uint64_t us);
+
+static inline void mdelay(uint64_t ms)
+{
+	while (ms--)
+		udelay(1000);
+}
+
 #endif /* _ASMPOWERPC_PROCESSOR_H_ */
lib/powerpc/asm/setup.h +2 -0
index b1e1e5a..23b4156 100644
@@ -11,6 +11,8 @@
 extern u32 cpus[NR_CPUS];
 extern int nr_cpus;
 
+extern uint64_t tb_hz;
+
 #define NR_MEM_REGIONS		8
 #define MR_F_PRIMARY		(1U << 0)
 struct mem_region {
lib/powerpc/processor.c +20 -0
index a78bc3c..a28f2f0 100644
@@ -5,6 +5,8 @@
 #include <libcflat.h>
 #include <asm/processor.h>
 #include <asm/ptrace.h>
+#include <asm/setup.h>
+#include <asm/barrier.h>
 
 static struct {
 	void (*func)(struct pt_regs *, void *data);
@@ -36,3 +38,21 @@ void do_handle_exception(struct pt_regs *regs)
 	printf("unhandled cpu exception 0x%lx\n", regs->trap);
 	abort();
 }
+
+void delay(uint64_t cycles)
+{
+	uint64_t start = get_tb();
+	/*
+	 * Pretty unlikely unless your server has been on for, or you want to
+	 * delay for, over 1000 years, but still.
+	 */
+	assert(cycles < (UINT64_MAX - start));
+	while ((get_tb() - start) < cycles)
+		cpu_relax();
+}
+
+void udelay(uint64_t us)
+{
+	assert(us < (UINT64_MAX / tb_hz));
+	delay((us * tb_hz) / 1000000);
+}
lib/powerpc/setup.c +7 -0
index e3d2afa..65bedf5 100644
@@ -24,6 +24,7 @@ extern void setup_args_progname(const char *args);
 
 u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) };
 int nr_cpus;
+uint64_t tb_hz;
 
 struct mem_region mem_regions[NR_MEM_REGIONS];
 phys_addr_t __physical_start, __physical_end;
@@ -72,6 +73,12 @@ static void cpu_set(int fdtnode, u32 regval, void *info)
 		data = (u32 *)prop->data;
 		params->dcache_bytes = fdt32_to_cpu(*data);
 
+		prop = fdt_get_property(dt_fdt(), fdtnode,
+					"timebase-frequency", NULL);
+		assert(prop != NULL);
+		data = (u32 *)prop->data;
+		tb_hz = fdt32_to_cpu(*data);
+
 		read_common_info = true;
 	}
 }
Signature · 1 line
2.5.5
#12 lore raw

PATCH v4 5/5 powerpc/tm: Add a test for H_CEDE while tm suspended

In reply to Suraj Jitindar Singh

On Power machines if a guest cedes while a tm transaction is in the suspended state then the checkpointed state of the vcpu may be lost and we lose the cpu in the host.

Add a file for tm tests "powerpc/tm.c" and add a test to check if the fix has been applied to the host kernel. If this fix hasn't been applied then the test will never complete and the cpu will be lost. Otherwise the test should succeed. Since this has the ability to mess things up in the host mark this test as don't run by default.

This vulnerability has been assigned the ID CVE-2016-5412.

Based on initial work done by: Cyril Bur <[email protected]>

---

Change Log:

V2 -> V3:
- Remove get_dec() and sleep() functions as sleep functionality is now provided by a generic implementation in processor.c.
- Replace TM instructions with raw machine code to support older compilers
V3 -> V4:
- Style Changes

---
 lib/powerpc/asm/hcall.h |   1 +
 powerpc/Makefile.common |   3 +-
 powerpc/tm.c            | 120 ++++++++++++++++++++++++++++++++++++++++++++++++
 powerpc/unittests.cfg   |   6 +++
 4 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 powerpc/tm.c
lib/powerpc/asm/hcall.h +1 -0
index 99bce79..80aa3e3 100644
@@ -18,6 +18,7 @@
 #define H_SET_SPRG0		0x24
 #define H_SET_DABR		0x28
 #define H_PAGE_INIT		0x2c
+#define H_CEDE			0xE0
 #define H_PUT_TERM_CHAR		0x58
 #define H_RANDOM		0x300
 #define H_SET_MODE		0x31C
powerpc/Makefile.common +2 -1
index 677030a..93e4f66 100644
@@ -8,7 +8,8 @@ tests-common = \
 	$(TEST_DIR)/selftest.elf \
 	$(TEST_DIR)/spapr_hcall.elf \
 	$(TEST_DIR)/rtas.elf \
-	$(TEST_DIR)/emulator.elf
+	$(TEST_DIR)/emulator.elf \
+	$(TEST_DIR)/tm.elf
 
 all: $(TEST_DIR)/boot_rom.bin test_cases
 
powerpc/tm.c new file +120 -0

120 lines not on this page · raw message

powerpc/unittests.cfg +6 -0
index 0098cb6..20dbde6 100644
@@ -53,3 +53,9 @@ groups = rtas
 
 [emulator]
 file = emulator.elf
+
+[h_cede_tm]
+file = tm.elf
+smp = 2,threads=2
+extra_params = -append "h_cede_tm"
+groups = nodefault,h_cede_tm
Signature · 1 line
2.5.5
Reviewed-by on v4 3/5
#3 lore raw

Re: [kvm-unit-tests PATCH V4 3/5] lib/powerpc: Add function to start secondary threads

In reply to Suraj Jitindar Singh
Quoted 93 lines · Suraj Jitindar Singh

Add the lib/powerpc/smp.c file and associated header files as a place to implement generic smp functionality for inclusion in tests.

Add functions start_all_cpus(), start_cpu() and start_thread() to start all stopped threads of all cpus, all stopped threads of a single cpu or a single stopped thread of a guest at a given execution location, respectively.

---

Change Log:

V2 -> V3:
- start_thread now returns int to reflect error, success or failure to start thread
- start_cpu returns number of threads on cpu and number successfully started
- start_all_cpus checks if number of threads started = total number of threads - 1
V3 -> V4:
- Style changes

---
 lib/powerpc/asm/smp.h   |  22 +++++++++++
 lib/powerpc/smp.c       | 102 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/ppc64/asm/smp.h     |   1 +
 powerpc/Makefile.common |   1 +
 4 files changed, 126 insertions(+)
 create mode 100644 lib/powerpc/asm/smp.h
 create mode 100644 lib/powerpc/smp.c
 create mode 100644 lib/ppc64/asm/smp.h
lib/powerpc/asm/smp.h new file +22 -0
new file mode 100644
index 0000000..21940b4
@@ -0,0 +1,22 @@
+#ifndef _ASMPOWERPC_SMP_H_
+#define _ASMPOWERPC_SMP_H_
+
+#include <libcflat.h>
+
+extern int nr_threads;
+
+struct start_threads {
+	int nr_threads;
+	int nr_started;
+};
+
+typedef void (*secondary_entry_fn)(void);
+
+extern void halt(void);
+
+extern int start_thread(int cpu_id, secondary_entry_fn entry, uint32_t r3);
+extern struct start_threads start_cpu(int cpu_node, secondary_entry_fn entry,
+				      uint32_t r3);
+extern bool start_all_cpus(secondary_entry_fn entry, uint32_t r3);
+
+#endif /* _ASMPOWERPC_SMP_H_ */
lib/powerpc/smp.c new file +25 -0
new file mode 100644
index 0000000..caa65b9
@@ -0,0 +1,102 @@
+/*
+ * Secondary cpu support
+ *
+ * Copyright 2016 Suraj Jitindar Singh, IBM.
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+
+#include <devicetree.h>
+#include <asm/setup.h>
+#include <asm/rtas.h>
+#include <asm/smp.h>
+
+int nr_threads;
+
+struct secondary_entry_data {
+	secondary_entry_fn entry;
+	uint64_t r3;
+	int nr_started;
+};
+
+/*
+ * Start stopped thread cpu_id at entry
+ * Returns:	<0 on failure to start stopped cpu
+ *		0  on success

lib/powerpc/smp.c line 35

Quoted 10 lines
lib/powerpc/smp.c new file +10 -0
+ *		>0 on cpu not in stopped state
+ */
+int start_thread(int cpu_id, secondary_entry_fn entry, uint32_t r3)
+{
+	int query_token, start_token, outputs[1], ret;
+
+	query_token = rtas_token("query-cpu-stopped-state");
+	start_token = rtas_token("start-cpu");
+	assert(query_token != RTAS_UNKNOWN_SERVICE &&
+			start_token != RTAS_UNKNOWN_SERVICE);

One TAB too much before start_token ?

lib/powerpc/smp.c line 38

Quoted 2 lines
lib/powerpc/smp.c new file +2 -0
+	ret = rtas_call(query_token, 1, 2, outputs, cpu_id);
+	if (ret) /* rtas query call failed */

The comment above is somewhat redundant - the printf statement below explains the code quite well already.
Also, according to the kernel coding style (which we use for kvm-unit-tests), I think you should use curly braces for both branches of a conditional statement if they are required at least for one of the branches (see chapter 3, right before chapter 3.1).

lib/powerpc/smp.c line 45

Quoted 7 lines
lib/powerpc/smp.c new file +7 -0
+		printf("query-cpu-stopped-state failed for cpu %d\n", cpu_id);
+	else if (!outputs[0]) { /* cpu in stopped state */
+		ret = rtas_call(start_token, 3, 1, NULL, cpu_id, entry, r3);
+		if (ret) /* rtas start-cpu call failed */
+			printf("failed to start cpu %d\n", cpu_id);
+	} else /* cpu not in stopped state */
+		ret = outputs[0];

That branch should also get some curly braces.

Quoted 19 lines
lib/powerpc/smp.c new file +19 -0
+
+	return ret;
+}
+
+/*
+ * Start all stopped threads (vcpus) on cpu_node
+ * Returns: Number of stopped cpus which were successfully started
+ */
+struct start_threads start_cpu(int cpu_node, secondary_entry_fn entry,
+			       uint32_t r3)
+{
+	int len, i, nr_threads, nr_started = 0;
+	const struct fdt_property *prop;
+	u32 *threads;
+
+	/* Get the id array of threads on this cpu_node */
+	prop = fdt_get_property(dt_fdt(), cpu_node,
+			"ibm,ppc-interrupt-server#s", &len);
+	assert(prop);

lib/powerpc/smp.c line 74

Quoted 10 lines
lib/powerpc/smp.c new file +10 -0
+
+	nr_threads = len >> 2; /* Divide by 4 since 4 bytes per thread */
+	threads = (u32 *)prop->data; /* Array of valid ids */
+
+	for (i = 0; i < nr_threads; i++) {
+		if (!start_thread(fdt32_to_cpu(threads[i]), entry, r3))
+			nr_started++;
+	}
+
+	return (struct start_threads) {nr_threads, nr_started};

Add maybe a space around each curly brace?

Quoted 40 lines
lib/powerpc/smp.c new file +28 -0
+}
+
+static void start_each_secondary(int fdtnode, u32 regval __unused, void *info)
+{
+	struct secondary_entry_data *datap = info;
+	struct start_threads ret = start_cpu(fdtnode, datap->entry, datap->r3);
+
+	nr_threads += ret.nr_threads;
+	datap->nr_started += ret.nr_started;
+}
+
+/*
+ * Start all stopped cpus on the guest at entry with register 3 set to r3
+ * We expect that we come in with only one thread currently started
+ * Returns:	TRUE on success
+ *		FALSE on failure
+ */
+bool start_all_cpus(secondary_entry_fn entry, uint32_t r3)
+{
+	struct secondary_entry_data data = { entry, r3,	0 };
+	int ret;
+
+	ret = dt_for_each_cpu_node(start_each_secondary, &data);
+	assert(ret = 0);
+
+	/* We expect that we come in with one thread already started */
+	return data.nr_started = nr_threads - 1;
+}
lib/ppc64/asm/smp.h new file +1 -0
new file mode 100644
index 0000000..67ced75
@@ -0,0 +1 @@
+#include "../../powerpc/asm/smp.h"
powerpc/Makefile.common +0 -0
index 404194b..677030a 100644

powerpc/Makefile.common cflatobjs += lib/powerpc/setup.o · line 43

Quoted 7 lines
powerpc/Makefile.common +1 -0
@@ -37,6 +37,7 @@ cflatobjs += lib/powerpc/setup.o
 cflatobjs += lib/powerpc/rtas.o
 cflatobjs += lib/powerpc/processor.o
 cflatobjs += lib/powerpc/handlers.o
+cflatobjs += lib/powerpc/smp.o
 
 FLATLIBS = $(libcflat) $(LIBFDT_archive)
 %.elf: CFLAGS += $(arch_CFLAGS)

Just some cosmetic nits ... it would be nice if you'd address them in case you respin, but I'm also fine with the patch in the current shape already, so:

Reviewed-by on v4 4/5
#6 lore raw

Re: [kvm-unit-tests PATCH V4 4/5] lib/powerpc: Implement generic delay function for use in unit tests

In reply to Suraj Jitindar Singh
Quoted 106 lines · Suraj Jitindar Singh

It would be nice if we had a generic delay function which could be used in unit tests, add one.

Add the variable tb_hz used to store the time base frequency which is read from the device tree on setup.

Add functions mdelay, udelay and delay in processor.c to delay for a given number of milliseconds, microseconds and time base ticks respectively.

---

Change Log:

V2 -> V3:
- Add patch to series
V3 -> V4:
- Reword sleep->delay
- Move cpu_relax to asm-generic/barrier.h
- Add assert to catch when delay fns called with too large values

---
 lib/asm-generic/barrier.h   |  4 ++++
 lib/powerpc/asm/processor.h | 19 +++++++++++++++++++
 lib/powerpc/asm/setup.h     |  2 ++
 lib/powerpc/processor.c     | 20 ++++++++++++++++++++
 lib/powerpc/setup.c         |  7 +++++++
 5 files changed, 52 insertions(+)
lib/asm-generic/barrier.h +4 -0
index 12ae782..6a990ff 100644
@@ -28,4 +28,8 @@
 #define smp_wmb()	wmb()
 #endif
 
+#ifndef cpu_relax
+#define cpu_relax()	asm volatile ("":::"memory")
+#endif
+
 #endif /* _ASM_BARRIER_H_ */
lib/powerpc/asm/processor.h +19 -0
index 09692bd..ac001e1 100644
@@ -1,6 +1,7 @@
 #ifndef _ASMPOWERPC_PROCESSOR_H_
 #define _ASMPOWERPC_PROCESSOR_H_
 
+#include <libcflat.h>
 #include <asm/ptrace.h>
 
 #ifndef __ASSEMBLY__
@@ -8,4 +9,22 @@ void handle_exception(int trap, void (*func)(struct pt_regs *, void *), void *);
 void do_handle_exception(struct pt_regs *regs);
 #endif /* __ASSEMBLY__ */
 
+static inline uint64_t get_tb(void)
+{
+	uint64_t tb;
+
+	asm volatile ("mfspr %[tb],268" : [tb] "=r" (tb));
+
+	return tb;
+}
+
+extern void delay(uint64_t cycles);
+extern void udelay(uint64_t us);
+
+static inline void mdelay(uint64_t ms)
+{
+	while (ms--)
+		udelay(1000);
+}
+
 #endif /* _ASMPOWERPC_PROCESSOR_H_ */
lib/powerpc/asm/setup.h +2 -0
index b1e1e5a..23b4156 100644
@@ -11,6 +11,8 @@
 extern u32 cpus[NR_CPUS];
 extern int nr_cpus;
 
+extern uint64_t tb_hz;
+
 #define NR_MEM_REGIONS		8
 #define MR_F_PRIMARY		(1U << 0)
 struct mem_region {
lib/powerpc/processor.c +2 -0
index a78bc3c..a28f2f0 100644
@@ -5,6 +5,8 @@
 #include <libcflat.h>
 #include <asm/processor.h>
 #include <asm/ptrace.h>
+#include <asm/setup.h>
+#include <asm/barrier.h>
 
 static struct {
 	void (*func)(struct pt_regs *, void *data);
@@ -36,3 +38,21 @@ void do_handle_exception(struct pt_regs *regs)
 	printf("unhandled cpu exception 0x%lx\n", regs->trap);
 	abort();
 }

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 50

Quoted 10 lines
lib/powerpc/processor.c +10 -0
+
+void delay(uint64_t cycles)
+{
+	uint64_t start = get_tb();
+	/*
+	 * Pretty unlikely unless your server has been on for, or you want to
+	 * delay for, over 1000 years, but still.
+	 */
+	assert(cycles < (UINT64_MAX - start));
+	while ((get_tb() - start) < cycles)

You could drop the parentheses around "get_tb() - start" ...

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 57

Quoted 7 lines
lib/powerpc/processor.c +7 -0
+		cpu_relax();
+}
+
+void udelay(uint64_t us)
+{
+	assert(us < (UINT64_MAX / tb_hz));
+	delay((us * tb_hz) / 1000000);

... and around "us * tb_hz".

Quoted 16 lines
lib/powerpc/processor.c +1 -0
+}
lib/powerpc/setup.c +1 -0
index e3d2afa..65bedf5 100644
@@ -24,6 +24,7 @@ extern void setup_args_progname(const char *args);
 
 u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) };
 int nr_cpus;
+uint64_t tb_hz;
 
 struct mem_region mem_regions[NR_MEM_REGIONS];
 phys_addr_t __physical_start, __physical_end;
@@ -72,6 +73,12 @@ static void cpu_set(int fdtnode, u32 regval, void *info)
 		data = (u32 *)prop->data;
 		params->dcache_bytes = fdt32_to_cpu(*data);

lib/powerpc/setup.c static void cpu_set(int fdtnode, u32 regval, void *info) · line 84

Quoted 10 lines
lib/powerpc/setup.c +6 -0
 
+		prop = fdt_get_property(dt_fdt(), fdtnode,
+					"timebase-frequency", NULL);
+		assert(prop != NULL);
+		data = (u32 *)prop->data;
+		tb_hz = fdt32_to_cpu(*data);
+
 		read_common_info = true;
 	}
 }

Two minor cosmetic nits, patch already looks also fine to me as it currently is, so:

Reviewed-by on v4 5/5
#13 lore raw

Re: [kvm-unit-tests PATCH V4 5/5] powerpc/tm: Add a test for H_CEDE while tm suspended

In reply to Suraj Jitindar Singh
Quoted 194 lines · Suraj Jitindar Singh

On Power machines if a guest cedes while a tm transaction is in the suspended state then the checkpointed state of the vcpu may be lost and we lose the cpu in the host.

Add a file for tm tests "powerpc/tm.c" and add a test to check if the fix has been applied to the host kernel. If this fix hasn't been applied then the test will never complete and the cpu will be lost. Otherwise the test should succeed. Since this has the ability to mess things up in the host mark this test as don't run by default.

This vulnerability has been assigned the ID CVE-2016-5412.

Based on initial work done by: Cyril Bur <[email protected]>

---

Change Log:

V2 -> V3:
- Remove get_dec() and sleep() functions as sleep functionality is now provided by a generic implementation in processor.c.
- Replace TM instructions with raw machine code to support older compilers
V3 -> V4:
- Style Changes

---
 lib/powerpc/asm/hcall.h |   1 +
 powerpc/Makefile.common |   3 +-
 powerpc/tm.c            | 120 ++++++++++++++++++++++++++++++++++++++++++++++++
 powerpc/unittests.cfg   |   6 +++
 4 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 powerpc/tm.c
lib/powerpc/asm/hcall.h +1 -0
index 99bce79..80aa3e3 100644
@@ -18,6 +18,7 @@
 #define H_SET_SPRG0		0x24
 #define H_SET_DABR		0x28
 #define H_PAGE_INIT		0x2c
+#define H_CEDE			0xE0
 #define H_PUT_TERM_CHAR		0x58
 #define H_RANDOM		0x300
 #define H_SET_MODE		0x31C
powerpc/Makefile.common +2 -1
index 677030a..93e4f66 100644
@@ -8,7 +8,8 @@ tests-common = \
 	$(TEST_DIR)/selftest.elf \
 	$(TEST_DIR)/spapr_hcall.elf \
 	$(TEST_DIR)/rtas.elf \
-	$(TEST_DIR)/emulator.elf
+	$(TEST_DIR)/emulator.elf \
+	$(TEST_DIR)/tm.elf
 
 all: $(TEST_DIR)/boot_rom.bin test_cases
 
powerpc/tm.c new file +120 -0
new file mode 100644
index 0000000..6ce750a
@@ -0,0 +1,120 @@
+/*
+ * Transactional Memory Unit Tests
+ *
+ * Copyright 2016 Suraj Jitindar Singh, IBM.
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.
+ */
+#include <libcflat.h>
+#include <asm/hcall.h>
+#include <asm/processor.h>
+#include <asm/handlers.h>
+#include <asm/smp.h>
+
+static int h_cede(void)
+{
+	register uint64_t r3 asm("r3") = H_CEDE;
+
+	asm volatile ("sc 1" : "+r"(r3) :
+			     : "r0", "r4", "r5", "r6", "r7", "r8", "r9",
+			       "r10", "r11", "r12", "xer", "ctr", "cc");
+
+	return r3;
+}
+
+/*
+ * Enable transactional memory
+ * Returns:	FALSE - Failure
+ *		TRUE - Success
+ */
+static bool enable_tm(void)
+{
+	uint64_t msr = 0;
+
+	asm volatile ("mfmsr %[msr]" : [msr] "=r" (msr));
+
+	msr |= (((uint64_t) 1) << 32);
+
+	asm volatile ("mtmsrd %[msr]\n\t"
+		      "mfmsr %[msr]" : [msr] "+r" (msr));
+
+	return !!(msr & (((uint64_t) 1) << 32));
+}
+
+/*
+ * Test H_CEDE call while transactional memory transaction is suspended
+ *
+ * WARNING: This tests for a known vulnerability in which the host may go down.
+ * Probably best not to run this if your host going down is going to cause
+ * problems.
+ *
+ * If the test passes then your kernel probably has the necessary patch.
+ * If the test fails then the H_CEDE call was unsuccessful and the
+ * vulnerability wasn't tested.
+ * If the test hits the vulnerability then it will never complete or report and
+ * the qemu process will block indefinitely. RCU stalls will be detected on the
+ * cpu and any process scheduled on the lost cpu will also block indefinitely.
+ */
+static void test_h_cede_tm(int argc, char **argv)
+{
+	int i;
+
+	if (argc > 2)
+		report_abort("Unsupported argument: '%s'", argv[2]);
+
+	handle_exception(0x900, &dec_except_handler, NULL);
+
+	if (!start_all_cpus(halt, 0))
+		report_abort("Failed to start secondary cpus");
+
+	if (!enable_tm())
+		report_abort("Failed to enable tm");
+
+	/*
+	 * Begin a transaction and guarantee we are in the suspend state
+	 * before continuing
+	 */
+	asm volatile ("1: .long 0x7c00051d\n\t"	/* tbegin. */
+		      "beq 2f\n\t"
+		      ".long 0x7c0005dd\n\t"	/* tsuspend. */
+		      "2: .long 0x7c00059c\n\t"	/* tcheck cr0 */
+		      "bf 2,1b" : : : "cr0");
+
+	for (i = 0; i < 500; i++) {
+		uint64_t rval = h_cede();
+
+		if (rval != H_SUCCESS)
+			break;
+		mdelay(5);
+	}
+
+	report("H_CEDE TM", i = 500);
+}
+
+struct {
+	const char *name;
+	void (*func)(int argc, char **argv);
+} hctests[] = {
+	{ "h_cede_tm", test_h_cede_tm },
+	{ NULL, NULL }
+};
+
+int main(int argc, char **argv)
+{
+	bool all;
+	int i;
+
+	report_prefix_push("tm");
+
+	all = argc = 1 || !strcmp(argv[1], "all");
+
+	for (i = 0; hctests[i].name != NULL; i++) {
+		if (all || strcmp(argv[1], hctests[i].name) = 0) {
+			report_prefix_push(hctests[i].name);
+			hctests[i].func(argc, argv);
+			report_prefix_pop();
+		}
+	}
+
+	return report_summary();
+}
powerpc/unittests.cfg +0 -0
index 0098cb6..20dbde6 100644

powerpc/unittests.cfg groups = rtas · line 61

Quoted 9 lines
powerpc/unittests.cfg +6 -0
@@ -53,3 +53,9 @@ groups = rtas
 
 [emulator]
 file = emulator.elf
+
+[h_cede_tm]
+file = tm.elf
+smp = 2,threads=2
+extra_params = -append "h_cede_tm"
+groups = nodefault,h_cede_tm
Reviewed-by on v4 1/5
#14 lore raw
In reply to Suraj Jitindar Singh
Quoted 32 lines · Suraj Jitindar Singh

Invoking run_tests.sh without the -g parameter will by default run all of the tests for a given architecture. This patch series will add a test which has the ability to bring down the host and thus it might be nice if we double check that the user actually wants to run that test instead of them unknowingly bringing down a machine they might not want to.

In order to do this add the option for a tests' group parameter in unittests.cfg to include "nodefault" on order to indicate that it shouldn't be run be default.

When tests are invoked via run_tests.sh those with the nodefault group parameter will be skipped unless explicitly specified by the "-g" command line option. When tests with the nodefault group parameter are built and run standalone the user will be prompted on invocation to confirm that they actually want to run the test.

This allows a developer to mark a test as having potentially adverse effects and thus requires an extra level of confirmation from the user before they are invoked. Existing functionality will be preserved and new tests can choose any group other than "nodefault" if they want to be run by default.

---

Change Log:

V2 -> V3:
- Move checking on standalone invokation into a function "skip_nodefault" in scripts/runtime.bash
V3 -> V4:
- Remove superfluous "STANDALONE=yes"

Quoted 6 lines
---
 arm/unittests.cfg     |  3 +++
 powerpc/unittests.cfg |  3 +++
 scripts/runtime.bash  | 27 ++++++++++++++++++++++++++-
 x86/unittests.cfg     |  3 +++
 4 files changed, 35 insertions(+), 1 deletion(-)
Reviewed-by on v4 4/5
#8 lore raw

Re: [kvm-unit-tests PATCH V4 4/5] lib/powerpc: Implement generic delay function for use in unit tests

In reply to Suraj Jitindar Singh
Quoted 106 lines · Suraj Jitindar Singh

It would be nice if we had a generic delay function which could be used in unit tests, add one.

Add the variable tb_hz used to store the time base frequency which is read from the device tree on setup.

Add functions mdelay, udelay and delay in processor.c to delay for a given number of milliseconds, microseconds and time base ticks respectively.

---

Change Log:

V2 -> V3:
- Add patch to series
V3 -> V4:
- Reword sleep->delay
- Move cpu_relax to asm-generic/barrier.h
- Add assert to catch when delay fns called with too large values

---
 lib/asm-generic/barrier.h   |  4 ++++
 lib/powerpc/asm/processor.h | 19 +++++++++++++++++++
 lib/powerpc/asm/setup.h     |  2 ++
 lib/powerpc/processor.c     | 20 ++++++++++++++++++++
 lib/powerpc/setup.c         |  7 +++++++
 5 files changed, 52 insertions(+)
lib/asm-generic/barrier.h +4 -0
index 12ae782..6a990ff 100644
@@ -28,4 +28,8 @@
 #define smp_wmb()	wmb()
 #endif
 
+#ifndef cpu_relax
+#define cpu_relax()	asm volatile ("":::"memory")
+#endif
+
 #endif /* _ASM_BARRIER_H_ */
lib/powerpc/asm/processor.h +19 -0
index 09692bd..ac001e1 100644
@@ -1,6 +1,7 @@
 #ifndef _ASMPOWERPC_PROCESSOR_H_
 #define _ASMPOWERPC_PROCESSOR_H_
 
+#include <libcflat.h>
 #include <asm/ptrace.h>
 
 #ifndef __ASSEMBLY__
@@ -8,4 +9,22 @@ void handle_exception(int trap, void (*func)(struct pt_regs *, void *), void *);
 void do_handle_exception(struct pt_regs *regs);
 #endif /* __ASSEMBLY__ */
 
+static inline uint64_t get_tb(void)
+{
+	uint64_t tb;
+
+	asm volatile ("mfspr %[tb],268" : [tb] "=r" (tb));
+
+	return tb;
+}
+
+extern void delay(uint64_t cycles);
+extern void udelay(uint64_t us);
+
+static inline void mdelay(uint64_t ms)
+{
+	while (ms--)
+		udelay(1000);
+}
+
 #endif /* _ASMPOWERPC_PROCESSOR_H_ */
lib/powerpc/asm/setup.h +2 -0
index b1e1e5a..23b4156 100644
@@ -11,6 +11,8 @@
 extern u32 cpus[NR_CPUS];
 extern int nr_cpus;
 
+extern uint64_t tb_hz;
+
 #define NR_MEM_REGIONS		8
 #define MR_F_PRIMARY		(1U << 0)
 struct mem_region {
lib/powerpc/processor.c +2 -0
index a78bc3c..a28f2f0 100644
@@ -5,6 +5,8 @@
 #include <libcflat.h>
 #include <asm/processor.h>
 #include <asm/ptrace.h>
+#include <asm/setup.h>
+#include <asm/barrier.h>
 
 static struct {
 	void (*func)(struct pt_regs *, void *data);
@@ -36,3 +38,21 @@ void do_handle_exception(struct pt_regs *regs)
 	printf("unhandled cpu exception 0x%lx\n", regs->trap);
 	abort();
 }

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 50

Quoted 10 lines
lib/powerpc/processor.c +10 -0
+
+void delay(uint64_t cycles)
+{
+	uint64_t start = get_tb();
+	/*
+	 * Pretty unlikely unless your server has been on for, or you want to
+	 * delay for, over 1000 years, but still.
+	 */
+	assert(cycles < (UINT64_MAX - start));
+	while ((get_tb() - start) < cycles)

I don't think the above assert is necessary. As long as the subtraction (get_tb() - start) produces a uint64_t, then the condition should always work - per C99. Maybe it should be written as (uint64_t)(get_tb() - start) to be 100% correct though.

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 56

Quoted 6 lines
lib/powerpc/processor.c +6 -0
+		cpu_relax();
+}
+
+void udelay(uint64_t us)
+{
+	assert(us < (UINT64_MAX / tb_hz));

Same comment here. I'm pretty sure (wrap around wraps my head, so I could be wrong) that the main concern is maintaining unsigned integer subtraction, which the C99 guarantees to wrap modulo 2^N, N being the number of bits of the unsigned integer.

Quoted 15 lines
lib/powerpc/processor.c +2 -0
+	delay((us * tb_hz) / 1000000);
+}
lib/powerpc/setup.c +1 -0
index e3d2afa..65bedf5 100644
@@ -24,6 +24,7 @@ extern void setup_args_progname(const char *args);
 
 u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) };
 int nr_cpus;
+uint64_t tb_hz;
 
 struct mem_region mem_regions[NR_MEM_REGIONS];
 phys_addr_t __physical_start, __physical_end;

lib/powerpc/setup.c static void cpu_set(int fdtnode, u32 regval, void *info) · line 80

Quoted 8 lines
lib/powerpc/setup.c +5 -0
@@ -72,6 +73,12 @@ static void cpu_set(int fdtnode, u32 regval, void *info)
 		data = (u32 *)prop->data;
 		params->dcache_bytes = fdt32_to_cpu(*data);
 
+		prop = fdt_get_property(dt_fdt(), fdtnode,
+					"timebase-frequency", NULL);
+		assert(prop != NULL);
+		data = (u32 *)prop->data;
+		tb_hz = fdt32_to_cpu(*data);

Arguably the dance I do with cpu_set_params to pass values back to cpu_init, where they simply get assigned to globals, is pointless. It's trying to maintain encapsulation (which I violate for nr_cpus anyway...) That said, I'd like to see tb_hz either integrate with the cpu_set_params pattern, or a cleanup patch come before this one, which removes cpu_set_params, allowing icache/dcache_bytes setting to match the tb_hz pattern. I won't hold this series up on that though.

Quoted 6 lines
lib/powerpc/setup.c +1 -0
+
 		read_common_info = true;
 	}
 }
Signature · 1 line
2.5.5

Neither of my comments are deal breakers, so

Thanks,
drew

Reviewed-by on v4 1/5
#15 lore raw
In reply to Suraj Jitindar Singh

2016-08-17 16:48+1000, Suraj Jitindar Singh:

Quoted 16 lines

Invoking run_tests.sh without the -g parameter will by default run all of the tests for a given architecture. This patch series will add a test which has the ability to bring down the host and thus it might be nice if we double check that the user actually wants to run that test instead of them unknowingly bringing down a machine they might not want to.

In order to do this add the option for a tests' group parameter in unittests.cfg to include "nodefault" on order to indicate that it shouldn't be run be default.

When tests are invoked via run_tests.sh those with the nodefault group parameter will be skipped unless explicitly specified by the "-g" command line option. When tests with the nodefault group parameter are built and run standalone the user will be prompted on invocation to confirm that they actually want to run the test.

Quoted 8 lines

This allows a developer to mark a test as having potentially adverse effects and thus requires an extra level of confirmation from the user before they are invoked. Existing functionality will be preserved and new tests can choose any group other than "nodefault" if they want to be run by default.

---

I have only nits, so

scripts/runtime.bash get_cmdline() · line 37

Quoted 8 lines
scripts/runtime.bash +6 -0
@@ -32,6 +32,25 @@ get_cmdline()
+skip_nodefault()
+{
+    [ "$STANDALONE" != "yes" ] && return 0
+
+    while true; do
+        read -p "Test marked not to be run by default, are you sure (Y/N)? " yn

"y/N" would help to understand the default with "".

scripts/runtime.bash get_cmdline() · line 49

Quoted 9 lines
scripts/runtime.bash +9 -0
+        case $yn in
+            "Y" | "y" | "Yes" | "yes")
+                return 1
+                ;;
+            "" | "N" | "n" | "No" | "no" | "q" | "quit" | "exit")
+                return 0
+                ;;
+            *)
+                ;;

The "*) ;;" case doesn't have to be there.

Quoted 4 lines
scripts/runtime.bash +4 -0
+        esac
+    done
+}
+
#4 lore raw

Re: [kvm-unit-tests PATCH V4 3/5] lib/powerpc: Add function to start secondary threads

In reply to Thomas Huth
Quoted 109 lines · Thomas Huth
Quoted 108 lines · Suraj Jitindar Singh

Add the lib/powerpc/smp.c file and associated header files as a place
to implement generic smp functionality for inclusion in tests.

Add functions start_all_cpus(), start_cpu() and start_thread() to start
all stopped threads of all cpus, all stopped threads of a single cpu or a
single stopped thread of a guest at a given execution location, respectively.

---

Change Log:

V2 -> V3:
- start_thread now returns int to reflect error, success or
failure to
  start thread
- start_cpu returns number of threads on cpu and number
successfully
  started
- start_all_cpus checks if number of threads started = total
number of
  threads - 1
V3 -> V4:
- Style changes
---
 lib/powerpc/asm/smp.h   |  22 +++++++++++ lib/powerpc/smp.c       | 102
++++++++++++++++++++++++++++++++++++++++++++++++
 lib/ppc64/asm/smp.h     |   1 +
 powerpc/Makefile.common |   1 +

 4 files changed, 126 insertions(+)

 create mode 100644 lib/powerpc/asm/smp.h
 create mode 100644 lib/powerpc/smp.c
 create mode 100644 lib/ppc64/asm/smp.h

lib/powerpc/asm/smp.h new file +17 -0
new file mode 100644
index 0000000..21940b4
@@ -0,0 +1,22 @@
+#ifndef _ASMPOWERPC_SMP_H_
+#define _ASMPOWERPC_SMP_H_
+
+#include <libcflat.h>
+
+extern int nr_threads;
+
+struct start_threads {
+	int nr_threads;
+	int nr_started;
+};
+
+typedef void (*secondary_entry_fn)(void);
+
+extern void halt(void);
+
+extern int start_thread(int cpu_id, secondary_entry_fn entry,

uint32_t r3);
+extern struct start_threads start_cpu(int cpu_node, secondary_entry_fn entry,

lib/powerpc/asm/smp.h new file +4 -0
+				      uint32_t r3);
+extern bool start_all_cpus(secondary_entry_fn entry, uint32_t r3);
+
+#endif /* _ASMPOWERPC_SMP_H_ */
lib/powerpc/smp.c new file +6 -0
new file mode 100644
index 0000000..caa65b9
@@ -0,0 +1,102 @@
+/*
+ * Secondary cpu support
+ *
+ * Copyright 2016 Suraj Jitindar Singh, IBM.
+ *
+ * This work is licensed under the terms of the GNU LGPL, version

2.
+ */

lib/powerpc/smp.c new file +21 -0
+
+#include <devicetree.h>
+#include <asm/setup.h>
+#include <asm/rtas.h>
+#include <asm/smp.h>
+
+int nr_threads;
+
+struct secondary_entry_data {
+	secondary_entry_fn entry;
+	uint64_t r3;
+	int nr_started;
+};
+
+/*
+ * Start stopped thread cpu_id at entry
+ * Returns:	<0 on failure to start stopped cpu
+ *		0  on success
+ *		>0 on cpu not in stopped state
+ */
+int start_thread(int cpu_id, secondary_entry_fn entry, uint32_t

r3)

Quoted 8 lines

lib/powerpc/smp.c line 35

Quoted 7 lines
lib/powerpc/smp.c new file +7 -0
+{
+	int query_token, start_token, outputs[1], ret;
+
+	query_token = rtas_token("query-cpu-stopped-state");
+	start_token = rtas_token("start-cpu");
+	assert(query_token != RTAS_UNKNOWN_SERVICE &&
+			start_token != RTAS_UNKNOWN_SERVICE);

One TAB too much before start_token ?

AFAIK the kernel coding style doesn't address how to indent a broken line. I personally prefer a double indent or aligning it to where the argument list starts on the line above if possible so when this is in a large block of code it's obvious that this is a broken line rather than the next level of code indentation.
Since this statement is followed by a blank line it's unlikely to lead to confusion - I'll go with the single indent.

Quoted 12 lines

lib/powerpc/smp.c line 38

Quoted 4 lines
lib/powerpc/smp.c new file +2 -0
+	ret = rtas_call(query_token, 1, 2, outputs, cpu_id);
+	if (ret) /* rtas query call failed */

The comment above is somewhat redundant - the printf statement below explains the code quite well already.
Also, according to the kernel coding style (which we use for kvm-unit-tests), I think you should use curly braces for both branches
of a conditional statement if they are required at least for one of the
branches (see chapter 3, right before chapter 3.1).

Yep

Quoted 13 lines

lib/powerpc/smp.c line 45

Quoted 12 lines
lib/powerpc/smp.c new file +1 -0
+		printf("query-cpu-stopped-state failed for cpu

%d\n", cpu_id);

lib/powerpc/smp.c new file +2 -0
+	else if (!outputs[0]) { /* cpu in stopped state */
+		ret = rtas_call(start_token, 3, 1, NULL, cpu_id,

entry, r3);

lib/powerpc/smp.c new file +2 -0
+		if (ret) /* rtas start-cpu call failed */
+			printf("failed to start cpu %d\n",

cpu_id);

lib/powerpc/smp.c new file +2 -0
+	} else /* cpu not in stopped state */
+		ret = outputs[0];

That branch should also get some curly braces.

Ditto

Quoted 27 lines
Quoted 27 lines
lib/powerpc/smp.c new file +9 -0
+
+	return ret;
+}
+
+/*
+ * Start all stopped threads (vcpus) on cpu_node
+ * Returns: Number of stopped cpus which were successfully started
+ */
+struct start_threads start_cpu(int cpu_node, secondary_entry_fn

entry,

lib/powerpc/smp.c new file +12 -0
+			       uint32_t r3)
+{
+	int len, i, nr_threads, nr_started = 0;
+	const struct fdt_property *prop;
+	u32 *threads;
+
+	/* Get the id array of threads on this cpu_node */
+	prop = fdt_get_property(dt_fdt(), cpu_node,
+			"ibm,ppc-interrupt-server#s", &len);
+	assert(prop);
+
+	nr_threads = len >> 2; /* Divide by 4 since 4 bytes per

thread */

lib/powerpc/smp.c new file +2 -0
+	threads = (u32 *)prop->data; /* Array of valid ids */
+
Quoted 8 lines

lib/powerpc/smp.c line 74

Quoted 7 lines
lib/powerpc/smp.c new file +2 -0
+	for (i = 0; i < nr_threads; i++) {
+		if (!start_thread(fdt32_to_cpu(threads[i]), entry,

r3))

lib/powerpc/smp.c new file +4 -0
+			nr_started++;
+	}
+
+	return (struct start_threads) {nr_threads, nr_started};

Add maybe a space around each curly brace?

Ok

Quoted 60 lines
Quoted 48 lines
lib/powerpc/smp.c new file +3 -0
+}
+
+static void start_each_secondary(int fdtnode, u32 regval __unused,

void *info)

lib/powerpc/smp.c new file +3 -0
+{
+	struct secondary_entry_data *datap = info;
+	struct start_threads ret = start_cpu(fdtnode, datap-
Quoted 1 line

entry, datap->r3);

lib/powerpc/smp.c new file +7 -0
+
+	nr_threads += ret.nr_threads;
+	datap->nr_started += ret.nr_started;
+}
+
+/*
+ * Start all stopped cpus on the guest at entry with register 3

set to r3
+ * We expect that we come in with only one thread currently started
+ * Returns: TRUE on success
+ * FALSE on failure
+ */

lib/powerpc/smp.c new file +3 -0
+bool start_all_cpus(secondary_entry_fn entry, uint32_t r3)
+{
+	struct secondary_entry_data data = { entry, r3,	0

};

lib/powerpc/smp.c new file +6 -0
+	int ret;
+
+	ret = dt_for_each_cpu_node(start_each_secondary, &data);
+	assert(ret = 0);
+
+	/* We expect that we come in with one thread already

started */

lib/powerpc/smp.c new file +2 -0
+	return data.nr_started = nr_threads - 1;
+}
lib/ppc64/asm/smp.h new file +1 -0
new file mode 100644
index 0000000..67ced75
@@ -0,0 +1 @@
+#include "../../powerpc/asm/smp.h"
powerpc/Makefile.common +0 -0
index 404194b..677030a 100644
@@ -37,6 +37,7 @@ cflatobjs += lib/powerpc/setup.o
Quoted 7 lines

 cflatobjs += lib/powerpc/rtas.o
 cflatobjs += lib/powerpc/processor.o
 cflatobjs += lib/powerpc/handlers.o
+cflatobjs += lib/powerpc/smp.o

 FLATLIBS = $(libcflat) $(LIBFDT_archive)
 %.elf: CFLAGS += $(arch_CFLAGS)

Just some cosmetic nits ... it would be nice if you'd address them in case you respin, but I'm also fine with the patch in the current shape
already, so:

Quoted 2 lines

Thanks

#9 lore raw

Re: [kvm-unit-tests PATCH V4 4/5] lib/powerpc: Implement generic delay function for use in unit tests

In reply to Andrew Jones
Quoted 125 lines · Andrew Jones
Quoted 124 lines · Suraj Jitindar Singh

It would be nice if we had a generic delay function which could be used
in unit tests, add one.

Add the variable tb_hz used to store the time base frequency which is read
from the device tree on setup.

Add functions mdelay, udelay and delay in processor.c to delay for a given
number of milliseconds, microseconds and time base ticks respectively.

---

Change Log:

V2 -> V3:
- Add patch to series
V3 -> V4:
- Reword sleep->delay
- Move cpu_relax to asm-generic/barrier.h
- Add assert to catch when delay fns called with too large
values
---
 lib/asm-generic/barrier.h   |  4 ++++ lib/powerpc/asm/processor.h | 19 +++++++++++++++++++ lib/powerpc/asm/setup.h     |  2 ++ lib/powerpc/processor.c     | 20 ++++++++++++++++++++ lib/powerpc/setup.c         |  7 +++++++

 5 files changed, 52 insertions(+)
lib/asm-generic/barrier.h +0 -0
index 12ae782..6a990ff 100644
@@ -28,4 +28,8 @@
  #define smp_wmb()	wmb()
  #endif

lib/asm-generic/barrier.h +4 -0
+#ifndef cpu_relax
+#define cpu_relax()	asm volatile ("":::"memory")
+#endif
+

 #endif /* _ASM_BARRIER_H_ */

lib/powerpc/asm/processor.h +0 -0
index 09692bd..ac001e1 100644
@@ -1,6 +1,7 @@
  #ifndef _ASMPOWERPC_PROCESSOR_H_
  #define _ASMPOWERPC_PROCESSOR_H_


+#include <libcflat.h>
 #include <asm/ptrace.h>

 #ifndef __ASSEMBLY__

lib/powerpc/asm/processor.h +0 -0
@@ -8,4 +9,22 @@ void handle_exception(int trap, void
 (*func)(struct pt_regs *, void *), void *);
  void do_handle_exception(struct pt_regs *regs);
  #endif /* __ASSEMBLY__ */

lib/powerpc/asm/processor.h +18 -0
+static inline uint64_t get_tb(void)
+{
+	uint64_t tb;
+
+	asm volatile ("mfspr %[tb],268" : [tb] "=r" (tb));
+
+	return tb;
+}
+
+extern void delay(uint64_t cycles);
+extern void udelay(uint64_t us);
+
+static inline void mdelay(uint64_t ms)
+{
+	while (ms--)
+		udelay(1000);
+}
+

 #endif /* _ASMPOWERPC_PROCESSOR_H_ */

lib/powerpc/asm/setup.h +0 -0
index b1e1e5a..23b4156 100644
@@ -11,6 +11,8 @@
  extern u32 cpus[NR_CPUS];
  extern int nr_cpus;

lib/powerpc/asm/setup.h +2 -0
+extern uint64_t tb_hz;
+

 #define NR_MEM_REGIONS 8
 #define MR_F_PRIMARY (1U << 0)
 struct mem_region {

lib/powerpc/processor.c +2 -0
index a78bc3c..a28f2f0 100644
@@ -5,6 +5,8 @@
  #include <libcflat.h>
  #include <asm/processor.h>
  #include <asm/ptrace.h>
+#include <asm/setup.h>
+#include <asm/barrier.h>


 static struct {
  void (*func)(struct pt_regs *, void *data);

lib/powerpc/processor.c +6 -0
@@ -36,3 +38,21 @@ void do_handle_exception(struct pt_regs *regs)
  	printf("unhandled cpu exception 0x%lx\n", regs->trap);
  	abort();
  }
+
+void delay(uint64_t cycles)
+{
+	uint64_t start = get_tb();
+	/*
+	 * Pretty unlikely unless your server has been on for, or

you want to

lib/powerpc/processor.c +3 -0
+	 * delay for, over 1000 years, but still.
+	 */
+	assert(cycles < (UINT64_MAX - start));
Quoted 8 lines

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 50

Quoted 1 line
lib/powerpc/processor.c +1 -0
+	while ((get_tb() - start) < cycles)

I don't think the above assert is necessary. As long as the subtraction
(get_tb() - start) produces a uint64_t, then the condition should always
work - per C99. Maybe it should be written as (uint64_t)(get_tb() - start)
to be 100% correct though.

This is to catch the case where the caller passes a ridiculously large cycles value (e.g. UINT64_MAX - 1) and/or start is sufficiently large that get_tb() - start will never be >= to cycles because the time-base counter will overflow and wrap around before that ever becomes true. This is super unlikely but will avoid an infinite loop in the event someone does it.

Quoted 12 lines

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 56

Quoted 8 lines
lib/powerpc/processor.c +6 -0
+		cpu_relax();
+}
+
+void udelay(uint64_t us)
+{
+	assert(us < (UINT64_MAX / tb_hz));

Same comment here. I'm pretty sure (wrap around wraps my head, so I could be wrong) that the main concern is maintaining unsigned integer subtraction, which the C99 guarantees to wrap modulo 2^N, N being the number of bits of the unsigned integer.

This is to catch when the caller tries to sleep for > 36000000000us (10 hrs), which I realise is highly unlikely. But in this case us * tb_hz will be too big to store in a u64. Thus this won't delay for the intended time, hence the assert.

Quoted 31 lines
Quoted 25 lines
lib/powerpc/processor.c +2 -0
+	delay((us * tb_hz) / 1000000);
+}
lib/powerpc/setup.c +0 -0
index e3d2afa..65bedf5 100644
@@ -24,6 +24,7 @@ extern void setup_args_progname(const char
 *args);


 u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) }; int nr_cpus;
+uint64_t tb_hz;

 struct mem_region mem_regions[NR_MEM_REGIONS]; phys_addr_t __physical_start, __physical_end;

lib/powerpc/setup.c +0 -0
@@ -72,6 +73,12 @@ static void cpu_set(int fdtnode, u32 regval,
 void *info)
  		data = (u32 *)prop->data;
  		params->dcache_bytes = fdt32_to_cpu(*data);

lib/powerpc/setup.c +2 -0
+		prop = fdt_get_property(dt_fdt(), fdtnode,
+					"timebase-frequency",

NULL);

lib/powerpc/setup.c static void cpu_set(int fdtnode, u32 regval, void *info) · line 80

Quoted 3 lines
lib/powerpc/setup.c +3 -0
+		assert(prop != NULL);
+		data = (u32 *)prop->data;
+		tb_hz = fdt32_to_cpu(*data);

Arguably the dance I do with cpu_set_params to pass values back to cpu_init, where they simply get assigned to globals, is pointless. It's

Quoted 8 lines

trying to maintain encapsulation (which I violate for nr_cpus anyway...)
That said, I'd like to see tb_hz either integrate with the cpu_set_params
pattern, or a cleanup patch come before this one, which removes cpu_set_params, allowing icache/dcache_bytes setting to match the tb_hz
pattern. I won't hold this series up on that though.

Yeah I see whats happening here, I'll make it match what is done for i/dcache.

Quoted 6 lines
Quoted 6 lines

+
  read_common_info = true;
  }
 }

Thanks

#7 lore raw

Re: [kvm-unit-tests PATCH V4 4/5] lib/powerpc: Implement generic delay function for use in unit tests

In reply to Thomas Huth
Quoted 162 lines · Thomas Huth
Quoted 121 lines · Suraj Jitindar Singh

It would be nice if we had a generic delay function which could be used
in unit tests, add one.

Add the variable tb_hz used to store the time base frequency which is read
from the device tree on setup.

Add functions mdelay, udelay and delay in processor.c to delay for a given
number of milliseconds, microseconds and time base ticks respectively.

---

Change Log:

V2 -> V3:
- Add patch to series
V3 -> V4:
- Reword sleep->delay
- Move cpu_relax to asm-generic/barrier.h
- Add assert to catch when delay fns called with too large
values
---
 lib/asm-generic/barrier.h   |  4 ++++ lib/powerpc/asm/processor.h | 19 +++++++++++++++++++ lib/powerpc/asm/setup.h     |  2 ++ lib/powerpc/processor.c     | 20 ++++++++++++++++++++ lib/powerpc/setup.c         |  7 +++++++

 5 files changed, 52 insertions(+)
lib/asm-generic/barrier.h +0 -0
index 12ae782..6a990ff 100644
@@ -28,4 +28,8 @@
  #define smp_wmb()	wmb()
  #endif

lib/asm-generic/barrier.h +4 -0
+#ifndef cpu_relax
+#define cpu_relax()	asm volatile ("":::"memory")
+#endif
+

 #endif /* _ASM_BARRIER_H_ */

lib/powerpc/asm/processor.h +0 -0
index 09692bd..ac001e1 100644
@@ -1,6 +1,7 @@
  #ifndef _ASMPOWERPC_PROCESSOR_H_
  #define _ASMPOWERPC_PROCESSOR_H_


+#include <libcflat.h>
 #include <asm/ptrace.h>

 #ifndef __ASSEMBLY__

lib/powerpc/asm/processor.h +0 -0
@@ -8,4 +9,22 @@ void handle_exception(int trap, void
 (*func)(struct pt_regs *, void *), void *);
  void do_handle_exception(struct pt_regs *regs);
  #endif /* __ASSEMBLY__ */

lib/powerpc/asm/processor.h +18 -0
+static inline uint64_t get_tb(void)
+{
+	uint64_t tb;
+
+	asm volatile ("mfspr %[tb],268" : [tb] "=r" (tb));
+
+	return tb;
+}
+
+extern void delay(uint64_t cycles);
+extern void udelay(uint64_t us);
+
+static inline void mdelay(uint64_t ms)
+{
+	while (ms--)
+		udelay(1000);
+}
+

 #endif /* _ASMPOWERPC_PROCESSOR_H_ */

lib/powerpc/asm/setup.h +0 -0
index b1e1e5a..23b4156 100644
@@ -11,6 +11,8 @@
  extern u32 cpus[NR_CPUS];
  extern int nr_cpus;

lib/powerpc/asm/setup.h +2 -0
+extern uint64_t tb_hz;
+

 #define NR_MEM_REGIONS 8
 #define MR_F_PRIMARY (1U << 0)
 struct mem_region {

lib/powerpc/processor.c +2 -0
index a78bc3c..a28f2f0 100644
@@ -5,6 +5,8 @@
  #include <libcflat.h>
  #include <asm/processor.h>
  #include <asm/ptrace.h>
+#include <asm/setup.h>
+#include <asm/barrier.h>


 static struct {
  void (*func)(struct pt_regs *, void *data);

lib/powerpc/processor.c +6 -0
@@ -36,3 +38,21 @@ void do_handle_exception(struct pt_regs *regs)
  	printf("unhandled cpu exception 0x%lx\n", regs->trap);
  	abort();
  }
+
+void delay(uint64_t cycles)
+{
+	uint64_t start = get_tb();
+	/*
+	 * Pretty unlikely unless your server has been on for, or

you want to

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 50

Quoted 4 lines
lib/powerpc/processor.c +4 -0
+	 * delay for, over 1000 years, but still.
+	 */
+	assert(cycles < (UINT64_MAX - start));
+	while ((get_tb() - start) < cycles)

You could drop the parentheses around "get_tb() - start" ...

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 57

Quoted 8 lines
lib/powerpc/processor.c +7 -0
+		cpu_relax();
+}
+
+void udelay(uint64_t us)
+{
+	assert(us < (UINT64_MAX / tb_hz));
+	delay((us * tb_hz) / 1000000);

... and around "us * tb_hz".

Quoted 24 lines
lib/powerpc/processor.c +1 -0
+}
lib/powerpc/setup.c +0 -0
index e3d2afa..65bedf5 100644
@@ -24,6 +24,7 @@ extern void setup_args_progname(const char
 *args);


 u32 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (~0U) }; int nr_cpus;
+uint64_t tb_hz;

 struct mem_region mem_regions[NR_MEM_REGIONS]; phys_addr_t __physical_start, __physical_end;

lib/powerpc/setup.c +0 -0
@@ -72,6 +73,12 @@ static void cpu_set(int fdtnode, u32 regval,
 void *info)
  		data = (u32 *)prop->data;
  		params->dcache_bytes = fdt32_to_cpu(*data);

lib/powerpc/setup.c +2 -0
+		prop = fdt_get_property(dt_fdt(), fdtnode,
+					"timebase-frequency",

NULL);

lib/powerpc/setup.c +1 -0
+		assert(prop != NULL);
Quoted 8 lines
Quoted 6 lines
lib/powerpc/setup.c +3 -0
+		data = (u32 *)prop->data;
+		tb_hz = fdt32_to_cpu(*data);
+

  read_common_info = true;
  }
 }

Two minor cosmetic nits, patch already looks also fine to me as it currently is, so:

I'm a bit of a bracket over user, but I like them in these cases if only to prove that what I think is happening actually is when I can't remember c operator precedence.

Quoted 3 lines

Thanks

#16 lore raw
In reply to Radim Krčmář
Quoted 37 lines · Radim Krčmář

2016-08-17 16:48+1000, Suraj Jitindar Singh:

Quoted 36 lines

Invoking run_tests.sh without the -g parameter will by default run all of
the tests for a given architecture. This patch series will add a test which
has the ability to bring down the host and thus it might be nice if we
double check that the user actually wants to run that test instead of
them unknowingly bringing down a machine they might not want to.

In order to do this add the option for a tests' group parameter in unittests.cfg to include "nodefault" on order to indicate that it shouldn't
be run be default.

When tests are invoked via run_tests.sh those with the nodefault group
parameter will be skipped unless explicitly specified by the "-g" command
line option. When tests with the nodefault group parameter are built and
run standalone the user will be prompted on invocation to confirm that
they actually want to run the test.

This allows a developer to mark a test as having potentially adverse
effects and thus requires an extra level of confirmation from the user
before they are invoked. Existing functionality will be preserved and new
tests can choose any group other than "nodefault" if they want to be run
by default.

Quoted 3 lines
Quoted 2 lines

---

I have only nits, so

Thanks, I'll respin and address these

Quoted 32 lines
Quoted 10 lines
scripts/runtime.bash +6 -0
@@ -32,6 +32,25 @@ get_cmdline()
+skip_nodefault()
+{
+    [ "$STANDALONE" != "yes" ] && return 0
+
+    while true; do
+        read -p "Test marked not to be run by default, are you

sure (Y/N)? " yn

"y/N" would help to understand the default with "".

Quoted 10 lines

+        case $yn in
+            "Y" | "y" | "Yes" | "yes")
+                return 1
+                ;;
+            "" | "N" | "n" | "No" | "no" | "q" | "quit" | "exit") +                return 0
+                ;;
+            *)
+                ;;

The "*) ;;" case doesn't have to be there.

Quoted 5 lines

+        esac
+    done

scripts/runtime.bash +2 -0
+}
+
#10 lore raw

Re: [kvm-unit-tests PATCH V4 4/5] lib/powerpc: Implement generic delay function for use in unit tests

In reply to Suraj Jitindar Singh
Quoted 20 lines · Suraj Jitindar Singh
Quoted 11 lines · Andrew Jones
Quoted 10 lines · Suraj Jitindar Singh
lib/powerpc/processor.c +6 -0
+
+void delay(uint64_t cycles)
+{
+	uint64_t start = get_tb();
+	/*
+	 * Pretty unlikely unless your server has been on for, or

you want to

lib/powerpc/processor.c +3 -0
+	 * delay for, over 1000 years, but still.
+	 */
+	assert(cycles < (UINT64_MAX - start));
Quoted 8 lines

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 50

Quoted 1 line
lib/powerpc/processor.c +1 -0
+	while ((get_tb() - start) < cycles)

I don't think the above assert is necessary. As long as the subtraction
(get_tb() - start) produces a uint64_t, then the condition should always
work - per C99. Maybe it should be written as (uint64_t)(get_tb() - start)
to be 100% correct though.

Quoted 6 lines

This is to catch the case where the caller passes a ridiculously large cycles value (e.g. UINT64_MAX - 1) and/or start is sufficiently large that get_tb() - start will never be >= to cycles because the time-base counter will overflow and wrap around before that ever becomes true. This is super unlikely but will avoid an infinite loop in the event someone does it.

I understand that. What I'm saying is that with unsigned integer subtraction, per C99, you don't have to worry about it. Just try

#include <stdio.h>
#include <stdint.h>
int main(void)

{
	uint64_t start = UINT64_MAX - 1;
	uint64_t tb = 5; // tb wrapped
	uint64_t cycles = 10;

	if ((tb - start) < cycles)
		printf("works fine\n");
	return 0;
}

to see that it works fine. As for guarding against ridiculously large cycles inputs, don't bother. How do you even define what's ridiculous? I'd say it's anything more than a minute...

Quoted 12 lines
Quoted 12 lines

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 56

Quoted 8 lines
lib/powerpc/processor.c +6 -0
+		cpu_relax();
+}
+
+void udelay(uint64_t us)
+{
+	assert(us < (UINT64_MAX / tb_hz));

Same comment here. I'm pretty sure (wrap around wraps my head, so I could be wrong) that the main concern is maintaining unsigned integer subtraction, which the C99 guarantees to wrap modulo 2^N, N being the number of bits of the unsigned integer.

Quoted 4 lines

This is to catch when the caller tries to sleep for > 36000000000us (10 hrs), which I realise is highly unlikely. But in this case us * tb_hz will be too big to store in a u64. Thus this won't delay for the intended time, hence the assert.

If the caller does that, the we're doing him a favor by wrapping the input... More seriously, while I'm in favor of asserts for external inputs (DT reads, command line inputs), I think it's safe to expect unit test developers to just not do something like this, or to be able to debug it themselves when they do, without the aid of asserts pinpointing the mistake for them.

Thanks,
drew

#11 lore raw

Re: [kvm-unit-tests PATCH V4 4/5] lib/powerpc: Implement generic delay function for use in unit tests

Quoted 53 lines · Andrew Jones
Quoted 26 lines · Suraj Jitindar Singh
Quoted 16 lines · Andrew Jones

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 50

Quoted 13 lines · Suraj Jitindar Singh
lib/powerpc/processor.c +6 -0
+
+void delay(uint64_t cycles)
+{
+	uint64_t start = get_tb();
+	/*
+	 * Pretty unlikely unless your server has been on for,

or
you want to

lib/powerpc/processor.c +4 -0
+	 * delay for, over 1000 years, but still.
+	 */
+	assert(cycles < (UINT64_MAX - start));
+	while ((get_tb() - start) < cycles)
Quoted 8 lines

I don't think the above assert is necessary. As long as the subtraction
(get_tb() - start) produces a uint64_t, then the condition should always
work - per C99. Maybe it should be written as (uint64_t)(get_tb() -
start)
to be 100% correct though.

Quoted 10 lines

This is to catch the case where the caller passes a ridiculously large
cycles value (e.g. UINT64_MAX - 1) and/or start is sufficiently large
that get_tb() - start will never be >= to cycles because the time- base
counter will overflow and wrap around before that ever becomes true.
This is super unlikely but will avoid an infinite loop in the event someone does it.

I understand that. What I'm saying is that with unsigned integer subtraction, per C99, you don't have to worry about it. Just try

#include <stdio.h>
#include <stdint.h>
int main(void)

{
	uint64_t start = UINT64_MAX - 1;
	uint64_t tb = 5; // tb wrapped
	uint64_t cycles = 10;

	if ((tb - start) < cycles)
		printf("works fine\n");
	return 0;
}
Quoted 4 lines

to see that it works fine. As for guarding against ridiculously large cycles inputs, don't bother. How do you even define what's ridiculous?
I'd say it's anything more than a minute...

Ok I understand what you're saying now. I'll bin the assert. Thanks.

Quoted 25 lines
Quoted 19 lines
Quoted 6 lines
Quoted 6 lines
Quoted 13 lines

lib/powerpc/processor.c void do_handle_exception(struct pt_regs *regs) · line 56

Quoted 6 lines
lib/powerpc/processor.c +6 -0
+		cpu_relax();
+}
+
+void udelay(uint64_t us)
+{
+	assert(us < (UINT64_MAX / tb_hz));

Same comment here. I'm pretty sure (wrap around wraps my head, so I
could be wrong) that the main concern is maintaining unsigned integer
subtraction, which the C99 guarantees to wrap modulo 2^N, N being the
number of bits of the unsigned integer.

Quoted 6 lines

This is to catch when the caller tries to sleep for > 36000000000us (10
hrs), which I realise is highly unlikely. But in this case us * tb_hz
will be too big to store in a u64. Thus this won't delay for the intended time, hence the assert.

Quoted 6 lines

If the caller does that, the we're doing him a favor by wrapping the input... More seriously, while I'm in favor of asserts for external inputs (DT reads, command line inputs), I think it's safe to expect unit test developers to just not do something like this, or to be able to debug it themselves when they do, without the aid of asserts pinpointing the mistake for them.

Makes sense, I'll ditch this assert as well.

Quoted 3 lines

Thanks,
drew

Thanks