From: Yan Zhao <yan.y.zhao@intel.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
David Hildenbrand <david@kernel.org>, <kvm@vger.kernel.org>,
<linux-kernel@vger.kernel.org>,
Stefan Teodorescu <fane@google.com>,
Dennis Tighe <dtighe@google.com>,
Sashiko Bot <sashiko-bot@kernel.org>,
Ackerley Tng <ackerleytng@google.com>
Subject: Re: [PATCH v5 6/6] KVM: guest_memfd: Drop superfluous WRITE_ONCE() when binding a memslot
Date: Thu, 24 Sep 2026 17:21:53 +0800 [thread overview]
Message-ID: <arTrsZGayO7U0Jjv@yzhao56-desk.sh.intel.com> (raw)
In-Reply-To: <arPhfHnMNbvthwGu@google.com>
On Wed, Sep 23, 2026 at 07:26:04AM -0700, Sean Christopherson wrote:
> On Wed, Sep 23, 2026, Yan Zhao wrote:
> > On Tue, Sep 22, 2026 at 06:47:05AM -0700, Sean Christopherson wrote:
> > > On Tue, Sep 22, 2026, Yan Zhao wrote:
> > > > The WRITE_ONCE() in kvm_gmem_unbind() and kvm_gmem_release() are also invoked
> > > > when the memslot is inactive and unreachable -- are they also superfluous?
> > >
> > > The WRITE_ONCE() in release() is necessary, because the file could be freed/released
> > > while it is still attached to a memslot.
> > Ah, release() can occur on an active memslot, so WRITE_ONCE() is needed to
> > ensure the READ_ONCE() in get_file_active() works correctly.
>
> Yep.
>
> > > I _think_ the one in unbind() is now superfluous after 0ee2c883b62d ("KVM:
> > > guest_memfd: take the invalidate lock when unbinding a dying file"), but that one
> > > needs more analysis.
> > Hmm, the line "CLASS(gmem_get_file, file)(slot)" in kvm_gmem_get_pfn() is not
> > protected by the invalidate lock.
>
> CLASS(gmem_get_file) can never be protected by the invalidate lock. Or rather,
> doing CLASS(gmem_get_file) while holding the invalidate lock is nonsensical,
> because taking the lock requires a reference to the inode, and if you have a
> (stable) reference to the inode, there's no reason to get a reference to a file.
Yes, I mentioned that with the hope of proving that the invalidate lock does not
make unbind() safer about dropping WRITE_ONCE(). :)
> > It should be superfluous even before commit 0ee2c883b62d, since "the caller is
> > responsible for ensuring the slot is unreachable before unbinding" ?
>
> Yes, I just haven't spent enough time thinking about it to be 100% confident :-)
>
> > > > Do we need the READ_ONCE() in __kvm_gmem_get_pfn(), considering that other slot
> > > > fields (e.g., slot->gmem.pgoff) are read without READ_ONCE()?
> > >
> > > Yes, it's needed, because of the aforementioned release(). The other slot fields
> > > are only ever modified when the slot is inactive, i.e. unreachable. That's why
> > > I think the unbind() WRITE_ONCE() is unnecessary; KVM should only unbind when the
> > > slot is inactive.
> > Maybe the READ_ONCE() in __kvm_gmem_get_pfn() is not necessary?
> > When __kvm_gmem_get_pfn() is invoked, a file refcount must have been taken, so a
> > concurent release() is not possible.
>
> No? KVM doesn't hold a reference to the file. Oooh, you're not talking about a
> long-term reference, you're talking about the reference acquired by kvm_gmem_get_file().
>
> Oh, duh. That READ_ONCE() is purely for a sanity check.
>
> struct file *slot_file = READ_ONCE(slot->gmem.file);
>
> ...
>
> if (file != slot_file) {
> WARN_ON_ONCE(slot_file);
> return ERR_PTR(-EFAULT);
> }
>
> So it's not strictly necessary, but since the entire point is to verify the slot
> pointer hasn't been clobbered, we do want the READ_ONCE() to guarantee the check
> is actually performed as intended.
Makes sense!
> But given that it should be impossible for release() to run concurrently (see
> above), and should be impossible for kvm_gmem_unbind() to run on a live memslot,
> then I'm pretty sure we can do this:
>
> diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
> index a13445c26d9d..1aeffb5bd2b0 100644
> --- a/virt/kvm/guest_memfd.c
> +++ b/virt/kvm/guest_memfd.c
> @@ -1130,14 +1130,11 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file,
> pgoff_t index, kvm_pfn_t *pfn,
> int *max_order)
> {
> - struct file *slot_file = READ_ONCE(slot->gmem.file);
> struct gmem_file *f = file->private_data;
> struct folio *folio;
>
> - if (file != slot_file) {
> - WARN_ON_ONCE(slot_file);
> + if (WARN_ON_ONCE(file != READ_ONCE(slot->gmem.file)))
> return ERR_PTR(-EFAULT);
> - }
>
> if (xa_load(&f->bindings, index) != slot) {
> WARN_ON_ONCE(xa_load(&f->bindings, index));
>
LGTM.
> Or just drop the check entirely? But I think it's worth keeping the check,
> especially since __kvm_gmem_get_pfn() will run under the invalidate lock once
> in-place conversion lands (see "KVM: guest_memfd: Introduce per-gmem attributes,
> use to guard user mappings"). At that point, the check would actually provide
> meaningful protection against KVM bugs.
I see. Thanks for the explanation!
next prev parent reply other threads:[~2026-09-24 9:22 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-22 0:13 [PATCH v5 0/6] KVM: guest_memfd: Fix binding bugs Sean Christopherson
2026-09-22 0:13 ` [PATCH v5 1/6] KVM: guest_memfd: Gracefully handle xarray errors when binding a memslot Sean Christopherson
2026-09-22 0:13 ` [PATCH v5 2/6] KVM: Use goto to handle errors during memslot preparation Sean Christopherson
2026-09-22 0:13 ` [PATCH v5 3/6] KVM: Only bind memslot to guest_memfd instance for CREATE operations Sean Christopherson
2026-09-22 8:15 ` David Hildenbrand (Arm)
2026-09-22 0:13 ` [PATCH v5 4/6] KVM: guest_memfd: Split bind() into prepare()+commit() phases Sean Christopherson
2026-09-22 12:01 ` David Hildenbrand (Arm)
2026-09-24 16:52 ` Ackerley Tng
2026-09-24 19:21 ` Sean Christopherson
2026-09-24 19:31 ` Sean Christopherson
2026-09-24 19:40 ` Sean Christopherson
2026-09-24 20:18 ` Ackerley Tng
2026-09-22 0:13 ` [PATCH v5 5/6] KVM: guest_memfd: Establish memslot<=>guest_memfd bindings *after* memslot is ready Sean Christopherson
2026-09-24 16:54 ` Ackerley Tng
2026-09-22 0:13 ` [PATCH v5 6/6] KVM: guest_memfd: Drop superfluous WRITE_ONCE() when binding a memslot Sean Christopherson
2026-09-22 11:53 ` Yan Zhao
2026-09-22 13:47 ` Sean Christopherson
2026-09-23 4:51 ` Yan Zhao
2026-09-23 14:26 ` Sean Christopherson
2026-09-24 9:21 ` Yan Zhao [this message]
2026-09-24 21:51 ` [PATCH v5 0/6] KVM: guest_memfd: Fix binding bugs 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=arTrsZGayO7U0Jjv@yzhao56-desk.sh.intel.com \
--to=yan.y.zhao@intel.com \
--cc=ackerleytng@google.com \
--cc=david@kernel.org \
--cc=dtighe@google.com \
--cc=fane@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=sashiko-bot@kernel.org \
--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®