From nobody Thu Dec 18 11:45:04 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 E83E127701 for ; Sat, 18 Jan 2025 00:35:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737160503; cv=none; b=n/jFoz+CvUHiwDSjBP6fIIgfHrTdWrDqkFuvnp5m3wHiPVHd2wJQJ7CLG0PtsevJn2aP1F27uVWjEmdubt8dy7s6Zakei5BGXSs10wapFFGnLmNDGSD3AIdprEkdAbk4WriqVV3+CAuGPJgXUNRc++/kG5g4uYAfudB46i92Za0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737160503; c=relaxed/simple; bh=SHTq2kDtgw9gkfy++/oIpJp+B4zXh2grXfh3cPHx9b8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pXu3Pqhshqw3gCsPbbrWxEri0/PAfEUsWpjcmMeqgd9Yj/FBzDhaTFPNTG9pPp2/l/+fDClkLu7DzTeqiSCCa2XH0PYLPzS0Cceaz88ph4G/4o8PWWYnOWWtfqLWCmpzJOV2jr0BgHYdTAwkp0TKYCHRtfB0SrQ1hyrFpn27fb4= 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=LHOvN3C4; arc=none smtp.client-ip=209.85.214.202 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="LHOvN3C4" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2167141e00eso53890805ad.2 for ; Fri, 17 Jan 2025 16:35:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737160501; x=1737765301; 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=HRXEZ2l2lIq+cPb1+bgcVVd/Y4QYpoxYvkHlcCCt+hc=; b=LHOvN3C4PjjZboGOavajjL05VF2RbaiDtiG70r5PJKFfeaSXEXiIFG+Werh0tSBAIC RQ4Ahpc4wMImD1lQaC7WrEHJlALYAVJ5xFh/nuNRrDeIQAH9YGqjQHR95PKKB0HZyBdF Neb64b3jrEtpOcedkmjas9g+ezgXCHaYyhNlOFVn83Jnxqj/obxTN75/xYRkoXZ19JQG p/3/dMhVvFs7d0UhWo/87vgAEV95elmgcWULlbxVq7PPyTwjqWD8BsvvEu2pTUCLcPGQ ScvImRliofxNo5Hc1xwEyikUy/382qQQSXx0LMq9p9+mfW/g1DcxeWbjmhYaS+lOzCHf VZMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737160501; x=1737765301; 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=HRXEZ2l2lIq+cPb1+bgcVVd/Y4QYpoxYvkHlcCCt+hc=; b=Uns2Hjb3Gg+cAmhckjYGmh4BeUGMbTCxsUlqyav8WLQbuYdd/7L/yxnjlvDMfUezfe 0K5i9aDakPfgLNtX8pbs096JHMy0JMwWrcN326bYONHHp0S33ibFK1QI1bjtAWkF9IJT bpK1SLOk/WY0zQhNUFNAPx1NHI6EQCKKTHa8VwPsx+7S/9X/JbTXbqlMDuSf5yu1VtZE wBGdLp3pCN144Hp5j4VsrGh1IINsb/EIoN0mx2nJWpzTY3HrKXu32KBvQQhVwSCm1COU +eqxrJodHVn55KapC60O4NGzh1CvxCBu3sWEjS9NusHcZZcvT5Ng159XufkaF3XtJebg 5sPA== X-Forwarded-Encrypted: i=1; AJvYcCW4q+afbPjflrUTOv4uPl5DTT1110iiBU+JUagW0jAaXybCH/lL4x5HEg14V7AurxuckfhGvWZjAsviiDk=@vger.kernel.org X-Gm-Message-State: AOJu0YyR/GibqxodVtUUxgSUYGa+Zj0P1tvMJERqnkK+zzyVH340T6G0 Or5p6syXR2+1z13PbensjLgKwJdinmOZqiWIrkENLUGZcZpOcMLUPOyT1j/7HNFlzz2LgU1NKZq q1g== X-Google-Smtp-Source: AGHT+IEdL1a3dtHM8hqnKo2mq4PnvT6aTyCFLyiYxbWZnYz41X4lQpTCsFqPdOa0DVupvRidoIK5scNjGvc= X-Received: from pgbcl22.prod.google.com ([2002:a05:6a02:996:b0:7fc:fac3:7df6]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:7fa5:b0:1e1:c8f5:19ee with SMTP id adf61e73a8af0-1eb214dfdddmr7213773637.25.1737160501146; Fri, 17 Jan 2025 16:35:01 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 17 Jan 2025 16:34:53 -0800 In-Reply-To: <20250118003454.2619573-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: <20250118003454.2619573-1-seanjc@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250118003454.2619573-4-seanjc@google.com> Subject: [PATCH v2 3/4] KVM: selftests: Manage CPUID array in Hyper-V CPUID test's core helper From: Sean Christopherson To: Vitaly Kuznetsov , Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Dongjie Zou Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allocate, get, and free the CPUID array in the Hyper-V CPUID test in the test's core helper, instead of copy+pasting code at each call site. In addition to deduplicating a small amount of code, restricting visibility of the array to a single invocation of the core test prevents "leaking" an array across test cases. Passing in @vcpu to the helper will also allow pivoting on VM-scoped information without needing to pass more booleans, e.g. to conditionally assert on features that require an in-kernel APIC. To avoid use-after-free bugs due to overzealous and careless developers, opportunstically add a comment to explain that the system-scoped helper caches the Hyper-V CPUID entries, i.e. that the caller is not responsible for freeing the memory. Cc: Vitaly Kuznetsov Signed-off-by: Sean Christopherson Reviewed-by: Vitaly Kuznetsov --- .../selftests/kvm/x86_64/hyperv_cpuid.c | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c b/tools/test= ing/selftests/kvm/x86_64/hyperv_cpuid.c index 9a0fcc713350..3188749ec6e1 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c @@ -41,13 +41,18 @@ static bool smt_possible(void) return res; } =20 -static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries, - bool evmcs_expected) +static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected) { + const struct kvm_cpuid2 *hv_cpuid_entries; int i; int nent_expected =3D 10; u32 test_val; =20 + if (vcpu) + hv_cpuid_entries =3D vcpu_get_supported_hv_cpuid(vcpu); + else + hv_cpuid_entries =3D kvm_get_supported_hv_cpuid(); + TEST_ASSERT(hv_cpuid_entries->nent =3D=3D nent_expected, "KVM_GET_SUPPORTED_HV_CPUID should return %d entries" " (returned %d)", @@ -109,6 +114,13 @@ static void test_hv_cpuid(const struct kvm_cpuid2 *hv_= cpuid_entries, * entry->edx); */ } + + /* + * Note, the CPUID array returned by the system-scoped helper is a one- + * time allocation, i.e. must not be freed. + */ + if (vcpu) + free((void *)hv_cpuid_entries); } =20 static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu) @@ -129,7 +141,6 @@ static void test_hv_cpuid_e2big(struct kvm_vm *vm, stru= ct kvm_vcpu *vcpu) int main(int argc, char *argv[]) { struct kvm_vm *vm; - const struct kvm_cpuid2 *hv_cpuid_entries; struct kvm_vcpu *vcpu; =20 TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID)); @@ -138,10 +149,7 @@ int main(int argc, char *argv[]) =20 /* Test vCPU ioctl version */ test_hv_cpuid_e2big(vm, vcpu); - - hv_cpuid_entries =3D vcpu_get_supported_hv_cpuid(vcpu); - test_hv_cpuid(hv_cpuid_entries, false); - free((void *)hv_cpuid_entries); + test_hv_cpuid(vcpu, false); =20 if (!kvm_cpu_has(X86_FEATURE_VMX) || !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) { @@ -149,9 +157,7 @@ int main(int argc, char *argv[]) goto do_sys; } vcpu_enable_evmcs(vcpu); - hv_cpuid_entries =3D vcpu_get_supported_hv_cpuid(vcpu); - test_hv_cpuid(hv_cpuid_entries, true); - free((void *)hv_cpuid_entries); + test_hv_cpuid(vcpu, true); =20 do_sys: /* Test system ioctl version */ @@ -161,9 +167,7 @@ int main(int argc, char *argv[]) } =20 test_hv_cpuid_e2big(vm, NULL); - - hv_cpuid_entries =3D kvm_get_supported_hv_cpuid(); - test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX)); + test_hv_cpuid(NULL, kvm_cpu_has(X86_FEATURE_VMX)); =20 out: kvm_vm_free(vm); --=20 2.48.0.rc2.279.g1de40edade-goog