On Sun, Aug 19, 2018 at 08:17:45AM +0800, Wei Yang wrote: > update_memslots() is only called by __kvm_set_memory_region(), in which > change is calculated to indicate the following behavior. With this What is the 'following behavior' you mention? > information, it is not necessary to do the calculation again in > update_memslots(). > > By encoding the number of slot adjustment in the lowest byte of change, > the slots->used_slots adjustment is accomplished by one addition. > > Signed-off-by: Wei Yang <richard.weiyang@xxxxxxxxx> > --- > include/linux/kvm_host.h | 11 +++++++---- > virt/kvm/kvm_main.c | 14 ++++---------- > 2 files changed, 11 insertions(+), 14 deletions(-) > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > index 4ee7bc548a83..e0ae6cf82c0d 100644 > --- a/include/linux/kvm_host.h > +++ b/include/linux/kvm_host.h > @@ -610,12 +610,15 @@ id_to_memslot(struct kvm_memslots *slots, int id) > * > * Since flags can be changed by some of these operations, the following > * differentiation is the best we can do for __kvm_set_memory_region(): > + * > + * Encode the number of slot adjustment in the lowest byte, which will > + * be used in update_memslots(). > */ > enum kvm_mr_change { > - KVM_MR_CREATE, > - KVM_MR_DELETE, > - KVM_MR_MOVE, > - KVM_MR_FLAGS_ONLY, > + KVM_MR_CREATE = 0x0001, > + KVM_MR_DELETE = 0x00ff, > + KVM_MR_MOVE = 0x0000, > + KVM_MR_FLAGS_ONLY = 0x0100, > }; > > int kvm_set_memory_region(struct kvm *kvm, > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 3d233ebfbee9..5c1911790f25 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -801,21 +801,15 @@ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot) > * sorted array and known changed memslot position. > */ > static void update_memslots(struct kvm_memslots *slots, > - struct kvm_memory_slot *new) > + struct kvm_memory_slot *new, > + enum kvm_mr_change change) > { > int id = new->id; > int i = slots->id_to_index[id]; > struct kvm_memory_slot *mslots = slots->memslots; > > WARN_ON(mslots[i].id != id); > - if (!new->npages) { > - WARN_ON(!mslots[i].npages); > - if (mslots[i].npages) > - slots->used_slots--; > - } else { > - if (!mslots[i].npages) > - slots->used_slots++; > - } > + slots->used_slots += (char)change; > > while (i < KVM_MEM_SLOTS_NUM - 1 && > new->base_gfn <= mslots[i + 1].base_gfn) { > @@ -1050,7 +1044,7 @@ int __kvm_set_memory_region(struct kvm *kvm, > memset(&new.arch, 0, sizeof(new.arch)); > } > > - update_memslots(slots, &new); > + update_memslots(slots, &new, change); > old_memslots = install_new_memslots(kvm, as_id, slots); > > kvm_arch_commit_memory_region(kvm, mem, &old, &new, change); > -- > 2.15.1 >