From: David Woodhouse <dwmw2@infradead.org>
To: paul@xen.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Paul Durrant <pdurrant@amazon.com>,
Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
"H. Peter Anvin" <hpa@zytor.com>,
x86@kernel.org
Subject: Re: [PATCH v4 09/13] KVM: xen: automatically use the vcpu_info embedded in shared_info
Date: Tue, 19 Sep 2023 16:38:40 +0100 [thread overview]
Message-ID: <6b20173bae6bbf2de03c64c158198b351900f4ea.camel@infradead.org> (raw)
In-Reply-To: <451eebfe-1df5-4f02-2ce1-998560feaa98@xen.org>
[-- Attachment #1: Type: text/plain, Size: 4046 bytes --]
On Tue, 2023-09-19 at 15:34 +0100, Paul Durrant wrote:
> > > + ret = kvm_gpc_activate(vi_gpc, gpa, sizeof(struct vcpu_info));
> >
> > From this moment, can't interrupts be delivered to the new vcpu_info,
> > even though the memcpy hasn't happened yet?
> >
>
> Hmm, that's a good point. TBH it would be nice to have an 'activate and
> leave locked' primitive to avoid this.
I suppose so from the caller's point of view in this case, but I'm
somewhat disinclined to add that complexity to the pfncache code.
We take the refresh_lock *mutex* in __kvm_gpc_refresh() so it's not as
simple as declaring that said function is called with the gpc rwlock
already held.
We also do the final gpc_unmap_khva() of the old mapping after dropping
the lock; *could* we call that with a write lock held? A write lock
which is going to be taken the MM notifier callbacks? Well, maybe not
in the case of the first *activate* which isn't really a 'refresh' per
se but the whole thing is making my skin itch. I don't like it.
> > I think we need to ensure that any kvm_xen_set_evtchn_fast() which
> > happens at this point cannot proceed, and falls back to the slow path.
> >
> > Can we set a flag before we activate the vcpu_info and clear it after
> > the memcpy is done, then make kvm_xen_set_evtchn_fast() return
> > EWOULDBLOCK whenever that flag is set?
> >
> > The slow path in kvm_xen_set_evtchn() takes kvm->arch.xen.xen_lock and
> > I think kvm_xen_vcpu_set_attr() has taken that same lock before you get
> > to this code, so it works out nicely?
> >
>
> Yes, I think that is safe... but if we didn't have the window between
> activating the vcpu_info cache and doing the copy we'd also be ok I
> think... Or perhaps we could simply preserve evtchn_pending_sel and copy
> the rest of it?
>
I suppose you could just write the evtchn_pending_sel word in the new
vcpu_info GPA to zero before setting up the pfncache for it.
When when you do the memcpy, you don't *just* memcpy the
evtchn_pending_sel word; you use the bitwise OR of the old and new, so
you catch any bits which got set in the new word in the interim?
But then again, who moves the vcpu_info while there are actually
interrupts in-flight to the vCPU in question? Maybe we just declare
that we don't care, and that interrupts may be lost in that case? Even
if *Xen* wouldn't have lost them (and I don't even know that part is
true).
> > This adds a new lock ordering rule of the vcpu_info lock(s) before the
> > shared_info lock. I don't know that it's *wrong* but it seems weird to
> > me; I expected the shared_info to come first?
> >
> > I avoided taking both at once in kvm_xen_set_evtchn_fast(), although
> > maybe if we are going to have a rule that allows both, we could revisit
> > that. Suspect it isn't needed.
> >
> > Either way it is worth a clear comment somewhere to document the lock
> > ordering, and I'd also like to know this has been tested with lockdep,
> > which is often cleverer than me.
> >
>
> Ok. I agree that shared_info before vcpu_info does seem more intuitive
> and maybe it would be better given the code in
> kvm_xen_set_evtchn_fast(). I'll seem how messy it gets in re-ordering
> and add a comment as you suggest.
>
I think they look interchangeable in this case. If we *do* take them
both in kvm_xen_set_evtchn_fast() then maybe we can simplify the slow
path where it set the bits in shared_info but then the vcpu_info gpc
was invalid. That currently uses a kvm->arch.xen.evtchn_pending_sel
shadow of the bits, and just kicks the vCPU to deliver them for
itself... but maybe that whole thing could be dropped, and
kvm_xen_set_evtchn_fast() can just return EWOULDBLOCK if it fails to
lock *both* shared_info and vcpu_info at the same time?
I didn't do that before, because I didn't want to introduce lock
ordering rules. But I'm happier to do so now. And I think we can ditch
a lot of hairy asm in kvm_xen_inject_pending_events() ?
[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5965 bytes --]
next prev parent reply other threads:[~2023-09-19 15:38 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-19 13:41 [PATCH v4 00/13] KVM: xen: update shared_info and vcpu_info handling Paul Durrant
2023-09-19 13:41 ` [PATCH v4 01/13] KVM: pfncache: add a map helper function Paul Durrant
2023-09-19 13:41 ` [PATCH v4 02/13] KVM: pfncache: add a mark-dirty helper Paul Durrant
2023-09-19 13:41 ` [PATCH v4 03/13] KVM: pfncache: add a helper to get the gpa Paul Durrant
2023-09-19 13:41 ` [PATCH v4 04/13] KVM: pfncache: base offset check on khva rather than gpa Paul Durrant
2023-09-19 13:41 ` [PATCH v4 05/13] KVM: pfncache: allow a cache to be activated with a fixed (userspace) HVA Paul Durrant
2023-09-19 14:20 ` David Woodhouse
2023-09-19 14:22 ` Paul Durrant
2023-09-19 13:41 ` [PATCH v4 06/13] KVM: xen: allow shared_info to be mapped by fixed HVA Paul Durrant
2023-09-19 13:41 ` [PATCH v4 07/13] KVM: xen: prepare for using 'default' vcpu_info Paul Durrant
2023-09-19 13:41 ` [PATCH v4 08/13] KVM: xen: prevent vcpu_id from changing whilst shared_info is valid Paul Durrant
2023-09-19 13:41 ` [PATCH v4 09/13] KVM: xen: automatically use the vcpu_info embedded in shared_info Paul Durrant
2023-09-19 14:18 ` David Woodhouse
2023-09-19 14:34 ` Paul Durrant
2023-09-19 15:38 ` David Woodhouse [this message]
2023-09-19 15:47 ` Paul Durrant
2023-09-19 16:14 ` David Woodhouse
2023-09-19 13:41 ` [PATCH v4 10/13] KVM: selftests / xen: set KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID Paul Durrant
2023-09-19 13:41 ` [PATCH v4 11/13] KVM: selftests / xen: map shared_info using HVA rather than GFN Paul Durrant
2023-09-19 13:41 ` [PATCH v4 12/13] KVM: selftests / xen: don't explicitly set the vcpu_info address Paul Durrant
2023-09-19 13:41 ` [PATCH v4 13/13] KVM: xen: advertize the KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA capability Paul Durrant
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=6b20173bae6bbf2de03c64c158198b351900f4ea.camel@infradead.org \
--to=dwmw2@infradead.org \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=paul@xen.org \
--cc=pbonzini@redhat.com \
--cc=pdurrant@amazon.com \
--cc=seanjc@google.com \
--cc=tglx@linutronix.de \
--cc=x86@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
all inboxes | Powered by JetHome®