From patchwork Mon Jan 13 22:27:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13938176 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 5156A1F8EF3 for ; Mon, 13 Jan 2025 22:27:44 +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=1736807266; cv=none; b=EtpMDLqFTHPXWzOEdwv0VxE8JEvIWQAVhw9CmqXnaWi3BD/NS/SI11uTUxMaWbWxHtiiY07QAe/kNHmY1GTPlCZ5XutteI7jzKfoOfAWcFKLt/eE2h0mkHRyjfERW7x7aGRpy+T+HTZK/pywXPX1uZK6YaKVLNWLqox0HvWnE0k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736807266; c=relaxed/simple; bh=vblzX9oZHAXxiqXhSe7IP/dcd0L4gup1FWh8NLr+JyY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Git6ehJ8/upbqPOY7toXQrw1HRxEbnV7hsBSk3+Cwwuu/u+q/HK1eBc4HIotLC3LYiNow8n6UhRIzLc/g6+zw3jRHQ5Npn73iZxuSJDYhtv4yElpskWzj8KKmDGSwgo43qsrybYjgazyloq/9aVstMTk0rr7QYP19DQMTr+qDMY= 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=1zI/CGE8; 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="1zI/CGE8" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-216750b679eso60322615ad.1 for ; Mon, 13 Jan 2025 14:27:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736807264; x=1737412064; 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=FedWRlmic2iRbfD7jbI+6doHjrw5hxRZcg5PhzQ2p6Y=; b=1zI/CGE84arHGyuQdpAnpAG8T8e0GBfHnhAGsyG7oTIB86Chtqu4VOmSurNpvcOB+u mkeIYdL1nTN20ehmFckI5gky5Szijdi4jxF45xycc0TRvVMDYf7a/DaeLXWtKyi1pxns LEd7pwtRstsOuuAqq8KzduiHaV9ZjMoy/8MW2vYCzTb+VFGdPhfweePJNCEF/Ll0Ilgq RvzneqiH7vtBz5wWq3KsMCWKA+EhBNJM76DscIqheqokUy4hA1EsyXRVWc4QdYMZKqn5 s+tHTVk1NIOPd90n7gH8bLltNUcBK25bzINrBXq2EtQ9l5wlSftcJGF8x/SV43xVggWQ k3xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736807264; x=1737412064; 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=FedWRlmic2iRbfD7jbI+6doHjrw5hxRZcg5PhzQ2p6Y=; b=biLFIBbO+mlnzxpHrc5p85fo9gyHhHKb8son32UDSVFOezOdrZW9zpKKd8Dib0rzZM BtzJciqmzRJsRt1LOeQDA8eeeFpwk2+r9IiGdXD+fZ/S7ucZmbjxfDpS9yvfew43zpmx HhzWVAF3XpbeXMmsBXNam/FowUNBnh0H7R7yiG+sTDUUFqk6C48+fw5lG+WNles+5CMV 6wf/NV8t47cMwZR79E0Kxet7PnuXc80GcjAPMbjMXM+vki6O9a424buLjDu8SA+9GzA0 hkV2sfC1G7ycA76J6HU/7EDCWBaGZFOTIYNM1F7v6xfYhbw+bMDrqlgr2xXJa1kN+AQh bZZQ== X-Gm-Message-State: AOJu0Ywum4ye5RjJ3SHi+nBuMhBxNTBcxyfhpTW0RDfYNKx6zgyyxHIe tipHes+1n6sCT1EovDgeDFRAJlrjCWzP5j3DmBotWyH3T8i7O+MQiW0YYMIZHQSFZQjfRtK34Yq /zA== X-Google-Smtp-Source: AGHT+IG/ROPkD8yLMtzQiIrh1R/MPmPtvI4UCtDnjy6mbuSklnLOxfXbAF79flfOKNZCBDDB0j65jdsuvK8= X-Received: from plsd19.prod.google.com ([2002:a17:902:b713:b0:212:4fad:1dc1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f705:b0:205:4721:19c with SMTP id d9443c01a7336-21a83fe48fdmr325122105ad.37.1736807263732; Mon, 13 Jan 2025 14:27:43 -0800 (PST) Reply-To: Sean Christopherson Date: Mon, 13 Jan 2025 14:27:36 -0800 In-Reply-To: <20250113222740.1481934-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250113222740.1481934-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.688.g23fc6f90ad-goog Message-ID: <20250113222740.1481934-2-seanjc@google.com> Subject: [PATCH 1/5] KVM: x86: Reject Hyper-V's SEND_IPI hypercalls if local APIC isn't in-kernel From: Sean Christopherson To: Vitaly Kuznetsov , Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Dongjie Zou , stable@vger.kernel Advertise support for Hyper-V's SEND_IPI and SEND_IPI_EX hypercalls if and only if the local API is emulated/virtualized by KVM, and explicitly reject said hypercalls if the local APIC is emulated in userspace, i.e. don't rely on userspace to opt-in to KVM_CAP_HYPERV_ENFORCE_CPUID. Rejecting SEND_IPI and SEND_IPI_EX fixes a NULL-pointer dereference if Hyper-V enlightenments are exposed to the guest without an in-kernel local APIC: dump_stack+0xbe/0xfd __kasan_report.cold+0x34/0x84 kasan_report+0x3a/0x50 __apic_accept_irq+0x3a/0x5c0 kvm_hv_send_ipi.isra.0+0x34e/0x820 kvm_hv_hypercall+0x8d9/0x9d0 kvm_emulate_hypercall+0x506/0x7e0 __vmx_handle_exit+0x283/0xb60 vmx_handle_exit+0x1d/0xd0 vcpu_enter_guest+0x16b0/0x24c0 vcpu_run+0xc0/0x550 kvm_arch_vcpu_ioctl_run+0x170/0x6d0 kvm_vcpu_ioctl+0x413/0xb20 __se_sys_ioctl+0x111/0x160 do_syscal1_64+0x30/0x40 entry_SYSCALL_64_after_hwframe+0x67/0xd1 Note, checking the sending vCPU is sufficient, as the per-VM irqchip_mode can't be modified after vCPUs are created, i.e. if one vCPU has an in-kernel local APIC, then all vCPUs have an in-kernel local APIC. Reported-by: Dongjie Zou Fixes: 214ff83d4473 ("KVM: x86: hyperv: implement PV IPI send hypercalls") Fixes: 2bc39970e932 ("x86/kvm/hyper-v: Introduce KVM_GET_SUPPORTED_HV_CPUID") Cc: stable@vger.kernel Signed-off-by: Sean Christopherson --- arch/x86/kvm/hyperv.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 4f0a94346d00..44c88537448c 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -2226,6 +2226,9 @@ static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc) u32 vector; bool all_cpus; + if (!lapic_in_kernel(vcpu)) + return HV_STATUS_INVALID_HYPERCALL_INPUT; + if (hc->code == HVCALL_SEND_IPI) { if (!hc->fast) { if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi, @@ -2852,7 +2855,8 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED; ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED; ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED; - ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED; + if (!vcpu || lapic_in_kernel(vcpu)) + ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED; ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED; if (evmcs_ver) ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; From patchwork Mon Jan 13 22:27:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13938177 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 263941F9EB3 for ; Mon, 13 Jan 2025 22:27:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736807267; cv=none; b=M6PmrZEDIW38yHrylTqiEueqxkk2iaffLAEgwbpDykiwrF+SmDrJC1isSrKuIb//uEWmO1lKeuxANxenn+RdUFWzcEv1TX+oJS5iOc/p3z6kmfF/tyHEMn11F6lvb4Wy4Q6cmRNRFNwIKKOSVPu9NjFDBtUcf+pYAFdWp+Tmo8E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736807267; c=relaxed/simple; bh=zqLBBXrug8v32oNQce0fAdA/PWXxsTOVhZ1W2PAUujg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Hyhe16ZVJzsYHAbuQHWr4A9TtvH5VjHAz1aaRURuAr0npx4m5t1fLhVZfib+m2EoKZMwF5/aoUWH53IMhSO+/5RChbHrA8qEgq+1oemiiXdcB+p91s0Ix3qu1Y0/T4sAF5apKu21fT+47UMo8UKom7eZ8IpOTEniqEriYshGfq0= 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=fq58RQRH; arc=none smtp.client-ip=209.85.216.74 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="fq58RQRH" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2f129f7717fso8690112a91.0 for ; Mon, 13 Jan 2025 14:27:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736807265; x=1737412065; 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=lOOaItQ8XjWrN4V+XnxPKIJp+wwpgWIYOMyymj/eGI4=; b=fq58RQRHVE7B25bcyzIUnGkKtG6O8D3+vBcC0+PWEwNE1fBg/cmE48lOcTxLVSysD6 zS/PeK6zoPkY4WyXcuNqyXva+vlDRHVdHGAw7NzKZrG2o5oAgK5NJESfTJ+rzk2eQcUG fhZ+eLw8PYdhfuSq0A/qn4M3nHVzA19Afwm+bY9StQNKtiTJpNQzheQJBPA0ETsuVCLX 6qS82tiHEN4C2hNqsERJWnQpm0v41ga+V+qFo8Ry8HA5vvV4/jETZayLjODvQGitXrhV i1dTTc7pHcYeC00xwL2YevBxtqfdf+IH8dHsqKhHGE37ka7tcDy4aVexMkSmANtHjemQ +VGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736807265; x=1737412065; 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=lOOaItQ8XjWrN4V+XnxPKIJp+wwpgWIYOMyymj/eGI4=; b=mgrG95XowKrMVdwEO4vEhY0G4tnkT4rqka4zYsgv+mT5UXVzXbuh+q3HHdTZOMR8yQ n/x3fx0Cz153Efo+VUR/DftSpxtwQpDoGpMId4AB3cwuDJ3hwZVgAnrd4ltKeMXtd0o8 fHKunt8LAVcypI/SrcoAPYmyGAzfM6dMXRnMIpEQTruzeXNlZCJ4vVt7Tu5Fg3HtXUQn pSyeYSAaO5GmwzGQ56Ufh0X0OnKOlrNKyCIk7jj20yh7Wce8MR5IPzDQc7Uja6oOOW85 Q9v17QqAJcjXaZdSNdAr+USO8gNNYM9cgYyAW8Pp9HTy8rcZQf08vpDbfMOMLQxYpTU+ OY2A== X-Gm-Message-State: AOJu0YwQKDafsejJf+nYrzpgUz0r0kfB2djHS8pZ2Vru0vyd2ye09vTQ xkQOspvq3PUwR3kc8tDmjVUKSC63DfHLVW7J2+ALQXR8LvRyeFbcEd6397FldckmAp24UUk4oR2 jgg== X-Google-Smtp-Source: AGHT+IE4SCIj0eT4MwfhXXuqwjQSQynoe//BaCPOdrBHH5GyAIKrqtS2BvL94oyZ9JQHJxJWpwkBjqq4Xkc= X-Received: from pfbcz13.prod.google.com ([2002:aa7:930d:0:b0:724:f17d:ebd7]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:8cc3:b0:1d9:2b51:3ccd with SMTP id adf61e73a8af0-1e88cf7ba29mr37755024637.7.1736807265389; Mon, 13 Jan 2025 14:27:45 -0800 (PST) Reply-To: Sean Christopherson Date: Mon, 13 Jan 2025 14:27:37 -0800 In-Reply-To: <20250113222740.1481934-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250113222740.1481934-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.688.g23fc6f90ad-goog Message-ID: <20250113222740.1481934-3-seanjc@google.com> Subject: [PATCH 2/5] KVM: selftests: Mark test_hv_cpuid_e2big() static in Hyper-V CPUID test From: Sean Christopherson To: Vitaly Kuznetsov , Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Dongjie Zou , stable@vger.kernel Make the Hyper-V CPUID test's local helper test_hv_cpuid_e2big() static, it's not used outside of the test (and isn't intended to be). Cc: Vitaly Kuznetsov Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c index 4f5881d4ef66..9a0fcc713350 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c @@ -111,7 +111,7 @@ static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries, } } -void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu) +static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu) { static struct kvm_cpuid2 cpuid = {.nent = 0}; int ret; From patchwork Mon Jan 13 22:27:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13938178 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 CA2161F9F70 for ; Mon, 13 Jan 2025 22:27:47 +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=1736807269; cv=none; b=iDpXg0QjAcenKphlUikRzdBnfAsX5E2HftVXKKCobmfPw7X2pVVchr28wb462oSuFpYaMvfVtt1UegoqDuIfwGqUASILJiThnUGUIJSPEM/hgxvEnD+LBjfQX/rxDsf2+HRoKmwUQP626rVSbZBLqJzpAe1T6647NWJXujq0IR8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736807269; c=relaxed/simple; bh=5GjPFjMPTzjbvv9jMn0Vu+JcFADgwPVyHZSXDMV12jA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DMJMULwR9D8LueCwj3Zsxv45YhhLQCYaWbgIQAsIkhLYymyCPWJHFSE7MlEhVPs7EPXsjIpI3n5uRN419B6DhPk/DZRkcAD06hE+G5qBC2qqmxQPGplwLlzWahfpzMBDS8kY9oV/CloeNE87tbI2pyKlbm8XbhOvzz4pas6EVY8= 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=T13yTbse; 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="T13yTbse" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21661949f23so140517735ad.3 for ; Mon, 13 Jan 2025 14:27:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736807267; x=1737412067; 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=39mnKX1L0qcb+XQar8pyjjef/DCqBo7Lr0CV2YNY9CI=; b=T13yTbseqeQAH5KEPiugc/exWyyjEyGMweAohAw+s5JWB7yfjYpo9vUZIcEqbpKJKl bcbfTbAAzipDmq1blXRBtSeYqAPIh671M7t7WBzQ3ourEkpQlmnHnychm7aXNqOvW4+z fA5f9/nU0GQGhlAhU7j4tnEgqL/CbIIPgkNkEVooR7emBvR9wlY9X22gNaa7Zeg36nF4 +zBLLiVemGOQDNo0tWongkdcDVOAyes3OdnkF+yIYHT0cEj+RoQfBSU3lvVCL7oYd77J 9MpXjMcN8nsbHzrHY/Jw12mOUVsJ6yUi+Mr2hP70XAZZjOsGnlIztsZwG8zp/Ovz4R7k T9lQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736807267; x=1737412067; 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=39mnKX1L0qcb+XQar8pyjjef/DCqBo7Lr0CV2YNY9CI=; b=GLxW8UUOlbMySX6ytA3e9gOwJxFx9BSc/Ujf4z3K6YF+0/O5vg1e1ziXwyMvTQsPg/ IHoxl61hDqIS4EQtPdEA26uZ4cnGDM53qqRbtVUDVD5ZXXhvvZgZlGXW3Jgdas9hdpEX KRCE5TW7hDnGpm0DYxFdFaiwMPSSRM3dgebyY5FfrhhNyCoU7y+L0RhqLvhaitq0mXPC YE2mgLujlKwfNGy93qY+e34EMc+F+GKBHc4qSJUSvB0ere8BySMTzv3FW4apyr/KMJPK LCI8Y04g0Xyqh8XDNS5ib+HqBSKbZ4EzanB6PJxFutJ8sK+LC17FRK7FK2c1BzkfqgQL 59bQ== X-Gm-Message-State: AOJu0YwtEw9Vb1xkJ+agFUdgknKgkp84cEHGIzIB02jwQb+yA/H7IDu3 Z2UHTBoDmvlv/4nRhFIpG+xDCNdxXtOL6/3zye0wPFLrTY40HWb9p7cBr03KDxkkFZ+OblbSRcX Ptg== X-Google-Smtp-Source: AGHT+IHpFJQ9BF+w23kswjBN8A1uwfUDYhkyBhOqmPuMktb7ZLvC/8lkvUR8KHuk272+C/dk8xx6cgvyp/g= X-Received: from plks12.prod.google.com ([2002:a17:903:2cc:b0:211:fb3b:763b]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c941:b0:215:a179:14ca with SMTP id d9443c01a7336-21a83f3eec9mr341082645ad.2.1736807267148; Mon, 13 Jan 2025 14:27:47 -0800 (PST) Reply-To: Sean Christopherson Date: Mon, 13 Jan 2025 14:27:38 -0800 In-Reply-To: <20250113222740.1481934-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250113222740.1481934-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.688.g23fc6f90ad-goog Message-ID: <20250113222740.1481934-4-seanjc@google.com> Subject: [PATCH 3/5] KVM: selftests: Explicitly free CPUID array at end of Hyper-V CPUID test From: Sean Christopherson To: Vitaly Kuznetsov , Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Dongjie Zou , stable@vger.kernel Explicitly free the array of CPUID entries at the end of the Hyper-V CPUID test, mainly in anticipation of moving management of the array into the main test helper. Cc: Vitaly Kuznetsov Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c index 9a0fcc713350..09f9874d7705 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c @@ -164,6 +164,7 @@ int main(int argc, char *argv[]) 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); out: kvm_vm_free(vm); From patchwork Mon Jan 13 22:27:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13938179 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 664151FA174 for ; Mon, 13 Jan 2025 22:27:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736807270; cv=none; b=Q1bn6SFKOX9Y1fXXD0cGiRi0mTzHXJ5mopyNFGBofCoow2t+dStF82TcKOhRPPvbmuuXFXLeiv5XIrUzNJ4xxzAN4u3Fx1thLpogdFMhQuTCa2dMF5NIxtO/0eypgJROGDvQYYnUv4wCN7MnM1j+79oJZNray9fMfV1j4KDl/Aw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736807270; c=relaxed/simple; bh=iZqIiGhqGkDp1l9h+Tl36RWsA0eh5huy0GX52g1XNG8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mFncItMwXcX7quVV4fjCu/1xq1tzLVFc1gL6ktzOHsTq6vEeVF07W7HOidCibs8LrjPU15mMwtTA2XEE3YvXRjP5NIB4yml0cy6Ons9bLl4p7fVLflxhesWWIS23wIkQ97WQUr0TaP3KUIPYSGUNzFwkhDZXU/xaViOqaXzqKww= 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=oHXtl8hc; arc=none smtp.client-ip=209.85.216.74 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="oHXtl8hc" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ee9f66cb12so8623970a91.1 for ; Mon, 13 Jan 2025 14:27:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736807269; x=1737412069; 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=BUiKBnRthtYi8hLYA5ryBjgwDDiPDiUap44/jYe6tjY=; b=oHXtl8hcAHWHwnMUfqGvqC1qBQE50SXKse1A2tpppX+sNOnfWvZ7IcCT95aiDwC673 EjWh4vrlNx0XtIrVaLWhvTFJnZhvi/PtEorRGFuP+NyV4oURKfRQ4X/5kExyhaEKRqJj 56l8SWdr/wkJ33PxkqqkXa2sE+yzrn8rezkIJkx1eaZo3t9Vabqt0elMfJ1y3LDJwwsg XgFExDBsuy7MvOYd+LXtlAcxT2lqijyHldY4psBJ2w8teAESZT9PgoVEgwgDai/OmaAv hi7hJ0hyr/QDN7u9Po4UlNMxlcFvRZpFXEnMym/KaxSl0isLnc8C2xo4BmJR6yi7Jj4D lptQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736807269; x=1737412069; 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=BUiKBnRthtYi8hLYA5ryBjgwDDiPDiUap44/jYe6tjY=; b=HQnbd0HIe5jrvMAQ9a5bZ9Lsso+sxNBmDhPD0O7Jxo4y5N6GiJKnxsDXKnTvXFF0ms mD/agihWWMJ5eRSC2wOL8/z7wOpZHkp0gLwWN0ojXozJ0OIH4ZRNkOSLFV4a4Uo3ydWS RRrJ38Nkzy0XIXj0s0WryyWLBqNLuAkZCKnnj28SpBVGZgJ+EM0l/KtJnqg9sDX/VtLB 6uu98im8cTrVQU4nQZJjeD3B4lzNhugtbakwrXzk6W7onrIlNbDJ7taUxXpj63g1zSYx 9iN5gLlBDQwZ3+DjjkQ5MqeNl4vggjoLOfrVTv62jXCwW+3e4YKMd+FuNtoF0nnM3Q3S k4uw== X-Gm-Message-State: AOJu0YzCqwQ1FI3jD0nNQbUk0OfQLacnddnRf2ntcpFsGSCoiGL5VkIM XIyQLG0TJPS8nLY9w3K5pG+wCj8YvpXrr85egLIJyzyWBWT+2VfYiu9mojmO4fr39mlLCCFVDHQ Y/g== X-Google-Smtp-Source: AGHT+IHr3YxVY4T6srwZUwxI0ZW6g2YPlW69vv/o0vnwcKqZpomk75DDB0odCx5hUB1kSCPXFWYmf+EoR1o= X-Received: from pjbsd14.prod.google.com ([2002:a17:90b:514e:b0:2ea:5613:4d5d]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2588:b0:2ee:c9b6:c26a with SMTP id 98e67ed59e1d1-2f548eae05amr33135273a91.11.1736807268726; Mon, 13 Jan 2025 14:27:48 -0800 (PST) Reply-To: Sean Christopherson Date: Mon, 13 Jan 2025 14:27:39 -0800 In-Reply-To: <20250113222740.1481934-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250113222740.1481934-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.688.g23fc6f90ad-goog Message-ID: <20250113222740.1481934-5-seanjc@google.com> Subject: [PATCH 4/5] 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 , stable@vger.kernel 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); } 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); From patchwork Mon Jan 13 22:27:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13938180 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 1B1071F943C for ; Mon, 13 Jan 2025 22:27:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736807272; cv=none; b=jONdZiSxI4+Vd/xtDr218pM5FViIdiRQeOYesr3Xs0OQ/f7kZpEmaJBG+FvC0dNMR9h8WeCzFEoDeF0PuXYSXnHwhWyxuvxcY7eAAzpc7GPw85GkqB0oXflkyKfy4+ZhTaS109iT5aGbxWIafifpV4zwjCxXSnB7UMTftrJJP4o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736807272; c=relaxed/simple; bh=rVO7MBo0ZRsY1HA2eYCKrFCp2627yEJRpTuMc9vzE00=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QabEs9HSoXc9bVYBeYWOcliF0rAiHS+yEC16/vRZTFVV/gHHJjZ0WN2VDSju2IX37lDjZyVwHYLxs018qWjIbd1fY+PjSshXNoykf9yBhgq/LrpgOxgQwIko7n+u2rO46GQ1becp0/bK07CPsrh38IM69L5UJYPbVzIpvpQE97k= 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=WJI5KfBS; arc=none smtp.client-ip=209.85.216.73 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="WJI5KfBS" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2efc3292021so12542412a91.1 for ; Mon, 13 Jan 2025 14:27:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736807270; x=1737412070; 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=jEclE4N/e4eNENlrwuP3qv7v6wm9foEArsL1tPTX0Ls=; b=WJI5KfBS5lcAQbkxHWvNHdz1qnITUV+sk5jbHKFTHW/xPQhdqsK/VtepuTQQjPHMkP cAodJnp83YHRKKOvBSG9iGGojEV/h0jeAXotGm8+wKYbt+n7o5sTgGXnJ0/84Ii3KV1O MYy4qLh2K339/MZaWBHuUUsfw/MAMLEhvu137howCi38fiNAzSdwi3AEu6JJevxLEDqO +OkJIeUpEQQ6O7s6yGQtQtljZk2mrfDbusq5YhNpnJ9aZntCrKd/acEj0sd/Q/YXaYMp 2hLuomNrbBvUdZrpLE0xhcgfc5pFbhphOybTHwWOaVfMm42yBejqnR0aomlEzEp3breW NAmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736807270; x=1737412070; 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=jEclE4N/e4eNENlrwuP3qv7v6wm9foEArsL1tPTX0Ls=; b=sM7NHxW0yqsqD3zlsQ0IALMAC5vIvm5NGEOINWfj7BK9sIXQqHEbgc/rynkQlB3noB //zM3wQrPpCPl3UfZLSbBJEYk02mZNjGwzfhPoT/X3kfqTPedsdjiJm9rMl6/2V4xCxD lYHREL1C1e19QKZFTXKfne4mVUqZVJfoG1vNGdqiGRFV07NYDCOWEhTAjsE5PrgZ9FrT 7+g20h323YJ5tylq/oTnViwdlTMcXTqfhaEQERqrmv7K8SyRLn8ptU8YyZ23Yql8GIC2 EovoJGdle8HrN4y3Feuyx7cMlwY0wUFQHO+hp4FVeZ5KTD1Tlq/NMOlKQ37fGVboYEIc 7FIQ== X-Gm-Message-State: AOJu0Yyby/3AaNfDPf5syuH/DkmqC7b1x9eDbOpx07mn6tUf0nIwlUOo WzZKYnqqOoAoRfVK/KZUB48Vb6UUmKkN6JZu0xpgtl8kFsSUaTV7gDbMQ1ZAdpEtAqT5oJtUK/l BKg== X-Google-Smtp-Source: AGHT+IFiLFKOYHUapcng1sIBy0z/SlnfN8YnWN43Y6s4+aq1QD/w0XiZiCqkv3J1FCAtlMyUn3VklWhmgG4= X-Received: from pjbov7.prod.google.com ([2002:a17:90b:2587:b0:2f4:3e59:8bb1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:53c7:b0:2ee:9e06:7db0 with SMTP id 98e67ed59e1d1-2f548f3400fmr33844299a91.11.1736807270414; Mon, 13 Jan 2025 14:27:50 -0800 (PST) Reply-To: Sean Christopherson Date: Mon, 13 Jan 2025 14:27:40 -0800 In-Reply-To: <20250113222740.1481934-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250113222740.1481934-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.688.g23fc6f90ad-goog Message-ID: <20250113222740.1481934-6-seanjc@google.com> Subject: [PATCH 5/5] KVM: selftests: Add CPUID tests for Hyper-V features that need in-kernel APIC From: Sean Christopherson To: Vitaly Kuznetsov , Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Dongjie Zou , stable@vger.kernel Add testcases to x86's Hyper-V CPUID test to verify that KVM advertises support for features that require an in-kernel local APIC appropriately, i.e. that KVM hides support from the vCPU-scoped ioctl if the VM doesn't have an in-kernel local APIC. Cc: Vitaly Kuznetsov Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c index 90c44765d584..f76b6a95b530 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c @@ -43,6 +43,7 @@ static bool smt_possible(void) static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected) { + const bool has_irqchip = !vcpu || vcpu->vm->has_irqchip; const struct kvm_cpuid2 *hv_cpuid_entries; int i; int nent_expected = 10; @@ -85,12 +86,19 @@ static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected) entry->eax, evmcs_expected ); break; + case 0x40000003: + TEST_ASSERT(has_irqchip || !(entry->edx & BIT(19)), + "Synthetic Timers should require in-kernel APIC"); + break; case 0x40000004: test_val = entry->eax & (1UL << 18); TEST_ASSERT(!!test_val == !smt_possible(), "NoNonArchitecturalCoreSharing bit" " doesn't reflect SMT setting"); + + TEST_ASSERT(has_irqchip || !(entry->eax & BIT(10)), + "Cluster IPI (i.e. SEND_IPI) should require in-kernel APIC"); break; case 0x4000000A: TEST_ASSERT(entry->eax & (1UL << 19), @@ -139,9 +147,14 @@ int main(int argc, char *argv[]) TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID)); - vm = vm_create_with_one_vcpu(&vcpu, guest_code); + /* Test the vCPU ioctl without an in-kernel local APIC. */ + vm = vm_create_barebones(); + vcpu = __vm_vcpu_add(vm, 0); + test_hv_cpuid(vcpu, false); + kvm_vm_free(vm); /* Test vCPU ioctl version */ + vm = vm_create_with_one_vcpu(&vcpu, guest_code); test_hv_cpuid_e2big(vm, vcpu); test_hv_cpuid(vcpu, false);