From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 0CEAB19E96A for ; Fri, 17 Jan 2025 16:31:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737131503; cv=none; b=KIcgw5xqZ6EgtmCO151Q5teya4Efu8tvJYvBzCm72MH2x7/Kcus8iO70TG9991rUfSS0vtSRPngu6N9ER89YsabMIqd9RAqFG4JORt1PVTcVFu4BIxDjwcf9RPytDb2d/FT6ZdyjNzEQH6Stl+3CKOdtYQJjdH8YHx1Wuqa0mBU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737131503; c=relaxed/simple; bh=FeICJz26VuZJTBSz/mgNGIoJHYW0xflODP3chhVaxzk=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=mRFjtyVtG7+CxQAaJECQWRbpiPRPnivCxrmCm8sRFWTgVG44PHXrjjchPTXvtm87MGwKYqz+4hiLdpwuLB8Vvd82rDBsfGmG9EYGOP6ZmcU/S0jAQ70qLyF2GlUFPR6jV2HOQ9b9WMTrv35QTlUkcScz5urCLk9ORIdIww5eA3M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=TrnMwar2; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="TrnMwar2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737131499; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=+77/SaUXCKkXV6hKKiCSh57OzjBaZbkjDOMBDHfY2X4=; b=TrnMwar2PLkWTU8WrlZBnXI6CZzyWcv6N9vuH13d6RXCov0Rzvpbyot+YSOpHk1s0hga9q zSlNPyhayKFU+1WyjVSEZKFvZ9Cz4ELtMxzEEDxcalUhcUSZg4ppSHrDY8V4UVVZtvHbmJ nh8Tn7ApXvJDa4kTOJxs6qEvgbeVFNk= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-313-HvQbvdDBPzySlfoWHFAUsQ-1; Fri, 17 Jan 2025 11:31:38 -0500 X-MC-Unique: HvQbvdDBPzySlfoWHFAUsQ-1 X-Mimecast-MFC-AGG-ID: HvQbvdDBPzySlfoWHFAUsQ Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4362153dcd6so11634105e9.2 for ; Fri, 17 Jan 2025 08:31:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131497; x=1737736297; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+77/SaUXCKkXV6hKKiCSh57OzjBaZbkjDOMBDHfY2X4=; b=S3KK5sbyAq7ndz5ivFNaNFwcxGqbwJCTOu6ptpHrLZX5A71u7Y+JjAH+OS1D53S+ls fR6kktdBjWZgGJOobOmxxV3k4n/8kpt+I/+pwBAa6y2frgF1czZ4eEozhJC8gfuTaJfT jwiUH3JwhPjLFOnHZWDTlpUis/S2POtMPLy3A1MWn0m5lWAk7RbjUV62U2v1xvgNB9gl THjnJFGLqDqKZHd8JtSZKorzNK331xeUeAUk8L3lBAfFuQjcOf0sM6rBKuNt2mVND+nn skStNntWyWeQSjZRjrLJKRx07WBQhcE9lkF8clepKVQTevD0zUUcYBjp7NnJS3fwV1gI FGeA== X-Forwarded-Encrypted: i=1; AJvYcCV0Hp4ZCdDI696s2bDh0OqGpCrIyh7SCj0ndA8pQQl3zS4O+XCdnsubCu4IG0Zb3IwhlAwNZxYTx+2Fy8E=@vger.kernel.org X-Gm-Message-State: AOJu0YxXi1fhaaBVkOKZhlcZyGJGRXDhohEUoWQKv5Lb9ozDHcgcC0iX jo12W0pUwb3FVNmHLPiEk1Cw75FkJZLUNc/QEAkx+PW0ujsyMa3jf4xzdAv2ltr3Nd4mi2A7yEy J/Kw+Ueiis2gN4oDEEFDu5mCe/In6VoGifZb55m+XQQTvnLPiXCjavu9DoT0+6A== X-Gm-Gg: ASbGnctxQDtEnwrhKl2B0aM9zvc1QJV03IdIxSv5SldX2jEeDXrSkyN5U7OROEKuh75 UVuuTq5Fz14q/n/0DwPSgFJg6PedHyZSObZHJhhtbmv89ov76vcieLzv9LDudo9LnAkzHcLSmwx 57ZMsWoj3vhxD3/GTTmk6AS7oDSiXMASyjprDUxbnnzi3IFudIoUP9OzIUQrwawZ+x+8LgfVung qov3J970dZTtaPuVxeiWBfwfDacaMS8BRuDiRp5Y2Rgli5tgwI= X-Received: by 2002:a05:600c:1d07:b0:436:488f:50a with SMTP id 5b1f17b1804b1-438913ef4b7mr34995965e9.17.1737131497118; Fri, 17 Jan 2025 08:31:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IFu82CO+1HMJl6Yl6mARaKmQ8v95VIoR/LMJxTs5DG8YsvMipJpx9rlFz3w6AWHY5Z1lYtE6g== X-Received: by 2002:a05:600c:1d07:b0:436:488f:50a with SMTP id 5b1f17b1804b1-438913ef4b7mr34995535e9.17.1737131496762; Fri, 17 Jan 2025 08:31:36 -0800 (PST) Received: from fedora (g2.ign.cz. [91.219.240.8]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43890469ba8sm37794535e9.37.2025.01.17.08.31.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jan 2025 08:31:36 -0800 (PST) From: Vitaly Kuznetsov To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Dongjie Zou , stable@vger.kernel Subject: Re: [PATCH 4/5] KVM: selftests: Manage CPUID array in Hyper-V CPUID test's core helper In-Reply-To: <20250113222740.1481934-5-seanjc@google.com> References: <20250113222740.1481934-1-seanjc@google.com> <20250113222740.1481934-5-seanjc@google.com> Date: Fri, 17 Jan 2025 17:31:35 +0100 Message-ID: <87h65x8krc.fsf@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Sean Christopherson writes: > 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. > > Cc: Vitaly Kuznetsov > Signed-off-by: Sean Christopherson > --- > .../selftests/kvm/x86_64/hyperv_cpuid.c | 25 ++++++++----------- > 1 file changed, 11 insertions(+), 14 deletions(-) > > diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c > index 09f9874d7705..90c44765d584 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; > } > > -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 = 10; > u32 test_val; > > + if (vcpu) > + hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu); > + else > + hv_cpuid_entries = kvm_get_supported_hv_cpuid(); > + > TEST_ASSERT(hv_cpuid_entries->nent == nent_expected, > "KVM_GET_SUPPORTED_HV_CPUID should return %d entries" > " (returned %d)", > @@ -109,6 +114,7 @@ static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries, > * entry->edx); > */ > } > + free((void *)hv_cpuid_entries); (see my comment on "[PATCH 3/5] KVM: selftests: Explicitly free CPUID array at end of Hyper-V CPUID test"): vcpu_get_supported_hv_cpuid() allocates memory for the resulting array each time, however, kvm_get_supported_hv_cpuid() doesn't so freeing hv_cpuid_entries here will result in returning already freed memory next time kvm_get_supported_hv_cpuid() path is taken. > } > > static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu) > @@ -129,7 +135,6 @@ static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu) > int main(int argc, char *argv[]) > { > struct kvm_vm *vm; > - const struct kvm_cpuid2 *hv_cpuid_entries; > struct kvm_vcpu *vcpu; > > TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID)); > @@ -138,10 +143,7 @@ int main(int argc, char *argv[]) > > /* Test vCPU ioctl version */ > test_hv_cpuid_e2big(vm, vcpu); > - > - hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu); > - test_hv_cpuid(hv_cpuid_entries, false); > - free((void *)hv_cpuid_entries); > + test_hv_cpuid(vcpu, false); > > if (!kvm_cpu_has(X86_FEATURE_VMX) || > !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) { > @@ -149,9 +151,7 @@ int main(int argc, char *argv[]) > goto do_sys; > } > vcpu_enable_evmcs(vcpu); > - hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu); > - test_hv_cpuid(hv_cpuid_entries, true); > - free((void *)hv_cpuid_entries); > + test_hv_cpuid(vcpu, true); > > do_sys: > /* Test system ioctl version */ > @@ -161,10 +161,7 @@ int main(int argc, char *argv[]) > } > > test_hv_cpuid_e2big(vm, NULL); > - > - hv_cpuid_entries = kvm_get_supported_hv_cpuid(); > - test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX)); > - free((void *)hv_cpuid_entries); > + test_hv_cpuid(NULL, kvm_cpu_has(X86_FEATURE_VMX)); > > out: > kvm_vm_free(vm); -- Vitaly