public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Ilias Stamatis <ilstam@amazon.com>
Cc: kvm@vger.kernel.org, pbonzini@redhat.com, pdurrant@amazon.co.uk,
	 dwmw@amazon.co.uk, nh-open-source@amazon.com
Subject: Re: [PATCH v3 6/6] KVM: selftests: Add coalesced_mmio_test
Date: Wed, 28 Aug 2024 10:20:39 -0700	[thread overview]
Message-ID: <Zs9cZ1ome6AjJ5nc@google.com> (raw)
In-Reply-To: <20240820133333.1724191-7-ilstam@amazon.com>

On Tue, Aug 20, 2024, Ilias Stamatis wrote:
> Test the KVM_CREATE_COALESCED_MMIO_BUFFER, KVM_REGISTER_COALESCED_MMIO2
> and KVM_UNREGISTER_COALESCED_MMIO2 ioctls.
> 
> Signed-off-by: Ilias Stamatis <ilstam@amazon.com>
> ---
> +	/*
> +	 * Test that allocating an fd and memory mapping it works
> +	 */
> +	ring_fd = __vm_ioctl(vm, KVM_CREATE_COALESCED_MMIO_BUFFER, NULL);
> +	TEST_ASSERT(ring_fd != -1, "Failed KVM_CREATE_COALESCED_MMIO_BUFFER");
> +
> +	ring = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
> +		    ring_fd, 0);
> +	TEST_ASSERT(ring != MAP_FAILED, "Failed to allocate ring buffer");

If we end up with KVM providing the buffer, there needs to be negative tests to
do weird things with the mapping.

> +	/*
> +	 * Test that the first and last ring indices are zero
> +	 */
> +	TEST_ASSERT_EQ(READ_ONCE(ring->first), 0);
> +	TEST_ASSERT_EQ(READ_ONCE(ring->last), 0);
> +
> +	/*
> +	 * Run the vCPU and make sure the first MMIO write results in a
> +	 * userspace exit since we have not setup MMIO coalescing yet.
> +	 */
> +	vcpu_run(vcpu);
> +	assert_mmio_write(vcpu, MEM_REGION_GPA, MMIO_WRITE_DATA);
> +
> +	/*
> +	 * Let's actually setup MMIO coalescing now...
> +	 */
> +	zone.addr = COALESCING_ZONE1_GPA;
> +	zone.size = COALESCING_ZONE1_SIZE;
> +	zone.buffer_fd = ring_fd;
> +	r = __vm_ioctl(vm, KVM_REGISTER_COALESCED_MMIO2, &zone);
> +	TEST_ASSERT(r != -1, "Failed KVM_REGISTER_COALESCED_MMIO2");
> +
> +	/*
> +	 * The guest will start doing MMIO writes in the coalesced regions but
> +	 * will also do a ucall when the buffer is half full. The first
> +	 * userspace exit should be due to the ucall and not an MMIO exit.
> +	 */
> +	vcpu_run(vcpu);
> +	assert_ucall_exit(vcpu, UCALL_SYNC);
> +	TEST_ASSERT_EQ(READ_ONCE(ring->first), 0);
> +	TEST_ASSERT_EQ(READ_ONCE(ring->last), KVM_COALESCED_MMIO_MAX / 2 + 1);

For testing the content, I generally prefer my version of the test as it has
fewer magic values.  To prep for this likely/potential future, I'll post a v2
that wraps the ring+mmio+pio information into a structure that can be passed
around, e.g. to guest_code() and the runner+verifier.  And I'll also tweak it
to include the GPA/PORT in the value written, e.g. so that the test will detect
if streams get crossed and a write goes to the wrong buffer.

struct kvm_coalesced_io {
	struct kvm_coalesced_mmio_ring *ring;
	uint32_t ring_size;
	uint64_t mmio_gpa;
	uint64_t *mmio;
#ifdef __x86_64__
	uint8_t pio_port;
#endif
};


That way, the basic test for multiple buffers can simply spin up two vCPUs and
run them concurrently with different MMIO+PIO regions and thus different buffers.

If we want a test case that interleaves MMIO+PIO across multiple buffers on a
single vCPU, it shouldn't be too hard to massage things to work with two buffers,
but honestly I don't see that as being super interesting.

What would be more interesting, and probably should be added, is two vCPUs
accessing the same region concurrently, e.g. to verify the locking.  The test
wouldn't be able to verify the order, i.e. the data can't be checked without some
form of ordering in the guest code, but it'd be a good fuzzer to make sure KVM
doesn't explode.

      reply	other threads:[~2024-08-28 17:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-20 13:33 [PATCH v3 0/6] KVM: Improve MMIO Coalescing API Ilias Stamatis
2024-08-20 13:33 ` [PATCH v3 1/6] KVM: Fix coalesced_mmio_has_room() Ilias Stamatis
2024-08-24  0:04   ` Sean Christopherson
2024-08-20 13:33 ` [PATCH v3 2/6] KVM: Add KVM_CREATE_COALESCED_MMIO_BUFFER ioctl Ilias Stamatis
2024-08-28 14:25   ` Sean Christopherson
2024-08-29 11:20     ` Stamatis, Ilias
2024-08-29 14:55       ` Sean Christopherson
2024-08-29 17:42         ` Stamatis, Ilias
2024-08-20 13:33 ` [PATCH v3 3/6] KVM: Support poll() on coalesced mmio buffer fds Ilias Stamatis
2024-08-20 13:33 ` [PATCH v3 4/6] KVM: Add KVM_(UN)REGISTER_COALESCED_MMIO2 ioctls Ilias Stamatis
2024-08-20 13:33 ` [PATCH v3 5/6] KVM: Documentation: Document v2 of coalesced MMIO API Ilias Stamatis
2024-08-20 13:33 ` [PATCH v3 6/6] KVM: selftests: Add coalesced_mmio_test Ilias Stamatis
2024-08-28 17:20   ` 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=Zs9cZ1ome6AjJ5nc@google.com \
    --to=seanjc@google.com \
    --cc=dwmw@amazon.co.uk \
    --cc=ilstam@amazon.com \
    --cc=kvm@vger.kernel.org \
    --cc=nh-open-source@amazon.com \
    --cc=pbonzini@redhat.com \
    --cc=pdurrant@amazon.co.uk \
    /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