linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Manali Shukla <manali.shukla@amd.com>
Cc: kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
	pbonzini@redhat.com,  nikunj@amd.com, bp@alien8.de
Subject: Re: [PATCH v5 5/5] KVM: selftests: Add bus lock exit test
Date: Fri, 16 May 2025 12:28:25 -0700	[thread overview]
Message-ID: <aCeR2TjPzC_OYBfG@google.com> (raw)
In-Reply-To: <20250502050346.14274-6-manali.shukla@amd.com>

On Fri, May 02, 2025, Manali Shukla wrote:
> diff --git a/tools/testing/selftests/kvm/x86/kvm_buslock_test.c b/tools/testing/selftests/kvm/x86/kvm_buslock_test.c
> new file mode 100644
> index 000000000000..9c081525ac2a
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/x86/kvm_buslock_test.c
> @@ -0,0 +1,135 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (C) 2024 Advanced Micro Devices, Inc.
> + */
> +
> +#include "test_util.h"
> +#include "kvm_util.h"
> +#include "processor.h"
> +#include "svm_util.h"
> +#include "vmx.h"
> +
> +#define NR_ITERATIONS 100
> +#define L2_GUEST_STACK_SIZE 64
> +
> +#pragma GCC diagnostic push
> +#pragma GCC diagnostic ignored "-Waddress-of-packed-member"

Eww.

> +
> +struct buslock_test {
> +	unsigned char pad[PAGE_SIZE - 2];
> +	atomic_long_t val;
> +} __packed;

You don't need an entire page to generate a bus lock, two cache lines will do
nicely.  And there's certain no need for __packed.

> +struct buslock_test test __aligned(PAGE_SIZE);
> +
> +static __always_inline void buslock_atomic_add(int i, atomic_long_t *v)
> +{
> +	asm volatile(LOCK_PREFIX "addl %1,%0"
> +		     : "+m" (v->counter)
> +		     : "ir" (i) : "memory");
> +}

If only there were utilities for atomics...

> +static void buslock_add(void)

guest_generate_buslocks()

> +{
> +	/*
> +	 * Increment a page unaligned variable atomically.
> +	 * This should generate a bus lock exit.

Not should, will.

> +	 */
> +	for (int i = 0; i < NR_ITERATIONS; i++)
> +		buslock_atomic_add(2, &test.val);

Don't do weird and completely arbitrary things like adding '2' instead of '1',
it makes readers look for intent and purpose that doesn't exist.

> +}

...

> +int main(int argc, char *argv[])
> +{
> +	struct kvm_vcpu *vcpu;
> +	struct kvm_run *run;
> +	struct kvm_vm *vm;
> +	vm_vaddr_t nested_test_data_gva;
> +
> +	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM) || kvm_cpu_has(X86_FEATURE_VMX));

There's no reason to make nested support a hard dependency, it's just as easy to
make it conditional.

> +	TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_BUS_LOCK_EXIT));
> +
> +	vm = vm_create(1);
> +	vm_enable_cap(vm, KVM_CAP_X86_BUS_LOCK_EXIT, KVM_BUS_LOCK_DETECTION_EXIT);
> +	vcpu = vm_vcpu_add(vm, 0, guest_code);
> +
> +	if (kvm_cpu_has(X86_FEATURE_SVM))
> +		vcpu_alloc_svm(vm, &nested_test_data_gva);
> +	else
> +		vcpu_alloc_vmx(vm, &nested_test_data_gva);
> +
> +	vcpu_args_set(vcpu, 1, nested_test_data_gva);
> +
> +	run = vcpu->run;
> +
> +	for (;;) {
> +		struct ucall uc;
> +
> +		vcpu_run(vcpu);
> +
> +		if (run->exit_reason == KVM_EXIT_IO) {
> +			switch (get_ucall(vcpu, &uc)) {
> +			case UCALL_ABORT:
> +				REPORT_GUEST_ASSERT(uc);
> +				/* NOT REACHED */
> +			case UCALL_SYNC:
> +				continue;
> +			case UCALL_DONE:
> +				goto done;
> +			default:
> +				TEST_FAIL("Unknown ucall 0x%lx.", uc.cmd);
> +			}
> +		}
> +
> +		TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_X86_BUS_LOCK);
> +	}

*sigh*

This doesn't actually ****VERIFY**** that the expected number of bus lock exits
were generated.  KVM could literally do nothing and the test will pass.  E.g. the
test passes if I do this:

diff --git a/tools/testing/selftests/kvm/x86/kvm_buslock_test.c b/tools/testing/selftests/kvm/x86/kvm_buslock_test.c
index 9c081525ac2a..aa65d6be0f13 100644
--- a/tools/testing/selftests/kvm/x86/kvm_buslock_test.c
+++ b/tools/testing/selftests/kvm/x86/kvm_buslock_test.c
@@ -93,10 +93,10 @@ int main(int argc, char *argv[])
        vm_vaddr_t nested_test_data_gva;
 
        TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM) || kvm_cpu_has(X86_FEATURE_VMX));
-       TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_BUS_LOCK_EXIT));
+//     TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_BUS_LOCK_EXIT));
 
        vm = vm_create(1);
-       vm_enable_cap(vm, KVM_CAP_X86_BUS_LOCK_EXIT, KVM_BUS_LOCK_DETECTION_EXIT);
+//     vm_enable_cap(vm, KVM_CAP_X86_BUS_LOCK_EXIT, KVM_BUS_LOCK_DETECTION_EXIT);
        vcpu = vm_vcpu_add(vm, 0, guest_code);
 
        if (kvm_cpu_has(X86_FEATURE_SVM))
--

The test would also fail to detect if KVM completely skipped the instruction.

This is not rocket science.  If you can't make your test fail by introducing bugs
in what you're testing, then your test is worthless.

No need for a v6, I'm going to do surgery when I apply, this series has dragged
on for far too long.

  reply	other threads:[~2025-05-16 19:28 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-02  5:03 [PATCH v5 0/5] Add support for the Bus Lock Threshold Manali Shukla
2025-05-02  5:03 ` [PATCH v5 1/5] KVM: x86: Make kvm_pio_request.linear_rip a common field for user exits Manali Shukla
2025-05-02  5:03 ` [PATCH v5 2/5] x86/cpufeatures: Add CPUID feature bit for the Bus Lock Threshold Manali Shukla
2025-05-12 10:46   ` Borislav Petkov
2025-05-02  5:03 ` [PATCH v5 3/5] KVM: SVM: Enable Bus lock threshold exit Manali Shukla
2025-05-02  5:03 ` [PATCH v5 4/5] KVM: SVM: Add support for KVM_CAP_X86_BUS_LOCK_EXIT on SVM CPUs Manali Shukla
2025-05-17 16:46   ` ALOK TIWARI
2025-05-02  5:03 ` [PATCH v5 5/5] KVM: selftests: Add bus lock exit test Manali Shukla
2025-05-16 19:28   ` Sean Christopherson [this message]
2025-05-19 11:45     ` Manali Shukla
2025-05-12  4:47 ` [PATCH v5 0/5] Add support for the Bus Lock Threshold Manali Shukla
2025-05-16 19:01   ` Sean Christopherson
2025-05-19 11:39     ` Manali Shukla
2025-05-20 16:48 ` Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aCeR2TjPzC_OYBfG@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=manali.shukla@amd.com \
    --cc=nikunj@amd.com \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).