Re: [PATCH v5 01/19] KVM: x86: Allocate new rmap and large page tracking when moving memslot

Sean Christopherson
2020-02-06T05:05:23Z

On Wed, Feb 05, 2020 at 09:58:58PM -0500, Peter Xu wrote:
> On Wed, Feb 05, 2020 at 06:17:15PM -0800, Sean Christopherson wrote:
> > On Wed, Feb 05, 2020 at 09:00:31PM -0500, Peter Xu wrote:
> > > On Wed, Feb 05, 2020 at 03:55:33PM -0800, Sean Christopherson wrote:
> > > > On Wed, Feb 05, 2020 at 04:49:52PM -0500, Peter Xu wrote:
> > > > > Instead of calling kvm_arch_create_memslot() explicitly again here,
> > > > > can it be replaced by below?
> > > > >
> > > > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> > > > > index 72b45f491692..85a7b02fd752 100644
> > > > > --- a/virt/kvm/kvm_main.c
> > > > > +++ b/virt/kvm/kvm_main.c
> > > > > @@ -1144,7 +1144,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
> > > > > new.dirty_bitmap = NULL;
> > > > >
> > > > > r = -ENOMEM;
> > > > > - if (change == KVM_MR_CREATE) {
> > > > > + if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) {
> > > > > new.userspace_addr = mem->userspace_addr;
> > > > >
> > > > > if (kvm_arch_create_memslot(kvm, &new, npages))
> > > >
> > > > No, because other architectures don't need to re-allocate new metadata on
> > > > MOVE and rely on __kvm_set_memory_region() to copy @arch from old to new,
> > > > e.g. see kvmppc_core_create_memslot_hv().
> > >
> > > Yes it's only required in x86, but iiuc it also will still work for
> > > ppc? Say, in that case ppc won't copy @arch from old to new, and
> > > kvmppc_core_free_memslot_hv() will free the old, however it should
> > > still work.
> >
> > No, calling kvm_arch_create_memslot() for MOVE will result in PPC leaking
> > memory due to overwriting slot->arch.rmap with a new allocation.
>
> Why? For the MOVE case, kvm_arch_create_memslot() will create a new
> rmap for the "new" memslot. If the whole procedure succeeded,
> kvm_free_memslot() will free the old rmap. If it failed,
> kvm_free_memslot() will free the new rmap if !NULL. Looks fine?

Oh, I see what you're suggesting. Please god no.

This is a bug fix that needs to be backported to stable. Arbitrarily
changing PPC behavior is a bad idea, especially since I don't know squat
about the PPC rmap behavior.

If it happens to fix a PPC rmap bug, then PPC should get an explicit fix.
If it's not a bug fix, then at best it is a minor performance hit due to an
extra allocation and the need to refill the rmap. Worst case scenario it
breaks PPC.

And unless this were a temporary change, which would be silly, I would have
to carry forward the change into "KVM: PPC: Move memslot memory allocation
into prepare_memory_region()", and again, I don't know squat about PPC.

I also don't want to effectively introduce a misnamed function, even if
only temporarily, e.g. it's kvm_arch_create_memslot(), not
kvm_arch_create_or_move_memslot(), because the whole flow gets reworked a
few patches later.