Skip to contentSkip to conversation

KVM: selftests: Clang fixes, Makefile cleanup

PATCH In review Sean Christopherson 5 participants 29 messages Dec 13, 2022 – Jan 11, 2023

Subsystems (4)
lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

cover KVM: selftests: Clang fixes, Makefile cleanup

Fix a variety of KVM selftests issues exposed by clang, and rework the Makefile and .gitignore to reduce the maintenance burden of selftests.

For the Makefile, programmatically generate the list of targets by looking for .c files, and opt-out via a dummy macro in the source instead of forcing architectures to opt-in. The opt-out approach is less error prone (harder to forget to add an arch), doesn't generate unnecessary conflicts if multiple tests are added simultanesouly, and makes it much easier to understand which tests aren't supported, e.g.

  $ git grep TEST_UNSUPPORTED | grep aarch64
  hardware_disable_test.c:TEST_UNSUPPORTED(aarch64);
  max_guest_memory_test.c:TEST_UNSUPPORTED(aarch64);
  system_counter_offset_test.c:TEST_UNSUPPORTED(aarch64);

This all started when trying to reproduce clang build errors reported by Raghu and Aaron that were introduced by commit 6b6f71484bf4 ("KVM:
selftests: Implement memcmp(), memcpy(), and memset() for guest use"). Just getting selftests to compile with clang was a nightmare, as it took me several hours to realize that "CC=clang make" and "make CC=clang" aren't equivalent, and that the "include ../lib.mak" buried halfway through the Makefile was overriding "CC=clang make".

After too many hours fighting to get clang working, my frustration with the Makefile boiled over a bit...

Note, I have fixes for the RISC-V RSEQ bugs (outside of selftests/kvm) that I'll post separately.

Tested on x86 and arm, build tested on s390x and RISC-V, all with both gcc and clang.

Sean Christopherson (14):
KVM: selftests: Define literal to asm constraint in aarch64 as
unsigned long
KVM: selftests: Delete dead code in x86_64/vmx_tsc_adjust_test.c
KVM: selftests: Fix divide-by-zero bug in memslot_perf_test
KVM: selftests: Use pattern matching in .gitignore
KVM: selftests: Fix a typo in x86-64's kvm_get_cpu_address_width()
KVM: selftests: Rename UNAME_M to ARCH_DIR, fill explicitly for x86
KVM: selftests: Use proper function prototypes in probing code
KVM: selftests: Probe -no-pie with actual CFLAGS used to compile
KVM: selftests: Explicitly disable builtins for mem*() overrides
KVM: selftests: Include lib.mk before consuming $(CC)
KVM: selftests: Disable "gnu-variable-sized-type-not-at-end" warning
KVM: selftests: Use wildcards to find library source files
KVM: selftests: Use wildcards to find targets and test source files
KVM: selftests: Enable RSEQ test for RISC-V

 tools/testing/selftests/kvm/.gitignore        |  91 +------
 tools/testing/selftests/kvm/Makefile          | 229 +++---------------
 .../selftests/kvm/aarch64/page_fault_test.c   |   2 +-
 .../selftests/kvm/access_tracking_perf_test.c |   3 +
 .../selftests/kvm/dirty_log_perf_test.c       |   3 +
 .../selftests/kvm/hardware_disable_test.c     |   4 +
 .../testing/selftests/kvm/include/test_util.h |  11 +
 .../selftests/kvm/lib/x86_64/processor.c      |   2 +-
 .../selftests/kvm/max_guest_memory_test.c     |   4 +
 .../kvm/memslot_modification_stress_test.c    |   3 +
 .../testing/selftests/kvm/memslot_perf_test.c |   6 +
 tools/testing/selftests/kvm/steal_time.c      |   3 +
 .../kvm/system_counter_offset_test.c          |   4 +
 .../kvm/x86_64/vmx_tsc_adjust_test.c          |   5 -
 14 files changed, 80 insertions(+), 290 deletions(-)

base-commit: f1a1d3aff0cc2e68a9ebbd8810d7dcd8cfe2714b

Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
author reviewer
#1 lore raw

PATCH 1/14 KVM: selftests: Define literal to asm constraint in aarch64 as unsigned long

Define a literal '0' asm input constraint to aarch64/page_fault_test's guest_cas() as an unsigned long to make clang happy.

tools/testing/selftests/kvm/aarch64/page_fault_test.c:120:16: error:
value size does not match register size specified by the constraint and modifier [-Werror,-Wasm-operand-widths]
:: "r" (0), "r" (TEST_DATA), "r" (guest_test_memory));
^
tools/testing/selftests/kvm/aarch64/page_fault_test.c:119:15: note:

    use constraint modifier "w"
                       "casal %0, %1, [%2]\n"
                              ^~
                              %w0
Fixes 35c581015712 ("KVM: selftests: aarch64: Add aarch64/page_fault_test")
Cc Ricardo Koller <[email protected]>
---
 tools/testing/selftests/kvm/aarch64/page_fault_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/aarch64/page_fault_test.c +1 -1
index 95d22cfb7b41..beb944fa6fd4 100644
@@ -117,7 +117,7 @@ static void guest_cas(void)
 	GUEST_ASSERT(guest_check_lse());
 	asm volatile(".arch_extension lse\n"
 		     "casal %0, %1, [%2]\n"
-		     :: "r" (0), "r" (TEST_DATA), "r" (guest_test_memory));
+		     :: "r" (0ul), "r" (TEST_DATA), "r" (guest_test_memory));
 	val = READ_ONCE(*guest_test_memory);
 	GUEST_ASSERT_EQ(val, TEST_DATA);
 }
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
Reviewed-by on v1 1/14
#2 lore raw

Re: [PATCH 01/14] KVM: selftests: Define literal to asm constraint in aarch64 as unsigned long

Quoted 14 lines · Sean Christopherson

Define a literal '0' asm input constraint to aarch64/page_fault_test's guest_cas() as an unsigned long to make clang happy.

   tools/testing/selftests/kvm/aarch64/page_fault_test.c:120:16: error:
     value size does not match register size specified by the constraint
     and modifier [-Werror,-Wasm-operand-widths]
                        :: "r" (0), "r" (TEST_DATA), "r" (guest_test_memory));
                                ^
   tools/testing/selftests/kvm/aarch64/page_fault_test.c:119:15: note:
     use constraint modifier "w"
                        "casal %0, %1, [%2]\n"
                               ^~
                               %w0
Quoted 6 lines
Fixes 35c581015712 ("KVM: selftests: aarch64: Add aarch64/page_fault_test")
Cc Ricardo Koller <[email protected]>
---
  tools/testing/selftests/kvm/aarch64/page_fault_test.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
author reviewer
#3 lore raw

PATCH 2/14 KVM: selftests: Delete dead code in x86_64/vmx_tsc_adjust_test.c

In reply to Sean Christopherson

Delete an unused struct definition in x86_64/vmx_tsc_adjust_test.c.

---
 tools/testing/selftests/kvm/x86_64/vmx_tsc_adjust_test.c | 5 -----
 1 file changed, 5 deletions(-)
tools/testing/selftests/kvm/x86_64/vmx_tsc_adjust_test.c +0 -5
index 5943187e8594..ff8ecdf32ae0 100644
@@ -49,11 +49,6 @@ enum {
 	NUM_VMX_PAGES,
 };
 
-struct kvm_single_msr {
-	struct kvm_msrs header;
-	struct kvm_msr_entry entry;
-} __attribute__((packed));
-
 /* The virtual machine object. */
 static struct kvm_vm *vm;
 
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
author reviewer
#4 lore raw

PATCH 3/14 KVM: selftests: Fix divide-by-zero bug in memslot_perf_test

In reply to Sean Christopherson

Check that the number of pages per slot is non-zero in get_max_slots() prior to computing the remaining number of pages. clang generates code that uses an actual DIV for calculating the remaining, which causes a #DE if the total number of pages is less than the number of slots.

traps: memslot_perf_te[97611] trap divide error ip:4030c4 sp:7ffd18ae58f0

         error:0 in memslot_perf_test[401000+cb000]
Fixes a69170c65acd ("KVM: selftests: memslot_perf_test: Report optimal memory slots")
---
 tools/testing/selftests/kvm/memslot_perf_test.c | 3 +++
 1 file changed, 3 insertions(+)
tools/testing/selftests/kvm/memslot_perf_test.c +3 -0
index e698306bf49d..e6587e193490 100644
@@ -265,6 +265,9 @@ static uint64_t get_max_slots(struct vm_data *data, uint32_t host_page_size)
 	slots = data->nslots;
 	while (--slots > 1) {
 		pages_per_slot = mempages / slots;
+		if (!pages_per_slot)
+			continue;
+
 		rempages = mempages % pages_per_slot;
 		if (check_slot_pages(host_page_size, guest_page_size,
 				     pages_per_slot, rempages))
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
Reviewed-by on v1 3/14
#5 lore raw

Re: [PATCH 03/14] KVM: selftests: Fix divide-by-zero bug in memslot_perf_test

Quoted 5 lines · Sean Christopherson

Check that the number of pages per slot is non-zero in get_max_slots() prior to computing the remaining number of pages. clang generates code that uses an actual DIV for calculating the remaining, which causes a #DE if the total number of pages is less than the number of slots.

Quoted 8 lines
   traps: memslot_perf_te[97611] trap divide error ip:4030c4 sp:7ffd18ae58f0
          error:0 in memslot_perf_test[401000+cb000]
Fixes a69170c65acd ("KVM: selftests: memslot_perf_test: Report optimal memory slots")
---
  tools/testing/selftests/kvm/memslot_perf_test.c | 3 +++
  1 file changed, 3 insertions(+)
author reviewer
#6 lore raw

PATCH 4/14 KVM: selftests: Use pattern matching in .gitignore

In reply to Sean Christopherson

Use pattern matching to exclude everything except .c, .h, .S, and .sh files from Git. Manually adding every test target has an absurd maintenance cost, is comically error prone, and leads to bikeshedding over whether or not the targets should be listed in alphabetical order.

Deliberately do not include the one-off assets, e.g. config, settings, .gitignore itself, etc as Git doesn't ignore files that are already in the repository. Adding the one-off assets won't prevent mistakes where developers forget to --force add files that don't match the "allowed".

---
 tools/testing/selftests/kvm/.gitignore | 91 ++------------------------
 1 file changed, 6 insertions(+), 85 deletions(-)
tools/testing/selftests/kvm/.gitignore +6 -85
index 6ce8c488d62e..6d9381d60172 100644
@@ -1,86 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
-/aarch64/aarch32_id_regs
-/aarch64/arch_timer
-/aarch64/debug-exceptions
-/aarch64/get-reg-list
-/aarch64/hypercalls
-/aarch64/page_fault_test
-/aarch64/psci_test
-/aarch64/vcpu_width_config
-/aarch64/vgic_init
-/aarch64/vgic_irq
-/s390x/memop
-/s390x/resets
-/s390x/sync_regs_test
-/s390x/tprot
-/x86_64/amx_test
-/x86_64/cpuid_test
-/x86_64/cr4_cpuid_sync_test
-/x86_64/debug_regs
-/x86_64/exit_on_emulation_failure_test
-/x86_64/fix_hypercall_test
-/x86_64/get_msr_index_features
-/x86_64/kvm_clock_test
-/x86_64/kvm_pv_test
-/x86_64/hyperv_clock
-/x86_64/hyperv_cpuid
-/x86_64/hyperv_evmcs
-/x86_64/hyperv_features
-/x86_64/hyperv_ipi
-/x86_64/hyperv_svm_test
-/x86_64/hyperv_tlb_flush
-/x86_64/max_vcpuid_cap_test
-/x86_64/mmio_warning_test
-/x86_64/monitor_mwait_test
-/x86_64/nested_exceptions_test
-/x86_64/nx_huge_pages_test
-/x86_64/platform_info_test
-/x86_64/pmu_event_filter_test
-/x86_64/set_boot_cpu_id
-/x86_64/set_sregs_test
-/x86_64/sev_migrate_tests
-/x86_64/smaller_maxphyaddr_emulation_test
-/x86_64/smm_test
-/x86_64/state_test
-/x86_64/svm_vmcall_test
-/x86_64/svm_int_ctl_test
-/x86_64/svm_nested_soft_inject_test
-/x86_64/svm_nested_shutdown_test
-/x86_64/sync_regs_test
-/x86_64/tsc_msrs_test
-/x86_64/tsc_scaling_sync
-/x86_64/ucna_injection_test
-/x86_64/userspace_io_test
-/x86_64/userspace_msr_exit_test
-/x86_64/vmx_apic_access_test
-/x86_64/vmx_close_while_nested_test
-/x86_64/vmx_dirty_log_test
-/x86_64/vmx_exception_with_invalid_guest_state
-/x86_64/vmx_invalid_nested_guest_state
-/x86_64/vmx_msrs_test
-/x86_64/vmx_preemption_timer_test
-/x86_64/vmx_set_nested_state_test
-/x86_64/vmx_tsc_adjust_test
-/x86_64/vmx_nested_tsc_scaling_test
-/x86_64/xapic_ipi_test
-/x86_64/xapic_state_test
-/x86_64/xen_shinfo_test
-/x86_64/xen_vmcall_test
-/x86_64/xss_msr_test
-/x86_64/vmx_pmu_caps_test
-/x86_64/triple_fault_event_test
-/access_tracking_perf_test
-/demand_paging_test
-/dirty_log_test
-/dirty_log_perf_test
-/hardware_disable_test
-/kvm_create_max_vcpus
-/kvm_page_table_test
-/max_guest_memory_test
-/memslot_modification_stress_test
-/memslot_perf_test
-/rseq_test
-/set_memory_region_test
-/steal_time
-/kvm_binary_stats_test
-/system_counter_offset_test
+*
+!/**/
+!*.c
+!*.h
+!*.S
+!*.sh
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
author reviewer
#7 lore raw

PATCH 5/14 KVM: selftests: Fix a typo in x86-64's kvm_get_cpu_address_width()

In reply to Sean Christopherson

Fix a == vs. = typo in kvm_get_cpu_address_width() that results in @pa_bits being left unset if the CPU doesn't support enumerating its MAX_PHY_ADDR. Flagged by clang's unusued-value warning.

lib/x86_64/processor.c:1034:51: warning: expression result unused [-Wunused-value]

                *pa_bits == kvm_cpu_has(X86_FEATURE_PAE) ? 36 : 32;
Fixes 3bd396353d18 ("KVM: selftests: Add X86_FEATURE_PAE and use it calc "fallback" MAXPHYADDR")
---
 tools/testing/selftests/kvm/lib/x86_64/processor.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +1 -1
index c4d368d56cfe..acfa1d01e7df 100644
@@ -1031,7 +1031,7 @@ bool is_amd_cpu(void)
 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
 {
 	if (!kvm_cpu_has_p(X86_PROPERTY_MAX_PHY_ADDR)) {
-		*pa_bits == kvm_cpu_has(X86_FEATURE_PAE) ? 36 : 32;
+		*pa_bits = kvm_cpu_has(X86_FEATURE_PAE) ? 36 : 32;
 		*va_bits = 32;
 	} else {
 		*pa_bits = kvm_cpu_property(X86_PROPERTY_MAX_PHY_ADDR);
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
Reviewed-by on v1 5/14
#8 lore raw

Re: [PATCH 05/14] KVM: selftests: Fix a typo in x86-64's kvm_get_cpu_address_width()

Quoted 18 lines · Sean Christopherson

Fix a == vs. = typo in kvm_get_cpu_address_width() that results in @pa_bits being left unset if the CPU doesn't support enumerating its MAX_PHY_ADDR. Flagged by clang's unusued-value warning.

lib/x86_64/processor.c:1034:51: warning: expression result unused [-Wunused-value]

                 *pa_bits == kvm_cpu_has(X86_FEATURE_PAE) ? 36 : 32;
Fixes 3bd396353d18 ("KVM: selftests: Add X86_FEATURE_PAE and use it calc "fallback" MAXPHYADDR")
---
  tools/testing/selftests/kvm/lib/x86_64/processor.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/x86_64/processor.c +0 -0
index c4d368d56cfe..acfa1d01e7df 100644

tools/testing/selftests/kvm/lib/x86_64/processor.c bool is_amd_cpu(void) · line 1034

Quoted 5 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +1 -1
@@ -1031,7 +1031,7 @@ bool is_amd_cpu(void)
  void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
  {
  	if (!kvm_cpu_has_p(X86_PROPERTY_MAX_PHY_ADDR)) {
-		*pa_bits == kvm_cpu_has(X86_FEATURE_PAE) ? 36 : 32;
+		*pa_bits = kvm_cpu_has(X86_FEATURE_PAE) ? 36 : 32;

:)

author reviewer
#9 lore raw

PATCH 6/14 KVM: selftests: Rename UNAME_M to ARCH_DIR, fill explicitly for x86

In reply to Sean Christopherson

Rename UNAME_M to ARCH_DIR and explicitly set it directly for x86. At this point, the name of the arch directory really doesn't have anything to do with uname -m, and UNAME_M is unnecessarily confusing given that its purpose is purely to identify the arch specific directory.

---
 tools/testing/selftests/kvm/Makefile | 49 +++++++++-------------------
 1 file changed, 15 insertions(+), 34 deletions(-)
tools/testing/selftests/kvm/Makefile +15 -34
index 947676983da1..d761a77c3a80 100644
@@ -7,35 +7,16 @@ top_srcdir = ../../../..
 include $(top_srcdir)/scripts/subarch.include
 ARCH            ?= $(SUBARCH)
 
-# For cross-builds to work, UNAME_M has to map to ARCH and arch specific
-# directories and targets in this Makefile. "uname -m" doesn't map to
-# arch specific sub-directory names.
-#
-# UNAME_M variable to used to run the compiles pointing to the right arch
-# directories and build the right targets for these supported architectures.
-#
-# TEST_GEN_PROGS and LIBKVM are set using UNAME_M variable.
-# LINUX_TOOL_ARCH_INCLUDE is set using ARCH variable.
-#
-# x86_64 targets are named to include x86_64 as a suffix and directories
-# for includes are in x86_64 sub-directory. s390x and aarch64 follow the
-# same convention. "uname -m" doesn't result in the correct mapping for
-# s390x and aarch64.
-#
-# No change necessary for x86_64
-UNAME_M := $(shell uname -m)
-
-# Set UNAME_M for arm64 compile/install to work
-ifeq ($(ARCH),arm64)
-	UNAME_M := aarch64
-endif
-# Set UNAME_M s390x compile/install to work
-ifeq ($(ARCH),s390)
-	UNAME_M := s390x
-endif
-# Set UNAME_M riscv compile/install to work
-ifeq ($(ARCH),riscv)
-	UNAME_M := riscv
+ifeq ($(ARCH),x86)
+	ARCH_DIR := x86_64
+else ifeq ($(ARCH),arm64)
+	ARCH_DIR := aarch64
+else ifeq ($(ARCH),s390)
+	ARCH_DIR := s390x
+else ifeq ($(ARCH),riscv)
+	ARCH_DIR := riscv
+else
+$(error Unknown architecture '$(ARCH)')
 endif
 
 LIBKVM += lib/assert.c
@@ -196,10 +177,10 @@ TEST_GEN_PROGS_riscv += kvm_page_table_test
 TEST_GEN_PROGS_riscv += set_memory_region_test
 TEST_GEN_PROGS_riscv += kvm_binary_stats_test
 
-TEST_PROGS += $(TEST_PROGS_$(UNAME_M))
-TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
-TEST_GEN_PROGS_EXTENDED += $(TEST_GEN_PROGS_EXTENDED_$(UNAME_M))
-LIBKVM += $(LIBKVM_$(UNAME_M))
+TEST_PROGS += $(TEST_PROGS_$(ARCH_DIR))
+TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(ARCH_DIR))
+TEST_GEN_PROGS_EXTENDED += $(TEST_GEN_PROGS_EXTENDED_$(ARCH_DIR))
+LIBKVM += $(LIBKVM_$(ARCH_DIR))
 
 INSTALL_HDR_PATH = $(top_srcdir)/usr
 LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
@@ -212,7 +193,7 @@ endif
 CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
 	-fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
 	-I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
-	-I$(<D) -Iinclude/$(UNAME_M) -I ../rseq -I.. $(EXTRA_CFLAGS) \
+	-I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \
 	$(KHDR_INCLUDES)
 
 no-pie-option := $(call try-run, echo 'int main() { return 0; }' | \
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
author reviewer
#10 lore raw

Re: [PATCH 06/14] KVM: selftests: Rename UNAME_M to ARCH_DIR, fill explicitly for x86

+David

Quoted 12 lines · Sean Christopherson

Rename UNAME_M to ARCH_DIR and explicitly set it directly for x86. At this point, the name of the arch directory really doesn't have anything to do with uname -m, and UNAME_M is unnecessarily confusing given that its purpose is purely to identify the arch specific directory.

---

tools/testing/selftests/kvm/Makefile +0 -5
-# No change necessary for x86_64
-UNAME_M := $(shell uname -m)
-
-# Set UNAME_M for arm64 compile/install to work
-ifeq ($(ARCH),arm64)

tools/testing/selftests/kvm/Makefile top_srcdir = ../../../.. · line 10

Quoted 10 lines
tools/testing/selftests/kvm/Makefile +1 -9
-	UNAME_M := aarch64
-endif
-# Set UNAME_M s390x compile/install to work
-ifeq ($(ARCH),s390)
-	UNAME_M := s390x
-endif
-# Set UNAME_M riscv compile/install to work
-ifeq ($(ARCH),riscv)
-	UNAME_M := riscv
+ifeq ($(ARCH),x86)

As discovered by by David, this breaks doing "ARCH=x86_64 make", which is an allowed/supported variant in the kernel proper, so this needs to be:

  ifneq (,$(filter $(ARCH),x86 x86_64))

or alternatively

  ifeq ($(ARCH),x86_64)
  ARCH := x86
  endif

Hmm, unless there's a reason to keep ARCH=x86_64, the latter appears to be the better option as lib.mak doesn't play nice with x86_64 either, e.g. ARCH=x86_64 LLVM=1 make fails. That's arguably a lib.mak bug, but it's trivial to handle in KVM's makefile so forcing lib.mak to handle both seems unnecessary.

I'll also add a comment to call out that $(ARCH) follows the kernel's terminology for arch/*, whereas for whatever reason KVM selftests effectively uses uname -m terminology.

One last thought/question, what do y'all think about renaming directories to follow the kernel proper? I.e. aarch64=>arm64, s390x=>s390, and x86_64=>x86. Then $(ARCH_DIR) would go away. The churn would be unfortunate, but it would be nice to align with arch/ and tools/arch/.

Quoted 10 lines
tools/testing/selftests/kvm/Makefile +9 -0
+	ARCH_DIR := x86_64
+else ifeq ($(ARCH),arm64)
+	ARCH_DIR := aarch64
+else ifeq ($(ARCH),s390)
+	ARCH_DIR := s390x
+else ifeq ($(ARCH),riscv)
+	ARCH_DIR := riscv
+else
+$(error Unknown architecture '$(ARCH)')
 endif
maintainer
#11 lore raw

Re: [PATCH 06/14] KVM: selftests: Rename UNAME_M to ARCH_DIR, fill explicitly for x86

Quoted 4 lines · Sean Christopherson

One last thought/question, what do y'all think about renaming directories to follow the kernel proper? I.e. aarch64=>arm64, s390x=>s390, and x86_64=>x86. Then $(ARCH_DIR) would go away. The churn would be unfortunate, but it would be nice to align with arch/ and tools/arch/.

aarch64->arm64 makes sense to me. Whether it is worth the churn is another question. As long as we don't try to backport tests, the damage should be limited to a single merge window.

          M.
Signature · 1 line
Jazz is not dead. It just smells funny...
maintainer
#12 lore raw

Re: [PATCH 06/14] KVM: selftests: Rename UNAME_M to ARCH_DIR, fill explicitly for x86

In reply to Sean Christopherson

Review

Quoted 13 lines · Sean Christopherson
(quoted diff) +10 -2
-ifeq ($(ARCH),riscv)
-	UNAME_M := riscv
+ifeq ($(ARCH),x86)
+	ARCH_DIR := x86_64
+else ifeq ($(ARCH),arm64)
+	ARCH_DIR := aarch64
+else ifeq ($(ARCH),s390)
+	ARCH_DIR := s390x
+else ifeq ($(ARCH),riscv)
+	ARCH_DIR := riscv
+else
+$(error Unknown architecture '$(ARCH)')
  endif

$(error) would break compiling via tools/testing/selftests/Makefile, so I am squashing this:

tools/testing/selftests/kvm/Makefile +1 -3
index d761a77c3a80..59f3eb53c932 100644
@@ -13,10 +13,8 @@ else ifeq ($(ARCH),arm64)
 	ARCH_DIR := aarch64
 else ifeq ($(ARCH),s390)
 	ARCH_DIR := s390x
-else ifeq ($(ARCH),riscv)
-	ARCH_DIR := riscv
 else
-$(error Unknown architecture '$(ARCH)')
+	ARCH_DIR := $(ARCH)
 endif
 
 LIBKVM += lib/assert.c

Then the aarch64 and s390x directories can be renamed---x86 too, but the ifeq needs to stay (just changed to do x86_64->x86 instead of the other way round).

Paolo

author reviewer
#13 lore raw

PATCH 7/14 KVM: selftests: Use proper function prototypes in probing code

In reply to Sean Christopherson

Make the main() functions in the probing code proper prototypes so that compiling the probing code with more strict flags won't generate false negatives.

<stdin>:1:5: error: function declaration isn’t a prototype [-Werror=strict-prototypes]

---
 tools/testing/selftests/kvm/Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/Makefile +2 -2
index d761a77c3a80..c22c3002492d 100644
@@ -196,11 +196,11 @@ CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
 	-I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \
 	$(KHDR_INCLUDES)
 
-no-pie-option := $(call try-run, echo 'int main() { return 0; }' | \
+no-pie-option := $(call try-run, echo 'int main(void) { return 0; }' | \
         $(CC) -Werror -no-pie -x c - -o "$$TMP", -no-pie)
 
 # On s390, build the testcases KVM-enabled
-pgste-option = $(call try-run, echo 'int main() { return 0; }' | \
+pgste-option = $(call try-run, echo 'int main(void) { return 0; }' | \
 	$(CC) -Werror -Wl$(comma)--s390-pgste -x c - -o "$$TMP",-Wl$(comma)--s390-pgste)
 
 LDLIBS += -ldl
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
Reviewed-by on v1 7/14
#14 lore raw

Re: [PATCH 07/14] KVM: selftests: Use proper function prototypes in probing code

Quoted 10 lines · Sean Christopherson

Make the main() functions in the probing code proper prototypes so that compiling the probing code with more strict flags won't generate false negatives.

<stdin>:1:5: error: function declaration isn’t a prototype [-Werror=strict-prototypes]

---
  tools/testing/selftests/kvm/Makefile | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
author reviewer
#15 lore raw

PATCH 8/14 KVM: selftests: Probe -no-pie with actual CFLAGS used to compile

In reply to Sean Christopherson

Probe -no-pie with the actual set of CFLAGS used to compile the tests, clang whines about -no-pie being unused if the tests are compiled with -static.

clang: warning: argument unused during compilation: '-no-pie' [-Wunused-command-line-argument]

---
 tools/testing/selftests/kvm/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/Makefile +1 -1
index c22c3002492d..a6050dcc381a 100644
@@ -197,7 +197,7 @@ CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
 	$(KHDR_INCLUDES)
 
 no-pie-option := $(call try-run, echo 'int main(void) { return 0; }' | \
-        $(CC) -Werror -no-pie -x c - -o "$$TMP", -no-pie)
+        $(CC) -Werror $(CFLAGS) -no-pie -x c - -o "$$TMP", -no-pie)
 
 # On s390, build the testcases KVM-enabled
 pgste-option = $(call try-run, echo 'int main(void) { return 0; }' | \
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
author reviewer
#16 lore raw

PATCH 9/14 KVM: selftests: Explicitly disable builtins for mem*() overrides

In reply to Sean Christopherson

Explicitly disable the compiler's builtin memcmp(), memcpy(), and memset(). Because only lib/string_override.c is built with -ffreestanding, the compiler reserves the right to do what it wants and can try to link the non-freestanding code to its own crud.

/usr/bin/x86_64-linux-gnu-ld: /lib/x86_64-linux-gnu/libc.a(memcmp.o): in function `memcmp_ifunc':
(.text+0x0): multiple definition of `memcmp'; tools/testing/selftests/kvm/lib/string_override.o:
tools/testing/selftests/kvm/lib/string_override.c:15: first defined here
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Fixes 6b6f71484bf4 ("KVM: selftests: Implement memcmp(), memcpy(), and memset() for guest use")
---
 tools/testing/selftests/kvm/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/Makefile +1 -0
index a6050dcc381a..6594ed51eeea 100644
@@ -191,6 +191,7 @@ else
 LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
 endif
 CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
+	-fno-builtin-memcmp -fno-builtin-memcpy -fno-builtin-memset \
 	-fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
 	-I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
 	-I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
maintainer
#17 lore raw

Re: [PATCH 09/14] KVM: selftests: Explicitly disable builtins for mem*() overrides

Quoted 9 lines · Sean Christopherson

Explicitly disable the compiler's builtin memcmp(), memcpy(), and memset(). Because only lib/string_override.c is built with -ffreestanding, the compiler reserves the right to do what it wants and can try to link the non-freestanding code to its own crud.

/usr/bin/x86_64-linux-gnu-ld: /lib/x86_64-linux-gnu/libc.a(memcmp.o): in function `memcmp_ifunc':
(.text+0x0): multiple definition of `memcmp'; tools/testing/selftests/kvm/lib/string_override.o:
tools/testing/selftests/kvm/lib/string_override.c:15: first defined here
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Hmm, that's weird though. I think it's an effect of ifunc and maybe even a linker bug. The patch makes sense anyway.

Paolo

author reviewer
#18 lore raw

PATCH 10/14 KVM: selftests: Include lib.mk before consuming $(CC)

In reply to Sean Christopherson

Include lib.mk before consuming $(CC) and document that lib.mk overwrites $(CC) unless make was invoked with -e or $(CC) was specified after make (which apparently makes the environment override the Makefile?!?!). Including lib.mk after using it for probing, e.g. for -no-pie, can lead to weirdness.

---
 tools/testing/selftests/kvm/Makefile | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/Makefile +5 -4
index 6594ed51eeea..2487db21b177 100644
@@ -182,6 +182,11 @@ TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(ARCH_DIR))
 TEST_GEN_PROGS_EXTENDED += $(TEST_GEN_PROGS_EXTENDED_$(ARCH_DIR))
 LIBKVM += $(LIBKVM_$(ARCH_DIR))
 
+# lib.mak defines $(OUTPUT), prepends $(OUTPUT)/ to $(TEST_GEN_PROGS), and most
+# importantly defines, i.e. overwrites, $(CC) (unless `make -e` or `make CC=`,
+# which causes the environment variable to override the makefile).
+include ../lib.mk
+
 INSTALL_HDR_PATH = $(top_srcdir)/usr
 LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
 LINUX_TOOL_INCLUDE = $(top_srcdir)/tools/include
@@ -207,10 +212,6 @@ pgste-option = $(call try-run, echo 'int main(void) { return 0; }' | \
 LDLIBS += -ldl
 LDFLAGS += -pthread $(no-pie-option) $(pgste-option)
 
-# After inclusion, $(OUTPUT) is defined and
-# $(TEST_GEN_PROGS) starts with $(OUTPUT)/
-include ../lib.mk
-
 LIBKVM_C := $(filter %.c,$(LIBKVM))
 LIBKVM_S := $(filter %.S,$(LIBKVM))
 LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C))
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
maintainer
#19 lore raw

Re: [PATCH 10/14] KVM: selftests: Include lib.mk before consuming $(CC)

Quoted 3 lines · Sean Christopherson

Include lib.mk before consuming $(CC) and document that lib.mk overwrites $(CC) unless make was invoked with -e or $(CC) was specified after make (which apparently makes the environment override the Makefile?!?!).

Yes, it does. In projects that don't use configure or similar, you might have seen

CFLAGS = -O2 -g

to be overridden with "make CFLAGS=-g" if optimization is undesirable.

Paolo

Quoted 2 lines

Including lib.mk after using it for probing, e.g. for -no-pie, can lead to weirdness.

author reviewer
#20 lore raw

PATCH 11/14 KVM: selftests: Disable "gnu-variable-sized-type-not-at-end" warning

In reply to Sean Christopherson

Disable gnu-variable-sized-type-not-at-end so that tests and libraries can create overlays of variable sized arrays at the end of structs when using a fixed number of entries, e.g. to get/set a single MSR.

It's possible to fudge around the warning, e.g. by defining a custom struct that hardcodes the number of entries, but that is a burden for both developers and readers of the code.

lib/x86_64/processor.c:664:19: warning: field 'header' with variable sized type 'struct kvm_msrs' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]

                struct kvm_msrs header;
                                ^
lib/x86_64/processor.c:772:19: warning: field 'header' with variable sized type 'struct kvm_msrs'
not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
                struct kvm_msrs header;
                                ^
lib/x86_64/processor.c:787:19: warning: field 'header' with variable sized type 'struct kvm_msrs'
not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
                struct kvm_msrs header;
                                ^

3 warnings generated.

x86_64/hyperv_tlb_flush.c:54:18: warning: field 'hv_vp_set' with variable sized type 'struct hv_vpset' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]

        struct hv_vpset hv_vp_set;
                        ^

1 warning generated.

x86_64/xen_shinfo_test.c:137:25: warning: field 'info' with variable sized type 'struct kvm_irq_routing' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]

        struct kvm_irq_routing info;
                               ^

1 warning generated.

---
 tools/testing/selftests/kvm/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/Makefile +1 -0
index 2487db21b177..9cff99a1cb2e 100644
@@ -196,6 +196,7 @@ else
 LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
 endif
 CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
+	-Wno-gnu-variable-sized-type-not-at-end \
 	-fno-builtin-memcmp -fno-builtin-memcpy -fno-builtin-memset \
 	-fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
 	-I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
#21 lore raw

Re: [PATCH 11/14] KVM: selftests: Disable "gnu-variable-sized-type-not-at-end" warning

Quoted 45 lines · Sean Christopherson

Disable gnu-variable-sized-type-not-at-end so that tests and libraries can create overlays of variable sized arrays at the end of structs when using a fixed number of entries, e.g. to get/set a single MSR.

It's possible to fudge around the warning, e.g. by defining a custom struct that hardcodes the number of entries, but that is a burden for both developers and readers of the code.

lib/x86_64/processor.c:664:19: warning: field 'header' with variable sized type 'struct kvm_msrs' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]

                struct kvm_msrs header;
                                ^
lib/x86_64/processor.c:772:19: warning: field 'header' with variable sized type 'struct kvm_msrs'
not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
                struct kvm_msrs header;
                                ^
lib/x86_64/processor.c:787:19: warning: field 'header' with variable sized type 'struct kvm_msrs'
not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]
                struct kvm_msrs header;
                                ^

3 warnings generated.

x86_64/hyperv_tlb_flush.c:54:18: warning: field 'hv_vp_set' with variable sized type 'struct hv_vpset' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]

        struct hv_vpset hv_vp_set;
                        ^

1 warning generated.

x86_64/xen_shinfo_test.c:137:25: warning: field 'info' with variable sized type 'struct kvm_irq_routing' not at the end of a struct or class is a GNU extension [-Wgnu-variable-sized-type-not-at-end]

        struct kvm_irq_routing info;
                               ^

1 warning generated.

---
 tools/testing/selftests/kvm/Makefile | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/Makefile +0 -0
index 2487db21b177..9cff99a1cb2e 100644

tools/testing/selftests/kvm/Makefile else · line 199

Quoted 4 lines
tools/testing/selftests/kvm/Makefile +1 -0
@@ -196,6 +196,7 @@ else
 LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
 endif
 CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
+       -Wno-gnu-variable-sized-type-not-at-end \

This is a clang-specific warning. This will need to be wrapped in a cc-option check.

tools/build/Build.include seems to redefine that make macro, so be sure to test it first.

Quoted 7 lines
tools/testing/selftests/kvm/Makefile +0 -1
        -fno-builtin-memcmp -fno-builtin-memcpy -fno-builtin-memset \
        -fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \
        -I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \
--

2.39.0.rc1.256.g54fd8350bd-goog

Signature · 2 lines
Thanks,
~Nick Desaulniers
author reviewer
#22 lore raw

Re: [PATCH 11/14] KVM: selftests: Disable "gnu-variable-sized-type-not-at-end" warning

Quoted 4 lines · Nick Desaulniers
Quoted 4 lines
tools/testing/selftests/kvm/Makefile +0 -0
index 2487db21b177..9cff99a1cb2e 100644
Quoted 8 lines

tools/testing/selftests/kvm/Makefile else · line 199

Quoted 5 lines
tools/testing/selftests/kvm/Makefile +1 -0
@@ -196,6 +196,7 @@ else
 LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include
 endif
 CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \
+       -Wno-gnu-variable-sized-type-not-at-end \

This is a clang-specific warning. This will need to be wrapped in a cc-option check.

Not that I'm against guarding this code, but I don't think cc-option() will do anything in this case.

AFAICT, gcc stopped treating unknown "-Wno" flags as unconditional errors starting with gcc-4.4, and the kernel's min supported version is 5.1. gcc-4.4 through gcc-9.5 all print a mild warning if there's a different error, but otherwise silently ignore the uknown "-Wno".

cc1: warning: unrecognized command line option '-Wno-gnu-variable-sized-type-not-at-end'

gcc-10.1 is even friendlier and notes that the unknown flag may have been related to the error.

cc1: note: unrecognized command-line option '-Wno-gnu-variable-sized-type-not-at-end' may have been intended to silence earlier diagnostics

Because cc-option() doesn't have errors in its probing code, it will return "true" on gcc for literally any "-Wno-*" input that gcc deems syntacially valid, e.g. gcc barfs on

  depends on $(cc-option,-Wno-)
  depends on $(cc-option,-Wno)

but happily succeeds with

  depends on $(cc-option,-Wno-lol-gcc)

Various man pages suggest -Wunknown-warnings is a thing, but no gcc version supported by godbolt recognizes it.

So unless I'm missing something, trying to detect lack of support will be non-trivial, and the worst case scenario is that users of older gcc version will see a potentially confusing warning when the build fails.

maintainer
#23 lore raw

Re: [PATCH 11/14] KVM: selftests: Disable "gnu-variable-sized-type-not-at-end" warning

Quoted 6 lines · Sean Christopherson

AFAICT, gcc stopped treating unknown "-Wno" flags as unconditional errors starting with gcc-4.4, and the kernel's min supported version is 5.1. gcc-4.4 through gcc-9.5 all print a mild warning if there's a different error, but otherwise silently ignore the uknown "-Wno".

cc1: warning: unrecognized command line option '-Wno-gnu-variable-sized-type-not-at-end'

scripts/Makefile.compiler has cc-disable-warning for this, we can copy it to tools/.

Paolo

author reviewer
#24 lore raw

PATCH 12/14 KVM: selftests: Use wildcards to find library source files

In reply to Sean Christopherson

Use $(wildcard ...) to find the library source files instead of manually defining the inputs, which is a maintenance burden and error prone.

---
 tools/testing/selftests/kvm/Makefile | 45 ++++------------------------
 1 file changed, 5 insertions(+), 40 deletions(-)
tools/testing/selftests/kvm/Makefile +5 -40
index 9cff99a1cb2e..a9930e9197da 100644
@@ -19,44 +19,6 @@ else
 $(error Unknown architecture '$(ARCH)')
 endif
 
-LIBKVM += lib/assert.c
-LIBKVM += lib/elf.c
-LIBKVM += lib/guest_modes.c
-LIBKVM += lib/io.c
-LIBKVM += lib/kvm_util.c
-LIBKVM += lib/memstress.c
-LIBKVM += lib/rbtree.c
-LIBKVM += lib/sparsebit.c
-LIBKVM += lib/test_util.c
-LIBKVM += lib/ucall_common.c
-LIBKVM += lib/userfaultfd_util.c
-
-LIBKVM_STRING += lib/string_override.c
-
-LIBKVM_x86_64 += lib/x86_64/apic.c
-LIBKVM_x86_64 += lib/x86_64/handlers.S
-LIBKVM_x86_64 += lib/x86_64/hyperv.c
-LIBKVM_x86_64 += lib/x86_64/memstress.c
-LIBKVM_x86_64 += lib/x86_64/processor.c
-LIBKVM_x86_64 += lib/x86_64/svm.c
-LIBKVM_x86_64 += lib/x86_64/ucall.c
-LIBKVM_x86_64 += lib/x86_64/vmx.c
-
-LIBKVM_aarch64 += lib/aarch64/gic.c
-LIBKVM_aarch64 += lib/aarch64/gic_v3.c
-LIBKVM_aarch64 += lib/aarch64/handlers.S
-LIBKVM_aarch64 += lib/aarch64/processor.c
-LIBKVM_aarch64 += lib/aarch64/spinlock.c
-LIBKVM_aarch64 += lib/aarch64/ucall.c
-LIBKVM_aarch64 += lib/aarch64/vgic.c
-
-LIBKVM_s390x += lib/s390x/diag318_test_handler.c
-LIBKVM_s390x += lib/s390x/processor.c
-LIBKVM_s390x += lib/s390x/ucall.c
-
-LIBKVM_riscv += lib/riscv/processor.c
-LIBKVM_riscv += lib/riscv/ucall.c
-
 # Non-compiled test targets
 TEST_PROGS_x86_64 += x86_64/nx_huge_pages_test.sh
 
@@ -213,10 +175,13 @@ pgste-option = $(call try-run, echo 'int main(void) { return 0; }' | \
 LDLIBS += -ldl
 LDFLAGS += -pthread $(no-pie-option) $(pgste-option)
 
-LIBKVM_C := $(filter %.c,$(LIBKVM))
-LIBKVM_S := $(filter %.S,$(LIBKVM))
+LIBKVM_C := $(filter-out lib/string_override.c,$(wildcard lib/*.c))
+LIBKVM_C += $(wildcard lib/$(ARCH_DIR)/*.c)
+LIBKVM_S := $(wildcard lib/*.S)
+LIBKVM_S += $(wildcard lib/$(ARCH_DIR)/*.S)
 LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C))
 LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S))
+LIBKVM_STRING := lib/string_override.c
 LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING))
 LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ)
 
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
maintainer
#25 lore raw

Re: [PATCH 12/14] KVM: selftests: Use wildcards to find library source files

Quoted 2 lines · Sean Christopherson

Use $(wildcard ...) to find the library source files instead of manually defining the inputs, which is a maintenance burden and error prone.

No, please don't. This leads to weird errors, for example when "git checkout" is interrupted with ^C. I have applied patches 1-11.

Paolo

author reviewer
#26 lore raw

Re: [PATCH 12/14] KVM: selftests: Use wildcards to find library source files

Quoted 6 lines · Paolo Bonzini
Quoted 2 lines · Sean Christopherson

Use $(wildcard ...) to find the library source files instead of manually defining the inputs, which is a maintenance burden and error prone.

No, please don't. This leads to weird errors, for example when "git checkout" is interrupted with ^C.

Well, don't do that ;-)

Are there concerns beyond having an incomplete and/or modified git working tree? E.g. could we do something crazy like fail the build by default if the working tree isn't pristine? The library files aren't terrible, but the number of tests is getting unwieldy, to say the least.

author reviewer
#27 lore raw

PATCH 13/14 KVM: selftests: Use wildcards to find targets and test source files

In reply to Sean Christopherson

Use $(wildcard ...) to find the test targets (.sh files) and test source files (.c and .S) instead of manually adding files/targets for every architecture, which is a maintenance burden and error prone, e.g. RISC-V supports RSEQ but doesn't build the test for reasons unknown.

To deal with common tests that are only supported on a subset of architectures, add a dummy macro TEST_UNSUPPORTED() that tests can use to declare the test as unsupported for a given architecture, and filter out unsupported tests for the target architecture via grep. As a bonus, explicitly naming unsupported architectures will also force developers to opt-out of architectures (or opt-out of tests when adding a new arch).

---
 tools/testing/selftests/kvm/Makefile          | 128 +-----------------
 .../selftests/kvm/access_tracking_perf_test.c |   3 +
 .../selftests/kvm/dirty_log_perf_test.c       |   3 +
 .../selftests/kvm/hardware_disable_test.c     |   4 +
 .../testing/selftests/kvm/include/test_util.h |  11 ++
 .../selftests/kvm/max_guest_memory_test.c     |   4 +
 .../kvm/memslot_modification_stress_test.c    |   3 +
 .../testing/selftests/kvm/memslot_perf_test.c |   3 +
 tools/testing/selftests/kvm/rseq_test.c       |   2 +
 tools/testing/selftests/kvm/steal_time.c      |   3 +
 .../kvm/system_counter_offset_test.c          |   4 +
 11 files changed, 46 insertions(+), 122 deletions(-)
tools/testing/selftests/kvm/Makefile +6 -122
index a9930e9197da..76382850a28f 100644
@@ -19,130 +19,14 @@ else
 $(error Unknown architecture '$(ARCH)')
 endif
 
-# Non-compiled test targets
-TEST_PROGS_x86_64 += x86_64/nx_huge_pages_test.sh
+TESTS_SRC := $(shell grep -L TEST_UNSUPPORTED\($(ARCH_DIR)\) *.c)
+TESTS_SRC += $(wildcard $(ARCH_DIR)/*.c)
 
-# Compiled test targets
-TEST_GEN_PROGS_x86_64 = x86_64/cpuid_test
-TEST_GEN_PROGS_x86_64 += x86_64/cr4_cpuid_sync_test
-TEST_GEN_PROGS_x86_64 += x86_64/get_msr_index_features
-TEST_GEN_PROGS_x86_64 += x86_64/exit_on_emulation_failure_test
-TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test
-TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
-TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
-TEST_GEN_PROGS_x86_64 += x86_64/hyperv_evmcs
-TEST_GEN_PROGS_x86_64 += x86_64/hyperv_features
-TEST_GEN_PROGS_x86_64 += x86_64/hyperv_ipi
-TEST_GEN_PROGS_x86_64 += x86_64/hyperv_svm_test
-TEST_GEN_PROGS_x86_64 += x86_64/hyperv_tlb_flush
-TEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test
-TEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test
-TEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test
-TEST_GEN_PROGS_x86_64 += x86_64/monitor_mwait_test
-TEST_GEN_PROGS_x86_64 += x86_64/nested_exceptions_test
-TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test
-TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test
-TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id
-TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test
-TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test
-TEST_GEN_PROGS_x86_64 += x86_64/smm_test
-TEST_GEN_PROGS_x86_64 += x86_64/state_test
-TEST_GEN_PROGS_x86_64 += x86_64/vmx_preemption_timer_test
-TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test
-TEST_GEN_PROGS_x86_64 += x86_64/svm_int_ctl_test
-TEST_GEN_PROGS_x86_64 += x86_64/svm_nested_shutdown_test
-TEST_GEN_PROGS_x86_64 += x86_64/svm_nested_soft_inject_test
-TEST_GEN_PROGS_x86_64 += x86_64/tsc_scaling_sync
-TEST_GEN_PROGS_x86_64 += x86_64/sync_regs_test
-TEST_GEN_PROGS_x86_64 += x86_64/ucna_injection_test
-TEST_GEN_PROGS_x86_64 += x86_64/userspace_io_test
-TEST_GEN_PROGS_x86_64 += x86_64/userspace_msr_exit_test
-TEST_GEN_PROGS_x86_64 += x86_64/vmx_apic_access_test
-TEST_GEN_PROGS_x86_64 += x86_64/vmx_close_while_nested_test
-TEST_GEN_PROGS_x86_64 += x86_64/vmx_dirty_log_test
-TEST_GEN_PROGS_x86_64 += x86_64/vmx_exception_with_invalid_guest_state
-TEST_GEN_PROGS_x86_64 += x86_64/vmx_msrs_test
-TEST_GEN_PROGS_x86_64 += x86_64/vmx_invalid_nested_guest_state
-TEST_GEN_PROGS_x86_64 += x86_64/vmx_set_nested_state_test
-TEST_GEN_PROGS_x86_64 += x86_64/vmx_tsc_adjust_test
-TEST_GEN_PROGS_x86_64 += x86_64/vmx_nested_tsc_scaling_test
-TEST_GEN_PROGS_x86_64 += x86_64/xapic_ipi_test
-TEST_GEN_PROGS_x86_64 += x86_64/xapic_state_test
-TEST_GEN_PROGS_x86_64 += x86_64/xss_msr_test
-TEST_GEN_PROGS_x86_64 += x86_64/debug_regs
-TEST_GEN_PROGS_x86_64 += x86_64/tsc_msrs_test
-TEST_GEN_PROGS_x86_64 += x86_64/vmx_pmu_caps_test
-TEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test
-TEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test
-TEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests
-TEST_GEN_PROGS_x86_64 += x86_64/amx_test
-TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test
-TEST_GEN_PROGS_x86_64 += x86_64/triple_fault_event_test
-TEST_GEN_PROGS_x86_64 += access_tracking_perf_test
-TEST_GEN_PROGS_x86_64 += demand_paging_test
-TEST_GEN_PROGS_x86_64 += dirty_log_test
-TEST_GEN_PROGS_x86_64 += dirty_log_perf_test
-TEST_GEN_PROGS_x86_64 += hardware_disable_test
-TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
-TEST_GEN_PROGS_x86_64 += kvm_page_table_test
-TEST_GEN_PROGS_x86_64 += max_guest_memory_test
-TEST_GEN_PROGS_x86_64 += memslot_modification_stress_test
-TEST_GEN_PROGS_x86_64 += memslot_perf_test
-TEST_GEN_PROGS_x86_64 += rseq_test
-TEST_GEN_PROGS_x86_64 += set_memory_region_test
-TEST_GEN_PROGS_x86_64 += steal_time
-TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test
-TEST_GEN_PROGS_x86_64 += system_counter_offset_test
+TEST_PROGS := $(wildcard *.sh)
+TEST_PROGS += $(wildcard $(ARCH_DIR)/*.sh)
 
-# Compiled outputs used by test targets
-TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test
-
-TEST_GEN_PROGS_aarch64 += aarch64/aarch32_id_regs
-TEST_GEN_PROGS_aarch64 += aarch64/arch_timer
-TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions
-TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list
-TEST_GEN_PROGS_aarch64 += aarch64/hypercalls
-TEST_GEN_PROGS_aarch64 += aarch64/page_fault_test
-TEST_GEN_PROGS_aarch64 += aarch64/psci_test
-TEST_GEN_PROGS_aarch64 += aarch64/vcpu_width_config
-TEST_GEN_PROGS_aarch64 += aarch64/vgic_init
-TEST_GEN_PROGS_aarch64 += aarch64/vgic_irq
-TEST_GEN_PROGS_aarch64 += access_tracking_perf_test
-TEST_GEN_PROGS_aarch64 += demand_paging_test
-TEST_GEN_PROGS_aarch64 += dirty_log_test
-TEST_GEN_PROGS_aarch64 += dirty_log_perf_test
-TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus
-TEST_GEN_PROGS_aarch64 += kvm_page_table_test
-TEST_GEN_PROGS_aarch64 += memslot_modification_stress_test
-TEST_GEN_PROGS_aarch64 += memslot_perf_test
-TEST_GEN_PROGS_aarch64 += rseq_test
-TEST_GEN_PROGS_aarch64 += set_memory_region_test
-TEST_GEN_PROGS_aarch64 += steal_time
-TEST_GEN_PROGS_aarch64 += kvm_binary_stats_test
-
-TEST_GEN_PROGS_s390x = s390x/memop
-TEST_GEN_PROGS_s390x += s390x/resets
-TEST_GEN_PROGS_s390x += s390x/sync_regs_test
-TEST_GEN_PROGS_s390x += s390x/tprot
-TEST_GEN_PROGS_s390x += demand_paging_test
-TEST_GEN_PROGS_s390x += dirty_log_test
-TEST_GEN_PROGS_s390x += kvm_create_max_vcpus
-TEST_GEN_PROGS_s390x += kvm_page_table_test
-TEST_GEN_PROGS_s390x += rseq_test
-TEST_GEN_PROGS_s390x += set_memory_region_test
-TEST_GEN_PROGS_s390x += kvm_binary_stats_test
-
-TEST_GEN_PROGS_riscv += demand_paging_test
-TEST_GEN_PROGS_riscv += dirty_log_test
-TEST_GEN_PROGS_riscv += kvm_create_max_vcpus
-TEST_GEN_PROGS_riscv += kvm_page_table_test
-TEST_GEN_PROGS_riscv += set_memory_region_test
-TEST_GEN_PROGS_riscv += kvm_binary_stats_test
-
-TEST_PROGS += $(TEST_PROGS_$(ARCH_DIR))
-TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(ARCH_DIR))
-TEST_GEN_PROGS_EXTENDED += $(TEST_GEN_PROGS_EXTENDED_$(ARCH_DIR))
-LIBKVM += $(LIBKVM_$(ARCH_DIR))
+TEST_GEN_PROGS := $(patsubst %.c, %, $(filter-out $(TEST_PROGS),$(TESTS_SRC)))
+TEST_GEN_PROGS_EXTENDED := $(patsubst %.c, %, $(filter $(TEST_PROGS),$(TESTS_SRC)))
 
 # lib.mak defines $(OUTPUT), prepends $(OUTPUT)/ to $(TEST_GEN_PROGS), and most
 # importantly defines, i.e. overwrites, $(CC) (unless `make -e` or `make CC=`,
tools/testing/selftests/kvm/access_tracking_perf_test.c +3 -0
index 3c7defd34f56..a6e20103116e 100644
@@ -48,6 +48,9 @@
 #include "guest_modes.h"
 #include "processor.h"
 
+TEST_UNSUPPORTED(s390x);
+TEST_UNSUPPORTED(riscv);
+
 /* Global variable used to synchronize all of the vCPU threads. */
 static int iteration;
 
tools/testing/selftests/kvm/dirty_log_perf_test.c +3 -0
index e9d6d1aecf89..65ef1e1ddab9 100644
@@ -19,6 +19,9 @@
 #include "memstress.h"
 #include "guest_modes.h"
 
+TEST_UNSUPPORTED(s390x);
+TEST_UNSUPPORTED(riscv);
+
 #ifdef __aarch64__
 #include "aarch64/vgic.h"
 
tools/testing/selftests/kvm/hardware_disable_test.c +4 -0
index f5d59b9934f1..f844cdb93611 100644
@@ -19,6 +19,10 @@
 
 #include "kvm_util.h"
 
+TEST_UNSUPPORTED(aarch64);
+TEST_UNSUPPORTED(s390x);
+TEST_UNSUPPORTED(riscv);
+
 #define VCPU_NUM 4
 #define SLEEPING_THREAD_NUM (1 << 4)
 #define FORK_NUM (1ULL << 9)
tools/testing/selftests/kvm/include/test_util.h +11 -0
index 80d6416f3012..fd94e94dfb78 100644
@@ -20,6 +20,17 @@
 #include <sys/mman.h>
 #include "kselftest.h"
 
+/*
+ * Flags a common test as unsupported for a given architecture.  Parsed during
+ * the build by the Makefile to filter out the test as appropriate.
+ */
+#define TEST_UNSUPPORTED(arch)				\
+	static_assert(!strcmp(#arch, "aarch64") ||	\
+		      !strcmp(#arch, "s390x")	||	\
+		      !strcmp(#arch, "riscv")	||	\
+		      !strcmp(#arch, "x86_64"),		\
+		      "Unsupported architecture: " #arch)
+
 static inline int _no_printf(const char *format, ...) { return 0; }
 
 #ifdef DEBUG
tools/testing/selftests/kvm/max_guest_memory_test.c +4 -0
index feaf2be20ff2..d5bc0cce1e36 100644
@@ -18,6 +18,10 @@
 #include "guest_modes.h"
 #include "processor.h"
 
+TEST_UNSUPPORTED(aarch64);
+TEST_UNSUPPORTED(s390x);
+TEST_UNSUPPORTED(riscv);
+
 static void guest_code(uint64_t start_gpa, uint64_t end_gpa, uint64_t stride)
 {
 	uint64_t gpa;
tools/testing/selftests/kvm/memslot_modification_stress_test.c +3 -0
index 9855c41ca811..b9bdbde664f4 100644
@@ -26,6 +26,9 @@
 #include "test_util.h"
 #include "guest_modes.h"
 
+TEST_UNSUPPORTED(s390x);
+TEST_UNSUPPORTED(riscv);
+
 #define DUMMY_MEMSLOT_INDEX 7
 
 #define DEFAULT_MEMSLOT_MODIFICATION_ITERATIONS 10
tools/testing/selftests/kvm/memslot_perf_test.c +3 -0
index e6587e193490..37085d2c2a03 100644
@@ -26,6 +26,9 @@
 #include <kvm_util.h>
 #include <processor.h>
 
+TEST_UNSUPPORTED(s390x);
+TEST_UNSUPPORTED(riscv);
+
 #define MEM_EXTRA_SIZE		SZ_64K
 
 #define MEM_SIZE		(SZ_512M + MEM_EXTRA_SIZE)
tools/testing/selftests/kvm/rseq_test.c +2 -0
index 3045fdf9bdf5..34c3df9b4e81 100644
@@ -22,6 +22,8 @@
 
 #include "../rseq/rseq.c"
 
+TEST_UNSUPPORTED(riscv);
+
 /*
  * Any bug related to task migration is likely to be timing-dependent; perform
  * a large number of migrations to reduce the odds of a false negative.
tools/testing/selftests/kvm/steal_time.c +3 -0
index c87f38712073..66f873ea8a73 100644
@@ -17,6 +17,9 @@
 #include "kvm_util.h"
 #include "processor.h"
 
+TEST_UNSUPPORTED(s390x);
+TEST_UNSUPPORTED(riscv);
+
 #define NR_VCPUS		4
 #define ST_GPA_BASE		(1 << 30)
 
tools/testing/selftests/kvm/system_counter_offset_test.c +4 -0
index 7f5b330b6a1b..dc7f5dc5fbcc 100644
@@ -14,6 +14,10 @@
 #include "kvm_util.h"
 #include "processor.h"
 
+TEST_UNSUPPORTED(aarch64);
+TEST_UNSUPPORTED(s390x);
+TEST_UNSUPPORTED(riscv);
+
 #ifdef __x86_64__
 
 struct test_case {
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
author reviewer
#28 lore raw

PATCH 14/14 KVM: selftests: Enable RSEQ test for RISC-V

In reply to Sean Christopherson

Enable the RSEQ test for RISC-V, which according to HAVE_RSEQ is supported by the kernel and thus should be tested. The RSEQ test was added shortly before RISC-V selftests support landed, i.e. was likely overlooked during merging.

Note, the RSEQ test currently doesn't compile with clang due to an issue in the base RSEQ test code. Given that clang is constantly broken for KVM selftests, enable the RSEQ test and deal with its broken clang state in a separate commit/series.

In file included from rseq_test.c:23:
In file included from ./../rseq/rseq.c:33:
In file included from ../rseq/rseq.h:97:
../rseq/rseq-riscv.h:657:17: error: invalid input constraint 'er' in asm
[off] "er" (off),
^

---
 tools/testing/selftests/kvm/rseq_test.c | 2 --
 1 file changed, 2 deletions(-)
tools/testing/selftests/kvm/rseq_test.c +0 -2
index 34c3df9b4e81..3045fdf9bdf5 100644
@@ -22,8 +22,6 @@
 
 #include "../rseq/rseq.c"
 
-TEST_UNSUPPORTED(riscv);
-
 /*
  * Any bug related to task migration is likely to be timing-dependent; perform
  * a large number of migrations to reduce the odds of a false negative.
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog