From patchwork Mon Jun 24 09:58:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: EwanHai X-Patchwork-Id: 13709262 Received: from mx1.zhaoxin.com (MX1.ZHAOXIN.COM [210.0.225.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEA23130A79 for ; Mon, 24 Jun 2024 09:58:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=210.0.225.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719223104; cv=none; b=OXw+aiuGoruxtKNcr7ZX9pSMo4wkpiRrC6o/FGjL20Y6B2KGy0ZKTkApakB2gxT4qj0RFnf0mn46Nz3U0+GAGXtUZ+idOp3+QiDGehr4qEkcLz1kjjHR3j+KM1aqd1j+VI0yxfyAUKMJZ87JhdNcXB3KA++eJPyapexK/wve368= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719223104; c=relaxed/simple; bh=qAu29C6X9tiJQihj331O74HEbD6E6pc+9XIU0vupD0U=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=P6fxOFI5q5No2KBeQWFo4USrBdboaBMQUpz14L0sNv/xvWjmbEEBeLppgKKIE+GhKwiqPhruMAu0buKVa79GPpm9rhOpiSkZJMlR5IqimCDr4V2REvYxa1irffbtQhvM54fwAxfJ7qHCaZ25hzbbnEsRTJJjsjb2NNPya+xuCas= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zhaoxin.com; spf=pass smtp.mailfrom=zhaoxin.com; arc=none smtp.client-ip=210.0.225.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=zhaoxin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zhaoxin.com X-ASG-Debug-ID: 1719223087-086e231108132540001-HEqcsx Received: from ZXSHMBX2.zhaoxin.com (ZXSHMBX2.zhaoxin.com [10.28.252.164]) by mx1.zhaoxin.com with ESMTP id nChhy2AYNOf1fh1A (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO); Mon, 24 Jun 2024 17:58:07 +0800 (CST) X-Barracuda-Envelope-From: EwanHai-oc@zhaoxin.com X-Barracuda-RBL-Trusted-Forwarder: 10.28.252.164 Received: from zxbjmbx1.zhaoxin.com (10.29.252.163) by ZXSHMBX2.zhaoxin.com (10.28.252.164) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Mon, 24 Jun 2024 17:58:07 +0800 Received: from ewan-server.zhaoxin.com (10.28.66.62) by zxbjmbx1.zhaoxin.com (10.29.252.163) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Mon, 24 Jun 2024 17:58:06 +0800 X-Barracuda-RBL-Trusted-Forwarder: 10.28.252.164 From: EwanHai X-Barracuda-RBL-Trusted-Forwarder: 10.29.252.163 To: CC: , , , , Subject: [PATCH v3] target/i386/kvm: Refine VMX controls setting for backward compatibility Date: Mon, 24 Jun 2024 05:58:06 -0400 X-ASG-Orig-Subj: [PATCH v3] target/i386/kvm: Refine VMX controls setting for backward compatibility Message-ID: <20240624095806.214525-1-ewanhai-oc@zhaoxin.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: zxbjmbx1.zhaoxin.com (10.29.252.163) To zxbjmbx1.zhaoxin.com (10.29.252.163) X-Barracuda-Connect: ZXSHMBX2.zhaoxin.com[10.28.252.164] X-Barracuda-Start-Time: 1719223087 X-Barracuda-Encrypted: ECDHE-RSA-AES128-GCM-SHA256 X-Barracuda-URL: https://10.28.252.35:4443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at zhaoxin.com X-Barracuda-Scan-Msg-Size: 2880 X-Barracuda-BRTS-Status: 1 X-Barracuda-Bayes: INNOCENT GLOBAL 0.0000 1.0000 -2.0210 X-Barracuda-Spam-Score: -2.02 X-Barracuda-Spam-Status: No, SCORE=-2.02 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=9.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.126686 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- Commit 4a910e1 ("target/i386: do not set unsupported VMX secondary execution controls") implemented a workaround for hosts that have specific CPUID features but do not support the corresponding VMX controls, e.g., hosts support RDSEED but do not support RDSEED-Exiting. In detail, commit 4a910e1 introduced a flag `has_msr_vmx_procbased_clts2`. If KVM has `MSR_IA32_VMX_PROCBASED_CTLS2` in its msr list, QEMU would use KVM's settings, avoiding any modifications to this MSR. However, this commit (4a910e1) didn't account for cases in older Linux kernels(4.17~5.2) where `MSR_IA32_VMX_PROCBASED_CTLS2` is in `kvm_feature_msrs`-obtained by ioctl(KVM_GET_MSR_FEATURE_INDEX_LIST), but not in `kvm_msr_list`-obtained by ioctl(KVM_GET_MSR_INDEX_LIST). As a result,it did not set the `has_msr_vmx_procbased_clts2` flag based on `kvm_msr_list` alone, even though KVM does maintain the value of this MSR. This patch supplements the above logic, ensuring that `has_msr_vmx_procbased_clts2` is correctly set by checking both MSR lists, thus maintaining compatibility with older kernels. Signed-off-by: EwanHai Reviewed-by: Zhao Liu --- Changes in v3: - Use a more precise version range in the comment, specifically "4.17~5.2" instead of "<5.3". Changes in v2: - Adjusted some punctuation in the commit message as per suggestions. - Added comments to the newly added code to indicate that it is a compatibility fix. v1 link: https://lore.kernel.org/all/20230925071453.14908-1-ewanhai-oc@zhaoxin.com/ v2 link: https://lore.kernel.org/all/20231127034326.257596-1-ewanhai-oc@zhaoxin.com/ --- target/i386/kvm/kvm.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c index 7ad8072748..a7c6c5b2d0 100644 --- a/target/i386/kvm/kvm.c +++ b/target/i386/kvm/kvm.c @@ -2386,6 +2386,7 @@ void kvm_arch_do_init_vcpu(X86CPU *cpu) static int kvm_get_supported_feature_msrs(KVMState *s) { int ret = 0; + int i; if (kvm_feature_msrs != NULL) { return 0; @@ -2420,6 +2421,20 @@ static int kvm_get_supported_feature_msrs(KVMState *s) return ret; } + /* + * Compatibility fix: + * Older Linux kernels (4.17~5.2) report MSR_IA32_VMX_PROCBASED_CTLS2 + * in KVM_GET_MSR_FEATURE_INDEX_LIST but not in KVM_GET_MSR_INDEX_LIST. + * This leads to an issue in older kernel versions where QEMU, + * through the KVM_GET_MSR_INDEX_LIST check, assumes the kernel + * doesn't maintain MSR_IA32_VMX_PROCBASED_CTLS2, resulting in + * incorrect settings by QEMU for this MSR. + */ + for (i = 0; i < kvm_feature_msrs->nmsrs; i++) { + if (kvm_feature_msrs->indices[i] == MSR_IA32_VMX_PROCBASED_CTLS2) { + has_msr_vmx_procbased_ctls2 = true; + } + } return 0; }