From: Sean Christopherson <seanjc@google.com>
To: Dongli Zhang <dongli.zhang@oracle.com>
Cc: kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
pbonzini@redhat.com, shuah@kernel.org,
linux-kernel@vger.kernel.org, joe.jin@oracle.com
Subject: Re: [PATCH 1/1] KVM: selftests: add kvmclock drift test
Date: Fri, 16 Aug 2024 08:15:15 -0700 [thread overview]
Message-ID: <Zr9tA1Zo9QXmCm9V@google.com> (raw)
In-Reply-To: <20240106083346.29180-1-dongli.zhang@oracle.com>
On Sat, Jan 06, 2024, Dongli Zhang wrote:
> diff --git a/tools/testing/selftests/kvm/x86_64/kvm_clock_drift.c b/tools/testing/selftests/kvm/x86_64/kvm_clock_drift.c
> new file mode 100644
> index 000000000000..324f0dbc5762
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/x86_64/kvm_clock_drift.c
For better or worse, our selftests naming adds a _test at the end.
However, should we call this kvm_clock_hotplug_test.c? That way the file level
comment isn't necessary (those these always become stale), and David's live update
suggestion won't conflict.
Or if the live update code fits nicely in this test, then kvm_clock_drift_test.c
is probably a good name.
> @@ -0,0 +1,223 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * The kvmclock drift test. Emulate vCPU hotplug and online to verify if
> + * there is kvmclock drift.
> + *
> + * Adapted from steal_time.c
This is really uninteresting.
> + * Copyright (C) 2020, Red Hat, Inc.
> + * Copyright (C) 2024 Oracle and/or its affiliates.
> + */
> +
> +#include <asm/kvm_para.h>
> +#include <asm/pvclock.h>
> +#include <asm/pvclock-abi.h>
> +#include <sys/stat.h>
> +
> +#include "kvm_util.h"
> +#include "processor.h"
> +
> +#define NR_VCPUS 2
> +#define NR_SLOTS 2
> +#define KVMCLOCK_SIZE sizeof(struct pvclock_vcpu_time_info)
> +/*
> + * KVMCLOCK_GPA is identity mapped
> + */
> +#define KVMCLOCK_GPA (1 << 30)
> +
> +static uint64_t kvmclock_gpa = KVMCLOCK_GPA;
Why hardcode an address? Can't the test have a global "struct pvclock_vcpu_time_info"
whose address is then queried by the host by reading the MSR?
Actually, can't the pvclock reads and asserts be done in the guest? Which is
arguably better since the guest observing drift (or not) is what we really care
about. Then the host side of the test never needs to resolve a host virtual
address for the pvclock_vcpu_time_info structure.
> +static void guest_code(int cpu)
> +{
> + struct pvclock_vcpu_time_info *kvmclock;
> +
> + /*
> + * vCPU#0 is to detect the change of pvclock_vcpu_time_info
Just vCPU0, which is the usually terminology in KVM land.
> + */
/* Single line comments should look like this. */
> + if (cpu == 0) {
Rather than switch inside the guest code, just use vcpu_arch_set_entry_point()
to point vCPU1 at a different function entirely. Then most of the commetns go
away, and the code is generally much easier to read.
> + GUEST_SYNC(0);
> +
> + kvmclock = (struct pvclock_vcpu_time_info *) kvmclock_gpa;
> + wrmsr(MSR_KVM_SYSTEM_TIME_NEW, kvmclock_gpa | KVM_MSR_ENABLED);
> +
> + /*
> + * Backup the pvclock_vcpu_time_info before vCPU#1 hotplug
> + */
> + kvmclock[1] = kvmclock[0];
> +
> + GUEST_SYNC(2);
> + /*
> + * Enter the guest to update pvclock_vcpu_time_info
> + */
> + GUEST_SYNC(4);
> + }
> +
> + /*
> + * vCPU#1 is to emulate the vCPU hotplug
> + */
> + if (cpu == 1) {
> + GUEST_SYNC(1);
> + /*
> + * This is after the host side MSR_IA32_TSC
> + */
Rather than add comments, use an enum to describe the stages:
enum {
SYNC_VCPU0_???
SYNC_VCPU1_???
SYNC_VCPU0_???
SYNC_VCPU1_???
}
That said, why does this test "emulate" hotplug? Why not literally hotplug a
vCPU? It's probably _less_ code, and the annoying NR_VCPUS #define goes away too,
because you can do:
vm = vm_create_with_one_vcpu(&vcpu0, vcpu0_code);
<setup kvmclock>
vcpu1 = vm_vcpu_add(vm, 1, vcpu1_code);
Then you probably only need one sync, from vCPU to alert the host that kvmclock
is setup.
> + GUEST_SYNC(3);
> + }
> +}
> +#define CLOCKSOURCE_PATH "/sys/devices/system/clocksource/clocksource0/current_clocksource"
> +
> +static void check_clocksource(void)
> +{
> + char *clk_name;
> + struct stat st;
> + FILE *fp;
> +
> + fp = fopen(CLOCKSOURCE_PATH, "r");
> + if (!fp) {
> + pr_info("failed to open clocksource file: %d; assuming TSC.\n",
> + errno);
> + return;
> + }
> +
> + if (fstat(fileno(fp), &st)) {
> + pr_info("failed to stat clocksource file: %d; assuming TSC.\n",
> + errno);
> + goto out;
> + }
> +
> + clk_name = malloc(st.st_size);
> + TEST_ASSERT(clk_name, "failed to allocate buffer to read file\n");
> +
> + if (!fgets(clk_name, st.st_size, fp)) {
> + pr_info("failed to read clocksource file: %d; assuming TSC.\n",
> + ferror(fp));
> + goto out;
> + }
> +
> + TEST_ASSERT(!strncmp(clk_name, "tsc\n", st.st_size),
TEST_REQUIRE, not TEST_ASSERT, i.e. skip the test if the clocksource isn't
supported, don't cause a failure.
> + "clocksource not supported: %s", clk_name);
> +out:
> + fclose(fp);
> +}
This can all be replaced with:
TEST_REQUIRE(sys_clocksource_is_based_on_tsc());
prev parent reply other threads:[~2024-08-16 15:15 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-06 8:33 [PATCH 1/1] KVM: selftests: add kvmclock drift test Dongli Zhang
2024-01-29 18:42 ` Dongli Zhang
2024-04-04 14:34 ` David Woodhouse
2024-08-16 15:15 ` Sean Christopherson [this message]
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=Zr9tA1Zo9QXmCm9V@google.com \
--to=seanjc@google.com \
--cc=dongli.zhang@oracle.com \
--cc=joe.jin@oracle.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=shuah@kernel.org \
/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