From nobody Thu Oct 2 07:46:31 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 423D5324B1E for ; Fri, 19 Sep 2025 22:34:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758321270; cv=none; b=RFvJsL6AHzurKXEdP53VwhtwTtuDaYJx9FNKi2t3A0fvmduv0sB+vA3DaLkjcbtYce/tyjuMZRnhtAEvrHIHX0IoiF/JjgcjonsgbPbTCLM11pVFvbUKopP3ehP4q4MeT7US5imJeH09dqCtjbE8OrtsHbZesD1wyBmrWAhz9oE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758321270; c=relaxed/simple; bh=9j7eMRNZfnKaut1MLIO4jNPk4U8qe/HJIqhBM9DKwnw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sCKESOmWv5hX0+d1uwEyxB/EhnBlNb3JbCeXaGTiOV5lkKP6FCU05e1eDUTCS68Rn67kwianwq5tuB/3yWQk5wibHyNO1tUs1fz9Vfh52vpKBC3nc+3zTWmnfOuTq51rubNDmUi1RnZA4zcZF/iqQsRjBRAA5b3a1p1VaNiAFEw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=EaJaKQdF; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EaJaKQdF" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-244581ce13aso48903905ad.2 for ; Fri, 19 Sep 2025 15:34:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1758321268; x=1758926068; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=7eTiTbmyW2tvaAMI1jt358bJAngHDTuuFupNysw0qX0=; b=EaJaKQdF4WuNznfKYQKa4ELsJXge54W03s/NBuR84gP4LCOkKFlTv3KydLnNpUIgKW vGex3LQzo8yi6wGUJJYif0n0ItOeCUPiejS3uaIeNMgTERzzowo+IWrUIwFMZydX+FVj V3RkNqhr8+fc/JQM5P7Md9Pb5gItsQMalexvxwAAJkQbhhK99LTy1tyi23I7CDpvjOAl xFmStVdEPhoTuEvAJVJE2ZLnwdp+fDG4a5Ahs5UsunjFaPgKVVLEKXO6N1f/FjE6cwps 3MX31xDx/hJ1/rc7yLXzdRmMKn2aNBcaUamitYTJ1ghBYi+7IGcNVaB23LavF6mz880i 5tVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758321268; x=1758926068; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7eTiTbmyW2tvaAMI1jt358bJAngHDTuuFupNysw0qX0=; b=ipCFNuh4OREUpFXag6eicKITV42wCEGB4NhrS7GvyvWShocNR4pb6KM7Bhd3nMicN4 7XtaHlvNU0BEX+jo7uqQC9rIRLm2QyPo7Bo8KgxBzbePJXfECB/Lm0+Bmi2h6PUcwLs9 M8sh6+ug767PJnXGhUOTQcQHVutpF69W/nPABvijZvc9Qnr1Z9CyBYcG4hLIvyTw12Bd SxGrlBu60kxba7feZlHG9ssgKTv8TkcaBtjJcUPXUhXAHtgIh+X2N685198uWdgevQj9 9mOiCp0qWfApHWxzOVsUx2gAtTl8lEeWx7w4kz+ExK94K7+z790JhjrVm85hzWMWTvg8 SOIQ== X-Forwarded-Encrypted: i=1; AJvYcCUIlVPZRVj73PkgxcmgyNQGaJMCKs2NBZAoKD0qxsE2+oLE0rBcKAp3toTRsjwOPnVUWM/Of07O+LwJt/4=@vger.kernel.org X-Gm-Message-State: AOJu0YxEKSMgkcsz0pnjIqnuPLeKcc+5ZpOk3Rdy8HmzfNoq0ueZM008 xvojT8VZBjBLkOCMAhqh4HOPLFhqf0iw+mAHYXV/IihjYzPAQ5W6T4vvKgKTFHDkywP1ReA+Kux mVhMV2w== X-Google-Smtp-Source: AGHT+IFXr8IRlLneO+WHx4jAfFZ2ebYEf5el1Mwif7m5GTJpYT1UbPToxu4FsdBBTNwL71GOUqmmaGJ/v64= X-Received: from pjbsd13.prod.google.com ([2002:a17:90b:514d:b0:325:a8d:a485]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2285:b0:268:f83a:835a with SMTP id d9443c01a7336-269ba575f3emr58130475ad.60.1758321268664; Fri, 19 Sep 2025 15:34:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 19 Sep 2025 15:32:54 -0700 In-Reply-To: <20250919223258.1604852-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250919223258.1604852-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.470.ga7dc726c21-goog Message-ID: <20250919223258.1604852-48-seanjc@google.com> Subject: [PATCH v16 47/51] KVM: selftests: Extend MSRs test to validate vCPUs without supported features From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Tom Lendacky , Mathias Krause , John Allen , Rick Edgecombe , Chao Gao , Binbin Wu , Xiaoyao Li , Maxim Levitsky , Zhang Yi Z , Xin Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a third vCPUs to the MSRs test that runs with all features disabled in the vCPU's CPUID model, to verify that KVM does the right thing with respect to emulating accesses to MSRs that shouldn't exist. Use the same VM to verify that KVM is honoring the vCPU model, e.g. isn't looking at per-VM state when emulating MSR accesses. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/x86/msrs_test.c | 28 ++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/x86/msrs_test.c b/tools/testing/se= lftests/kvm/x86/msrs_test.c index 952439e0c754..f69091ebd270 100644 --- a/tools/testing/selftests/kvm/x86/msrs_test.c +++ b/tools/testing/selftests/kvm/x86/msrs_test.c @@ -296,12 +296,17 @@ static void test_msrs(void) MSR_TEST(MSR_IA32_PL3_SSP, canonical_val, canonical_val | 1, SHSTK), }; =20 + const struct kvm_x86_cpu_feature feat_none =3D X86_FEATURE_NONE; + const struct kvm_x86_cpu_feature feat_lm =3D X86_FEATURE_LM; + /* - * Create two vCPUs, but run them on the same task, to validate KVM's + * Create three vCPUs, but run them on the same task, to validate KVM's * context switching of MSR state. Don't pin the task to a pCPU to - * also validate KVM's handling of cross-pCPU migration. + * also validate KVM's handling of cross-pCPU migration. Use the full + * set of features for the first two vCPUs, but clear all features in + * third vCPU in order to test both positive and negative paths. */ - const int NR_VCPUS =3D 2; + const int NR_VCPUS =3D 3; struct kvm_vcpu *vcpus[NR_VCPUS]; struct kvm_vm *vm; =20 @@ -316,6 +321,23 @@ static void test_msrs(void) sync_global_to_guest(vm, msrs); sync_global_to_guest(vm, ignore_unsupported_msrs); =20 + /* + * Clear features in the "unsupported features" vCPU. This needs to be + * done before the first vCPU run as KVM's ABI is that guest CPUID is + * immutable once the vCPU has been run. + */ + for (idx =3D 0; idx < ARRAY_SIZE(__msrs); idx++) { + /* + * Don't clear LM; selftests are 64-bit only, and KVM doesn't + * honor LM=3D0 for MSRs that are supposed to exist if and only + * if the vCPU is a 64-bit model. Ditto for NONE; clearing a + * fake feature flag will result in false failures. + */ + if (memcmp(&msrs[idx].feature, &feat_lm, sizeof(feat_lm)) && + memcmp(&msrs[idx].feature, &feat_none, sizeof(feat_none))) + vcpu_clear_cpuid_feature(vcpus[2], msrs[idx].feature); + } + for (idx =3D 0; idx < ARRAY_SIZE(__msrs); idx++) { sync_global_to_guest(vm, idx); =20 --=20 2.51.0.470.ga7dc726c21-goog