Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp4052491pxv; Tue, 13 Jul 2021 09:38:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzQaGq5bu7fwhCPvOwLiBaahZG56eHetAotR9gairxyK0c5iIaDAOT8Ib0xWOFRzbKNhoiq X-Received: by 2002:a17:906:4f14:: with SMTP id t20mr6595081eju.12.1626194308617; Tue, 13 Jul 2021 09:38:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626194308; cv=none; d=google.com; s=arc-20160816; b=EpHn+aq1Y36YS7GNk73iifEN7XWURny/flcHr9uUTaCzr/UJIBUShMfIUspJ09YqAG x29eLCKJd62xkWl5nC8KFz9tYQsMVJouY7g2iW1R7BTCZNJ//S9LyPOnPDxL+w3sQlx7 Nww3u5Ev1A+sXp/FwQcqeFRBScnILbz9jILHhBCW5bCVUaPzI/BQOnl5o7Mu10WN16An TAquBQOiOdt4R9wJQ8EeuGikpiHM0LieIKv57qrvKzCddCb/eJ+uK8doJowz1YVDfZ9U aIULSOzakyeCIywinMuROpuEtlSYbIXxDR5apXepi8lGHyH3sdnG+3WD1g11Esv6HLpi KB0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=+rqcgnm7H23kDtzI+NHHhjYq+hDDRi5POz7LV9x1q80=; b=Qxbxy1eXK7Ezf133qwj8aAPyMDrOnko1WkiYPyxJuQbsxURuBC7kMblHDUoCrJVrfN NVaBLB2os5EOuoDvVZcLT/jCSlCsovcETGnMc8T/0FSAm1xzal0HQpM9/8XPJjvAFw/r h4GWBR2xtbhy/CzF/LX51OsDywM9BoaJ6VM993PwpG0gVyrJtY9INF6Oao1Xe4p75DHW tUh9YHelB3e6vFWefhroHtoq9jTjT6EpO7U4ayLIgdOEXzFMjSDp1F4OGw7GgatqE0T6 dztG0ctIf/p8jTWwayEGzlbCGsKOJLYIrgZfUDx9oJ8dz2xqNgXSiR3ps2C8Q1/a6Mq5 9DJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EUJbXsjQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x22si20097564edr.450.2021.07.13.09.38.02; Tue, 13 Jul 2021 09:38:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EUJbXsjQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235423AbhGMQil (ORCPT + 99 others); Tue, 13 Jul 2021 12:38:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234733AbhGMQie (ORCPT ); Tue, 13 Jul 2021 12:38:34 -0400 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A667C0613AD for ; Tue, 13 Jul 2021 09:34:54 -0700 (PDT) Received: by mail-qk1-x749.google.com with SMTP id k63-20020a37a1420000b02903b4fb67f606so17336965qke.10 for ; Tue, 13 Jul 2021 09:34:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=+rqcgnm7H23kDtzI+NHHhjYq+hDDRi5POz7LV9x1q80=; b=EUJbXsjQJoA5ET64LXIKu5IwjyTv2OWjBqPQfKNl2ofUWNM0hNyDZH5Ne3c9P89XsG 6oSPkScJ1YGYbjScYTuHIfTGFWtPqSyAADXT662JDTvVUnz6RyTkTDWuYvgE/G4sZ7t2 FWXnX0XyxBe4Qfb+XxY0AfGU8pOpLbIZho0UE48yoGm4MNJijRm3yuEudWA5VGc4HFZh OxElfscJXYpk7c+b5m044mL6z041YBXvHwhE4hGHHEM+uwaX5geQF5G1QhTXn3QPQCKt mZllRrXhxZZ0qCds7XN9/LtRHF4xjMRfFzH5Wzdwdb/h5KmM03LV+VUADPwvuSM+5b2b j1xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=+rqcgnm7H23kDtzI+NHHhjYq+hDDRi5POz7LV9x1q80=; b=gTPBufkh1hQM46L5C/Yw2ISwORHbRIx+4BEoljRFTWKyJKM3NjD+w+AAZkypyNEAXd Hatt6MrUvYfFl5el+PA/8GbnYmvtKbbpy3jGJRcPJVT93gm1TjQyxSYGSH6NaTU7gsXi On6bLD0KWIF3M4FlYXT8aFxm/pUs4yu4hNWIU9NYAYrtarIzjJr3LTfdMGzm89PyH9je s3P+XNoQa1zHT7/CGauf9WYJnsHoWRnKv4uS7nT9Lb+WJRFZpLFLx7jCRxOEq33ECbyB OBp9Ba8RHX6LJgQ6tVK5Y3Yh2ieUpOdNdGvo5d78W9Jp5jP3gjqMu3e0DL4x3oJGkyHh EWIw== X-Gm-Message-State: AOAM5312LesIgBpt9C9p46u9k8pPpXqGdT8NoCno1WMHMrDXIB5suTFz rsso3/HYwqKRGJF+DHvzAc0aCorNgCs= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a05:6214:1021:: with SMTP id k1mr5843936qvr.4.1626194093657; Tue, 13 Jul 2021 09:34:53 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:19 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-42-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 41/46] KVM: VMX: Smush x2APIC MSR bitmap adjustments into single function From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Reiji Watanabe Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Consolidate all of the dynamic MSR bitmap adjustments into vmx_update_msr_bitmap_x2apic(), and rename the mode tracker to reflect that it is x2APIC specific. If KVM gains more cases of dynamic MSR pass-through, odds are very good that those new cases will be better off with their own logic, e.g. see Intel PT MSRs and MSR_IA32_SPEC_CTRL. Attempting to handle all updates in a common helper did more harm than good, as KVM ended up collecting a large number of useless "updates". Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 55 ++++++++++++++++-------------------------- arch/x86/kvm/vmx/vmx.h | 2 +- 2 files changed, 22 insertions(+), 35 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index bc09a2f7cb5f..cdde1dfaa574 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -3812,21 +3812,6 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type) vmx_set_msr_bitmap_write(msr_bitmap, msr); } -static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) -{ - u8 mode = 0; - - if (cpu_has_secondary_exec_ctrls() && - (secondary_exec_controls_get(to_vmx(vcpu)) & - SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { - mode |= MSR_BITMAP_MODE_X2APIC; - if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) - mode |= MSR_BITMAP_MODE_X2APIC_APICV; - } - - return mode; -} - static void vmx_reset_x2apic_msrs(struct kvm_vcpu *vcpu, u8 mode) { unsigned long *msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; @@ -3844,11 +3829,29 @@ static void vmx_reset_x2apic_msrs(struct kvm_vcpu *vcpu, u8 mode) } } -static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu, u8 mode) +static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu) { + struct vcpu_vmx *vmx = to_vmx(vcpu); + u8 mode; + if (!cpu_has_vmx_msr_bitmap()) return; + if (cpu_has_secondary_exec_ctrls() && + (secondary_exec_controls_get(vmx) & + SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { + mode = MSR_BITMAP_MODE_X2APIC; + if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) + mode |= MSR_BITMAP_MODE_X2APIC_APICV; + } else { + mode = 0; + } + + if (!(mode ^ vmx->x2apic_msr_bitmap_mode)) + return; + + vmx->x2apic_msr_bitmap_mode = mode; + vmx_reset_x2apic_msrs(vcpu, mode); /* @@ -3865,21 +3868,6 @@ static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu, u8 mode) } } -static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) -{ - struct vcpu_vmx *vmx = to_vmx(vcpu); - u8 mode = vmx_msr_bitmap_mode(vcpu); - u8 changed = mode ^ vmx->msr_bitmap_mode; - - if (!changed) - return; - - if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) - vmx_update_msr_bitmap_x2apic(vcpu, mode); - - vmx->msr_bitmap_mode = mode; -} - void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); @@ -4139,8 +4127,7 @@ static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); } - if (cpu_has_vmx_msr_bitmap()) - vmx_update_msr_bitmap(vcpu); + vmx_update_msr_bitmap_x2apic(vcpu); } u32 vmx_exec_control(struct vcpu_vmx *vmx) @@ -6186,7 +6173,7 @@ void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) } secondary_exec_controls_set(vmx, sec_exec_control); - vmx_update_msr_bitmap(vcpu); + vmx_update_msr_bitmap_x2apic(vcpu); } static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 1b3dd5ddf235..e370091d57c6 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -229,7 +229,7 @@ struct nested_vmx { struct vcpu_vmx { struct kvm_vcpu vcpu; u8 fail; - u8 msr_bitmap_mode; + u8 x2apic_msr_bitmap_mode; /* * If true, host state has been stored in vmx->loaded_vmcs for -- 2.32.0.93.g670b81a890-goog