From: "Stamatis, Ilias" <ilstam@amazon.co.uk>
To: "pbonzini@redhat.com" <pbonzini@redhat.com>,
"seanjc@google.com" <seanjc@google.com>
Cc: "maz@kernel.org" <maz@kernel.org>,
"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
"Stamatis, Ilias" <ilstam@amazon.co.uk>,
"anup@brainfault.org" <anup@brainfault.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"oliver.upton@linux.dev" <oliver.upton@linux.dev>,
"paul@xen.org" <paul@xen.org>
Subject: Re: [PATCH 2/2] KVM: Clean up coalesced MMIO ring full check
Date: Tue, 27 Aug 2024 09:56:54 +0000 [thread overview]
Message-ID: <f8ce24507b836170e1ca5df8586a3478c164386d.camel@amazon.co.uk> (raw)
In-Reply-To: <20240823191354.4141950-3-seanjc@google.com>
On Fri, 2024-08-23 at 12:13 -0700, Sean Christopherson wrote:
> Fold coalesced_mmio_has_room() into its sole caller, coalesced_mmio_write(),
> as it's really just a single line of code, has a goofy return value, and
> is unnecessarily brittle.
>
> E.g. if coalesced_mmio_has_room() were to check ring->last directly, or
> the caller failed to use READ_ONCE(), KVM would be susceptible to TOCTOU
> attacks from userspace.
>
> Opportunistically add a comment explaining why on earth KVM leaves one
> entry free, which may not be obvious to readers that aren't famailiar with
s/famailiar/familiar
> ring buffers.
>
> No functional change intended.
>
> Cc: Ilias Stamatis <ilstam@amazon.com>
> Cc: Paul Durrant <paul@xen.org>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
> virt/kvm/coalesced_mmio.c | 29 ++++++++---------------------
> 1 file changed, 8 insertions(+), 21 deletions(-)
>
> diff --git a/virt/kvm/coalesced_mmio.c b/virt/kvm/coalesced_mmio.c
> index 184c5c40c9c1..375d6285475e 100644
> --- a/virt/kvm/coalesced_mmio.c
> +++ b/virt/kvm/coalesced_mmio.c
> @@ -40,25 +40,6 @@ static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
> return 1;
> }
>
> -static int coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev *dev, u32 last)
> -{
> - struct kvm_coalesced_mmio_ring *ring;
> -
> - /* Are we able to batch it ? */
> -
> - /* last is the first free entry
> - * check if we don't meet the first used entry
> - * there is always one unused entry in the buffer
> - */
> - ring = dev->kvm->coalesced_mmio_ring;
> - if ((last + 1) % KVM_COALESCED_MMIO_MAX == READ_ONCE(ring->first)) {
> - /* full */
> - return 0;
> - }
> -
> - return 1;
> -}
> -
> static int coalesced_mmio_write(struct kvm_vcpu *vcpu,
> struct kvm_io_device *this, gpa_t addr,
> int len, const void *val)
> @@ -72,9 +53,15 @@ static int coalesced_mmio_write(struct kvm_vcpu *vcpu,
>
> spin_lock(&dev->kvm->ring_lock);
>
> + /*
> + * last is the index of the entry to fill. Verify userspace hasn't
> + * set last to be out of range, and that there is room in the ring.
> + * Leave one entry free in the ring so that userspace can differentiate
> + * between an empty ring and a full ring.
> + */
> insert = READ_ONCE(ring->last);
> - if (!coalesced_mmio_has_room(dev, insert) ||
> - insert >= KVM_COALESCED_MMIO_MAX) {
> + if (insert >= KVM_COALESCED_MMIO_MAX ||
> + (insert + 1) % KVM_COALESCED_MMIO_MAX == READ_ONCE(ring->first)) {
> spin_unlock(&dev->kvm->ring_lock);
> return -EOPNOTSUPP;
> }
> --
> 2.46.0.295.g3b9ea8a38a-goog
>
Reviewed-by: Ilias Stamatis <ilstam@amazon.com>
prev parent reply other threads:[~2024-08-27 9:56 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-23 19:13 [PATCH 0/2] KVM: Coalesced IO cleanup and test Sean Christopherson
2024-08-23 19:13 ` [PATCH 1/2] KVM: selftests: Add a test for coalesced MMIO (and PIO on x86) Sean Christopherson
2024-08-27 11:15 ` Stamatis, Ilias
2024-08-27 14:58 ` Sean Christopherson
2024-08-23 19:13 ` [PATCH 2/2] KVM: Clean up coalesced MMIO ring full check Sean Christopherson
2024-08-27 9:56 ` Stamatis, Ilias [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=f8ce24507b836170e1ca5df8586a3478c164386d.camel@amazon.co.uk \
--to=ilstam@amazon.co.uk \
--cc=anup@brainfault.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maz@kernel.org \
--cc=oliver.upton@linux.dev \
--cc=paul@xen.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.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
all inboxes | Powered by JetHome®