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++; -- 2.15.1