Re: [PATCH 4/9] KVM: selftests: Pass args to vCPU instead of using globals

Andrew Jones
2019-10-03T07:38:12Z

On Fri, Sep 27, 2019 at 09:18:32AM -0700, Ben Gardon wrote:
> In preparation for supporting multiple vCPUs in the demand paging test,
> pass arguments to the vCPU instead of syncing globals to it.
>
> Signed-off-by: Ben Gardon <bgardon@google.com>
> ---
> .../selftests/kvm/demand_paging_test.c | 61 +++++++++++--------
> 1 file changed, 37 insertions(+), 24 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/demand_paging_test.c b/tools/testing/selftests/kvm/demand_paging_test.c
> index 19982a33a0ca2..8fd46e99d9e30 100644
> --- a/tools/testing/selftests/kvm/demand_paging_test.c
> +++ b/tools/testing/selftests/kvm/demand_paging_test.c
> @@ -44,7 +44,6 @@
> */
> static uint64_t host_page_size;
> static uint64_t guest_page_size;
> -static uint64_t guest_num_pages;
>
> static char *guest_data_prototype;
>
> @@ -65,14 +64,13 @@ static uint64_t guest_test_virt_mem = DEFAULT_GUEST_TEST_MEM;
> * Continuously write to the first 8 bytes of each page in the demand paging
> * memory region.
> */
> -static void guest_code(void)
> +static void guest_code(uint64_t gva, uint64_t pages)
> {
> int i;
>
> - for (i = 0; i < guest_num_pages; i++) {
> - uint64_t addr = guest_test_virt_mem;
> + for (i = 0; i < pages; i++) {
> + uint64_t addr = gva + (i * guest_page_size);
>
> - addr += i * guest_page_size;
> addr &= ~(host_page_size - 1);
> *(uint64_t *)addr = 0x0123456789ABCDEF;
> }
> @@ -84,18 +82,31 @@ static void guest_code(void)
> static void *host_test_mem;
> static uint64_t host_num_pages;
>
> +struct vcpu_thread_args {
> + uint64_t gva;
> + uint64_t pages;
> + struct kvm_vm *vm;
> + int vcpu_id;
> +};
> +
> static void *vcpu_worker(void *data)
> {
> int ret;
> - struct kvm_vm *vm = data;
> + struct vcpu_thread_args *args = (struct vcpu_thread_args *)data;
> + struct kvm_vm *vm = args->vm;
> + int vcpu_id = args->vcpu_id;
> + uint64_t gva = args->gva;
> + uint64_t pages = args->pages;
> struct kvm_run *run;
>
> - run = vcpu_state(vm, VCPU_ID);
> + vcpu_args_set(vm, vcpu_id, 2, gva, pages);

AArch64 doesn't implement vcpu_args_set(), but I see in the first patch
that you've added this test to AArch64 as well.

Wouldn't it be easier to just create a global array of size nr-vcpus for
each variable that needs to be shared with the guest? Then derive the
per-cpu index from the acpi-id or maybe abuse some msr for it. We could
probably even add some macros to build some type of a per-cpu framework.

Thanks,
drew