Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp927033ybe; Fri, 13 Sep 2019 08:26:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqx1N3c1xEMCAAash4LK0OX3gKDKI8s4Id9KgHhQxmh4XkzccfrRG20xDb56TORANr1A9+yg X-Received: by 2002:a05:6402:332:: with SMTP id q18mr49322673edw.149.1568388416223; Fri, 13 Sep 2019 08:26:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568388416; cv=none; d=google.com; s=arc-20160816; b=d9409YaWQS1+yhtxYmftRNeM6zzaLTSkQ7yZkjcdMIcan2qym2/6asfngs0pN757bB wERbe9J/xBqIVR8y3RLYXTdtq7NU2ulGsGimgTyY+ElhKbUWHKdbKhXsW0bjf+wU26cb 83dr46Ko1I966VUeIbFNlkenN4CObwkBe/ekTKIy7FTtXuzwFZGtqlLHA89rPFaxGTvz Tlg7N1ty6Uzh4BSaWD/UHsrf9J/xpn6090iy9TYnuuWvTN5I9NXofaMZkrWl5Aw2GlbR SwDCVrBt6MdRMInL4BJQEVPDVZF+683+s6LhRWqXuU1seoFhdn96JCToXUMNmpq/iYNF F3Ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=BqN7Eha0cwUO+2b38r6cOT3v6EHcOzW8JsUGCrxcdC0=; b=V9czdfodT6xJeQs4lVl+20oqqLyDLVSNlu0/YF5qGf2thZESBTgTT7q5WpgMORZEM/ OH2ICBvxbomlIE+7mG9X8WoyqhUAFfaj4BoipkIROEcMbMv2tDCV+U0gbcyxbvkOt4UT twxoBs4RX5tOLQDfWWlFG8Sbe7dB3dOxsbK3nGRBVkpsqNLkqrxE52e/z66aSiGuiLnd zD8VkqisDrum5h0kvokVd+WYdU2NqBMxWHFTdie5g/Hn5To41nNFXwQWJdNGTvqdHlj5 VPp3NLrqxtpb+wwYK2yjtNNHgZO2Zw12tHOK5zSDUbHXq/UHHK7MW4+K/Ka7sga0Lylz jsDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dfJ5xvM0; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j35si17993773eda.144.2019.09.13.08.26.32; Fri, 13 Sep 2019 08:26:56 -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; dkim=pass header.i=@kernel.org header.s=default header.b=dfJ5xvM0; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391723AbfIMNcV (ORCPT + 99 others); Fri, 13 Sep 2019 09:32:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:38862 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389286AbfIMNNN (ORCPT ); Fri, 13 Sep 2019 09:13:13 -0400 Received: from localhost (unknown [104.132.45.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E25E0208C0; Fri, 13 Sep 2019 13:13:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1568380392; bh=sGSm+WEEazJKMqmflV8kB6JhIDapV9tpfMGzwrd4KZM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dfJ5xvM0oRp2Qt0qmjr6yJIiJUpIipcEIE8uFU/l1DxnpDUH8v9GnguxUNVsr9baV h5Oii9Gy0W8c4HOKNhc+6wZ6dfoC8mJUOZYwtLM2c0eWrqYeJPMV3PK5kmMO7kBS5+ H98vuTdNl7nLcGT45ciUq2wz2UQUsi0yj2yz8CSs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Vitaly Kuznetsov , Roman Kagan , Paolo Bonzini , Sasha Levin Subject: [PATCH 4.19 042/190] KVM: x86: hyperv: keep track of mismatched VP indexes Date: Fri, 13 Sep 2019 14:04:57 +0100 Message-Id: <20190913130603.107888371@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190913130559.669563815@linuxfoundation.org> References: <20190913130559.669563815@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 87ee613d076351950b74383215437f841ebbeb75 ] 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 Reviewed-by: Roman Kagan Signed-off-by: Paolo Bonzini Signed-off-by: Sasha Levin --- 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 3245b95ad2d97..b6417454a9d79 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -784,6 +784,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 3f2775aac5545..2bb554b90b3c2 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; + + /* + * The VP index is initialized to vcpu_index by + * kvm_hv_vcpu_postcreate so they initially match. Now the + * VP index is changing, adjust num_mismatched_vp_indexes if + * it now matches or no longer matches vcpu_idx. + */ + 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.20.1