Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1004906imm; Wed, 26 Sep 2018 10:03:49 -0700 (PDT) X-Google-Smtp-Source: ACcGV62U8KqWAre2SOoKnFEIoG1GVKW9Y/g5UXfIA+D3A1zAEv4JxRSEpSPhSiwlm04lonTyfh0X X-Received: by 2002:a62:59d5:: with SMTP id k82-v6mr7101974pfj.143.1537981429167; Wed, 26 Sep 2018 10:03:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537981429; cv=none; d=google.com; s=arc-20160816; b=Pu1/qQuq3HEnLWbhGRNj7XDRkV/b72cZbo3ZKQ8NXFJy2Wfnwno45st1nNpjRv1WcL tiBeiV1wODTjYRbqnRpOJ9SipL4pl46GmXjKoUNpG/RI2Xf/WwLaSq8TpTcZzs4TLp8G /C03WhaJh8vbEgNR4UuT0oDfhB4v8bM+KNI0Cl/VIcQilPq92WFcpo+9h0AP6IKuzEnI QLVJOftDmFX0ylaBzyjkeHqhvnaLnFYp4qQFHBiY4ct9DDMXeuKYt8OEjlATRBRhg/hx INJUf6zw8ItWx4wG7K5dc8N53xCI5q7brdmSA54ZrRzRuymnJBzL7Pyu+VLi2iHB52vI MGnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=gPN0tyE+ueoZqSzK1IMcALxz9iKUJZZGNhmh6MGlzI4=; b=jaEoCDGd58DLCn1be0/RrBBFi0NoJFzUs7LbUODynaOuKJFoN8GLbc3tCCRzPHY2eT 5Hs2n2lXM7ncnskSctcvfG6ht6e6KWc0dkwXvwFP4ML0xKLpzpQaH1s1Gr25Tc2RKoKI tOnKXB1GPVFacMnjJAwAGuJyPdwAIp4j4TNO3wRs/1bQC4KFPCnf7KVrFuIZmCAek8YK S7asmxLqNtzmJI/n19sH1+9mmQ6DhXX2I5YV5NP3C+KNyPBYIrBBKDVvAM6VGPCRGAvi rkYappLZZuw5uTlrCU2+olA/1IpDuHyqyBvKVjYAs3jTXZ1g7oFCE9oUh75pO+hh5BEQ ++Nw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o33-v6si5660138plb.489.2018.09.26.10.03.33; Wed, 26 Sep 2018 10:03:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728834AbeIZXRL (ORCPT + 99 others); Wed, 26 Sep 2018 19:17:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34948 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728645AbeIZXRK (ORCPT ); Wed, 26 Sep 2018 19:17:10 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6FF3130001E2; Wed, 26 Sep 2018 17:03:17 +0000 (UTC) Received: from vitty.brq.redhat.com (unknown [10.43.2.217]) by smtp.corp.redhat.com (Postfix) with ESMTP id DB0A660BE7; Wed, 26 Sep 2018 17:03:14 +0000 (UTC) From: Vitaly Kuznetsov To: kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Roman Kagan , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , "Michael Kelley (EOSG)" , Mohammed Gamal , Cathy Avery , Wanpeng Li , linux-kernel@vger.kernel.org Subject: [PATCH v6 4/7] KVM: x86: hyperv: keep track of mismatched VP indexes Date: Wed, 26 Sep 2018 19:02:56 +0200 Message-Id: <20180926170259.29796-5-vkuznets@redhat.com> In-Reply-To: <20180926170259.29796-1-vkuznets@redhat.com> References: <20180926170259.29796-1-vkuznets@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Wed, 26 Sep 2018 17:03:17 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In most common cases VP index of a vcpu matches its vcpu index. Userspace is, however, free to set any mapping it wishes and we need to account for that when we need to find a vCPU with a particular VP index. To keep search algorithms optimal in both cases introduce 'num_mismatched_vp_indexes' counter showing how many vCPUs with mismatching VP index we have. In case the counter is zero we can assume vp_index == vcpu_idx. Signed-off-by: Vitaly Kuznetsov --- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/hyperv.c | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 09b2e3e2cf1b..711f79f1b5e6 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -781,6 +781,9 @@ struct kvm_hv { u64 hv_reenlightenment_control; u64 hv_tsc_emulation_control; u64 hv_tsc_emulation_status; + + /* How many vCPUs have VP index != vCPU index */ + atomic_t num_mismatched_vp_indexes; }; enum kvm_irqchip_mode { diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index c8764faf783b..6a19c8e3c432 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -1045,11 +1045,31 @@ static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host) struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv; switch (msr) { - case HV_X64_MSR_VP_INDEX: - if (!host || (u32)data >= KVM_MAX_VCPUS) + case HV_X64_MSR_VP_INDEX: { + struct kvm_hv *hv = &vcpu->kvm->arch.hyperv; + int vcpu_idx = kvm_vcpu_get_idx(vcpu); + u32 new_vp_index = (u32)data; + + if (!host || new_vp_index >= KVM_MAX_VCPUS) return 1; - hv_vcpu->vp_index = (u32)data; + + if (new_vp_index == hv_vcpu->vp_index) + return 0; + + /* + * VP index is changing, increment num_mismatched_vp_indexes in + * case it was equal to vcpu_idx before; on the other hand, if + * the new VP index matches vcpu_idx num_mismatched_vp_indexes + * needs to be decremented. + */ + if (hv_vcpu->vp_index == vcpu_idx) + atomic_inc(&hv->num_mismatched_vp_indexes); + else if (new_vp_index == vcpu_idx) + atomic_dec(&hv->num_mismatched_vp_indexes); + + hv_vcpu->vp_index = new_vp_index; break; + } case HV_X64_MSR_VP_ASSIST_PAGE: { u64 gfn; unsigned long addr; -- 2.17.1