From nobody Wed Dec 17 23:51:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A6BAEC04FDF for ; Tue, 15 Aug 2023 21:36:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240303AbjHOVgT (ORCPT ); Tue, 15 Aug 2023 17:36:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240294AbjHOVfw (ORCPT ); Tue, 15 Aug 2023 17:35:52 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 480051FC4 for ; Tue, 15 Aug 2023 14:35:41 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-589f437fef0so32460747b3.3 for ; Tue, 15 Aug 2023 14:35:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692135340; x=1692740140; 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=bCGvdYVO9EKbpa2jmZF0w8IsHUC4STqQLwNPEWIlFrg=; b=4seg8jbUuzcf+Jrd/1/VCZUZyBFVOg1OI+RgVOOd17/vtRlmnaSGCfGp+yaEQXkaUV T93drzsN5+fFePzUoNFE5SkAYPYATw3WDE7OlPsPwxwzAMhJUK4DDKasrG347gUl0E0m bjiWWESP9bDXOkIXBXe5d/i1vj2ID5gyS8VmBT98ZfA0J7RSycx3pK9aDweUnUg1pEpZ OMJRY5agn0k5JzWysk/6LYC5fkHk1aZcSyGv+bny6dm9yLROA7+zldsiELd3m9avnZ+y AOqsx3XjHgeRNK9pgBLcqe3SoweWvFnkgZGQmTVd+2LdyxnWwa93d1buUfX/MTMzuYaq imaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692135340; x=1692740140; 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=bCGvdYVO9EKbpa2jmZF0w8IsHUC4STqQLwNPEWIlFrg=; b=MKSEudTuBgdiqHXrlsKrRPTX/ZjKLDcmWC4JhyFnhvglWMXK1fWMHnpx92b2L/yMuM 73+LXrOuawa/XpDD4gP58NLK7OXXOUKZeohQBIHCAwM/5i88RcmR463J9Q4m1EYDFPSp FicIzy1RcnvjL0QG3APWEq1Q1Nw1PHgKTHzBwaIz1bGVEwH7gqg+cZQxqi1d8tWtEbq/ RzYwvgPqvf/IynDLUg1zdA8VmpjHd4XiSzOHB/DAriYAjjrWPD3g1bRXQ1/+uyiXxi+Q nWfbvPrnVWBt+IsglUrPEkUy4MOVWtiYxjoZWhkPn/9MvLZryyWUH3An9ICN9UxOkv0/ AwOA== X-Gm-Message-State: AOJu0YzLMwRvFgflm2XZ3Ss9PvmrheWg1YetMNUIvFbfOUPyVpra3wVI gwlsKI4ta2qKql2LsJR4CizhWkqKZQU= X-Google-Smtp-Source: AGHT+IGRuzrjE+vpyIzdyF/OQOo1U5zpH5XKrPubx9S5xZyoHO/CMSeIvVbf4AW+/aR8z9iCg8Y6aE7RL/Y= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:af27:0:b0:579:f832:74b with SMTP id n39-20020a81af27000000b00579f832074bmr200736ywh.10.1692135340576; Tue, 15 Aug 2023 14:35:40 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 15 Aug 2023 14:35:24 -0700 In-Reply-To: <20230815213533.548732-1-seanjc@google.com> Mime-Version: 1.0 References: <20230815213533.548732-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.694.ge786442a9b-goog Message-ID: <20230815213533.548732-2-seanjc@google.com> Subject: [PATCH 01/10] KVM: SVM: Drop pointless masking of default APIC base when setting V_APIC_BAR From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Maxim Levitsky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop VMCB_AVIC_APIC_BAR_MASK, it's just a regurgitation of the maximum theoretical 4KiB-aligned physical address, i.e. is not novel in any way, and its only usage is to mask the default APIC base, which is 4KiB aligned and (obviously) a legal physical address. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/svm.h | 2 -- arch/x86/kvm/svm/avic.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 72ebd5e4e975..1e70600e84f7 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -257,8 +257,6 @@ struct __attribute__ ((__packed__)) vmcb_control_area { =20 #define AVIC_DOORBELL_PHYSICAL_ID_MASK GENMASK_ULL(11, 0) =20 -#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL - #define AVIC_UNACCEL_ACCESS_WRITE_MASK 1 #define AVIC_UNACCEL_ACCESS_OFFSET_MASK 0xFF0 #define AVIC_UNACCEL_ACCESS_VECTOR_MASK 0xFFFFFFFF diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index cfc8ab773025..7062164e4041 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -251,7 +251,7 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *= vmcb) vmcb->control.avic_backing_page =3D bpa & AVIC_HPA_MASK; vmcb->control.avic_logical_id =3D lpa & AVIC_HPA_MASK; vmcb->control.avic_physical_id =3D ppa & AVIC_HPA_MASK; - vmcb->control.avic_vapic_bar =3D APIC_DEFAULT_PHYS_BASE & VMCB_AVIC_APIC_= BAR_MASK; + vmcb->control.avic_vapic_bar =3D APIC_DEFAULT_PHYS_BASE; =20 if (kvm_apicv_activated(svm->vcpu.kvm)) avic_activate_vmcb(svm); --=20 2.41.0.694.ge786442a9b-goog From nobody Wed Dec 17 23:51:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88DBFC04FE0 for ; Tue, 15 Aug 2023 21:36:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240288AbjHOVgR (ORCPT ); Tue, 15 Aug 2023 17:36:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240295AbjHOVfw (ORCPT ); Tue, 15 Aug 2023 17:35:52 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DE5B1FCE for ; Tue, 15 Aug 2023 14:35:42 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-68877684da1so753452b3a.1 for ; Tue, 15 Aug 2023 14:35:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692135342; x=1692740142; 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=SFpi+WGuvYcTqc5iutU3gDOga20tlCn0kmKYiGzba1s=; b=74tVcNm00OZaHpD1xVuPf4Jjj8KBDUlS++wNuw6IOr9Xww7JLEHfMfpDin20hyekXn UAZzdeOvKkgZUS6btGqtbj1EtOcbudgfP8uunjSwa10kl53dOsD2H4PMXQQ7CpyOKOA0 fFJjtiO9IkGgau4YpenIfY616mMKNSNOk/KybhEQhb/R1OBjzFS1FxAE5sc0kRQ+eebR f+hPBPVTsmtcKA98ubnfVz6rHszpNxsmqCasREoyiC8y1zLZQWM3maIi8g+mst+1jtCV qq4sZhxyR+JYTtYagySkZoMNAZfN36FpIVb+Q3rAOdXkc6+JefuhcRoK+Iwm0FGQTYOH wB+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692135342; x=1692740142; 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=SFpi+WGuvYcTqc5iutU3gDOga20tlCn0kmKYiGzba1s=; b=M4wFFSbebaN4Xj0hBKTS11vw5PTWluKnUVG7z1R19OVx6yAR1YIymLexhTvE4RVkPY 3wKHmRyRf4vV7wKaRpkSOj4TNgffOhf5w+X6DbtFzXQolzX2mJ8LZZE7PbOEk9z70SON nngzCgcVoniT2+bYIqQ0qUXw31B5QZQJ+ZL5ev3dHbEsI+xi8m3qWRCebqK2AR0vD9Vk 4WY5Fnh+/OTYy+yM8rMQyKHUaniiOetsFK/TkrXvJ57URnoccPpQ7f8jQZrEnSvGgRSY uXzYQFXLMd+DJSA31h1r89D+lroaXVHQmFsQ2RlSDIt0JZEWUOrGO4CrSn01XL5rxtPl CC+A== X-Gm-Message-State: AOJu0YyEJjW6t4vXwl58HGCzfhNDe6LuMfp9HkcZXylccMTdSzeAT/7q PYRw119hU1zKvVk5FMPgwhkaH0GSshA= X-Google-Smtp-Source: AGHT+IG5MHzZLav8gJ1/MnkDQMEocS8Nk95fglVaOP6iG8M0zB5pJMu3CTRFZ48wrIVfKHX4OCFGKU3C7d8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:a17:b0:682:24c1:2951 with SMTP id p23-20020a056a000a1700b0068224c12951mr8029pfh.0.1692135342319; Tue, 15 Aug 2023 14:35:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 15 Aug 2023 14:35:25 -0700 In-Reply-To: <20230815213533.548732-1-seanjc@google.com> Mime-Version: 1.0 References: <20230815213533.548732-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.694.ge786442a9b-goog Message-ID: <20230815213533.548732-3-seanjc@google.com> Subject: [PATCH 02/10] KVM: SVM: Use AVIC_HPA_MASK when initializing vCPU's Physical ID entry From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Maxim Levitsky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use AVIC_HPA_MASK instead of AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK when initializing a vCPU's Physical ID table entry, the two masks are identical. Keep both #defines for now, along with a few new static asserts. A future change will clean up the entire mess (spoiler alert, the masks are pointless). Opportunisitically move the bitwise-OR of AVIC_PHYSICAL_ID_ENTRY_VALID_MASK outside of the call to __sme_set(), again to pave the way for code deduplication. __sme_set() is purely additive, i.e. ORing in the valid bit before or after the C-bit does not change the end result. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/svm.h | 2 ++ arch/x86/kvm/svm/avic.c | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 1e70600e84f7..609c9b596399 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -285,6 +285,8 @@ static_assert((AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX= _INDEX_MASK) =3D=3D AVIC_MAX_ static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) =3D= =3D X2AVIC_MAX_PHYSICAL_ID); =20 #define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) +static_assert(AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK =3D=3D AVIC_HPA_MAS= K); +static_assert(AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK =3D=3D GENMASK_ULL(= 51, 12)); =20 #define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) =20 diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 7062164e4041..442c58ef8158 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -308,9 +308,8 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) if (!entry) return -EINVAL; =20 - new_entry =3D __sme_set((page_to_phys(svm->avic_backing_page) & - AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK) | - AVIC_PHYSICAL_ID_ENTRY_VALID_MASK); + new_entry =3D __sme_set(page_to_phys(svm->avic_backing_page) & AVIC_HPA_M= ASK) | + AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; WRITE_ONCE(*entry, new_entry); =20 svm->avic_physical_id_cache =3D entry; --=20 2.41.0.694.ge786442a9b-goog From nobody Wed Dec 17 23:51:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E9C7CC05052 for ; Tue, 15 Aug 2023 21:36:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240314AbjHOVgU (ORCPT ); Tue, 15 Aug 2023 17:36:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240298AbjHOVfy (ORCPT ); Tue, 15 Aug 2023 17:35:54 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B852A19A5 for ; Tue, 15 Aug 2023 14:35:44 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-583c49018c6so75527387b3.0 for ; Tue, 15 Aug 2023 14:35:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692135344; x=1692740144; 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=/PvDkYU7pub77IFsZZiQMv91FRnqIzJSJjC/8q+MYGc=; b=oSgseZt86wOzM6XbvxcoKwzE9JqjHNoKLVYniiWvDo9//Z99SbHzwV1kCKmMJb7jyb /KKsQDeS/ouvKqjA81omMCunJ1t8ioevf4rM7TFeZ5swcs/C9dxOC0mnHpAZeLK/VYiZ lklz1En2ZrCx4Cs5N6ZtsMDApZTn97E1+qBN1C3rvAlFShcFavHxOFWwtWKN3w9Jltwu rorsnqd+5JmQgKRO1p6N1yGSlbq7sgl5rCmzSXS+lkdQqZps24CXZb4XwtiMQGvwf+/a S3ZdeFHXksmbvYQ4snDjFBMWt1zRt8Sra8Sp6SL+KfKcu9cGUBntyLlDpMbET+760pvL vzSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692135344; x=1692740144; 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=/PvDkYU7pub77IFsZZiQMv91FRnqIzJSJjC/8q+MYGc=; b=RJVssde440EQiq1wSlEbh6HzxvKJWh0vIX0bFTGskuRbnjoeMdq5FAF5lT+dcGU1mU 742Qilc8SvgEc5e4rrdZSJm9Ia4OPkrnMstvY/RRJAeIoYljY9mvMQ+DieMRDBPdZvUm 2tj6+ESiR980yFbDm1pDgXsCwf2GrIvd8bGfn0DmUb42mj/5WHGNBu0bivRbxpkNdFgb EbWsxPb3o8ysr3L5o+oFwu15Pz2p6zWOjZIGVjah78qZh6q6YzCdZQqcJkgnac84+VLt NrBSMfDsaTzogtOcJCKD+nU9PeC8VmshXVkNAmxVqAI6rxACdR/mehKQxnxFppZCkv6E UKJQ== X-Gm-Message-State: AOJu0YwG+6sTefT7DGFUjy3x3a/efqrGFRmCkC2fqCtXN0XdwIrQYLMR QxME/VOdRc/0QMgF/S96ipsR7Z+H9Zs= X-Google-Smtp-Source: AGHT+IEZFDsXm9stnifzZSAuqvVw0msILihz96PnJBxpFjupmiJnnJYjLfZp3vldSCKfc9/0T4IECdwkeoc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:bc41:0:b0:58c:8c9f:c05a with SMTP id b1-20020a81bc41000000b0058c8c9fc05amr3247ywl.9.1692135344061; Tue, 15 Aug 2023 14:35:44 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 15 Aug 2023 14:35:26 -0700 In-Reply-To: <20230815213533.548732-1-seanjc@google.com> Mime-Version: 1.0 References: <20230815213533.548732-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.694.ge786442a9b-goog Message-ID: <20230815213533.548732-4-seanjc@google.com> Subject: [PATCH 03/10] KVM: SVM: Drop pointless masking of kernel page pa's with "AVIC's" HPA mask From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Maxim Levitsky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop AVIC_HPA_MASK and all its users, the mask is just the 4KiB-aligned maximum theoretical physical address for x86-64 CPUs. All usage in KVM masks the result of page_to_phys(), which on x86-64 is guaranteed to be 4KiB aligned and a legal physical address; if either of those requirements doesn't hold true, KVM has far bigger problems. The unnecessarily masking in avic_init_vmcb() also incorrectly assumes that SME's C-bit resides between bits 51:11; that holds true for current CPUs, but isn't required by AMD's architecture: In some implementations, the bit used may be a physical address bit Key word being "may". Opportunistically use the GENMASK_ULL() version for AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK, which is far more readable than a set of repeating Fs. Keep the macro even though it's unused, and will likely never be used, as it helps visualize the layout of an entry. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/svm.h | 6 +----- arch/x86/kvm/svm/avic.c | 11 +++++------ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 609c9b596399..df644ca3febe 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -250,7 +250,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31) =20 #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK GENMASK_ULL(11, 0) -#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK (0xFFFFFFFFFFULL << 12) +#define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK GENMASK_ULL(51, 12) #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62) #define AVIC_PHYSICAL_ID_ENTRY_VALID_MASK (1ULL << 63) #define AVIC_PHYSICAL_ID_TABLE_SIZE_MASK (0xFFULL) @@ -284,10 +284,6 @@ enum avic_ipi_failure_cause { static_assert((AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) =3D=3D= AVIC_MAX_PHYSICAL_ID); static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) =3D= =3D X2AVIC_MAX_PHYSICAL_ID); =20 -#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF) -static_assert(AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK =3D=3D AVIC_HPA_MAS= K); -static_assert(AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK =3D=3D GENMASK_ULL(= 51, 12)); - #define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) =20 struct vmcb_seg { diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 442c58ef8158..b8313f2d88fa 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -248,9 +248,9 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *= vmcb) phys_addr_t lpa =3D __sme_set(page_to_phys(kvm_svm->avic_logical_id_table= _page)); phys_addr_t ppa =3D __sme_set(page_to_phys(kvm_svm->avic_physical_id_tabl= e_page)); =20 - vmcb->control.avic_backing_page =3D bpa & AVIC_HPA_MASK; - vmcb->control.avic_logical_id =3D lpa & AVIC_HPA_MASK; - vmcb->control.avic_physical_id =3D ppa & AVIC_HPA_MASK; + vmcb->control.avic_backing_page =3D bpa; + vmcb->control.avic_logical_id =3D lpa; + vmcb->control.avic_physical_id =3D ppa; vmcb->control.avic_vapic_bar =3D APIC_DEFAULT_PHYS_BASE; =20 if (kvm_apicv_activated(svm->vcpu.kvm)) @@ -308,7 +308,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) if (!entry) return -EINVAL; =20 - new_entry =3D __sme_set(page_to_phys(svm->avic_backing_page) & AVIC_HPA_M= ASK) | + new_entry =3D __sme_set(page_to_phys(svm->avic_backing_page)) | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; WRITE_ONCE(*entry, new_entry); =20 @@ -917,8 +917,7 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int h= ost_irq, struct amd_iommu_pi_data pi; =20 /* Try to enable guest_mode in IRTE */ - pi.base =3D __sme_set(page_to_phys(svm->avic_backing_page) & - AVIC_HPA_MASK); + pi.base =3D __sme_set(page_to_phys(svm->avic_backing_page)); pi.ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode =3D true; --=20 2.41.0.694.ge786442a9b-goog From nobody Wed Dec 17 23:51:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4E4B2C07E8F for ; Tue, 15 Aug 2023 21:36:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240339AbjHOVgX (ORCPT ); Tue, 15 Aug 2023 17:36:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240307AbjHOVfz (ORCPT ); Tue, 15 Aug 2023 17:35:55 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4AB01FDE for ; Tue, 15 Aug 2023 14:35:46 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d66c957e1b2so3507435276.0 for ; Tue, 15 Aug 2023 14:35:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692135346; x=1692740146; 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=IBUBLv/HPCB4LKxnLIHtLexFhB0Q0oPfVfCHc1YuUoQ=; b=ftNRWSl2wcGncg449q6rittoHtsDkLodn9kmNNUK4LvngOrGtwDM2K09o9jf3R2pWi J3rnWu1wfSmkPaWGCrTivT9XIwI25fnxEfMmLd6yElwf41//GnxDXpOLLLVK7QiJpuph UKe2V+pWXdFaU0qqnuvDqot6lzX4CyW+S+uwhI8I6SEQKtr1i6KNzbfokbei/gfCCPoT ZmVsyHDjk0qvfkmihtZhiRv4q2CrPlagNMXQjJOA1vii66vGwm3w3VkLMCuZDqEakcrX KT0mCmG0VjFv/EahokcTaUV85DWjL6GdJehIiImnAwPJF2dIpRUWlXyzp6djaTYHXdaR Vg9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692135346; x=1692740146; 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=IBUBLv/HPCB4LKxnLIHtLexFhB0Q0oPfVfCHc1YuUoQ=; b=e+pKyS8sSUhfh54RRgsJeVQapJ4Rcm0vxSOM7xx/lIoFb2IWl08054dfDCs2C7cV30 hz01WrL3VyYnnE7J1rvwp5MiULLwKqgC317UHL76hm4vl11k1YqtdQZhrIILVgQ+XXsA 1X2pgfpdftwboHkfI6OYO7thL1n0P+mCkajFil+cfDxaC/SOWCEJmNGnT4HXJcDx+rGU gmjNVNXOZPco/StthPpfNdZlVJvG2vjENFHSdrwnMDfmuFg6B1XNAobSSQvwQlsuJspj rlVIcgtoRf2xlkzCstH47oICDJUItzusPOQZxj0HmS2NOHHUqkVIpgA+YmjMBHXu8cwC 28sw== X-Gm-Message-State: AOJu0YxDuU/maiOuY4UOThfgeMHBTlcshxTP6UMMhZz9/Wr9jXf+yaf6 fw7qKCCwmUT/fXIkWg88jz6IZcPeClM= X-Google-Smtp-Source: AGHT+IEe878IgObP3kTHS4toaTgW55ygHhngjMRQJ+zm+0LnGR8rirD+rxQqbK+V+ONXjM8I6rX58b3A/iQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a5b:90e:0:b0:d6b:1a89:6673 with SMTP id a14-20020a5b090e000000b00d6b1a896673mr100ybq.5.1692135345838; Tue, 15 Aug 2023 14:35:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 15 Aug 2023 14:35:27 -0700 In-Reply-To: <20230815213533.548732-1-seanjc@google.com> Mime-Version: 1.0 References: <20230815213533.548732-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.694.ge786442a9b-goog Message-ID: <20230815213533.548732-5-seanjc@google.com> Subject: [PATCH 04/10] KVM: SVM: Add helper to deduplicate code for getting AVIC backing page From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Maxim Levitsky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a helper to get the physical address of the AVIC backing page, both to deduplicate code and to prepare for getting the address directly from apic->regs, at which point it won't be all that obvious that the address in question is what SVM calls the AVIC backing page. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Maxim Levitsky --- arch/x86/kvm/svm/avic.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index b8313f2d88fa..954bdb45033b 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -241,14 +241,18 @@ int avic_vm_init(struct kvm *kvm) return err; } =20 +static phys_addr_t avic_get_backing_page_address(struct vcpu_svm *svm) +{ + return __sme_set(page_to_phys(svm->avic_backing_page)); +} + void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb) { struct kvm_svm *kvm_svm =3D to_kvm_svm(svm->vcpu.kvm); - phys_addr_t bpa =3D __sme_set(page_to_phys(svm->avic_backing_page)); phys_addr_t lpa =3D __sme_set(page_to_phys(kvm_svm->avic_logical_id_table= _page)); phys_addr_t ppa =3D __sme_set(page_to_phys(kvm_svm->avic_physical_id_tabl= e_page)); =20 - vmcb->control.avic_backing_page =3D bpa; + vmcb->control.avic_backing_page =3D avic_get_backing_page_address(svm); vmcb->control.avic_logical_id =3D lpa; vmcb->control.avic_physical_id =3D ppa; vmcb->control.avic_vapic_bar =3D APIC_DEFAULT_PHYS_BASE; @@ -308,7 +312,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) if (!entry) return -EINVAL; =20 - new_entry =3D __sme_set(page_to_phys(svm->avic_backing_page)) | + new_entry =3D avic_get_backing_page_address(svm) | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; WRITE_ONCE(*entry, new_entry); =20 @@ -859,7 +863,7 @@ get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq= _routing_entry *e, pr_debug("SVM: %s: use GA mode for irq %u\n", __func__, irq.vector); *svm =3D to_svm(vcpu); - vcpu_info->pi_desc_addr =3D __sme_set(page_to_phys((*svm)->avic_backing_p= age)); + vcpu_info->pi_desc_addr =3D avic_get_backing_page_address(*svm); vcpu_info->vector =3D irq.vector; =20 return 0; @@ -917,7 +921,7 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int h= ost_irq, struct amd_iommu_pi_data pi; =20 /* Try to enable guest_mode in IRTE */ - pi.base =3D __sme_set(page_to_phys(svm->avic_backing_page)); + pi.base =3D avic_get_backing_page_address(svm); pi.ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode =3D true; --=20 2.41.0.694.ge786442a9b-goog From nobody Wed Dec 17 23:51:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12BACC07E8B for ; Tue, 15 Aug 2023 21:36:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240327AbjHOVgW (ORCPT ); Tue, 15 Aug 2023 17:36:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240309AbjHOVf4 (ORCPT ); Tue, 15 Aug 2023 17:35:56 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27C841BC3 for ; Tue, 15 Aug 2023 14:35:48 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-564f73e2c59so6286380a12.2 for ; Tue, 15 Aug 2023 14:35:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692135347; x=1692740147; 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=CHnuPfUXbmZWu5F2MvTRaFvjEQfMFtfyp4fO+JPGxbI=; b=kVAV3xXrMTAbLQ3rPVK4qegv5MzcfS8e5dljWpr6AvcDuBCLiIlKECoYWD0XJVho2d jGHvwxgZljxFQgu9npgxS0x58JZUu5bU6WNjVCyPnO35SrWnCN2H+7CpCT70QXB8cRcr Pm68+mIdMG7Vl/qw6lSltNtd2pWdOLis+Zl8fjgJ6ApWsU6cxkl4XOtNCEDxY2oiqK5g eHSIZrYlahQWr00vrJsklrB6UFyfC5BbaAtr0DMTu0XBUvY5RFq/76/YBxjT4ujsAS2m vAqC/DTBbvrhK664IFPuNgeJmAz/J50Ejod+21zKsCGZ5Otsf7bOl91ky9QfiCR7/JOz 00ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692135347; x=1692740147; 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=CHnuPfUXbmZWu5F2MvTRaFvjEQfMFtfyp4fO+JPGxbI=; b=J86EiJr1Cj5a/u0104uwnN2TO9WgTBlPvv9cGQ5udO+a6WV5QKTIwHqAfzLR1SMYy1 OoALuVQ6ULnU6TL8GqBDFHIcX/g9508G/Na0wW86P9p+EBvmOultSBZe9KtXh+dAwfpc xr657jmCZntj3F09ZBZLqnvcDrtkS+vjCuzzdvvhbkRZ6f/OWVzysLyGn2PUaL0SjG+k K3dOSG3IfGWSGUIbmHRO5iSBwHQ5HUZXXGDPWRd//HjcNqVnzYxrFpANBCobiU+QtzGj l8tX9PyQzWPJB4pYmKTXwaA0q4fwdyuUrY/Oi9697Jcy4eLuFq2ibLfA7zEREtJ8PSPS SZsg== X-Gm-Message-State: AOJu0YyrQ6/a7xPvQ8urzz6IPPefvXvRmePgT14PL15SSVlRFZzMN5sC 9Po9q1vgYZc5o/VfKXdd9sSGeYXmoH0= X-Google-Smtp-Source: AGHT+IGZpFTzWg3Yj6yV3ARgS1gRpt4WlnLvCCsXQ6y9zKKLCPAYed5RzAVM7pWLFGE752cW0iko5FNITv0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:1c3:b0:1b5:2496:8c0d with SMTP id e3-20020a17090301c300b001b524968c0dmr24608plh.3.1692135347694; Tue, 15 Aug 2023 14:35:47 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 15 Aug 2023 14:35:28 -0700 In-Reply-To: <20230815213533.548732-1-seanjc@google.com> Mime-Version: 1.0 References: <20230815213533.548732-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.694.ge786442a9b-goog Message-ID: <20230815213533.548732-6-seanjc@google.com> Subject: [PATCH 05/10] KVM: SVM: Drop vcpu_svm's pointless avic_backing_page field From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Maxim Levitsky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop vcpu_svm's avic_backing_page pointer and instead grab the physical address of KVM's vAPIC page directly from the source. Getting a physical address from a kernel virtual address is not an expensive operation, and getting the physical address from a struct page is *more* expensive for CONFIG_SPARSEMEM=3Dy kernels. Regardless, none of the paths that consume the address are hot paths, i.e. shaving cycles is not a priority. Eliminating the "cache" means KVM doesn't have to worry about the cache being invalid, which will simplify a future fix when dealing with vCPU IDs that are too big. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 4 +--- arch/x86/kvm/svm/svm.h | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 954bdb45033b..e49b682c8469 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -243,7 +243,7 @@ int avic_vm_init(struct kvm *kvm) =20 static phys_addr_t avic_get_backing_page_address(struct vcpu_svm *svm) { - return __sme_set(page_to_phys(svm->avic_backing_page)); + return __sme_set(__pa(svm->vcpu.arch.apic->regs)); } =20 void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb) @@ -305,8 +305,6 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) return ret; } =20 - svm->avic_backing_page =3D virt_to_page(vcpu->arch.apic->regs); - /* Setting AVIC backing page address in the phy APIC ID table */ entry =3D avic_get_physical_id_entry(vcpu, id); if (!entry) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 2237230aad98..a9fde1bb85ee 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -261,7 +261,6 @@ struct vcpu_svm { =20 u32 ldr_reg; u32 dfr_reg; - struct page *avic_backing_page; u64 *avic_physical_id_cache; =20 /* --=20 2.41.0.694.ge786442a9b-goog From nobody Wed Dec 17 23:51:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F188C10F00 for ; Tue, 15 Aug 2023 21:36:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240344AbjHOVgY (ORCPT ); Tue, 15 Aug 2023 17:36:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240322AbjHOVf6 (ORCPT ); Tue, 15 Aug 2023 17:35:58 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15D531BDD for ; Tue, 15 Aug 2023 14:35:50 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-268476c3b2aso7913497a91.1 for ; Tue, 15 Aug 2023 14:35:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692135349; x=1692740149; 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=jXXlrX5DzE4Hghu7ToUaWJNzp/mTz7CuA6+i3yYWT20=; b=USjc9iB3NqYv1bv2FZwD7xwEEynvodyTs0vCg/keKBBbTvxsxOjxknFq9MHnwQ665M eSQqAqV66fZIdfZFXD2bmMhEmAisBI78aYi9OP+x1eaVpHRzEJZrDwbwB4ep4ObGkWnF gN+mveNmI46n7FTJcfDySmGbvsGmYjA9jk1oU9PkQb902bONPll6NLl01s9WMmWhH4XZ n5Hpf9Us0QgLfPrMV5ANSkTDiADkT8EegqmGBbf2dkvcO8dcbxet6I+QQY23PCRO26S3 7Z41veYXH0HWxpS7gQikz5YJdMdIQvfaQ7jZS2eEQl17qUqFGVhs/KTMiU03AMAGPJz4 C+OA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692135349; x=1692740149; 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=jXXlrX5DzE4Hghu7ToUaWJNzp/mTz7CuA6+i3yYWT20=; b=WQ7sJd0yWD+SHTucTeA7dyES77V74eFf84qryNyfIevki/nujtZKguPbg6exINa1qe sUQgwqnler7HoG8c9f0p77FcmpHU3+wuqJI5W1unZLgM6Qgk8saYSW6R4mRsOjyrzBRW kWe6IXUI1HvFCplzq55omsI1tOhue5mY6IQNc1Vsmrj9MQu6eobkIKS7gI4xQY7/lXRK l/OqF/jvOdUsTi3yQRYItbrAKbE+hmo3uIaP+sAWORqhUrXLn8hVtzezciExJq++eT/S qrKQnC+KnUnSYDrWlZKIcxRNXfEsOF1zC2gBp4lD+IEFphJNOUXCtmGmvSDyPGIbn4wU m+Rw== X-Gm-Message-State: AOJu0YwW+af3Odyj2crHPCeoaXH36O9iJlqc2Vxv1m1Xk/ZfTtdGBvhG WPvgu9wt2jwNgNrJdz3xPm358htLF+g= X-Google-Smtp-Source: AGHT+IFk0HNZ30dHY5GX2GHPf9vSUKa+3cF4WUclVLhZGcIW2/yXqodNdtcU6wu1QFuNC3eybCy1wlJf+aA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:db52:b0:269:84ec:7140 with SMTP id u18-20020a17090adb5200b0026984ec7140mr838342pjx.2.1692135349548; Tue, 15 Aug 2023 14:35:49 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 15 Aug 2023 14:35:29 -0700 In-Reply-To: <20230815213533.548732-1-seanjc@google.com> Mime-Version: 1.0 References: <20230815213533.548732-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.694.ge786442a9b-goog Message-ID: <20230815213533.548732-7-seanjc@google.com> Subject: [PATCH 06/10] iommu/amd: KVM: SVM: Use pi_desc_addr to derive ga_root_ptr From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Maxim Levitsky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use vcpu_data.pi_desc_addr instead of amd_iommu_pi_data.base to get the GA root pointer. KVM is the only source of amd_iommu_pi_data.base, and KVM's one and only path for writing amd_iommu_pi_data.base computes the exact same value for vcpu_data.pi_desc_addr and amd_iommu_pi_data.base, and fills amd_iommu_pi_data.base if and only if vcpu_data.pi_desc_addr is valid, i.e. amd_iommu_pi_data.base is fully redundant. Signed-off-by: Sean Christopherson Reviewed-by: Joao Martins --- arch/x86/kvm/svm/avic.c | 1 - drivers/iommu/amd/iommu.c | 2 +- include/linux/amd-iommu.h | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index e49b682c8469..bd81e3517838 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -919,7 +919,6 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int h= ost_irq, struct amd_iommu_pi_data pi; =20 /* Try to enable guest_mode in IRTE */ - pi.base =3D avic_get_backing_page_address(svm); pi.ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode =3D true; diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index c3b58a8389b9..9814df73b9a7 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3622,7 +3622,7 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *= data, void *vcpu_info) =20 pi_data->prev_ga_tag =3D ir_data->cached_ga_tag; if (pi_data->is_guest_mode) { - ir_data->ga_root_ptr =3D (pi_data->base >> 12); + ir_data->ga_root_ptr =3D (vcpu_pi_info->pi_desc_addr >> 12); ir_data->ga_vector =3D vcpu_pi_info->vector; ir_data->ga_tag =3D pi_data->ga_tag; ret =3D amd_iommu_activate_guest_mode(ir_data); diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index 953e6f12fa1c..30d19ad0e8a9 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -20,7 +20,6 @@ struct amd_iommu; struct amd_iommu_pi_data { u32 ga_tag; u32 prev_ga_tag; - u64 base; bool is_guest_mode; struct vcpu_data *vcpu_data; void *ir_data; --=20 2.41.0.694.ge786442a9b-goog From nobody Wed Dec 17 23:51:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D660C16B13 for ; Tue, 15 Aug 2023 21:36:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240352AbjHOVgZ (ORCPT ); Tue, 15 Aug 2023 17:36:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240328AbjHOVf7 (ORCPT ); Tue, 15 Aug 2023 17:35:59 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3619D1BE5 for ; Tue, 15 Aug 2023 14:35:52 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d679709cdb5so4728279276.1 for ; Tue, 15 Aug 2023 14:35:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692135351; x=1692740151; 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=CxcghMO4oqjMEN7NjAyYvu8EEPu+7dtIkj0PyyVU1PY=; b=UGhD9P3fwdCHLmsPnxBCw3IYw9fOU6RCS+CQwGE8IF1AWIgaPQgWFwxsPo3orqd5WN JKwbQYGkn5xQAiaPPerJL/k0tWOtPbnEpwCSUoHp9lwxKvsIwjPAs/TrAdaVgz7YrDpr YBDkuDs/nY2xT9YLhf/G+X3WmSckYw83Xm1NGjnqC2R7PR63CKD3kVBBKudi7KlMxjQM 26OLmeO573eo+M6qYuUbUdYVv4VpupZ+xghORV2sVTR0bKsK6cjerz7uRPSvM3wPOX+t RjUhmUrYBYjQvHe4K4LTOkEC+rlHBm2zWYYyeMJBOwUVfwBP7Vi2jkgDroKaTHQJGGj0 NsYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692135351; x=1692740151; 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=CxcghMO4oqjMEN7NjAyYvu8EEPu+7dtIkj0PyyVU1PY=; b=VCsZSDZwlWfQvm5JKgsQz8xMu2Gx2ow963DSOzCv2uXRwXLvLExPJOi82g7RVXLzvg g0wbq2sgxaTYb5MW0BrXmhQMOfUIOJgmnhYo8Kyh11yWZBdEVYelaBqKLCh8D/VSJA8D mgkpA0cvplGJqUF2S1UHoF4LztRvsjE6YWLFJH1bpQ9AWjtot7PhTQmgV+bNFE5ZNRtS 70vZhR7VzHDUjNtStac8FOUxafCnWpCRul/WcyoYo4FES+/mKTE6U+U3JgoAz6u8TGpZ XPHAvaiJMsQ699ge3W7/Yas5q8x/ialRM15DTjZkF03Jm11IwdZg3aE+cedz18V87jNB 8mRQ== X-Gm-Message-State: AOJu0YwG1eqpu3zdYYUbqEEmzOWJsaZbJgA6iuLu8YDQeZ5rcq7sUPYA Qi5IYP7GO0yJziiWxmlRmLcBClAHi0A= X-Google-Smtp-Source: AGHT+IH/wuwBL/LCXf2KX0BggSJOFAhvk+tGLuKVrUxDAKbe8bS9qAslf27WVZxhdW5q8B/OCgwFoC140tI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:cf8c:0:b0:d10:5b67:843c with SMTP id f134-20020a25cf8c000000b00d105b67843cmr56ybg.4.1692135351506; Tue, 15 Aug 2023 14:35:51 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 15 Aug 2023 14:35:30 -0700 In-Reply-To: <20230815213533.548732-1-seanjc@google.com> Mime-Version: 1.0 References: <20230815213533.548732-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.694.ge786442a9b-goog Message-ID: <20230815213533.548732-8-seanjc@google.com> Subject: [PATCH 07/10] KVM: SVM: Inhibit AVIC if ID is too big instead of rejecting vCPU creation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Maxim Levitsky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Inhibit AVIC with a new "ID too big" flag if userspace creates a vCPU with an ID that is too big, but otherwise allow vCPU creation to succeed. Rejecting KVM_CREATE_VCPU with EINVAL violates KVM's ABI as KVM advertises that the max vCPU ID is 4095, but disallows creating vCPUs with IDs bigger than 254 (AVIC) or 511 (x2AVIC). Alternatively, KVM could advertise an accurate value depending on which AVIC mode is in use, but that wouldn't really solve the underlying problem, e.g. would be a breaking change if KVM were to ever try and enable AVIC or x2AVIC by default. Cc: Maxim Levitsky Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/svm/avic.c | 16 ++++++++++++++-- arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 60d430b4650f..4c2d659a1269 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1243,6 +1243,12 @@ enum kvm_apicv_inhibit { * mapping between logical ID and vCPU. */ APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED, + + /* + * AVIC is disabled because the vCPU's APIC ID is beyond the max + * supported by AVIC/x2AVIC, i.e. the vCPU is unaddressable. + */ + APICV_INHIBIT_REASON_ID_TOO_BIG, }; =20 struct kvm_arch { diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index bd81e3517838..522feaa711b4 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -284,9 +284,21 @@ static int avic_init_backing_page(struct kvm_vcpu *vcp= u) int id =3D vcpu->vcpu_id; struct vcpu_svm *svm =3D to_svm(vcpu); =20 + /* + * Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC + * hardware. Do so immediately, i.e. don't defer the update via a + * request, as avic_vcpu_load() expects to be called if and only if the + * vCPU has fully initialized AVIC. Bypass all of the helpers and just + * clear apicv_active directly, the vCPU isn't reachable and the VMCB + * isn't even initialized at this point, i.e. there is no possibility + * of needing to deal with the n + */ if ((!x2avic_enabled && id > AVIC_MAX_PHYSICAL_ID) || - (id > X2AVIC_MAX_PHYSICAL_ID)) - return -EINVAL; + (id > X2AVIC_MAX_PHYSICAL_ID)) { + kvm_set_apicv_inhibit(vcpu->kvm, APICV_INHIBIT_REASON_ID_TOO_BIG); + vcpu->arch.apic->apicv_active =3D false; + return 0; + } =20 if (!vcpu->arch.apic->regs) return -EINVAL; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index a9fde1bb85ee..8b798982e5d0 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -632,7 +632,8 @@ extern struct kvm_x86_nested_ops svm_nested_ops; BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED) | \ BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) | \ BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) | \ - BIT(APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED) \ + BIT(APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED) | \ + BIT(APICV_INHIBIT_REASON_ID_TOO_BIG) \ ) =20 bool avic_hardware_setup(void); --=20 2.41.0.694.ge786442a9b-goog From nobody Wed Dec 17 23:51:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C3A2C10F1A for ; Tue, 15 Aug 2023 21:36:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240357AbjHOVg0 (ORCPT ); Tue, 15 Aug 2023 17:36:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240335AbjHOVgB (ORCPT ); Tue, 15 Aug 2023 17:36:01 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1B5519A5 for ; Tue, 15 Aug 2023 14:35:54 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-589f7d66f22so25204237b3.3 for ; Tue, 15 Aug 2023 14:35:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692135354; x=1692740154; 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=PakTUVCPT5RglwMpqqLace7eQ/bk2K7iwBZe/rVtlVs=; b=5rLK6uTTx0ExsbLfIixDo7od8tbQaS4kXbrmtgrAvOiSx66zMkL0PbeN8cCO3GmcET rQfJDOFy2SKfMk/vtb/AALMSNoJTG5bAo3AKUoAhK76FiiD4f5wBluvY/NmVU7/Knp5X 4jsJimcYPRNr/ZLa/k+ME8OQVgCjtP7dLlMplcjjToWJxQ0vWbi5zYk08mCd+/wOf53g KCYyjN0tRxykyQUuNoQK/Y7V23OE4vuYeccDSAAnIMA5ucLUapfi/t0Md05tZ9Q+D1V6 0vWqH9BF/OC4Ce5aKiKt3s+fHLlyZQlzkrwwYYImjSrE61q9wggw7dmrXCp77o0mh3Vz Kgjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692135354; x=1692740154; 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=PakTUVCPT5RglwMpqqLace7eQ/bk2K7iwBZe/rVtlVs=; b=kiZBFFES3iu08PaOSQKLkUOchc+9s2wpZ0FMjNedN50U0JEW3syMAA/ZHipaQxLBXQ ORnD84fpxXWpfpBoVzk224qxq8ALt3gooI6ScrlLfjTm71Q8ISJlYjEQl/YmIrJoh2ZN bMXLHEvlU+l4pJI8bcNGHCk+hqHzxG62+PTy3AM1OIPu5ndMX7dK84EPbQxo2VoFlEXe 6CW+2xLrft6Zb7UHXAeu7Ms8rDF/upqtavM+FpTTpIGv+Ks947fYGBpznwkjPDrYg3/v KHXV+U7QgzRfghYdROzVbA+IoGx3sHbWPqBA7QCL64uFklH8q5TWic7lrR6KXy6nMMgz zxyw== X-Gm-Message-State: AOJu0Yxxw7bOcPH8QYD3qJsV4q9GPbG6lFzrdigHU5YNxHeE+t3yvdoh whxxciYrZ8w/ZntQGH9/FxVPo5e6Aa8= X-Google-Smtp-Source: AGHT+IHTFzi+EmSO3knDI1gLm28+MBww8MUDQX629AwYVZWFBhP3uZRswmmdR81NdMuUAnbgxq8ugbnzpzU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:ab08:0:b0:c61:7151:6727 with SMTP id u8-20020a25ab08000000b00c6171516727mr23ybi.10.1692135353782; Tue, 15 Aug 2023 14:35:53 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 15 Aug 2023 14:35:31 -0700 In-Reply-To: <20230815213533.548732-1-seanjc@google.com> Mime-Version: 1.0 References: <20230815213533.548732-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.694.ge786442a9b-goog Message-ID: <20230815213533.548732-9-seanjc@google.com> Subject: [PATCH 08/10] KVM: SVM: WARN if KVM attempts to create AVIC backing page with user APIC From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Maxim Levitsky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" WARN if KVM attempts to allocate a vCPU's AVIC backing page without an in-kernel local APIC. avic_init_vcpu() bails early if the APIC is not in-kernel, and KVM disallows enabling an in-kernel APIC after vCPUs have been created, i.e. it should be impossible to reach avic_init_backing_page() without the vAPIC being allocated. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 522feaa711b4..3b2d00d9ca9b 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -300,7 +300,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) return 0; } =20 - if (!vcpu->arch.apic->regs) + if (WARN_ON_ONCE(!vcpu->arch.apic->regs)) return -EINVAL; =20 if (kvm_apicv_activated(vcpu->kvm)) { --=20 2.41.0.694.ge786442a9b-goog From nobody Wed Dec 17 23:51:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF13EC18E72 for ; Tue, 15 Aug 2023 21:36:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240367AbjHOVg2 (ORCPT ); Tue, 15 Aug 2023 17:36:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240345AbjHOVgD (ORCPT ); Tue, 15 Aug 2023 17:36:03 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 894A31FC3 for ; Tue, 15 Aug 2023 14:35:56 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d65201b777dso10248594276.0 for ; Tue, 15 Aug 2023 14:35:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692135356; x=1692740156; 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=XKYtflVs6x0P0nG2XmReTxoXGzhm2UoHQdfpKsYPPNY=; b=gTV6SCMN5bJtw7ZipgqQ/Ha6xtnuyOxIzI9hPVU/IHJWYusADTAXSdZSEjqrdDvKFC H24by3+WRSsHD8ktrTbPXqbnVMsshBgkR7Qtizb5ozlvhvguhcV4O6Dy0UQmPrlGU5q1 kmSjTHik0kFE+ree6QrqFLChvoY0qLuwjy7n00kwpKgzKsukZuE4WZc3RJXjeBegfnzN QNCzsgylSTpei/Dpc2l18J7ku3sZgSKl1oRWYeP8ytJcXPe6azd0ac2ZUKTf/E1wzsiS 0J/cMrZ1DN4bFw9uLJVz7hyOkGRqhVejQvIy1/z+gDrAaxhs9P9+pI0332AplAir6Vx2 orYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692135356; x=1692740156; 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=XKYtflVs6x0P0nG2XmReTxoXGzhm2UoHQdfpKsYPPNY=; b=lCt2s7G0zDOmx4dJHwHeHwE0LPexwTTEPpItnk9znzXeFCrJrbwAXzpLUl2oKaZcYO qn3kRmGebBa7nSVblt1ZYfI2erpbrKweU3Jl60ovFdycqA1y3Jll8gvvngmfc5XDU/v/ nqA5GNVFwGoj71YKj/qMdyI2o+kP3Wml9Xb0j4/NbQDmclIa9HEfB9Kkh8ce9HZ3Mksn yjnlrPXzcgk3aVB7de2gAXfKxSJuWbnUxyUvrWbkFDC+Wd6x+AGSUvcP05c/aTJzXN6Q ii7S1nYpgJlpwtQIQdj25L01t6iC/62z0yheLJcXjkJ04Dcxrwf1F/1uG4ubBEx/DxUV cALA== X-Gm-Message-State: AOJu0YxYB9iFGYHlFClAuoQLr76pP7tGBwVxGjEWnuVC+uhAkeMuTatd 85nmTRWV+VDa9t+qRMRa9rT4M/hGAbQ= X-Google-Smtp-Source: AGHT+IErNwqVt6Ll9LVDM795Zz4F0y53aJAzMjQ8Os681uzsdutwM4dp6vSZf6bhQDOx24qg7MUvE1zlle0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1021:b0:d2c:649:f848 with SMTP id x1-20020a056902102100b00d2c0649f848mr4288ybt.1.1692135355855; Tue, 15 Aug 2023 14:35:55 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 15 Aug 2023 14:35:32 -0700 In-Reply-To: <20230815213533.548732-1-seanjc@google.com> Mime-Version: 1.0 References: <20230815213533.548732-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.694.ge786442a9b-goog Message-ID: <20230815213533.548732-10-seanjc@google.com> Subject: [PATCH 09/10] KVM: SVM: Drop redundant check in AVIC code on ID during vCPU creation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Maxim Levitsky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop avic_get_physical_id_entry()'s compatibility check on the incoming ID, as its sole caller, avic_init_backing_page(), performs the exact same check. Drop avic_get_physical_id_entry() entirely as the only remaining functionality is getting the address of the Physical ID table, and accessing the array without an immediate bounds check is kludgy. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 3b2d00d9ca9b..6803e2d7bc22 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -263,26 +263,12 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb= *vmcb) avic_deactivate_vmcb(svm); } =20 -static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu, - unsigned int index) -{ - u64 *avic_physical_id_table; - struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); - - if ((!x2avic_enabled && index > AVIC_MAX_PHYSICAL_ID) || - (index > X2AVIC_MAX_PHYSICAL_ID)) - return NULL; - - avic_physical_id_table =3D page_address(kvm_svm->avic_physical_id_table_p= age); - - return &avic_physical_id_table[index]; -} - static int avic_init_backing_page(struct kvm_vcpu *vcpu) { - u64 *entry, new_entry; + struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); + struct vcpu_svm *svm =3D to_svm(vcpu); + u64 *table, new_entry; int id =3D vcpu->vcpu_id; - struct vcpu_svm *svm =3D to_svm(vcpu); =20 /* * Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC @@ -318,15 +304,13 @@ static int avic_init_backing_page(struct kvm_vcpu *vc= pu) } =20 /* Setting AVIC backing page address in the phy APIC ID table */ - entry =3D avic_get_physical_id_entry(vcpu, id); - if (!entry) - return -EINVAL; + table =3D page_address(kvm_svm->avic_physical_id_table_page); =20 new_entry =3D avic_get_backing_page_address(svm) | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; - WRITE_ONCE(*entry, new_entry); + WRITE_ONCE(table[id], new_entry); =20 - svm->avic_physical_id_cache =3D entry; + svm->avic_physical_id_cache =3D &table[id]; =20 return 0; } --=20 2.41.0.694.ge786442a9b-goog From nobody Wed Dec 17 23:51:11 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0AC9C19F4F for ; Tue, 15 Aug 2023 21:36:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240362AbjHOVg1 (ORCPT ); Tue, 15 Aug 2023 17:36:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240347AbjHOVgE (ORCPT ); Tue, 15 Aug 2023 17:36:04 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 379A31BDD for ; Tue, 15 Aug 2023 14:35:58 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-53f6e19f814so6452868a12.3 for ; Tue, 15 Aug 2023 14:35:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1692135358; x=1692740158; 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=5htqcXlGR48S3bix4KQFlJHZscL9sK8wNsMNCEa/blA=; b=Zk+tB7gzteBvpoOVWZ93sOqgDN/4qLNDGkzUSOPo8c/LXzjJ8h85ibn1YLVhAERiz8 cEEUxZJ1MbfFQUVKhtuCw9/mueNfpYJmEN/qoahPn78RJ5HFmGVZyfkenmqPYndKsFDG d//ppkrYcZ2gdlFiTgmJUSZ0MyYKX3saNwgRaZMm4nz8PvzRc6ZGIOgit523gFh+DZgh u07AxJDGrhNUg6otI1ZGHL6OzrYzrJijDEJAhtLfxzjue6eU3MirrcJqOBD8sSZgwcwX Q32dJUZAxPwNE+ARQ4ubEy0VPVxEO5L64+V75raPf+hF0cUsvxckNki3sphpTLYyXwmi 12lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692135358; x=1692740158; 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=5htqcXlGR48S3bix4KQFlJHZscL9sK8wNsMNCEa/blA=; b=f3KpqQTF97/mioBzMWJC8E6hEbsXUT2V5GlUDQAeTH9K9MXgWx/DxGlonN7cbm6+aE lY+8IIu/j2Vt9pn1h3M4pJZpaP7sok4eZIFtkbUROTn9xPLVDM+NvIS9cmuOVCRfvYS2 6ugjfC/bnSr5P36Dg7WvHWSpf+gXKQpJNRI38h5pEqByotWT7p/3pcPiBseBVQKt0zvK p2MncB4PHzXuaOmdTbKEPPKjuchQyeHLElcxV0KGfl++uLrWLjZiFNZ+y5g8Jv1y5ZdY WjFZDxVuwdVPLs6VnM+cnxvxDqUeEwQOZ0iKcEBpabAGn6zG6kqFVvsgxhu7RXPv09Bc x47Q== X-Gm-Message-State: AOJu0YyKzbwxqaOFtmt9Cb5wH+IroZd8zN9iCyzjF+7RIYv0IFmolhC2 CDgjUF8yR4C8hG6CXOxnt7qHbHKu3OQ= X-Google-Smtp-Source: AGHT+IGcI4ph95Gs7JodHbEwJ7O1llYZ+9O4HY1NoWEkoEoV07dwpOKAONjIa8qpQqKt1nWRe0LQ+mQlSlI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:77cc:0:b0:564:1f95:71e5 with SMTP id s195-20020a6377cc000000b005641f9571e5mr13554pgc.2.1692135357795; Tue, 15 Aug 2023 14:35:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 15 Aug 2023 14:35:33 -0700 In-Reply-To: <20230815213533.548732-1-seanjc@google.com> Mime-Version: 1.0 References: <20230815213533.548732-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.694.ge786442a9b-goog Message-ID: <20230815213533.548732-11-seanjc@google.com> Subject: [PATCH 10/10] KVM: SVM: Rename "avic_physical_id_cache" to "avic_physical_id_entry" From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Maxim Levitsky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rename the vCPU's pointer to its AVIC Physical ID entry from "cache" to "entry". While the field technically caches the result of the pointer calculation, it's all too easy to misinterpret the name and think that the field somehow caches the _data_ in the table. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 10 +++++----- arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 6803e2d7bc22..8d162ff83aa8 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -310,7 +310,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; WRITE_ONCE(table[id], new_entry); =20 - svm->avic_physical_id_cache =3D &table[id]; + svm->avic_physical_id_entry =3D &table[id]; =20 return 0; } @@ -1028,14 +1028,14 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) if (kvm_vcpu_is_blocking(vcpu)) return; =20 - entry =3D READ_ONCE(*(svm->avic_physical_id_cache)); + entry =3D READ_ONCE(*(svm->avic_physical_id_entry)); WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); =20 entry &=3D ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; entry |=3D (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); entry |=3D AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; =20 - WRITE_ONCE(*(svm->avic_physical_id_cache), entry); + WRITE_ONCE(*(svm->avic_physical_id_entry), entry); avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, true); } =20 @@ -1046,7 +1046,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) =20 lockdep_assert_preemption_disabled(); =20 - entry =3D READ_ONCE(*(svm->avic_physical_id_cache)); + entry =3D READ_ONCE(*(svm->avic_physical_id_entry)); =20 /* Nothing to do if IsRunning =3D=3D '0' due to vCPU blocking. */ if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) @@ -1055,7 +1055,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) avic_update_iommu_vcpu_affinity(vcpu, -1, 0); =20 entry &=3D ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; - WRITE_ONCE(*(svm->avic_physical_id_cache), entry); + WRITE_ONCE(*(svm->avic_physical_id_entry), entry); } =20 void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 8b798982e5d0..4362048493d1 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -261,7 +261,7 @@ struct vcpu_svm { =20 u32 ldr_reg; u32 dfr_reg; - u64 *avic_physical_id_cache; + u64 *avic_physical_id_entry; =20 /* * Per-vcpu list of struct amd_svm_iommu_ir: --=20 2.41.0.694.ge786442a9b-goog