On 19/08/2018 02:17, Wei Yang wrote: > This is a trivial optimization of the first loop in update_memslots(). > > Since used_slots records the number of valid slots, it could be used as the > boundary of the loop instead of looping the whole array and check npages. > > Signed-off-by: Wei Yang <richard.weiyang@xxxxxxxxx> > --- > virt/kvm/kvm_main.c | 6 ++---- > 1 file changed, 2 insertions(+), 4 deletions(-) > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 5c1911790f25..fac3225eff35 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -806,15 +806,13 @@ static void update_memslots(struct kvm_memslots *slots, > { > int id = new->id; > int i = slots->id_to_index[id]; > + int used_slots = slots->used_slots; > struct kvm_memory_slot *mslots = slots->memslots; > > WARN_ON(mslots[i].id != id); > slots->used_slots += (char)change; > > - while (i < KVM_MEM_SLOTS_NUM - 1 && > - new->base_gfn <= mslots[i + 1].base_gfn) { > - if (!mslots[i + 1].npages) > - break; > + while (i < used_slots - 1 && new->base_gfn <= mslots[i + 1].base_gfn) { > mslots[i] = mslots[i + 1]; > slots->id_to_index[mslots[i].id] = i; > i++; > The loop is removing an element from the array, so the correct limit for the index would be the *old* length of the array, not the new one. Alternatively... just keep the current code. :) It is already loading mslots[i+1] to get the base_gfn, so the load is not expensive. This is also not really a fast path. Paolo