From patchwork Fri Nov 1 18:35:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13859667 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 721E21CEE91 for ; Fri, 1 Nov 2024 18:36:00 +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=1730486161; cv=none; b=u7iYOzmsHwwwne+PLmxuP7yw8ATgDAfewL0ALphRq8/MFvEzG5bGDdyGrq4qDsk3lDPrGSjfkcOvlodKmIZbMrPyRivfmBRuVvg1559Pea1MYa583GQsM7KptzyhsseD5jldj6iIhOGQZTudGotJBgu3GVeqqjgRP6iVhU8WdZs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486161; c=relaxed/simple; bh=GthnIBzx9iijyVZUANBkchjX+hZXG4UWjfuYUeGfwps=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=isecvOaxv+4zb8lvOmyUjgnIADHE7AJ6gg4S+iKlDX/x7xzrfy3fnyd2pXHXaWeazAo5XqhKgBIV+Me1JURIKMh3jsNORWYROaZAnHheidSewusRuFICGoNa8W7eL0jlE4xPj4kSAEj74IhMKyl+pvy3v7pkAOZ9nLagrYfCzX0= 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=iqqvtpmi; 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="iqqvtpmi" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7c6b192a39bso2367431a12.2 for ; Fri, 01 Nov 2024 11:36:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730486160; x=1731090960; 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=qVWIo3tvorcfd9vZ45ZC5Hm+N0sYYOu19bKF2QN63SQ=; b=iqqvtpmithxzTSFB0KhCn/cf15yMVYssppD+h4ZQ0s83vUf+3CTvsE2ZYxk7N/uwCm vadb4yNcVXb1DYGlUyYybPicrVpoYk8Wz4CErKlr3gZ8aajpR0WpGtUXbhkCEZY4g1HF XSJKWOgsK1sWUxdki1ZiAqfPjsbgv9vraHt5HnPSOc3v2vyoOPnODm4Bm3LOj/E8vF4C x0lgZOWYWXWuEjcgP/2/xgLEwP2JFTb5ODi+aCqkF1ovwFVM3ASTsxbVMWXdexyWbqgk wpayYECA33Qh/OZhqAXX3x4ScMdScRr7SY1FTVGKybIh0ZZcr8qzG04ErtuNRneU+A0B n8DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730486160; x=1731090960; 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=qVWIo3tvorcfd9vZ45ZC5Hm+N0sYYOu19bKF2QN63SQ=; b=DtEKN+31W7vtuXJQptcSyHl4bHFQpReoBX2hLhEDiYlKCjAy/U4HzoemlGh2R26Aqk xVx1v2Zb58td4GFxJoLrT+MpAEncGCgXLC8gIKPisRWmOTauNU5nCitiRof/KrlqBzZM 5RdqHI3R7A7CVDwPJm00AOr5aQbnZErYvgZ6CCAAZHf2ErayCk6oh61R5d8logqDXRlX YH2nA94TOU3duHYrhDsJCmMMN97L0W2WUOr9fMs/RWqkhdYtUCRCKmSpknnmI2jXEUcA mpeDm8ZLg7I2stJCw9bweZ3ChptvCHJmNX6epdaI7W0NBYQxusEz2Bo6dR93dz9EKEQE tmcQ== X-Gm-Message-State: AOJu0Yzp9JUuVi8+Xsz0jIBleWqCeFN+GxSHS+HfSFRuYNSdQf41ybqy ias7X5dob1hh30PB7SDQ8Nsh3dB4kW8MtSi0r2R/eNdV5JjvVDK+khg8xmot0oTscIGk+sCxJzv aZA== X-Google-Smtp-Source: AGHT+IHktkXZ3RvXifEjmjETa4vTjpVmQp9Hs4/ZkuOBFXaF0+ekg7ATFuDxNEhzDH/TUjSiGZFcgQgAWKs= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a63:5556:0:b0:7a1:6633:6a07 with SMTP id 41be03b00d2f7-7ee4107c403mr8192a12.2.1730486159703; Fri, 01 Nov 2024 11:35:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 1 Nov 2024 11:35:47 -0700 In-Reply-To: <20241101183555.1794700-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241101183555.1794700-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241101183555.1794700-2-seanjc@google.com> Subject: [PATCH v2 1/9] KVM: x86: Short-circuit all kvm_lapic_set_base() if MSR value isn't changing From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang Do nothing in kvm_lapic_set_base() if the APIC base MSR value is the same as the current value. All flows except the handling of the base address explicitly take effect if and only if relevant bits are changing. For the base address, invoking kvm_lapic_set_base() before KVM initializes the base to APIC_DEFAULT_PHYS_BASE during vCPU RESET would be a KVM bug, i.e. KVM _must_ initialize apic->base_address before exposing the vCPU (to userspace or KVM at-large). Note, the inhibit is intended to be set if the base address is _changed_ from the default, i.e. is also covered by the RESET behavior. Reviewed-by: Kai Huang Reviewed-by: Paolo Bonzini Link: https://lore.kernel.org/r/20241009181742.1128779-2-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 65412640cfc7..8fe63f719254 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2582,6 +2582,9 @@ void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value) u64 old_value = vcpu->arch.apic_base; struct kvm_lapic *apic = vcpu->arch.apic; + if (old_value == value) + return; + vcpu->arch.apic_base = value; if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) From patchwork Fri Nov 1 18:35:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13859668 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 721DF1CF2BB for ; Fri, 1 Nov 2024 18:36:02 +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=1730486164; cv=none; b=aPje3ogpLxejr1ozWJxDDwafO/DXOimp+UpuF15l7dQ0uJXe/yYV2WEBGHF1lqq6/nzWvZ3NoZPhd/MyH65kvAz369aD3lbU6PZhmIJ9d2dUItooC/3zkF5kfvFK9B894FvtV9hdYGYBM9AaHCv3QwfueCPxJN7Q7GIiJtDLrnc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486164; c=relaxed/simple; bh=kVsh1e4mXUBXaMtm0DNpuubdczbQWRrvs26jscxulfg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=a3d9kOFBf7EZdTuKTrfnieMmM4Al9m+AvQ54dlj+Q82Ny/xQjqijhWcccTwywYLxNipb/KE+yXqAcYQ44NgfaT7c2OHsYfaH3nXsD5IPZaDklwhCCkq7DwJ8fX03+3uPqepYAka6iNmstgOpffsytMyWzDDIlXtAvGtNg4OIerM= 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=T5A/luYT; 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="T5A/luYT" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ea6aa3b68bso17965997b3.3 for ; Fri, 01 Nov 2024 11:36:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730486161; x=1731090961; 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=UwAATxlpk+S16792LfWJF7rOwrCD8DVw6q+MsAm54QM=; b=T5A/luYToY6RzQ9SW6D1lr5sw65OXrPWtsdBJ8HnNWZz2oTbWjf2OizRzAdNgYlOLm JSIBHRqVXFyUeY1tSoGS+PO+Bu4+X1PO3ahiNqmKCR5RqjcZ7sc84XEuEWueos2aNhPm 8UjWBG8oXtC6COStoiJutd0CX23yyOo4DJ87zAm/dJ5bowzwNi5Ty8Yn/hFtqklQInIy oTYeKrSzCMzQByWoFIH4CSRrX76oYRKrIziN4C5o+bdXkbWShXpCYyhyo49SN2ZJpbzu AhVhuvQp1+mqAYDEUFjNTCFifjXQ6/NZ/PktygCLbqEq8/KmEscluCr03U11smP20FVr bivQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730486161; x=1731090961; 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=UwAATxlpk+S16792LfWJF7rOwrCD8DVw6q+MsAm54QM=; b=p24Slm40olc3uBjlrwvy0Z2gqzaTxhO7FNUMjw8BzLFayhgxKVCKheY+XRRoZtirrH PH3aX58PJSzDtSMtacyI2Xc4pNmDc6DZryW0bLSTi21KRjbuPyGl7Xl7sxAmnq9qFpGf gHaCA+QOtZCcPkbg9Xq0JNmPMTmNiJUKER4kiakEjeBR+m81IKIoz1Seo0MxfqqdOHe7 Z22MMQm1OOM4d9Wkk6HxQbzspDQcqqC2L+1wRJTOzzzEY0P0XZpj7mw+5v+N6tBjbLIv XRbh7Wip0I+CmfTgBcVjd5z8f5+x3jG0fIN87WGccjHsTCc5GnaRJri+FwW/JVpe9xkI oZ/g== X-Gm-Message-State: AOJu0YwNaiXTPeBMqx70VHX0irDW3AGL4+epP7UmZiDZmdrlFMUoDUiy CfVrpfXELq5BoqS1HGWvVEWKufxH+jHeT5ub1E8M14Z30uobfYvoRzEJRNyFi5rS/1zyXaAldHT OYw== X-Google-Smtp-Source: AGHT+IGIOQ0uPuaobqh//fSiVB6HaPkTpJ/i3mQUQwamiGOepXC3UO6URBxNFd8cT/8+saz/WWzVM7BUBbA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a05:690c:4c09:b0:6db:b2ed:7625 with SMTP id 00721157ae682-6ea521c92b4mr298177b3.0.1730486161589; Fri, 01 Nov 2024 11:36:01 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 1 Nov 2024 11:35:48 -0700 In-Reply-To: <20241101183555.1794700-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241101183555.1794700-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241101183555.1794700-3-seanjc@google.com> Subject: [PATCH v2 2/9] KVM: x86: Drop superfluous kvm_lapic_set_base() call when setting APIC state From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang Now that kvm_lapic_set_base() does nothing if the "new" APIC base MSR is the same as the current value, drop the kvm_lapic_set_base() call in the KVM_SET_LAPIC flow that passes in the current value, as it too does nothing. Note, the purpose of invoking kvm_lapic_set_base() was purely to set apic->base_address (see commit 5dbc8f3fed0b ("KVM: use kvm_lapic_set_base() to change apic_base")). And there is no evidence that explicitly setting apic->base_address in KVM_SET_LAPIC ever had any functional impact; even in the original commit 96ad2cc61324 ("KVM: in-kernel LAPIC save and restore support"), all flows that set apic_base also set apic->base_address to the same address. E.g. svm_create_vcpu() did open code a write to apic_base, svm->vcpu.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; but it also called kvm_create_lapic() when irqchip_in_kernel() is true. Reviewed-by: Kai Huang Reviewed-by: Paolo Bonzini Link: https://lore.kernel.org/r/20241009181742.1128779-3-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 8fe63f719254..9f88a49654b0 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -3060,7 +3060,6 @@ int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s) kvm_x86_call(apicv_pre_state_restore)(vcpu); - kvm_lapic_set_base(vcpu, vcpu->arch.apic_base); /* set SPIV separately to get count of SW disabled APICs right */ apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV))); From patchwork Fri Nov 1 18:35:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13859669 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 3849B1CF7AD for ; Fri, 1 Nov 2024 18:36:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486165; cv=none; b=icnUjc6FacGDd9aAf+w0lO9d9Fw2drBeQbs9mNlH2TB4wLANO5GkjdrcLEKfS+mFdzRm+7UVmQU5ZqTPt7WUOza3LxQeLAXeIenWwbmITbuXmCpWR1gRwX1qRKFWQPaUCf8vFx2SgrGpX7+NzHZJGK/U5vZU+8iq4NBNVKexhcw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486165; c=relaxed/simple; bh=AQHWFIKx05Q6I0khQtg6qx7F98/ChoeEEBN+Jwsa0Kw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MG4sBx5KNlt2O0x1eFXwO33iUJ/0Xo3W0onjHbmoWIbV2DAoAZWJQcWZutDrkC3EllPYImpFaNTIY3r8BpHxHbGckleFsBE+81FRbhwI43EnaHmmxICZ8mO6jmlbgPB/+wh2IiQVJYAqJl3XzYyj2yKiX1dyi/8xyAv/6SQ6H9U= 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=0KSDPFR6; arc=none smtp.client-ip=209.85.210.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="0KSDPFR6" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71e6241c002so2264563b3a.1 for ; Fri, 01 Nov 2024 11:36:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730486163; x=1731090963; 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=RrKAd/LcvqmweQgUO1Es4Qu822f05reWQBoEwGCwlQ0=; b=0KSDPFR6um5WtgOJ7qSEae2UXGBxY/TDQB/5vHKRrMMH/xh3mStNnYsO+xf0S+hDpD dfvy+NaDT4vx3JqfSU8/HlyGy2OuZ8IzoKivhc1CIeR/u0hKgTlsMSR90Fz+cq2TmXsz gQ01OTBqwrbEdoG2z/CUz2Sp3CtFMn9T+Dg208L0clmPXyTaHeF0jh0si6gCEGcVG4mc 9C8wKRKYf6bn69IftvpM7LvZ1NFj5MpubuNDUfFmDjdaDx7wm3qpHMnG77hGbwjIIGjH TjqxLBfSlZpWrf6ZRofPlUbq3gKEVo5Rbi2Bl8PVPESU8wWqTCKvJB0SRXxx3esSx7ko dKMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730486163; x=1731090963; 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=RrKAd/LcvqmweQgUO1Es4Qu822f05reWQBoEwGCwlQ0=; b=BZlJA60M1TN/Ab8nKOlmBmwrcDf/JRSklNRBQ3QdLH8ac5cHyY38UXw6xUQa7PYL+8 HM39OeYrZLeRQaNtOq66EjR4dU5ETeDTjx+JAbhGI8FfjILOpXUEVYy0MDEepy5m5lYr vOf7thUqG58Xxzrgb89NxvRHMFE+3sISEQKuNR/ydXq/DbE3Qd7Wu8g2ulHvyI1nPkgl RMzkRHCB38Sni+jtndOvcqjrN6GNq1nm17pRKEcSKCaFjeZJbjMHlOX55cC3vb29zs3s B7eziKZG9zfSSUlfIu9siIepjG9QRKH9LN3WG2ScBSwGYhFF7Mc3Kh9EQlMdxc2Xb8eB cDAg== X-Gm-Message-State: AOJu0Yy3Cs/tZ9MXGeS8FsQi5DffeUGjX1D7oWi48Wm+sXCvz2yBJDez jNI9Gf5lSBn/sNB6vzp89KQkAZuVYJepgqqDSVnNvfqAVmdcIiA2rWC6icaKhoaFVE0G9BwF+AF crw== X-Google-Smtp-Source: AGHT+IEfb7kN1e0CSUsCamBepUoGEaAPZKajSLl220fb2ebC5QNlJYvpwQhj814gCEnvIJyCqu1f9UL5Ir8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a05:6a00:2790:b0:71e:6b3b:9f2d with SMTP id d2e1a72fcca58-720bc83e022mr26129b3a.1.1730486163504; Fri, 01 Nov 2024 11:36:03 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 1 Nov 2024 11:35:49 -0700 In-Reply-To: <20241101183555.1794700-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241101183555.1794700-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241101183555.1794700-4-seanjc@google.com> Subject: [PATCH v2 3/9] KVM: x86: Get vcpu->arch.apic_base directly and drop kvm_get_apic_base() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang Access KVM's emulated APIC base MSR value directly instead of bouncing through a helper, as there is no reason to add a layer of indirection, and there are other MSRs with a "set" but no "get", e.g. EFER. No functional change intended. Reviewed-by: Kai Huang Reviewed-by: Paolo Bonzini Link: https://lore.kernel.org/r/20241009181742.1128779-4-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.h | 1 - arch/x86/kvm/x86.c | 13 ++++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index 1b8ef9856422..441abc4f4afd 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -117,7 +117,6 @@ bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src, struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map); void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high); -u64 kvm_get_apic_base(struct kvm_vcpu *vcpu); int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info); int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e45040c2bf03..118e6eba35ba 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -668,14 +668,9 @@ static void drop_user_return_notifiers(void) kvm_on_user_return(&msrs->urn); } -u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) -{ - return vcpu->arch.apic_base; -} - enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu) { - return kvm_apic_mode(kvm_get_apic_base(vcpu)); + return kvm_apic_mode(vcpu->arch.apic_base); } EXPORT_SYMBOL_GPL(kvm_get_apic_mode); @@ -4315,7 +4310,7 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data = 1 << 24; break; case MSR_IA32_APICBASE: - msr_info->data = kvm_get_apic_base(vcpu); + msr_info->data = vcpu->arch.apic_base; break; case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff: return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); @@ -10173,7 +10168,7 @@ static void post_kvm_run_save(struct kvm_vcpu *vcpu) kvm_run->if_flag = kvm_x86_call(get_if_flag)(vcpu); kvm_run->cr8 = kvm_get_cr8(vcpu); - kvm_run->apic_base = kvm_get_apic_base(vcpu); + kvm_run->apic_base = vcpu->arch.apic_base; kvm_run->ready_for_interrupt_injection = pic_in_kernel(vcpu->kvm) || @@ -11725,7 +11720,7 @@ static void __get_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) sregs->cr4 = kvm_read_cr4(vcpu); sregs->cr8 = kvm_get_cr8(vcpu); sregs->efer = vcpu->arch.efer; - sregs->apic_base = kvm_get_apic_base(vcpu); + sregs->apic_base = vcpu->arch.apic_base; } static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) From patchwork Fri Nov 1 18:35:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13859670 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 2BF4F1CFED7 for ; Fri, 1 Nov 2024 18:36:06 +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=1730486167; cv=none; b=E3fPRkM2YdV8KLXBf66Jaq5UbSguIcTYD/qyVMirB3MMPitxHezTJPHZvyCFcX4IKYO36ZFy7oeviXA5kfODRsBZvptzlVJgyYjTRLVEP6YUrdU0Czy6z9392fVP4cUWIENjU03sYNUoCgMVEXd3H4YXY472gQm57JFpRg5Xjv8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486167; c=relaxed/simple; bh=ZvhPKlVHbgPPM+VoBwR5IIXoCb+DdIwR8vcjRgoEO1A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ejwJq8u33SgkWZe14ntaIpzaYLKTetRGVePjgC+Mt3gpj6nR5uerga+ihzsJ4oY0BG98WtN8tFmCQadxyIQGyQsCM13IKapy4US40lXKyH1080mAQb7h6KxFNivVXcTyu2Q9z6/CEIl21BSRrWhiUeTZAOtdB9SpeRHYXpFmcZE= 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=zCJeDBy8; 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="zCJeDBy8" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e30ba241e7eso4540518276.2 for ; Fri, 01 Nov 2024 11:36:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730486165; x=1731090965; 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=QM3s7U/z00nQJq1TtO3+CW97lZlfwpLmjD1tvhSkGao=; b=zCJeDBy8Y7Alyac/pvMt7u4ZZkwOrcgZi4vWHPGT1PJhrD2bzlQFa8jF07Wc2oMfKA 83hMl1ldCzHpUNF3xsyzzoqEZsxseG9UCy0wjP3LFGBCoGne73blcmwHfX7KLCs+SBiD dMER9od0vs1OQ5CBxKIhnIs9cbuFAUqngNA4friNrtfq7D32WSlxnDF/Ykn4qRSiuaq2 Mm5DEfp6jX8bNv2TnDpNR/eU+dEr88SZf6IXMGw3zBGUFC8diXKqqlJw7SA5tVLhnd27 CRDpElF+OmF+RaxW5NlwZyCS2vA5mT1fht+IqeGrej9WqYwyU195ZCppic4JUWcK568V gWVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730486165; x=1731090965; 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=QM3s7U/z00nQJq1TtO3+CW97lZlfwpLmjD1tvhSkGao=; b=QmsYogj8dslT6oAFDDdvyPJosubGEFWTuftPm15Fy/dWvyAhNekY+TlSh8Tr+4fZEQ riejpKwPjPfwqqKoYJ+9zequ4CIuDYD5DtIhl2J4SAAHjooLkl+i4P+1oSrpaARuSBRd KonOmqM7UaWgHTD9OITCzRcfG04PVq4XLZmMD8NgYiKCekyT8KzpbH0W5KcYGzHJR45M JzaHwvICEt5wbMPMoDnl8z0UIzZZFyM6+gMuwH8GRoy+K1RnshU8Pz9vFhAYgl3pVXsR 9uwDO+BZ6Fb+a3H3t7sZqGghJSuPVvnHudbOMn09wEcL8b3ucZVqadz1xg9lvfczPscr L1sQ== X-Gm-Message-State: AOJu0Yy2VtVEyWcEwkx4RcCGdOINfDjA0PR87JzwN59aPBok+Qs/Yo3K ADj5DlVTrhShtaixu4m9MwSGRMNnvolQKQPDTi8b0doU77LerWJX/cn8fJlbg9+CPLQjbfMAhDJ 6xA== X-Google-Smtp-Source: AGHT+IG3BgrIkNF6YeRzyo0VcV/pAm0pQUkv9rII4p7eMueQ7pEvcf2Etu5yQzbU5p84yZjVWLlKD1CGFP0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a25:83c3:0:b0:e30:c79e:16bc with SMTP id 3f1490d57ef6-e30c79e1861mr13666276.8.1730486165271; Fri, 01 Nov 2024 11:36:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 1 Nov 2024 11:35:50 -0700 In-Reply-To: <20241101183555.1794700-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241101183555.1794700-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241101183555.1794700-5-seanjc@google.com> Subject: [PATCH v2 4/9] KVM: x86: Inline kvm_get_apic_mode() in lapic.h From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang Inline kvm_get_apic_mode() in lapic.h to avoid a CALL+RET as well as an export. The underlying kvm_apic_mode() helper is public information, i.e. there is no state/information that needs to be hidden from vendor modules. No functional change intended. Reviewed-by: Kai Huang Reviewed-by: Paolo Bonzini Link: https://lore.kernel.org/r/20241009181742.1128779-5-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.h | 6 +++++- arch/x86/kvm/x86.c | 6 ------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index 441abc4f4afd..fc4bd36d44cf 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -120,7 +120,6 @@ void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high); int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info); int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); -enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu); int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu); u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu); @@ -270,6 +269,11 @@ static inline enum lapic_mode kvm_apic_mode(u64 apic_base) return apic_base & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE); } +static inline enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu) +{ + return kvm_apic_mode(vcpu->arch.apic_base); +} + static inline u8 kvm_xapic_id(struct kvm_lapic *apic) { return kvm_lapic_get_reg(apic, APIC_ID) >> 24; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 118e6eba35ba..95af45542355 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -668,12 +668,6 @@ static void drop_user_return_notifiers(void) kvm_on_user_return(&msrs->urn); } -enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu) -{ - return kvm_apic_mode(vcpu->arch.apic_base); -} -EXPORT_SYMBOL_GPL(kvm_get_apic_mode); - int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) { enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); From patchwork Fri Nov 1 18:35:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13859671 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 A687E1D0405 for ; Fri, 1 Nov 2024 18:36:07 +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=1730486169; cv=none; b=TTbOegP/AxB6jF3oS8eQajkckwZHpzGjVuNgnj8822Fn9Ks8iz0kguS6b4d0isbOMFQGqYjl+kg5KVWoW+faF3m7oNS9kX6jlR5Zn8L1Uv+jA+zBjA3bxJdmrki3SgivjujvU5h89iwLX+o5jE55Fc02B6u9K9lDrS5dm5AWfTI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486169; c=relaxed/simple; bh=Zq395NiiKporolLjoST+6+2+QTwzJOuMC+X29AbEL8U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Voc+/OAxC0MKYwoUIKvrm7sKZYR0afg34ZSN8HIsy+9zd+Bpl7Xu6WaxsKn5iXwCHG6UnfmZqzT6atvbrg32+OINDQu1fIY09ZzruK8wawVeGnZof/FXPAhnSt4VnURJ/RxnoESeTKm7RVixghU2ttddDlSYto0Iu5akUZ5FcaM= 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=h+0/seQw; 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="h+0/seQw" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7ee07d0f395so2453303a12.3 for ; Fri, 01 Nov 2024 11:36:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730486167; x=1731090967; 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=6jSxIG8GHxBvV25BqBO2yI1fCAIS8ozpFMSRDXOmwqc=; b=h+0/seQwYnhYGdnQNS7FT1h53uIlhgyljfiZluwq9hSH4/GWPJCTQo6LlM2YS5gnUJ BO1aphj1nfZjNRaF74cS3iaYRN7rmbbol1M2yzzMjKePaW7LWv5dzx+mgOuueMLQbTYY bxu3e6zNhymaSULDHYzAtV2Xy4MfR6gOzT6itK+xtuQLPFYe98BpzyqlbKKJRHICCLLD ZE2bzX6KTTXI9uQJF/0AvwnPVveFoX++ybYWZvdbZc59DA7H4GVb232E623w5+LAnFJx uPcMC0LWpv/pR/2mYr40YOPFcbRrS3+qAFI6OJ4YegyMUEkTbwvGjTBK5NvJFXlS0MWa ye2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730486167; x=1731090967; 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=6jSxIG8GHxBvV25BqBO2yI1fCAIS8ozpFMSRDXOmwqc=; b=CSISY4w/NQYu3iUZxklKJGhowMqsuCK1yUNsLarOiEu4t8nPEq1OIBYBnWexhCWiJ5 6E5b8X7jpFt7+52yEGqOQbE7XcWUMdLDwFqbo1YF0ovMSVZZYflU5eTVXACyuCpJuYB/ YzH/HaZIE1yjA+7z0Z+CZPVLH/JqJQofKFrpcnLlqc8S+TYLoLUk96R9xsjnSRMWlt9X CMPCabA1GX+Ujk/ln66FvflWwrKEx9u+qw05//u+S6D4UqL6UtQ5yGMDyZVQtQFJg4jA UdFjHtdTnBRk/CeqwoO5UhMwU3KstVSpvnL1EY2XBTjir+fx9fswJRyDzrAiqd+mmce4 m/Kg== X-Gm-Message-State: AOJu0YwaXEljnxsrWHkTH7t0Vd47GAA85razpieEjZeuQGAKmI46Tsas u2JSeOLYlsIkpfg8jw9EpBMZwN6gjk2nlBE1yELSNsbBB4YVq89x3GpfoAEYyZGa9ye3jX/zkzE enQ== X-Google-Smtp-Source: AGHT+IFrzmaPgq4f61T2d+s5PZaLEK0ub9APvURzd9YoOZMotBWAM23YHpiHaxsWiIK/KyaT/0sYL80OMUA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a17:902:864b:b0:20c:90b9:3cf1 with SMTP id d9443c01a7336-21103cf3ef7mr80685ad.7.1730486167022; Fri, 01 Nov 2024 11:36:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 1 Nov 2024 11:35:51 -0700 In-Reply-To: <20241101183555.1794700-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241101183555.1794700-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241101183555.1794700-6-seanjc@google.com> Subject: [PATCH v2 5/9] KVM: x86: Move kvm_set_apic_base() implementation to lapic.c (from x86.c) From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang Move kvm_set_apic_base() to lapic.c so that the bulk of KVM's local APIC code resides in lapic.c, regardless of whether or not KVM is emulating the local APIC in-kernel. This will also allow making various helpers visible only to lapic.c. No functional change intended. Reviewed-by: Kai Huang Reviewed-by: Paolo Bonzini Link: https://lore.kernel.org/r/20241009181742.1128779-6-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 21 +++++++++++++++++++++ arch/x86/kvm/x86.c | 21 --------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 9f88a49654b0..b4cc5b0e8796 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2628,6 +2628,27 @@ void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value) } } +int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) +{ + enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); + enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); + u64 reserved_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu) | 0x2ff | + (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE); + + if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) + return 1; + if (!msr_info->host_initiated) { + if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC) + return 1; + if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC) + return 1; + } + + kvm_lapic_set_base(vcpu, msr_info->data); + kvm_recalculate_apic_map(vcpu->kvm); + return 0; +} + void kvm_apic_update_apicv(struct kvm_vcpu *vcpu) { struct kvm_lapic *apic = vcpu->arch.apic; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 95af45542355..57dca2bdd40d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -668,27 +668,6 @@ static void drop_user_return_notifiers(void) kvm_on_user_return(&msrs->urn); } -int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) -{ - enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); - enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); - u64 reserved_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu) | 0x2ff | - (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE); - - if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) - return 1; - if (!msr_info->host_initiated) { - if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC) - return 1; - if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC) - return 1; - } - - kvm_lapic_set_base(vcpu, msr_info->data); - kvm_recalculate_apic_map(vcpu->kvm); - return 0; -} - /* * Handle a fault on a hardware virtualization (VMX or SVM) instruction. * From patchwork Fri Nov 1 18:35:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13859672 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 411601D0DE9 for ; Fri, 1 Nov 2024 18:36:12 +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=1730486173; cv=none; b=pvRPMxm3qI23LF2g/QnNaUgiHTIJmZ3Mv6EnJNlZpKMvjpcH0GMhtcqBC/+Lr/tOCQr1V22+FwF6QO63T8P7t2NBFLPZYkqD0AEZyPuDgS44EO1gEE+P0wFPcDrRu637PTspNSOnRu3ZuzJlUc6w+Zh3ozp+jvWTa1zNoqNuz74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486173; c=relaxed/simple; bh=/frGkRXoQlzrjkWvNh5KGw/1JeGin0TpI2B3D2X+sy8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nbv84/rjJ70tKWPkRk7h8iZdr5cwL2NCBVw+1L7UpvbBL1S+LWm5+pgad+kzqG/RX+9hwqr4BuMYtjpG74wIEFp7MzHuvuzekpnKjit/ZycKRcPKB7/uTlAGPomL3btWCuwviXss6bvZyzqSPabjFEK3qGSylLqnX9O4+9ykY/w= 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=dgfN+Q51; 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="dgfN+Q51" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e38fabff35so43718097b3.0 for ; Fri, 01 Nov 2024 11:36:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730486171; x=1731090971; 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=o15oczZjLr65ny5I+TbaXC6dNA2ZMYul3o5wZUJGViI=; b=dgfN+Q51vS8AgcAnzRzp26kucDbAP5S5O0P9bbVJvLvU6Lzx2BTHWEurFjU9ywaM3S +BLOtYGSy0JGyKHt1oMNI9q3SH7vo2tXd3YcNtMqPdXPTC07usx1bKD6XL8kRz5ZaXXC PO1f0Ik+897lSmQR2Ki0bT6kVT2Ew0wqO1XWK2QgH/6Gjj1BSTQ77Z4JhZ2AJ99iuUM0 4xIkXzwYH4AbDR46jXjqnmNTpZBNh7emm/zd8nvIfZTgYEMkJJ6endCQuw7EgWKym6U2 3FAgAGbzGBuk03f3b+GDIQaRIQXwqD5KLuz/1qtTJDJTEeTbVr1LPMlun8jRNNiVjo1l 8wnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730486171; x=1731090971; 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=o15oczZjLr65ny5I+TbaXC6dNA2ZMYul3o5wZUJGViI=; b=Y4nAZq0OG5QaN+gOF9DLUzEnjnSSN+ASoQrjrjapRngrHX8dk1VixjGjOzkGnzpqfr WS4iS4EOe48PE0WmrhnUfg2fACWhhYnYrIQFUfhEkebK4X39bcAsF9mfvl30ZaYm5tt/ mPJkKY+Eh26qRWVPZtFFJb3TWs1QjXMvvJYQvrRaj8bfd7D5An5iQB7NZWbDAL/R+r22 JW9RLzk5cAQFJRGsV/0s3N4IUhb+b+gyNDx6TYt3U5wwSSQ2afKLk5JMu9dN/Oa35oPi F3d+NE5mH0bVKm0A/CHfJ2Z1ohhTxpJvq2gNREb5IsjstJ9MD63sAyMhIg0TYO8hXF4j 5JNQ== X-Gm-Message-State: AOJu0Yw3UDOrkmKl9eXdBnbw3pajii99jTk/ich05cRnGISzEAU1BtkC xamPc8VbKmttLQEXeCEVglLlVFD/6cX5vjexyqbEj4Av1NkTRt3XDHPlrxpqa40C2+c76JD4RHi aXA== X-Google-Smtp-Source: AGHT+IFAnYY7eTRPwUDiTbrEgfBgDblXGdjqSKzoGufvhnLN4rVG8UxtuXTk54TsLBw0n2mqTw17pJu+pNo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a05:690c:4510:b0:6e2:371f:4afe with SMTP id 00721157ae682-6e9d8b21492mr11215937b3.4.1730486171289; Fri, 01 Nov 2024 11:36:11 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 1 Nov 2024 11:35:52 -0700 In-Reply-To: <20241101183555.1794700-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241101183555.1794700-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241101183555.1794700-7-seanjc@google.com> Subject: [PATCH v2 6/9] KVM: x86: Rename APIC base setters to better capture their relationship From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang Rename kvm_set_apic_base() and kvm_lapic_set_base() to kvm_apic_set_base() and __kvm_apic_set_base() respectively to capture that the underscores version is a "special" variant (it exists purely to avoid recalculating the optimized map multiple times when stuffing the RESET value). Opportunistically add a comment explaining why kvm_lapic_reset() uses the inner helper. Note, KVM deliberately invokes kvm_arch_vcpu_create() while kvm->lock is NOT held so that vCPU setup isn't serialized if userspace is creating multiple/all vCPUs in parallel. I.e. triggering an extra recalculation is not limited to theoretical/rare edge cases, and so is worth avoiding. No functional change intended. Reviewed-by: Kai Huang Reviewed-by: Paolo Bonzini Link: https://lore.kernel.org/r/20241009181742.1128779-7-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 15 +++++++++++---- arch/x86/kvm/lapic.h | 3 +-- arch/x86/kvm/x86.c | 4 ++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index b4cc5b0e8796..0472a94e7b3b 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2577,7 +2577,7 @@ u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu) return (tpr & 0xf0) >> 4; } -void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value) +static void __kvm_apic_set_base(struct kvm_vcpu *vcpu, u64 value) { u64 old_value = vcpu->arch.apic_base; struct kvm_lapic *apic = vcpu->arch.apic; @@ -2628,7 +2628,7 @@ void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value) } } -int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) +int kvm_apic_set_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) { enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); @@ -2644,7 +2644,7 @@ int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) return 1; } - kvm_lapic_set_base(vcpu, msr_info->data); + __kvm_apic_set_base(vcpu, msr_info->data); kvm_recalculate_apic_map(vcpu->kvm); return 0; } @@ -2740,7 +2740,14 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event) msr_val = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE; if (kvm_vcpu_is_reset_bsp(vcpu)) msr_val |= MSR_IA32_APICBASE_BSP; - kvm_lapic_set_base(vcpu, msr_val); + + /* + * Use the inner helper to avoid an extra recalcuation of the + * optimized APIC map if some other task has dirtied the map. + * The recalculation needed for this vCPU will be done after + * all APIC state has been initialized (see below). + */ + __kvm_apic_set_base(vcpu, msr_val); } if (!apic) diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index fc4bd36d44cf..0dd5055852ad 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -95,7 +95,6 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event); u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu); void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8); void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu); -void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value); void kvm_recalculate_apic_map(struct kvm *kvm); void kvm_apic_set_version(struct kvm_vcpu *vcpu); void kvm_apic_after_set_mcg_cap(struct kvm_vcpu *vcpu); @@ -117,7 +116,7 @@ bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src, struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map); void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high); -int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info); +int kvm_apic_set_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info); int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 57dca2bdd40d..e01188dc82d1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3863,7 +3863,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_MTRRdefType: return kvm_mtrr_set_msr(vcpu, msr, data); case MSR_IA32_APICBASE: - return kvm_set_apic_base(vcpu, msr_info); + return kvm_apic_set_base(vcpu, msr_info); case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff: return kvm_x2apic_msr_write(vcpu, msr, data); case MSR_IA32_TSC_DEADLINE: @@ -11879,7 +11879,7 @@ static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs, apic_base_msr.data = sregs->apic_base; apic_base_msr.host_initiated = true; - if (kvm_set_apic_base(vcpu, &apic_base_msr)) + if (kvm_apic_set_base(vcpu, &apic_base_msr)) return -EINVAL; if (vcpu->arch.guest_state_protected) From patchwork Fri Nov 1 18:35:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13859673 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 169431D0BBF for ; Fri, 1 Nov 2024 18:36:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486175; cv=none; b=O86NJXgh70ii+n5uOP7ZHZml48vk3pT/vuyFI5IHghgEd93WWfCRQlm9O+rXBFWZqNnnMDgzTV9sLMldwmjno4AxsxbpOs+46FyYl1KUfSzzXo8aC4i60bMnn64F0YRieFX2SFyvX4hFJ1ooDb8zwopRn3CZAjWGQm7PbXB09LE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486175; c=relaxed/simple; bh=zFx2TIjK1OdJNejtX+ft1iBzbu3KT2YeNAnYrxfGZuM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OE6RZqEhQA5d4vrYJ0w+k5RZF/ZR6H2CMv70+AT4cD7HYsqWlFIp1hsWzIyT5ISsEw1QstyAMAJWYJZIH4WhlsOhR8TCwJBqyIIzEA/yo4xH7ftCNtkTLAm679wGNC8Ze4o/uvwsb1cXA8QQ56dXYN+f+HtFnEWFckgRDnwWOTE= 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=SmPeNUEG; arc=none smtp.client-ip=209.85.219.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="SmPeNUEG" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e2b9f2c6559so3698168276.2 for ; Fri, 01 Nov 2024 11:36:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730486173; x=1731090973; 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=SuEo4GUloct+B3/M5HAiS1mpZ2QSXRnWVclEOfjiYAM=; b=SmPeNUEGRTa0t+Z9O7shSsbiu4qZme35Tu4LOjjQlIMEnw0SlqQgMhQCFFRAOUDnwy yZKBvrtZTMc8PG0DzD8rB28oK93eF8GjfDEZhoqqvc09o2LOT0gIGdkOaDNntholTXTZ 2ctFvQTMK5uRqkwe4TugrZsPy4mhGALHRl3B7V6vyO/OCTii+5kkMaB9riy0ySREAOOB pqZeXfTXNdEHuM8q0ru49RzXUUInwBT8pEMQRyDkncFfZ9Bt2YnEpmsNVg+pAZpcjJpy ktU3Rs5LUtAY5Dq1j/k79OxbaArXdSvr5CMdheGEPDC9UDI/6CGU9bnBP+NIsR18U2OT 3Fag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730486173; x=1731090973; 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=SuEo4GUloct+B3/M5HAiS1mpZ2QSXRnWVclEOfjiYAM=; b=mD/45YSd3SiAgs4yF/yHgFdSljYEQmlvBjBJfhzcZze6pF46I8TW4HQSvQrS14IsqU fBz4XNd5XPNymPizsebuR0pO4/CS9KJeK4l2KWawd4dEvHtiJDrxqKa2CUokzs58e1x4 oaI/mh5W/Ou8YIv2KuAHUMPGVhVUnIfjUJT9U5O3pMlPRxtFe5OuiNwiQzrIF8xTbr7T bP1lqAirvbr6xFn/pMemgLq+YnNoXOMUc2apyNnPs9xfe6u0AuSqWvAZAldu5gAGAnCy tQsosRLD7vs4fe8ndhyHoKt73pPGKAYP0WcNxGlJHlGbe2X557/TF2MoWq2pRD04cjDr 7KgQ== X-Gm-Message-State: AOJu0Yz4VnwlMsrPoiVdJcTAddjMneESaUcnyo+XmedSkJsbkpJMaA1q vsTw5qJyyJ1zYT3alXokLkscKVrWT1rnWIy8tYhaDXmxXcaWOdOg0JaFuPv01o2Jqt7Wr3BZyBP +Pg== X-Google-Smtp-Source: AGHT+IE22lmjUlicPPO8OKaqhuLO/T4xSBECZnj8FSh00/I8mPt7rNgmEDffLoleV3L6b6O0qgyc6nCwMfY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a25:8541:0:b0:e28:e97f:5394 with SMTP id 3f1490d57ef6-e30e5a3e278mr4578276.4.1730486173118; Fri, 01 Nov 2024 11:36:13 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 1 Nov 2024 11:35:53 -0700 In-Reply-To: <20241101183555.1794700-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241101183555.1794700-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241101183555.1794700-8-seanjc@google.com> Subject: [PATCH v2 7/9] KVM: x86: Make kvm_recalculate_apic_map() local to lapic.c From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang Make kvm_recalculate_apic_map() local to lapic.c now that all external callers are gone. No functional change intended. Reviewed-by: Kai Huang Reviewed-by: Paolo Bonzini Link: https://lore.kernel.org/r/20241009181742.1128779-8-seanjc@google.com Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 2 +- arch/x86/kvm/lapic.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 0472a94e7b3b..7ddbebf78761 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -382,7 +382,7 @@ enum { DIRTY }; -void kvm_recalculate_apic_map(struct kvm *kvm) +static void kvm_recalculate_apic_map(struct kvm *kvm) { struct kvm_apic_map *new, *old = NULL; struct kvm_vcpu *vcpu; diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index 0dd5055852ad..fdd6cf29a0be 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -95,7 +95,6 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event); u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu); void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8); void kvm_lapic_set_eoi(struct kvm_vcpu *vcpu); -void kvm_recalculate_apic_map(struct kvm *kvm); void kvm_apic_set_version(struct kvm_vcpu *vcpu); void kvm_apic_after_set_mcg_cap(struct kvm_vcpu *vcpu); bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source, From patchwork Fri Nov 1 18:35:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13859674 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.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 337831D12FB for ; Fri, 1 Nov 2024 18:36:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486177; cv=none; b=ZNAzgJaZXC/68Q9rA3RIRIQyaH9v67dBgw+UcEPzKlJu4DyznUJ4FdMejbf4DswMW/R1/ZXdrw7IldltN4pMDTbCm8xz9DaxrxI9wh82oYgZFxjeQIrO6knLpzuQW8NbYxwJaQNsew0LZqlK8RdbiVoeqnBx2hoXKrC+B68dQ4Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486177; c=relaxed/simple; bh=xTRxQxZLwJ4nMy+y+tKIFp/3uKotLx1vv5T1qyPMJ2g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NkmKTA4jfPWZ05ju7Uj87MDCi/0HzD+R2EFxvol6ZnYXRlT3fUvemMmg/5b7SmqiWbdMAs8B2ykn2VmCtTvxUNqXdHb4Glys856inBCjX0mENKDMEf69jrxHdXV/dclk8AI4/GyS6s6u6XLOyeJQKgOg4xOQp6EJTZhcDTbaTgw= 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=HgoMX9CP; arc=none smtp.client-ip=209.85.128.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="HgoMX9CP" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e0082c1dd0so53005267b3.3 for ; Fri, 01 Nov 2024 11:36:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730486175; x=1731090975; 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=rdMTMV14I8S9mlDGalYCD9r7IonKg5RbW8JA0XpZZZo=; b=HgoMX9CPtqRFtczCPAbPWWAWyumSuMkQKj0eibfdGCmVjKGvruo5I615+91UkIvKEx cnhN8yLJh2RufT6MgTOk8hw84Uyv8Jhb4tiR0H3VuXV1i5Al/MaQHCxVV56ZtDkfDGjM JmZFN7btEbB8CTPHvm1/ZVwCeSHCEJSt+mUxx47bXa9blkfJ0U6mdYzutsL2Qw0/GG8O SwGrwAMMq6IVpkLdfEwmtMsMqSrNbxENlfTIrI1z0+RV+Nm/5NUChPYY09gZ5sSkvPDl D61Nx4ybXGAguX0cg8AfUtLKI3EMaGd+vMTV8DZkKs1ZgxRS585/DRoN3I06/m715Q5e cvHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730486175; x=1731090975; 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=rdMTMV14I8S9mlDGalYCD9r7IonKg5RbW8JA0XpZZZo=; b=jEDIvOMwYS3wFYMsOKrE+mnIOU/xLIN+6VhOe+j1zzwOQfOoZVtnwLlVvF1xMK4u74 y3HU7WCfNxEEBICgUcAa1tFZ+yifH4I2KLJcBv8bQpUcQZHA03fMv0tGB11WWA//n+ii UVFjsMTiZGWZLdu65BWJA55EDqwnXyOtvRpf29gK2Ukv5+yqYmsQaPae83VwmRDbN9c5 UnIWhnGM+bGo/0VXwlnDCqydeLs2s5M7KRfskL1CBiKbnfY4sRszO5ExtoSe8hd6KpAV ogL7+Qhr7r+5nAzCoxcT+WLGc0HIghWfXgJpfjY39kjsg5GqHzauOk7otEWxmdLoquKT oTww== X-Gm-Message-State: AOJu0YyF0gl7chM4n7LA2+1kgofC4R39mbi5qCtAh2p4vHmA8t0NujdG hzu1tPlkVOnVSyqLPlujEKJQmdldfVG1aUowzJB1sIMeXL23Up/TnjZCd0tPK0A0egVtVRBQgwB +0Q== X-Google-Smtp-Source: AGHT+IHPt+QUH9dPUzxntcxqiIHKttKNl1vJk3v2qJSERf0BWXO6FKSBZF5MJjSzw0vlhoa9bYeqWzIk7uo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a25:aa66:0:b0:e29:6e61:3daf with SMTP id 3f1490d57ef6-e30cf3e6ac7mr7184276.2.1730486174949; Fri, 01 Nov 2024 11:36:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 1 Nov 2024 11:35:54 -0700 In-Reply-To: <20241101183555.1794700-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241101183555.1794700-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241101183555.1794700-9-seanjc@google.com> Subject: [PATCH v2 8/9] KVM: x86: Unpack msr_data structure prior to calling kvm_apic_set_base() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang Pass in the new value and "host initiated" as separate parameters to kvm_apic_set_base(), as forcing the KVM_SET_SREGS path to declare and fill an msr_data structure is awkward and kludgy, e.g. __set_sregs_common() doesn't even bother to set the proper MSR index. No functional change intended. Suggested-by: Kai Huang Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 10 +++++----- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/x86.c | 7 ++----- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 7ddbebf78761..7b2342e40e4e 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2628,23 +2628,23 @@ static void __kvm_apic_set_base(struct kvm_vcpu *vcpu, u64 value) } } -int kvm_apic_set_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) +int kvm_apic_set_base(struct kvm_vcpu *vcpu, u64 value, bool host_initiated) { enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); - enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); + enum lapic_mode new_mode = kvm_apic_mode(value); u64 reserved_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu) | 0x2ff | (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE); - if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) + if ((value & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) return 1; - if (!msr_info->host_initiated) { + if (!host_initiated) { if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC) return 1; if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC) return 1; } - __kvm_apic_set_base(vcpu, msr_info->data); + __kvm_apic_set_base(vcpu, value); kvm_recalculate_apic_map(vcpu->kvm); return 0; } diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index fdd6cf29a0be..24add38beaf0 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -115,7 +115,7 @@ bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src, struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map); void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high); -int kvm_apic_set_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info); +int kvm_apic_set_base(struct kvm_vcpu *vcpu, u64 value, bool host_initiated); int kvm_apic_get_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s); int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e01188dc82d1..8637bc001096 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3863,7 +3863,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_MTRRdefType: return kvm_mtrr_set_msr(vcpu, msr, data); case MSR_IA32_APICBASE: - return kvm_apic_set_base(vcpu, msr_info); + return kvm_apic_set_base(vcpu, data, msr_info->host_initiated); case APIC_BASE_MSR ... APIC_BASE_MSR + 0xff: return kvm_x2apic_msr_write(vcpu, msr, data); case MSR_IA32_TSC_DEADLINE: @@ -11870,16 +11870,13 @@ static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs, int *mmu_reset_needed, bool update_pdptrs) { - struct msr_data apic_base_msr; int idx; struct desc_ptr dt; if (!kvm_is_valid_sregs(vcpu, sregs)) return -EINVAL; - apic_base_msr.data = sregs->apic_base; - apic_base_msr.host_initiated = true; - if (kvm_apic_set_base(vcpu, &apic_base_msr)) + if (kvm_apic_set_base(vcpu, sregs->apic_base, true)) return -EINVAL; if (vcpu->arch.guest_state_protected) From patchwork Fri Nov 1 18:35:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13859675 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 CC2BB1D2F5D for ; Fri, 1 Nov 2024 18:36:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486180; cv=none; b=GcmT+Z0CoyT1yE0BhiG0ECscDemx7O/jHiPoUS/w8JA/JjM6L2eCl68wbSsXfrOWaIf622m2siEw1Bv6HJh7K775+w//nn06Ez6hjv8FvPRLwbVxVYlJ4DZpbPzzxyd3y8rPUje+tgSLjds8Uo0iClfKyAZ6WJSF3oNNNPIX7KA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730486180; c=relaxed/simple; bh=wETa+dLcXchkDF6niCLZKhIJhZbg0XK3Wfx1WgkxMCc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TiSgTxCnOJGmvOxQBUUTpaOVX4erJs310BQOFnv7DC3GLpiZODtPSMwOzp96yrF5wryKCpmhpQGuhNniOrYN0TYj8lm0A0TChBjdKOmKKZMTr9hO60PzKsmXvNCLj+rIrX9jQRoLV2ncgSPlChS8T0KoTgYJgZZwPzYTJzI1uRk= 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=MDHk3TRy; arc=none smtp.client-ip=209.85.215.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="MDHk3TRy" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7cd9ac1fa89so2555843a12.1 for ; Fri, 01 Nov 2024 11:36:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1730486178; x=1731090978; 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=t6n3TCROGALKBhF87tS4HVc6hSldgq3keX1Nmca5GdQ=; b=MDHk3TRyDF0J5zovlyekZcJk15Cg90yrmpNGQnIykNg4UT/vWmf2MFi8otvSWkgtDD 0ShRvpELKEW7cj+rT0B8yo9L8pAs0R1+iJAcuSNODEj1SIzSZ6rg08GMp2aB4t7EnPDU HNw0NBkH8reDlTtAFQ0m5k0dHbMJsv8GVy7gMtPdu0UozJf+fcKg6R28H/J5nOSCAxEn mOdWVRcMdcmtHt0xahGe6B+0M5cDxlq76L8ExAw6KpA8mgp0yIon3EZ4L/tf15iR9CKV NXDsVJt3ICxYv92h9zIhc0eSM+/3OeTy07vRX/YMauqzqMbPIEGHVbn+KofzrIoX7wit Hqrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730486178; x=1731090978; 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=t6n3TCROGALKBhF87tS4HVc6hSldgq3keX1Nmca5GdQ=; b=dvexJYbrCKeeTVglOdrHAFh/wi/l/4h9+gVhBCnTNdGqU140VhZelnuypls8bO0vH3 GokdvFxUcfjCCuXVmvr8KfiRVUe4pelxpiGdismpYKHFuOFbtVexCzGiRsGe7drYDLrn 2eRyMYOlpjalZ2XO4AxW46V7HDBDNmhJGaw7MC1jxDjYaiZhIpXpqj63N/ahFsBbvHMy 8vP9oRUpHfNI1ynTiBzRviuVBUh9sP/gM/ymsoDgdY+AX7zOrfuFu9y3/rBc2pgqCPVu trxem18zMxQ/e6jdzXzF1AWx/42acCpNkWsOK9bvTbAb1+J7GYozgkRdzJhK2T1SrmwA lgJw== X-Gm-Message-State: AOJu0YxFH9lQ1gHQpRcViR3xIBue3ZBizQFpWOdwCjD9k4R+CSQ42fID IM1glJSA/TJUCTqLBJPdvEayjd88X1KM+4hcGG0H7n7xIQykne2jTHfTHMbEASo8vxN8/Rz3Yy4 ujg== X-Google-Smtp-Source: AGHT+IH9li/61FEUW1xJGw5b33eDgRmdCZyYAPvyxxo44gUVaRYqdHjdk4v5TgKyy+vIqOwzof42J6bDicI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a17:902:e54b:b0:20b:7be8:8ecc with SMTP id d9443c01a7336-210c68803d5mr2548335ad.1.1730486178136; Fri, 01 Nov 2024 11:36:18 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 1 Nov 2024 11:35:55 -0700 In-Reply-To: <20241101183555.1794700-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241101183555.1794700-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.163.g1226f6d8fa-goog Message-ID: <20241101183555.1794700-10-seanjc@google.com> Subject: [PATCH v2 9/9] KVM: x86: Short-circuit all of kvm_apic_set_base() if MSR value is unchanged From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang Do nothing in from kvm_apic_set_base() if the incoming MSR value is the same as the current value, as validating the mode transitions is obviously unnecessary, and rejecting the write is pointless if the vCPU already has an invalid value, e.g. if userspace is doing weird things and modified guest CPUID after setting MSR_IA32_APICBASE. Bailing early avoids kvm_recalculate_apic_map()'s slow path in the rare scenario where the map is DIRTY due to some other vCPU dirtying the map, in which case it's the other vCPU/task's responsibility to recalculate the map. Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 7b2342e40e4e..59a64b703aad 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2582,9 +2582,6 @@ static void __kvm_apic_set_base(struct kvm_vcpu *vcpu, u64 value) u64 old_value = vcpu->arch.apic_base; struct kvm_lapic *apic = vcpu->arch.apic; - if (old_value == value) - return; - vcpu->arch.apic_base = value; if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) @@ -2632,6 +2629,10 @@ int kvm_apic_set_base(struct kvm_vcpu *vcpu, u64 value, bool host_initiated) { enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); enum lapic_mode new_mode = kvm_apic_mode(value); + + if (vcpu->arch.apic_base == value) + return 0; + u64 reserved_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu) | 0x2ff | (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);