From nobody Sat Feb 7 17:50:57 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 422264F615 for ; Fri, 2 Aug 2024 19:51:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722628286; cv=none; b=GdV8pfSqFOu/Wr08PKuCveZYNMjOlQaBdJZiEj2WExVCTq/ykPCayQLz3W4pMc1yn5Q9aXUwHQcVtPotm/vliypcbza6PJ1e2AalyoU6Y2quf3pf4QjAxOMowp0NJ0MoocG96NVmbGX9e7kcLU3HXjgE5fMYcXy8rp/5DSPJb1g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722628286; c=relaxed/simple; bh=zql+KU31olSPyMQGIOOmlA41hrQI7991hIRTrHnkF5g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WIEPhoWPOEoDi/yT1HGnGGBHFF6xn3TBe0697sblHTHFAlipm0sOpszEWsUWS1GzPOEPLMcdDGMnM3Y8QiwY2fa6Rgjhg96/EGnS4rSGL/cxhWdnfQA1jSRD6iLlxyF1ic6rHGIyUAcpb0OSdq3TA9xxZUh995FLDQxw66O33P0= 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=RvfoDdEd; arc=none smtp.client-ip=209.85.219.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="RvfoDdEd" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e05e9e4dfbeso13185417276.1 for ; Fri, 02 Aug 2024 12:51:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722628284; x=1723233084; 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=j3MDuI39zu1XA4gRdGvyMxqjMSntoDT90qhT0kUFZqs=; b=RvfoDdEdubGsBZqZvhW7Mwi2ErJo/qjP2Dc430keGlJ4PbjVJ+396w4eXSD62UNmt2 WTJGpuLtEZJcKN0qUZF1J2e3l28yUH9j/TvNSDAtA843Z6W59goEPq8Kl60flLVuXCKW t1vaFZ6RArUF9ojfUtVUaKggx9Wz8T6gERL1oClnZD3FmuF4GED045x7NBdF3Ni07+Xm dUXCF24Jd43HLSqOuX8LS25M1UawDTHoZfSBdDR/Qq5w4et58tv84mPSVfvAzluVBwJx l0TGP1hVNtUJ+F9bC0hU2YVJykcivKxtTKQBDNlwynuYMN+Uv2HgCk0i6gOASzRkU+lu wbNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722628284; x=1723233084; 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=j3MDuI39zu1XA4gRdGvyMxqjMSntoDT90qhT0kUFZqs=; b=knM7qQRaUEZLIYdAEP9RDjy6b/r4avf5ohsLZmuhG8ZYlyYrGwZFcD/oz0eScvlaa0 211GlioeLkgzqgRw7bLn5BLAoBxOizUZ+McEZyWXWQaNVKI6Gq0/gFnss/C4v205lb22 2u0yJrOvbyT4q+p7C6XMy6ITKjkcwDuC3kDApCWBsAO5ffg9xH4hnzXBHX7NyPQdVLKl 6pdiThR/+nh0oJ6ttirUw7zbl4g+WTA/KwW4/q75QbMs003LfdEEChBzTLj/Hw2pYipy IH4ljp4RjBDEFWhW7ZcAuCeYeEz0lrPWzUTu8jjkhioW9dwD+hR+3JdDXHc4gnO7s2Sn mO8Q== X-Forwarded-Encrypted: i=1; AJvYcCVH5RO4rMotj4IivrRPAyGGrEiA+lGevP1GJReKrY1fbad0bLIPySPrbnasmaBJMowGZg0T4LKWoJnLxGOD8SisnVv6dRehwCjHDl8Z X-Gm-Message-State: AOJu0YwkYyEhcvSHK9SDNqMgdCmz4ae9bnzmsfdKcV5jCMgvT0K4ElDD L3ok3NUXjmUKAr8jceQIcSLzeEsYC+nj3Gb3DndgXMmmg7+r6tA1gPTvvrBwh+4dWw6+mIpG6DR Bog== X-Google-Smtp-Source: AGHT+IHXmtQ4fn/w4A9o1IxjByT19mNXJlLz8OGSPq2+ASik931Ksxoz9M3ugqUoQtYF2Bf10ojDjPLGf1M= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a5b:405:0:b0:e0b:f69b:da30 with SMTP id 3f1490d57ef6-e0bf69be1bdmr634276.9.1722628284272; Fri, 02 Aug 2024 12:51:24 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 12:51:16 -0700 In-Reply-To: <20240802195120.325560-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: <20240802195120.325560-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802195120.325560-2-seanjc@google.com> Subject: [PATCH 1/5] KVM: x86: Re-enter guest if WRMSR(X2APIC_ICR) fastpath is successful From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Re-enter the guest in the fastpath if WRMSR emulation for x2APIC's ICR is successful, as no additional work is needed, i.e. there is no code unique for WRMSR exits between the fastpath and the "!=3D EXIT_FASTPATH_NONE" check in __vmx_handle_exit(). Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index af6c8cf6a37a..cf397110953f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2173,7 +2173,7 @@ fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_= vcpu *vcpu) data =3D kvm_read_edx_eax(vcpu); if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) { kvm_skip_emulated_instruction(vcpu); - ret =3D EXIT_FASTPATH_EXIT_HANDLED; + ret =3D EXIT_FASTPATH_REENTER_GUEST; } break; case MSR_IA32_TSC_DEADLINE: --=20 2.46.0.rc2.264.g509ed76dc8-goog From nobody Sat Feb 7 17:50:57 2026 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 D476F6E611 for ; Fri, 2 Aug 2024 19:51:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722628289; cv=none; b=vBD+8R7DK2uT3/dfDytlP745jj2MiiJRFej/ryeWUuN+Ai0iaNlyWD+Sh42hinjlKlYywYr21YQeSq1cBzFTptoIeW45X5xr3tYURUavgNcD1Pg7ks8917yDGNUfrIgJomI/XOlJZiG6SdU1vK/rHgzNquytdA+bTtCjKKqukK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722628289; c=relaxed/simple; bh=TlVRiUICJX5iI1P2EisP0czzGlPcN/R9pgkv0nTLVyw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JCRVuUPLndY/F6et5DnfT/mLmAS8sBavdAG4bgqdcV+oBc6rAbcaFHDuYCNR9rZ1E+sBIPwjpJ+lThpbt3tuFaWmuzqBCrlw2hRmOWC4oVXVNVUalm2F4KZg8wT0sJAziqeui2t4x6grJp0WQmwYdKH7Qts9r8RJqyvh89QQSnw= 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=gRfxlCDL; arc=none smtp.client-ip=209.85.128.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="gRfxlCDL" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6688b5b40faso201461027b3.2 for ; Fri, 02 Aug 2024 12:51:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722628286; x=1723233086; 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=iMEFOq5Tk4KyJyaLBMXv5gnfBdOhdNIzblrVnJpsjo0=; b=gRfxlCDL95WQMFsto3I01rjyW+WQ3sYDWiGzxuUpkwFgpmdMEYgqPmYRE8VH7vYuTM Tq26s1pmLTmT8OUfMFkPFRmvu6l4P3ex/mPUobuH2Qnx/SIkWGQ8QztQjJCwNQ4BR0Di k5ybn97zdOfP717Id8ZKa1HsoNnqj32PU5ksWkqJwiZbtftrffS1AnGVksu/hoCaoYaF KZ2Q6xSJfNy9hDnq9FuSMEyPLsEw4gpJB001kcjNl5cgtFRHwuEwHwBmjQT6s4zitsX7 H1YftSItbuxZjBHHIazGIuezsJMoH1tGEyRiZ3TvqVNcjCfVYjQ2kL1422+h/G0sVsrN 5Vrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722628286; x=1723233086; 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=iMEFOq5Tk4KyJyaLBMXv5gnfBdOhdNIzblrVnJpsjo0=; b=jSphE4GGHysYwpR6Z+m5wo1wpMtSUP9e1kk7vWHArAl1Ro9EsQOMKm6qOt8RIVVHl2 UVLCTLz20bQPMqt5EwDMj53+LRpqjEj5/OvttLx4uamZ9Pf1JUGyXH1QJW+lGFL8sWti 1krR4qqMBhmDzXoImuP3SCDB+qLbJ8+1mprGOjPdbelTTbfwcb6Bol4RfvmwkhFhVTRw MnQpa89XdajPSiVx8T000Nl1nHr/RTa2WS9o8f0GdTzu5h2VpHcyWN3n3WPyUPMEiyTB jmUv6hZIZ6hABzX1OpElH4T721/h4joI1LCDf12oSs844arBgtCVhmHD0bN2P1Sm4m+l PK5w== X-Forwarded-Encrypted: i=1; AJvYcCUA0oVYmG/K1Cmk8hWu3lyFypsPIBde1GHZVov4KILrC9BiEXlTucKP+OhlW+d0kUbYItkZH/DLUnms5AYfScFXJ7sjhtsiHWQicjzE X-Gm-Message-State: AOJu0YzvS5+Fv6+ENkyBhw9KuExFMut2qPv1ChwvJOk0sEPXGrPASXXc nUe1YHfTsmS4aTN6pbItFH39izBLQRDg11acrKwhDtp55FZg099+wCxkpY+gzGYWNvpBdID9jpx hPg== X-Google-Smtp-Source: AGHT+IEMQIhmUdXTQxP5VzE0A3c9BCZw8dM8LR647TTNv36aIGWuOmIV2yy9svYvy4E20onflnwZ7K40qLM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:113:b0:62c:f6fd:5401 with SMTP id 00721157ae682-68963bd9bb6mr1995527b3.6.1722628285936; Fri, 02 Aug 2024 12:51:25 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 12:51:17 -0700 In-Reply-To: <20240802195120.325560-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: <20240802195120.325560-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802195120.325560-3-seanjc@google.com> Subject: [PATCH 2/5] KVM: x86: Dedup fastpath MSR post-handling logic From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that the WRMSR fastpath for x2APIC_ICR and TSC_DEADLINE are identical, ignoring the backend MSR handling, consolidate the common bits of skipping the instruction and setting the return value. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index cf397110953f..332584476129 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2164,31 +2164,32 @@ fastpath_t handle_fastpath_set_msr_irqoff(struct kv= m_vcpu *vcpu) { u32 msr =3D kvm_rcx_read(vcpu); u64 data; - fastpath_t ret =3D EXIT_FASTPATH_NONE; + fastpath_t ret; + bool handled; =20 kvm_vcpu_srcu_read_lock(vcpu); =20 switch (msr) { case APIC_BASE_MSR + (APIC_ICR >> 4): data =3D kvm_read_edx_eax(vcpu); - if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) { - kvm_skip_emulated_instruction(vcpu); - ret =3D EXIT_FASTPATH_REENTER_GUEST; - } + handled =3D !handle_fastpath_set_x2apic_icr_irqoff(vcpu, data); break; case MSR_IA32_TSC_DEADLINE: data =3D kvm_read_edx_eax(vcpu); - if (!handle_fastpath_set_tscdeadline(vcpu, data)) { - kvm_skip_emulated_instruction(vcpu); - ret =3D EXIT_FASTPATH_REENTER_GUEST; - } + handled =3D !handle_fastpath_set_tscdeadline(vcpu, data); break; default: + handled =3D false; break; } =20 - if (ret !=3D EXIT_FASTPATH_NONE) + if (handled) { + kvm_skip_emulated_instruction(vcpu); + ret =3D EXIT_FASTPATH_REENTER_GUEST; trace_kvm_msr_write(msr, data); + } else { + ret =3D EXIT_FASTPATH_NONE; + } =20 kvm_vcpu_srcu_read_unlock(vcpu); =20 --=20 2.46.0.rc2.264.g509ed76dc8-goog From nobody Sat Feb 7 17:50:57 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 91F057C0B7 for ; Fri, 2 Aug 2024 19:51:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722628289; cv=none; b=bHP71J8OTjisPyx9GHrg7EXlu3tjWHfwgszrhbGdDKPHV5lcTMKH7ZySOO1zTuHRDNrPy3JZEu6+pQeQ/k6ZkJQsWw+7tpT3S0g40myifOKNlDEvWKacdJHhoCOKY26CT8vnCVsZKwejvyXeSmeElb8kLQ6yLxV4HPREKS5Q79s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722628289; c=relaxed/simple; bh=kH7IYR7zBJzDgCb4mp8jhpOwUdQ4CJQGnGMEivOUcfI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Dy7bKb69ofKLU0yem/6i+5T6uxGA7zX7OoHi3qkPviHhaegoZwgIHzcYgrCOCx0ETL30cJGSSZltQc/NeaS8d/NvScyQFKwh5Jh7t0mtxrLlGUF8TPp0ZzPoWS4sRjOb/ye+aL93lYXppY4d6g/P8AYE62KytLjOBUJlOmpf7KQ= 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=czdn7o28; arc=none smtp.client-ip=209.85.215.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="czdn7o28" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7a732cdd03fso6946395a12.0 for ; Fri, 02 Aug 2024 12:51:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722628288; x=1723233088; 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=0yZA/m2BmuooHYMrQtHZI/2LD/LZNOm46ViK83mQlGA=; b=czdn7o28UWBh1H7Rc6p1josjWAiFIKyOWzYuJM2avuxuo+DpalcHk4+DxQmYq9QssD 7RLFlgTi8v4amYOGS2pXruOfKlcHSJC7NiBzRuT3cOm+s2bXcv94999MsROqaXp1/7mp 6JGmDBuaQ15Ejef9ShW3Ek0b3wt+VyC8sjNCAIfwCjC4lGZ8/2guXXiQ3D7dTr2fYfY4 86SuLk8cpB7tKZxL/uwR09xcjPrI3u5t8kPpIaurYAp4kzyYxIVna4yu/Jxn3T8Du4pn pISa0m6s3nvpTORm0XhuTcX7mrA3pLhOfUf+z9Gq5b4n1SNR6Xil4KtFhDScqXMtzG2l 6NHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722628288; x=1723233088; 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=0yZA/m2BmuooHYMrQtHZI/2LD/LZNOm46ViK83mQlGA=; b=kolBHZPuZYVn3jv39QsnLkktPnMxPiwOUrftFKvW19qwUjDn8kxtf63jF37VRk9SOn 3pp3YFRglYs+x+fWw9UHmEfd+3i0iuOFm+5F+STBKJcDu2DAHDZ9xfeOVYIU25khZshN FQLUeWgnsbJqx/wIo5E/lzTAt7t+9kb/m0jIfdTL5hvQufhlFRihwmSCPlSSYWe0PC9T FRKRbkTqb36lLYIM/ZoHfCTo/+gwXteg+x1NhJSjN6LghvVHw2mAaMvPVo6ajyjH10vV /jhOacUY0OcyMPlKBA1OwVCZfLmoP5YL30MFQb0KCkZu3O4m1QXgue1IUm5yTnZQZjSW paqw== X-Forwarded-Encrypted: i=1; AJvYcCWXiT0TvJIqGzR8tOy+rgBJPM/NClTTmvQAH7h/Beee+tD7/OmkJnuzx3KxKuqZPXjYPnjDpFaqzZ9tsxRR4aZrsEKU3AuG/L7bBZkh X-Gm-Message-State: AOJu0YzubmmEJDMhwLXdeDV83e9mu4NKsljZVpYWfB1F8WvzSfa4VYod ydgndO/YqRTrNFo5PzhZBZeQZAYURrta8f8N2u4FcIZ5kZApIEu5d92PhoYH1KgazxYAHmwt2ni GZw== X-Google-Smtp-Source: AGHT+IENPeRQ23Bb2K/Kq1zD91vd3dLeBsSlYk8jiLIo5PGRaYnIsL438VpxjIKwYaVrRR+p4LzacGFzdNU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:6307:0:b0:75c:49a4:2a2c with SMTP id 41be03b00d2f7-7b749d180e2mr7687a12.7.1722628287593; Fri, 02 Aug 2024 12:51:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 12:51:18 -0700 In-Reply-To: <20240802195120.325560-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: <20240802195120.325560-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802195120.325560-4-seanjc@google.com> Subject: [PATCH 3/5] KVM: x86: Exit to userspace if fastpath triggers one on instruction skip From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Exit to userspace if a fastpath handler triggers such an exit, which can happen when skipping the instruction, e.g. due to userspace single-stepping the guest via KVM_GUESTDBG_SINGLESTEP or because of an emulation failure. Fixes: 404d5d7bff0d ("KVM: X86: Introduce more exit_fastpath_completion enu= m values") Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 950a03e0181e..736dda300849 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -211,6 +211,7 @@ enum exit_fastpath_completion { EXIT_FASTPATH_NONE, EXIT_FASTPATH_REENTER_GUEST, EXIT_FASTPATH_EXIT_HANDLED, + EXIT_FASTPATH_EXIT_USERSPACE, }; typedef enum exit_fastpath_completion fastpath_t; =20 diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 332584476129..3c54a241696f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2184,8 +2184,10 @@ fastpath_t handle_fastpath_set_msr_irqoff(struct kvm= _vcpu *vcpu) } =20 if (handled) { - kvm_skip_emulated_instruction(vcpu); - ret =3D EXIT_FASTPATH_REENTER_GUEST; + if (!kvm_skip_emulated_instruction(vcpu)) + ret =3D EXIT_FASTPATH_EXIT_USERSPACE; + else + ret =3D EXIT_FASTPATH_REENTER_GUEST; trace_kvm_msr_write(msr, data); } else { ret =3D EXIT_FASTPATH_NONE; @@ -11206,6 +11208,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) if (vcpu->arch.apic_attention) kvm_lapic_sync_from_vapic(vcpu); =20 + if (unlikely(exit_fastpath =3D=3D EXIT_FASTPATH_EXIT_USERSPACE)) + return 0; + r =3D kvm_x86_call(handle_exit)(vcpu, exit_fastpath); return r; =20 --=20 2.46.0.rc2.264.g509ed76dc8-goog From nobody Sat Feb 7 17:50:57 2026 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 5D641130A5C for ; Fri, 2 Aug 2024 19:51:30 +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=1722628292; cv=none; b=h3HuRzKpHLqlTnhBMld+yPuyJB1OVgi6TJUqWa/Qm0hFPqQYSgG2wTEY9/G1Y7/N9OJKt2cEXLsOth4+/I/Hv3+dlMeu6kGaD71pcccs53O2nNwGwWtB+mOMKqIjByZDU53HZcWLxTYkOIBs/Nl6nkAorMhnVSJbQCJoAnnHcVg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722628292; c=relaxed/simple; bh=GisDFGhm1yyVC6WE7YFlKal02q+ebK2NzRvmB0/ukH4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Bzg37XYJBdPjj96b8sEhSdlFbDnkeojj7qRpdyXa4ijPIX01kUxQmUBSW82TPQn/cdQwHGDbCHaG1pKAtzBU/6MMmUnNClsGkLyo+HSougue1QUmePw94WIHlsVCSwIs3kPeN7Ma4haDa8RHaWu6CH7T7wSjPjhB62PG5deFJEE= 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=A/6RYMB7; 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="A/6RYMB7" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-1fc5e1ab396so86848675ad.2 for ; Fri, 02 Aug 2024 12:51:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722628289; x=1723233089; 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=rTtTuEYhkp7ZliDdCROPWm4/aH5E9MbOqSEDHs65qFg=; b=A/6RYMB7DxMNCw0lA6At4tRxJqtHXS87PyI1lKNVoylXhdaogfKBdS2dDADgngHVai 1cRaUxXK0pzLO78vPRQy+FAlWJWGYzuw8xnPODS4CEQmZ0P7r+DiU7nOhmmYdSjpdD7l fPXfHPV1wPRjqZOu5GaZNqGUEZ6MbOIch/Nc2ysoyZOUTV15g7xbgIha8XNaN8OhgXHg HjSvNjYFW/2273E6AbZiWjE4daNO47Q36UjQbJZt+bq7i/ekAa/SyLZi/ROGyTkzrjmK ffTuq8BW1Dg67Ml+UqoTQVDcncPDpr7m2aWEQyZHjsIyZ7TqkEMdYE+GFZnEHB5s1TlF k/eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722628289; x=1723233089; 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=rTtTuEYhkp7ZliDdCROPWm4/aH5E9MbOqSEDHs65qFg=; b=OkUQBnW8unFVBk8dodfPn2qTn1kTwlWW4fPkMsjxkMmYLlHRgti66mgs+HKF31T6mN kaeUnEma79zH+XE7oIT0Vj+po977Wv4ICSGZk2FIG/zL6sr3Y8sUVrS/7RnuwuzAVxJN yqORvOWlpFXegOqISpCACYGwq+gR2CI4XlOQRRfjm6MTypasrpTujdaVzdbeK/hKVwCV cyKQNUOEHu1I0SeXyIveL6QUNTqdb2iHgMV4XY8NvT1fahFyTVDfr6izKK+aDmvM5Bop 7ZuKvCgToB/4wy/rnP4+Ky+ZERav05HHPhzbU5u/dlvpBhCKeBic/Cv497nBj+J26aUL uADA== X-Forwarded-Encrypted: i=1; AJvYcCVcBt49WLnWDZy0sgCKeQTenZ2UK0HKeOApThrGbW46nvuc7OwnCQZUdmHmNXqE01xuV3xw2uXfPrEEU+pIgYqs7fnVNZZHJTUqFSiC X-Gm-Message-State: AOJu0Yx9eRsvYMWfgNGFxmHhIeCQbb70cxHL4Qn4yXRFfkoNglLWWKpF nihOT3zhP1V/qEGwySlpquuwq1OhjLelJph0NHDzhcNgck95R0JP5mgPnpz/Cs0y2bir5oBlrY6 M8w== X-Google-Smtp-Source: AGHT+IFBUss19Xpkp75NzbgRP0olqSoTDLPxcqWGsDO3MC/8JqTWqabd+CJKuvDdDMWWc9kapW0n3uESCPc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:ec92:b0:1fb:f03:8935 with SMTP id d9443c01a7336-1ff573e3bc2mr3366305ad.7.1722628289493; Fri, 02 Aug 2024 12:51:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 12:51:19 -0700 In-Reply-To: <20240802195120.325560-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: <20240802195120.325560-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802195120.325560-5-seanjc@google.com> Subject: [PATCH 4/5] KVM: x86: Reorganize code in x86.c to co-locate vCPU blocking/running helpers From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Shuffle code around in x86.c so that the various helpers related to vCPU blocking/running logic are (a) located near each other and (b) ordered so that HLT emulation can use kvm_vcpu_has_events() in a future path. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 264 ++++++++++++++++++++++----------------------- 1 file changed, 132 insertions(+), 132 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3c54a241696f..46686504cd47 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9927,51 +9927,6 @@ void kvm_x86_vendor_exit(void) } EXPORT_SYMBOL_GPL(kvm_x86_vendor_exit); =20 -static int __kvm_emulate_halt(struct kvm_vcpu *vcpu, int state, int reason) -{ - /* - * The vCPU has halted, e.g. executed HLT. Update the run state if the - * local APIC is in-kernel, the run loop will detect the non-runnable - * state and halt the vCPU. Exit to userspace if the local APIC is - * managed by userspace, in which case userspace is responsible for - * handling wake events. - */ - ++vcpu->stat.halt_exits; - if (lapic_in_kernel(vcpu)) { - vcpu->arch.mp_state =3D state; - return 1; - } else { - vcpu->run->exit_reason =3D reason; - return 0; - } -} - -int kvm_emulate_halt_noskip(struct kvm_vcpu *vcpu) -{ - return __kvm_emulate_halt(vcpu, KVM_MP_STATE_HALTED, KVM_EXIT_HLT); -} -EXPORT_SYMBOL_GPL(kvm_emulate_halt_noskip); - -int kvm_emulate_halt(struct kvm_vcpu *vcpu) -{ - int ret =3D kvm_skip_emulated_instruction(vcpu); - /* - * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered - * KVM_EXIT_DEBUG here. - */ - return kvm_emulate_halt_noskip(vcpu) && ret; -} -EXPORT_SYMBOL_GPL(kvm_emulate_halt); - -int kvm_emulate_ap_reset_hold(struct kvm_vcpu *vcpu) -{ - int ret =3D kvm_skip_emulated_instruction(vcpu); - - return __kvm_emulate_halt(vcpu, KVM_MP_STATE_AP_RESET_HOLD, - KVM_EXIT_AP_RESET_HOLD) && ret; -} -EXPORT_SYMBOL_GPL(kvm_emulate_ap_reset_hold); - #ifdef CONFIG_X86_64 static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr, unsigned long clock_type) @@ -11224,6 +11179,67 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) return r; } =20 +static bool kvm_vcpu_running(struct kvm_vcpu *vcpu) +{ + return (vcpu->arch.mp_state =3D=3D KVM_MP_STATE_RUNNABLE && + !vcpu->arch.apf.halted); +} + +static bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) +{ + if (!list_empty_careful(&vcpu->async_pf.done)) + return true; + + if (kvm_apic_has_pending_init_or_sipi(vcpu) && + kvm_apic_init_sipi_allowed(vcpu)) + return true; + + if (vcpu->arch.pv.pv_unhalted) + return true; + + if (kvm_is_exception_pending(vcpu)) + return true; + + if (kvm_test_request(KVM_REQ_NMI, vcpu) || + (vcpu->arch.nmi_pending && + kvm_x86_call(nmi_allowed)(vcpu, false))) + return true; + +#ifdef CONFIG_KVM_SMM + if (kvm_test_request(KVM_REQ_SMI, vcpu) || + (vcpu->arch.smi_pending && + kvm_x86_call(smi_allowed)(vcpu, false))) + return true; +#endif + + if (kvm_test_request(KVM_REQ_PMI, vcpu)) + return true; + + if (kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) + return true; + + if (kvm_arch_interrupt_allowed(vcpu) && kvm_cpu_has_interrupt(vcpu)) + return true; + + if (kvm_hv_has_stimer_pending(vcpu)) + return true; + + if (is_guest_mode(vcpu) && + kvm_x86_ops.nested_ops->has_events && + kvm_x86_ops.nested_ops->has_events(vcpu, false)) + return true; + + if (kvm_xen_has_pending_events(vcpu)) + return true; + + return false; +} + +int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) +{ + return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu); +} + /* Called within kvm->srcu read side. */ static inline int vcpu_block(struct kvm_vcpu *vcpu) { @@ -11295,12 +11311,6 @@ static inline int vcpu_block(struct kvm_vcpu *vcpu) return 1; } =20 -static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu) -{ - return (vcpu->arch.mp_state =3D=3D KVM_MP_STATE_RUNNABLE && - !vcpu->arch.apf.halted); -} - /* Called within kvm->srcu read side. */ static int vcpu_run(struct kvm_vcpu *vcpu) { @@ -11352,6 +11362,77 @@ static int vcpu_run(struct kvm_vcpu *vcpu) return r; } =20 +static int __kvm_emulate_halt(struct kvm_vcpu *vcpu, int state, int reason) +{ + /* + * The vCPU has halted, e.g. executed HLT. Update the run state if the + * local APIC is in-kernel, the run loop will detect the non-runnable + * state and halt the vCPU. Exit to userspace if the local APIC is + * managed by userspace, in which case userspace is responsible for + * handling wake events. + */ + ++vcpu->stat.halt_exits; + if (lapic_in_kernel(vcpu)) { + vcpu->arch.mp_state =3D state; + return 1; + } else { + vcpu->run->exit_reason =3D reason; + return 0; + } +} + +int kvm_emulate_halt_noskip(struct kvm_vcpu *vcpu) +{ + return __kvm_emulate_halt(vcpu, KVM_MP_STATE_HALTED, KVM_EXIT_HLT); +} +EXPORT_SYMBOL_GPL(kvm_emulate_halt_noskip); + +int kvm_emulate_halt(struct kvm_vcpu *vcpu) +{ + int ret =3D kvm_skip_emulated_instruction(vcpu); + /* + * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered + * KVM_EXIT_DEBUG here. + */ + return kvm_emulate_halt_noskip(vcpu) && ret; +} +EXPORT_SYMBOL_GPL(kvm_emulate_halt); + +int kvm_emulate_ap_reset_hold(struct kvm_vcpu *vcpu) +{ + int ret =3D kvm_skip_emulated_instruction(vcpu); + + return __kvm_emulate_halt(vcpu, KVM_MP_STATE_AP_RESET_HOLD, + KVM_EXIT_AP_RESET_HOLD) && ret; +} +EXPORT_SYMBOL_GPL(kvm_emulate_ap_reset_hold); + +bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu) +{ + return kvm_vcpu_apicv_active(vcpu) && + kvm_x86_call(dy_apicv_has_pending_interrupt)(vcpu); +} + +bool kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu) +{ + return vcpu->arch.preempted_in_kernel; +} + +bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) +{ + if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) + return true; + + if (kvm_test_request(KVM_REQ_NMI, vcpu) || +#ifdef CONFIG_KVM_SMM + kvm_test_request(KVM_REQ_SMI, vcpu) || +#endif + kvm_test_request(KVM_REQ_EVENT, vcpu)) + return true; + + return kvm_arch_dy_has_pending_interrupt(vcpu); +} + static inline int complete_emulated_io(struct kvm_vcpu *vcpu) { return kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE); @@ -13163,87 +13244,6 @@ void kvm_arch_commit_memory_region(struct kvm *kvm, kvm_arch_free_memslot(kvm, old); } =20 -static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) -{ - if (!list_empty_careful(&vcpu->async_pf.done)) - return true; - - if (kvm_apic_has_pending_init_or_sipi(vcpu) && - kvm_apic_init_sipi_allowed(vcpu)) - return true; - - if (vcpu->arch.pv.pv_unhalted) - return true; - - if (kvm_is_exception_pending(vcpu)) - return true; - - if (kvm_test_request(KVM_REQ_NMI, vcpu) || - (vcpu->arch.nmi_pending && - kvm_x86_call(nmi_allowed)(vcpu, false))) - return true; - -#ifdef CONFIG_KVM_SMM - if (kvm_test_request(KVM_REQ_SMI, vcpu) || - (vcpu->arch.smi_pending && - kvm_x86_call(smi_allowed)(vcpu, false))) - return true; -#endif - - if (kvm_test_request(KVM_REQ_PMI, vcpu)) - return true; - - if (kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) - return true; - - if (kvm_arch_interrupt_allowed(vcpu) && kvm_cpu_has_interrupt(vcpu)) - return true; - - if (kvm_hv_has_stimer_pending(vcpu)) - return true; - - if (is_guest_mode(vcpu) && - kvm_x86_ops.nested_ops->has_events && - kvm_x86_ops.nested_ops->has_events(vcpu, false)) - return true; - - if (kvm_xen_has_pending_events(vcpu)) - return true; - - return false; -} - -int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) -{ - return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu); -} - -bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu) -{ - return kvm_vcpu_apicv_active(vcpu) && - kvm_x86_call(dy_apicv_has_pending_interrupt)(vcpu); -} - -bool kvm_arch_vcpu_preempted_in_kernel(struct kvm_vcpu *vcpu) -{ - return vcpu->arch.preempted_in_kernel; -} - -bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) -{ - if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) - return true; - - if (kvm_test_request(KVM_REQ_NMI, vcpu) || -#ifdef CONFIG_KVM_SMM - kvm_test_request(KVM_REQ_SMI, vcpu) || -#endif - kvm_test_request(KVM_REQ_EVENT, vcpu)) - return true; - - return kvm_arch_dy_has_pending_interrupt(vcpu); -} - bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) { if (vcpu->arch.guest_state_protected) --=20 2.46.0.rc2.264.g509ed76dc8-goog From nobody Sat Feb 7 17:50:57 2026 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 46308136E37 for ; Fri, 2 Aug 2024 19:51:32 +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=1722628293; cv=none; b=b8hvk03tkh2ragER+qHtU+OkJWQ23vXmuRoCvZaGyanYDJysRqtpW6XRb28riCVVNkLYnQEfG7/pDQSRN8+P3jMU+tQfhMjlZPXEV6UB39u9eDUyp5yf0w93EzESdLFQir2aHTPKT5EiqGiC2ic5R9n6c8HsasDEh4+rTiESlfk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722628293; c=relaxed/simple; bh=mqE0o9zRwOx1Mn/Z6tsZ+L0NFQbPg40KmGQ99XrGl84=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NVt9AIDSmUmdg3+L6gv7+fJX9YYZAUfmFyQfOyPotLS8wDsWy4Bu3bskCdqPWwmqGRRUyAiyuzE3dnBzKF4Z+e402Q/FnMtpuv+f0mvUU8nC1HdwH5AcjMxbEkBTl4lmVGd40AZIlfWLk5LLseIPTjI0hkfl55SM9ONZ5iyNm1k= 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=QMmrmz1H; 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="QMmrmz1H" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1fc60ef3076so79827555ad.1 for ; Fri, 02 Aug 2024 12:51:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722628291; x=1723233091; 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=nbb8wMxDZTZlGNXuFp2fO6u+E/pERXdN8QklXoMJTsY=; b=QMmrmz1HJQVD/3afA/OZM4byS7A4t0tDwa96junvLXjETMtlfpsxI/k0o1g2EjAWmG ZRtz8sWCitc8VPrmvrpdbYMJGbcR0Ao2GogV8gD4TtWElUgSAIw+v+86xCeZuhEReUe8 GtAUaEuG7n1ux6Y8VgyPaxCu/R2IXLu3lMUjcO/gmsWTpyXgIV810ML+l+ZOqEgqeVj8 Vm8wyfKq724oBn9g21HxhKVnV633B02qrh60uzZIKynAzEbx4S1g5m/EthBtBsKE3OFB Q/VRIaBQuafAXKoXGM68fXkk1j9dL8J2CshKVfoKw62HeOQ7Ihmw3q2qzbvzS/C/s1oM amMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722628291; x=1723233091; 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=nbb8wMxDZTZlGNXuFp2fO6u+E/pERXdN8QklXoMJTsY=; b=fu5cUTBR8liZ7FWviHtmnh5ajJXtPHdaMX+wnm2piqdMhgBswqXsFnDwubvdtRb2lb cQhdpkb8wpmcGQEnTY2O575QgHXO3CGFWx/YKbTVt4SCxEy8n0V5jGHaQ8K0GwD2s8n9 1vYRUmtiq+MaVMBbW8CpTsjaSxHrIT34SG7KhVUo9iysbo3sBQVeimOlOgmbcW2h4u0j +oIixuu2ddUR5qO7GDVsaDnRouCLWV+0QuQXZkycSOMbDIb7uQgEINr7LSurCOw63WEL ubdP3WywarY10aL4+QxsR+cM3pyjjULQESDOXWkQcn3fRlQVdXiKjHrB77Hw7+/f4BfB eXxA== X-Forwarded-Encrypted: i=1; AJvYcCVTb1sywp14PuizO9CYzr90GQkvaRvm1di4PMJNRj5nPWykVAWl12opUyXghH9uS5fWmyqaIM4Kv+5sSJn4qAatV7hwM7kbGQeAiYZb X-Gm-Message-State: AOJu0Yx8cF6wY87e5KoJoYzcvRoxTYiSET4cupVEK0HQGy835XO/Az9+ LVjiuWK+zBddsmqe7k2I/ihufXurHmrU1lXna05ry5nNIH/w7YK2WWNhLfchcRzZ2VQA8Ej51iu CGw== X-Google-Smtp-Source: AGHT+IEeAJQqKfvBQKb2VjxpO0q2I+GKyZiMO8d1EwpY/xEJMhUHvJjVefCWVqCWyOQEcOEK5i7Edt5hsBM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:c38c:b0:1fb:7f2c:5642 with SMTP id d9443c01a7336-1ff5730306cmr2707885ad.4.1722628291438; Fri, 02 Aug 2024 12:51:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 12:51:20 -0700 In-Reply-To: <20240802195120.325560-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: <20240802195120.325560-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802195120.325560-6-seanjc@google.com> Subject: [PATCH 5/5] KVM: x86: Add fastpath handling of HLT VM-Exits From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a fastpath for HLT VM-Exits by immediately re-entering the guest if it has a pending wake event. When virtual interrupt delivery is enabled, i.e. when KVM doesn't need to manually inject interrupts, this allows KVM to stay in the fastpath run loop when a vIRQ arrives between the guest doing CLI and STI;HLT. Without AMD's Idle HLT-intercept support, the CPU generates a HLT VM-Exit even though KVM will immediately resume the guest. Note, on bare metal, it's relatively uncommon for a modern guest kernel to actually trigger this scenario, as the window between the guest checking for a wake event and committing to HLT is quite small. But in a nested environment, the timings change significantly, e.g. rudimentary testing showed that ~50% of HLT exits where HLT-polling was successful would be serviced by this fastpath, i.e. ~50% of the time that a nested vCPU gets a wake event before KVM schedules out the vCPU, the wake event was pending even before the VM-Exit. Link: https://lore.kernel.org/all/20240528041926.3989-3-manali.shukla@amd.c= om Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 13 +++++++++++-- arch/x86/kvm/vmx/vmx.c | 2 ++ arch/x86/kvm/x86.c | 23 ++++++++++++++++++++++- arch/x86/kvm/x86.h | 1 + 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index c115d26844f7..64381ff63034 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4144,12 +4144,21 @@ static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu) =20 static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu) { + struct vcpu_svm *svm =3D to_svm(vcpu); + if (is_guest_mode(vcpu)) return EXIT_FASTPATH_NONE; =20 - if (to_svm(vcpu)->vmcb->control.exit_code =3D=3D SVM_EXIT_MSR && - to_svm(vcpu)->vmcb->control.exit_info_1) + switch (svm->vmcb->control.exit_code) { + case SVM_EXIT_MSR: + if (!svm->vmcb->control.exit_info_1) + break; return handle_fastpath_set_msr_irqoff(vcpu); + case SVM_EXIT_HLT: + return handle_fastpath_hlt(vcpu); + default: + break; + } =20 return EXIT_FASTPATH_NONE; } diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index f18c2d8c7476..f6382750fbf0 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7265,6 +7265,8 @@ static fastpath_t vmx_exit_handlers_fastpath(struct k= vm_vcpu *vcpu, return handle_fastpath_set_msr_irqoff(vcpu); case EXIT_REASON_PREEMPTION_TIMER: return handle_fastpath_preemption_timer(vcpu, force_immediate_exit); + case EXIT_REASON_HLT: + return handle_fastpath_hlt(vcpu); default: return EXIT_FASTPATH_NONE; } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 46686504cd47..eb5ea963698f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11373,7 +11373,10 @@ static int __kvm_emulate_halt(struct kvm_vcpu *vcp= u, int state, int reason) */ ++vcpu->stat.halt_exits; if (lapic_in_kernel(vcpu)) { - vcpu->arch.mp_state =3D state; + if (kvm_vcpu_has_events(vcpu)) + vcpu->arch.pv.pv_unhalted =3D false; + else + vcpu->arch.mp_state =3D state; return 1; } else { vcpu->run->exit_reason =3D reason; @@ -11398,6 +11401,24 @@ int kvm_emulate_halt(struct kvm_vcpu *vcpu) } EXPORT_SYMBOL_GPL(kvm_emulate_halt); =20 +fastpath_t handle_fastpath_hlt(struct kvm_vcpu *vcpu) +{ + int ret; + + kvm_vcpu_srcu_read_lock(vcpu); + ret =3D kvm_emulate_halt(vcpu); + kvm_vcpu_srcu_read_unlock(vcpu); + + if (!ret) + return EXIT_FASTPATH_EXIT_USERSPACE; + + if (kvm_vcpu_running(vcpu)) + return EXIT_FASTPATH_REENTER_GUEST; + + return EXIT_FASTPATH_EXIT_HANDLED; +} +EXPORT_SYMBOL_GPL(handle_fastpath_hlt); + int kvm_emulate_ap_reset_hold(struct kvm_vcpu *vcpu) { int ret =3D kvm_skip_emulated_instruction(vcpu); diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 50596f6f8320..5185ab76fdd2 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -334,6 +334,7 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vc= pu, int emulation_type, int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, int emulation_type, void *insn, int insn_len); fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu); +fastpath_t handle_fastpath_hlt(struct kvm_vcpu *vcpu); =20 extern struct kvm_caps kvm_caps; extern struct kvm_host_values kvm_host; --=20 2.46.0.rc2.264.g509ed76dc8-goog