Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753027AbbHENZq (ORCPT ); Wed, 5 Aug 2015 09:25:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47488 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752824AbbHENYb (ORCPT ); Wed, 5 Aug 2015 09:24:31 -0400 From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= To: linux-kernel@vger.kernel.org Cc: kvm@vger.kernel.org, Paolo Bonzini Subject: [PATCH 4/5] KVM: optimize common cases in KVM_USER_EXIT Date: Wed, 5 Aug 2015 15:21:16 +0200 Message-Id: <1438780877-31838-5-git-send-email-rkrcmar@redhat.com> In-Reply-To: <1438780877-31838-1-git-send-email-rkrcmar@redhat.com> References: <1438780877-31838-1-git-send-email-rkrcmar@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2120 Lines: 54 VCPU with vcpu->vcpu_id has highest probability of being stored in kvm->vcpus[vcpu->vcpu_id]. Other common case, sparse sequential vcpu_id, is more likely to find a match downwards from vcpu->vcpu_id. Random distribution does not matter so we first search slots [vcpu->vcpu_id..0] and then slots (vcpu->vcpu_id..kvm->online_vcpus). If we value cycles over memory, a direct map between vcpu_id and vcpu->vcpu_id would be better. (Like kvm_for_each_vcpu, the code avoid the kvm->lock by presuming that kvm->online_vcpus doesn't shrink and that the vcpu pointer is set up before incrementing. kvm_free_vcpus() breaks that presumption, but vm is destroyed only after the fd has been released.) Signed-off-by: Radim Krčmář --- virt/kvm/kvm_main.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 024428b64812..7d532591d5af 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2650,7 +2650,7 @@ int kvm_vm_ioctl_user_exit(struct kvm *kvm, struct kvm_user_exit *info) * KVM_CREATE_VCPU, where we cast from unsigned long. */ int vcpu_id = info->vcpu_id; - int idx; + int idx, first; struct kvm_vcpu *vcpu; const struct kvm_user_exit valid = {.vcpu_id = info->vcpu_id}; @@ -2659,7 +2659,11 @@ int kvm_vm_ioctl_user_exit(struct kvm *kvm, struct kvm_user_exit *info) if (memcmp(info, &valid, sizeof(valid))) return -EINVAL; - kvm_for_each_vcpu(idx, vcpu, kvm) + for (idx = first = min(vcpu_id, atomic_read(&kvm->online_vcpus) - 1); + idx >= 0 ? (vcpu = kvm_get_vcpu(kvm, idx)) != NULL + : ++first < atomic_read(&kvm->online_vcpus) && + (vcpu = kvm_get_vcpu(kvm, first)) != NULL; + idx--) if (vcpu->vcpu_id == vcpu_id) { kvm_make_request(KVM_REQ_EXIT, vcpu); kvm_vcpu_kick(vcpu); -- 2.5.0 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/