From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3AF44201000 for ; Fri, 23 May 2025 01:00:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962015; cv=none; b=qt3S+R+0AtnaQfi1OxSXEpZinGb7uUVxHsBnKjoBa4iZATDsJlNci+t8IEY/KP8FTOA2hOSx6tl+wUMn/xvzEHsqvSOJWB3wLXZTGngjtT4OcOGSTCvEFbsAcjXXPX5Vz+x5om+uhDbQXcP2BejktOBLsBnLq3GPJmiLJPXw8MQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962015; c=relaxed/simple; bh=aqId/MfG7UYKWnmP31wra99FFnDu4Tk8TU75m8cWlHk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=s2XOf7OedX8FrDDhAhHdCo2NkzODyjcMfMaD0Zwvy+6OBuyck5hi7E1uBDI1OTkUEgB2FoVBu9hzRBGOE5UTF0Za5OXsoCYhyBWDjlFeGAfFVQZQqE3N0i6jwv0eX69AySzsAXTyS/eyms+PxuFVhwgj9ZykybHIP+hG6zQSF9c= 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=apW9p/FK; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="apW9p/FK" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30ea0e890ccso6053869a91.2 for ; Thu, 22 May 2025 18:00:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962012; x=1748566812; 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=h6Fi2uGZoFyI+fpVWxb9iu5QelXyEXqpjgp2EpCb/GA=; b=apW9p/FKEnFNfVpGK6Uvom65mVDFUIdppNqNM84zLvmwn8uu1qIV5x/QX4fZFaHdra NY7unc6xhhRt9wLrWPxlB4q8K4Dqu2qDAML8TAG+HVrg/obQIRvQP9yKqfmWwdkJrdli Y14GIxF2zyb1HMLpyj4/NCNDgTQ55oC5kYYbOjDCv4KOIKe+bSkHWXWIj3TlrtMXIVg3 DYMoop/imfjAlbz7Oo30UEmrQ6bPQ9ZXNOC0YpPiYK32+U5wOC5AAWjssC1JZ08UgRaw 3VjCFQ0atpzoe43kbEppCRYEC0Z0sj51lqcNgcmXfSjDR3opReARf6JmWa/SDUW8puAS I7aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962012; x=1748566812; 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=h6Fi2uGZoFyI+fpVWxb9iu5QelXyEXqpjgp2EpCb/GA=; b=IyogPyYTYM8Y+GHgEMUDES9NV1xvUI7tGOB3RhMITP4fxJ6hGycfo+JI0+dZI+jzIb lQJy9FYoDSNI6Plsh+3xPyE/vzf0k2RnIvgqlDHW1/ytWx3iDbb36uL3zn8B22KtU7xT 9z4c+V06Xzk/swqgInRfueRh+Cts3VFMXovh1ofoHCV+3Hq6kuSxMRyDtWezOwIH+J3G 83D0RDMKgPlDToPpHaVE8CLErKllJkkfPjcOM7HjYTBwVCjxaoKyW2lP9Ifir+Ubdo/0 8uFcumjaciQ8gXeDwbLVCjShbDT8e9/hL8IXYBsH3icDYMc5BrCIpRM+Y8Ec+zbN7TCF 7BjA== X-Forwarded-Encrypted: i=1; AJvYcCVWOBkhZcBAilYnBnUhevIUq/z0zj/zafk5W128X/cJHMMENoMq/cy+1pEjP3BSdWimEt+/7dHS7ZsnJRc=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1N6bZ+ncNXXJFSpJTD8ywcVwGqFc8Unsdf4KRrDd3X9fzOHfi YSMbHRYiddhK3Gfw9NHIbswEC2ufaYqTGgsKwvX3JBIy0AvnxsxGlii9jPM6IRB94i1Y1vS7p8I 3aGSTRw== X-Google-Smtp-Source: AGHT+IEmQ/iBobSURg+CBZvIq2SLSqxNOv+mYEeHW+hSWn3teTRHzGk20jzFX4bEh5sFDD0AsX6zHW7sN2o= X-Received: from pjbdy5.prod.google.com ([2002:a17:90b:6c5:b0:2fc:1356:bcc3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3dc7:b0:310:8d73:d9d9 with SMTP id 98e67ed59e1d1-3108d73da51mr17092365a91.18.1747962012532; Thu, 22 May 2025 18:00:12 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:06 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-2-seanjc@google.com> Subject: [PATCH v2 01/59] KVM: x86: Pass new routing entries and irqfd when updating IRTEs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When updating IRTEs in response to a GSI routing or IRQ bypass change, pass the new/current routing information along with the associated irqfd. This will allow KVM x86 to harden, simplify, and deduplicate its code. Since adding/removing a bypass producer is now conveniently protected with irqfds.lock, i.e. can't run concurrently with kvm_irq_routing_update(), use the routing information cached in the irqfd instead of looking up the information in the current GSI routing tables. Opportunistically convert an existing printk() to pr_info() and put its string onto a single line (old code that strictly adhered to 80 chars). Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 6 ++++-- arch/x86/kvm/svm/avic.c | 18 +++++++---------- arch/x86/kvm/svm/svm.h | 5 +++-- arch/x86/kvm/vmx/posted_intr.c | 19 ++++++++--------- arch/x86/kvm/vmx/posted_intr.h | 8 ++++++-- arch/x86/kvm/x86.c | 36 ++++++++++++++++++--------------- include/linux/kvm_host.h | 7 +++++-- virt/kvm/eventfd.c | 11 +++++----- 8 files changed, 58 insertions(+), 52 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 21ccb122ab76..2a6ef1398da7 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -296,6 +296,7 @@ enum x86_intercept_stage; */ #define KVM_APIC_PV_EOI_PENDING 1 =20 +struct kvm_kernel_irqfd; struct kvm_kernel_irq_routing_entry; =20 /* @@ -1844,8 +1845,9 @@ struct kvm_x86_ops { void (*vcpu_blocking)(struct kvm_vcpu *vcpu); void (*vcpu_unblocking)(struct kvm_vcpu *vcpu); =20 - int (*pi_update_irte)(struct kvm *kvm, unsigned int host_irq, - uint32_t guest_irq, bool set); + int (*pi_update_irte)(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, + unsigned int host_irq, uint32_t guest_irq, + struct kvm_kernel_irq_routing_entry *new); void (*pi_start_assignment)(struct kvm *kvm); void (*apicv_pre_state_restore)(struct kvm_vcpu *vcpu); void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 7338879d1c0c..adacf00d6664 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -18,6 +18,7 @@ #include #include #include +#include =20 #include =20 @@ -885,21 +886,14 @@ get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_i= rq_routing_entry *e, return 0; } =20 -/* - * avic_pi_update_irte - set IRTE for Posted-Interrupts - * - * @kvm: kvm - * @host_irq: host irq of the interrupt - * @guest_irq: gsi of the interrupt - * @set: set or unset PI - * returns 0 on success, < 0 on failure - */ -int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq, - uint32_t guest_irq, bool set) +int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, + unsigned int host_irq, uint32_t guest_irq, + struct kvm_kernel_irq_routing_entry *new) { struct kvm_kernel_irq_routing_entry *e; struct kvm_irq_routing_table *irq_rt; bool enable_remapped_mode =3D true; + bool set =3D !!new; int idx, ret =3D 0; =20 if (!kvm_arch_has_assigned_device(kvm) || !kvm_arch_has_irq_bypass()) @@ -925,6 +919,8 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int h= ost_irq, if (e->type !=3D KVM_IRQ_ROUTING_MSI) continue; =20 + WARN_ON_ONCE(new && memcmp(e, new, sizeof(*new))); + /** * Here, we setup with legacy mode in the following cases: * 1. When cannot target interrupt to a specific vcpu. diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index e6f3c6a153a0..b35fce30d923 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -736,8 +736,9 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu); void avic_vcpu_put(struct kvm_vcpu *vcpu); void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu); void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu); -int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq, - uint32_t guest_irq, bool set); +int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, + unsigned int host_irq, uint32_t guest_irq, + struct kvm_kernel_irq_routing_entry *new); void avic_vcpu_blocking(struct kvm_vcpu *vcpu); void avic_vcpu_unblocking(struct kvm_vcpu *vcpu); void avic_ring_doorbell(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index 5c615e5845bf..110fb19848ab 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -2,6 +2,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 #include +#include =20 #include #include @@ -294,17 +295,9 @@ void vmx_pi_start_assignment(struct kvm *kvm) kvm_make_all_cpus_request(kvm, KVM_REQ_UNBLOCK); } =20 -/* - * vmx_pi_update_irte - set IRTE for Posted-Interrupts - * - * @kvm: kvm - * @host_irq: host irq of the interrupt - * @guest_irq: gsi of the interrupt - * @set: set or unset PI - * returns 0 on success, < 0 on failure - */ -int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, - uint32_t guest_irq, bool set) +int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, + unsigned int host_irq, uint32_t guest_irq, + struct kvm_kernel_irq_routing_entry *new) { struct kvm_kernel_irq_routing_entry *e; struct kvm_irq_routing_table *irq_rt; @@ -312,6 +305,7 @@ int vmx_pi_update_irte(struct kvm *kvm, unsigned int ho= st_irq, struct kvm_lapic_irq irq; struct kvm_vcpu *vcpu; struct vcpu_data vcpu_info; + bool set =3D !!new; int idx, ret =3D 0; =20 if (!vmx_can_use_vtd_pi(kvm)) @@ -329,6 +323,9 @@ int vmx_pi_update_irte(struct kvm *kvm, unsigned int ho= st_irq, hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { if (e->type !=3D KVM_IRQ_ROUTING_MSI) continue; + + WARN_ON_ONCE(new && memcmp(e, new, sizeof(*new))); + /* * VT-d PI cannot support posting multicast/broadcast * interrupts to a vCPU, we still use interrupt remapping diff --git a/arch/x86/kvm/vmx/posted_intr.h b/arch/x86/kvm/vmx/posted_intr.h index 80499ea0e674..a94afcb55f7f 100644 --- a/arch/x86/kvm/vmx/posted_intr.h +++ b/arch/x86/kvm/vmx/posted_intr.h @@ -3,6 +3,9 @@ #define __KVM_X86_VMX_POSTED_INTR_H =20 #include +#include +#include + #include =20 void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu); @@ -11,8 +14,9 @@ void pi_wakeup_handler(void); void __init pi_init_cpu(int cpu); void pi_apicv_pre_state_restore(struct kvm_vcpu *vcpu); bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu); -int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, - uint32_t guest_irq, bool set); +int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, + unsigned int host_irq, uint32_t guest_irq, + struct kvm_kernel_irq_routing_entry *new); void vmx_pi_start_assignment(struct kvm *kvm); =20 static inline int pi_find_highest_vector(struct pi_desc *pi_desc) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3ac6f7c83a06..8a4662bc2521 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13630,31 +13630,31 @@ int kvm_arch_irq_bypass_add_producer(struct irq_b= ypass_consumer *cons, struct kvm_kernel_irqfd *irqfd =3D container_of(cons, struct kvm_kernel_irqfd, consumer); struct kvm *kvm =3D irqfd->kvm; - int ret; + int ret =3D 0; =20 kvm_arch_start_assignment(irqfd->kvm); =20 spin_lock_irq(&kvm->irqfds.lock); irqfd->producer =3D prod; =20 - ret =3D kvm_x86_call(pi_update_irte)(irqfd->kvm, - prod->irq, irqfd->gsi, 1); - if (ret) - kvm_arch_end_assignment(irqfd->kvm); - + if (irqfd->irq_entry.type =3D=3D KVM_IRQ_ROUTING_MSI) { + ret =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, + irqfd->gsi, &irqfd->irq_entry); + if (ret) + kvm_arch_end_assignment(irqfd->kvm); + } spin_unlock_irq(&kvm->irqfds.lock); =20 - return ret; } =20 void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, struct irq_bypass_producer *prod) { - int ret; struct kvm_kernel_irqfd *irqfd =3D container_of(cons, struct kvm_kernel_irqfd, consumer); struct kvm *kvm =3D irqfd->kvm; + int ret; =20 WARN_ON(irqfd->producer !=3D prod); =20 @@ -13667,11 +13667,13 @@ void kvm_arch_irq_bypass_del_producer(struct irq_= bypass_consumer *cons, spin_lock_irq(&kvm->irqfds.lock); irqfd->producer =3D NULL; =20 - ret =3D kvm_x86_call(pi_update_irte)(irqfd->kvm, - prod->irq, irqfd->gsi, 0); - if (ret) - printk(KERN_INFO "irq bypass consumer (token %p) unregistration" - " fails: %d\n", irqfd->consumer.token, ret); + if (irqfd->irq_entry.type =3D=3D KVM_IRQ_ROUTING_MSI) { + ret =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, + irqfd->gsi, NULL); + if (ret) + pr_info("irq bypass consumer (token %p) unregistration fails: %d\n", + irqfd->consumer.token, ret); + } =20 spin_unlock_irq(&kvm->irqfds.lock); =20 @@ -13679,10 +13681,12 @@ void kvm_arch_irq_bypass_del_producer(struct irq_= bypass_consumer *cons, kvm_arch_end_assignment(irqfd->kvm); } =20 -int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq, - uint32_t guest_irq, bool set) +int kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, + struct kvm_kernel_irq_routing_entry *old, + struct kvm_kernel_irq_routing_entry *new) { - return kvm_x86_call(pi_update_irte)(kvm, host_irq, guest_irq, set); + return kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, irqfd->producer->i= rq, + irqfd->gsi, new); } =20 bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *old, diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 0e151db44ecd..27c7087820cb 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2397,6 +2397,8 @@ struct kvm_vcpu *kvm_get_running_vcpu(void); struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void); =20 #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS) +struct kvm_kernel_irqfd; + bool kvm_arch_has_irq_bypass(void); int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *, struct irq_bypass_producer *); @@ -2404,8 +2406,9 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypa= ss_consumer *, struct irq_bypass_producer *); void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *); void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *); -int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq, - uint32_t guest_irq, bool set); +int kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, + struct kvm_kernel_irq_routing_entry *old, + struct kvm_kernel_irq_routing_entry *new); bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *, struct kvm_kernel_irq_routing_entry *); #endif /* CONFIG_HAVE_KVM_IRQ_BYPASS */ diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 11e5d1e3f12e..85581550dc8d 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -285,9 +285,9 @@ void __attribute__((weak)) kvm_arch_irq_bypass_start( { } =20 -int __attribute__((weak)) kvm_arch_update_irqfd_routing( - struct kvm *kvm, unsigned int host_irq, - uint32_t guest_irq, bool set) +int __weak kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, + struct kvm_kernel_irq_routing_entry *old, + struct kvm_kernel_irq_routing_entry *new) { return 0; } @@ -619,9 +619,8 @@ void kvm_irq_routing_update(struct kvm *kvm) #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS) if (irqfd->producer && kvm_arch_irqfd_route_changed(&old, &irqfd->irq_entry)) { - int ret =3D kvm_arch_update_irqfd_routing( - irqfd->kvm, irqfd->producer->irq, - irqfd->gsi, 1); + int ret =3D kvm_arch_update_irqfd_routing(irqfd, &old, &irqfd->irq_entr= y); + WARN_ON(ret); } #endif --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CADAB207DF3 for ; Fri, 23 May 2025 01:00:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962017; cv=none; b=VQ2Pm4ZRSmQHgwzuBN2pLVIfH+dL8Kh2zb9W5yZISoecZJKrMeUuM9Qie2xSX6/FBOKh9vDeZwuJT5UobphdTSyyG8Tw/mbUcHozxPgbpdytHxqnmhRGl2d/GAS4HjKQox9nebiMcq+BE/1p7p4zZjj2pVtTGo7Ejghf4Ohf3sA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962017; c=relaxed/simple; bh=mvkZYjU4uGxDblpZA8nNBV7n4sCKASVs9pybsjf603s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SBIGp0aWzMesKDBJutWQkYUGDk2+RapMepcEIBxd6flCpVaPiramr5OBxUyf93xt6kpYo/03GKEMu18kIjUef9uLjQHQ6vV1p+oA6I/8ElAMDiM6WQeVJRz+50IlbDQOiJQCnRYrzwXoyj0+/4aAmeOqxz4I/WetTmw+YjrJ0Fk= 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=QoqigZcs; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QoqigZcs" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30e8425926eso9530546a91.1 for ; Thu, 22 May 2025 18:00:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962014; x=1748566814; 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=SuttjA1uv1MM8ghxAd18nmuJvf/ZeQZQp2cdIz4Q+kA=; b=QoqigZcsNp7MZ0FLSlOei1khj5/yn1as7CjYpuSAfkP1p9nrNATYtxOcBmXgEwqRYE Jrce9vT3ZL7wFzqgDaU1z5A7wO0gcgyGjHoGTnukUjc+YO7ic1P6aUFSFlXabq5PqLir Vya1YETaKuluRtZeILPn6IbypOu1avVbRKq0BQeqIE1sqEjZZrBq9+U02lyFccezCUWZ own+N+gkdDdmhnvI/6c9T6as2f19z7u4qj59rPxwG+ZJptuLDecxUUXwRedyWpE5xb+0 0Q3Af/2C6nKjtdudfkePbXUXAAw8zgfPJGtINnmhUt0JBTt62oEdeX0sLDIjaW7PL83V s8iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962014; x=1748566814; 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=SuttjA1uv1MM8ghxAd18nmuJvf/ZeQZQp2cdIz4Q+kA=; b=ci+fnw3P4FB7uJ32J585WmOcOZFBVaV68RLkVovwsDz3+4RbaXKRTbLTo0830wybBQ 3ga5sNa9niy0p6EGiK2UW0MixUU+oN7Jq0g1X17JoJRcp89fDERLeKVocsJkn9bIQqH3 /8OYGpJLWRwlGTAFPDKNYQAuia7CzWuseDZekKtN1tspbPDQ+YMv+Q/vOJsyrAmERWPk Frp5o2FhpVcBxjDy+zE7thOFdaPbQl9wg2/P9Jr2OhOPwtew8G5UxxjCABNJ456Y6wNp 5sV9MTO1ijT6GejOShWlWI1bRJGxkEfMEox3DGbUAKt8Idl7ZmcpkcaZYLM0Buy6ma3P qCFw== X-Forwarded-Encrypted: i=1; AJvYcCVpHqyEpdWQXHnUcDcG0qJ8jY2lQLt7fe71tGp3JtIjcvtNLoDIKlGsnmbSiDdjBLemTIrWHxIStjNk+FY=@vger.kernel.org X-Gm-Message-State: AOJu0Ywnxd7kI8QpwnY7dL5/X30yF/uImgylFygrNt0blRScbY6lNFYH Sk4Z+kzcMO7YJhuu8iVDSr0wjKf1IwwUni0NnavfeGpWKtt3X1xT5uolLZ3A61XMiezMR3i8QuH z3wGnJQ== X-Google-Smtp-Source: AGHT+IHvGfEkd5Q2ffJqUBN9h8J/z7B3st8B6OlB6B7Gzf7cq9xOto4DtaM1MY5H2e6vi787gSdtHFnKCYE= X-Received: from pjbqo12.prod.google.com ([2002:a17:90b:3dcc:b0:2ea:3a1b:f493]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:54d0:b0:305:2d68:8d57 with SMTP id 98e67ed59e1d1-30e830ca02bmr33157665a91.5.1747962014026; Thu, 22 May 2025 18:00:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:07 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-3-seanjc@google.com> Subject: [PATCH v2 02/59] KVM: SVM: Track per-vCPU IRTEs using kvm_kernel_irqfd structure From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Track the IRTEs that are posting to an SVM vCPU via the associated irqfd structure and GSI routing instead of dynamically allocating a separate data structure. In addition to eliminating an atomic allocation, this will allow hoisting much of the IRTE update logic to common x86. Cc: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 71 +++++++++++++++------------------------ arch/x86/kvm/svm/svm.h | 10 +++--- include/linux/kvm_irqfd.h | 3 ++ 3 files changed, 36 insertions(+), 48 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index adacf00d6664..d33c01379421 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -75,14 +75,6 @@ static bool next_vm_id_wrapped =3D 0; static DEFINE_SPINLOCK(svm_vm_data_hash_lock); bool x2avic_enabled; =20 -/* - * This is a wrapper of struct amd_iommu_ir_data. - */ -struct amd_svm_iommu_ir { - struct list_head node; /* Used by SVM for per-vcpu ir_list */ - void *data; /* Storing pointer to struct amd_ir_data */ -}; - static void avic_activate_vmcb(struct vcpu_svm *svm) { struct vmcb *vmcb =3D svm->vmcb01.ptr; @@ -746,8 +738,8 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu,= bool activate) { int ret =3D 0; unsigned long flags; - struct amd_svm_iommu_ir *ir; struct vcpu_svm *svm =3D to_svm(vcpu); + struct kvm_kernel_irqfd *irqfd; =20 if (!kvm_arch_has_assigned_device(vcpu->kvm)) return 0; @@ -761,11 +753,11 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcp= u, bool activate) if (list_empty(&svm->ir_list)) goto out; =20 - list_for_each_entry(ir, &svm->ir_list, node) { + list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) { if (activate) - ret =3D amd_iommu_activate_guest_mode(ir->data); + ret =3D amd_iommu_activate_guest_mode(irqfd->irq_bypass_data); else - ret =3D amd_iommu_deactivate_guest_mode(ir->data); + ret =3D amd_iommu_deactivate_guest_mode(irqfd->irq_bypass_data); if (ret) break; } @@ -774,27 +766,30 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcp= u, bool activate) return ret; } =20 -static void svm_ir_list_del(struct vcpu_svm *svm, struct amd_iommu_pi_data= *pi) +static void svm_ir_list_del(struct vcpu_svm *svm, + struct kvm_kernel_irqfd *irqfd, + struct amd_iommu_pi_data *pi) { unsigned long flags; - struct amd_svm_iommu_ir *cur; + struct kvm_kernel_irqfd *cur; =20 spin_lock_irqsave(&svm->ir_list_lock, flags); - list_for_each_entry(cur, &svm->ir_list, node) { - if (cur->data !=3D pi->ir_data) + list_for_each_entry(cur, &svm->ir_list, vcpu_list) { + if (cur->irq_bypass_data !=3D pi->ir_data) continue; - list_del(&cur->node); - kfree(cur); + if (WARN_ON_ONCE(cur !=3D irqfd)) + continue; + list_del(&irqfd->vcpu_list); break; } spin_unlock_irqrestore(&svm->ir_list_lock, flags); } =20 -static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data = *pi) +static int svm_ir_list_add(struct vcpu_svm *svm, + struct kvm_kernel_irqfd *irqfd, + struct amd_iommu_pi_data *pi) { - int ret =3D 0; unsigned long flags; - struct amd_svm_iommu_ir *ir; u64 entry; =20 if (WARN_ON_ONCE(!pi->ir_data)) @@ -811,25 +806,14 @@ static int svm_ir_list_add(struct vcpu_svm *svm, stru= ct amd_iommu_pi_data *pi) struct kvm_vcpu *prev_vcpu =3D kvm_get_vcpu_by_id(kvm, vcpu_id); struct vcpu_svm *prev_svm; =20 - if (!prev_vcpu) { - ret =3D -EINVAL; - goto out; - } + if (!prev_vcpu) + return -EINVAL; =20 prev_svm =3D to_svm(prev_vcpu); - svm_ir_list_del(prev_svm, pi); + svm_ir_list_del(prev_svm, irqfd, pi); } =20 - /** - * Allocating new amd_iommu_pi_data, which will get - * add to the per-vcpu ir_list. - */ - ir =3D kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_ATOMIC | __GFP_ACCOUN= T); - if (!ir) { - ret =3D -ENOMEM; - goto out; - } - ir->data =3D pi->ir_data; + irqfd->irq_bypass_data =3D pi->ir_data; =20 spin_lock_irqsave(&svm->ir_list_lock, flags); =20 @@ -844,10 +828,9 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struc= t amd_iommu_pi_data *pi) amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK, true, pi->ir_data); =20 - list_add(&ir->node, &svm->ir_list); + list_add(&irqfd->vcpu_list, &svm->ir_list); spin_unlock_irqrestore(&svm->ir_list_lock, flags); -out: - return ret; + return 0; } =20 /* @@ -951,7 +934,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, * scheduling information in IOMMU irte. */ if (!ret && pi.is_guest_mode) - svm_ir_list_add(svm, &pi); + svm_ir_list_add(svm, irqfd, &pi); } =20 if (!ret && svm) { @@ -992,7 +975,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, =20 vcpu =3D kvm_get_vcpu_by_id(kvm, id); if (vcpu) - svm_ir_list_del(to_svm(vcpu), &pi); + svm_ir_list_del(to_svm(vcpu), irqfd, &pi); } } out: @@ -1004,8 +987,8 @@ static inline int avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r) { int ret =3D 0; - struct amd_svm_iommu_ir *ir; struct vcpu_svm *svm =3D to_svm(vcpu); + struct kvm_kernel_irqfd *irqfd; =20 lockdep_assert_held(&svm->ir_list_lock); =20 @@ -1019,8 +1002,8 @@ avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu= , int cpu, bool r) if (list_empty(&svm->ir_list)) return 0; =20 - list_for_each_entry(ir, &svm->ir_list, node) { - ret =3D amd_iommu_update_ga(cpu, r, ir->data); + list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) { + ret =3D amd_iommu_update_ga(cpu, r, irqfd->irq_bypass_data); if (ret) return ret; } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index b35fce30d923..cc27877d69ae 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -310,10 +310,12 @@ struct vcpu_svm { u64 *avic_physical_id_cache; =20 /* - * Per-vcpu list of struct amd_svm_iommu_ir: - * This is used mainly to store interrupt remapping information used - * when update the vcpu affinity. This avoids the need to scan for - * IRTE and try to match ga_tag in the IOMMU driver. + * Per-vCPU list of irqfds that are eligible to post IRQs directly to + * the vCPU (a.k.a. device posted IRQs, a.k.a. IRQ bypass). The list + * is used to reconfigure IRTEs when the vCPU is loaded/put (to set the + * target pCPU), when AVIC is toggled on/off (to (de)activate bypass), + * and if the irqfd becomes ineligible for posting (to put the IRTE + * back into remapped mode). */ struct list_head ir_list; spinlock_t ir_list_lock; diff --git a/include/linux/kvm_irqfd.h b/include/linux/kvm_irqfd.h index 8ad43692e3bb..6510a48e62aa 100644 --- a/include/linux/kvm_irqfd.h +++ b/include/linux/kvm_irqfd.h @@ -59,6 +59,9 @@ struct kvm_kernel_irqfd { struct work_struct shutdown; struct irq_bypass_consumer consumer; struct irq_bypass_producer *producer; + + struct list_head vcpu_list; + void *irq_bypass_data; }; =20 #endif /* __LINUX_KVM_IRQFD_H */ --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 665CE20B81E for ; Fri, 23 May 2025 01:00:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962017; cv=none; b=pi/AzwCcMa9aZDrEgmC8igMTpGPhB+SD2Ta2+4eM/CyJI9/EqFK11kHR3KBGeG+CKoh48tsKGgDfeI6iVXwpm0lePxPklDlhif14lXf5fzL+WKFLsKE1fuPlZnE0jWBXoWXUkiQnY68EAg1j1MlF6zHNwq0dM5bofzjhBnwhnus= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962017; c=relaxed/simple; bh=x9Js4xRf2fagahtWsIdqeRgieCQlq12uWu+DkKct3A0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GVgcjFlUKwZj6bEoJNbNUt+w3DGMv38u5hAmdfHX/rK/B0D3AobOlSA7CJyntiRRI9w6eGEI7IchGrL6THfC3muQrVg4ne3trH1NX9tGrx12x5lJnnVu974sUaDbt7Fj7BOBEeVZItfba7dR5lPb6Z3/z2TOTRJAiFsJ6ikwHw8= 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=jBW6HKRJ; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jBW6HKRJ" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30e810d6901so5670358a91.3 for ; Thu, 22 May 2025 18:00:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962016; x=1748566816; 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=f+eDLIQHFMcpEOYcHglDhK5o4FzkgGqWkRl/nqwWdGk=; b=jBW6HKRJsvqLsDyZjV2jHRRVDKAhoCyAKDrRFB02ilfP5H2YtkT7M01pXGmWgJrnP1 zLMX0jpy5c/rdcypLQj6TUw6OnjPehaFQGTrrPPRkEaoBb7DrEXqGbmr2Bx4wr/pj/jg kjp3c+/5ND3/HUhvVBR2FjP6hVzaw4s6+pjvNSoeSLqoHq4kR27nblPjiKx7ud7JdPxk 0uM4ti+M2NuR2ZrDxaon52Fj3AWUEk81IkAWPvr2KVxjkhAKRFmAvxGuP7GkPXNB3pJE vgfZayoDs9NjGem6nLwGVBCzzjFQqGXvdApn26J7uZuJ8T88F3CzII8ZPtgy/aJb49Y0 3OhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962016; x=1748566816; 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=f+eDLIQHFMcpEOYcHglDhK5o4FzkgGqWkRl/nqwWdGk=; b=vtapF0HCmyIcujF5XPL9Kc4VmRDPOJjcdNngioMZPmsB7rSlYY24yZgZPKHFng6oAc DbcQK6l5MzOmaB1SxMgARpMSfGWopDmSM2CZzfY23qj8T6O5IxlOD1f/AFN7gLMrM5mN QXnVbe6ix+tDpeSV2VE/JX56w9h4rj6Wtmg+twiCfebuLMg+lveE+a18Yr8nOThRYJ8s JKkQrXQi/xVTyy0HqhMgA9go0P0V1KBiMAIwRqbD9XRFcUoPC0GMIdgEYhHBmIlbvOPg frfKgAcyTewiLSjvRYNvKXg+buLWWB17BIBsKuk8VByUOf8cS2idfIWjAxeghwQX/Rhl 2tIA== X-Forwarded-Encrypted: i=1; AJvYcCXyEgXBevyafdE78+MSOVTXKnbq+OEyRNn2MVxN7fBVj++K0k3OX1Is/7kIpbgk2JyM3F7lt5El18yG/HU=@vger.kernel.org X-Gm-Message-State: AOJu0YyC/wdZayo5v1+0MnRkQxHaFLUuKaFPcgtsKO3teJ8TR56LY6j4 nCSFM6j6hPcHuG0OiFmC5VI5IHqB/VaD0jqbS2uif7Ir3z9Y4Cyz4lPJIrcmNzxic4h1Vspp/ZE odqXLuw== X-Google-Smtp-Source: AGHT+IFRK4pCGNx5RVFqayo33KWfIvlE7D1S00v4g5t5HPuyq20BbcD+1Y9gRM+96VncuPv0B1J4drbDnBM= X-Received: from pjbsn15.prod.google.com ([2002:a17:90b:2e8f:b0:30c:4b1f:78ca]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4c87:b0:2fe:d766:ad8e with SMTP id 98e67ed59e1d1-310e96b6d28mr1783221a91.4.1747962015762; Thu, 22 May 2025 18:00:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:08 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-4-seanjc@google.com> Subject: [PATCH v2 03/59] KVM: SVM: Delete IRTE link from previous vCPU before setting new IRTE From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Delete the previous per-vCPU IRTE link prior to modifying the IRTE. If forcing the IRTE back to remapped mode fails, the IRQ is already broken; keeping stale metadata won't change that, and the IOMMU should be sufficiently paranoid to sanitize the IRTE when the IRQ is freed and reallocated. This will allow hoisting the vCPU tracking to common x86, which in turn will allow most of the IRTE update code to be deduplicated. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 60 +++++++++------------------------------ include/linux/kvm_irqfd.h | 1 + 2 files changed, 14 insertions(+), 47 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index d33c01379421..ed7374f0bd5a 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -766,23 +766,19 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcp= u, bool activate) return ret; } =20 -static void svm_ir_list_del(struct vcpu_svm *svm, - struct kvm_kernel_irqfd *irqfd, - struct amd_iommu_pi_data *pi) +static void svm_ir_list_del(struct kvm_kernel_irqfd *irqfd) { + struct kvm_vcpu *vcpu =3D irqfd->irq_bypass_vcpu; unsigned long flags; - struct kvm_kernel_irqfd *cur; =20 - spin_lock_irqsave(&svm->ir_list_lock, flags); - list_for_each_entry(cur, &svm->ir_list, vcpu_list) { - if (cur->irq_bypass_data !=3D pi->ir_data) - continue; - if (WARN_ON_ONCE(cur !=3D irqfd)) - continue; - list_del(&irqfd->vcpu_list); - break; - } - spin_unlock_irqrestore(&svm->ir_list_lock, flags); + if (!vcpu) + return; + + spin_lock_irqsave(&to_svm(vcpu)->ir_list_lock, flags); + list_del(&irqfd->vcpu_list); + spin_unlock_irqrestore(&to_svm(vcpu)->ir_list_lock, flags); + + irqfd->irq_bypass_vcpu =3D NULL; } =20 static int svm_ir_list_add(struct vcpu_svm *svm, @@ -795,24 +791,7 @@ static int svm_ir_list_add(struct vcpu_svm *svm, if (WARN_ON_ONCE(!pi->ir_data)) return -EINVAL; =20 - /** - * In some cases, the existing irte is updated and re-set, - * so we need to check here if it's already been * added - * to the ir_list. - */ - if (pi->prev_ga_tag) { - struct kvm *kvm =3D svm->vcpu.kvm; - u32 vcpu_id =3D AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag); - struct kvm_vcpu *prev_vcpu =3D kvm_get_vcpu_by_id(kvm, vcpu_id); - struct vcpu_svm *prev_svm; - - if (!prev_vcpu) - return -EINVAL; - - prev_svm =3D to_svm(prev_vcpu); - svm_ir_list_del(prev_svm, irqfd, pi); - } - + irqfd->irq_bypass_vcpu =3D &svm->vcpu; irqfd->irq_bypass_data =3D pi->ir_data; =20 spin_lock_irqsave(&svm->ir_list_lock, flags); @@ -904,6 +883,8 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, =20 WARN_ON_ONCE(new && memcmp(e, new, sizeof(*new))); =20 + svm_ir_list_del(irqfd); + /** * Here, we setup with legacy mode in the following cases: * 1. When cannot target interrupt to a specific vcpu. @@ -962,21 +943,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, pi.prev_ga_tag =3D 0; pi.is_guest_mode =3D false; ret =3D irq_set_vcpu_affinity(host_irq, &pi); - - /** - * Check if the posted interrupt was previously - * setup with the guest_mode by checking if the ga_tag - * was cached. If so, we need to clean up the per-vcpu - * ir_list. - */ - if (!ret && pi.prev_ga_tag) { - int id =3D AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag); - struct kvm_vcpu *vcpu; - - vcpu =3D kvm_get_vcpu_by_id(kvm, id); - if (vcpu) - svm_ir_list_del(to_svm(vcpu), irqfd, &pi); - } } out: srcu_read_unlock(&kvm->irq_srcu, idx); diff --git a/include/linux/kvm_irqfd.h b/include/linux/kvm_irqfd.h index 6510a48e62aa..361c07f4466d 100644 --- a/include/linux/kvm_irqfd.h +++ b/include/linux/kvm_irqfd.h @@ -60,6 +60,7 @@ struct kvm_kernel_irqfd { struct irq_bypass_consumer consumer; struct irq_bypass_producer *producer; =20 + struct kvm_vcpu *irq_bypass_vcpu; struct list_head vcpu_list; void *irq_bypass_data; }; --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 4276F212B2B for ; Fri, 23 May 2025 01:00:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962019; cv=none; b=e7pnlz1vnC263AqlGaR+LJ7JDNEAC+4ROIS2UDujRHBczIiHzDRjlNyPMT1UEynLmKDGM1AabY564XpZz5v9bMRhL32sE64ja/eMZ1dc8gf4eeE6hJHwNLH08zF/Yh8c5QtsfYq5xtPN4LA97UXyKhzSj5KSn/eKkSp2KYxtKBE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962019; c=relaxed/simple; bh=oo94zQ9KP44wU971cBBIeljSw8H5jEienGLBcJLhnEk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GkuMSEhMeZxMwljYwjjOUkJGyMJlNtruIa2jDpW5UU7pW+hhfridx68zcgm0Io/SdMB5/N5xlwb9ZoL7adVNhxJCVgcp4yYUhnh3l9Gft4wC0pTDFelGvkAWWJcFDQlqbfEWzw3Zg2ALdFpJoAWjVSMJ5TQMhi8OO5igwTNLLgI= 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=uVbBMXU2; arc=none smtp.client-ip=209.85.210.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="uVbBMXU2" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7425efba1a3so369857b3a.0 for ; Thu, 22 May 2025 18:00:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962017; x=1748566817; 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=Qq62gkgLP+wVktrApN5Abqg6uzedhCCWd9aEc00+u+w=; b=uVbBMXU2SxR3z2p1u4X+HCvvDi/UJYsjvuKq/+KMe9X616+R2M650fvw6IzKZTJoNp tqQz97dpjisJ2S1D0pHd8MjhwSfj0gqechHILkX9pDQIVkxKLBDALW0JgAlDmuGLZJIk UTscWtHErhHqwEoJz9cJ9ws5bFZhELZ83qi/gKdwOJMcHHrbKo3OpXTzkVZv96o1Utzw LyqLDe9byASIRyhZRUNZ/b/7qLFGlOFI3M2ovpC0r3JGL7f6/yqQTF7UWLb503eq633k OgIYXpwbXxNhMA6vTj9CjZ+/nhimCxG17f/OCq8Y+x5VnZviS38/tZFRMADBk2unNwqT 3EMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962017; x=1748566817; 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=Qq62gkgLP+wVktrApN5Abqg6uzedhCCWd9aEc00+u+w=; b=IN0CX591xr0SU5SA7GbUCjTmLPfx3YGJyn/I+uIctrucHcgImxnqm9EpZ/e/GtARZO NDZWS3gcbLbuNlom8CiDBDr1XKykrmFwMjOmGQzfj5LXTItllrCdp13SgtysHlTG+5Uk bb+qXMyeDbfE6B+abwsw9miVvxy2OwHeSuImE5S6T8rChAoh5HdYe5GAIPXuQfsVkJ5T 90mT4wKdvyLUZ8sVtW+Xn8Y/iprm2+quOZgV62Uzd0mDsY1nFRtUripoF6NvUZqHGGNP Jhuf7ym3ehuwVLGTfe1kEfKBHKlrGV+mOa2rmsyM2tcBazn2/UO7ZPewBy4M0D3RzQ54 LHBg== X-Forwarded-Encrypted: i=1; AJvYcCUk2YYBa4t03YD5eVLLPB+unsJOfrx+iJICnnkcgE6YgXm5ngai8uxozI0pgOJ9dbDprGYRdmXU9mC2usU=@vger.kernel.org X-Gm-Message-State: AOJu0YwvJZZrU7OT8+NtBeq4jRvJ/adspP/22dyHJ+vyyP6KNXPqUdiK AAa45Y/rur4qSgAjwR8nTRIggNMWXfdsCp2fSOrzq1iDh65Xks5xYV15BEeF8sAd2UpIBb4Nj4h vIoiQvw== X-Google-Smtp-Source: AGHT+IGuYoObsP1SNouvA21unXNvbU7VlCtk1/aF9baeg7tLlY7NAY16P1XuLv+D0VMCs09pXt7is6cf1ZE= X-Received: from pfqz29.prod.google.com ([2002:aa7:9e5d:0:b0:740:6f6:7346]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2d8c:b0:73b:ac3d:9d6b with SMTP id d2e1a72fcca58-745ece00f2cmr2255014b3a.4.1747962017305; Thu, 22 May 2025 18:00:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:09 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-5-seanjc@google.com> Subject: [PATCH v2 04/59] iommu/amd: KVM: SVM: Delete now-unused cached/previous GA tag fields From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Delete the amd_ir_data.prev_ga_tag field now that all usage is superfluous. Reviewed-by: Vasant Hegde Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 2 -- drivers/iommu/amd/amd_iommu_types.h | 1 - drivers/iommu/amd/iommu.c | 10 ---------- include/linux/amd-iommu.h | 2 +- 4 files changed, 1 insertion(+), 14 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index ed7374f0bd5a..4e8380d2f017 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -938,9 +938,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, /** * Here, pi is used to: * - Tell IOMMU to use legacy mode for this interrupt. - * - Retrieve ga_tag of prior interrupt remapping data. */ - pi.prev_ga_tag =3D 0; pi.is_guest_mode =3D false; ret =3D irq_set_vcpu_affinity(host_irq, &pi); } diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_io= mmu_types.h index 5089b58e528a..57a96f3e7b84 100644 --- a/drivers/iommu/amd/amd_iommu_types.h +++ b/drivers/iommu/amd/amd_iommu_types.h @@ -1060,7 +1060,6 @@ struct irq_2_irte { }; =20 struct amd_ir_data { - u32 cached_ga_tag; struct amd_iommu *iommu; struct irq_2_irte irq_2_irte; struct msi_msg msi_entry; diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index f34209b08b4c..f23635b062f0 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3887,23 +3887,13 @@ static int amd_ir_set_vcpu_affinity(struct irq_data= *data, void *vcpu_info) ir_data->cfg =3D irqd_cfg(data); pi_data->ir_data =3D ir_data; =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_vector =3D vcpu_pi_info->vector; ir_data->ga_tag =3D pi_data->ga_tag; ret =3D amd_iommu_activate_guest_mode(ir_data); - if (!ret) - ir_data->cached_ga_tag =3D pi_data->ga_tag; } else { ret =3D amd_iommu_deactivate_guest_mode(ir_data); - - /* - * This communicates the ga_tag back to the caller - * so that it can do all the necessary clean up. - */ - if (!ret) - ir_data->cached_ga_tag =3D 0; } =20 return ret; diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index 062fbd4c9b77..1f9b13d803c5 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -19,8 +19,8 @@ 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.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 963AD204C0F for ; Fri, 23 May 2025 01:00:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962023; cv=none; b=YvheuGLuxQG2i5UxaVJVRYfPRFnLxqJkJa/V0JL1jZSDljCDTEwM0xwf9uX732+9H6DrjYVJqadKdvM5GuHtJZFiiq7wu3VK1Ft4qHwRh6HcToJmHNAI4bb4ce8JzHEg1j7rAzbJBKL4Xm9/6BEKW5YSULK58yubUJXrO4iuRrA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962023; c=relaxed/simple; bh=xtpaD7HVsJFlh7tiOLocGEu9Ud8v7ZkKhAZvNZl2KIA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VdMgme7CbetWiOvQ5mlO4b/WTq4qhmpLPPTnrQbosB4S2KmmHZN0BUTJZmN6hOn4kf4eKSer3VZndMOF2Z2duKegT7byCcKwc0fAyA041W7gh0zyhs08wCOdk/+0PALw1cFYXw0tUH8a0a2JElNVVWMB21dQ9nRKLB9iPe5rNjs= 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=ciLt02Hg; arc=none smtp.client-ip=209.85.210.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="ciLt02Hg" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-73dcce02a5cso5495074b3a.0 for ; Thu, 22 May 2025 18:00:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962019; x=1748566819; 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=Y8ehtQMuf6IARUqP5Cyf64+dJov4SLh6VlRU3VKIIQ8=; b=ciLt02HgPiHJRw6qX2Apjld6eBoa5n3mYLT5FRj3un8UEcUV3X1Rckh74XtHUc+A+4 3vPUZH9LffAtwjwHXNnb1ICph2p3uFPsZhKcwD83OkKJwQlPdBMvA9zkYKBKss80nJvN QJDlZUsmyuEEqwXhvT02pU4+BdIHAAu2nf+iamghw8+13crn5qen+a7gWtP7DSqME4/m zerJZUSGx4F89rL3EO2zvBYryL7Penu9aOUFDz5s90MK/kRngKXtvt26a+FMVEYerDwD VIsVQXKoFQPKouPBWRB0piPMR/5ualHJwWDq/zeO1Ye4DQU1WDN9oulWHFWc4xfR7Gn5 Bdng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962019; x=1748566819; 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=Y8ehtQMuf6IARUqP5Cyf64+dJov4SLh6VlRU3VKIIQ8=; b=VJsqWs/vikkeCvOMchSiut4n7Zv1MzktYj6YcEDUZnr81CNuLPvHbJv79QKNChJIJm e9ngrm7VEiKqT5f9XtI+XGsdTF56NtGYmC2RmN4bgP8Pfm3tj6qOpbbjby6IujN2+1aw sEix1wMyR2Qq7nSMiw11CXjdYyiTKVXIqtwMUJH7wmO5e35C8MQKNwQjwNl4v3M5F5ep fMuJ1DItx79isoaNqd14IL/P5tqB56DqT2NPmW7tgUSOb2gg2arikcwHocvmhh6u2aJm QUiA3x/u/u6zALhI9cb8leNHRm68KAaYF9K7Vb8jkCVwxz58P7dg/nfAAjiOxds+n0Kk pL/Q== X-Forwarded-Encrypted: i=1; AJvYcCWg6phmxSeDycelTua/a+lV9+I1JooZh5odR8pauXOqR4twa5Jlp7gp/sTHS+gGN0KTnvgVYBKGG+tIe2A=@vger.kernel.org X-Gm-Message-State: AOJu0YxQ2JcCTK2K22/kTHeGIAxx0/1b4XiFn78pEeDLA5Xmk/TGrfD6 29CjXM/gS99qw6kXlgiP5WNU2OMX8cm4dz8Cb/eL1f0rC9nuiWcFn2NSo5qgf7XHpr7tjgDPQhu IiouIig== X-Google-Smtp-Source: AGHT+IEHm61pElXc5o66L35Mfu9cWxQa9x3UMSXO1VOYhFDjyuiVflcnZ6ZC6JzEj6wH5WWXGFCHISdtxs8= X-Received: from pfoi12.prod.google.com ([2002:aa7:87cc:0:b0:73e:665:360]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:9144:b0:736:4d05:2e2e with SMTP id d2e1a72fcca58-742accc2342mr33071193b3a.6.1747962018848; Thu, 22 May 2025 18:00:18 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:10 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-6-seanjc@google.com> Subject: [PATCH v2 05/59] KVM: SVM: Delete IRTE link from previous vCPU irrespective of new routing From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Delete the IRTE link from the previous vCPU irrespective of the new routing state, i.e. even if the IRTE won't be configured to post IRQs to a vCPU. Whether or not the new route is postable as no bearing on the *old* route. Failure to delete the link can result in KVM incorrectly updating the IRTE, e.g. if the "old" vCPU is scheduled in/out. Fixes: 411b44ba80ab ("svm: Implements update_pi_irte hook to setup posted i= nterrupt") Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 4e8380d2f017..c981ce764b45 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -861,6 +861,12 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, if (!kvm_arch_has_assigned_device(kvm) || !kvm_arch_has_irq_bypass()) return 0; =20 + /* + * If the IRQ was affined to a different vCPU, remove the IRTE metadata + * from the *previous* vCPU's list. + */ + svm_ir_list_del(irqfd); + pr_debug("SVM: %s: host_irq=3D%#x, guest_irq=3D%#x, set=3D%#x\n", __func__, host_irq, guest_irq, set); =20 @@ -883,8 +889,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, =20 WARN_ON_ONCE(new && memcmp(e, new, sizeof(*new))); =20 - svm_ir_list_del(irqfd); - /** * Here, we setup with legacy mode in the following cases: * 1. When cannot target interrupt to a specific vcpu. --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB61121CC4F for ; Fri, 23 May 2025 01:00:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962023; cv=none; b=beNJ/VXwWonyZxDU+uR4jMo0gkOrpjjKHnjjcOhAj5YBGvooWH3sfYiDokhQDE4ro0aNfCWvG3aFEIhB5ZOLQv2Wx2N2PBLLOx/Y8DN+o2fU9GP0CPINPYLz5Fn9AwCTY5KLtD/jfuCkg2/qHm99ZBSoYL08t/0NIaGBJi6vI2M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962023; c=relaxed/simple; bh=aNmoBC5BL5ItcJ/WDwb3zDPcOis7QcSa1BwF4a2ekgY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BRjg1uPKB4g+EiX35/jYHZCdUn53tEhMAzO9yzG+nIg7w4lxKHDLtb/n2bKHFe+3PAjzR2Tj60dZGRRXjne4MBMlHifi5oMrPNkF0mHBUnYY0+PENhQOrVF/tiQVvUJZp7wY5hXRTQGEfGgVAwD9Bc9WerrgxyzQqZ7NzBLSL/A= 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=xwQ68Jkg; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xwQ68Jkg" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3108d5123c4so2086588a91.1 for ; Thu, 22 May 2025 18:00:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962021; x=1748566821; 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=RwZHc1ywJBDFGfINHhpI7dwV0ed9uFhVGKRmUUyRiuk=; b=xwQ68Jkg2qRiwtI/W7HJPLIhVJ0RpJCnhYlTA4uRnMzAYgqlEPF2ybXPo7y72Q4I/4 vNlT5aDtovCt5V4RAwjpkSaIB/3HAHP/Fn79a3AprySugLqBZm1EuM8qd2vUpub33ZqM DEx03IS9L2rDvRu7aA7CEpOdZg1JtRAWAenZqRst1FRf3OXAMwB6WGY07ZKSlVGdoWz3 bqx3cG49UGvhWJHpfd3WcPAxcbD4qMf1lyEJhz47a+MRu9JhtQ9WuYT+lTZuOBHfaSdj 0PgC+Ht8j3XUBNWh7zO0ltKBhTos6WLOzPQWYxS5puiAxPFcO+EFOnhYWwLP4EWh6rQm hOgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962021; x=1748566821; 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=RwZHc1ywJBDFGfINHhpI7dwV0ed9uFhVGKRmUUyRiuk=; b=c1Z4Oam5WFjmbxkH1aX9FcU/WvY3n+DHcSo+DVtZqQFJKAvwQpPMUqpncdUV6aq3fR hloYlnrcDOrfMZtZmLSn0sZoOI2+0v0hzdGd0jIu2fSe0Rx/k95ID7Hs4OeM9pu1fj7P yOFzzGnEdbttYynmEV4oS0BrHiOPgEDhqMBF7UPneqnN9HS0qEI8EMUaO7ICPS6VUzoD W5T0pI2Pem+nuzwjbNJWRw86x6Olwk9QDJAylwz2XLF1q+hw8aEJnjT9wCdeakB9GgmJ lMZ42GXvaw+4zj6A2PU9MB3zzj4dRDGUjCDgsoGAhfZfDheOZKjEBzibMXMuQjpY6ome UhIw== X-Forwarded-Encrypted: i=1; AJvYcCW6GgdtWSFVaZRF8oSLXyHrbZ7yxXcIVpRBRgdU3sw8h6Vdli921PmsRMN8mTVOFUwbcEab0MGyol6HUIs=@vger.kernel.org X-Gm-Message-State: AOJu0YxoYeG+5LcsdqvZ9E65AoWrPDJ6TGZBgMYI6ZIE9pVInBANbiry kg0ANgyhja+Nz09O7RGkS+48PuxjyDa5dTn4r7+PQU0eEa+9IeoDJ+s1v0Nund8gn1LKmggodfq hM6ZdIA== X-Google-Smtp-Source: AGHT+IFArGigRbQ3xY6m7aPjNKtLLh8N+euULRawZd3DzF9QWqKL/5ofHFtPHZbDhE4jbFVVScTN2jPKnTM= X-Received: from pjbsg3.prod.google.com ([2002:a17:90b:5203:b0:301:1ea9:63b0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:35ca:b0:2ee:f440:53ed with SMTP id 98e67ed59e1d1-30e83228de0mr33805240a91.31.1747962021097; Thu, 22 May 2025 18:00:21 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:11 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-7-seanjc@google.com> Subject: [PATCH v2 06/59] KVM: SVM: Drop pointless masking of default APIC base when setting V_APIC_BAR From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack 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. Tested-by: Sairaj Kodilkar 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 ad954a1a6656..89a666952b01 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -260,8 +260,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 c981ce764b45..5344ae76c590 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -244,7 +244,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.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5543321E0BC for ; Fri, 23 May 2025 01:00:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962024; cv=none; b=SP6ImPZSJZXw7eP6HhI0EnAWXBWCgw1PFPdG6WNVQtgrQxpWQZiqk+XdxjwBkn1NbvFjRcW7HyrFn85/tGkMqUfbuZGBveCWQVYPrRyE/M7ORPc72Zk+6Pqvg2QTfXmrxQOupgTmdUowEHTs+5kR+k5Lpdg0UXer+5dOPFbzivA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962024; c=relaxed/simple; bh=gqinHJl4ph8b7HfdeibMhW7beyjALh/UjkVTBB01jQg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tac5Mhwlnotnvz2a9hlzAaFbY4Zat8NHlzrWrjaqAroVaZYmmiahkC3IH6yYnkfIuBQqokxXQzBDO+bbfa8vBZ4BikzB+IcDoIFd1nS6aRhWzNYRC9uGlAHPSk8jwhcPnAIisNvP+misQJSG+ERUCyIlUkTKYZ8uGnW5mNYywbQ= 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=W1nKfFt9; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="W1nKfFt9" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30eda215ea4so4610740a91.1 for ; Thu, 22 May 2025 18:00:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962022; x=1748566822; 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=9Cs4IdaV3ASNd4zhQE7PBc/8KU9AFgbUF8yCRvi0apU=; b=W1nKfFt9a/lAXIGyKXjq2I5os6RHamvBqBp+zuC2/AOa9JDaMFKfXiVXySx08ksDUE ryQHkT+sThPgeo7sCLNZhm2U0w1kdytO6JVSrFTSQ25cTnbdlAKlehrX7sWX73nH3Rm+ y/IslzD2ixqO/exQQBfPyhFvvDVAqzr2DswhSDX0p7yywPKHuBfxqJXWKiKgx+E5NiNy sLDxDcVhbjPg2/Y9UiGhQXCGvWTgA7U3bjzb695cDZugPcBm1q/ZYTnLqEUpKA9Div4b fY0qtVYj2ysKrEdArfp+QZOldu4EYw82wYXrIW4qeioHHdEgG7Fy55YveiMZI6PXbsU8 4Biw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962022; x=1748566822; 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=9Cs4IdaV3ASNd4zhQE7PBc/8KU9AFgbUF8yCRvi0apU=; b=Zz7XtN3FXDlcwrtZeRv4dViWZxMvzLVZczLvEQZ16I5lRkYKV5M4l1go7tC5PrrUzG AWBmcpXJvky5msPP+jkN7gOF1eQ3LvrwjcJ4WTUOW7Fo8kVz6MFifJ1l92A7fecpbst/ df5Z5AXHnBmxhFsffKKVKvZiWTGbdq+n7T+DS/QFq1MFV1qWem5KMLacJCHdWe3Szj3/ 55YhsD8gI0zOp9himKSpPpGcyMzk/Lw9s0Pk3SeoZ7z11KiIeEts+EKkD243sW+Hnz2B A7uyBy4LFzNvdZL75nFjMlKgjnmuOq2UGMJKFLYWGe1+DeZmv3VUBNOhiWC1rGWHrjFz TLYA== X-Forwarded-Encrypted: i=1; AJvYcCUKQbf0zEz4xEZH9vCtHjVv3bExKB2wvUk+7mU1tPYmz5cRrwsEBA58BRkdlc2vwXN0lTmbOU2pXDCKjyM=@vger.kernel.org X-Gm-Message-State: AOJu0YxIu4hOkl7JqST5caUZXA04m1eAfUXy5flqDn72OmVejjjYvG5R SEZTpyLNElaS/25kUu2QCfGFCtdxvJS84p4FaUsisraFFm8XnWMBg42B2v7wy877+ihcgdrAJiH SDPxo8g== X-Google-Smtp-Source: AGHT+IEq1yGKX3GT8g6xR16/Op9j5PmX+/amiR5/uKGcbJN98SDicWh7GG9+SGCFoQyaNG+rqASDrwH5FjU= X-Received: from pjbph15.prod.google.com ([2002:a17:90b:3bcf:b0:2f9:dc36:b11]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1648:b0:30a:3dde:6af4 with SMTP id 98e67ed59e1d1-30e7d5be456mr37760914a91.31.1747962022624; Thu, 22 May 2025 18:00:22 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:12 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-8-seanjc@google.com> Subject: [PATCH v2 07/59] KVM: SVM: Drop pointless masking of kernel page pa's with AVIC HPA masks From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack 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, as x86-64 is currently defined (going beyond PA52 would require an entirely new paging mode, which would arguably create a new, different architecture). 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. Drop masking the avic_backing_page with AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK for all the same reasons, but keep the macro even though it's unused in functional code. It's a distinct architectural define, and having the definition in software helps visualize the layout of an entry. And to be hyper-paranoid about MAXPA going beyond 52, add a compile-time assert to ensure the kernel's maximum supported physical address stays in bounds. The unnecessary 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. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/include/asm/svm.h | 4 +--- arch/x86/kvm/svm/avic.c | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 89a666952b01..36f67c69ea66 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -253,7 +253,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) @@ -288,8 +288,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) - #define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) #define SVM_SEV_FEAT_RESTRICTED_INJECTION BIT(3) #define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 5344ae76c590..4b882148f2c0 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -241,9 +241,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)) @@ -301,9 +301,12 @@ static int avic_init_backing_page(struct kvm_vcpu *vcp= u) 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); + /* Note, fls64() returns the bit position, +1. */ + BUILD_BUG_ON(__PHYSICAL_MASK_SHIFT > + fls64(AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK)); + + new_entry =3D __sme_set(page_to_phys(svm->avic_backing_page)) | + AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; WRITE_ONCE(*entry, new_entry); =20 svm->avic_physical_id_cache =3D entry; @@ -903,8 +906,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, enable_remapped_mode =3D false; =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.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 36040221F11 for ; Fri, 23 May 2025 01:00:24 +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=1747962027; cv=none; b=gdmv0dAz3UUxrdeB5wNmPAcQ3TvPFbEBB+BHjStLAyeSs8RQtyaCQscc1RzC8rr65HNbXsxWsu05w6fhMC/Vhf6nSK8gq6NAVcDEYo6jRTXkxied8QERKESi8umJzQ9FRa7TFqRdRzpTN9h6naX0CMahfzG7zE2kX9iK03sJg14= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962027; c=relaxed/simple; bh=5virlWR1yyo4ENo07jjtWumRwvWVsFnmiSwna+Ulx/0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=K6R5CkJjuSZ2OfPU6KMTIbpQ1FRDbwoQ1QrYCQPsVnIKgXrusgDJC18zd+YXxqmMaXD+BO7htH/pn0QmoW5DiyKdgP+UKUJ7IwV5XQcltXyNi59ASqoK7XrkJCIMBheHB5VHTHvTWwlHCZSTSY8doJcCopSO2z5vJllKDD6ZCH0= 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=clHUS3DJ; 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="clHUS3DJ" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-6c8f99fef10so8792584a12.3 for ; Thu, 22 May 2025 18:00:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962024; x=1748566824; 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=sR1Ojn0ohf6sLFNiGoBcbnqontvMAHQrTSIwGVP+aHg=; b=clHUS3DJjwPSb5VKK97ivpeGC4yfOFif6cN8GTHd5nP8Nj+pSowyn8D8whQ+FvLPPC KN2R8XJRpNNMTA1po1sqTPP9cQD5vMAj/8spUGtEIXosIRNHDt1R6Mx4RHQ9PH6NHBG/ jy/nZtE+ufIV4n4r+p6JsZkxNzAu29XqlfxR9PGbRCdhKb142xyTgfDMTEPR2nNBmmET FJMei434m8RsQOplWET/ohY5jDlbXh4F1q0kAOFOwKQaZ7FVjsuNbSwFtk5Ya5z3h2iV cET1igAkucgjAX0sEtY9SIEKa8vwrG01xkNTjWiLyyFxitiPGtRIMBmqEXFBCZz89fmS AgdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962024; x=1748566824; 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=sR1Ojn0ohf6sLFNiGoBcbnqontvMAHQrTSIwGVP+aHg=; b=mQDg/EckW8cNjG7Cyb/Ryb0nx95mp2XvlgcXhzoZdc2B4GCn315Noadahg7Qj1uzTu Kz9XVxDmh6Zttu8SeAeIGCUMLDz5batub3eQ7rufFJP0dsiCnwg82Vs10Q5TsuY610Kq IhSPJhq9RXxLjE86hKr/meJa5oJ+zjQ2lQq9cpZzmzKgj8Wx6aKtIj0Afg+ytUnN1guz iCh1Y1zmjBWwjMiuaxQcUqH0yPW0T7VkbwHFdWrUiEp5Cd6LuhgwQVaLgYi8JfOEAZLF iqcr4QBV4BvcVkSPq8cGlP6fa+h8CEoSZfqHvITPilhFe+xwLfMKKgF0aQHyMsOgunKi ff8g== X-Forwarded-Encrypted: i=1; AJvYcCUB1gvQ4BmKKVhB82rNoNa2cCyvK+8StT26YYOpcQCohGBr1ko614/EwUFRRr08xjMok03gJtTcgCOX1lA=@vger.kernel.org X-Gm-Message-State: AOJu0Ywc/j5q8fYMkleH8y/MllSNZapIJwn2O0o/vzRs6v+uqCupo2GW 1Usa1uQfnECdCv2lRYDVzeckchdYlPxvpIdet9Qed6eH89jzERTPtQDBtTFpFZbm90UhaRt68cC CJRka0w== X-Google-Smtp-Source: AGHT+IG7kjhiAPt1VbCwrIgMAeCdVOr8h4q7zUqtkF3UVUBioDojmu3vxag2uDQB19/1vOdzds+ONUhBcgo= X-Received: from pgar17.prod.google.com ([2002:a05:6a02:2e91:b0:b21:868e:36fd]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:9012:b0:1f5:9175:2596 with SMTP id adf61e73a8af0-2170cb260edmr38962920637.13.1747962024381; Thu, 22 May 2025 18:00:24 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:13 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-9-seanjc@google.com> Subject: [PATCH v2 08/59] KVM: SVM: Add helper to deduplicate code for getting AVIC backing page From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack 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. Reviewed-by: Maxim Levitsky Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- 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 4b882148f2c0..c36f7db9252e 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -234,14 +234,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; @@ -305,7 +309,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) BUILD_BUG_ON(__PHYSICAL_MASK_SHIFT > fls64(AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK)); =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 @@ -845,7 +849,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; @@ -906,7 +910,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, enable_remapped_mode =3D false; =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.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 034052222A5 for ; Fri, 23 May 2025 01:00:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962028; cv=none; b=dq7Xm+Y5qvOnLj5CpzVJXKyX1wbvJUpM0gNWnLKEFLh9XMPNZIza4lPoQyTEuSCbPZSuGEw+Kp4yrpSdOcUInw7ey4ekTlVO52hv3UNaY+0Q6avjab3yM3WwpYXV3ckXC9RSKwUejQw0isx6xqC4DT7h08LEUzRQHd2QE1yEhQ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962028; c=relaxed/simple; bh=j0ASWP3bWTrNWVjsJSKCYoX8Gmsgdig5lrhdNEmpSMQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OR+Au4P/YM+/uNz86Kg5coxAHae04QjMuraUDH9QVhtXQKWGPVDU2XDXSrDo3boZcD3Zk6u8PQZIwf6vM4RIxx12iDkIinzGptPHKiKq3yT2m1K5cXkr8Xx3a7E52O9NN4pepHkGs457rTOqX4oeQ05jDQKw7pBzm2IICjLZ18U= 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=0gbdXoOn; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0gbdXoOn" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30ebfc28a1eso8131566a91.2 for ; Thu, 22 May 2025 18:00:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962026; x=1748566826; 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=HLLwB8uaam+w+auGeOJdoWeeiO/rpte3+zLDk0hPf3k=; b=0gbdXoOnbnkwvcfdi5czcvmS1vEb7+1V79ug5irBpATMiHlVvJOk/JGVeUYnrVnYr6 OvdSlGoHwaNpHb0VNTWiP2RhPCaD3+qJ+7CR0GBDfjt+OfY+uliHueDdLQFeNxC4o1uJ EH+W+l1/cPsm/DGp2QFkhSWXbThY+MQqwckRuggH+EC3OgAlrGvix2GFiAk6Ma5m+cV4 lzuT/xP6vM8Fkg+/9Z+zVXYhFiC65Q40ebG+vICbuOqBuZ34dPiAPqWwI1Ykl7w3I/H+ ACUNXJBZ0T5AwXq+w9VQ/z6hxBxKw1aSQDvQFSpUEjX0Zh2es7bHcThQlby+HrYgcVwS 2VMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962026; x=1748566826; 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=HLLwB8uaam+w+auGeOJdoWeeiO/rpte3+zLDk0hPf3k=; b=i8E8OC64ux/A6zes1q2qSR8i2RY7KyHkjt9JJQuVkvlic5QDEAmV2Fs4meeV24hDt7 cWJHCNvhtvONtXHyeallauad+sGloIpdE4ixE1I6bw/mYQUGrgb0oc7uZVAl6ogHh9Jl mzV3+8bcntY2TTSsvkT262RZGuB6eqaDmvCfNMce5n7+1bpQPD8CYuyxOKInATdBVTDn PutQNjmP4fWFKarJgyoQxDq/4wG5oKAK1Fwwg2DNCclebpIfR8Y+qxA4Dek+zkHDYaiX d49uBokhHv+G+y5HJ8fgtBUTGMO3JT8ubFz9H+S9F9okpfMQ8Lno7CuPlSsW3W1KTcXX cdfw== X-Forwarded-Encrypted: i=1; AJvYcCWUmYfxWfUsxaAx0XMf1UE5mTSh7ojoQ4DZ2jRh7dMjoJ1N7t4/5y6Xp9whZ9TkNLea9iQ0lbiazgLuBEw=@vger.kernel.org X-Gm-Message-State: AOJu0Yz5RXSHE9eiFSK8Gs4hS1pRFHaHpwRYbpwMgNNVZQd1XLR6Uwnq sNlGl/d7YlS6BbgUKawEkEUIoBBqThlu/RH6yZEPZZ7VJcL54Hfk2kBwr1+/OA7CGydqlkRHgAX IukX39A== X-Google-Smtp-Source: AGHT+IFfuPhNF4QHWjfa9+uUgFQmIGeujJj5PfNmNyVQGzTqn6A2kUoJFJoVip3tZ9o2B4MRTJUIsR9I0NA= X-Received: from pjbrr6.prod.google.com ([2002:a17:90b:2b46:b0:30a:89a8:cef0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:e18b:b0:2fe:a515:4a98 with SMTP id 98e67ed59e1d1-30e8323ecdfmr32685566a91.31.1747962026242; Thu, 22 May 2025 18:00:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:14 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-10-seanjc@google.com> Subject: [PATCH v2 09/59] KVM: SVM: Drop vcpu_svm's pointless avic_backing_page field From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack 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. 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. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 6 ++---- arch/x86/kvm/svm/svm.h | 1 - 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index c36f7db9252e..ab228872a19b 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -236,7 +236,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) @@ -281,7 +281,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) (id > X2AVIC_MAX_PHYSICAL_ID)) return -EINVAL; =20 - if (!vcpu->arch.apic->regs) + if (WARN_ON_ONCE(!vcpu->arch.apic->regs)) return -EINVAL; =20 if (kvm_apicv_activated(vcpu->kvm)) { @@ -298,8 +298,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 cc27877d69ae..1585288200f4 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -306,7 +306,6 @@ struct vcpu_svm { =20 u32 ldr_reg; u32 dfr_reg; - struct page *avic_backing_page; u64 *avic_physical_id_cache; =20 /* --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82E7C223326 for ; Fri, 23 May 2025 01:00:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962030; cv=none; b=T0dwWQ2lefzFslHS1knFdHHxOqG+vWnTuNVMNV3kmXzfz82wWqX+4tYopygPQYuP0Syvpumqp6Yk41QUkW1q5EEcwRMQTfqBxPRU07EK/3OGpOEfrxUE8gzzDZwZbwbFWcDq+wBrVS6pP7UfUCH127qeMMh/tLoYvNYGwMwB5Ps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962030; c=relaxed/simple; bh=OynwmpZzE+tOR+c6k83RPGCMFRJPHQO+vuelylIgD9s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Waw0eV0yVtlexr5ooj4A6Fam66DVt/avRUwvdggMdJRGdKL3PA9jPTYktjQpdb8jrnvWnvPrfJm9clO06gqhXq044Ogrq7Cl92yVBuyGHVc2Xne83bv75pOxSTkcOpltGFiFW+br35eOJeaQDewUAdifj8YXuIO656OYhJUsdBA= 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=oYbF9q7k; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oYbF9q7k" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30eac9886ffso4644978a91.1 for ; Thu, 22 May 2025 18:00:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962028; x=1748566828; 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=la0aXzVeKKVBiy8uaO6FAWX8ZpsMttxn3OpIPvBkLxA=; b=oYbF9q7kbDqIdGDE5LmRDxh2iSJnlpGLe+8nje7170u12thsBHr5ocsBk4f8GsL3cL kvPl/c/2A/cQG5F0mLLhgE3WE/yQNlQ3PUkXpwUeXvzBHF+bG9F0w6U0f1HTZYJ2k0e9 e3NXlTp/FPjlUgDN44sdel+i9dSgevSOlbzGCBUteX9apD/Ql8j/lYWqdcZxJY9Zo9ui PcJryJgKQx11jurQ2LP5EQ8btsZOVC77TashGOGBvzmFKfeksrqOlXRA2jjqatge4H1/ AucJSl7ljrkwayoee3UHeW5zG7bbb7ah+RIgEZlH1LIL+yAXnsvIeR+nrk3gp1VQdCw4 z5vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962028; x=1748566828; 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=la0aXzVeKKVBiy8uaO6FAWX8ZpsMttxn3OpIPvBkLxA=; b=KrzJ6yoWoNuOWlg7zr2QDyZclNoPeDW+jgZVrB4dS8TFmQXjy2m7Qv31DUnJhWDVRF NKBi/jrTUep+8efrcJ1meBm9e3cBI7iiEoj72sVG6AYsWNWtDushZw577tePhZbxkON7 LJ6rnjMaU8b6yMJDAkNJpPUbOjkYl+5aCP7YyEM0+n/e/oHSXkfiU0OwzWDD5PoF1Z6t YzfFu7tn+QmQGVYWKLLyYfFF1LwACOmvtL7GT3Is92EFNFoyeIrXQdFEwMtVCT5Az5L/ rdWfdxhAPU7q4ygVBC3klcdiiAvXB6dkHTDO++WwArz0DOkJUHwh7bhjF/55g4t2npzY w2dQ== X-Forwarded-Encrypted: i=1; AJvYcCWTemiYnSVSsVY71OKvt69WPxWXn4imoMqaj8J46r0ZOyWXBoYCdu3SfYUHIDWC191EwIVXraQQ8uLiMho=@vger.kernel.org X-Gm-Message-State: AOJu0Yx+EyIYOfgVLpmqRxlxHE4laGqqNkqhuKq/8rYTKn30URkHO9iA cLCubHCApDUmeeTh8VAAnXKz7C7Nh+4CamQCyY5NTF4XGcRP4XHtowVsZfoinX1Ts78Liq5eEE5 QBJXjDg== X-Google-Smtp-Source: AGHT+IFB3ajdgTxrTXVysFpFaHJbw/mIl1eAI1Y8d57Q56yfcB9pR6oBP4Leg9XUSb1oftSrh5d450so4NU= X-Received: from pjbpl16.prod.google.com ([2002:a17:90b:2690:b0:30e:7003:7604]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d44e:b0:2ee:d63f:d8f with SMTP id 98e67ed59e1d1-310e96c9598mr1763628a91.13.1747962028063; Thu, 22 May 2025 18:00:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:15 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-11-seanjc@google.com> Subject: [PATCH v2 10/59] KVM: SVM: Inhibit AVIC if ID is too big instead of rejecting vCPU creation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack 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 Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 9 ++++++++- arch/x86/kvm/svm/avic.c | 14 ++++++++++++-- arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 2a6ef1398da7..a9b709db7c59 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1314,6 +1314,12 @@ enum kvm_apicv_inhibit { */ APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED, =20 + /* + * 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_PHYSICAL_ID_TOO_BIG, + NR_APICV_INHIBIT_REASONS, }; =20 @@ -1332,7 +1338,8 @@ enum kvm_apicv_inhibit { __APICV_INHIBIT_REASON(IRQWIN), \ __APICV_INHIBIT_REASON(PIT_REINJ), \ __APICV_INHIBIT_REASON(SEV), \ - __APICV_INHIBIT_REASON(LOGICAL_ID_ALIASED) + __APICV_INHIBIT_REASON(LOGICAL_ID_ALIASED), \ + __APICV_INHIBIT_REASON(PHYSICAL_ID_TOO_BIG) =20 struct kvm_arch { unsigned long n_used_mmu_pages; diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index ab228872a19b..f0a74b102c57 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -277,9 +277,19 @@ 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. Immediately clear apicv_active, i.e. don't wait until the + * KVM_REQ_APICV_UPDATE request is processed on the first KVM_RUN, as + * avic_vcpu_load() expects to be called if and only if the vCPU has + * fully initialized AVIC. + */ 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_PHYSICAL_ID_TOO_BI= G); + vcpu->arch.apic->apicv_active =3D false; + return 0; + } =20 if (WARN_ON_ONCE(!vcpu->arch.apic->regs)) return -EINVAL; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 1585288200f4..71e3c003580e 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -722,7 +722,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_PHYSICAL_ID_TOO_BIG) \ ) =20 bool avic_hardware_setup(void); --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 51C0F22422B for ; Fri, 23 May 2025 01:00:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962031; cv=none; b=W9YiUN0lC4Jmp4Ah806jd95Hd9l6KStOKGbD2aDng2JJfYy1z6BIOQAWleQi5C32lmakkYpDEAczA9JS544X22/q+61LQkXDCJxYeXaHEoNbFK2KxT3yUV4k0dyIETSZvfeRAfvtqUQW6WI0Z4DdBiBy+vjoqtDUw9fWZRTzmZc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962031; c=relaxed/simple; bh=NmtIJk+NXuVQcgcHe/OX20tZuAMC4d7N9CttC/YW7ug=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nmBTlbgl42rsA+78H5Ac2WYH5WezIA7FL57Uxd+7cLWyw8Ph2jpFGvyUCax6dpGGqRz75EO1ZGl76QlW77Aak0sR7Leqh8Eo+6jp/v6QCafgKuM9gHi/DQYJrFLhSYV2NT90dBA8jKD/k0L9d/D7xUH+YhvOQsFi5Popqwyhzmg= 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=Q/8ZFsMN; arc=none smtp.client-ip=209.85.210.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="Q/8ZFsMN" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7391d68617cso316896b3a.0 for ; Thu, 22 May 2025 18:00:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962030; x=1748566830; 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=qvU0dOld0vUayuYrP2inmYflxGewHdZ05MuEuDFSsRI=; b=Q/8ZFsMNQTmVGG73IxrE34EtnSFIQRVbxmpIZ90x8N9JR3OkgegC8ckYi2BtmNeGgq 2A2RpW0S+5pfuBstUgea5C67M8UEg6Yg9ix/tGXkaf4mwN3j9iyImJK00Z+2txRc1tna +/EUYUhBGYijPgVX6Y2nO0mW/N/YjZWf/wY9Le7AbYNHxD2GcMNKdDdwpmKgISFO1cWw NT82/ngvN/JTzvFNjjE8jZsKNRomLbOB2vneBNK+C7FaR3ogjn/jjt367H1Hc4P3vIf6 7yziZ0wT8OMsHOJRm8srPSwnAOU1cqLYqIExNKS/gKADaOVkKFMKM5aclpXYu6mtF7SK j68Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962030; x=1748566830; 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=qvU0dOld0vUayuYrP2inmYflxGewHdZ05MuEuDFSsRI=; b=OLNJGQRaFFrqhTKsNeijqw0JNRV1ej2sgdbgo5dI4HZhuw6N3yRaV1JOnBPNs7BJik si17J4sJGAS7w7+eIWnEl195APgZDHh5uNriGSPxcdbWhtil+kg6zK21sm1byJngC+Lj RQjQO1peBDKfyipmZVB2E9tsE48x5G6ylnrxITdf2fImUKtdk9DUhDRhiT52kfnznfuy iS/KdfG3PBqAINeIGKdnXipRYRbQJiJR1LrOroZefXdxD+InTH9UInmc0ylCA6SQCiWL iw6pwX7d8E02C9+NShv3RfwxJtdDAdNDTjjEfpRkp1C2Oz7RRNupO0P0Zbsv0gletIZc QzQg== X-Forwarded-Encrypted: i=1; AJvYcCVJjiurhFQNRcPxBl2bW4F54MUW7T5FihTxDsrUKMjuZNl2BVy0yUJ3+jHXbZlR+D7qsPLAyjPRyRKf174=@vger.kernel.org X-Gm-Message-State: AOJu0Yx9kaRjORLJfmE+nPuQOYhRe9aczxa2/LImb6rPDBREQraGtOTE l8IUdNPtOYRPmQfYTND4pIcRraOOgh2VbF0zOp82xTwAggDFLuypDVDGJnPeFSEZa14yEG6XhIh znEGc4g== X-Google-Smtp-Source: AGHT+IHUAkRGBcbeHEZC8d5MhipzP08zCSRQ2GxGmkCxhyIuuWUqgFNrdbfXKGFZCAm/KodkxoGK3IgwYXs= X-Received: from pgah21.prod.google.com ([2002:a05:6a02:4e95:b0:b26:f984:15bc]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:78e:b0:1f0:e6db:b382 with SMTP id adf61e73a8af0-21877a3a205mr2221344637.8.1747962029662; Thu, 22 May 2025 18:00:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:16 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-12-seanjc@google.com> Subject: [PATCH v2 11/59] KVM: SVM: Drop redundant check in AVIC code on ID during vCPU creation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack 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. Opportunistically add a compile-time assertion to ensure the vcpu_id can't result in a bounds overflow, e.g. if KVM (really) messed up a maximum physical ID #define, as well as run-time assertions so that a NULL pointer dereference is morphed into a safer WARN(). No functional change intended. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index f0a74b102c57..948bab48083b 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -256,26 +256,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; - int id =3D vcpu->vcpu_id; + struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); struct vcpu_svm *svm =3D to_svm(vcpu); + u32 id =3D vcpu->vcpu_id; + u64 *table, new_entry; =20 /* * Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC @@ -291,6 +277,9 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) return 0; } =20 + BUILD_BUG_ON((AVIC_MAX_PHYSICAL_ID + 1) * sizeof(*table) > PAGE_SIZE || + (X2AVIC_MAX_PHYSICAL_ID + 1) * sizeof(*table) > PAGE_SIZE); + if (WARN_ON_ONCE(!vcpu->arch.apic->regs)) return -EINVAL; =20 @@ -309,9 +298,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) } =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 /* Note, fls64() returns the bit position, +1. */ BUILD_BUG_ON(__PHYSICAL_MASK_SHIFT > @@ -319,9 +306,9 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) =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; } @@ -1004,6 +991,9 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) if (WARN_ON(h_physical_id & ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK= )) return; =20 + if (WARN_ON_ONCE(!svm->avic_physical_id_cache)) + return; + /* * No need to update anything if the vCPU is blocking, i.e. if the vCPU * is being scheduled in after being preempted. The CPU entries in the @@ -1044,6 +1034,9 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) =20 lockdep_assert_preemption_disabled(); =20 + if (WARN_ON_ONCE(!svm->avic_physical_id_cache)) + return; + /* * Note, reading the Physical ID entry outside of ir_list_lock is safe * as only the pCPU that has loaded (or is loading) the vCPU is allowed --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 310A72080F4 for ; Fri, 23 May 2025 01:00:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962033; cv=none; b=un2K9Fk4npaFhUAUlMJqXBpKI1iYd5N53GaEVgmRfb7znVud1Eyxivy7IrgGeOYTftp1vIi1MW73kA2PrkXGjr5u1/m/fbqnTSE9hu3K8Eusm8KN7G+9e7ZmnxJeO0uBU/q/dMQhz4iN474OoCAPGwBJL0w87wTunXKbEC3VARw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962033; c=relaxed/simple; bh=1u7yaWCSV8swor7EDb2Vzxng28EOGOs32xp2c0ARZ8M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EHKRBsFbJ66DSI1vwgiyW+LspWV0xoFV1cKs2ZyqvirBJs5KhX3AgcRQiHe82muTZ2NT4amVkZQUbbtkxxpN0IDznuruCW8I+a8uninGtelXEG9lF+DtzdqII9dTp9EI7jecXIX9qvD8ItwAb8kEF848Xhfr/6cYU++IelCqzUo= 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=14abEIiv; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="14abEIiv" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30eac9886ffso4645058a91.1 for ; Thu, 22 May 2025 18:00:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962031; x=1748566831; 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=TWVWI0HB+j/yivqbFmMmYR1x9Y24z6jTPvGpz798n6o=; b=14abEIiv5opjbAde1BUd+QC3oZpNYvnuC8Ndzaq+7jIlDPbuWIrcuNvzZkrX6GFiih oNaN3+zg49OPGCxjVYekwPbdF0pMNNYE7IL81PerEJ4Y6k4MvM6bg1Wu8fois4aXjQZb kAl5Po0CWtLoYGCec6YQ6KTf7SHyOLAPU97Oxr1EHSNOMgKYlxpSBTS3XGhVQZ4qVr+F qGSR6XvxurB+zY5MDRpkLTjhaxQp9Xr3+jX+JkkCf9n5NQMJTwDsO7WCuB86McJ2izn5 MBBkDS3JUgT04tfQBvaVYF13ZW72Bh7W/Y4FD7KjW3fOldmc33LOy9mvxMcFipUjhde6 MWow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962031; x=1748566831; 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=TWVWI0HB+j/yivqbFmMmYR1x9Y24z6jTPvGpz798n6o=; b=CGunW2NyTpTiDVCrY87AbEWwOk9rAdRW0mLSTG/dIYNIQ1FJQO0FcJ2h19y2E4itjz nlp7TMzp8LxjcsiJaDYHg3pweDIfoxmz1wXseUrr7IBMtxR7d1ZEpa2bwx8iZXRV4QlC MTiLyG5UIosTxEdnpCGc2Vh5pgjLEaANEfLiE/xKNw7ulY7CRUHviBe8iUddbfgIEcT7 ypNjcOgFIukouDrQ/r7gl1AIDmkdNDqf5jZY6sx799ZY3JUc/sbOVYKxPv7zNglCoGXT 4QtgJ3cc3fazwV20npqIvY2OoheVwjFA7CeFGyS3IHx8AQkza+Mg13oEZkawKy5GUCN2 Cg+A== X-Forwarded-Encrypted: i=1; AJvYcCXj+PNXGq3nqq1gSv9wayM8UlOH/Thu5i2GmZYu7PRJeWHwhO06Pt4AwCqs4p4HaqZBAsN+UYCy4jEb/7g=@vger.kernel.org X-Gm-Message-State: AOJu0YyC57GkGF9Id6xH6nPwlPuKqOmoa/u5CN5SjusPcYzprNbVd60/ h54q0cnqWyYWSm7ED5dgh/M1Yca9C9GsSTigwLwZOU9w3QKm0ztuALwxv4440GnOtw/nLsXL2pS iDvi0Sw== X-Google-Smtp-Source: AGHT+IFAvtZwrOPujc8Jiezt5ZUqdB9goJmViesuN7rSLHyglpb7Gd0+oRTXrUjZLjFNcb4QeIdzixEWwiw= X-Received: from pjf14.prod.google.com ([2002:a17:90b:3f0e:b0:2fc:d77:541]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1645:b0:2ee:94d1:7a89 with SMTP id 98e67ed59e1d1-310e96b6096mr1702204a91.1.1747962031141; Thu, 22 May 2025 18:00:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:17 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-13-seanjc@google.com> Subject: [PATCH v2 12/59] KVM: SVM: Track AVIC tables as natively sized pointers, not "struct pages" From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allocate and track AVIC's logical and physical tables as u32 and u64 pointers respectively, as managing the pages as "struct page" pointers adds an almost absurd amount of boilerplate and complexity. E.g. with page_address() out of the way, svm->avic_physical_id_cache becomes completely superfluous, and will be removed in a future cleanup. No functional change intended. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 49 ++++++++++++++--------------------------- arch/x86/kvm/svm/svm.h | 4 ++-- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 948bab48083b..bf18b0b643d9 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -172,10 +172,8 @@ void avic_vm_destroy(struct kvm *kvm) if (!enable_apicv) return; =20 - if (kvm_svm->avic_logical_id_table_page) - __free_page(kvm_svm->avic_logical_id_table_page); - if (kvm_svm->avic_physical_id_table_page) - __free_page(kvm_svm->avic_physical_id_table_page); + free_page((unsigned long)kvm_svm->avic_logical_id_table); + free_page((unsigned long)kvm_svm->avic_physical_id_table); =20 spin_lock_irqsave(&svm_vm_data_hash_lock, flags); hash_del(&kvm_svm->hnode); @@ -188,27 +186,19 @@ int avic_vm_init(struct kvm *kvm) int err =3D -ENOMEM; struct kvm_svm *kvm_svm =3D to_kvm_svm(kvm); struct kvm_svm *k2; - struct page *p_page; - struct page *l_page; u32 vm_id; =20 if (!enable_apicv) return 0; =20 - /* Allocating physical APIC ID table (4KB) */ - p_page =3D alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); - if (!p_page) + kvm_svm->avic_physical_id_table =3D (void *)get_zeroed_page(GFP_KERNEL_AC= COUNT); + if (!kvm_svm->avic_physical_id_table) goto free_avic; =20 - kvm_svm->avic_physical_id_table_page =3D p_page; - - /* Allocating logical APIC ID table (4KB) */ - l_page =3D alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); - if (!l_page) + kvm_svm->avic_logical_id_table =3D (void *)get_zeroed_page(GFP_KERNEL_ACC= OUNT); + if (!kvm_svm->avic_logical_id_table) goto free_avic; =20 - kvm_svm->avic_logical_id_table_page =3D l_page; - spin_lock_irqsave(&svm_vm_data_hash_lock, flags); again: vm_id =3D next_vm_id =3D (next_vm_id + 1) & AVIC_VM_ID_MASK; @@ -242,12 +232,10 @@ static phys_addr_t avic_get_backing_page_address(stru= ct vcpu_svm *svm) 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 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 avic_get_backing_page_address(svm); - vmcb->control.avic_logical_id =3D lpa; - vmcb->control.avic_physical_id =3D ppa; + vmcb->control.avic_logical_id =3D __sme_set(__pa(kvm_svm->avic_logical_id= _table)); + vmcb->control.avic_physical_id =3D __sme_set(__pa(kvm_svm->avic_physical_= id_table)); vmcb->control.avic_vapic_bar =3D APIC_DEFAULT_PHYS_BASE; =20 if (kvm_apicv_activated(svm->vcpu.kvm)) @@ -261,7 +249,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); struct vcpu_svm *svm =3D to_svm(vcpu); u32 id =3D vcpu->vcpu_id; - u64 *table, new_entry; + u64 new_entry; =20 /* * Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC @@ -277,8 +265,8 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) return 0; } =20 - BUILD_BUG_ON((AVIC_MAX_PHYSICAL_ID + 1) * sizeof(*table) > PAGE_SIZE || - (X2AVIC_MAX_PHYSICAL_ID + 1) * sizeof(*table) > PAGE_SIZE); + BUILD_BUG_ON((AVIC_MAX_PHYSICAL_ID + 1) * sizeof(new_entry) > PAGE_SIZE || + (X2AVIC_MAX_PHYSICAL_ID + 1) * sizeof(new_entry) > PAGE_SIZE); =20 if (WARN_ON_ONCE(!vcpu->arch.apic->regs)) return -EINVAL; @@ -297,18 +285,16 @@ static int avic_init_backing_page(struct kvm_vcpu *vc= pu) return ret; } =20 - /* Setting AVIC backing page address in the phy APIC ID table */ - table =3D page_address(kvm_svm->avic_physical_id_table_page); - /* Note, fls64() returns the bit position, +1. */ BUILD_BUG_ON(__PHYSICAL_MASK_SHIFT > fls64(AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK)); =20 + /* Setting AVIC backing page address in the phy APIC ID table */ new_entry =3D avic_get_backing_page_address(svm) | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; - WRITE_ONCE(table[id], new_entry); + WRITE_ONCE(kvm_svm->avic_physical_id_table[id], new_entry); =20 - svm->avic_physical_id_cache =3D &table[id]; + svm->avic_physical_id_cache =3D &kvm_svm->avic_physical_id_table[id]; =20 return 0; } @@ -442,7 +428,7 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm,= struct kvm_lapic *source if (apic_x2apic_mode(source)) avic_logical_id_table =3D NULL; else - avic_logical_id_table =3D page_address(kvm_svm->avic_logical_id_table_p= age); + avic_logical_id_table =3D kvm_svm->avic_logical_id_table; =20 /* * AVIC is inhibited if vCPUs aren't mapped 1:1 with logical @@ -544,7 +530,6 @@ unsigned long avic_vcpu_get_apicv_inhibit_reasons(struc= t kvm_vcpu *vcpu) static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool= flat) { struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); - u32 *logical_apic_id_table; u32 cluster, index; =20 ldr =3D GET_APIC_LOGICAL_ID(ldr); @@ -565,9 +550,7 @@ static u32 *avic_get_logical_id_entry(struct kvm_vcpu *= vcpu, u32 ldr, bool flat) return NULL; index +=3D (cluster << 2); =20 - logical_apic_id_table =3D (u32 *) page_address(kvm_svm->avic_logical_id_t= able_page); - - return &logical_apic_id_table[index]; + return &kvm_svm->avic_logical_id_table[index]; } =20 static void avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ld= r) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 71e3c003580e..ec5d77d42a49 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -123,8 +123,8 @@ struct kvm_svm { =20 /* Struct members for AVIC */ u32 avic_vm_id; - struct page *avic_logical_id_table_page; - struct page *avic_physical_id_table_page; + u32 *avic_logical_id_table; + u64 *avic_physical_id_table; struct hlist_node hnode; =20 struct kvm_sev_info sev_info; --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 B0254226CF7 for ; Fri, 23 May 2025 01:00:33 +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=1747962035; cv=none; b=TA40fsrrIJACFo6togmP0wmJxyF1AqKo+fTrCIZKy4HnzASjxaHinI958Y0odEdbgqbucCXNSpfz1gbz8xgkDAIA8J51Qd9ATbEvE/kNfxpPr2VPn+P1aRJtJesUIpYUC1Q7ShcYPn7NyKCrnjOytwOb238yePWfePzjbgkBLpQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962035; c=relaxed/simple; bh=CI55+D0kEm5O6+xiEBD1DyHjTu/geN9Q80IyZ5PbyXI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rrB3aWgJQhX+ynG50oyw8A3YKQvnouuSSqdhf4EYMpuC3KLGk85AV9Ee5B8eBAuh2UdnY5sd0Nh+fwtP7rFNTcdvKLecodBMmnmrohxN5nFx/kWcPoFPeGq11dadNdIr24to0YjIaN9krG19ZHLgvQ9RdQIB5nl8VkyIMqVT5CY= 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=LZRhpwBt; 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="LZRhpwBt" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b2c0cbf9fddso405277a12.3 for ; Thu, 22 May 2025 18:00:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962033; x=1748566833; 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=mgom0My0RXyxt3zGYxIwZj5mPyM4HyH/qZA3LIBxvLE=; b=LZRhpwBtd+KpkGdfkoo7EfQXrgfOkZ4Fm8rndgIRCxqmmMicsggfA1cXkx8nlw4yVm 0zx1PKhO1WFqtXsz2awdDlXLc/+DQP/aXaD9oEw6rKOw01682VXWzRzFRi9zFDRVOCOX PffKDCwuokP25ihvdKvZVoNIuPbAX++3IYS4JRp6LrbTgGBcObPQYJNy+Q9rezU2iiWJ AzpPOg34LVophMVxGej9mH+pLbmRUBZZIa+54RgVf15n0dz2L3OyUxQgIrXovpDP8s31 YAqFoEASbIFfI9sCJIjzfCKH0dC48jFnsILPp0vCtY6SAtgipghuHlhKLix3QbcC/6G3 0HbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962033; x=1748566833; 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=mgom0My0RXyxt3zGYxIwZj5mPyM4HyH/qZA3LIBxvLE=; b=jIVwbpW/LYOEcdYMXl4xN+YK6rVlhRCoPbF/gqMpCEXCaw+xni8CLkzyaR5va714UJ jFWVkQxjpsFw3t4wFwNp8OGs1o713zcChhm6rWQ2pes6wglT+ulfkQ4QuiJ+hxPEFk78 VAWd7Kp9IJacA93/IJ/jGMh6DjSPmMBcVmZQAYBktR/mD54vOcbfEMaQDanmPZQ1J7Tj 9d9jU+QTtpqYOLY1OfYYmDWOSglBH4B47JenJuksKr2DFfCZNJ0WXufrfj8ae8cQNFt1 Gu+dRQZykbHXgB3G7S/ogSTZ01pyMWqheJz6l04s/3ET/QyggRjgJa9e4zoy65lCgfuW aLhw== X-Forwarded-Encrypted: i=1; AJvYcCXWkeFblCwNpDIVX/z28ra7kx3kw/cHBqotE43sWpku6VWVs/D2YoSRDP47AxKriK5PnlHsclwGk1GZaDo=@vger.kernel.org X-Gm-Message-State: AOJu0Yz94UBzu9b6tC3yPOUrMy1J7iAnxCSCAgmhbwmxnZdZ1YKHchyA xmFS9yj+AGxF/y0eIhIyx+qG9S45Fps88/Vzr61/wLNbEO9zdxQDZc3CayMTTXxmMjAFY60teHZ NaeKVyg== X-Google-Smtp-Source: AGHT+IGxjUT6ZTFdn1fL46e7uP64GFmrOiUImY5wsG9IiTzQ4jbxEpbauT01oNGyrrPeVaV1B6nroWQExuI= X-Received: from pjuw7.prod.google.com ([2002:a17:90a:d607:b0:308:867e:1ced]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3c90:b0:30a:a50e:349c with SMTP id 98e67ed59e1d1-30e83226f25mr38441643a91.30.1747962033065; Thu, 22 May 2025 18:00:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:18 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-14-seanjc@google.com> Subject: [PATCH v2 13/59] KVM: SVM: Drop superfluous "cache" of AVIC Physical ID entry pointer From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop the vCPU's pointer to its AVIC Physical ID entry, and simply index the table directly. Caching a pointer address is completely unnecessary for performance, and 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. Suggested-by: Maxim Levitsky Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 27 +++++++++++++++------------ arch/x86/kvm/svm/svm.h | 1 - 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index bf18b0b643d9..0c0be274d29e 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -294,8 +294,6 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; WRITE_ONCE(kvm_svm->avic_physical_id_table[id], new_entry); =20 - svm->avic_physical_id_cache =3D &kvm_svm->avic_physical_id_table[id]; - return 0; } =20 @@ -770,13 +768,16 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struct kvm_kernel_irqfd *irqfd, struct amd_iommu_pi_data *pi) { + struct kvm_vcpu *vcpu =3D &svm->vcpu; + struct kvm *kvm =3D vcpu->kvm; + struct kvm_svm *kvm_svm =3D to_kvm_svm(kvm); unsigned long flags; u64 entry; =20 if (WARN_ON_ONCE(!pi->ir_data)) return -EINVAL; =20 - irqfd->irq_bypass_vcpu =3D &svm->vcpu; + irqfd->irq_bypass_vcpu =3D vcpu; irqfd->irq_bypass_data =3D pi->ir_data; =20 spin_lock_irqsave(&svm->ir_list_lock, flags); @@ -787,7 +788,7 @@ static int svm_ir_list_add(struct vcpu_svm *svm, * will update the pCPU info when the vCPU awkened and/or scheduled in. * See also avic_vcpu_load(). */ - entry =3D READ_ONCE(*(svm->avic_physical_id_cache)); + entry =3D READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]); if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK, true, pi->ir_data); @@ -964,17 +965,18 @@ avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu= , int cpu, bool r) =20 void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { - u64 entry; + struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); int h_physical_id =3D kvm_cpu_get_apicid(cpu); struct vcpu_svm *svm =3D to_svm(vcpu); unsigned long flags; + u64 entry; =20 lockdep_assert_preemption_disabled(); =20 if (WARN_ON(h_physical_id & ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK= )) return; =20 - if (WARN_ON_ONCE(!svm->avic_physical_id_cache)) + if (WARN_ON_ONCE(vcpu->vcpu_id * sizeof(entry) >=3D PAGE_SIZE)) return; =20 /* @@ -996,14 +998,14 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) */ spin_lock_irqsave(&svm->ir_list_lock, flags); =20 - entry =3D READ_ONCE(*(svm->avic_physical_id_cache)); + entry =3D READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]); 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(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, true); =20 spin_unlock_irqrestore(&svm->ir_list_lock, flags); @@ -1011,13 +1013,14 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) =20 void avic_vcpu_put(struct kvm_vcpu *vcpu) { - u64 entry; + struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); struct vcpu_svm *svm =3D to_svm(vcpu); unsigned long flags; + u64 entry; =20 lockdep_assert_preemption_disabled(); =20 - if (WARN_ON_ONCE(!svm->avic_physical_id_cache)) + if (WARN_ON_ONCE(vcpu->vcpu_id * sizeof(entry) >=3D PAGE_SIZE)) return; =20 /* @@ -1027,7 +1030,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) * can't be scheduled out and thus avic_vcpu_{put,load}() can't run * recursively. */ - entry =3D READ_ONCE(*(svm->avic_physical_id_cache)); + entry =3D READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]); =20 /* Nothing to do if IsRunning =3D=3D '0' due to vCPU blocking. */ if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) @@ -1046,7 +1049,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(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); =20 spin_unlock_irqrestore(&svm->ir_list_lock, flags); =20 diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index ec5d77d42a49..f225d0bed152 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -306,7 +306,6 @@ struct vcpu_svm { =20 u32 ldr_reg; u32 dfr_reg; - u64 *avic_physical_id_cache; =20 /* * Per-vCPU list of irqfds that are eligible to post IRQs directly to --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2BFAF22B598 for ; Fri, 23 May 2025 01:00:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962036; cv=none; b=jtpeERRVI+fc7WCo2QAaAbEECM7zuKNlBicQAZuGys0Trzb2bJ6dq9xidkY4jiYUYNXXUEzUq7CNbJiPsV/kBvlI5f/p51nXRm9q8OMuiczORSmhQ8ACvyfEVSQwoY88Hxw4KTsde0+YqKR6Ysqju9Wr5EuHcUFgY6xL4YFlQmQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962036; c=relaxed/simple; bh=dlp1owjlTB9Q2ZNdky6CeTiZTUsuxD6LWgF2OyRxR9k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eySCka+/AmtmL/stmp4pr1yX6ciEULSxQbpAy1NjQneCo2TMjwlhhpTQurGc6lzDuOsLuwjV2KEZBrJznpMd+qg2ba/BdLid71mn+i8qotWlfsohhbM5e2K8qfttFH63fTAYUpLuXzZ6lDhUEzPNH1wYUob5YcEea4C4BqLzjNw= 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=up1wemXs; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="up1wemXs" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30e8346b49cso7253355a91.0 for ; Thu, 22 May 2025 18:00:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962035; x=1748566835; 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=8hYW+M1uoR4gCkeYUzndz4aHuf/uvwQq+NXGlsnTPHE=; b=up1wemXssdVa41ki89XeT1Y+EkDpINGXeUNpQh/0fNDYt1Aoy2F4cE+ECOEWpRX2aN eqcfJelZyTGeQDmECmfkE4ne6/fYAnJ36ql9NMtbPenxaEpiTaMWmDfZsigNM1qBFIyo 5cNZmQXEkNNYd3lOZMLmDR3hkto3y44U0ngqOjDIywrDwuYqZlRcaAIvP1gpAPGjidgE TMoghop0gOLOcRBsXuOvXZnwMHWdi7CTKcjFc75PNL2aO6FBBdrLRTwGnDeN9cvTqoM+ XhizYwCh9SmVaUNthb7N68OibPb82Em5wTPCWbzETb+BnwafFLkPX9FXHt4XJFl54CkX DskA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962035; x=1748566835; 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=8hYW+M1uoR4gCkeYUzndz4aHuf/uvwQq+NXGlsnTPHE=; b=AOCbDZK22kedIHE+c4EN3g/VWL4GBKuXVLkuympS+vCfIPSyG0Xz0mhMfY/iws+qab +ojWvCGnaNjBl9DG3VRlTtc6WmnUlCoYJccQytCwlEUatSvfp+smHYixF+DjXFk/IT1g SwTV9nRybASwEIDSqTigza9HI4+2UHks9c3JgGBbtZHDdEYfIjFE518fRczrqMOtkpLx vG66UYTq4yotO1Psy8li5Dy46ESg8HzA9UiLyH2XJcD7LJ62kwo36LGgIoHKlnKDtMD3 2kOWfoUfFRgVRdRQSIiGArfddUr0Uj0Er0G83iTpbajXUkIWuHpcOVvVUvGDtitVfO4j e74w== X-Forwarded-Encrypted: i=1; AJvYcCUaYw/pHZx6t27bvPv1sMUbLE14KvsgLoNH+sYcIFvIGM9tiz9w3Hz0X6wZ2Pu/51REeBhhYDeMFDLzLWg=@vger.kernel.org X-Gm-Message-State: AOJu0YzyEiuDdGHDki0ASQWkUp54tCwmvI2Qs+4f3dPLBFddIPmqcIxU vi3+8ZxOTWtq5w4ooGPEu/Ib15Ix07eVK621HIjwRpWyod5uuWqSl/bXzbDU+p7Iw78KLXWA4wf kuxvC5Q== X-Google-Smtp-Source: AGHT+IERiQoZPoEemihmVi9aR21C5+4ZWBIzokk0EnGUgkUK/kwxgXf2E6CpkLPTP/gednSgPST54I+bLEA= X-Received: from pji7.prod.google.com ([2002:a17:90b:3fc7:b0:2fa:a30a:3382]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:a015:b0:30e:ae5:479d with SMTP id 98e67ed59e1d1-30e83216f63mr30402479a91.27.1747962034725; Thu, 22 May 2025 18:00:34 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:19 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-15-seanjc@google.com> Subject: [PATCH v2 14/59] KVM: VMX: Move enable_ipiv knob to common x86 From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move enable_ipiv to common x86 so that it can be reused by SVM to control IPI virtualization when AVIC is enabled. SVM doesn't actually provide a way to truly disable IPI virtualization, but KVM can get close enough by skipping the necessary table programming. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/capabilities.h | 1 - arch/x86/kvm/vmx/vmx.c | 2 -- arch/x86/kvm/x86.c | 3 +++ 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index a9b709db7c59..cba82d7a701d 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1950,6 +1950,7 @@ struct kvm_arch_async_pf { extern u32 __read_mostly kvm_nr_uret_msrs; extern bool __read_mostly allow_smaller_maxphyaddr; extern bool __read_mostly enable_apicv; +extern bool __read_mostly enable_ipiv; extern bool __read_mostly enable_device_posted_irqs; extern struct kvm_x86_ops kvm_x86_ops; =20 diff --git a/arch/x86/kvm/vmx/capabilities.h b/arch/x86/kvm/vmx/capabilitie= s.h index cb6588238f46..5316c27f6099 100644 --- a/arch/x86/kvm/vmx/capabilities.h +++ b/arch/x86/kvm/vmx/capabilities.h @@ -15,7 +15,6 @@ extern bool __read_mostly enable_ept; extern bool __read_mostly enable_unrestricted_guest; extern bool __read_mostly enable_ept_ad_bits; extern bool __read_mostly enable_pml; -extern bool __read_mostly enable_ipiv; extern int __read_mostly pt_mode; =20 #define PT_MODE_SYSTEM 0 diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 9ff00ae9f05a..f79604bc0127 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -112,8 +112,6 @@ static bool __read_mostly fasteoi =3D 1; module_param(fasteoi, bool, 0444); =20 module_param(enable_apicv, bool, 0444); - -bool __read_mostly enable_ipiv =3D true; module_param(enable_ipiv, bool, 0444); =20 module_param(enable_device_posted_irqs, bool, 0444); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 8a4662bc2521..b645ccda0999 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -226,6 +226,9 @@ EXPORT_SYMBOL_GPL(allow_smaller_maxphyaddr); bool __read_mostly enable_apicv =3D true; EXPORT_SYMBOL_GPL(enable_apicv); =20 +bool __read_mostly enable_ipiv =3D true; +EXPORT_SYMBOL_GPL(enable_ipiv); + bool __read_mostly enable_device_posted_irqs =3D true; EXPORT_SYMBOL_GPL(enable_device_posted_irqs); =20 --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 276A825178E for ; Fri, 23 May 2025 01:00:37 +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=1747962038; cv=none; b=fJgE5KyMRNkyrmLc8AXL2GLMkf5MNoYCEXqzFgPZ7nwdc2EshgxFr5DjdADB3szoc5PvGlY14pCLRQWm5DhrmXaPJTqpOEfggsszt3eb35qvZCN0NRj1EVlgQGIFxf+RPDtvM24nGPws7SLrGTwiLKxT5zWip+ETLT8BS4c4m40= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962038; c=relaxed/simple; bh=RZFsW6qFlraESQBEs5rfLtFloxBZh5YHOEXcImlGM/o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rDPcHh8qr1ZWs2+V9j4OKzWLuApkX01wlvIFlww3Wq9ltycrtOeD13iPuKwAhOT4GHkbsBlu9XiBDTQTyA1n+3M+s8ouaqhlTy8fck4P27sWyFuZP5Mic7Zq3ahyL0wRkzBWDIBhe3lMecQ+F4XGsXtEhoSTVkpX4KHXS7qqdsE= 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=y1lRMV0w; 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="y1lRMV0w" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b1ffc678adfso5532446a12.0 for ; Thu, 22 May 2025 18:00:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962036; x=1748566836; 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=v7+beRxhk1Z6lswHDIGdsgi9gbmmpiHmcppYR2/XJao=; b=y1lRMV0wIdofWmN43Giu8RQVwV/vFxmWxDeqHDeco7XWiWtJVwtuAH59BFrXLtv5TI EcjeoslE/HLAsorriSHG6DiMxSDHkQcsirub944teYBtI06YswbOmFo70mFAfuolQd6w wi0/t3Er5N1wmEkVBmm6cPNeE1rHqZ7avVVOOCIYpGi2dHr0M6gtlqm0gf+wfUiF+COo M1I/8iTFiQWCmHwlaNv/ZqZarZv0XNKA0SwSLED0mYtAIE6+m2cMB9GMXerx1DdJOTie Vz/oRoSzOsyNGQjPbFqg6ejZ9s8a/JE8CzERScgAvoEjiXxw1fWHzO2oILbfzc3+lGqt HHeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962036; x=1748566836; 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=v7+beRxhk1Z6lswHDIGdsgi9gbmmpiHmcppYR2/XJao=; b=YRiEIADIJU4GHbW+qFQZ9BLW61ebz7iq+IMPGZLErGNqc2AC3KCjJXsjj40A/6qqpx wyFukrhAqfcSht02mHyXiSYdm35TpUdyIGMiDVFxY/rBUBMkvscK6hg9+jtt8XaxjBqc 6lX6mfdrGMHd1rcS4ClwdqCd9GUznXIs7UwTw/ltScG7JXpGhPJPcR5ZU5YdzlycIxOx reU/Dw5i1zaKkDP9tPJcE6pge+YUMEGJTb4w2ecu9hdxSihE63tcVxyYXx3kiewzDqF7 KA3QLMCkMTFmZhisGo3MLzMqRz5HjeSbV+DGW/mKKxFImIDGiUKlj35+RllfQ+zhoxy8 Ki2g== X-Forwarded-Encrypted: i=1; AJvYcCXPtXNHRwCmYvwCsGqZLbYA78ibBqd//oz0YYaM9j6Jdn0+r3JwiVPJ1PaWC+ZeyWWUvmnSqyDtdFuvWFw=@vger.kernel.org X-Gm-Message-State: AOJu0YxcugZeQfVpQN2BkpoxJy3b1/sMUybJso6htBj/ThbRu3fkylIQ rG7jFL4yWU6HUDreuCPe2iHBctQvOxE3kNvUESm9OxtM1t0JN4T0dW5rY5JrpliJgh6t4QL3oVz TszJkYw== X-Google-Smtp-Source: AGHT+IGN4WqjP5AS34icJap3ZKQQtP5FF3Ggc2KLquv6lW3PDaM97SY0/Y7I+AQ0F4GmpynVDAdw2DfaNtI= X-Received: from pjbsi6.prod.google.com ([2002:a17:90b:5286:b0:2fc:1158:9fe5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d2ce:b0:30a:29be:8fe1 with SMTP id 98e67ed59e1d1-30e7d5212b9mr43135285a91.9.1747962036484; Thu, 22 May 2025 18:00:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:20 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-16-seanjc@google.com> Subject: [PATCH v2 15/59] KVM: SVM: Add enable_ipiv param, never set IsRunning if disabled From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maxim Levitsky Let userspace "disable" IPI virtualization for AVIC via the enable_ipiv module param, by never setting IsRunning. SVM doesn't provide a way to disable IPI virtualization in hardware, but by ensuring CPUs never see IsRunning=3D1, every IPI in the guest (except for self-IPIs) will generate a VM-Exit. To avoid setting the real IsRunning bit, while still allowing KVM to use each vCPU's entry to update GA log entries, simply maintain a shadow of the entry, without propagating IsRunning updates to the real table when IPI virtualization is disabled. Providing a way to effectively disable IPI virtualization will allow KVM to safely enable AVIC on hardware that is susceptible to erratum #1235, which causes hardware to sometimes fail to detect that the IsRunning bit has been cleared by software. Note, the table _must_ be fully populated, as broadcast IPIs skip invalid entries, i.e. won't generate VM-Exit if every entry is invalid, and so simply pointing the VMCB at a common dummy table won't work. Alternatively, KVM could allocate a shadow of the entire table, but that'd be a waste of 4KiB since the per-vCPU entry doesn't actually consume an additional 8 bytes of memory (vCPU structures are large enough that they are backed by order-N pages). Signed-off-by: Maxim Levitsky [sean: keep "entry" variables, reuse enable_ipiv, split from erratum] Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 32 ++++++++++++++++++++++++++------ arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/svm/svm.h | 8 ++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 0c0be274d29e..48c737e1200a 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -292,6 +292,13 @@ static int avic_init_backing_page(struct kvm_vcpu *vcp= u) /* Setting AVIC backing page address in the phy APIC ID table */ new_entry =3D avic_get_backing_page_address(svm) | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; + svm->avic_physical_id_entry =3D new_entry; + + /* + * Initialize the real table, as vCPUs must have a valid entry in order + * for broadcast IPIs to function correctly (broadcast IPIs ignore + * invalid entries, i.e. aren't guaranteed to generate a VM-Exit). + */ WRITE_ONCE(kvm_svm->avic_physical_id_table[id], new_entry); =20 return 0; @@ -769,8 +776,6 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) { struct kvm_vcpu *vcpu =3D &svm->vcpu; - struct kvm *kvm =3D vcpu->kvm; - struct kvm_svm *kvm_svm =3D to_kvm_svm(kvm); unsigned long flags; u64 entry; =20 @@ -788,7 +793,7 @@ static int svm_ir_list_add(struct vcpu_svm *svm, * will update the pCPU info when the vCPU awkened and/or scheduled in. * See also avic_vcpu_load(). */ - entry =3D READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]); + entry =3D svm->avic_physical_id_entry; if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK, true, pi->ir_data); @@ -998,14 +1003,26 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) */ spin_lock_irqsave(&svm->ir_list_lock, flags); =20 - entry =3D READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]); + entry =3D 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 + svm->avic_physical_id_entry =3D entry; + + /* + * If IPI virtualization is disabled, clear IsRunning when updating the + * actual Physical ID table, so that the CPU never sees IsRunning=3D1. + * Keep the APIC ID up-to-date in the entry to minimize the chances of + * things going sideways if hardware peeks at the ID. + */ + if (!enable_ipiv) + entry &=3D ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; + WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); + avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, true); =20 spin_unlock_irqrestore(&svm->ir_list_lock, flags); @@ -1030,7 +1047,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) * can't be scheduled out and thus avic_vcpu_{put,load}() can't run * recursively. */ - entry =3D READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]); + entry =3D 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)) @@ -1049,7 +1066,10 @@ 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(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); + svm->avic_physical_id_entry =3D entry; + + if (enable_ipiv) + WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); =20 spin_unlock_irqrestore(&svm->ir_list_lock, flags); =20 diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 0ad1a6d4fb6d..56d11f7b4bef 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -231,6 +231,7 @@ module_param(tsc_scaling, int, 0444); */ static bool avic; module_param(avic, bool, 0444); +module_param(enable_ipiv, bool, 0444); =20 module_param(enable_device_posted_irqs, bool, 0444); =20 @@ -5594,6 +5595,7 @@ static __init int svm_hardware_setup(void) enable_apicv =3D avic =3D avic && avic_hardware_setup(); =20 if (!enable_apicv) { + enable_ipiv =3D false; svm_x86_ops.vcpu_blocking =3D NULL; svm_x86_ops.vcpu_unblocking =3D NULL; svm_x86_ops.vcpu_get_apicv_inhibit_reasons =3D NULL; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index f225d0bed152..939ff0e35a2b 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -307,6 +307,14 @@ struct vcpu_svm { u32 ldr_reg; u32 dfr_reg; =20 + /* This is essentially a shadow of the vCPU's actual entry in the + * Physical ID table that is programmed into the VMCB, i.e. that is + * seen by the CPU. If IPI virtualization is disabled, IsRunning is + * only ever set in the shadow, i.e. is never propagated to the "real" + * table, so that hardware never sees IsRunning=3D1. + */ + u64 avic_physical_id_entry; + /* * Per-vCPU list of irqfds that are eligible to post IRQs directly to * the vCPU (a.k.a. device posted IRQs, a.k.a. IRQ bypass). The list --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB84825D549 for ; Fri, 23 May 2025 01:00:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962040; cv=none; b=qeakh7mTZZq56iX3K1B/7CodIFIVPePiXEmgY6gi1LQiN67Gx2niH4Z9NHIzOrqryjtOZVf6PqZR/zMDhsjS0fvbSLCEceILWhm9WDMF0nUWICpgaX8MpWT/jT3qBNGqJziqvtmpuky8J0Yhg/aC1oDXiTIXnGHqCm5z7aW+HEA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962040; c=relaxed/simple; bh=nHqa6WlMR0U01OJANRFGYwjUMNRNK2ND6QPMEXp4N18=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gVS/g7HOhYV3TCmvl+JGyZPyWbj+vv1mleyWEFcG9fJojUWj1zqG2YimFXjF/m6ENE9eyxaiBmoNdOQRkEptAAgOmEqmHrvR2JGL2RFo2yWOLg3PXTep3BSX/vuShfBXWPQSlifm9GkejL35WEpN4nJchXtnvQuZIm6bsGVGZ+A= 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=FDGXcOrH; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FDGXcOrH" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30ebfc28a1eso8131906a91.2 for ; Thu, 22 May 2025 18:00:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962038; x=1748566838; 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=CxW6KKsPctpa5xxKfQSk3XBX60R5QhwquqFgbTrtuqU=; b=FDGXcOrH27gT47oZjpE7XjMJkn0KZTdi4f7vcxdbQSOgrhv15NKDihEwOwN10IDAOQ MZOu87NZ9tBZo6nrg0e4KiWbmSnWmMkQh0O49JCB4yUUetsbiOCZ/XutWJthlaYprBlh 3vK0OD0zZl2S6UZgSePL6WV0znzPln/EcUE6a2Y0QVS6wqSFMoCRRnK5dawX855j736P SQEL/sREfxJAJ8tjDS3AFcrqZsU+v/15VF/Upkdils7J8tMWTXtQPuZeXpxeF55DS5RT 5s7ucZvJnhFtpD8GaEcR55uczo8yUNRAMEWlkKlyT6qgJ6QmK0R3JvfFtCtEhpEsMHUx 9QGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962038; x=1748566838; 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=CxW6KKsPctpa5xxKfQSk3XBX60R5QhwquqFgbTrtuqU=; b=bTnqDHZZPI/+SSlHvWT1vLBxPK/hZq8hfwbySH5eaRWOLvTi4i1HmN/hRvMnckyT4L H+7EpqnobXLzfkNUEpwg0TxuS5E540alKEA47z/sJG/1eMUEqUqMLDuwwljiT2Di/MeW PvO9sKx7gWYAEmpetwfFh62rOtANti9fS5ak7dgtkTMEAZIhev5k8BJfLVI4Bfxb3AFL kmfLASH3VsfT3Q6AcmxUsfZFXyh96COVCHYM98bvgY3qUAeZVIkafgchktNx5dHOykdd e+OV5hXY8x2aGnZDpfr1UNNpj9pCOlY5mNYlok+Z+5gsRpppIoWxY5LcYFWRIwHjxUfr p/iA== X-Forwarded-Encrypted: i=1; AJvYcCWoD+NA1KhL+NKHu/6upZi/nTkeK52sNrhtYcF97LakHa1JIMTWpzsZKGE12za662pSxqGvdp/BSxusv/s=@vger.kernel.org X-Gm-Message-State: AOJu0YwXhyqKC4wcqHC+dhha15/ETt2vi2iWKCBnzrPTkt0U4QAufT3l /UoksloXMCkOqPoTBZB3FWoE0y5TqXrqaUNh9vAaUVDzfwSdVUz4soj/WvOG65M+Sfifzrf6CoI DfP/2EA== X-Google-Smtp-Source: AGHT+IHk8lbSET1s/ssqemmnc/hWnJF6h21Rp292LM1IvL/agAUDkDm3aH+8vSocZT0JgbvOwlT/Y0l6Lss= X-Received: from pjbsi11.prod.google.com ([2002:a17:90b:528b:b0:30a:7c16:a1aa]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1dc2:b0:2ee:b2e6:4276 with SMTP id 98e67ed59e1d1-30e8322593dmr39301128a91.27.1747962038018; Thu, 22 May 2025 18:00:38 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:21 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-17-seanjc@google.com> Subject: [PATCH v2 16/59] KVM: SVM: Disable (x2)AVIC IPI virtualization if CPU has erratum #1235 From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Maxim Levitsky Disable IPI virtualization on AMD Family 17h CPUs (Zen2 and Zen1), as hardware doesn't reliably detect changes to the 'IsRunning' bit during ICR write emulation, and might fail to VM-Exit on the sending vCPU, if IsRunning was recently cleared. The absence of the VM-Exit leads to KVM not waking (or triggering nested VM-Exit) of the target vCPU(s) of the IPI, which can lead to hung vCPUs, unbounded delays in L2 execution, etc. To workaround the erratum, simply disable IPI virtualization, which prevents KVM from setting IsRunning and thus eliminates the race where hardware sees a stale IsRunning=3D1. As a result, all ICR writes (except when "Self" shorthand is used) will VM-Exit and therefore be correctly emulated by KVM. Disabling IPI virtualization does carry a performance penalty, but benchmarkng shows that enabling AVIC without IPI virtualization is still much better than not using AVIC at all, because AVIC still accelerates posted interrupts and the receiving end of the IPIs. Note, when virtualizaing Self-IPIs, the CPU skips reading the physical ID table and updates the vIRR directly (because the vCPU is by definition actively running), i.e. Self-IPI isn't susceptible to the erratum *and* is still accelerated by hardware. Signed-off-by: Maxim Levitsky [sean: rebase, massage changelog, disallow user override] Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 48c737e1200a..bf8b59556373 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -1187,6 +1187,14 @@ bool avic_hardware_setup(void) if (x2avic_enabled) pr_info("x2AVIC enabled\n"); =20 + /* + * Disable IPI virtualization for AMD Family 17h CPUs (Zen1 and Zen2) + * due to erratum 1235, which results in missed GA log events and thus + * missed wake events for blocking vCPUs due to the CPU failing to see + * a software update to clear IsRunning. + */ + enable_ipiv =3D enable_ipiv && boot_cpu_data.x86 !=3D 0x17; + amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); =20 return true; --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 5DF67261586 for ; Fri, 23 May 2025 01:00:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962042; cv=none; b=V7GzgJi9OUxGcTjSP2YBFrT5+qbOdB6VyCWxJ8XqFDrbJGT52WdQk/4u8QNOgZyWXOym8Q36X6Lze8norVPg86bHnC7NyMbEnFCRcD64kmWy+5dEH6vUzEqV5jbSCjAVg9beEseNVBqP4g8iRBP+3eeEALhBo4FCMAiVaXNQyrw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962042; c=relaxed/simple; bh=4de0q3T14DeT0F0X3ZAAYDHlZ1QD6sh3cOnu2ImrrHk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AzXdZXync5p89Fe12hYJWEjcsoYScB3ll/Tche3msqEbvCs9hZDEmjxQiMMJdyDZcUum4c1TKwum5NrZab9E3RPv6cPXVK1GW+Caus0c50Q87ZxM2zZIDhjLB2RZM0uVWpT4oYYagH7TYUvoFgoD3VJNN5PTW2j+SKRTqbx1Gco= 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=PCb4Bu4g; arc=none smtp.client-ip=209.85.210.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="PCb4Bu4g" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7375e2642b4so6468769b3a.2 for ; Thu, 22 May 2025 18:00:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962040; x=1748566840; 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=aWd9B+cmIuOPKCFRA3Y6+3mZ7hC0+f0rb42YHlM7f7Q=; b=PCb4Bu4grvgIpiqiC6cqGrtbX9/a1qTb4DwhDI4jDGrtPaeRwHO7mx2cVkK8ImL08A n7VKAB07iHZZsrCpQ91bP4LZcOYA/UMHZTNCLKgE0VffYLo+IYk7TlnPV8SClHZXvqDF Pu748ONkgmU6C8qKkcavok6fmSMV1yEBCYABO3j+T50sSpFjovJAOCRDhDNW0DgJ4gMB 3UE1TYQ17C22lictqalz9SSpUwBK1qEsNFSIlTrLDB0bRDoU32PcT4xgfxHdZqy+CiDI EYM+qG4KbNfSz//qLfNj5IkPHUhu9ixUgLvh58P3tGwYyqs0BaWNOXcuszwn68Md1Rrq //sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962040; x=1748566840; 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=aWd9B+cmIuOPKCFRA3Y6+3mZ7hC0+f0rb42YHlM7f7Q=; b=kDfQNoDfPMYgsDESsYFkgWLzNOF1yH7T6AkuAraUZdlpGkgB/dSvhNH3WCvSALP+YH TBXCOulIjvjVCWV0TkrOy5iropkZuLLd1oAh4qIkaynPlrKH+OcoFohUpjGYx+TSuzVM wn237UqYHiEDxcQ4xekFn2VO/0z6QqhT/o/vLOJB+vZp4gHrH++JSNnLYwxiBG5ZDaMM H853/CcW0dUTVOtmXm/KjuRBdKSJkVUaFEB2S1x4xDf7zGjIf2IRaMo63+EWvBlLXGKg 3d0bAH9RNadqZAjwo7RUJokvl2jx422DHBUetO2KodwYDnXT2B7tNA9zEyCwHRAwvlBN 4KUw== X-Forwarded-Encrypted: i=1; AJvYcCXZx9VyZaip2z71lkVUCFMIrx1bYiQeRJyNlMwJOM9+piKY/DQstAip4ITz7Umb4kA9/LYJ5MhdffWNLzw=@vger.kernel.org X-Gm-Message-State: AOJu0YwYNYYhtnHvW+fsIUaNVyfu7bLZyJonBbhSWPHQcpwDZmHrB40Z MUruC3dDs6qzyYo+hgeNNd4lnKJJNbLbPqvizxUoMc/414hG9W/tRawXULySQcVpbc/PxcBShbQ ydkCBsw== X-Google-Smtp-Source: AGHT+IHIoGb2eWkztEC9ZxX2CyQRptApdmezXKPQEhifuGSkSLTWTrhoDwK2W6RjGyel2aIvam5GJ+N3m38= X-Received: from pgar23.prod.google.com ([2002:a05:6a02:2e97:b0:b26:bb7f:b0d3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:350d:b0:215:e9ec:81d2 with SMTP id adf61e73a8af0-216219bd5c3mr48285536637.32.1747962039674; Thu, 22 May 2025 18:00:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:22 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-18-seanjc@google.com> Subject: [PATCH v2 17/59] KVM: VMX: Suppress PI notifications whenever the vCPU is put From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Suppress posted interrupt notifications (set PID.SN=3D1) whenever the vCPU is put, i.e. unloaded, not just when the vCPU is preempted, as KVM doesn't do anything in response to a notification IRQ that arrives in the host, nor does KVM rely on the Outstanding Notification (PID.ON) flag when the vCPU is unloaded. And, the cost of scanning the PIR to manually set PID.ON when loading the vCPU is quite small, especially relative to the cost of loading (and unloading) a vCPU. On the flip side, leaving SN clear means a notification for the vCPU will result in a spurious IRQ for the pCPU, even if vCPU task is scheduled out, running in userspace, etc. Even worse, if the pCPU is running a different vCPU, the spurious IRQ could trigger posted interrupt processing for the wrong vCPU, which is technically a violation of the architecture, as setting bits in PIR aren't supposed to be propagated to the vIRR until a notification IRQ is received. The saving grace of the current behavior is that hardware sends notification interrupts if and only if PID.ON=3D0, i.e. only the first posted interrupt for a vCPU will trigger a spurious IRQ (for each window where the vCPU is unloaded). Ideally, KVM would suppress notifications before enabling IRQs in the VM-Exit, but KVM relies on PID.ON as an indicator that there is a posted interrupt pending in PIR, e.g. in vmx_sync_pir_to_irr(), and sadly there is no way to ask hardware to set PID.ON, but not generate an interrupt. That could be solved by using pi_has_pending_interrupt() instead of checking only PID.ON, but it's not at all clear that would be a performance win, as KVM would end up scanning the entire PIR whenever an interrupt isn't pending. And long term, the spurious IRQ window, i.e. where a vCPU is loaded with IRQs enabled, can effectively be made smaller for hot paths by moving performance critical VM-Exit handlers into the fastpath, i.e. by never enabling IRQs for hot path VM-Exits. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/posted_intr.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index 110fb19848ab..d4826a6b674f 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -73,13 +73,10 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) /* * If the vCPU wasn't on the wakeup list and wasn't migrated, then the * full update can be skipped as neither the vector nor the destination - * needs to be changed. + * needs to be changed. Clear SN even if there is no assigned device, + * again for simplicity. */ if (pi_desc->nv !=3D POSTED_INTR_WAKEUP_VECTOR && vcpu->cpu =3D=3D cpu) { - /* - * Clear SN if it was set due to being preempted. Again, do - * this even if there is no assigned device for simplicity. - */ if (pi_test_and_clear_sn(pi_desc)) goto after_clear_sn; return; @@ -225,17 +222,23 @@ void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) if (!vmx_needs_pi_wakeup(vcpu)) return; =20 - if (kvm_vcpu_is_blocking(vcpu) && + /* + * If the vCPU is blocking with IRQs enabled and ISN'T being preempted, + * enable the wakeup handler so that notification IRQ wakes the vCPU as + * expected. There is no need to enable the wakeup handler if the vCPU + * is preempted between setting its wait state and manually scheduling + * out, as the task is still runnable, i.e. doesn't need a wake event + * from KVM to be scheduled in. + * + * If the wakeup handler isn't being enabled, Suppress Notifications as + * the cost of propagating PIR.IRR to PID.ON is negligible compared to + * the cost of a spurious IRQ, and vCPU put/load is a slow path. + */ + if (!vcpu->preempted && kvm_vcpu_is_blocking(vcpu) && ((is_td_vcpu(vcpu) && tdx_interrupt_allowed(vcpu)) || (!is_td_vcpu(vcpu) && !vmx_interrupt_blocked(vcpu)))) pi_enable_wakeup_handler(vcpu); - - /* - * Set SN when the vCPU is preempted. Note, the vCPU can both be seen - * as blocking and preempted, e.g. if it's preempted between setting - * its wait state and manually scheduling out. - */ - if (vcpu->preempted) + else pi_set_sn(pi_desc); } =20 --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA13D26F464 for ; Fri, 23 May 2025 01:00:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962043; cv=none; b=uvwpTwxZcKRCANxYIm6MN2YVEWhbf+dLbDdYlOI7PPlWoqfcBWdRcKsVqC0sXzwMo52jKwFmpedM+CzlfsTJFu737ixBVSiXsVbIJC5zmtD3yyk8YlE33eMNuLXqmBeSUFXnof9FYVcxBoCosklOHmYNSOj50kovCt8twNjk97k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962043; c=relaxed/simple; bh=qKfn0sKD3VJ10k6Yz/i/tMNieJlz/OEPZhtcbe8s2Bg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CtGnXviB7vjUN71Mh6+9EOp3imK5JgZSumtR3Mhnpu9isc2lphKB9hz2IRQaO9Lb4GjWGl+y96yXHg1gXQgWKe4Q8a7eeTavOJhtsS48dhoBRJDQEjyxAKozJDx/fcUpA4GPQhg3Z86WSIZ+HzsmVXxwmm7k/Dp41os+UlZsf58= 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=p340EsbV; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="p340EsbV" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30f01e168b9so363574a91.1 for ; Thu, 22 May 2025 18:00:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962041; x=1748566841; 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=2h36XJDcqMOP2O/HIXVaEhk+4kdxNMwCrjoN8g3bZnc=; b=p340EsbVAHPp875Kvny4ob2M/aZQLpHieoyaG/EiDRXoTDODpo19VARKAE6SAcDJyS fFAmmCFXV8v+JF8NyVRmUTsNi3dsbe1SpkUuBFPIWn3Ux18KEsjXVZYm22q2rbZQ9Cm4 0S6HC0+TPqzumK+aLymqJF6Ld5QM1CqacPwAl/lbgj6R2kJzTaHW0RFOx3jBxCntx0B7 tk70yW/2M/FLDbLeuGjDDZhwQoeiYv3mhcMar4TiqOBdBlz3svoNuOP/fNriEcLYLqMV Hq+E50PezzSsLjnHO4tkfO+qhZENdUuDyM4E9Ek42Jm0LxZhf488ty5y+KAf9cKqs5Cg Hvig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962041; x=1748566841; 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=2h36XJDcqMOP2O/HIXVaEhk+4kdxNMwCrjoN8g3bZnc=; b=OCFk3v/g6xngdF+LNsxtHZr1dJ71/MvM9A1dWON3i8oCEATRg5oZwEtGscLb5tDHOA 891TOG+0+03RG8NAPQWEFvf41mAScvyHyzqCdDgoH83Qg23zc3/umwIjAJj11QDwfLSe lvc6/vY1QYPxLfTf615+iQmc54wip8Oi5TPLPVvNRkAoBjgyY9hUjRoUSkxzrf6sO4Ln yWaGu+lheDCiqWm9NsvixGeftw6FfSbYkcLfAui4GutIVJcq2Kmuo6rr8MgIKIb1LsWa JbGP22WHAnfHzzjGOJVHeUEkgQIoM7AUSQLPKhPyC4BX3Q6hto+DjHueabJuZ/W+yTsw Unlw== X-Forwarded-Encrypted: i=1; AJvYcCUivGE8Rd83pHEmFLKc1zaHDeT9xHEVmqbNH4pFSOFDYqneZFTk/13a4eIbI1rNxamYSu+uxSgMpVouKN0=@vger.kernel.org X-Gm-Message-State: AOJu0Ywre7WxY6CDSCbupxvIm727HHHkmUzcEuymlMTnMIa88SEJtE0D CDpoRHq9TIQIsXOTWnbdqpap8LoVqkFprkuRN1cM6jkOh4rKM3wGfHZzpXJNwtQ3lYdhT/Qwtdm /vfNaZw== X-Google-Smtp-Source: AGHT+IEMSIoZYl1TZlKkM0v8bu58XVa9qs8p/xOaWsLXJEUYuTfjToJ8vFZtbUt+y6pO6aPc+iYxsxiZ6fE= X-Received: from pjbdj5.prod.google.com ([2002:a17:90a:d2c5:b0:310:89bf:401]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1944:b0:302:fc48:4f0a with SMTP id 98e67ed59e1d1-310e87b54bemr1697766a91.0.1747962041094; Thu, 22 May 2025 18:00:41 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:23 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-19-seanjc@google.com> Subject: [PATCH v2 18/59] KVM: SVM: Add a comment to explain why avic_vcpu_blocking() ignores IRQ blocking From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a comment to explain why KVM clears IsRunning when putting a vCPU, even though leaving IsRunning=3D1 would be ok from a functional perspective. Per Maxim's experiments, a misbehaving VM could spam the AVIC doorbell so fast as to induce a 50%+ loss in performance. Link: https://lore.kernel.org/all/8d7e0d0391df4efc7cb28557297eb2ec9904f1e5.= camel@redhat.com Cc: Maxim Levitsky Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index bf8b59556373..3cf929ac117f 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -1121,19 +1121,24 @@ void avic_vcpu_blocking(struct kvm_vcpu *vcpu) if (!kvm_vcpu_apicv_active(vcpu)) return; =20 - /* - * Unload the AVIC when the vCPU is about to block, _before_ - * the vCPU actually blocks. - * - * Any IRQs that arrive before IsRunning=3D0 will not cause an - * incomplete IPI vmexit on the source, therefore vIRR will also - * be checked by kvm_vcpu_check_block() before blocking. The - * memory barrier implicit in set_current_state orders writing - * IsRunning=3D0 before reading the vIRR. The processor needs a - * matching memory barrier on interrupt delivery between writing - * IRR and reading IsRunning; the lack of this barrier might be - * the cause of errata #1235). - */ + /* + * Unload the AVIC when the vCPU is about to block, _before_ the vCPU + * actually blocks. + * + * Note, any IRQs that arrive before IsRunning=3D0 will not cause an + * incomplete IPI vmexit on the source; kvm_vcpu_check_block() handles + * this by checking vIRR one last time before blocking. The memory + * barrier implicit in set_current_state orders writing IsRunning=3D0 + * before reading the vIRR. The processor needs a matching memory + * barrier on interrupt delivery between writing IRR and reading + * IsRunning; the lack of this barrier might be the cause of errata #1235= ). + * + * Clear IsRunning=3D0 even if guest IRQs are disabled, i.e. even if KVM + * doesn't need to detect events for scheduling purposes. The doorbell + * used to signal running vCPUs cannot be blocked, i.e. will perturb the + * CPU and cause noisy neighbor problems if the VM is sending interrupts + * to the vCPU while it's scheduled out. + */ avic_vcpu_put(vcpu); } =20 --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DEB620C489 for ; Fri, 23 May 2025 01:00:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962044; cv=none; b=cqmC8wAwKUnlTa0eWYmA57/Zk04Zq0vjIQ3Xey4VfS3Ti11AyrEGUebe0+U6heVzatNu0f7nAY0dD0wz85TBWigOyWjzVxZJA93JIQsxVT10eDKJyAW9MJcYwQdyec5QvGbqt7dAu6v1l82TCz90/Pyh0N6OvyilHVUWrh5bq4w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962044; c=relaxed/simple; bh=J1mv6D20whYTlmsfDBfjzwzrzJ6/jskwNzsjA8aeq6U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ARI0/5T0griv8XELcbilaqmkA8PcqnfI0OufzkSykQaszI2sv+RaFIeaxxVw7jusOw0KW0gQOzV4gpSJaOhsP1n+TyQXOF/Tubk3ZcBWjDsTqNPH5TBSRFZXkxnghvdvJe2RnpyDfZH6ZjKUPufIRfQiU6ZvYtZor+w+An5rk50= 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=ku3bRi/A; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ku3bRi/A" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30ebfc28a1eso8132026a91.2 for ; Thu, 22 May 2025 18:00:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962042; x=1748566842; 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=fKnvJ2LV8riPZoPU3O/CI+BFOmSeLWZITljIzp2E0GU=; b=ku3bRi/AaUAkD69ojG0a4y4Lzb/7HqhBHhvyixgQ7+vQ9f9qezSJO90hecb1JuuWjA YTOqutPagSMnC6/O7qvBgw+cFc+xB/Bfp1E4Fr3YinFmT5UqcfRvkhKl0yizrQBit9sN WOEUC58pqlkq/blieg1IsBbOE5SZ7oIo1vImPOdcQfLJvzQN2cgbVVIhEFTXciY9iP2h tolnGoB1fpE7wAaRneCEe6TNcEOZln7APHmh1AWuw+29Ko0PD+papCTO/IV6A0VBKeve hw9PZWkjMQHB0+I7a4pCWynJJGjaKjnPISWBr0lWr4FbMghtNIyA8Ct5tF/7ebK2asCF 77Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962042; x=1748566842; 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=fKnvJ2LV8riPZoPU3O/CI+BFOmSeLWZITljIzp2E0GU=; b=vE7YtnWOj2dayG2+67g4APSyVClVu1l93EeX7f+x+ATpc1hKuAuwrQLqCJDBgwis5f boUnYtkvQHA6MgaiBwJLVQNPQj2rhslwf7vnuN+O6pmvEESM0CrZP/HT6+hjihJSIGyP JXfA0mUgpOWv1SS0AvdlHdJu2G2JlFi8mkuqIgauJSQ0H/MaJaaiwrUM2e/xN0Li5i1J c0dvwO/PVOX9OeoWRZ/c7BEeCtuywWT6ja/5i/olnKUaaJGWaFIWvm1/UA3Aa53g/8rx 2JJUKGETYXBMOMF7sHQ7+xTZ+A+gV6uPbKPZ1evhojr9Y3dYjZCgr/TbF4uM9YAl8Ei+ GQ4Q== X-Forwarded-Encrypted: i=1; AJvYcCXUtur0Yri9cvcINMl87YhR2d6OR/kL4EaU1JaAYr/FvkKLD7v263dW+fEqg8c/Mx2mKgOf+LyJf5DoM/U=@vger.kernel.org X-Gm-Message-State: AOJu0Yzh5nPHh1wgwkvBOUIv2/+Ma40MQrKAU7AdDDwauvuT3Rez6zvg wy0fyVgT7Cjd63r/m1fcmepMsBrp6SwS6+j+4uomiuaNZUWXT/9o9UuW7GzQxiWqf5uUlHY7gr5 Ed41icg== X-Google-Smtp-Source: AGHT+IGR/Df5BJxs3ol6VXQkpzy/R+7lIXiH+7qF+fijHNHGe4vwvP4VxYidt3ojzy4f91Evs8kBdHkzJi4= X-Received: from pji7.prod.google.com ([2002:a17:90b:3fc7:b0:2fa:a30a:3382]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5746:b0:2ee:f687:6acb with SMTP id 98e67ed59e1d1-30e830fc346mr35983530a91.13.1747962042547; Thu, 22 May 2025 18:00:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:24 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-20-seanjc@google.com> Subject: [PATCH v2 19/59] iommu/amd: KVM: SVM: Use pi_desc_addr to derive ga_root_ptr From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack 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. Cc: Maxim Levitsky Reviewed-by: Joao Martins Reviewed-by: Vasant Hegde Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 7 +++++-- drivers/iommu/amd/iommu.c | 2 +- include/linux/amd-iommu.h | 2 -- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 3cf929ac117f..461300bc5608 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -893,8 +893,11 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, =20 enable_remapped_mode =3D false; =20 - /* Try to enable guest_mode in IRTE */ - pi.base =3D avic_get_backing_page_address(svm); + /* + * Try to enable guest_mode in IRTE. Note, the address + * of the vCPU's AVIC backing page is passed to the + * IOMMU via vcpu_info->pi_desc_addr. + */ 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 f23635b062f0..512167f7aef4 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3888,7 +3888,7 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *= data, void *vcpu_info) pi_data->ir_data =3D ir_data; =20 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 1f9b13d803c5..deeefc92a5cf 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -19,8 +19,6 @@ struct amd_iommu; */ struct amd_iommu_pi_data { u32 ga_tag; - u64 base; - bool is_guest_mode; struct vcpu_data *vcpu_data; void *ir_data; --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D5B2E20C031 for ; Fri, 23 May 2025 01:00:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962046; cv=none; b=QvCH/eewEOKEvhhAbu4H97JxkiledOXnC86Wx0fTdxIEbh8TI5ZGdNQsUz2NpbfHA+xZPr/frbfxih75bGpXopR1nMKQ5wKqihiVzeP9ilWYQOeuqWlAGPbpFtmvOSy6fbZRWPtr58mCgUjmvyZAHyxATytFWSzdCkG1KFVslM4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962046; c=relaxed/simple; bh=fW2G94RfLD78AMcqDsZ5ps4G4Sno9GFbL+j5/DPryVk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GaFmn/HYI/Z9Utve4fsNnxCMaP0Nj9vhP+LQJpuqiKABr0Ltb0KmnPFifMhbo1ZzlVH7yN3ksloSS0rNDLQSGeveAOXksji0XREAmyDbKEzBNOJ5YwD36/uVS3hoLu9dy41VsCb7vwe9gIzOvoQsjjC8+0dZPutx9v0PPgLDqQM= 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=RTs1nHHs; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RTs1nHHs" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-310a0668968so2254576a91.0 for ; Thu, 22 May 2025 18:00:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962044; x=1748566844; 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=LEcBPUoa4oxCDsr/z1+DJV9LnDo6qsS8eDb1DWTU3As=; b=RTs1nHHs9KUWdPU3KLg31b8ljMlwp/KoQH3QQ3unj3oXLVSfYzzpPVGtKTpZG6kAXq hrJQO+ge/GlQy3gcyxvECWmjRLTcHbzUekKXx+a996a/twxwQLWC1rOiBNLZbAO7QDRJ 8RAoIxEjJNL85uFe8vqbo/gYqDJgZFQ5xKGu36jBayJXs9QtUO1HAsHnlxSCri985XCX bWvtAYkxcH4Bb0l5i0QFdFBITm5lwT0nqUeieWx4lBXCod3vdUhZjZx9xJRpjtq2JnDK aX9XRNU/FngkFF7tZSSxXBLmUjLn3ZT0YebT/SoO9v1wGDgphC7XP+fCY3T25Dg496Gj yr+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962044; x=1748566844; 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=LEcBPUoa4oxCDsr/z1+DJV9LnDo6qsS8eDb1DWTU3As=; b=K8/Sz7KW4c0/4sVXB7l6E71QSmXHr3wTCgUbKeISYDdGBC1sH2D8iQcZEq83emzGnT QVdS/YQ6Wu5vbPfIMaRFXzCdripI7tJuvX56xlaw7eKBD0d6MAuQPItp3a0BAjZ2LRCr bdUIlJ04b13pPOTYRsvQk4aVBc0ieqT7Y6xne6xu6Afi4mBQzcgNgooM2WkCd3yY+8u0 1TB1r4mVGj3UXehockq2L7EVYwJYM4keBHfuIgM9Ye1OSxHHfIoMDk6EMdiNepq1IqEd F+eMQpBNFISh/26s0HTG7G62TXOWlhoE/gP3/e1tF2jJPxDiJVC6QSfAO2/6Mh6x9YKL 56iA== X-Forwarded-Encrypted: i=1; AJvYcCULDjd1Ix18Zct6ekvjc1T4G8YrvPa/SFH3CodkV+ceJPniEAW44nbHMKTCYJFw0bXtnqbQK7ztZytyQ7Q=@vger.kernel.org X-Gm-Message-State: AOJu0YzbkqNaD+YZK6eY2ZcII7gTRBe0puDaqqF5lbZaVP479flH79eL OVJjGVgddq77jg17eQ+nzEGZ1FXEh4QG04so9G3kBHOoA/pVaHb9i/keQEeKVhQfhFENlSSdZe+ HSP73qg== X-Google-Smtp-Source: AGHT+IGKaHuHYtNmHM5Kq9jQbWuDh5HQqrhGXZK4H0D0g7q7j+eU/9f1bdcnBIi1ajs832Ec0O94mR8aIqk= X-Received: from pjbsl8.prod.google.com ([2002:a17:90b:2e08:b0:301:1bf5:2f07]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3b48:b0:2ee:d433:7c50 with SMTP id 98e67ed59e1d1-30e7d5ac7ecmr33843110a91.23.1747962044116; Thu, 22 May 2025 18:00:44 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:25 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-21-seanjc@google.com> Subject: [PATCH v2 20/59] iommu/amd: KVM: SVM: Pass NULL @vcpu_info to indicate "not guest mode" From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Pass NULL to amd_ir_set_vcpu_affinity() to communicate "don't post to a vCPU" now that there's no need to communicate information back to KVM about the previous vCPU (KVM does its own tracking). Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 16 ++++------------ drivers/iommu/amd/iommu.c | 10 +++++++--- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 461300bc5608..6260bf3697ba 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -927,18 +927,10 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqf= d, struct kvm *kvm, } } =20 - ret =3D 0; - if (enable_remapped_mode) { - /* Use legacy mode in IRTE */ - struct amd_iommu_pi_data pi; - - /** - * Here, pi is used to: - * - Tell IOMMU to use legacy mode for this interrupt. - */ - pi.is_guest_mode =3D false; - ret =3D irq_set_vcpu_affinity(host_irq, &pi); - } + if (enable_remapped_mode) + ret =3D irq_set_vcpu_affinity(host_irq, NULL); + else + ret =3D 0; out: srcu_read_unlock(&kvm->irq_srcu, idx); return ret; diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 512167f7aef4..5141507587e1 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3864,7 +3864,6 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *= data, void *vcpu_info) { int ret; struct amd_iommu_pi_data *pi_data =3D vcpu_info; - struct vcpu_data *vcpu_pi_info =3D pi_data->vcpu_data; struct amd_ir_data *ir_data =3D data->chip_data; struct irq_2_irte *irte_info =3D &ir_data->irq_2_irte; struct iommu_dev_data *dev_data; @@ -3885,9 +3884,14 @@ static int amd_ir_set_vcpu_affinity(struct irq_data = *data, void *vcpu_info) return -EINVAL; =20 ir_data->cfg =3D irqd_cfg(data); - pi_data->ir_data =3D ir_data; =20 - if (pi_data->is_guest_mode) { + if (pi_data) { + struct vcpu_data *vcpu_pi_info =3D pi_data->vcpu_data; + + pi_data->ir_data =3D ir_data; + + WARN_ON_ONCE(!pi_data->is_guest_mode); + 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; --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6952C274FF5 for ; Fri, 23 May 2025 01:00:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962048; cv=none; b=m5u9OuHpNL67GpI1JWTBjqsswL3myNYxHyIfWVRm5k1JC/7E/XQnBHLRvV46x3CXKvdKKYYi9NkNd4nilH9q0Xd8t+J/43O5LLY0A02jw0HDLfWYspOMcnxtZvFm1TDLQanH2chffwkRuEkZnxbef77tleVO046QpAC0QWUlsJo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962048; c=relaxed/simple; bh=XTKaQxa4npnh2rWsQb6P4+FcVAHho6ewDy41tUd6na8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PfABxpd35z6B/govudZMPihmKRNogkvljwgsDde0AfI1WOqmTnAcN6Bj+v407H849fqFN7InPTPUS+t3qF6+YBInSQc6zNzg5OWY7EI2sai58b4ZxA01aka3QtZdHRvvYamr+zO0es32P+MAEHZonYQdD7fcTcAU3oYZRbgvGWI= 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=ttTXcdbJ; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ttTXcdbJ" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30a29af28d1so6784187a91.0 for ; Thu, 22 May 2025 18:00:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962046; x=1748566846; 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=W1PAenioYvdV3EBJg7xX9kzyF9B21do+ZbIq4hvNUSI=; b=ttTXcdbJM0t693aPAsErXzVTLl0aVQHfbNt3/jkANg8edHoFzTDtmWSNJf+1e2Hbai fji0YbdCYlsF0qU3uHUU94dzIl9U3IMERGmcVuK9IOVsdwS7iVe9KXfd0EmC0OWMCO9L L1Js9H7kPIgXK/v6Mk51GEY4DzZVA9TD896KfGB6ATPcSXHG9lYztIAPha0lNBtD5r90 8+yG2xpqEbJx5PxT4afEZrjNkzlvOjmwVGyQBfQ0ahwiZvDFXpSduazslD1C6HLl2Mh/ TarmzJceQRGDRjtTkH5rIYoTe6PYXIY91PD2YTur5sGV3ayorQAGQdagc4Y4+tsfiOU/ moWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962046; x=1748566846; 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=W1PAenioYvdV3EBJg7xX9kzyF9B21do+ZbIq4hvNUSI=; b=pn3k+tPXMvzHFY993GOnMfKH6Du4ny3S9tYmtUScZF3mENRwGc18CNvQnHyBBK88vV 6uPZAe1wrOkI0Y0kgp6UyeoPW0KSHjCzXUKkQ/OoA7rh+pnsJucDPVBWUYa84+ixExXj OQTLHLQrOuM7Bwr0bNUAL6EkaNo1phMT0/st8J3NhXjaObrZm30sN/LHmPP/+BROG2+e 1OP0Aj8CiyOU4GptTHDIm1z074z9fr2MhP9ehWNA8AiNIxShvcyAcKfHgWqGiWv8aN/C 756ZR2QBVM/5Ne+pNzvDk0pe/JW/AzqDw11r7K1AzReCEXBtDGUxalE7RS4U8hjbkLqh OfEg== X-Forwarded-Encrypted: i=1; AJvYcCWJ3Osl/dvWZhHQBine8v8DQnXhcJqHYmHEYqEr+BbXGI9sdD1fhneiQ6QyCffBZhZVdBnbJQv+z7Nxdhk=@vger.kernel.org X-Gm-Message-State: AOJu0YwINBw8DcAVB7h1t2yFct/KeIIhd/BY/uy4AiNXigFwxNX8yC85 KautADpquYRPvAB9JkYTNVscKyX6X1IunPd4q3yF9ScmlAjhJYFc3vnxZgaWMqf22o9onx8jx9o kfe+qYw== X-Google-Smtp-Source: AGHT+IF50BW7eNbKLTes6wg+6QoUM4Fli2u18WBE8vabKew1Ppo7X2JHY21NH8Gk7becGtcg20a3VvulZ6s= X-Received: from pjbqo12.prod.google.com ([2002:a17:90b:3dcc:b0:2ea:3a1b:f493]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4a86:b0:30e:840a:92ed with SMTP id 98e67ed59e1d1-310e973cef6mr1696555a91.31.1747962045911; Thu, 22 May 2025 18:00:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:26 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-22-seanjc@google.com> Subject: [PATCH v2 21/59] KVM: SVM: Stop walking list of routing table entries when updating IRTE From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that KVM explicitly passes the new/current GSI routing to pi_update_irte(), simply use the provided routing entry and stop walking the routing table to find that entry. KVM, via setup_routing_entry() and sanity checked by kvm_get_msi_route(), disallows having a GSI configured to trigger multiple MSIs. I.e. this is subtly a glorified nop, as KVM allows at most one MSI per GSI, the for-loop can only ever process one entry, and that entry is the new/current entry (see the WARN_ON_ONCE() added by "KVM: x86: Pass new routing entries and irqfd when updating IRTEs" to ensure @new matches the entry found in the routing table). Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 109 ++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 65 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 6260bf3697ba..a83769bb8123 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -843,11 +843,10 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqf= d, struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, struct kvm_kernel_irq_routing_entry *new) { - struct kvm_kernel_irq_routing_entry *e; - struct kvm_irq_routing_table *irq_rt; bool enable_remapped_mode =3D true; - bool set =3D !!new; - int idx, ret =3D 0; + struct vcpu_data vcpu_info; + struct vcpu_svm *svm =3D NULL; + int ret =3D 0; =20 if (!kvm_arch_has_assigned_device(kvm) || !kvm_arch_has_irq_bypass()) return 0; @@ -859,72 +858,53 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqf= d, struct kvm *kvm, svm_ir_list_del(irqfd); =20 pr_debug("SVM: %s: host_irq=3D%#x, guest_irq=3D%#x, set=3D%#x\n", - __func__, host_irq, guest_irq, set); + __func__, host_irq, guest_irq, !!new); =20 - idx =3D srcu_read_lock(&kvm->irq_srcu); - irq_rt =3D srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); + /** + * Here, we setup with legacy mode in the following cases: + * 1. When cannot target interrupt to a specific vcpu. + * 2. Unsetting posted interrupt. + * 3. APIC virtualization is disabled for the vcpu. + * 4. IRQ has incompatible delivery mode (SMI, INIT, etc) + */ + if (new && new->type =3D=3D KVM_IRQ_ROUTING_MSI && + !get_pi_vcpu_info(kvm, new, &vcpu_info, &svm) && + kvm_vcpu_apicv_active(&svm->vcpu)) { + struct amd_iommu_pi_data pi; =20 - if (guest_irq >=3D irq_rt->nr_rt_entries || - hlist_empty(&irq_rt->map[guest_irq])) { - pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", - guest_irq, irq_rt->nr_rt_entries); - goto out; - } + enable_remapped_mode =3D false; =20 - hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { - struct vcpu_data vcpu_info; - struct vcpu_svm *svm =3D NULL; - - if (e->type !=3D KVM_IRQ_ROUTING_MSI) - continue; - - WARN_ON_ONCE(new && memcmp(e, new, sizeof(*new))); + /* + * Try to enable guest_mode in IRTE. Note, the address + * of the vCPU's AVIC backing page is passed to the + * IOMMU via vcpu_info->pi_desc_addr. + */ + pi.ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, + svm->vcpu.vcpu_id); + pi.is_guest_mode =3D true; + pi.vcpu_data =3D &vcpu_info; + ret =3D irq_set_vcpu_affinity(host_irq, &pi); =20 /** - * Here, we setup with legacy mode in the following cases: - * 1. When cannot target interrupt to a specific vcpu. - * 2. Unsetting posted interrupt. - * 3. APIC virtualization is disabled for the vcpu. - * 4. IRQ has incompatible delivery mode (SMI, INIT, etc) + * Here, we successfully setting up vcpu affinity in + * IOMMU guest mode. Now, we need to store the posted + * interrupt information in a per-vcpu ir_list so that + * we can reference to them directly when we update vcpu + * scheduling information in IOMMU irte. */ - if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set && - kvm_vcpu_apicv_active(&svm->vcpu)) { - struct amd_iommu_pi_data pi; - - enable_remapped_mode =3D false; - - /* - * Try to enable guest_mode in IRTE. Note, the address - * of the vCPU's AVIC backing page is passed to the - * IOMMU via vcpu_info->pi_desc_addr. - */ - pi.ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, - svm->vcpu.vcpu_id); - pi.is_guest_mode =3D true; - pi.vcpu_data =3D &vcpu_info; - ret =3D irq_set_vcpu_affinity(host_irq, &pi); - - /** - * Here, we successfully setting up vcpu affinity in - * IOMMU guest mode. Now, we need to store the posted - * interrupt information in a per-vcpu ir_list so that - * we can reference to them directly when we update vcpu - * scheduling information in IOMMU irte. - */ - if (!ret && pi.is_guest_mode) - svm_ir_list_add(svm, irqfd, &pi); - } - - if (!ret && svm) { - trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id, - e->gsi, vcpu_info.vector, - vcpu_info.pi_desc_addr, set); - } - - if (ret < 0) { - pr_err("%s: failed to update PI IRTE\n", __func__); - goto out; - } + if (!ret) + ret =3D svm_ir_list_add(svm, irqfd, &pi); + } + + if (!ret && svm) { + trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id, + guest_irq, vcpu_info.vector, + vcpu_info.pi_desc_addr, !!new); + } + + if (ret < 0) { + pr_err("%s: failed to update PI IRTE\n", __func__); + goto out; } =20 if (enable_remapped_mode) @@ -932,7 +912,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, else ret =3D 0; out: - srcu_read_unlock(&kvm->irq_srcu, idx); return ret; } =20 --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29852276031 for ; Fri, 23 May 2025 01:00:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962049; cv=none; b=VvqZeqDGY6AMJh1jsMTWv8mvUmA/OQZqv2S4CWHqoqarwbgca3ACLaTgYltOZKsB/KFig2YgjdC98TqM6swvWHD5ALE5xHQFPKr2yPXxeUMB05XLuHSJMQUT7pTdj/CRq/UQluZPjXaeQQ2q1b2PfMGuOfc1V/xATBQ3+b7pGg0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962049; c=relaxed/simple; bh=srDOFDWE3P2VL5WlCjbeOnuh/V7VTaVfjbBhk5s85qM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nR9Igse/uOG3M9hCA2b9KPKZMnBvtkHqUTpwdD255z063yf1f1aWwJF/aa4Lp1Wrr9IwiC+XlYzeVgc74x3DhZH70B4OvNg5UuSKa0ulTwNQhsqPkrsCGOTaJ6osi48kD+hX3doXpgslHJpLUt7SjVSNEfrfJW4nlNWQMFQDL+g= 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=qvUOvEpZ; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qvUOvEpZ" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30eac9886ffso4645451a91.1 for ; Thu, 22 May 2025 18:00:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962047; x=1748566847; 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=9WMm03XHcPOkUVkdGVy3jRNjqSu09sZJKxdu9rxT7Kc=; b=qvUOvEpZgcvx7s62vrpGyL8gYM8IO8D4p/uexWDl5JHKV9N/CTzNOzT37LxakXK9mm 1XDQR792VAcelgDMWCNVqOYVuuz0akBo6IDtf7hJ4rSybe2uqY8Jlf0m5R1nVPrznGmT ZR1aGcM8cn2ba01KvbKO0TrGWWe9zS3afnnkc9cb/PsoE4HGIyEzYjkI5vXEffHTgrPD D1UpdJhVFFrpdNTXGnnMnHiz5v8WRt2+c5HWTTSKnB1iTcGYtOlK9GgqvbeVkhjMHI9J KuRnMWRuqExqIa9HsejTpbuyHQEVgAHi2xB6L42lgCiSFEdsK3G116bTJqu9dTrpybCD 2o8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962047; x=1748566847; 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=9WMm03XHcPOkUVkdGVy3jRNjqSu09sZJKxdu9rxT7Kc=; b=KO2XjGdTx2ijeDVI+8paG1JKJVVZW4chONCBtJ0rbhhNopHJeJjrYqBKiL5g/PU9mO ILz5ocsfZs66tU/p9gabZep6u/MPn33iLWAX1HShuu5YFQRioioHshvHLWSGZ8rUzxp8 x/gEbH8U+BOcPsTFfFhBuMsXQImyDcBd12fW8p++E+kZbqfc3ct18lCQfg3STYnmcHJp uu0xzvi72LDbR37WR+RE34cFNtxpcOoW83XhR6IPYdprrv2AwXypVqCZruEqQ7BZjr6g JtDW3bIfeMDHKW+j/O/6CNqLpYxaLFEpmOJsjTn/zGkcz3cgqBJRTR4BnOtnYBQyg4OJ jTdQ== X-Forwarded-Encrypted: i=1; AJvYcCVgMkzwY7OP5ajcFAmSXq4HiOpvNlncjjg6a9jQzGLwClUfCOEx34P+kFiCzGKtVyUwAz24SNkAoRxVH/Q=@vger.kernel.org X-Gm-Message-State: AOJu0YyosEXzy/PV7e4iL2hbIaLSP6b/WkHH9i40+Hq4JwvCkcz1OSdP LLgEjrTPlNlo3ZsXM1BcMC5AqZJ4cgDr1gYLgebiRr1bsPAFGWJVUVgqeVsq3OVpKKNe+MinzMK CPz7uxg== X-Google-Smtp-Source: AGHT+IHScxVo8921NDXHfwYzbnuN/lZJlpUOiAfvprIuuohbUElxMDY5rmt5M3Lf2Xtm0bYMmcmAvG8AwgQ= X-Received: from pjbhl12.prod.google.com ([2002:a17:90b:134c:b0:30a:7da4:f075]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5143:b0:310:cf8f:6724 with SMTP id 98e67ed59e1d1-310e9740405mr1799767a91.30.1747962047581; Thu, 22 May 2025 18:00:47 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:27 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-23-seanjc@google.com> Subject: [PATCH v2 22/59] KVM: VMX: Stop walking list of routing table entries when updating IRTE From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that KVM provides the to-be-updated routing entry, stop walking the routing table to find that entry. KVM, via setup_routing_entry() and sanity checked by kvm_get_msi_route(), disallows having a GSI configured to trigger multiple MSIs, i.e. the for-loop can only process one entry. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/posted_intr.c | 100 +++++++++++---------------------- 1 file changed, 33 insertions(+), 67 deletions(-) diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index d4826a6b674f..e59eae11f476 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -302,78 +302,44 @@ int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, struct kvm_kernel_irq_routing_entry *new) { - struct kvm_kernel_irq_routing_entry *e; - struct kvm_irq_routing_table *irq_rt; - bool enable_remapped_mode =3D true; struct kvm_lapic_irq irq; struct kvm_vcpu *vcpu; struct vcpu_data vcpu_info; - bool set =3D !!new; - int idx, ret =3D 0; =20 if (!vmx_can_use_vtd_pi(kvm)) return 0; =20 - idx =3D srcu_read_lock(&kvm->irq_srcu); - irq_rt =3D srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); - if (guest_irq >=3D irq_rt->nr_rt_entries || - hlist_empty(&irq_rt->map[guest_irq])) { - pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", - guest_irq, irq_rt->nr_rt_entries); - goto out; - } - - hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { - if (e->type !=3D KVM_IRQ_ROUTING_MSI) - continue; - - WARN_ON_ONCE(new && memcmp(e, new, sizeof(*new))); - - /* - * VT-d PI cannot support posting multicast/broadcast - * interrupts to a vCPU, we still use interrupt remapping - * for these kind of interrupts. - * - * For lowest-priority interrupts, we only support - * those with single CPU as the destination, e.g. user - * configures the interrupts via /proc/irq or uses - * irqbalance to make the interrupts single-CPU. - * - * We will support full lowest-priority interrupt later. - * - * In addition, we can only inject generic interrupts using - * the PI mechanism, refuse to route others through it. - */ - - kvm_set_msi_irq(kvm, e, &irq); - if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) || - !kvm_irq_is_postable(&irq)) - continue; - - vcpu_info.pi_desc_addr =3D __pa(vcpu_to_pi_desc(vcpu)); - vcpu_info.vector =3D irq.vector; - - trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, - vcpu_info.vector, vcpu_info.pi_desc_addr, set); - - if (!set) - continue; - - enable_remapped_mode =3D false; - - ret =3D irq_set_vcpu_affinity(host_irq, &vcpu_info); - if (ret < 0) { - printk(KERN_INFO "%s: failed to update PI IRTE\n", - __func__); - goto out; - } - } - - if (enable_remapped_mode) - ret =3D irq_set_vcpu_affinity(host_irq, NULL); - - ret =3D 0; -out: - srcu_read_unlock(&kvm->irq_srcu, idx); - return ret; + /* + * VT-d PI cannot support posting multicast/broadcast + * interrupts to a vCPU, we still use interrupt remapping + * for these kind of interrupts. + * + * For lowest-priority interrupts, we only support + * those with single CPU as the destination, e.g. user + * configures the interrupts via /proc/irq or uses + * irqbalance to make the interrupts single-CPU. + * + * We will support full lowest-priority interrupt later. + * + * In addition, we can only inject generic interrupts using + * the PI mechanism, refuse to route others through it. + */ + if (!new || new->type !=3D KVM_IRQ_ROUTING_MSI) + goto do_remapping; + + kvm_set_msi_irq(kvm, new, &irq); + + if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) || + !kvm_irq_is_postable(&irq)) + goto do_remapping; + + vcpu_info.pi_desc_addr =3D __pa(vcpu_to_pi_desc(vcpu)); + vcpu_info.vector =3D irq.vector; + + trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, guest_irq, + vcpu_info.vector, vcpu_info.pi_desc_addr, true); + + return irq_set_vcpu_affinity(host_irq, &vcpu_info); +do_remapping: + return irq_set_vcpu_affinity(host_irq, NULL); } --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 314D3278772 for ; Fri, 23 May 2025 01:00:49 +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=1747962051; cv=none; b=mPmCl/pqy4lxwmmf2jroN2QnMuibPGsszcV3HeZm3pjWNmw90WDIsehZJs7oszXvxmg/ifruz+/L7nJR8/AAOdHA3OXrf84PAdv/rM0PHdBzJlTr7/NeX0QmeROJrnezv15rG5FceJIdL5B077c+VWciGcmfqfSUKH1lVfY/jnI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962051; c=relaxed/simple; bh=MZyvPz8cErBI6x41AwT2Y1YCOuXjfCDnsgc3RV5iM38=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WdxPvC4jKX/e/EC2Oxxg+7AympwkmDdeApjW3hW6RadFz7wTM4Qv9urQ3DEXQs21EwIDBKS2pZIg1gtIy16GmZtBVrHC6nEnemUdIkjzy8x640GcVGeStuS10ZmWHE+Wc2PLS4jOl6VJzDPx2MypvjJPeN/L9yhwYrCn7Qgw//g= 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=K0ujhG1S; 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="K0ujhG1S" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b269789425bso8820884a12.0 for ; Thu, 22 May 2025 18:00:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962049; x=1748566849; 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=/QbRbxgKq4IX+c52p+RG6yGaQTEP98D8xjd3vBs8mBU=; b=K0ujhG1SqVJkC06tHo3v2JZ43Mlov9RveM4XCv0x9bQUKLnOeci3J8s4lC+IFM6Nrq TcSv+yikNA1BHDhErtCqC8jeYRlZSVF46GCOwZCo5jw/5Uj6dt86oM1mOEcUdSLwVTW6 90O9e70/6as28DNOIPZaPIjTLZbNfihvnTkosArskBf3b6mMMhV3k2lzsVui/jRgLVun ui9Eaw9GjpnAdhqIdGi9LkKBPAeHGkWaYS2vvIFLHbQS5dZfO2uPkmN6Nw8PhdRPYexD dgqY5C/GXRzOQcCRdXn1jig8PCqzWPYdegbOJL5UvLwKx0BXfQLXIWLKobNWs97qUQPN q9eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962049; x=1748566849; 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=/QbRbxgKq4IX+c52p+RG6yGaQTEP98D8xjd3vBs8mBU=; b=wst7zPiRyLN1Dq/bguh6Mr/Wy6GfTM+m/9vo0XVlPFGDilDjcA4gl+fOjrrZ/Lov9H PC0U2Qt82QbnsVPmJeycDs39TCF/IJ9BkHvy6liiHdpiTtr/ZloCdKyMWvMIcFlZnqG5 LmTyPFtM3pud1M+fphjOffniS9Veg6FsjI/N9L0DiaWyrvDjBNtGG9SNBg9z5pv9Zz5C cJKvln7l7nXxBEQqpt2QtbaPDcCYcfNCQj4y9Vg7fEfr1BQc8sJvJJdnFU8WdMdKuRoP vXAZFzTevqE4+cGyWHn8SdW/a5EzCwHsqZQDJeSvUTh4ZxZ3bPImVjp2ZfIPrqzh0DgI 4c8g== X-Forwarded-Encrypted: i=1; AJvYcCW5Pdz2bK1k9S3sKDgEuYTPZXPp+MMN0sodo86AqbPYK+hkrmYlJiez1bdIUg8DBp1CKx4NJy4Blx9AYbo=@vger.kernel.org X-Gm-Message-State: AOJu0Ywy+uwGVpRwwO4CgPEYpR/diaLlhHYkzVR33UwydKojXVIlI8fX FG0ueELqoMXMcLePXwpnJnaW7CktTAAVWlNl6VRc0fgkrF3uNpCXuO/5nJ1pghln3A7Ttde5XYh p4AoDkQ== X-Google-Smtp-Source: AGHT+IEoGfxyus4zW6bOW0Hkaw5yAvIEipFHaV1PyBgzP1+WYMZEtkQ5SFyRDx1RYRCGinQDFi3EhnerZLE= X-Received: from pjbpm5.prod.google.com ([2002:a17:90b:3c45:b0:30a:9720:ea33]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4fcd:b0:30e:9349:2d7f with SMTP id 98e67ed59e1d1-30e9349309dmr36081293a91.4.1747962049336; Thu, 22 May 2025 18:00:49 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:28 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-24-seanjc@google.com> Subject: [PATCH v2 23/59] KVM: SVM: Extract SVM specific code out of get_pi_vcpu_info() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Genericize SVM's get_pi_vcpu_info() so that it can be shared with VMX. The only SVM specific information it provides is the AVIC back page, and that can be trivially retrieved by its sole caller. No functional change intended. Cc: Francesco Lavra Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index a83769bb8123..3bbd565dcd0f 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -816,14 +816,14 @@ static int svm_ir_list_add(struct vcpu_svm *svm, */ static int get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e, - struct vcpu_data *vcpu_info, struct vcpu_svm **svm) + struct vcpu_data *vcpu_info, struct kvm_vcpu **vcpu) { struct kvm_lapic_irq irq; - struct kvm_vcpu *vcpu =3D NULL; + *vcpu =3D NULL; =20 kvm_set_msi_irq(kvm, e, &irq); =20 - if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) || + if (!kvm_intr_is_single_vcpu(kvm, &irq, vcpu) || !kvm_irq_is_postable(&irq)) { pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n", __func__, irq.vector); @@ -832,8 +832,6 @@ get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq= _routing_entry *e, =20 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 avic_get_backing_page_address(*svm); vcpu_info->vector =3D irq.vector; =20 return 0; @@ -845,7 +843,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, { bool enable_remapped_mode =3D true; struct vcpu_data vcpu_info; - struct vcpu_svm *svm =3D NULL; + struct kvm_vcpu *vcpu =3D NULL; int ret =3D 0; =20 if (!kvm_arch_has_assigned_device(kvm) || !kvm_arch_has_irq_bypass()) @@ -868,19 +866,20 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqf= d, struct kvm *kvm, * 4. IRQ has incompatible delivery mode (SMI, INIT, etc) */ if (new && new->type =3D=3D KVM_IRQ_ROUTING_MSI && - !get_pi_vcpu_info(kvm, new, &vcpu_info, &svm) && - kvm_vcpu_apicv_active(&svm->vcpu)) { + !get_pi_vcpu_info(kvm, new, &vcpu_info, &vcpu) && + kvm_vcpu_apicv_active(vcpu)) { struct amd_iommu_pi_data pi; =20 enable_remapped_mode =3D false; =20 + vcpu_info.pi_desc_addr =3D avic_get_backing_page_address(to_svm(vcpu)); + /* * Try to enable guest_mode in IRTE. Note, the address * of the vCPU's AVIC backing page is passed to the * IOMMU via vcpu_info->pi_desc_addr. */ - pi.ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, - svm->vcpu.vcpu_id); + pi.ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id); pi.is_guest_mode =3D true; pi.vcpu_data =3D &vcpu_info; ret =3D irq_set_vcpu_affinity(host_irq, &pi); @@ -893,11 +892,11 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqf= d, struct kvm *kvm, * scheduling information in IOMMU irte. */ if (!ret) - ret =3D svm_ir_list_add(svm, irqfd, &pi); + ret =3D svm_ir_list_add(to_svm(vcpu), irqfd, &pi); } =20 - if (!ret && svm) { - trace_kvm_pi_irte_update(host_irq, svm->vcpu.vcpu_id, + if (!ret && vcpu) { + trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, guest_irq, vcpu_info.vector, vcpu_info.pi_desc_addr, !!new); } --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B59102798E2 for ; Fri, 23 May 2025 01:00:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962054; cv=none; b=tRxQqxO/t72DKslhZpFAVDKt/cW7nzvrwQJ4NskLlILzaCpfWwYlIOIT3YwbnBGwUIcbBqW+zcDi2YnDl37Hu+TgsM4lHWbU48bru4qEIh9oDYMMnM4Oc61A1Sq4eNo54Dogo8soJwloyuITzVcnxJRRe/uIjz7QhtPvsR1OaD4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962054; c=relaxed/simple; bh=CXftmt6Rv0xuDp1ncXMCQ2Iq5vWS80lFiGXqp9hH0Wk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hYEcrx+ss0iy+odXBcOlyvP593+OV+V3pzZm8fWBkT6tIRgBkX3hcUfzO00PjTNNXAh61CXGB6mPjVeGqtyrjE8alNNdZnOHN/PFDpD9tELCmfNDP7HwZnd41OTwYv7wgXzVevC53XezAP4oS61oEXUSKU4jjso/phTChhF+gN4= 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=vNYgxjyV; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vNYgxjyV" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3108d5156e8so3442097a91.1 for ; Thu, 22 May 2025 18:00:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962051; x=1748566851; 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=rKGx/dcplKQAs++4ShZiLUsnF0fAAm6Wg1gHjnqOhAE=; b=vNYgxjyV9URebtAUlmMNdEzdVaZGYANMH5YEAN5FIpJFURnQ4NCjl4EoKOba5zvEWj UBeJaDNSwQIDLpTQ+z2suP1VCkngXcWJw5DbH6Yb6ny3W2cLbE4SRL8CV/r+33vqACYK urvJEjIynEK6j9TsinKx7h+06Ny+Xkl5ZP/yxG6e3uojjOoPwvHLE9kEX8XQ8f9cMocq xa1BpEVoPjgR3b9m7tbzL1axNm0gDJcYxTov8ehl9SBpy/Oxh7QSEydG/6fMq+zbM4SY QbFDgzbZfR1xPR7f3KJ2I01q8US5swM/lfe1tPq6oyDVApCQB6gZpz4ZaNGcheZzSVtb S2BA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962051; x=1748566851; 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=rKGx/dcplKQAs++4ShZiLUsnF0fAAm6Wg1gHjnqOhAE=; b=AuS+s3mE2DH8fWxzNzb4XUmUaQ9FkI4XbARGmoWpcbSuU9J8nyqSwBSiLVuRW60P3Z 6GAw5gCgHZfBhsSMJEjvYE4VYk5JG4yRW8MvzrR+MwFsJ8neFa1LAFLe8J10QEOdev9z zu1evWEZelHll0IIOAlCF9B52ayyF8ccPNYbcBKBO8DydFm6A7LAR/SGea5lzJiF8JpD fB7JcIQQL7u8CVLQMHX+I8agsWyJzfsrxC5dP2A23X5o8wJNMXI5V4rC82ymrnrZzfd/ f5K3K4z3vS18ANHYPAv6IqUg7VusQc3LhPDfaqD79XU24ecjSXstEmP3xboNgD7y9djk bbvg== X-Forwarded-Encrypted: i=1; AJvYcCW0pXauYHGWiFNdgTbTsR6tbgn5QN6St8+oYrngVmRGSFrS5LLLbuQhfmnHtR6jvLic6Mp1XWrLgggj640=@vger.kernel.org X-Gm-Message-State: AOJu0YwlQpX0y5nVfaLv0E+tcsv/RJT9P/bHsCKtob8XPuDkXMbBnidf hapHks4Wnqm/S31C8sAX8btLKskk/fOawTxhM2qUTXCGELYsY/Hg806ZXs9NcNzWTYqor3LR5zY +pjKV7A== X-Google-Smtp-Source: AGHT+IG0lW4uEVpvpd+X7AgmnpqUOdNQN1x4hf8EtHj36zwvoU1Ib5gejTv/GUolVnEbYv2D7W5sxDw/nR8= X-Received: from pjbpm9.prod.google.com ([2002:a17:90b:3c49:b0:308:64af:7bb9]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:54ce:b0:30e:8c5d:8e4 with SMTP id 98e67ed59e1d1-310e96e73c8mr2089364a91.16.1747962051165; Thu, 22 May 2025 18:00:51 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:29 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-25-seanjc@google.com> Subject: [PATCH v2 24/59] KVM: x86: Move IRQ routing/delivery APIs from x86.c => irq.c From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move a bunch of IRQ routing and delivery APIs from x86.c to irq.c. x86.c has grown quite fat, and irq.c is the perfect landing spot. Opportunistically rewrite kvm_arch_irq_bypass_del_producer()'s comment, as the existing comment has several typos and is rather confusing. Suggested-by: Paolo Bonzini Signed-off-by: Sean Christopherson --- arch/x86/kvm/irq.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 87 --------------------------------------------- 2 files changed, 88 insertions(+), 87 deletions(-) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index 314a93599942..3f75b8130c3b 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -11,6 +11,7 @@ =20 #include #include +#include =20 #include "hyperv.h" #include "ioapic.h" @@ -332,6 +333,18 @@ int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_ro= uting_entry *e, return -EWOULDBLOCK; } =20 +int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, + bool line_status) +{ + if (!irqchip_in_kernel(kvm)) + return -ENXIO; + + irq_event->status =3D kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, + irq_event->irq, irq_event->level, + line_status); + return 0; +} + bool kvm_arch_can_set_irq_routing(struct kvm *kvm) { return irqchip_in_kernel(kvm); @@ -494,3 +507,78 @@ void kvm_arch_irq_routing_update(struct kvm *kvm) if (irqchip_split(kvm)) kvm_make_scan_ioapic_request(kvm); } + +int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, + struct irq_bypass_producer *prod) +{ + struct kvm_kernel_irqfd *irqfd =3D + container_of(cons, struct kvm_kernel_irqfd, consumer); + struct kvm *kvm =3D irqfd->kvm; + int ret =3D 0; + + kvm_arch_start_assignment(irqfd->kvm); + + spin_lock_irq(&kvm->irqfds.lock); + irqfd->producer =3D prod; + + if (irqfd->irq_entry.type =3D=3D KVM_IRQ_ROUTING_MSI) { + ret =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, + irqfd->gsi, &irqfd->irq_entry); + if (ret) + kvm_arch_end_assignment(irqfd->kvm); + } + spin_unlock_irq(&kvm->irqfds.lock); + + return ret; +} + +void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, + struct irq_bypass_producer *prod) +{ + struct kvm_kernel_irqfd *irqfd =3D + container_of(cons, struct kvm_kernel_irqfd, consumer); + struct kvm *kvm =3D irqfd->kvm; + int ret; + + WARN_ON(irqfd->producer !=3D prod); + + /* + * If the producer of an IRQ that is currently being posted to a vCPU + * is unregistered, change the associated IRTE back to remapped mode as + * the IRQ has been released (or repurposed) by the device driver, i.e. + * KVM must relinquish control of the IRTE. + */ + spin_lock_irq(&kvm->irqfds.lock); + irqfd->producer =3D NULL; + + if (irqfd->irq_entry.type =3D=3D KVM_IRQ_ROUTING_MSI) { + ret =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, + irqfd->gsi, NULL); + if (ret) + pr_info("irq bypass consumer (token %p) unregistration fails: %d\n", + irqfd->consumer.token, ret); + } + + spin_unlock_irq(&kvm->irqfds.lock); + + + kvm_arch_end_assignment(irqfd->kvm); +} + +int kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, + struct kvm_kernel_irq_routing_entry *old, + struct kvm_kernel_irq_routing_entry *new) +{ + return kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, irqfd->producer->i= rq, + irqfd->gsi, new); +} + +bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *old, + struct kvm_kernel_irq_routing_entry *new) +{ + if (old->type !=3D KVM_IRQ_ROUTING_MSI || + new->type !=3D KVM_IRQ_ROUTING_MSI) + return true; + + return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); +} diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b645ccda0999..a9900c246bb3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6549,18 +6549,6 @@ void kvm_arch_sync_dirty_log(struct kvm *kvm, struct= kvm_memory_slot *memslot) kvm_vcpu_kick(vcpu); } =20 -int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, - bool line_status) -{ - if (!irqchip_in_kernel(kvm)) - return -ENXIO; - - irq_event->status =3D kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, - irq_event->irq, irq_event->level, - line_status); - return 0; -} - int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) { @@ -13627,81 +13615,6 @@ bool kvm_arch_has_noncoherent_dma(struct kvm *kvm) } EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma); =20 -int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, - struct irq_bypass_producer *prod) -{ - struct kvm_kernel_irqfd *irqfd =3D - container_of(cons, struct kvm_kernel_irqfd, consumer); - struct kvm *kvm =3D irqfd->kvm; - int ret =3D 0; - - kvm_arch_start_assignment(irqfd->kvm); - - spin_lock_irq(&kvm->irqfds.lock); - irqfd->producer =3D prod; - - if (irqfd->irq_entry.type =3D=3D KVM_IRQ_ROUTING_MSI) { - ret =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, - irqfd->gsi, &irqfd->irq_entry); - if (ret) - kvm_arch_end_assignment(irqfd->kvm); - } - spin_unlock_irq(&kvm->irqfds.lock); - - return ret; -} - -void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, - struct irq_bypass_producer *prod) -{ - struct kvm_kernel_irqfd *irqfd =3D - container_of(cons, struct kvm_kernel_irqfd, consumer); - struct kvm *kvm =3D irqfd->kvm; - int ret; - - WARN_ON(irqfd->producer !=3D prod); - - /* - * When producer of consumer is unregistered, we change back to - * remapped mode, so we can re-use the current implementation - * when the irq is masked/disabled or the consumer side (KVM - * int this case doesn't want to receive the interrupts. - */ - spin_lock_irq(&kvm->irqfds.lock); - irqfd->producer =3D NULL; - - if (irqfd->irq_entry.type =3D=3D KVM_IRQ_ROUTING_MSI) { - ret =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, - irqfd->gsi, NULL); - if (ret) - pr_info("irq bypass consumer (token %p) unregistration fails: %d\n", - irqfd->consumer.token, ret); - } - - spin_unlock_irq(&kvm->irqfds.lock); - - - kvm_arch_end_assignment(irqfd->kvm); -} - -int kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, - struct kvm_kernel_irq_routing_entry *old, - struct kvm_kernel_irq_routing_entry *new) -{ - return kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, irqfd->producer->i= rq, - irqfd->gsi, new); -} - -bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *old, - struct kvm_kernel_irq_routing_entry *new) -{ - if (old->type !=3D KVM_IRQ_ROUTING_MSI || - new->type !=3D KVM_IRQ_ROUTING_MSI) - return true; - - return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); -} - bool kvm_vector_hashing_enabled(void) { return vector_hashing; --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2293D279785 for ; Fri, 23 May 2025 01:00:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962054; cv=none; b=NRoSl7EMUISFMtPrWUnttGBqFmLw6wArauRua3/vZQuksvbpqayzsh+xiKvDNvTFxOgtu8DT6G20k35qJTpJkGT47gY7LuSTFpMCs3jvjmGmtZ03oKGy/ruDVVJ1blHjqXwTvVPcduLBQFAOU50VsKtoRHY1DgHKmYhkuHxdU3Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962054; c=relaxed/simple; bh=jRCSdoFCqMKn8jTYzEDiHtmp4OccuyujV8O7UeMdZj8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TuvvL/qCK3R1pmrrCpPQkup2AG7qNgI/aErwMzmrpORDXNevwvcL1Mc30PtVp8ogecSmxZpsNjDf4fuzSUTo+sd2n0qNJvnLOM7Mhic7qFenPM4AoCGK0Pf9ZmaivzYUtL2kDLa9RU8BWy91POz0p6E5QrPv5OoJEA9rbfTijS4= 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=PoTeoETN; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PoTeoETN" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3108d5123c4so2086981a91.1 for ; Thu, 22 May 2025 18:00:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962052; x=1748566852; 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=hG0IAIiIYv3kAH9AK2gcTVyZB3y4/6He2P464UJI1dU=; b=PoTeoETNIZn/jFu2Z/A0950v5cB0UiJsdtO+MNm0fWKhd90kh1aNyN44l0SpEJ+2Zc 37HZCnDJCch1jiLW96Zykho0/BWFY5Pu6TD3309kpLarteT/UyeoeeBSrcF845+HACu2 mMJ1EN9o6ImH7li3fsgUpqd1NYZUmo9eRE3abMG2ltU+EIzwu+yE2XfsU6VUFDOr9+Kg IkeT71cHZGZrvuVqgeHYdgXnHE3/SBF7aV2zv4PqGCiylpmRdsQpEoKH4ftVapOgGsvM wkTqt5E1tBYuJp6wQELonTGvPeJHrk5/RciYq+wV2fXXPoL7OjwY8G6E4dKdS9T7n8Nz 4OdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962053; x=1748566853; 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=hG0IAIiIYv3kAH9AK2gcTVyZB3y4/6He2P464UJI1dU=; b=NjZ16MfmB7ubJd5vIzHzJkXLGPNl448nHGEj7bcTAJR8MCMBApYERwWAbKtIJrSwn1 09hF6bIjH6bl8X8slVO3CRvwHv08mzgGN6NfORHs5HlAr3JisaQ/DYCLyHT3E8BKjAbN dN7hvjXNdzCRWHwOp2LUG+bH3PEHLJQEf6YxK6iTXUAn++FusLS9LSqv1V087u5vdw2A kFhHuky16zBYheBn6IWVoKUlVmhZ+F7sXAgijk0MIQREiqL/P5+kZTpv1ge2Qhmmgyu8 TUpdLE2ZmkfjbkEGojcDlfUU5GDBN45X4tXhZj1fUyx07IB1vveoyeRvXqHtTI/lxtCz A8QA== X-Forwarded-Encrypted: i=1; AJvYcCUAkhaSJe1ehfRx86Goj7GTunYvJp621PPntf1XUDoYdwjN/0gc3JDyjnG10L+m3/7ji7acmuQtfGfPqus=@vger.kernel.org X-Gm-Message-State: AOJu0Yw5EQyc2aRD9fO7YHeIu2xOdtkoOmaQaWqqQP23V+DrJBCcdL0l sHj93U7l/nLPhEWohjXeof/+YDvNupec/JlXy/Tr+UYu+/d/f03k/uRnDON5QP6mUhmaaf2Bw61 A8xZwNw== X-Google-Smtp-Source: AGHT+IGmy5pPbJfwDE9rf/Cqih7MqBWbBVfwVzubGvYONxB6zdFJ/6ZU0L0cNDAY5jG/p/nKPtIes2/D6Og= X-Received: from pjb6.prod.google.com ([2002:a17:90b:2f06:b0:2f8:49ad:406c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:57e8:b0:30a:255c:9d10 with SMTP id 98e67ed59e1d1-30e830e87f6mr38952255a91.8.1747962052715; Thu, 22 May 2025 18:00:52 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:30 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-26-seanjc@google.com> Subject: [PATCH v2 25/59] KVM: x86: Nullify irqfd->producer after updating IRTEs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Nullify irqfd->producer (when it's going away) _after_ updating IRTEs so that the producer can be queried during the update. Signed-off-by: Sean Christopherson --- arch/x86/kvm/irq.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index 3f75b8130c3b..6374a7cf8664 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -549,7 +549,6 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass= _consumer *cons, * KVM must relinquish control of the IRTE. */ spin_lock_irq(&kvm->irqfds.lock); - irqfd->producer =3D NULL; =20 if (irqfd->irq_entry.type =3D=3D KVM_IRQ_ROUTING_MSI) { ret =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, @@ -558,10 +557,10 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypa= ss_consumer *cons, pr_info("irq bypass consumer (token %p) unregistration fails: %d\n", irqfd->consumer.token, ret); } + irqfd->producer =3D NULL; =20 spin_unlock_irq(&kvm->irqfds.lock); =20 - kvm_arch_end_assignment(irqfd->kvm); } =20 --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 14EC227A91F for ; Fri, 23 May 2025 01:00:54 +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=1747962057; cv=none; b=a5mzW9Usqo3JVUI8MoYQgSK/XItLrvYtF/Zs85IUIv2zvt1RogHRiz+YxVeVhnnXYVLa/CUFkJPaBHRP/J+ngfCk05po7CPwgHuPRszVOqTbqLJX88k87WnBlsQFvPUpHMzbbWMb4hEgKYL2FCBMZCxtoIfW1JjKbGM8uNDM3f4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962057; c=relaxed/simple; bh=reLr9P58MSC3wjP35aUgGXuVRmf102/ZPKYE8/pzV1Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=B0loZk+CiJkk8PA3uXMbUEqoAPTQ7n4aZKqb5uh+0tFdh69/51R2Cr0KMGFCtWJoCEkpcI6KngiUFrP9mkiAP/Y5Ar5M/EtTm0GVWKaQNwV6p3ZCWjf8v7V2VTM+2OkW2u2iYL+evRGSNje+kRSmzZuUOAydxQx5HZatLLk8duI= 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=kceeTXCb; 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="kceeTXCb" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b115fb801bcso9188501a12.3 for ; Thu, 22 May 2025 18:00:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962054; x=1748566854; 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=FVhvnc/YpYZobtQ9o9NxPodzh+EhVjLEhzCkGWjABzI=; b=kceeTXCbNyPoTFInl4I1zdwoCOTftghsrFOSV8kViisPRdn7pfSflNqro3t3/IAvpF HjFuC1B7IP/Tl3rD6K4VfBe30QjOUJBH6Vz69/HQme1hstX6sIHCpL7G8JXm5SEXwbVj gK51l35W09tlYRf7iSaW3GIQz/gqh1dpS+WqsBCSec7gfj8XmTPhNS4d8mrn08OyoeXZ z8FJkSuvooCcK384TG/z3dssHSgkLJiKRr7g4B0e5hLqfyIvxQRxcNvHxXH6+saKVkZk bBB/oZsJKm9/cI0paexlud3SalC+5l8HmitcOkrfn3jw0MQxhY3Fv0yDFGbcuUlZhDyG zXHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962054; x=1748566854; 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=FVhvnc/YpYZobtQ9o9NxPodzh+EhVjLEhzCkGWjABzI=; b=fk44b/xDvBlq7brEF4mdS46WQhdNs2GswHZ/RzsB7KzIW44Nsn3wHci63Q+VWqD9dX W4ueljNstqHjad5OQCtzrdqlHKXFSlRt4deRyyI39aQxri27Epq06AVTx2EmeNmhrrRn ioM5CG3jz5eHvSUZjFpweNaGxmga2c2Ce5F8U/6ZKJC0i7m0m0pRnvm+ErwqwO4u6wpj vsRAvf/a1yS3qDDfv4W98kJPNUqwWfLYGHIZpCwZgkN3k5YwYQ8L295ohqGiRL01Q5CU V6V844ki3066nMcyd3HRGACcYxcl850Ci+mmz1HOAGSmUSeqYuLu0StyL4ZacROi+Ox5 gmVg== X-Forwarded-Encrypted: i=1; AJvYcCWx4o8YfsnGvJcO6O6s/LeSENgN7B38/Nnblejb6XK5pysFVMbDOhvlp06bXG7gCN+gVy5k4Efd2NZT+aE=@vger.kernel.org X-Gm-Message-State: AOJu0YxqaCaJfPAIUofyxYudzXHj5bG45l2HMUbvGzZTLESUSAYDr9q4 /qqQvb0zzfo2rr9ViwW+u9XfV01AECKGbe9eoqwlLR9x7rpIcqjg3tYvN+ZCmK7dQAwQl9FaSUn 9MKSVzw== X-Google-Smtp-Source: AGHT+IFnoLSV725ynjIxwWXrWpZxFAUy0Evtj55LS8rKIxU1+INdg2aBUchusqAm0MknijR/iiZNe/0+4Ik= X-Received: from pjbpl16.prod.google.com ([2002:a17:90b:2690:b0:30e:7003:7604]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:28c5:b0:2fe:a79e:f56f with SMTP id 98e67ed59e1d1-30e7d522165mr38581496a91.13.1747962054379; Thu, 22 May 2025 18:00:54 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:31 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-27-seanjc@google.com> Subject: [PATCH v2 26/59] KVM: x86: Dedup AVIC vs. PI code for identifying target vCPU From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hoist the logic for identifying the target vCPU for a posted interrupt into common x86. The code is functionally identical between Intel and AMD. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/irq.c | 45 +++++++++++++++--- arch/x86/kvm/svm/avic.c | 82 ++++++++------------------------- arch/x86/kvm/svm/svm.h | 2 +- arch/x86/kvm/vmx/posted_intr.c | 55 ++++++---------------- arch/x86/kvm/vmx/posted_intr.h | 2 +- 6 files changed, 75 insertions(+), 113 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index cba82d7a701d..c722adfedd96 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1854,7 +1854,7 @@ struct kvm_x86_ops { =20 int (*pi_update_irte)(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, - struct kvm_kernel_irq_routing_entry *new); + struct kvm_vcpu *vcpu, u32 vector); void (*pi_start_assignment)(struct kvm *kvm); void (*apicv_pre_state_restore)(struct kvm_vcpu *vcpu); void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index 6374a7cf8664..92a2137e0402 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -508,6 +508,42 @@ void kvm_arch_irq_routing_update(struct kvm *kvm) kvm_make_scan_ioapic_request(kvm); } =20 +static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd, + struct kvm_kernel_irq_routing_entry *entry) +{ + struct kvm *kvm =3D irqfd->kvm; + struct kvm_vcpu *vcpu =3D NULL; + struct kvm_lapic_irq irq; + + if (!irqchip_in_kernel(kvm) || + !kvm_arch_has_irq_bypass() || + !kvm_arch_has_assigned_device(kvm)) + return 0; + + if (entry && entry->type =3D=3D KVM_IRQ_ROUTING_MSI) { + kvm_set_msi_irq(kvm, entry, &irq); + + /* + * Force remapped mode if hardware doesn't support posting the + * virtual interrupt to a vCPU. Only IRQs are postable (NMIs, + * SMIs, etc. are not), and neither AMD nor Intel IOMMUs support + * posting multicast/broadcast IRQs. If the interrupt can't be + * posted, the device MSI needs to be routed to the host so that + * the guest's desired interrupt can be synthesized by KVM. + * + * This means that KVM can only post lowest-priority interrupts + * if they have a single CPU as the destination, e.g. only if + * the guest has affined the interrupt to a single vCPU. + */ + if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) || + !kvm_irq_is_postable(&irq)) + vcpu =3D NULL; + } + + return kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, irqfd->producer->i= rq, + irqfd->gsi, vcpu, irq.vector); +} + int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, struct irq_bypass_producer *prod) { @@ -522,8 +558,7 @@ int kvm_arch_irq_bypass_add_producer(struct irq_bypass_= consumer *cons, irqfd->producer =3D prod; =20 if (irqfd->irq_entry.type =3D=3D KVM_IRQ_ROUTING_MSI) { - ret =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, - irqfd->gsi, &irqfd->irq_entry); + ret =3D kvm_pi_update_irte(irqfd, &irqfd->irq_entry); if (ret) kvm_arch_end_assignment(irqfd->kvm); } @@ -551,8 +586,7 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass= _consumer *cons, spin_lock_irq(&kvm->irqfds.lock); =20 if (irqfd->irq_entry.type =3D=3D KVM_IRQ_ROUTING_MSI) { - ret =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, - irqfd->gsi, NULL); + ret =3D kvm_pi_update_irte(irqfd, NULL); if (ret) pr_info("irq bypass consumer (token %p) unregistration fails: %d\n", irqfd->consumer.token, ret); @@ -568,8 +602,7 @@ int kvm_arch_update_irqfd_routing(struct kvm_kernel_irq= fd *irqfd, struct kvm_kernel_irq_routing_entry *old, struct kvm_kernel_irq_routing_entry *new) { - return kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, irqfd->producer->i= rq, - irqfd->gsi, new); + return kvm_pi_update_irte(irqfd, new); } =20 bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *old, diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 3bbd565dcd0f..14a1544af192 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -803,52 +803,12 @@ static int svm_ir_list_add(struct vcpu_svm *svm, return 0; } =20 -/* - * Note: - * The HW cannot support posting multicast/broadcast - * interrupts to a vCPU. So, we still use legacy interrupt - * remapping for these kind of interrupts. - * - * For lowest-priority interrupts, we only support - * those with single CPU as the destination, e.g. user - * configures the interrupts via /proc/irq or uses - * irqbalance to make the interrupts single-CPU. - */ -static int -get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e, - struct vcpu_data *vcpu_info, struct kvm_vcpu **vcpu) -{ - struct kvm_lapic_irq irq; - *vcpu =3D NULL; - - kvm_set_msi_irq(kvm, e, &irq); - - if (!kvm_intr_is_single_vcpu(kvm, &irq, vcpu) || - !kvm_irq_is_postable(&irq)) { - pr_debug("SVM: %s: use legacy intr remap mode for irq %u\n", - __func__, irq.vector); - return -1; - } - - pr_debug("SVM: %s: use GA mode for irq %u\n", __func__, - irq.vector); - vcpu_info->vector =3D irq.vector; - - return 0; -} - int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, - struct kvm_kernel_irq_routing_entry *new) + struct kvm_vcpu *vcpu, u32 vector) { - bool enable_remapped_mode =3D true; - struct vcpu_data vcpu_info; - struct kvm_vcpu *vcpu =3D NULL; int ret =3D 0; =20 - if (!kvm_arch_has_assigned_device(kvm) || !kvm_arch_has_irq_bypass()) - return 0; - /* * If the IRQ was affined to a different vCPU, remove the IRTE metadata * from the *previous* vCPU's list. @@ -856,7 +816,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, svm_ir_list_del(irqfd); =20 pr_debug("SVM: %s: host_irq=3D%#x, guest_irq=3D%#x, set=3D%#x\n", - __func__, host_irq, guest_irq, !!new); + __func__, host_irq, guest_irq, !!vcpu); =20 /** * Here, we setup with legacy mode in the following cases: @@ -865,23 +825,23 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqf= d, struct kvm *kvm, * 3. APIC virtualization is disabled for the vcpu. * 4. IRQ has incompatible delivery mode (SMI, INIT, etc) */ - if (new && new->type =3D=3D KVM_IRQ_ROUTING_MSI && - !get_pi_vcpu_info(kvm, new, &vcpu_info, &vcpu) && - kvm_vcpu_apicv_active(vcpu)) { - struct amd_iommu_pi_data pi; - - enable_remapped_mode =3D false; - - vcpu_info.pi_desc_addr =3D avic_get_backing_page_address(to_svm(vcpu)); - + if (vcpu && kvm_vcpu_apicv_active(vcpu)) { /* * Try to enable guest_mode in IRTE. Note, the address * of the vCPU's AVIC backing page is passed to the * IOMMU via vcpu_info->pi_desc_addr. */ - pi.ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id); - pi.is_guest_mode =3D true; - pi.vcpu_data =3D &vcpu_info; + struct vcpu_data vcpu_info =3D { + .pi_desc_addr =3D avic_get_backing_page_address(to_svm(vcpu)), + .vector =3D vector, + }; + + struct amd_iommu_pi_data pi =3D { + .ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id), + .is_guest_mode =3D true, + .vcpu_data =3D &vcpu_info, + }; + ret =3D irq_set_vcpu_affinity(host_irq, &pi); =20 /** @@ -893,12 +853,11 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqf= d, struct kvm *kvm, */ if (!ret) ret =3D svm_ir_list_add(to_svm(vcpu), irqfd, &pi); - } =20 - if (!ret && vcpu) { - trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, - guest_irq, vcpu_info.vector, - vcpu_info.pi_desc_addr, !!new); + trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, guest_irq, + vector, vcpu_info.pi_desc_addr, true); + } else { + ret =3D irq_set_vcpu_affinity(host_irq, NULL); } =20 if (ret < 0) { @@ -906,10 +865,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, goto out; } =20 - if (enable_remapped_mode) - ret =3D irq_set_vcpu_affinity(host_irq, NULL); - else - ret =3D 0; + ret =3D 0; out: return ret; } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 939ff0e35a2b..b5cd1927b009 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -747,7 +747,7 @@ void avic_apicv_post_state_restore(struct kvm_vcpu *vcp= u); void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu); int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, - struct kvm_kernel_irq_routing_entry *new); + struct kvm_vcpu *vcpu, u32 vector); void avic_vcpu_blocking(struct kvm_vcpu *vcpu); void avic_vcpu_unblocking(struct kvm_vcpu *vcpu); void avic_ring_doorbell(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index e59eae11f476..3de767c5d6b2 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -300,46 +300,19 @@ void vmx_pi_start_assignment(struct kvm *kvm) =20 int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, - struct kvm_kernel_irq_routing_entry *new) + struct kvm_vcpu *vcpu, u32 vector) { - struct kvm_lapic_irq irq; - struct kvm_vcpu *vcpu; - struct vcpu_data vcpu_info; - - if (!vmx_can_use_vtd_pi(kvm)) - return 0; - - /* - * VT-d PI cannot support posting multicast/broadcast - * interrupts to a vCPU, we still use interrupt remapping - * for these kind of interrupts. - * - * For lowest-priority interrupts, we only support - * those with single CPU as the destination, e.g. user - * configures the interrupts via /proc/irq or uses - * irqbalance to make the interrupts single-CPU. - * - * We will support full lowest-priority interrupt later. - * - * In addition, we can only inject generic interrupts using - * the PI mechanism, refuse to route others through it. - */ - if (!new || new->type !=3D KVM_IRQ_ROUTING_MSI) - goto do_remapping; - - kvm_set_msi_irq(kvm, new, &irq); - - if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) || - !kvm_irq_is_postable(&irq)) - goto do_remapping; - - vcpu_info.pi_desc_addr =3D __pa(vcpu_to_pi_desc(vcpu)); - vcpu_info.vector =3D irq.vector; - - trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, guest_irq, - vcpu_info.vector, vcpu_info.pi_desc_addr, true); - - return irq_set_vcpu_affinity(host_irq, &vcpu_info); -do_remapping: - return irq_set_vcpu_affinity(host_irq, NULL); + if (vcpu) { + struct vcpu_data vcpu_info =3D { + .pi_desc_addr =3D __pa(vcpu_to_pi_desc(vcpu)), + .vector =3D vector, + }; + + trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, guest_irq, + vcpu_info.vector, vcpu_info.pi_desc_addr, true); + + return irq_set_vcpu_affinity(host_irq, &vcpu_info); + } else { + return irq_set_vcpu_affinity(host_irq, NULL); + } } diff --git a/arch/x86/kvm/vmx/posted_intr.h b/arch/x86/kvm/vmx/posted_intr.h index a94afcb55f7f..94ed66ea6249 100644 --- a/arch/x86/kvm/vmx/posted_intr.h +++ b/arch/x86/kvm/vmx/posted_intr.h @@ -16,7 +16,7 @@ void pi_apicv_pre_state_restore(struct kvm_vcpu *vcpu); bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu); int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, - struct kvm_kernel_irq_routing_entry *new); + struct kvm_vcpu *vcpu, u32 vector); void vmx_pi_start_assignment(struct kvm *kvm); =20 static inline int pi_find_highest_vector(struct pi_desc *pi_desc) --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 C898127B4E8 for ; Fri, 23 May 2025 01:00:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962058; cv=none; b=AqvKa3awBLyJ8YyBec+O6T+qUBsemG7Zr68lYyumEjWw+AzlAoSEsmsu4ScsWDYhhHHLJh0TmiUkmCwUESfZzfNLTzMSV8zN/X9exoqKs61Wf/A7AP4Op2wb+P5MWQZc5KwgNHf7eyA2OFvDxk/FxeOOWucp1Cat67yHdTGljUI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962058; c=relaxed/simple; bh=wFEV1hAEK9Cq21Vuw1s3f6mxULh3lU3hrKHetm6s0gA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BV6+fyWptChw822aYHTXde4uk4NlGkRh7kfhE1tM0dS04nqjbS5migkIC7BAcPBnf/dVdoE4/b2WZ5CTFVec9rRvhVtgq3Z0lPInqLV1sAA4fTl2b20zVbG0eOIiSilabLhco6lMqJsTgINitnIQu7Ha+kBa61VOm0uqq3R+IhE= 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=V+8K8VJ4; arc=none smtp.client-ip=209.85.210.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="V+8K8VJ4" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-742c9c92bb1so4647177b3a.3 for ; Thu, 22 May 2025 18:00:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962056; x=1748566856; 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=yhhE7JoXXWTWvG6yCkhC0scHPBf3Grq2tdmL6Ns+pLM=; b=V+8K8VJ4fv1bO/CAfX2V1B0oO9//Ngj81jaI4DCE+9gZ5TG+ZsuxsXppSSs7cG1wfI soDXVQTP9MD9elOfUOnxq0bJvtksirHK+hyYhOEBlQWLjMETiaAqcuUP3lxBXV6HEvKs ml6d5T9bi2pYCaUV+cuhYqbN33RDpKsCWnZJ9Eu7Heoag3iWqvglqcP2vJUZRN67x6K4 NfBTIWfcpRSprq2rK26x0CRQAq9xEHPXsfZelrWlHeO6kQH1AdcTC14xBXHV8jrmYVo7 SFIGXdxq/deiFX83ZFEON2Rc5CBIxuDMqkaPgIFNBLoC+O0XEvRgXOvraJwmHgpJ4zTR fHLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962056; x=1748566856; 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=yhhE7JoXXWTWvG6yCkhC0scHPBf3Grq2tdmL6Ns+pLM=; b=A/YyL67tMqrfFGc1yqGjbqyV1f0rBVrUCMZcU9BEJ/V/5bOCVzsU1GWxhuPtPph+mM A48SfFrwBiDvGiDksLS62JSiHZ47YKR1Bg5pN32cMJrizb7xdUlVzmBVh6pRg7YFtzn1 qj67cM97MjPNF3k7lBiTFTw1kxKVSEE810PaDWoA3Y7xxs6BJvyLtow7mMCw6NfmQlYx C3VUo/fI2R6zuEcxoZlCWiHFsAmnEC0Bxg7av/cZ6qe4rGHtSBlxaRVXI4jn3An1nZsB 3OYUVBa0lt97suLi/YMJfONWCbm+Fw6PzqT5V04eqW1mBijVfW3HOcetA7952Er0nmI0 e+PQ== X-Forwarded-Encrypted: i=1; AJvYcCUNO5PFBONAtv0DLK22KKt4lQEQ+Gqsz/C8OyFe6T3K4kJP4NITwTG7Y7NDyUjMp8UnqDjHKkcLEGdF0wY=@vger.kernel.org X-Gm-Message-State: AOJu0YwTqMGTKdKcNnNbJEwz7f0obXylKYvF5WFtPvwj4z8oLCtnrr/0 VgTFKwzLA+ODG6CVschoI0JzFmV+9v64tEO+SBx1mDDusgX6zR7C1E/vHbZb/bcIOHcCsDrQ/hJ sZ5v30Q== X-Google-Smtp-Source: AGHT+IGRx2ZiGMUhau6D91BFbr/Ev2ssLnSEh7KdoNoZVLG8Xa09euUH8Eb2wiP0l+nV6+T4r2JytUx0iL0= X-Received: from pfbea26.prod.google.com ([2002:a05:6a00:4c1a:b0:741:1f46:47b1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:170a:b0:740:aa31:fe66 with SMTP id d2e1a72fcca58-745ed842bf8mr1610578b3a.4.1747962056133; Thu, 22 May 2025 18:00:56 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:32 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-28-seanjc@google.com> Subject: [PATCH v2 27/59] KVM: x86: Move posted interrupt tracepoint to common code From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the pi_irte_update tracepoint to common x86, and call it whenever the IRTE is modified. Tracing only the modifications that result in an IRQ being posted to a vCPU makes the tracepoint useless for debugging. Drop the vendor specific address; plumbing that into common code isn't worth the trouble, as the address is meaningless without a whole pile of other information that isn't provided in any tracepoint. Signed-off-by: Sean Christopherson --- arch/x86/kvm/irq.c | 12 ++++++++++-- arch/x86/kvm/svm/avic.c | 6 ------ arch/x86/kvm/trace.h | 19 +++++++------------ arch/x86/kvm/vmx/posted_intr.c | 3 --- arch/x86/kvm/x86.c | 1 - 5 files changed, 17 insertions(+), 24 deletions(-) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index 92a2137e0402..f20b6da30d6f 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -511,9 +511,11 @@ void kvm_arch_irq_routing_update(struct kvm *kvm) static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm_kernel_irq_routing_entry *entry) { + unsigned int host_irq =3D irqfd->producer->irq; struct kvm *kvm =3D irqfd->kvm; struct kvm_vcpu *vcpu =3D NULL; struct kvm_lapic_irq irq; + int r; =20 if (!irqchip_in_kernel(kvm) || !kvm_arch_has_irq_bypass() || @@ -540,8 +542,13 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd = *irqfd, vcpu =3D NULL; } =20 - return kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, irqfd->producer->i= rq, - irqfd->gsi, vcpu, irq.vector); + r =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, host_irq, irqfd->gs= i, + vcpu, irq.vector); + if (r) + return r; + + trace_kvm_pi_irte_update(host_irq, vcpu, irqfd->gsi, irq.vector, !!vcpu); + return 0; } =20 int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, @@ -595,6 +602,7 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass= _consumer *cons, =20 spin_unlock_irq(&kvm->irqfds.lock); =20 + kvm_arch_end_assignment(irqfd->kvm); } =20 diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 14a1544af192..d8d50b8f14bb 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -815,9 +815,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, */ svm_ir_list_del(irqfd); =20 - pr_debug("SVM: %s: host_irq=3D%#x, guest_irq=3D%#x, set=3D%#x\n", - __func__, host_irq, guest_irq, !!vcpu); - /** * Here, we setup with legacy mode in the following cases: * 1. When cannot target interrupt to a specific vcpu. @@ -853,9 +850,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, */ if (!ret) ret =3D svm_ir_list_add(to_svm(vcpu), irqfd, &pi); - - trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, guest_irq, - vector, vcpu_info.pi_desc_addr, true); } else { ret =3D irq_set_vcpu_affinity(host_irq, NULL); } diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index ababdba2c186..57d79fd31df0 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -1176,37 +1176,32 @@ TRACE_EVENT(kvm_smm_transition, * Tracepoint for VT-d posted-interrupts and AMD-Vi Guest Virtual APIC. */ TRACE_EVENT(kvm_pi_irte_update, - TP_PROTO(unsigned int host_irq, unsigned int vcpu_id, - unsigned int gsi, unsigned int gvec, - u64 pi_desc_addr, bool set), - TP_ARGS(host_irq, vcpu_id, gsi, gvec, pi_desc_addr, set), + TP_PROTO(unsigned int host_irq, struct kvm_vcpu *vcpu, + unsigned int gsi, unsigned int gvec, bool set), + TP_ARGS(host_irq, vcpu, gsi, gvec, set), =20 TP_STRUCT__entry( __field( unsigned int, host_irq ) - __field( unsigned int, vcpu_id ) + __field( int, vcpu_id ) __field( unsigned int, gsi ) __field( unsigned int, gvec ) - __field( u64, pi_desc_addr ) __field( bool, set ) ), =20 TP_fast_assign( __entry->host_irq =3D host_irq; - __entry->vcpu_id =3D vcpu_id; + __entry->vcpu_id =3D vcpu ? vcpu->vcpu_id : -1; __entry->gsi =3D gsi; __entry->gvec =3D gvec; - __entry->pi_desc_addr =3D pi_desc_addr; __entry->set =3D set; ), =20 - TP_printk("PI is %s for irq %u, vcpu %u, gsi: 0x%x, " - "gvec: 0x%x, pi_desc_addr: 0x%llx", + TP_printk("PI is %s for irq %u, vcpu %d, gsi: 0x%x, gvec: 0x%x", __entry->set ? "enabled and being updated" : "disabled", __entry->host_irq, __entry->vcpu_id, __entry->gsi, - __entry->gvec, - __entry->pi_desc_addr) + __entry->gvec) ); =20 /* diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index 3de767c5d6b2..687ffde3b61c 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -308,9 +308,6 @@ int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd, = struct kvm *kvm, .vector =3D vector, }; =20 - trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, guest_irq, - vcpu_info.vector, vcpu_info.pi_desc_addr, true); - return irq_set_vcpu_affinity(host_irq, &vcpu_info); } else { return irq_set_vcpu_affinity(host_irq, NULL); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a9900c246bb3..3966801bcb0d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -14014,7 +14014,6 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full); -EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log); --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4957227C150 for ; Fri, 23 May 2025 01:00:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962059; cv=none; b=rgGK6Gx9zHYnBza3JKAyGgvi7+b87awDhocW7nCub7pCHTjV/0LJQWbHVlhjuQKdi8j5HaJBk4QwKs2FdI7YzlnYCYskvvxYTkoRLGYHky3kGnQs3wXACfy0bsx2wh3W8yOjnHfkaz3WIrKtAgSl/FypPRjMXP5hgaRpyH1PnkU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962059; c=relaxed/simple; bh=3cDGPT4Fs5i3llYV6N7Z3zsG/IoHE6e+aDJIWGzC/ns=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Y5BaLfT319kTvZhABYbw6ToDLwzW8cYichJYILYessVmpbzfrUN3dAS6ThDWGnsX3dfZ+n8iZLmxPzfQjsqZ/9SgPPXrzAYTzUj+vnwo5DZQKk4A/RGAQlBAVLLZvd9qkRo1iCcEDcpShkLsERVYjELSPWTCqGd/526uRy63WnQ= 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=GHgTEVAM; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GHgTEVAM" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30e9338430eso5418587a91.3 for ; Thu, 22 May 2025 18:00:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962058; x=1748566858; 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=MOfqSC9jfe6qWef3o61heciDY2fzEzj3IP2K1ebfjaE=; b=GHgTEVAMsrItmgcDbmKrSJIfspAIPbHjwIVE+ljyk9XT4NjS4vXL9XbtTs8a2rr8L5 qFSj/CkeTc0Q/1to1zKw4Q/VXgBL//4oGeGGrCuKuNmTURX8lH2sFvBjkr4735MeR/6y 5B493PWkshZtLU4l65RQWvCeMSDvzicVuJkT95qKAWaUv0oMRTEkocdFFYb5YItwj6kL aeDUftnMppskgr3RrohQiFCA+f5Vol8/PiaV/+Jgb7Uc9p+pScihlvLOf0pDyHpwXnpO CS/Qel1bq1GBaWa7cAQhDgblOlpB0PCEe+vSiO/aQMcdvBNxv3r3GM07zFtTiibpWPSr 453A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962058; x=1748566858; 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=MOfqSC9jfe6qWef3o61heciDY2fzEzj3IP2K1ebfjaE=; b=ECQmZmRpJRCzomORxikrDO2wBELEs9nzAs1SfVDuirT9mJydRsYD+tMfGu+cG9GE1Y FaAcKg/MotdxkSC4JKtwpaqsY8jMcDyBxPldCxRxQlB8YK9iEkWuNxHdSyk4xDWoStAI 4zursUl5HlFMQre4yAnbGXHB8RFgeThON/c2g4gPBVsCE2YZBOMz8NC1NAiJU9acIW0u tGvNW+tYcll8PdPcLVBL+ZOS09wnIGmPoMqEftBKzik1DoQfv7l6IbifNdDiIX51O/yD zarN+LL7R1b5AIYv9xM4dRE/L7bcntTOfM1IIP16yS/bM46ehYXD1eYO/MAVMxr3rNMf 9gpA== X-Forwarded-Encrypted: i=1; AJvYcCUsJm4PhfQ1ZvEc61/0VQhQk92OVjivi6szVwCnv7U97yL31CbrmqzBdHJ3HisB97BoUH9cC7wMBs4NYyA=@vger.kernel.org X-Gm-Message-State: AOJu0Yyx7SvBlz2zAFsSQ7iNm1j6FqDe0zAAA0AhKyICiR801ntsj1oE z7fwANG05aj8xEBSfpwyvvRAdKiSHhk8eIjI6CMADWygnuzv9RWgkYNH+vWRgxB28pVQQ6Krbly 8ESmhoQ== X-Google-Smtp-Source: AGHT+IGbnIYZKIV7+UM33kPe0GOe3t9BK31wvtSS50odFLwVDGUrD18GfGcmK5AWEuZ0hAkPUYG529hmiGs= X-Received: from pja13.prod.google.com ([2002:a17:90b:548d:b0:2ef:786a:1835]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3b86:b0:30e:8c5d:8f8 with SMTP id 98e67ed59e1d1-30e8c5d09b3mr44883074a91.14.1747962057767; Thu, 22 May 2025 18:00:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:33 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-29-seanjc@google.com> Subject: [PATCH v2 28/59] KVM: SVM: Clean up return handling in avic_pi_update_irte() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Clean up the return paths for avic_pi_update_irte() now that the refactoring dust has settled. Opportunistically drop the pr_err() on IRTE update failures. Logging that a failure occurred without _any_ context is quite useless. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index d8d50b8f14bb..a0f3cdd2ea3f 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -807,8 +807,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, struct kvm_vcpu *vcpu, u32 vector) { - int ret =3D 0; - /* * If the IRQ was affined to a different vCPU, remove the IRTE metadata * from the *previous* vCPU's list. @@ -838,8 +836,11 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, .is_guest_mode =3D true, .vcpu_data =3D &vcpu_info, }; + int ret; =20 ret =3D irq_set_vcpu_affinity(host_irq, &pi); + if (ret) + return ret; =20 /** * Here, we successfully setting up vcpu affinity in @@ -848,20 +849,9 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, * we can reference to them directly when we update vcpu * scheduling information in IOMMU irte. */ - if (!ret) - ret =3D svm_ir_list_add(to_svm(vcpu), irqfd, &pi); - } else { - ret =3D irq_set_vcpu_affinity(host_irq, NULL); + return svm_ir_list_add(to_svm(vcpu), irqfd, &pi); } - - if (ret < 0) { - pr_err("%s: failed to update PI IRTE\n", __func__); - goto out; - } - - ret =3D 0; -out: - return ret; + return irq_set_vcpu_affinity(host_irq, NULL); } =20 static inline int --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 074CF27D77B for ; Fri, 23 May 2025 01:01:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962062; cv=none; b=KaT0RRQDe3iMGkaZNnh3XPVyMPV2HXZODQWjOORxsoA9MTnsbLMvb8WtiO4zz9Y/nzmf2gK+tI1I4O0EYvT1aBIX1RIua7zhnnObbw/Zmq52Xeji61AuJ/ftdKymVT5cfZcSty9Elr7l/MqLZ6qu/o5+mnDtTjUIS6ukq3HtMwU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962062; c=relaxed/simple; bh=oNRMXcQ79z/ppsp/MdXUoLSNSsfC0IRqSoysTfP2n0c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DqNfpoEYItxJpo6OSTw9RxmCjlnRW8gX+92OMgkZ6eT6LpX6j8KWKul6JgIPf7sHySHH+Z1QTvSZn4lBSdx5yoMLenAEkV/VDVEQw5J4P7UmGGsKp/ete9wDM/Jwu6ze0Ra0BTEVYXm3zIpENRgBUHERCTffL7NA/0TKsIQC2eA= 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=Cje8x8/h; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Cje8x8/h" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30eac9886ffso4645759a91.1 for ; Thu, 22 May 2025 18:01:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962059; x=1748566859; 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=JMjSdTj+suhJzKf/G1gZtLOB7F2Sxx7/bINJC6X9hG0=; b=Cje8x8/hhV4D4g4w3qPLyqwuYXpMpIiZ+2ToX8ltJCiv8JDCOSY39UforEANW0QuwI HwCY8Glx8l62DW9N2W1E/thXcXpUcF2Rbi5r4iKRO/abdp5rSQqkK0tAlkc5spLHc+5E yCkSyQESZ4+UNUN7CXgXCv8Xe0NCefKkzNq1tR8WrvflUVlrn5iE2V/1l72XGKkZwmmt +gdGi2Up3CHZSFpIDDnlKiNRFjCKp1y9crjEgDbN6ASMuw/rznth5qKVTYkPQ3N9jOYm SaBQ3QkEyJ68TA/nk1LbFDYSZfFSphkw8MMDr2vaSsApEnlrK8NjfZD7ZBFHKQTOp5BI jYcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962059; x=1748566859; 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=JMjSdTj+suhJzKf/G1gZtLOB7F2Sxx7/bINJC6X9hG0=; b=vsdUqAGuCAIh6leO0VsoXbL4Tdrsi1nnit5QDIOrCD2wTFgG2d4SgeDh870MTKsp+J o06Wdl0rLOd9ZofCYDWfazVFyPEX+DKKaSFd3A0GM8TkxT0R3HnjzU7embkTArVG+oLx JyR5+JRWzUIh9dQpLNJQoKy9enxpPOhQklNx8gDXB9vThgxNofhBUVfV+4J7XphEgeby eHSErH0r9VLbg6vukUbvM09ld7YCmZKmpSm7VNW4LIO3rHgSs2P6g8yklIiET/2Mi5vH oDGnRoOTzqXfOB1MFG6DrcdoLT67Y0d9Aco6ISOKhlgz+BdiSOBvuIP1ooaJGxtE/x4D lwNA== X-Forwarded-Encrypted: i=1; AJvYcCXV/afHUVKoZOLMflLwlK5iE/3nccOSMRXYvJZI/Z1bUVm/MD6r7cu611BsgbPJum9Eucc4W6h1qjhZOXE=@vger.kernel.org X-Gm-Message-State: AOJu0Yw76r4iadKOOpbSYrrb2EA0La85/CMpMnJPLoK2QHN4+FtnffhV r3ff2+etIGsdkMFqjXuNo2p0HoG+46Apa1ILaOuf1P4UYB8AN/nL+QWemCoD2f3lfIbTPGowgDZ y5cvjBw== X-Google-Smtp-Source: AGHT+IF8c6fJ8yMCN4wj6UnyIXb58GYfChHNsI8pVlq/3hKy38hpN1X8/00HWG6ufh/FDU2XY+RsgA7OHYI= X-Received: from pjbnd11.prod.google.com ([2002:a17:90b:4ccb:b0:301:b354:8d63]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:17c7:b0:30e:a1ec:68dd with SMTP id 98e67ed59e1d1-310e96c95aemr1724491a91.8.1747962059471; Thu, 22 May 2025 18:00:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:34 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-30-seanjc@google.com> Subject: [PATCH v2 29/59] iommu: KVM: Split "struct vcpu_data" into separate AMD vs. Intel structs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Split the vcpu_data structure that serves as a handoff from KVM to IOMMU drivers into vendor specific structures. Overloading a single structure makes the code hard to read and maintain, is *very* misleading as it suggests that mixing vendors is actually supported, and bastardizing Intel's posted interrupt descriptor address when AMD's IOMMU already has its own structure is quite unnecessary. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/include/asm/irq_remapping.h | 15 ++++++++++++++- arch/x86/kvm/svm/avic.c | 21 ++++++++------------- arch/x86/kvm/vmx/posted_intr.c | 4 ++-- drivers/iommu/amd/iommu.c | 12 ++++-------- drivers/iommu/intel/irq_remapping.c | 10 +++++----- include/linux/amd-iommu.h | 12 ------------ 6 files changed, 33 insertions(+), 41 deletions(-) diff --git a/arch/x86/include/asm/irq_remapping.h b/arch/x86/include/asm/ir= q_remapping.h index 5036f13ab69f..2dbc9cb61c2f 100644 --- a/arch/x86/include/asm/irq_remapping.h +++ b/arch/x86/include/asm/irq_remapping.h @@ -26,7 +26,20 @@ enum { IRQ_REMAP_X2APIC_MODE, }; =20 -struct vcpu_data { +/* + * This is mainly used to communicate information back-and-forth + * between SVM and IOMMU for setting up and tearing down posted + * interrupt + */ +struct amd_iommu_pi_data { + u64 vapic_addr; /* Physical address of the vCPU's vAPIC. */ + u32 ga_tag; + u32 vector; /* Guest vector of the interrupt */ + bool is_guest_mode; + void *ir_data; +}; + +struct intel_iommu_pi_data { u64 pi_desc_addr; /* Physical address of PI Descriptor */ u32 vector; /* Guest vector of the interrupt */ }; diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index a0f3cdd2ea3f..6085a629c5e6 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -822,23 +822,18 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqf= d, struct kvm *kvm, */ if (vcpu && kvm_vcpu_apicv_active(vcpu)) { /* - * Try to enable guest_mode in IRTE. Note, the address - * of the vCPU's AVIC backing page is passed to the - * IOMMU via vcpu_info->pi_desc_addr. + * Try to enable guest_mode in IRTE. */ - struct vcpu_data vcpu_info =3D { - .pi_desc_addr =3D avic_get_backing_page_address(to_svm(vcpu)), - .vector =3D vector, - }; - - struct amd_iommu_pi_data pi =3D { - .ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id), + struct amd_iommu_pi_data pi_data =3D { + .ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, + vcpu->vcpu_id), .is_guest_mode =3D true, - .vcpu_data =3D &vcpu_info, + .vapic_addr =3D avic_get_backing_page_address(to_svm(vcpu)), + .vector =3D vector, }; int ret; =20 - ret =3D irq_set_vcpu_affinity(host_irq, &pi); + ret =3D irq_set_vcpu_affinity(host_irq, &pi_data); if (ret) return ret; =20 @@ -849,7 +844,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, * we can reference to them directly when we update vcpu * scheduling information in IOMMU irte. */ - return svm_ir_list_add(to_svm(vcpu), irqfd, &pi); + return svm_ir_list_add(to_svm(vcpu), irqfd, &pi_data); } return irq_set_vcpu_affinity(host_irq, NULL); } diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index 687ffde3b61c..3a23c30f73cb 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -303,12 +303,12 @@ int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, struct kvm_vcpu *vcpu, u32 vector) { if (vcpu) { - struct vcpu_data vcpu_info =3D { + struct intel_iommu_pi_data pi_data =3D { .pi_desc_addr =3D __pa(vcpu_to_pi_desc(vcpu)), .vector =3D vector, }; =20 - return irq_set_vcpu_affinity(host_irq, &vcpu_info); + return irq_set_vcpu_affinity(host_irq, &pi_data); } else { return irq_set_vcpu_affinity(host_irq, NULL); } diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 5141507587e1..36749efcc781 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3860,10 +3860,10 @@ int amd_iommu_deactivate_guest_mode(void *data) } EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode); =20 -static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info) +static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *info) { int ret; - struct amd_iommu_pi_data *pi_data =3D vcpu_info; + struct amd_iommu_pi_data *pi_data =3D info; struct amd_ir_data *ir_data =3D data->chip_data; struct irq_2_irte *irte_info =3D &ir_data->irq_2_irte; struct iommu_dev_data *dev_data; @@ -3886,14 +3886,10 @@ static int amd_ir_set_vcpu_affinity(struct irq_data= *data, void *vcpu_info) ir_data->cfg =3D irqd_cfg(data); =20 if (pi_data) { - struct vcpu_data *vcpu_pi_info =3D pi_data->vcpu_data; - pi_data->ir_data =3D ir_data; =20 - WARN_ON_ONCE(!pi_data->is_guest_mode); - - 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_root_ptr =3D (pi_data->vapic_addr >> 12); + ir_data->ga_vector =3D pi_data->vector; ir_data->ga_tag =3D pi_data->ga_tag; ret =3D amd_iommu_activate_guest_mode(ir_data); } else { diff --git a/drivers/iommu/intel/irq_remapping.c b/drivers/iommu/intel/irq_= remapping.c index 3bc2a03cceca..6165bb919520 100644 --- a/drivers/iommu/intel/irq_remapping.c +++ b/drivers/iommu/intel/irq_remapping.c @@ -1244,10 +1244,10 @@ static void intel_ir_compose_msi_msg(struct irq_dat= a *irq_data, static int intel_ir_set_vcpu_affinity(struct irq_data *data, void *info) { struct intel_ir_data *ir_data =3D data->chip_data; - struct vcpu_data *vcpu_pi_info =3D info; + struct intel_iommu_pi_data *pi_data =3D info; =20 /* stop posting interrupts, back to the default mode */ - if (!vcpu_pi_info) { + if (!pi_data) { __intel_ir_reconfigure_irte(data, true); } else { struct irte irte_pi; @@ -1265,10 +1265,10 @@ static int intel_ir_set_vcpu_affinity(struct irq_da= ta *data, void *info) /* Update the posted mode fields */ irte_pi.p_pst =3D 1; irte_pi.p_urgent =3D 0; - irte_pi.p_vector =3D vcpu_pi_info->vector; - irte_pi.pda_l =3D (vcpu_pi_info->pi_desc_addr >> + irte_pi.p_vector =3D pi_data->vector; + irte_pi.pda_l =3D (pi_data->pi_desc_addr >> (32 - PDA_LOW_BIT)) & ~(-1UL << PDA_LOW_BIT); - irte_pi.pda_h =3D (vcpu_pi_info->pi_desc_addr >> 32) & + irte_pi.pda_h =3D (pi_data->pi_desc_addr >> 32) & ~(-1UL << PDA_HIGH_BIT); =20 ir_data->irq_2_iommu.posted_vcpu =3D true; diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index deeefc92a5cf..99b4fa9a0296 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -12,18 +12,6 @@ =20 struct amd_iommu; =20 -/* - * This is mainly used to communicate information back-and-forth - * between SVM and IOMMU for setting up and tearing down posted - * interrupt - */ -struct amd_iommu_pi_data { - u32 ga_tag; - bool is_guest_mode; - struct vcpu_data *vcpu_data; - void *ir_data; -}; - #ifdef CONFIG_AMD_IOMMU =20 struct task_struct; --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 D185B27C152 for ; Fri, 23 May 2025 01:01:01 +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=1747962064; cv=none; b=qeTOSuDeduSSvSzWXi3P/XaG0gr3/zqFPnxuOUeMopQe4cN0dNTVYu1nXxAgA7k9IRqzE1Fo7vaRp6Jhxesi56cMUKyOOUNiro4QMU9Sx2lE95bO1hRzZ0VxqRG5dgiSPHmQ7bqeUKRQe9w/fJyFLygQNxIbVqAzwKcEMk2oSrM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962064; c=relaxed/simple; bh=CjEiv3EBSt3Kbej2hI7WprWBZwegAM8tmmcYdNo1npY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Xse83FLfGHlRVA7dYMsH9L6aiuQzNhmq0GS5SzjC3nX5Df+HmOS6wC/0VSGChGpTuFBLFmux1mmzgTRVWXegTroKM6qfA7DWpiMQsulfCvDFsNHWsTOKqibl5xvYK/ram/IkWi3PD9HfVOa43iCIhGPkd0ZIU/c6ZtseeILFNp0= 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=yanjyWes; 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="yanjyWes" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-af534e796baso5345615a12.3 for ; Thu, 22 May 2025 18:01:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962061; x=1748566861; 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=I/V/wUMCmixrEnIVNqfJcxmN2QXnsdAPP3QKe6AvyEw=; b=yanjyWesW8oYnASX7ORC1Y+kgyIlvj3Szpvv0I/7s26pPS9maYlAQzsvu0wTrpeyJ7 fhK/i1TVtwapjuoHMPeMZiM84XzLM9vjCoph4FSl2v4j9pUN41dKmesltGRVK1efKptP qzKNf+Y/VtzGdXyc5Da1SxVZTKlOVtKrc0Mnug4TEC6bhkAtojJLZqGHv4he+iZu1Lne BPogYHD5AB+pu05F8FdEznv3VwhVNSAbI095ZAsqWjWe70EpM6KSZT1UDqP2QnZjldJx GNvISnsdKYOmQ+lL7kkZugZ0AxIJB76ufTwO6HG6ZQ0wsZYEaOuQR1CwFkai+JEawMgN yRrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962061; x=1748566861; 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=I/V/wUMCmixrEnIVNqfJcxmN2QXnsdAPP3QKe6AvyEw=; b=K7pGbm356jD4RXkhbanhw+7NPVJxCISp4DhauesVGqfuQy7LQ9sXAXQ0dUKyCjWt43 RSocREbGUltL+69TuXv+2RxNPlECzsaTRmXWEceG9payWJnFEX+MasOh5NWhYEixsHM3 mjzdSOu+/c1PxhSByvKxNAUhbfG1+eEGh3lv+Fakg3J6kxrCrP5TAs5PpAoijO/lqjZA kmZIPxV4QNwL9RjsvbczUwafJVjhla6JJ4pL4uINc9aQ7gyK+s6F6BRQw8Aa0N2RMpgE FxMQiuKxmX17nbC5wZxin5R2KGlR5O0nJzvwJ7M/O1rrC0JqMgCv4KU8gyPD2SLimwXu 17Yw== X-Forwarded-Encrypted: i=1; AJvYcCXU3j9mGIgZizFp+JrfELuQdZdM0bW7SuklsmL6lmdqrq9hN4hFlYaREzEJz4rEDOjO1aOIybbSbADa56E=@vger.kernel.org X-Gm-Message-State: AOJu0YwskKV0X3S4oupCauSqxysopVsKyddsedC0IKabp12mccE5E6SU mZnrzfHCH634AnGSqt1kV1ynXmZKibBFd9IAy65X3rtMgpTeb9UTmDx36arRIR/Y4QqB796soqP gNmMtTg== X-Google-Smtp-Source: AGHT+IEotc86oF2PtG/8v7ATzTs0oMnFmKSHoNnIFngKOe27XotzUkoD7+iAiHXRlObAKnBtxXNE1dwWSgM= X-Received: from pgbcr12.prod.google.com ([2002:a05:6a02:410c:b0:b2b:2ba7:184f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:748e:b0:1f5:6b36:f57a with SMTP id adf61e73a8af0-2187a6de22bmr1225154637.39.1747962061182; Thu, 22 May 2025 18:01:01 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:35 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-31-seanjc@google.com> Subject: [PATCH v2 30/59] KVM: Don't WARN if updating IRQ bypass route fails From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Don't bother WARNing if updating an IRTE route fails now that vendor code provides much more precise WARNs. The generic WARN doesn't provide enough information to actually debug the problem, and has obviously done nothing to surface the myriad bugs in KVM's implementation. Signed-off-by: Sean Christopherson --- arch/x86/kvm/irq.c | 8 ++++---- include/linux/kvm_host.h | 6 +++--- virt/kvm/eventfd.c | 15 ++++++--------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index f20b6da30d6f..b3ce870d1d91 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -606,11 +606,11 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypa= ss_consumer *cons, kvm_arch_end_assignment(irqfd->kvm); } =20 -int kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, - struct kvm_kernel_irq_routing_entry *old, - struct kvm_kernel_irq_routing_entry *new) +void kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, + struct kvm_kernel_irq_routing_entry *old, + struct kvm_kernel_irq_routing_entry *new) { - return kvm_pi_update_irte(irqfd, new); + kvm_pi_update_irte(irqfd, new); } =20 bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *old, diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 27c7087820cb..ee79b1167e02 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2406,9 +2406,9 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypa= ss_consumer *, struct irq_bypass_producer *); void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *); void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *); -int kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, - struct kvm_kernel_irq_routing_entry *old, - struct kvm_kernel_irq_routing_entry *new); +void kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, + struct kvm_kernel_irq_routing_entry *old, + struct kvm_kernel_irq_routing_entry *new); bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *, struct kvm_kernel_irq_routing_entry *); #endif /* CONFIG_HAVE_KVM_IRQ_BYPASS */ diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 85581550dc8d..a4f80fe8a5f3 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -285,11 +285,11 @@ void __attribute__((weak)) kvm_arch_irq_bypass_start( { } =20 -int __weak kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, - struct kvm_kernel_irq_routing_entry *old, - struct kvm_kernel_irq_routing_entry *new) +void __weak kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, + struct kvm_kernel_irq_routing_entry *old, + struct kvm_kernel_irq_routing_entry *new) { - return 0; + } =20 bool __attribute__((weak)) kvm_arch_irqfd_route_changed( @@ -618,11 +618,8 @@ void kvm_irq_routing_update(struct kvm *kvm) =20 #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS) if (irqfd->producer && - kvm_arch_irqfd_route_changed(&old, &irqfd->irq_entry)) { - int ret =3D kvm_arch_update_irqfd_routing(irqfd, &old, &irqfd->irq_entr= y); - - WARN_ON(ret); - } + kvm_arch_irqfd_route_changed(&old, &irqfd->irq_entry)) + kvm_arch_update_irqfd_routing(irqfd, &old, &irqfd->irq_entry); #endif } =20 --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C9A3280314 for ; Fri, 23 May 2025 01:01:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962064; cv=none; b=UpZmXP8Rm6Ftw1A2J5QIdO28Owz+UMM5sTLtVf0IrYEdY+ImA9sWl+FbEtBmbGqyvoGyJcHxosRSHiZM3FG8OzMHg67KXYuzjmfmdSYrmqgEOw4fCGrxjkT4CPeDtSQsWEqRK5RdjNGpuv4WIcWfNsE8Q1jyB9zNOk0u1FMQdSQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962064; c=relaxed/simple; bh=myxsKIMu9z2uOdqZo9sjfpCseSVPuh0CYv0CumURj6M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YMclHbAYbo0JPh9U8t6gAkpLLM5LKPM2ZJcnBcpBN7gHli2TJdUBXxCao8qSsP3g4hFzENGg+14euVwqhjg0klaM7DOOjoY1RHuf7mon9dUgpsO4OPyZLUcexqYdwe4tisDWDojJ5BcSmdon+GQwwe1VqTnghWvb06hP806ytD8= 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=WxrI960j; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WxrI960j" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3108d5538c0so2091282a91.0 for ; Thu, 22 May 2025 18:01:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962063; x=1748566863; 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=Z6qJrl8moJt7bfJRd6rO8Y2MTdMUnxYZYEX7kVfA4zY=; b=WxrI960jyZ6SxgFMcPlvfOPhWWcpQcZ8rNwoiMFkS1ofqeoAPzb2uF7fCnV1Q6+XlH KqcBI/U1KC5ndGSWLSX+oAOTcQ0EWz00n+pUFFYgIuT982olZaxT36FFwQRBOMWoYxbN jIRMMFp+WLPjqDdpYcrzU+4vUGwpeEXJ1mwO4/HJFBQXCRK5c8RKbR4LIQdzlrwlK3cH ZaQJY9+qiiPomtADVDuTE2AH7fA0aBbqqoT730LkJxDh6KCtxM3rYPyeE0P21oewYnNe cQuE9jvAkdyfc7iTOC0DqHx8FEOuaxF3XklAoHsNR4fnE8oS8aK3h17XLUqVXMGip5aG 9kpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962063; x=1748566863; 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=Z6qJrl8moJt7bfJRd6rO8Y2MTdMUnxYZYEX7kVfA4zY=; b=J/QokrH9D8XArlOuV6eVF0SvcUZyr0ZytSl0ZaOq5095+J1KHBqAsGgqxlvrKfrRbg W1+zkmXjZSgjDM6dmYApOcehR6p23f2mX94V+c0+V5Bj502n43a/og62NvBvd0w93Yk8 cQou2tgETFQStAUy+z13Q+MiwHdE9I+LLMHS60xUMhTEer8PRgnHvBVqYqW1dr+icboP Jt13QoGgyE1jtY5ndQtjI59iOQalGgzGfi7av4i0a78BPln3GAixHqK/J9Tgqdzo4SNB V8tYdHKC2fTka2tHHAhkbbdjTz7c5ZEC+vbfxD7lFT6YZk+MxLhhhp6KCuJDVl2nIgPj TELw== X-Forwarded-Encrypted: i=1; AJvYcCVYttD9tV3v2/t7uEPyCFwFpq+629TkMgQl2mvXNe9C1oD09xo3bM8FgxAaZok1W7ZFK0bfFaQ3GSkAyZA=@vger.kernel.org X-Gm-Message-State: AOJu0Yy9rI75MPq3MHY6Py8pu4Q66m5vtkAK/MbLJv0EE8CTCyl79hpm wKCjfHpbjEYLZpzIh0KEWJiw0kzXOx3RDdPL2+yIdchCK3plAMWngblSE1ISZavhnKYCIfIwXpA T6shIxw== X-Google-Smtp-Source: AGHT+IHGd9AMcPRaXLw73jWmjjZZ11tSRCThxeyCURZKAbhkOZc/Mi2j9RJn55Hndx07vxGEYk1l0sThWdQ= X-Received: from pjb7.prod.google.com ([2002:a17:90b:2f07:b0:2ff:6132:8710]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1f8c:b0:2fc:3264:3666 with SMTP id 98e67ed59e1d1-30e7d5be34bmr41066961a91.30.1747962062808; Thu, 22 May 2025 18:01:02 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:36 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-32-seanjc@google.com> Subject: [PATCH v2 31/59] KVM: Fold kvm_arch_irqfd_route_changed() into kvm_arch_update_irqfd_routing() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Fold kvm_arch_irqfd_route_changed() into kvm_arch_update_irqfd_routing(). Calling arch code to know whether or not to call arch code is absurd. Signed-off-by: Sean Christopherson --- arch/x86/kvm/irq.c | 15 +++++---------- include/linux/kvm_host.h | 2 -- virt/kvm/eventfd.c | 10 +--------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index b3ce870d1d91..582fc17ae02b 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -610,15 +610,10 @@ void kvm_arch_update_irqfd_routing(struct kvm_kernel_= irqfd *irqfd, struct kvm_kernel_irq_routing_entry *old, struct kvm_kernel_irq_routing_entry *new) { + if (old->type =3D=3D KVM_IRQ_ROUTING_MSI && + new->type =3D=3D KVM_IRQ_ROUTING_MSI && + !memcmp(&old->msi, &new->msi, sizeof(new->msi))) + return; + kvm_pi_update_irte(irqfd, new); } - -bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *old, - struct kvm_kernel_irq_routing_entry *new) -{ - if (old->type !=3D KVM_IRQ_ROUTING_MSI || - new->type !=3D KVM_IRQ_ROUTING_MSI) - return true; - - return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); -} diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index ee79b1167e02..706f2402ae8e 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2409,8 +2409,6 @@ void kvm_arch_irq_bypass_start(struct irq_bypass_cons= umer *); void kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, struct kvm_kernel_irq_routing_entry *old, struct kvm_kernel_irq_routing_entry *new); -bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *, - struct kvm_kernel_irq_routing_entry *); #endif /* CONFIG_HAVE_KVM_IRQ_BYPASS */ =20 #ifdef CONFIG_HAVE_KVM_INVALID_WAKEUPS diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index a4f80fe8a5f3..defc2c04d241 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -291,13 +291,6 @@ void __weak kvm_arch_update_irqfd_routing(struct kvm_k= ernel_irqfd *irqfd, { =20 } - -bool __attribute__((weak)) kvm_arch_irqfd_route_changed( - struct kvm_kernel_irq_routing_entry *old, - struct kvm_kernel_irq_routing_entry *new) -{ - return true; -} #endif =20 static int @@ -617,8 +610,7 @@ void kvm_irq_routing_update(struct kvm *kvm) irqfd_update(kvm, irqfd); =20 #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS) - if (irqfd->producer && - kvm_arch_irqfd_route_changed(&old, &irqfd->irq_entry)) + if (irqfd->producer) kvm_arch_update_irqfd_routing(irqfd, &old, &irqfd->irq_entry); #endif } --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 07BFA283CBE for ; Fri, 23 May 2025 01:01:04 +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=1747962066; cv=none; b=mb8K8PZiVMd1Qgn0TbonE9s6UfJxXL73ZHYHPSV4daAtUcUyjjt+G5+VlKWPaZ4OREnbHT2gSpxccZ1qKX7p9Qpt3KsdfTqP2GbA6jVQodO02JRgePtqCw2wguub6ptv4IjuObgoMsf1iN3TUsGZOoUPhic0ZKNAEELiu9d+q7s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962066; c=relaxed/simple; bh=lF7pC8sohabNgcaIb1E8HB/O7nTwbIatU6uqVNQw5dg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DRHHGZZdv61rEQbDulNFCgmz0EtLbakvD3xbRBuaWe4SrHhA50NeWRX33acIRLNc2diq5A45h5qD7FhLc01JpouVtc675zksPzp8LB7EHfdBbw3HrViNv5/amKwBMzeRe8i7/qngeeThUPJ4iz319vhM+ByQhJ23RuT73BTBwRs= 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=F6oWaVrY; 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="F6oWaVrY" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b26e33ae9d5so8986424a12.1 for ; Thu, 22 May 2025 18:01:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962064; x=1748566864; 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=VEEMumgPPOxuu5AKa6Opm2ERFPwrtqEfeM08GKoEFwc=; b=F6oWaVrYaVXLtdfVpgmSmF+lqbZFv3xWF3l9OcfkamoAEi3o6ABXB2eVhwo4RgOAvz /zxu+wBzoLoCdsNI5p9UOriY0tUA4PyQA0Hj1IzhyWZJNmQlBZEFeiOa47cpLIyKQYlJ t+KTWQspbYa/us7Q83VMfToKUgiz8ALSHgtuincC3Uwo6BxRmC81vJ/sqVnQLvSMrnnS YqWeHHiRiZMMMXAj2eiGMLgjvA7fqUBCZxUBxrCnlac2AyRXk9TwAq2i/pW2B+AIE7cI cwIaQjNa7gFod/JpBBe839ssb8uvwkOKMUePGzYvst2ZLxyoOUm9r9Y10esNVLkN5mbn aSwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962064; x=1748566864; 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=VEEMumgPPOxuu5AKa6Opm2ERFPwrtqEfeM08GKoEFwc=; b=fY8t3NoHeK6guulUqi1AdSkSjQTq3jkP19A9atT1wHmzzLWpRCUqPWBkJxzgff3Gi3 IgBvdLTYC2SsmgJV17RRtax8JggOxMwOKcwsXAxVXxqb30Y9QSB5zn4A963HVXn9VT/i LisZaclaEHOA1h7WhvxB+t5uH9Dc2Q5BfAL4Zc4ZRWGTZfU945yVWbuj30DhNY8l47n7 24FSd4OCpUteMRyAOu1TzUwY83SZ6HqqaT68ltUWEjP9FpNSF3a77ktnn1SPSkxzNTcv NwL49ZJwvmfXI03SpnRPqPqn/KSL1xvt767rB4OTu1K/eJKLwtEio34ImNCUyisQWbfE JXUw== X-Forwarded-Encrypted: i=1; AJvYcCWdZ0lurRZ8Wqv1NR9UqYDJ7Fy3wQ9F2nBhY/z6iFsLBlumwthHO0tn+n4gU3a6xnJcgCDMwdBpPU479vo=@vger.kernel.org X-Gm-Message-State: AOJu0YzdR2Z/AAG+L1HAgS+MVfGEE8m1iyDXVqVJ5/PxzlXstAIXKn2B 3cAzat6e0IAinIMzF+Ri4S8N7GL8MqUQFYY98qe4rAgT5mQaW+IMir8HImY6DCpk0maQVm36Y+h ZjSXftQ== X-Google-Smtp-Source: AGHT+IETqKO87aMFsUZqxCg0LdZa+zJR5n+2fRyhOgSEe0vCrHKlfeYquaUmcKRaIo3hs3+dxPHGjYw4zFc= X-Received: from pgbfq25.prod.google.com ([2002:a05:6a02:2999:b0:b26:e54d:d7dd]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:a129:b0:1f5:8e54:9f10 with SMTP id adf61e73a8af0-2170cde51b5mr47706400637.34.1747962064242; Thu, 22 May 2025 18:01:04 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:37 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-33-seanjc@google.com> Subject: [PATCH v2 32/59] KVM: x86: Track irq_bypass_vcpu in common x86 code From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Track the vCPU that is being targeted for IRQ bypass, a.k.a. for a posted IRQ, in common x86 code. This will allow for additional consolidation of the SVM and VMX code. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/irq.c | 7 ++++++- arch/x86/kvm/svm/avic.c | 4 ---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index 582fc17ae02b..3a0f28e98429 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -544,8 +544,13 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd = *irqfd, =20 r =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, host_irq, irqfd->gs= i, vcpu, irq.vector); - if (r) + if (r) { + WARN_ON_ONCE(irqfd->irq_bypass_vcpu && !vcpu); + irqfd->irq_bypass_vcpu =3D NULL; return r; + } + + irqfd->irq_bypass_vcpu =3D vcpu; =20 trace_kvm_pi_irte_update(host_irq, vcpu, irqfd->gsi, irq.vector, !!vcpu); return 0; diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 6085a629c5e6..97b747e82012 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -767,22 +767,18 @@ static void svm_ir_list_del(struct kvm_kernel_irqfd *= irqfd) spin_lock_irqsave(&to_svm(vcpu)->ir_list_lock, flags); list_del(&irqfd->vcpu_list); spin_unlock_irqrestore(&to_svm(vcpu)->ir_list_lock, flags); - - irqfd->irq_bypass_vcpu =3D NULL; } =20 static int svm_ir_list_add(struct vcpu_svm *svm, struct kvm_kernel_irqfd *irqfd, struct amd_iommu_pi_data *pi) { - struct kvm_vcpu *vcpu =3D &svm->vcpu; unsigned long flags; u64 entry; =20 if (WARN_ON_ONCE(!pi->ir_data)) return -EINVAL; =20 - irqfd->irq_bypass_vcpu =3D vcpu; irqfd->irq_bypass_data =3D pi->ir_data; =20 spin_lock_irqsave(&svm->ir_list_lock, flags); --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B3CE12882B0 for ; Fri, 23 May 2025 01:01:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962068; cv=none; b=Ek1V9ABKbeTm+M2g1YxkSamCD0MkSzmA58POXwaI7Dxc91zJ4Dpbnx8twgEL5Oq3rRlBFT1ENj0HSeD0uur0xVuqL21hTr+ipZz6+moTDJ+U4GgIeH/eFWSuw0bk1jMqkmrg31gZ7Ek4Z83sK3jXuaUn++sbUXw9ZOJC1mqHqtU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962068; c=relaxed/simple; bh=D8FN8kkLx7L8tVrUTR+61zNHHfC5qjIkeg2WDBKUnVA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=i5ljl89N1mgBsbUGmfPFgBbOsP1FeL9F1xbp8h0UVWzCOT3dFBrnp9OyPKMwXC4oBb08rgxfqYYMQwe5GlTKY6pYEIm1KqfdU8Opt5AWhXlsYWhL7uOtuwbizYEnyw1EbHQWxlfEmRXo0WknSqa/VdUJMNiwDk9cl19o/pUFjU4= 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=vabktCzN; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="vabktCzN" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30ed6bd1b4cso7454636a91.0 for ; Thu, 22 May 2025 18:01:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962066; x=1748566866; 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=a8iEK4WECydE0ey1yGfRxrlymXScvl+S1HRsDMKm8xs=; b=vabktCzN5A6ODhh1oq1YbFw6Gz4vl3FRUoeLffbuMUUFEpeBenrJWDoKpea5npxFxN wsdxAfPTKwHTMs7xrxMlnhCzSG4Nb/upBYH+U6Cd352f0jrm3Xd/mTzi/ez7oTW0U7ZU ViMxN4c6+2RJSAi98MKDdb4lgXUV/HH3vQzOdP7iKM3706roULR4Rm+fUkxM2r4hnLKH yQJs5gboYmUZDTP9FZCxVwhZ5ZtLSYzJ3gNZUXbdKE+IvZEWmSafoMtgkfcQMzi0fBZY ZNoVMgcTn2jlCFO1ORgqhEijF0aSbKIvwCxajLZBFWFMqq4dLZ6haHyPvhjv/FY81IB1 rZcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962066; x=1748566866; 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=a8iEK4WECydE0ey1yGfRxrlymXScvl+S1HRsDMKm8xs=; b=rH1PxgQuY6ub5OQza9kqO7Cndufd2Jc4wijuGQ4gHSPQI33JL7CZSSRDuOK3ygOzHk eq/iEF0xUXzOTe0dlVx+ul7gyYbsn0UObU3nhG/pFZ73tkjWXZps02tFkZCHPwM+4PAK DtroxKkgCMSU5c0paN1X3bqTjMknqoVsjfD9xnVhFbYDWyoI03+qJT6PuBYdpurAHbph 0lUl4VGyygiEnnySLPSkLOFLqzB3690wYCsZwfSgCX881O30suDbdanMIc9HDmQ3sFB5 1PJlVI89XcYrWiOHkhEX1ww3QPRerMPEUJVolIxA4+1VDaxGWel8NtYzx4ZyyuyE5HWs Oumg== X-Forwarded-Encrypted: i=1; AJvYcCXlzHZJ3KCJ8XOaIB8QMR/feKdVNQFEfbh8kOavwiIzWw/czCysGHRDyBRkn6N6R37AIh4333pw2kvFBVQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyLrYGNq1iqA3VPsOqWMjpwDroCKHPR/JL7qtslq4NXYPu49Xus Zw/NcdGvOkHPawnn2/qBpWPxCDnj8ROhov/VrRrKgeP4iGMExDec/1YqoNnpH5m25Uq8XS+Ud6U 7xOT1ag== X-Google-Smtp-Source: AGHT+IHwPMZGzfvW4HHKJ8l4pKBM2HZBGdDFPBOti30bJ3R5xYNqLd2qkSG+uTsnts5TsJg7XZvkaLmeguw= X-Received: from pjp15.prod.google.com ([2002:a17:90b:55cf:b0:2ea:5084:5297]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3b8f:b0:2ff:6608:78cd with SMTP id 98e67ed59e1d1-30e830fbe31mr42654796a91.9.1747962065843; Thu, 22 May 2025 18:01:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:38 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-34-seanjc@google.com> Subject: [PATCH v2 33/59] KVM: x86: Skip IOMMU IRTE updates if there's no old or new vCPU being targeted From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Don't "reconfigure" an IRTE into host controlled mode when it's already in the state, i.e. if KVM's GSI routing changes but the IRQ wasn't and still isn't being posted to a vCPU. Signed-off-by: Sean Christopherson --- arch/x86/kvm/irq.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index 3a0f28e98429..67fc8901d15f 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -542,6 +542,9 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *= irqfd, vcpu =3D NULL; } =20 + if (!irqfd->irq_bypass_vcpu && !vcpu) + return 0; + r =3D kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, host_irq, irqfd->gs= i, vcpu, irq.vector); if (r) { --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC3E2289E2E for ; Fri, 23 May 2025 01:01:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962069; cv=none; b=Lfxg5yvcBqfIMZQibMidGXcDe8E03PEpX8gjYFtCua1aXn4a5gRZK08+6SBiNv8yWTS0N86NVJJcPTOmNF8e7Me+FAsvy+YziJbZrg14p6C4jSJyVMO6PkX3Uex/PoyEV9wEu6Q6spTsDJHBDDfG/KZE4vKs2zyKkovtMZfxbIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962069; c=relaxed/simple; bh=7dnveniOu+/d08DxHilZb5jCCAKl0BL94CVVwVEl7eU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RpH47N73I+HHidB8P0L+AsLKagqyhMewZL/M7PbFlu5GB/rHA0dJgIjHCx+/0cWkvstIPfcjdcEkTxSiWoz1+xU8gwewQM+TXOQF9DXnodNaAfB23Oeyw+JPI6wQWjWEn/2oLDPJhe1hwyRbgmquF5X1UsPH5/3/gWWkKzJgGIg= 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=KnL0TPZA; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="KnL0TPZA" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-310c5c2c38cso1551068a91.1 for ; Thu, 22 May 2025 18:01:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962067; x=1748566867; 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=V6x9LNMhumiNfsLAp1ILmLoBe8XRi6VNornrEAAWP04=; b=KnL0TPZA+CahvQD/gJb50jqsiUFpCFIcid1ZqV8Q0xzi+b2IJsIZh+rDJYd2u7uxa8 +6dTiTXb7IHbUUykyq8AXkR2jKl1uOuFFsFWUfbC9NyEDmjR5tJ88vwwARn3Mnedz6cP QXn1Igd188+N5yWsjf0S1c0iQLbTIB8miYMUIOrCPP53yxCS75oo6Jld8PX59cRwRICX aMmCrng/6onslccskuUX+dp2o81HycUoN1E6MPObYFL05QYsyH/nY6BuyKr+nuvGt08S mi5mj9rrdL3u8NXpUI9d9S0b0GUhvVR1BLei+4EV7fpTyIuplULdnEPv7K1tBzRIcvSN mrVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962067; x=1748566867; 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=V6x9LNMhumiNfsLAp1ILmLoBe8XRi6VNornrEAAWP04=; b=lNHF4zI4UCSfYEiTaMTSrGrmWNvk88by6Y7S7hn3UjnwO875LUtTH2kq0nfcmFFrJ9 kuQYgf+eZhEFhrZ+QsXDM4dZbTg57lCc2GhD+Q8lmPhineHdNKGFf49aoEEO4CMHh1hG P13ZI/yOMl/P/Sk2OZ88v5hcPAk0PXtAC6FiciseOrknQh0RJlDDj929jPmz7rIccu7W oQYbhRwTZt7xCGAvRxJOXhKOfDLCZMzXIVTx5gFu5evzIaWOe0caCXPbtenVMAQ6eteP 13qA8TJ5MxsQaETc5Giphr0b2lwKeiI7W7XM3Jmpw3uxhVDyFk3auaZj92JH9rjtZuim CMcQ== X-Forwarded-Encrypted: i=1; AJvYcCVIn+OMa9QGntjgSTDZ3s2Jidd5iTcux6/kgkAE/KP6BweE9JSO5QmfjZK8jVTm35a4/o4dlBhkQJtolVk=@vger.kernel.org X-Gm-Message-State: AOJu0Yw4Qcrc2WrhYmoqf5veS59QBqeQZKFZwnBEXLwA4i1qhr8xTYWn 6cIY2DFIXxX6PxI4EPZZ1OrtCSCTB0mGlKbM7flgNqxczVcD3/KVVQsx5YLDzvrybGzLTJoy77t tTlO4OQ== X-Google-Smtp-Source: AGHT+IHFMNVKJa5e8umXYgZ4QtpyHnpqS+oxhyJku1m1yyj6LMAR+nnLi5IpcTUffXcAu8Fv+d+vw5Lcpqc= X-Received: from pjbrr6.prod.google.com ([2002:a17:90b:2b46:b0:30a:89a8:cef0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:c883:b0:2ee:e518:c1cb with SMTP id 98e67ed59e1d1-30e830c797dmr41531451a91.7.1747962067465; Thu, 22 May 2025 18:01:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:39 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-35-seanjc@google.com> Subject: [PATCH v2 34/59] KVM: x86: Don't update IRTE entries when old and new routes were !MSI From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Skip the entirety of IRTE updates on a GSI routing change if neither the old nor the new routing is for an MSI, i.e. if the neither routing setup allows for posting to a vCPU. If the IRTE isn't already host controlled, KVM has bigger problems. Signed-off-by: Sean Christopherson --- arch/x86/kvm/irq.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index 67fc8901d15f..19fa3452d201 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -618,6 +618,10 @@ void kvm_arch_update_irqfd_routing(struct kvm_kernel_i= rqfd *irqfd, struct kvm_kernel_irq_routing_entry *old, struct kvm_kernel_irq_routing_entry *new) { + if (new->type !=3D KVM_IRQ_ROUTING_MSI && + old->type !=3D KVM_IRQ_ROUTING_MSI) + return; + if (old->type =3D=3D KVM_IRQ_ROUTING_MSI && new->type =3D=3D KVM_IRQ_ROUTING_MSI && !memcmp(&old->msi, &new->msi, sizeof(new->msi))) --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8843928AB1E for ; Fri, 23 May 2025 01:01:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962071; cv=none; b=OA2KPhewqUXWiHAxnqWeIcPnL6Ja+J5VBoLaSRhHOKRfTkf7svrb64CypoWm2wOJroSv90DFdFNnQFkeB5AySs3kfji+bHM3xLsVsNHHlKphXPPLK4TftKKqToqPrdDfqqvO9BsswxP3bwL2W4KqANh4e4YE8GGvrOrRusg2lzY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962071; c=relaxed/simple; bh=U0lXuztKsbQ6vOwPvXvlCRdfHq5pEEfqlrsbN4zSEW0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oX19wd43UGoZpdiXtQPBOxclnQn0EvXhzsM4bdxTyzrIrOj9YDp4ILy5rWRX361SQsHJDcVcVqxPV7jVGiNmlsiuEYzQMMwhat4nIJtDw4121YS482Dt11KDTDoa5pGgSdXg23kpWTWhNkqSwRjkvqcKbymWBcQGfC0Rt1YbzD4= 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=MVanIT1O; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="MVanIT1O" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-310e7c24158so475009a91.3 for ; Thu, 22 May 2025 18:01:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962069; x=1748566869; 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=kq0Cy8cDTnLLgDeX9paVAv72AILfWzwvKgS57Jhig+k=; b=MVanIT1OoPcg09dBZnLcXW8De7OJ0LFWlYH8rZ4qfuVvFuybkdcwngH6gPaG/uXfI/ WQkQ6PgcjGDQFxVSowHjupTbUM0MjccPDq5iZazVUufPUTzFKBAZ1s0M5zxnFYnBMxI4 kvUQ8CiGMsaMtxcV7A82nRgGMhXvDrJCsshx/qcFcaMvCxYTYGni8V70/eiRaeHGGRcM NZUHYSNkY4UD3z5yNlidzEIwbg/EPjuYALj/t0gUay1rpRNxUFNvwp8VmQCpOyXx2SiI aU5NZFgu/8b86iT6DAhrWrhpdBV+YTtJC/YE2JP+7O7Itq0NlGsrlQAb7++rFEkuVPcv KlJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962069; x=1748566869; 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=kq0Cy8cDTnLLgDeX9paVAv72AILfWzwvKgS57Jhig+k=; b=Yvk/vI11L4AnIlO1Dt0lK9Mh4vPcdKxKhnR/bmsOnYc2U/R8izgYzizHU9WE8/UKg2 E9PqXXVvlK5P5L1J1z9x/orC7g+O+eLKCIrL6SL5L813IWC4TfbRarMQFe2023Rei2qY cYS0sxMklgFN5wX70NKuC+768twMFhVtFbQjzaq6bJKnaySc2876gbDGoTxsxDwmqyGe OlhJInC8XrPxBmATxTNoIB5wGwyRNbb5+n2aFZX5g5DjprQyV1NRvX4U1HDkAAA1YUa+ TAKSiyyOH+1S/M3Grfq8q1sTQpj4xP821+mhk8UmYxzvPBRSxfZwB0WFliWE1BwCfgaZ v8yw== X-Forwarded-Encrypted: i=1; AJvYcCXWl7lGhd9q+3O4T1Qr6uJWNtdJ5/17MmW76InIH6MtOXvnNYBXjrRMtpRGCvuBMQbhXmcbcvsLpvGZaFk=@vger.kernel.org X-Gm-Message-State: AOJu0YxaQuH3izEB9Xhh7vZowkG8yJlYKYuzjgKep0U90U/CYWEnZSnd 2UOGnal8D5/8FLFVntrmf4Jnru+T6JUWRMTDTldS9PdwGU6uAt+k6yDEfrEk3u2dwAXKaakt6l/ tumWh2Q== X-Google-Smtp-Source: AGHT+IFtGk6LcnmjIj0taAHjAXYUJ8hEzTCCNrk1y9BpY/2QgpzZNWMJPoqJgLycpc+0uJxQVWlfD9j1X1g= X-Received: from pjbpb5.prod.google.com ([2002:a17:90b:3c05:b0:308:670e:aa2c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:ec8c:b0:30c:540b:9ac with SMTP id 98e67ed59e1d1-30e830ebd3fmr44143328a91.10.1747962069037; Thu, 22 May 2025 18:01:09 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:40 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-36-seanjc@google.com> Subject: [PATCH v2 35/59] KVM: SVM: Revert IRTE to legacy mode if IOMMU doesn't provide IR metadata From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Revert the IRTE back to remapping mode if the AMD IOMMU driver mucks up and doesn't provide the necessary metadata. Returning an error up the stack without actually handling the error is useless and confusing. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 97b747e82012..f1e9f0dd43e8 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -769,16 +769,13 @@ static void svm_ir_list_del(struct kvm_kernel_irqfd *= irqfd) spin_unlock_irqrestore(&to_svm(vcpu)->ir_list_lock, flags); } =20 -static int svm_ir_list_add(struct vcpu_svm *svm, - struct kvm_kernel_irqfd *irqfd, - struct amd_iommu_pi_data *pi) +static void svm_ir_list_add(struct vcpu_svm *svm, + struct kvm_kernel_irqfd *irqfd, + struct amd_iommu_pi_data *pi) { unsigned long flags; u64 entry; =20 - if (WARN_ON_ONCE(!pi->ir_data)) - return -EINVAL; - irqfd->irq_bypass_data =3D pi->ir_data; =20 spin_lock_irqsave(&svm->ir_list_lock, flags); @@ -796,7 +793,6 @@ static int svm_ir_list_add(struct vcpu_svm *svm, =20 list_add(&irqfd->vcpu_list, &svm->ir_list); spin_unlock_irqrestore(&svm->ir_list_lock, flags); - return 0; } =20 int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, @@ -833,6 +829,16 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, if (ret) return ret; =20 + /* + * Revert to legacy mode if the IOMMU didn't provide metadata + * for the IRTE, which KVM needs to keep the IRTE up-to-date, + * e.g. if the vCPU is migrated or AVIC is disabled. + */ + if (WARN_ON_ONCE(!pi_data.ir_data)) { + irq_set_vcpu_affinity(host_irq, NULL); + return -EIO; + } + /** * Here, we successfully setting up vcpu affinity in * IOMMU guest mode. Now, we need to store the posted @@ -840,7 +846,8 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, * we can reference to them directly when we update vcpu * scheduling information in IOMMU irte. */ - return svm_ir_list_add(to_svm(vcpu), irqfd, &pi_data); + svm_ir_list_add(to_svm(vcpu), irqfd, &pi_data); + return 0; } return irq_set_vcpu_affinity(host_irq, NULL); } --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29B1028B7C3 for ; Fri, 23 May 2025 01:01:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962072; cv=none; b=raM6c9STilcADA7DlvR5VvMROnxlVOd3mCtkSesFD2ppKiIhJlr830TFBYIpRr6Iws+fkDAPaJStR5h1i/zoVIYeOZD3YHYLy4o+UaOl7p4VHhBOIsIHsmFOWnHmJadpsaDGaQllWJXNwRSlXcQPY/Htv9SUZZ1kNhZgS0noIgc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962072; c=relaxed/simple; bh=rr28ytoqBfvrEIg2zljySxOCv3GKwF/AvjFSbsb8WHQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=U64w95kZKSZMGUD15bITOcfXOGvhzwKHNU+Uh6hsnf49COdl+679STJ/r87elKagnbqgW5xTR9eMegTkLx+o1PjUkZ1ouEHsfXSJhJBy+JCDG3wFs/tvICtgqBSTuv5zu8a0ZcW4toCCGsnkSzpEZOe9mJfc7O+ogNfyiS0GQTw= 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=RqA2lqhX; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="RqA2lqhX" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30ec5cc994eso4818073a91.2 for ; Thu, 22 May 2025 18:01:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962070; x=1748566870; 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=w0kCfWU98XQwV041YO1Y2EVRT9qYS2hPdBh6QvqO+sc=; b=RqA2lqhXcZqWKtxuE/R1r0wtrDw0ToAXQxK6zRQkJaLdL1rOThSjI01dYaFpAmZBpe y1Y81t3bDKa0AcdO5bEvydyCJnorgnFUwYIkoUWLOURvh9DC4Uav3eiVUcjho2yhg6SQ a+xvmtIi+zXTAqq1yCZr65bWtqi8L9h6Fz9h1DZxZZwuVHInWkn5as5TJA4d1J8x99aG 5I+kg7lHbx7ybm51VpYG0UYrKPmVgjQ6u9S1/aDoJR7bg6Ij2RGDTehXoYPMpspc6imr 2C/nfoSfuUOHVmSDD/eCB4ZKBbrBCs9Fngb4OCvdggtB8/tk3M6ZdKbBwox0orbxlIO+ FijQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962070; x=1748566870; 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=w0kCfWU98XQwV041YO1Y2EVRT9qYS2hPdBh6QvqO+sc=; b=fyswFzpM078+8gogG0LvvEjM6ymdpp+zwz3sluF6ItSoCq98efWuuZb6q2uxy/XDI/ 6OypkqZIo+l36gUvsAIY0oVq+3Dt7uzdkypANQITH3htbrhq50gyQtSwAPOBsZlLEE/g MwYDrxpEMDJeudaAbQK1VWHefZof53EVanE/c3gYnCcV+aCYyks8B4EtUNgYjDN8taGS ODx2ng0mOheMtxofqO6iz+0ARPPhMN/ZdFu4XWrBLwMn2sfqrp8W6nukMwysvZxShqW3 zH1TvDUt1cHiOHP+nT/4Q1E5kfIek06HoyComhfnd9bPPq3Th1AMpAOrnngpvhIfJMzo P+eQ== X-Forwarded-Encrypted: i=1; AJvYcCU0hYPtIgo33nxHk2a4AiRp+dNzdeQG4U7H0YR9vrg6niNT67RGBqSRwmy9qW/3oUb3cRiB0BmwDHehvgM=@vger.kernel.org X-Gm-Message-State: AOJu0YxAxy6ttEQJvt2wY1bjRkDst3gVrkUfALG28pXAB39hgJnPZ0A2 TteoqmxtyAndsfEHd+eXuE3apWpOBMmglLvTXxIJ+R8U4b5fO6wJw9bDcMZWow84PLqhrZ7b6jL 5aC6H3w== X-Google-Smtp-Source: AGHT+IGjkNbeOSur3vbEkZXFamu+YYNEKfRbtS/EU3c2ZsJsvBusKBLrOpiwTklBFI3+nSzpA/Uw0Nr96q4= X-Received: from pjx8.prod.google.com ([2002:a17:90b:5688:b0:30a:31eb:ec8e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:28c5:b0:2fc:a3b7:108e with SMTP id 98e67ed59e1d1-30e7d4fea80mr41800434a91.4.1747962070676; Thu, 22 May 2025 18:01:10 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:41 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-37-seanjc@google.com> Subject: [PATCH v2 36/59] KVM: SVM: Take and hold ir_list_lock across IRTE updates in IOMMU From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that svm_ir_list_add() isn't overloaded with all manner of weird things, fold it into avic_pi_update_irte(), and more importantly take ir_list_lock across the irq_set_vcpu_affinity() calls to ensure the info that's shoved into the IRTE is fresh. While preemption (and IRQs) is disabled on the task performing the IRTE update, thanks to irqfds.lock, that task doesn't hold the vCPU's mutex, i.e. preemption being disabled is irrelevant. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 55 +++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index f1e9f0dd43e8..4747fb09aca4 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -769,32 +769,6 @@ static void svm_ir_list_del(struct kvm_kernel_irqfd *i= rqfd) spin_unlock_irqrestore(&to_svm(vcpu)->ir_list_lock, flags); } =20 -static void svm_ir_list_add(struct vcpu_svm *svm, - struct kvm_kernel_irqfd *irqfd, - struct amd_iommu_pi_data *pi) -{ - unsigned long flags; - u64 entry; - - irqfd->irq_bypass_data =3D pi->ir_data; - - spin_lock_irqsave(&svm->ir_list_lock, flags); - - /* - * Update the target pCPU for IOMMU doorbells if the vCPU is running. - * If the vCPU is NOT running, i.e. is blocking or scheduled out, KVM - * will update the pCPU info when the vCPU awkened and/or scheduled in. - * See also avic_vcpu_load(). - */ - entry =3D svm->avic_physical_id_entry; - if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) - amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK, - true, pi->ir_data); - - list_add(&irqfd->vcpu_list, &svm->ir_list); - spin_unlock_irqrestore(&svm->ir_list_lock, flags); -} - int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, struct kvm_vcpu *vcpu, u32 vector) @@ -823,8 +797,18 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, .vapic_addr =3D avic_get_backing_page_address(to_svm(vcpu)), .vector =3D vector, }; + struct vcpu_svm *svm =3D to_svm(vcpu); + u64 entry; int ret; =20 + /* + * Prevent the vCPU from being scheduled out or migrated until + * the IRTE is updated and its metadata has been added to the + * list of IRQs being posted to the vCPU, to ensure the IRTE + * isn't programmed with stale pCPU/IsRunning information. + */ + guard(spinlock_irqsave)(&svm->ir_list_lock); + ret =3D irq_set_vcpu_affinity(host_irq, &pi_data); if (ret) return ret; @@ -839,14 +823,19 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqf= d, struct kvm *kvm, return -EIO; } =20 - /** - * Here, we successfully setting up vcpu affinity in - * IOMMU guest mode. Now, we need to store the posted - * interrupt information in a per-vcpu ir_list so that - * we can reference to them directly when we update vcpu - * scheduling information in IOMMU irte. + /* + * Update the target pCPU for IOMMU doorbells if the vCPU is + * running. If the vCPU is NOT running, i.e. is blocking or + * scheduled out, KVM will update the pCPU info when the vCPU + * is awakened and/or scheduled in. See also avic_vcpu_load(). */ - svm_ir_list_add(to_svm(vcpu), irqfd, &pi_data); + entry =3D svm->avic_physical_id_entry; + if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) + amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MAS= K, + true, pi_data.ir_data); + + irqfd->irq_bypass_data =3D pi_data.ir_data; + list_add(&irqfd->vcpu_list, &svm->ir_list); return 0; } return irq_set_vcpu_affinity(host_irq, NULL); --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42B69219A90 for ; Fri, 23 May 2025 01:01:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962074; cv=none; b=CjIetZggVtLiLYXegKI7ptGcQLYv9fJ+haxih4A9xztd8qUSoHb3Rl515YZdJ9+5tPpLRWDtK4JSSh7I8VyA48y4qUuZcL5Kc7WJBX+SyOBu/so+wrs0i0Z5BN3j975CPfxnQ234HH9YT/iWXqgXgE3gzH23sLl5a77ftHeNMrk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962074; c=relaxed/simple; bh=OoaKuYJuGBNrzRs4FOlcqG4EfEZ0eKgNguock8Nm660=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mYfwv3bQ7lAFtYgjzQRwFtWJhedH0QFl+MdP9HSGVX11HySheUriJ9ftXJxFoDWjYtOia4Z416G8S/cy/ckfEkjZneACxpa3ldf88qptpuU/Sqgf7MytwMLR0cM3e24aUOAfIS9khVGd45Cuh8D4Pov3cXBsBvjLoi37im3uHEk= 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=UlyUrYEj; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UlyUrYEj" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-310a0668968so2255193a91.0 for ; Thu, 22 May 2025 18:01:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962072; x=1748566872; 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=QZ/HVVcKE1D9F/RksSitv8YykybKRfr9scegH3bUh2Y=; b=UlyUrYEjdVZunsrh+BToNNWHq4fLxFgLdK47+ZROj1lhq4xu+f2Jh7zpXsgtSRQneM 8VwXh5Pbas0WvMpjThAyf5tojMoxmneOk76DK98z6UlDVDPTzibZZ1yRkWeFPq8w2+0i LcQ+wJCmsnDmCw9AkxX71fHfK0TpJI250QJ+o5F9u6yAIeRIFl3xj0mcz6zkiPGJVdFr hNz2T5T7Ydi2+xI8JTqyTu9e5fCWtWn46A2US6GD8bPDvnqw9aETkRk37rLQL3hOGUIH n1M1iUh4l/lBJ83Rcxj67qv2YNapTTf820bhVL2nGMrWGw/vXA2xc26CF3DrouLaHuNj zJUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962072; x=1748566872; 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=QZ/HVVcKE1D9F/RksSitv8YykybKRfr9scegH3bUh2Y=; b=m0JWdbY/3nVvP7HdhTIEpreFXgIVyOkz3q/2yMEdjkYVUrI7cxbHNbLhEQDf/h7Vs0 UoBFaBYABLtqE1FYTBNuM+olQarNpM7rmmf5CiiPteduaN4X9VHxbBDsoEnCbA7C0nmc X9hCJ9xf1c+5LjQGnAtKL/OEs1iAtsleFRIOJvltq42UYrh5msPxzdaPYpNTLMe/o+jb A4e1DZZUoA8ZnvpH9C5D6RqL9Fy7H8mlUpJfZGv+wG9Nqd+8V1uMqkvPzjItnyaJ1nHM OtGXL78TxhW+7D+HmqKxJ/igIwC3YMjHF08Fsq1wlmyGJ8JxOGycAYuhlUFxuD4fm+ty HS3g== X-Forwarded-Encrypted: i=1; AJvYcCXBOzYqbkWdur+82vX8p5QrUf+6Bt6pwPzmFqPRemh6m4RW96fubWgtoQY5Vwidr0/UX6s5dF7xl6M7xmk=@vger.kernel.org X-Gm-Message-State: AOJu0YwcW1YraU2mAeXeCGiVDk7VEqiIZzXPk8wpy6IVR2TdFdREtvKo B7/2InYA1S42RvKFe/fybUTLldDuz/psjHPeA8aSZ6hQQdUaALs9glLZ4BxpoixwoU8oAr3rl9O izl7hLg== X-Google-Smtp-Source: AGHT+IEivPKszStQSfYiwNpJ7/umbLGOogBna7x7mx10TTJkqWGmqjwZ44z3FVYVN+th/LcfZ1pWsae8+lg= X-Received: from pja14.prod.google.com ([2002:a17:90b:548e:b0:2ef:d283:5089]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3b86:b0:30e:8c5d:8f8 with SMTP id 98e67ed59e1d1-30e8c5d09b3mr44884823a91.14.1747962072336; Thu, 22 May 2025 18:01:12 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:42 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-38-seanjc@google.com> Subject: [PATCH v2 37/59] iommu/amd: Document which IRTE fields amd_iommu_update_ga() can modify From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a comment to amd_iommu_update_ga() to document what fields it can safely modify without issuing an invalidation of the IRTE, and to explain its role in keeping GA IRTEs up-to-date. Per page 93 of the IOMMU spec dated Feb 2025: When virtual interrupts are enabled by setting MMIO Offset 0018h[GAEn] and IRTE[GuestMode=3D1], IRTE[IsRun], IRTE[Destination], and if present IRTE[= GATag], are not cached by the IOMMU. Modifications to these fields do not require= an invalidation of the Interrupt Remapping Table. Link: https://lore.kernel.org/all/9b7ceea3-8c47-4383-ad9c-1a9bbdc9044a@orac= le.com Cc: Joao Martins Signed-off-by: Sean Christopherson --- drivers/iommu/amd/iommu.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 36749efcc781..5adc932b947e 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3986,6 +3986,18 @@ int amd_iommu_create_irq_domain(struct amd_iommu *io= mmu) return 0; } =20 +/* + * Update the pCPU information for an IRTE that is configured to post IRQs= to + * a vCPU, without issuing an IOMMU invalidation for the IRTE. + * + * This API is intended to be used when a vCPU is scheduled in/out (or sto= ps + * running for any reason), to do a fast update of IsRun and (conditionall= y) + * Destination. + * + * Per the IOMMU spec, the Destination, IsRun, and GATag fields are not ca= ched + * and thus don't require an invalidation to ensure the IOMMU consumes fre= sh + * information. + */ int amd_iommu_update_ga(int cpu, bool is_run, void *data) { struct amd_ir_data *ir_data =3D (struct amd_ir_data *)data; --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 B1CC428DB72 for ; Fri, 23 May 2025 01:01:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962076; cv=none; b=A2YvetIu7+2j++jHmdeRL1N3UrNgALL+s3N7V7jS3oSavzZGg2mgbsLG5wBngf3V/HdsqOWBMCE9C1knJjSaP+U5fwm+0tb2Kfcj3yeoS/3qWDh99Kk8aIUPXcl06BifQBfKia+f2GLTMyCLtotMkRSxJ4yfcSOu1zBAN7KZ7G4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962076; c=relaxed/simple; bh=GqaZYSOh5Yivyj2qFqb00jJ6hR1JshloGdmkeWMFgz8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EdOvNdmI++SfbkcorBX+Un0DoQ/btuVBrgBjCgc7ANXyugLzkPxQeE1WfWA2MnS9vYrPUm3b5WZyiY1xCnRfYrR3EB6/QjsLuyV2kWctxCBbl+xCTX/fNRArFHRmK90Xelbm2QonGwBSzX+dl5S9cliFhv287k5295MFvNCKJNw= 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=ZYWqPcsg; arc=none smtp.client-ip=209.85.210.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="ZYWqPcsg" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-742951722b3so7407973b3a.2 for ; Thu, 22 May 2025 18:01:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962074; x=1748566874; 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=x0SQOCMzf8MbWWt1DyUg5caMlYYVCTMHKmyxGamNwc0=; b=ZYWqPcsgQ0EtRfq32IQ9P7EdSYZUGVH+Eab8vog4S+NQ8yb+GDRDFHtFoRhdotdQk9 5zEXB5s8zu81Wv52ZC/gXZUIH84e+AI8E8MlLZOQTHuYMrXM4OICKk+Zwf7BcZ8h/LMD kA0aFHh3XAEjR1qyZNOyVOgBhqxqDrxIOpuxa4qRg0DEa3h+BjeCX0gv70aZCLqxzFtz am4gcL/8TuXqPfCIlReM196o/iFykBJdIMJf+WDxXGqRIxesLf7wiCOf6CqSeN1rSTvz Wgf82OuOg4ODqI/+noJJp8ICv7ESpjdQnEnzSomqQZkqSgs/NqexXsLqMYKvy0RgNXn9 6KMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962074; x=1748566874; 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=x0SQOCMzf8MbWWt1DyUg5caMlYYVCTMHKmyxGamNwc0=; b=Xy756GdwndO3jwD2VegM0GyGZWcpp74Q8jH79YikatModW4pLIL8ptsG1UHGur1Kgj sMn2jwYJrg/VFRbyLxHiSScBa4Ih2W9EQc1Gn5RBRZVDpR/3f3e9IYZ6DpKL6CkQ/vkE sb09tExEyemm3kKn073e020zei7PklOVI4cAiTi97oTtw+itqBVZ0yKgbr/FCmQJURL6 i5NImcWnms807udQn5nKQlO38ReBodLbpqu/p3jDlE/2ir+qNYSPSnKRapqH2DR+9KcA oV5r+l8zBJ+X5PpgVg8LbBSlQwbSNE9OnCmSJ8ybxAMLI6vOdr4HDsehxuS9/9KGU9CM izqA== X-Forwarded-Encrypted: i=1; AJvYcCVcmwqVQJoRqb7FdOZeUrTxfmAP5WaZnBcJACGj5Ydw6q1B2ZhvFHK4xqhbwzWIGNv28rkopt/kEXjYaf8=@vger.kernel.org X-Gm-Message-State: AOJu0YzlstFHdH4s7OVg73ghzKPUS/jVATg3DN951hSKpHhNbJPrsxuH GyEIgKQ/vtiq2iz8olq5/Wc49Wf/myrGTcTb3j7DHLMvhs1KUVs87F0t/AOJC9w7EfApoc+JZhr /2ImAuw== X-Google-Smtp-Source: AGHT+IGnAWR1Ge5/kNVjom0UceZOieMbcvP7V10C2KnBUEzzCVxlhXi3O6dwFOIaCRRkmy7JIN59ucCqu8k= X-Received: from pfbcd23.prod.google.com ([2002:a05:6a00:4217:b0:736:7120:dd05]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:e0e:b0:736:9f20:a175 with SMTP id d2e1a72fcca58-742acc8d8f4mr36938652b3a.2.1747962073957; Thu, 22 May 2025 18:01:13 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:43 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-39-seanjc@google.com> Subject: [PATCH v2 38/59] iommu/amd: KVM: SVM: Infer IsRun from validity of pCPU destination From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Infer whether or not a vCPU should be marked running from the validity of the pCPU on which it is running. amd_iommu_update_ga() already skips the IRTE update if the pCPU is invalid, i.e. passing %true for is_run with an invalid pCPU would be a blatant and egregrious KVM bug. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 11 +++++------ drivers/iommu/amd/iommu.c | 14 +++++++++----- include/linux/amd-iommu.h | 6 ++---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 4747fb09aca4..c79648d96752 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -832,7 +832,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, entry =3D svm->avic_physical_id_entry; if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MAS= K, - true, pi_data.ir_data); + pi_data.ir_data); =20 irqfd->irq_bypass_data =3D pi_data.ir_data; list_add(&irqfd->vcpu_list, &svm->ir_list); @@ -841,8 +841,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, return irq_set_vcpu_affinity(host_irq, NULL); } =20 -static inline int -avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r) +static inline int avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, i= nt cpu) { int ret =3D 0; struct vcpu_svm *svm =3D to_svm(vcpu); @@ -861,7 +860,7 @@ avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, = int cpu, bool r) return 0; =20 list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) { - ret =3D amd_iommu_update_ga(cpu, r, irqfd->irq_bypass_data); + ret =3D amd_iommu_update_ga(cpu, irqfd->irq_bypass_data); if (ret) return ret; } @@ -923,7 +922,7 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) =20 WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); =20 - avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, true); + avic_update_iommu_vcpu_affinity(vcpu, h_physical_id); =20 spin_unlock_irqrestore(&svm->ir_list_lock, flags); } @@ -963,7 +962,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) */ spin_lock_irqsave(&svm->ir_list_lock, flags); =20 - avic_update_iommu_vcpu_affinity(vcpu, -1, 0); + avic_update_iommu_vcpu_affinity(vcpu, -1); =20 entry &=3D ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; svm->avic_physical_id_entry =3D entry; diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 5adc932b947e..bb804bbc916b 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3990,15 +3990,17 @@ int amd_iommu_create_irq_domain(struct amd_iommu *i= ommu) * Update the pCPU information for an IRTE that is configured to post IRQs= to * a vCPU, without issuing an IOMMU invalidation for the IRTE. * - * This API is intended to be used when a vCPU is scheduled in/out (or sto= ps - * running for any reason), to do a fast update of IsRun and (conditionall= y) - * Destination. + * If the vCPU is associated with a pCPU (@cpu >=3D 0), configure the Dest= ination + * with the pCPU's APIC ID and set IsRun, else clear IsRun. I.e. treat vC= PUs + * that are associated with a pCPU as running. This API is intended to be= used + * when a vCPU is scheduled in/out (or stops running for any reason), to d= o a + * fast update of IsRun and (conditionally) Destination. * * Per the IOMMU spec, the Destination, IsRun, and GATag fields are not ca= ched * and thus don't require an invalidation to ensure the IOMMU consumes fre= sh * information. */ -int amd_iommu_update_ga(int cpu, bool is_run, void *data) +int amd_iommu_update_ga(int cpu, void *data) { struct amd_ir_data *ir_data =3D (struct amd_ir_data *)data; struct irte_ga *entry =3D (struct irte_ga *) ir_data->entry; @@ -4015,8 +4017,10 @@ int amd_iommu_update_ga(int cpu, bool is_run, void *= data) APICID_TO_IRTE_DEST_LO(cpu); entry->hi.fields.destination =3D APICID_TO_IRTE_DEST_HI(cpu); + entry->lo.fields_vapic.is_run =3D true; + } else { + entry->lo.fields_vapic.is_run =3D false; } - entry->lo.fields_vapic.is_run =3D is_run; =20 return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, ir_data->irq_2_irte.index, entry); diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index 99b4fa9a0296..fe0e16ffe0e5 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -30,8 +30,7 @@ static inline void amd_iommu_detect(void) { } /* IOMMU AVIC Function */ extern int amd_iommu_register_ga_log_notifier(int (*notifier)(u32)); =20 -extern int -amd_iommu_update_ga(int cpu, bool is_run, void *data); +extern int amd_iommu_update_ga(int cpu, void *data); =20 extern int amd_iommu_activate_guest_mode(void *data); extern int amd_iommu_deactivate_guest_mode(void *data); @@ -44,8 +43,7 @@ amd_iommu_register_ga_log_notifier(int (*notifier)(u32)) return 0; } =20 -static inline int -amd_iommu_update_ga(int cpu, bool is_run, void *data) +static inline int amd_iommu_update_ga(int cpu, void *data) { return 0; } --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D6612918F4 for ; Fri, 23 May 2025 01:01:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962078; cv=none; b=N5f2BR51JrCL8u+gr9NpkNArL74smARGs+PuaYA2ULRX0Iv+R7YpbWHGmNR+e0UoX2EGU62Twgi3rkvMQ6IrFAidsGmZapf7UcO6V6/WzmjjMZwPtRSHF3howGVUryfOymT4/GE6Q4M2k6cNPtVDkLyuxKRnGBu+O5DuaMXu1aI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962078; c=relaxed/simple; bh=OTyd4z4G26pTKv/gtSvFLn3KOsoDuaV6Uaii5aU2CKU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HrrVirJrvxrrwGh6IhleyqRmR9E6HfnFoB4W2Qz28wR1chuaxb9kYOMb36uPsSI1n0C71hneEpZf3gRH2+sHdsKJctGAqLsZbrPgHwG38wh7xvQd6Y4Ekhha92ST76bN6xdSdRFpdb2lrNCI2dRAsqHuFHgrBfykTU8o3OY+DHA= 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=QebTaCgn; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QebTaCgn" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2320502023fso54840945ad.2 for ; Thu, 22 May 2025 18:01:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962076; x=1748566876; 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=v1/xwskHEy3a7iwKlZ21odoIzGy2RsxvZsd2bkDzu00=; b=QebTaCgnAdCEG8ZT/5OYz/P3ldJeWrienJm9h910NBlbxBoLYrG8z+gdqBoa1aKgBP JBgra85HFKiUJRh3mfHn2Wg/X86skXciEmg39xpcqA4g/yr4v1RwdPhFgS5ApFcsexpP TT+A3sxX1S76CQ5quMrWBnnpXuJ9ZxBn1pRLQ5Oi/4eEUSJJWPUl2hVmZXY8p9/RzkH/ PLjo4q/e7dy0xkNYI6qKV/f9hRN5B0p/tPf2ipCb7HYhHDHr6cZHPY4opTqRkOUKw2kX 8Ch04U1hJOORFwH2/CDIcU/NI7vz+mhWgAXZZyaIhuNeU4gd/2dR1ROEljLuVVk6+NC5 gIug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962076; x=1748566876; 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=v1/xwskHEy3a7iwKlZ21odoIzGy2RsxvZsd2bkDzu00=; b=Cz14RCulaWdU4zOSm7mwcIoOlIUo99TalVq7qHVgoh3g38G/5M9blFxArSw+F4w/wl sXTUNV58imgiMTXl/ZxG+AwA/ZIDf6vIHvO+UNH9luY+LAvX8WuGtJ7Eu3hP9SRgh0oE vEQXwEoPYjhF2WaJEKsJ7r2Uhz7HioUM16mAyn0M8TvxMVFgybIkYyFlNdVlWn1Pac9X k0DqKr9M8yREmKhOoR3s9p5UKvBkJgg09QfWO4xEP7zQolrPSKAEyajVO/o88hoyoVTn zsRQcxOMwkWD292Zs1cse4dHC7f4RE5WfXINiH0shBfN2yTVN59NWOQXchCGGIw/NEvC yBwg== X-Forwarded-Encrypted: i=1; AJvYcCWJ8S6yyIOlfj0BuMKR/Quz957KzurskjRRsCtGJGATMuu+RLOsqTzs4HEkbpuazrfZ3PYmyh2MOYH2P34=@vger.kernel.org X-Gm-Message-State: AOJu0YxP+Ry7xnN8w39na/iKi//9fv9uqqsEZBKg1efSRCqQ8BQtmS9P 2h6KARBf1ijzMnAScvmEM5eOPFO2E1ztJ8Rwm55/a+qKaWJYHiKSPe8trNWYhWk94lOTge1K/hB ddNHJzA== X-Google-Smtp-Source: AGHT+IHjCMH4aai/H7vZNCaUw2MuOMj84va8p0PVDKonC3W735IHcQVEgyMS2pGd5RXUwW7ue5Pu6DTe2ak= X-Received: from plbq8.prod.google.com ([2002:a17:903:1788:b0:223:551e:911c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1a43:b0:215:b9a6:5cb9 with SMTP id d9443c01a7336-233f21be86dmr16748935ad.5.1747962075669; Thu, 22 May 2025 18:01:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:44 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-40-seanjc@google.com> Subject: [PATCH v2 39/59] iommu/amd: Factor out helper for manipulating IRTE GA/CPU info From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Split the guts of amd_iommu_update_ga() to a dedicated helper so that the logic can be shared with flows that put the IRTE into posted mode. Opportunistically move amd_iommu_update_ga() and its new helper above amd_iommu_activate_guest_mode() so that it's all co-located. Signed-off-by: Sean Christopherson --- drivers/iommu/amd/iommu.c | 87 +++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index bb804bbc916b..15718b7b8bd4 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3804,6 +3804,52 @@ static const struct irq_domain_ops amd_ir_domain_ops= =3D { .deactivate =3D irq_remapping_deactivate, }; =20 +static void __amd_iommu_update_ga(struct irte_ga *entry, int cpu) +{ + if (cpu >=3D 0) { + entry->lo.fields_vapic.destination =3D + APICID_TO_IRTE_DEST_LO(cpu); + entry->hi.fields.destination =3D + APICID_TO_IRTE_DEST_HI(cpu); + entry->lo.fields_vapic.is_run =3D true; + } else { + entry->lo.fields_vapic.is_run =3D false; + } +} + +/* + * Update the pCPU information for an IRTE that is configured to post IRQs= to + * a vCPU, without issuing an IOMMU invalidation for the IRTE. + * + * If the vCPU is associated with a pCPU (@cpu >=3D 0), configure the Dest= ination + * with the pCPU's APIC ID and set IsRun, else clear IsRun. I.e. treat vC= PUs + * that are associated with a pCPU as running. This API is intended to be= used + * when a vCPU is scheduled in/out (or stops running for any reason), to d= o a + * fast update of IsRun and (conditionally) Destination. + * + * Per the IOMMU spec, the Destination, IsRun, and GATag fields are not ca= ched + * and thus don't require an invalidation to ensure the IOMMU consumes fre= sh + * information. + */ +int amd_iommu_update_ga(int cpu, void *data) +{ + struct amd_ir_data *ir_data =3D (struct amd_ir_data *)data; + struct irte_ga *entry =3D (struct irte_ga *) ir_data->entry; + + if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) || + !entry || !entry->lo.fields_vapic.guest_mode) + return 0; + + if (!ir_data->iommu) + return -ENODEV; + + __amd_iommu_update_ga(entry, cpu); + + return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, + ir_data->irq_2_irte.index, entry); +} +EXPORT_SYMBOL(amd_iommu_update_ga); + int amd_iommu_activate_guest_mode(void *data) { struct amd_ir_data *ir_data =3D (struct amd_ir_data *)data; @@ -3985,45 +4031,4 @@ int amd_iommu_create_irq_domain(struct amd_iommu *io= mmu) =20 return 0; } - -/* - * Update the pCPU information for an IRTE that is configured to post IRQs= to - * a vCPU, without issuing an IOMMU invalidation for the IRTE. - * - * If the vCPU is associated with a pCPU (@cpu >=3D 0), configure the Dest= ination - * with the pCPU's APIC ID and set IsRun, else clear IsRun. I.e. treat vC= PUs - * that are associated with a pCPU as running. This API is intended to be= used - * when a vCPU is scheduled in/out (or stops running for any reason), to d= o a - * fast update of IsRun and (conditionally) Destination. - * - * Per the IOMMU spec, the Destination, IsRun, and GATag fields are not ca= ched - * and thus don't require an invalidation to ensure the IOMMU consumes fre= sh - * information. - */ -int amd_iommu_update_ga(int cpu, void *data) -{ - struct amd_ir_data *ir_data =3D (struct amd_ir_data *)data; - struct irte_ga *entry =3D (struct irte_ga *) ir_data->entry; - - if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) || - !entry || !entry->lo.fields_vapic.guest_mode) - return 0; - - if (!ir_data->iommu) - return -ENODEV; - - if (cpu >=3D 0) { - entry->lo.fields_vapic.destination =3D - APICID_TO_IRTE_DEST_LO(cpu); - entry->hi.fields.destination =3D - APICID_TO_IRTE_DEST_HI(cpu); - entry->lo.fields_vapic.is_run =3D true; - } else { - entry->lo.fields_vapic.is_run =3D false; - } - - return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, - ir_data->irq_2_irte.index, entry); -} -EXPORT_SYMBOL(amd_iommu_update_ga); #endif --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC171293B5D for ; Fri, 23 May 2025 01:01:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962079; cv=none; b=Olb0Yi0NNJcr/VKT3PlSFWSas/J+OTjWWgVgzdX8cIuo9D+Li8f96Evj0+ZL0qSJtpd6FMIifVRHw2SHfcOOj34LYQWiwhnWx5C0oFrlLnJF0CILNLzWcLjR8Km8GlgNthjN2gTiTijmnxLOYk7WAUcf28o1rZc/p0e1omOlcQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962079; c=relaxed/simple; bh=gFtnllkdHoK++kmV2uIN5igfGRjKb467ITbvyuZHPpg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=a/VQmt7YHOD6UAb4kmzWe4Q6hovRYZ7TDbIkEOlvfo+fN+GHHZp1lx+iOFJrWswoOcn9AAQmVJkWkjfSRLB50rnC8SPDPH8cJMhRl8u9v+ChCDDM77Garbrokb7i/LOsZ7xxe286MwegIrjhaLNibp5T8ndGIguBwBo+Y2TSK0Q= 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=ii/K0F48; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ii/K0F48" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-310c5c2c38cso1551297a91.1 for ; Thu, 22 May 2025 18:01:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962077; x=1748566877; 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=hpBhApwrSh8aZyMZyjubfokkuVPDDm+wIxTVx+y5WkM=; b=ii/K0F48V+DnpMyf4bwJKMVHk0QZGDndFtrC3mPrOOikaadQ6pJduPSSPjpb0sBcD1 +i4zYNljViYlrvnqnsMptFvTYayfzSiI6V+NlnRJbp/j1qlt8lE66TccjilGb0BBBTgt kNUNKar8JvKKhClGC3ndcYdry6621cPHU/thIFhowLACGxEZNeHm9BBTonL27mctm1CS IFqJP7Zor3LW5NrHhqzAuPqbfsvGnroXPh3K8VzcPALfss74in7cHbsrvvk7ySyIVWuq 19DVKw6Ua5Ohp4cb5mfNTVhGYR39zTTc6RgHp+VT4Sn7+j38/bG3x183ouHiGbpVUiCA 20ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962077; x=1748566877; 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=hpBhApwrSh8aZyMZyjubfokkuVPDDm+wIxTVx+y5WkM=; b=HgPjkKHPHgymNar9tXgmGUkxHGcZ69pe6cMmiRZx6VQ1+TuPWeAdcBQSd+a4DwNGXA p9ISwesqf4qqfO9Q3BVsc4g4rAX6W6d/dgLDOLoWn5Tp/yuNlJ1mOafC5hWAOKhaJko2 c6PSKkX2XhWRdQ9TZs3VIcN2dTrdP6zmipLB8ng3txtgqqKUf4J7EDGRw1uwMJaYnSJw EK9lpPk5c8wvtdQQR/iZitshc2jRyHLgLtIJ0x841rmetHFificNgf64D3RM0L4rGXcY tTx/7mO7bCfClZLDVbZZRy0cmo0MMw1PZkSXx+oZ2l6iEWlOQkx4x87e3n2s9h/DuCTk mkpg== X-Forwarded-Encrypted: i=1; AJvYcCW95XugRbBfGxve/TMWthQLOX1uxSgdgEA1jfFBNrk+0kBEzL98HRmE7roojJvpgWq14DLeKF3uAsNNFzQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzkoaM+zhVcvLDkGySXKoXuA4WfclIVxBR/l0L99pKA5YApNO0Z 5/scRdaaKJrFqGwNNLsDwhSwAnh7v8qrgOVE7Cm1eTDIaSPJX3ikAb5u4TEEXgx6yIt6DMD3ngt tJp+4hA== X-Google-Smtp-Source: AGHT+IGTYhCOjxbmKRXOrjbAavunsh63uzQMspWrxbHwEJtJhbbYao5aLSwUJ5OJG/DqIAodT2HfVFXoKXw= X-Received: from pjb7.prod.google.com ([2002:a17:90b:2f07:b0:2ff:6132:8710]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4acf:b0:2ee:aa28:79aa with SMTP id 98e67ed59e1d1-30e830c772dmr35674777a91.6.1747962077355; Thu, 22 May 2025 18:01:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:45 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-41-seanjc@google.com> Subject: [PATCH v2 40/59] iommu/amd: KVM: SVM: Set pCPU info in IRTE when setting vCPU affinity From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that setting vCPU affinity is guarded with ir_list_lock, i.e. now that avic_physical_id_entry can be safely accessed, set the pCPU info straight-away when setting vCPU affinity. Putting the IRTE into posted mode, and then immediately updating the IRTE a second time if the target vCPU is running is wasteful and confusing. This also fixes a flaw where a posted IRQ that arrives between putting the IRTE into guest_mode and setting the correct destination could cause the IOMMU to ring the doorbell on the wrong pCPU. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/irq_remapping.h | 1 + arch/x86/kvm/svm/avic.c | 26 ++++++++++++++------------ drivers/iommu/amd/iommu.c | 6 ++++-- include/linux/amd-iommu.h | 4 ++-- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/arch/x86/include/asm/irq_remapping.h b/arch/x86/include/asm/ir= q_remapping.h index 2dbc9cb61c2f..4c75a17632f6 100644 --- a/arch/x86/include/asm/irq_remapping.h +++ b/arch/x86/include/asm/irq_remapping.h @@ -35,6 +35,7 @@ struct amd_iommu_pi_data { u64 vapic_addr; /* Physical address of the vCPU's vAPIC. */ u32 ga_tag; u32 vector; /* Guest vector of the interrupt */ + int cpu; bool is_guest_mode; void *ir_data; }; diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index c79648d96752..16557328aa58 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -726,6 +726,7 @@ void avic_apicv_post_state_restore(struct kvm_vcpu *vcp= u) =20 static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) { + int apic_id =3D kvm_cpu_get_apicid(vcpu->cpu); int ret =3D 0; unsigned long flags; struct vcpu_svm *svm =3D to_svm(vcpu); @@ -745,7 +746,7 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu,= bool activate) =20 list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) { if (activate) - ret =3D amd_iommu_activate_guest_mode(irqfd->irq_bypass_data); + ret =3D amd_iommu_activate_guest_mode(irqfd->irq_bypass_data, apic_id); else ret =3D amd_iommu_deactivate_guest_mode(irqfd->irq_bypass_data); if (ret) @@ -809,6 +810,18 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, */ guard(spinlock_irqsave)(&svm->ir_list_lock); =20 + /* + * Update the target pCPU for IOMMU doorbells if the vCPU is + * running. If the vCPU is NOT running, i.e. is blocking or + * scheduled out, KVM will update the pCPU info when the vCPU + * is awakened and/or scheduled in. See also avic_vcpu_load(). + */ + entry =3D svm->avic_physical_id_entry; + if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) + pi_data.cpu =3D entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; + else + pi_data.cpu =3D -1; + ret =3D irq_set_vcpu_affinity(host_irq, &pi_data); if (ret) return ret; @@ -823,17 +836,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, return -EIO; } =20 - /* - * Update the target pCPU for IOMMU doorbells if the vCPU is - * running. If the vCPU is NOT running, i.e. is blocking or - * scheduled out, KVM will update the pCPU info when the vCPU - * is awakened and/or scheduled in. See also avic_vcpu_load(). - */ - entry =3D svm->avic_physical_id_entry; - if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) - amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MAS= K, - pi_data.ir_data); - irqfd->irq_bypass_data =3D pi_data.ir_data; list_add(&irqfd->vcpu_list, &svm->ir_list); return 0; diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 15718b7b8bd4..718bd9604f71 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3850,7 +3850,7 @@ int amd_iommu_update_ga(int cpu, void *data) } EXPORT_SYMBOL(amd_iommu_update_ga); =20 -int amd_iommu_activate_guest_mode(void *data) +int amd_iommu_activate_guest_mode(void *data, int cpu) { struct amd_ir_data *ir_data =3D (struct amd_ir_data *)data; struct irte_ga *entry =3D (struct irte_ga *) ir_data->entry; @@ -3871,6 +3871,8 @@ int amd_iommu_activate_guest_mode(void *data) entry->hi.fields.vector =3D ir_data->ga_vector; entry->lo.fields_vapic.ga_tag =3D ir_data->ga_tag; =20 + __amd_iommu_update_ga(entry, cpu); + return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, ir_data->irq_2_irte.index, entry); } @@ -3937,7 +3939,7 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *= data, void *info) ir_data->ga_root_ptr =3D (pi_data->vapic_addr >> 12); ir_data->ga_vector =3D pi_data->vector; ir_data->ga_tag =3D pi_data->ga_tag; - ret =3D amd_iommu_activate_guest_mode(ir_data); + ret =3D amd_iommu_activate_guest_mode(ir_data, pi_data->cpu); } else { ret =3D amd_iommu_deactivate_guest_mode(ir_data); } diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index fe0e16ffe0e5..c9f2df0c4596 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -32,7 +32,7 @@ extern int amd_iommu_register_ga_log_notifier(int (*notif= ier)(u32)); =20 extern int amd_iommu_update_ga(int cpu, void *data); =20 -extern int amd_iommu_activate_guest_mode(void *data); +extern int amd_iommu_activate_guest_mode(void *data, int cpu); extern int amd_iommu_deactivate_guest_mode(void *data); =20 #else /* defined(CONFIG_AMD_IOMMU) && defined(CONFIG_IRQ_REMAP) */ @@ -48,7 +48,7 @@ static inline int amd_iommu_update_ga(int cpu, void *data) return 0; } =20 -static inline int amd_iommu_activate_guest_mode(void *data) +static inline int amd_iommu_activate_guest_mode(void *data, int cpu) { return 0; } --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E51C6297B7D for ; Fri, 23 May 2025 01:01:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962083; cv=none; b=jV4HcVfh2L+QeLxJVmsucxmCRXrTXt+MY/BWnfaAdfVV7YQkK7wwLmLeJ5RF6ZFRttE3qOVSO0YfNZtqaQwExBlDN7Ttvw+Z9aUBgrNaShiuYvThVIy9lANkcBbK/Bz3C44xTzqNinAJ8KoRPZePJFhPXPYM/XAIfeMCL+FHqX8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962083; c=relaxed/simple; bh=xeyn+RxaGBxwSWBZQAPqlCwxh3rYfve9eQNJaUpZTwo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=F1/a203trTAshKbj7klsFvBjCAM5R4uJuPPgKxg5nrQnpenQzVZY9gi0StAZq7Q2hGOifdrVgsYQsDXgeaupeAE/mOaExYvVsgGQnwdUcd8bxlw6LMldy2UXpht84hvxUzLUY7M2x9tH6x/ajfneqZVYYCZQVdaLZhVFJh/oEGk= 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=gq07cCri; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gq07cCri" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-310a0668968so2255293a91.0 for ; Thu, 22 May 2025 18:01:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962079; x=1748566879; 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=sv4CvuHYTDrFzn739/UZWBkxNDDiJssoZ41UjvZmRoU=; b=gq07cCribNni9IVQusxjToaP7G6/dAAuS1z6MwNQCJUQbUrDcTBnOdKcvby4P6ZrOo J80yga5tzphUIEhSxmnRT8199EBvw/ieyK/IpTzsG0DL1vXcv6XrOigoMh7GBuP6N9SU Zq5yjRVfQ9vx7XDx/U6oTVu1eyBfHIQFzq5UScrNEcxYH5OP04bXrXepgkKCN0zZY51T Y2iCZRWAYJ6V9NjXLrvku/0Hfr4y/ioPOUP1NeSZwiQBqy06HiujMYijd+J5Pr9C6V2k Sh9v+kPJBqAnNIXe9nmIXuswS3GiwzwzoTpwC+JOzG7vfmXpqhy98nAjYgLJKIyI+lZ+ dI9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962079; x=1748566879; 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=sv4CvuHYTDrFzn739/UZWBkxNDDiJssoZ41UjvZmRoU=; b=RtFqDFtddPnsTBhyyAD5iOkYdE5Q03pOTiF0+2n3hibG6sEWASy/OTJBMGJFuNAvQK NkxOgmmtJvxU/Uz6XxFp4GMclUVx7gYrXLqopH3Vqt+2HdTQqhe8JrgTVBEXFnVX9TnS /M3b28rJCiVDNf5nyhxjDI1f4oKFSfermBQyZ6aXINil2pq044r2t/SqwjcSP0GMAzfb rbDmoo6a4XwfSnlN10WVg0RtOFutxzny2zSVHVxK70/t3ZnrKhSbgDGkTmtfp2TWA+8e E0l/rvJH7VXGiRgGel3mjDB1Yu7AavA8it+dTCbQkdVhtOsSK9pfTlYLasxWwwypdBJd tTXQ== X-Forwarded-Encrypted: i=1; AJvYcCWUGmkekc7KGbKB06hi20tmqyDlJlNmOqVzd3292tjN321uvqlQaD8k+ihJ4Cuu3A8NLhEx3cvvGYJTD6s=@vger.kernel.org X-Gm-Message-State: AOJu0YzkfE9As6BlqtR2eFthHkeOzzKdb4NOyom6LtscVi1jeqBJ58Et gR6KmS6smALo6fXOUCSNkMbcwIn2I1lolkQknQdX4TSfg/N1HFJKWd6r0KVWPcibyh2J3TCRgLH swi12Gw== X-Google-Smtp-Source: AGHT+IFmimbvYwJ03lxstnhA7yCM94NNBPUvAkLOj/+0cXnXxDC/143/hDROJrXgXM94OCkZkRbyoWrpgOE= X-Received: from pjbsu5.prod.google.com ([2002:a17:90b:5345:b0:30a:4874:5389]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:37c4:b0:310:8d73:c553 with SMTP id 98e67ed59e1d1-3108d73c662mr13416575a91.10.1747962079117; Thu, 22 May 2025 18:01:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:46 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-42-seanjc@google.com> Subject: [PATCH v2 41/59] iommu/amd: KVM: SVM: Add IRTE metadata to affined vCPU's list if AVIC is inhibited From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If an IRQ can be posted to a vCPU, but AVIC is currently inhibited on the vCPU, go through the dance of "affining" the IRTE to the vCPU, but leave the actual IRTE in remapped mode. KVM already handles the case where AVIC is inhibited =3D> uninhibited with posted IRQs (see avic_set_pi_irte_mode()= ), but doesn't handle the scenario where a postable IRQ comes along while AVIC is inhibited. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 16 ++++++---------- drivers/iommu/amd/iommu.c | 5 ++++- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 16557328aa58..2e3a8fda0355 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -780,21 +780,17 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqf= d, struct kvm *kvm, */ svm_ir_list_del(irqfd); =20 - /** - * Here, we setup with legacy mode in the following cases: - * 1. When cannot target interrupt to a specific vcpu. - * 2. Unsetting posted interrupt. - * 3. APIC virtualization is disabled for the vcpu. - * 4. IRQ has incompatible delivery mode (SMI, INIT, etc) - */ - if (vcpu && kvm_vcpu_apicv_active(vcpu)) { + if (vcpu) { /* - * Try to enable guest_mode in IRTE. + * Try to enable guest_mode in IRTE, unless AVIC is inhibited, + * in which case configure the IRTE for legacy mode, but track + * the IRTE metadata so that it can be converted to guest mode + * if AVIC is enabled/uninhibited in the future. */ struct amd_iommu_pi_data pi_data =3D { .ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id), - .is_guest_mode =3D true, + .is_guest_mode =3D kvm_vcpu_apicv_active(vcpu), .vapic_addr =3D avic_get_backing_page_address(to_svm(vcpu)), .vector =3D vector, }; diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 718bd9604f71..becef69a306d 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3939,7 +3939,10 @@ static int amd_ir_set_vcpu_affinity(struct irq_data = *data, void *info) ir_data->ga_root_ptr =3D (pi_data->vapic_addr >> 12); ir_data->ga_vector =3D pi_data->vector; ir_data->ga_tag =3D pi_data->ga_tag; - ret =3D amd_iommu_activate_guest_mode(ir_data, pi_data->cpu); + if (pi_data->is_guest_mode) + ret =3D amd_iommu_activate_guest_mode(ir_data, pi_data->cpu); + else + ret =3D amd_iommu_deactivate_guest_mode(ir_data); } else { ret =3D amd_iommu_deactivate_guest_mode(ir_data); } --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 43672296D00 for ; Fri, 23 May 2025 01:01:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962082; cv=none; b=ZXKOir5dm0cnY9NA7TGkGz0wH+rbPNy+6v4OWmfgveh67JKF6BCeLBSmpD61z3Y797weWeTjVuerlANYXRbAl3mzG01zH/yPWVayvLHCDaG70od26I/l2SUXxjl9p4RAV7OnLFvl4qu/kHyh9Vzu1NQmUOxnW8PHMd310/qkeKI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962082; c=relaxed/simple; bh=hkXL8tIOvSpgs8BbYZ/kSqxwAWQJTMH+qltnqmcm2m0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=H/C8Nl9zKSUKvrkfvv+QHCjhgmv8nBRp5DLekvooLunfqenmGL1fmmSFj4a+Y3M855yKPUpACN2Sl1CjGgC2EDo2CvBY0Ih6phu2sfkyYHxosLHmN5fUfJwAh3OJZqNbve+0hnWef4kbdkrItCzSqoVKKmDn5poEa6v/4oGWezU= 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=PPeaDPIV; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PPeaDPIV" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30a29af28d1so6784570a91.0 for ; Thu, 22 May 2025 18:01:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962081; x=1748566881; 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=YWgBZPn6hQ/WyvsMJWVblHWlgsDadk9OyN3hzX+dlyw=; b=PPeaDPIVlGUV/NvxJHrMkfgXuoHJgyNaLy7bOFtD5RVaDwqObS1zFpGoHXDIJT+LYe WVOBIo/NFHwSBVzDfz4pAlfIKnafDxmgDjOEwxU3/+J/XKp/wTuYA0srL0UsLtdSXWON 2Y1LTBpCKUdofauX7ae+b1cujL4Rnzb7JAvWJt3uOHmFnUxBdioLqp6Uw/TFzO8xPat2 RYjoWDSHa7iZOc49mvicX3jXgcUyUkmfZjflVND4T3XRSx+9uKWKqsR1uF8BaMMi5PDU cGFH6GJTqHSWKL+BNH9aLFsKHGqjgJQreP3i3mR8nh3E93mEgkQnCVLP7MzZsuvq3dSS ypoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962081; x=1748566881; 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=YWgBZPn6hQ/WyvsMJWVblHWlgsDadk9OyN3hzX+dlyw=; b=i/oa8nTcggc1886+dHguZOgCYytRoE3+4/kkhGuzXJPp9F6KgeBh7kDM+MWTWlhfeT 1LYPLTvcWaz56+g1KDJTD6VL0z9NEM37iYHRT2LJPCf4K6yjimEzSmsWSUe4ASbt4oKL OcgzB9IxDlUv1zsgzSeGPjHBmIFnN4QwnwbLbwqpryD979pdfN66TVb3g+WriNBeQiDP EH/3+NW1vmUNwIJw2HGb4yEsfJoWCds9cKkzomy7sXt5OIBEda5QT8yWmpalV5LXhFry CuNp0kyKRTDvJktpa3JoSKCL44gUKgfeinI67bg0D0KvWA7rx9aT1UYYNyertMhRrnjb tdLA== X-Forwarded-Encrypted: i=1; AJvYcCXmN69mAmxgXbkn+cz9fk5ylu31stnTFzpq32HL2bnyp9rxlD856K/qD0vl+OBYKxx7QJFnyd5bSXjWTSw=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6Wsp3lRPhL50XtXF/3+K/HEL/Z+R0kX0L2H0dk98s/xwTW37t BF9Pyh5kMFEJJ3pkiO2NrdAopDfGPVSOj8Gi1M7XwXbxr4SijIgBTbb4tGgRLXYTcdv3wfe8fk2 U9TccuA== X-Google-Smtp-Source: AGHT+IENrNvN8kjYKcc8X8f5fjuZCrY8RjRuwJA8T06sgnoEuhkTZxv+Y0xj/vx377YeaaqzuMYjIUVGtfU= X-Received: from pjg13.prod.google.com ([2002:a17:90b:3f4d:b0:30a:133a:213f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2749:b0:2ee:f076:20f1 with SMTP id 98e67ed59e1d1-310e95f3481mr2198254a91.0.1747962080822; Thu, 22 May 2025 18:01:20 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:47 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-43-seanjc@google.com> Subject: [PATCH v2 42/59] KVM: SVM: Don't check for assigned device(s) when updating affinity From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Don't bother checking if a VM has an assigned device when updating AVIC vCPU affinity, querying ir_list is just as cheap and nothing prevents racing with changes in device assignment. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 2e3a8fda0355..dadd982b03c0 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -847,9 +847,6 @@ static inline int avic_update_iommu_vcpu_affinity(struc= t kvm_vcpu *vcpu, int cpu =20 lockdep_assert_held(&svm->ir_list_lock); =20 - if (!kvm_arch_has_assigned_device(vcpu->kvm)) - return 0; - /* * Here, we go through the per-vcpu ir_list to update all existing * interrupt remapping table entry targeting this vcpu. --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1552B2C3768 for ; Fri, 23 May 2025 01:01:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962084; cv=none; b=FbrFlEnkWnGij4T+VdclmRIKdzw/+q8kU6qV8EXlvyQT46j/rGl7cGdBHOwxsrn/Q/oPy6Z/m1kJKEYQ7IlNMxjwGcNmWP1Xbzm51aN9we7b0n7eATeV4FfG91flsjL+J4BE9dIg/K010b6zK1TzJOfkKqhHMyvOO9bHMFry45o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962084; c=relaxed/simple; bh=Z00yQ0PQn26W+YkvG1/+a4jEvueuLPaetcdy4ijqCYQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GrVACt/q8RfyJ/vDQGCB2ShrpkgXFS60VC3vNcQgHfjd9upBMo6mLOWPnNdfmO2whlvotcbtNygG+T9cOuflexX2/r/0mkKz8UfblboCOBWerciko5J1WoJjEOSQmej1t6cSH98xJjZ8Q61QCzQJEo1MTXxOpOdjCet7uwVvHc4= 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=X6KY7wC7; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="X6KY7wC7" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30ebf91d150so7040141a91.0 for ; Thu, 22 May 2025 18:01:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962082; x=1748566882; 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=zrysmAdotRY3tUKAweOx9pfjToN/g2hJE4Ng9AMbRtg=; b=X6KY7wC71sGhF8qc6wRH17VGL2H9By+lUhIcmm0m9l7lQGGNr4I0uNkITR9Qon+W4M YbyoT44AWd3MSRgOV5BFyFVGgbiyXRAMuPPo2H46bCx48LcRABH6mY7L9YRMB/X0mmBM JDjqh/KrS4zAtKFaqNVpaA26EGRrEVncoNjOUu2V+Oxt/rVT/+uJuYMgRjgz4VDcQMJ4 UPbC7yIdmqqWowzEtjLLF9OXuITYt0wXoNXxK5oqB4Ao2HorGNAIpU1BL5JDVh84f7Un jdh6l8xUvDjeysYRJDKupzTMc/oKp5rhkJKCoNJ8Tu4tapmkyTBJutq/ioE7Pk1BTZLw vHhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962082; x=1748566882; 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=zrysmAdotRY3tUKAweOx9pfjToN/g2hJE4Ng9AMbRtg=; b=weV4gTPHYnkG5GA5y+9m9JkcUk27usCsnc3cskzbaE/e2U6+aCIFu+SLoFzgLGylwI m5n3gEtVpXiM9AN4Px+9jDhIPAIOqMlPmyf+fNKO0nyYtRryqmq4ptdXCREFx1cifLhp duI9xaAizSf6cKgXFKy4z3L+qQkLKRnFZLJf9mp1FIqUw6kEdEpEbpWZNZUUofCfcJi7 quGnpHkdfucEE+mA7luQ5kbxgShDH0eO+EjPY2kYu3duohj6rKCk+9tGaF+/8aPE3rfy vnKTy9c6/ka9KU8UGexvF1OFkb5/iSB6yiMv1phhqqhhGWls+KZ8rD5nfGw5CoI07LvC cc6w== X-Forwarded-Encrypted: i=1; AJvYcCU2Bd+zDuz72FniYB31w313q6A/FD0rQ4Epv9TF0yq4qR96rP3cz71RQUs8qRcNacYJMH0DMYR2jiF+Icw=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0/hHB4s79yCsow8NuqfEQcXawHuRxAjEd5noZTwuNxQGm+s4X iUj+Jc+s/wxVSNz+117R5D1M/TTHCT4+ztnhC3DWDQW4zBVL4opa+QRrm1QaJITHTG0aaLrgrS0 BhOWcXw== X-Google-Smtp-Source: AGHT+IFYYw2vNKO7X1E4jTH8MJBys/Xi26XTWV4xJAxpgqO4gyT9OdqFXK5/A1nwW8HDjPEMRjulaYFMODU= X-Received: from pjbsn4.prod.google.com ([2002:a17:90b:2e84:b0:2fc:aac:e580]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:394d:b0:2ee:d024:e4fc with SMTP id 98e67ed59e1d1-30e7d5bb433mr48333499a91.33.1747962082505; Thu, 22 May 2025 18:01:22 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:48 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-44-seanjc@google.com> Subject: [PATCH v2 43/59] KVM: SVM: Don't check for assigned device(s) when activating AVIC From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Don't short-circuit IRTE updating when (de)activating AVIC based on the VM having assigned devices, as nothing prevents AVIC (de)activation from racing with device (de)assignment. And from a performance perspective, bailing early when there is no assigned device doesn't add much, as ir_list_lock will never be contended if there's no assigned device. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index dadd982b03c0..ab7fb8950cc0 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -732,9 +732,6 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu,= bool activate) struct vcpu_svm *svm =3D to_svm(vcpu); struct kvm_kernel_irqfd *irqfd; =20 - if (!kvm_arch_has_assigned_device(vcpu->kvm)) - return 0; - /* * Here, we go through the per-vcpu ir_list to update all existing * interrupt remapping table entry targeting this vcpu. --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 07CAE2D29C6 for ; Fri, 23 May 2025 01:01:26 +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=1747962088; cv=none; b=HK7bC83LLJZwco/LkBY73zw1J7BTUZqPwkKRm7Qd+lpmauiltfocXZqXZjJ8hYzHVRV8Mei53UdaPdTgs4ECDY9WmAvr/ihGHiQFGN2nWCSiXE0oQxCEA0YND9G7XxNZzRonkpGX+mi1HV9eI8kvyIszeVKnR0s7OPclejddGCw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962088; c=relaxed/simple; bh=55qjTgWoCAqQ0qlezLcudUP6UhJvdAn22WKHfJ6neN0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Xvur8C7c+zF7H4JzexPUFruc2tCVCUGYE6dM4yqB5Zx1D4Zvh4sgD4iffJFZKs8CoVF5OMhHiGT4r4kpfcMvrl1x0WLNrvIM9nEFCkVb7gpUXfLxI4K8UHgGI/H2WO09WZTV3r1PJw9WSY9zObltifLrvd+Dl+sOSk0ocv9tz5w= 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=HdY8PFjM; 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="HdY8PFjM" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b269789425bso8821815a12.0 for ; Thu, 22 May 2025 18:01:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962086; x=1748566886; 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=gUhbzbIFz6N1ly0iEWpzNSdv8WcFPzr+G6Brn0aKnfs=; b=HdY8PFjMZt6H2RfcNGXUgIZXb8lxivtddwihRNXrp2IAlftGaRznb2/T1bPZIHvihW OX511FjByY13jc3gtUc4EMW1yqsFyIfr5WEiIn9mbAHPeg4y6Zyi516Sr0OJ9o6JFM7H 9Q6HoPc/Aj/lSepccBjzYwthSzS6OphwUcJcYq9ORb27O+JnKxsWv9ke6m9ypq7y+MYv H1vwp4503XKM/o6S9KFOrZTLxxxgODphDdVgg1U6PX0iHRVUBLY2vcrfwZ5ndbKzNVi0 Ebsf0dyUzlCx/TwvNmFuzeeyNEIkuUg9p23TP3RjFawrvUdgp2gB/uyqfKfKSdOeRZSv dnAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962086; x=1748566886; 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=gUhbzbIFz6N1ly0iEWpzNSdv8WcFPzr+G6Brn0aKnfs=; b=nHmn6jBNKKVk7hCaUipfPj4AEKBIKLRa1IO32Ed+enShasLb0ZYkD8mtyVn9I/WdPP e7yOC4DacBVVzvLUos/IHW/TOLhEGUsxy+MOgoiYmyTRNURQ7L/vwIVW5FvwsYVpkByH MBygEdVKRlWGMWc43QxB+r0479bnTNPNzUSlR7ezLYo4K2ttqD2ohj/griNpOOxD3TOQ SgV0THzde2RDM5vx+xG24CG/HoD8FQNUrcRxPeJanzy8bzl2j9Q4BpVHO9hm3KxcAiTM zwvgW58vfW8PtCyXnASxO1+YVObqB+mygxygG9QgjHPysud9DxxwLBWEw23GjipAr7P1 vYmg== X-Forwarded-Encrypted: i=1; AJvYcCUaHeTcRIMb45zqtG0TH/WqBrTkK41QwX9Xn1snTzif1PhC0gwQnd6fJ27TBoAq8hXv9FhvvoOyIunUDUQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwUFCAxfCBbUjDLmU0aznaKrN6/qA82mGrStBVVqrofCdgX0Tde am+Aa8FyDFwwTRS3ASwUDykBEh94iPG5T7/OCFmHFF8sNtC1ztMdgWxDcaEbzerm6pm0qKID5qq VR5gDcQ== X-Google-Smtp-Source: AGHT+IFmNcRCVGsHwu2ASi0oy2rdhg/Fb0rUQuE9H39VclrV/Td0/+Bcwnto5U4Ohf9/HGIiubwzMKlglKw= X-Received: from pjbnb5.prod.google.com ([2002:a17:90b:35c5:b0:30a:8ffa:9154]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:17d0:b0:2ff:53ad:a0ec with SMTP id 98e67ed59e1d1-30e7d57f38bmr34769259a91.21.1747962084260; Thu, 22 May 2025 18:01:24 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:49 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-45-seanjc@google.com> Subject: [PATCH v2 44/59] KVM: SVM: WARN if (de)activating guest mode in IOMMU fails From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" WARN if (de)activating "guest mode" for an IRTE entry fails as modifying an IRTE should only fail if KVM is buggy, e.g. has stale metadata. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index ab7fb8950cc0..6048cd90e731 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -724,10 +724,9 @@ void avic_apicv_post_state_restore(struct kvm_vcpu *vc= pu) avic_handle_ldr_update(vcpu); } =20 -static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) +static void avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) { int apic_id =3D kvm_cpu_get_apicid(vcpu->cpu); - int ret =3D 0; unsigned long flags; struct vcpu_svm *svm =3D to_svm(vcpu); struct kvm_kernel_irqfd *irqfd; @@ -742,16 +741,15 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcp= u, bool activate) goto out; =20 list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) { + void *data =3D irqfd->irq_bypass_data; + if (activate) - ret =3D amd_iommu_activate_guest_mode(irqfd->irq_bypass_data, apic_id); + WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apic_id)); else - ret =3D amd_iommu_deactivate_guest_mode(irqfd->irq_bypass_data); - if (ret) - break; + WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data)); } out: spin_unlock_irqrestore(&svm->ir_list_lock, flags); - return ret; } =20 static void svm_ir_list_del(struct kvm_kernel_irqfd *irqfd) --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9B6D2D29C4 for ; Fri, 23 May 2025 01:01:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962088; cv=none; b=titVtmseEVV4A8Kdatn+RINhT5i3egzUDinD+4hz0N2kUGvVWJCojx3xuM5aufK3TeiD/Y4TBEStX9PpTF+/j/L9E6DEoO4agaM5KFZjI72cMFopC5P/CqwQPlA7swgcMTeCXycKF0wvEIy73//HqKTDwj28PNbMcRRV68GZsGM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962088; c=relaxed/simple; bh=8SrQ+oKGvvTIJysid8Rxw5XPwDm17oKx2xAyAvAXgIw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rw+cjNe/9AlS2behiP7U6aG/0m9Mf21ZZnZqlVWdREFtGPwssWD7sA4q4uZYuLo/tbemajP4s9D4a0XwjiCvNSBjm7uyu5ypd4dLiY8NcX/wBpZJVQOhuScdeDu7KC05yD1SG+ko/lrVfILFVYPTSzOMxsXaz0tl1KwewBy5Z5E= 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=LHr3OwOl; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LHr3OwOl" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-232340ba283so4275275ad.0 for ; Thu, 22 May 2025 18:01:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962086; x=1748566886; 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=Nv/wXOYxNnKgHAHyhi8Vk/t+dWw1N4nnlsKYjly3sjA=; b=LHr3OwOllnjVG9aOqedKSZrqHyrEllrbqTNIRtZ8TdsqdpNYzu5FkjqDKVEipTHyPs ULmN1dX6qxY4vuP+ubQVPI/3ZhC32Ic3Pji0ddzCDH2mN5NVF3xM9dxBUcve+Zu+DU+F NhcIEdpbJhh4aLTyZuOyNwNW8I3YfMlr1aiv84SUf80f4I9StcnV5xAcC73QQIzcgVXi lbmO6ixSNBf+LFOK+YgR/cdDA8jK+QQEAvVdoAjX4onJXYmEK2K757mgCr+jVuP2OXuP d6alL4ZS+CqO5YfBMUpLkHOc2J60TW4nYVO9EInzlye+frHB5S8FLNO8klJofBKJ2uLw 696A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962086; x=1748566886; 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=Nv/wXOYxNnKgHAHyhi8Vk/t+dWw1N4nnlsKYjly3sjA=; b=WpgW4E4odPe5vcRawIUGHaHb/ZBpCMS08o9oFU3niIYYJ9gWkzWmV5bpQXa7+Zg1GE M6tGmYZLrWnUqkSQVihfGTBurGmzDMNGW4F7sn0l75trT2vyPR+gPGQLBitIs7K+qLIJ JS4xM+Rh6VcKnKy2pLPd579naNsGNffJQ758OgiW/kqRJkAte9peG12BYvG0uDe60duH t5K3uN9ytVpg0GsVZZpriwEwLd1IL5tqjwHxle6ZcCgkse5eFseK8lyxAjjOEzkfNtVB ACA599k5dqPNyNW1BJJ4UbIUFS48XSDatFYUP4IEpfE/aF1xSssOjl1bb/Y7D5WGBuio SX2A== X-Forwarded-Encrypted: i=1; AJvYcCXdp6ZVrEWDl2r0ykhrkVXpBkALsiJ1vNxUW5Sjx5lxqF9HQbHrkw76DhFw+uuYfK/dTWAfxMux4PxQ5aU=@vger.kernel.org X-Gm-Message-State: AOJu0Yz/u4r2t4ubMiuionsPgB+7/nmbNPyF6txqN22dFjpWbEZf/97d cQhr2bxMxsBxC6I5FEPt9ySGhTcWANg1Ka9qPaVTjwQjG16+FL1XQlFx4FinlaEFlFTXumyYxl1 lgzGdJQ== X-Google-Smtp-Source: AGHT+IF3/E0iVH/vlMLWt9XVgizbCsZM8J/q2ciZDlo+jMfmZ18HWLQgxdx78gP0A2Hywms02HUD6gsHXw4= X-Received: from pjbnd7.prod.google.com ([2002:a17:90b:4cc7:b0:2fa:1fac:2695]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:46cb:b0:231:fc4b:c04 with SMTP id d9443c01a7336-233f06ba057mr20489615ad.17.1747962086040; Thu, 22 May 2025 18:01:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:50 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-46-seanjc@google.com> Subject: [PATCH v2 45/59] KVM: SVM: Process all IRTEs on affinity change even if one update fails From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When updating IRTE GA fields, keep processing all other IRTEs if an update fails, as not updating later entries risks making a bad situation worse. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 6048cd90e731..24e07f075646 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -849,12 +849,10 @@ static inline int avic_update_iommu_vcpu_affinity(str= uct kvm_vcpu *vcpu, int cpu if (list_empty(&svm->ir_list)) return 0; =20 - list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) { + list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) ret =3D amd_iommu_update_ga(cpu, irqfd->irq_bypass_data); - if (ret) - return ret; - } - return 0; + + return ret; } =20 void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 9717D2D29D7 for ; Fri, 23 May 2025 01:01:28 +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=1747962090; cv=none; b=QB8zHhk8b95w7h/Z61atutnBnImQEAZG/P0TN/RvHZ4pr1K7/NxCf5TbhHuUfes1C5hTEy41iin23NZUX5A0uYje7U3ywBmmVZ6V55SCGWSnnijBKKHgVEcwJpQKP/W0HsY2VZ8o9kS3Kc/mMnq0VHMH7xXZXs/63AINPdiQ3mM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962090; c=relaxed/simple; bh=axZocm4dvPcmbiB/ZwqdO3wy7w4x+of0K0yBbiaqjq8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fbF9zj1zPmTOjnA9Xy70XbzTMciERT99e0Y1BmuvGOe4R67/RF6VUhkgFrz2LBQ+UZT8dQ3AMn1dA+cwhNHBMW7yTGmIorzIOo+9PnHXxTPl5uwlft3WD8UAS96B5WAbHom/sDRin/YP718cMjmIY7oXWpDLbWych0ZKWG7I8oc= 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=mfGD89c5; 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="mfGD89c5" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-742b8c0eaf0so3422810b3a.2 for ; Thu, 22 May 2025 18:01:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962088; x=1748566888; 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=i45otOSm+TkhMTHaG59UwoG0Q0Wz+ReqP6qkOsiZs3I=; b=mfGD89c5GaGeqR1CxkeZPlwOStK94+B6TBO6SfWFs1J6kXCCmUo45YSwqz8xluBne8 A6qA5V5rdwoulengwCAHkWnGj6R8nQMoEypRzhqmaLZQRVK3WduTPBXdb6w/gF+u8FVo t/BLG8e84TrWuGUoljIJ60BR6wWWy6blkV/7/EiYY32weq7bQlegtcBBCQS/SpTmx2Nz GhiSotX1q0Y1BotZzR+lxxO0tjqBSA9mgzK20vvU3Z1xgKFmABun7qNzDsiRyms9hG0A CoDlGf6vrLislIVU0TsoCtk/TxwglpR195f4scbP7QSFr8IaVjm4ASv4xklNETpgv+P0 9hDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962088; x=1748566888; 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=i45otOSm+TkhMTHaG59UwoG0Q0Wz+ReqP6qkOsiZs3I=; b=SMS65mA8pVL/eYqel02xq9oElojEpwQ9qd/111nAzprzqtZF493jXI+PSl5krsUBTV mAogoSOoqKu/ibHl97/iqPWUcWxnCjzeRE1fsUEVDwRBfc7Y58FZI+uOwwvROA3gD6x7 Tya9ToB75YpCYf5TEyhjgHbSvfIMjB4ESx1vjoOZ395UD5mKlDjWvUV2qqlH6/lechVN rFHyCvT47Ar+3YAz4238eXGzbCoSijSOJ/EupVnz+6jA4K/EBuG6LoZkYU29sl3BMVje lcAKRaQrA7/cH1XVMEx40sYHqRtZjIUBQ3qZ7VlluSQYGmTulSE0EkNEaBucK/H5jH7g o6eQ== X-Forwarded-Encrypted: i=1; AJvYcCUEhUtJ1EB+hZWJ03yt6Z2HcI3hW1Z8Xt5fi10mwRHwNuW/hhlu2hE/QTykwLEI12r9bIeBBZqT0Iq/QJU=@vger.kernel.org X-Gm-Message-State: AOJu0YyXb9CKJ/xjF4bDjrPU4inZYCt2cq76LOqd7g8/gASwcR480c0K 42tWMTd4KZ5fAwXbqvNanTFaL818wT6/ldKwOTBFe63puoallOa2fhznezuRtCJw55HsWN5fAvE 8Wmxo6A== X-Google-Smtp-Source: AGHT+IGKXUDv5+WdC/0P78GDw78LzUL5LFq8JfcqEQP/cHtEYBO78G4z6PZ0oz9z2Xbic1SU/rHQzxKGAu0= X-Received: from pfbgx21.prod.google.com ([2002:a05:6a00:1e15:b0:741:cbad:dafb]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:928c:b0:742:a77b:8c3 with SMTP id d2e1a72fcca58-742a978715fmr38003734b3a.4.1747962087855; Thu, 22 May 2025 18:01:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:51 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-47-seanjc@google.com> Subject: [PATCH v2 46/59] KVM: SVM: WARN if updating IRTE GA fields in IOMMU fails From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" WARN if updating GA information for an IRTE entry fails as modifying an IRTE should only fail if KVM is buggy, e.g. has stale metadata, and because returning an error that is always ignored is pointless. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 24e07f075646..d1f7b35c1b02 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -834,9 +834,8 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, return irq_set_vcpu_affinity(host_irq, NULL); } =20 -static inline int avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, i= nt cpu) +static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu) { - int ret =3D 0; struct vcpu_svm *svm =3D to_svm(vcpu); struct kvm_kernel_irqfd *irqfd; =20 @@ -847,12 +846,10 @@ static inline int avic_update_iommu_vcpu_affinity(str= uct kvm_vcpu *vcpu, int cpu * interrupt remapping table entry targeting this vcpu. */ if (list_empty(&svm->ir_list)) - return 0; + return; =20 list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) - ret =3D amd_iommu_update_ga(cpu, irqfd->irq_bypass_data); - - return ret; + WARN_ON_ONCE(amd_iommu_update_ga(cpu, irqfd->irq_bypass_data)); } =20 void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 484732D3229 for ; Fri, 23 May 2025 01:01:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962091; cv=none; b=aCrZmoUWrCUMMw48C8OhYFx2JYRVavWxRC0IaJlF5NqYoB/CaGaDeHF3he+qWsUpc4XVy5NDkBmwL1vsuKGJ3N3R/gQAbFIdvvgugnKYJGAOl7kY9qOYOC4WA3DZwxc4oZnhSjLEtGOrX29DHYEvMfVMAx4rCVbAKby7SG+4nHA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962091; c=relaxed/simple; bh=AMRZmDoiaK20f+IpNDlcqYN2AhFBOCz2nspxKJfmrw4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NOT54EYurCh/9weZ1usxwuKYWQoVgwY/y1EZ69qwjNvUtp2sTW/I3aCcnXaeqpBs23UTFuxoqxwWnrqpeMavsu62kUvFoHmwkLejYdF+AZXb43mhT8V2GveE5SmsyofDemHr8LdVtY4kyTAbt6HPEEczn/4Cg2EE8v5++tLAeHA= 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=no6xGc//; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="no6xGc//" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3108d5156e8so3442715a91.1 for ; Thu, 22 May 2025 18:01:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962089; x=1748566889; 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=j1lrYko58mfUpTnz/5S46tcMBGz9QzsmcN9d4X8ezso=; b=no6xGc//ijKN2W90VkNAp79cSjVZy4QvopPI/VkayNo3fe9y3/AGJ4nIQuUa+jh0un ao2D/2YprlPiPSTmivjYGAMbAvMuPvjs5pW+NLodFvT2hUu3bAjAPj2szVVTUnHj+fj7 q+kZsYajk7f48WtRvm5wfyk9DYt3MEIJLe7am8PTCliGaWGKQzW1WEnHtzvKHunz1glJ u4Fc2TFP1OTMgSSsAp2wKfyfL/tJHZwZr3r2i66CE5GyRl8TGalNG2n04qV7RqY/781e EEbOeTgNmcS8YZIZxFnhtFM8YGrKieiIY8JENyuAoqtGvH49xSpExB2+2fu8RSSfTZve sUnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962089; x=1748566889; 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=j1lrYko58mfUpTnz/5S46tcMBGz9QzsmcN9d4X8ezso=; b=nYSS6lUH7i+HwSRpaL+TCGGH0rY/6z+Llz2OHOOjd+xYJakqeMBYvdAQK2t106hsj+ 0gH8qOs4jFlThZBuDtSoxR6Vx9frfFKCqaTlvQTd7PBV5fT3A3rmg4tNGeWs2syal8Ll RueIEkWI4dniE9CNV17ITZugrsMNLp+/cKCZcNol6lyMu5woUp8DrRAqtQaI4Cwj3Q1l oXDVtaZ32rpOSKrLOYkuwtz4VIFvku4t6sHdtLOF5FXo5ZvMebzRMsWki5EjaqC8mdE6 PwZ5+FLGUYnZn0EBh9SW90m4ZNRKlqlBSmzrmpYue1l1ngXO7iPqcq1j98pvP6mqGiN9 ffOA== X-Forwarded-Encrypted: i=1; AJvYcCXFVN86vylccKU+8QFLJuqbIcoDDXxMz8Zhhbf+6dJ29nmYocwS7ItmLOmAftSnYv+X1o/bbHZCoCe1Ldc=@vger.kernel.org X-Gm-Message-State: AOJu0YxrSv8ia3HlazfMp8NFCHF1W8287Y9WR1E/h/+KLpstlVeU0Pgb DJRJ120nRsgITA2DAJwthPcb7YLG+aUbPvl6D6ikRw242OkAsTdFnw5r2TeAfse4gAsvlQjhtwV HjSYIIA== X-Google-Smtp-Source: AGHT+IEiUGctmed5Jm4/azl2/SL+jFNyFyoqxOj4Sx+0n2i6EPF2TpT8R/3hGNPh8Oh64n+PfETUY1WDfs0= X-Received: from pjbsu5.prod.google.com ([2002:a17:90b:5345:b0:30a:4874:5389]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3b8d:b0:2ff:5c4e:5acd with SMTP id 98e67ed59e1d1-310e9740a23mr1736244a91.35.1747962089593; Thu, 22 May 2025 18:01:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:52 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-48-seanjc@google.com> Subject: [PATCH v2 47/59] KVM: x86: Drop superfluous "has assigned device" check in kvm_pi_update_irte() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Don't bother checking if the VM has an assigned device when updating IRTE entries. kvm_arch_irq_bypass_add_producer() explicitly increments the assigned device count, kvm_arch_irq_bypass_del_producer() explicitly decrements the count before invoking kvm_pi_update_irte(), and kvm_irq_routing_update() only updates IRTE entries if there's an active IRQ bypass producer. Signed-off-by: Sean Christopherson --- arch/x86/kvm/irq.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index 19fa3452d201..904aac4d5e08 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -517,9 +517,7 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *= irqfd, struct kvm_lapic_irq irq; int r; =20 - if (!irqchip_in_kernel(kvm) || - !kvm_arch_has_irq_bypass() || - !kvm_arch_has_assigned_device(kvm)) + if (!irqchip_in_kernel(kvm) || !kvm_arch_has_irq_bypass()) return 0; =20 if (entry && entry->type =3D=3D KVM_IRQ_ROUTING_MSI) { --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5EAB2D3A7C for ; Fri, 23 May 2025 01:01:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962093; cv=none; b=gAmwtGX60Ea0GaBrV3c+qBkFdULS+CC6AOTjYHwooVlE7eaSOItJAiTAVVDgxVPGy6AuRgg0c7ReQIzzFJISG1zGz043PhLSe/XXwWYEY2tUC/hcmj2OWC4Qe1ozj8+JohjPLtlvAiQL/qZHur7RcJwcSJrR/wPovBfv+YjzrYE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962093; c=relaxed/simple; bh=6xj74XotH+Q4CRZUCiYbLA659UygnyGaE+O2rFb5mnw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VJzKrbIgfU83UE3sLQ06/AO5XvmElSwOBnqGDIXETH/DLuthPrhzvqrblVzLmhB0a7AqdH1Cjigb6Mu6hljphc50PnbPEfREu9dSq0D7X2FRnpi6gCVJJGtQtwwdOh3oVsx0slOkOzzrNoLkHnCXV4kA8XCkaHMGzRjtfNtYkPg= 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=ikIWNxSC; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ikIWNxSC" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30e895056f0so9232013a91.3 for ; Thu, 22 May 2025 18:01:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962091; x=1748566891; 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=SI8/xkDYLLYF6taZwnw26LAv3lyi2NrQBm7/9JBUNzc=; b=ikIWNxSCyR7jBiGL7zet5PLQEbHFWWeMwdZyifOl7uaY1L7f6f5ug+wSxYcwDZlvfp sjyNi0131OGVyEg0sIx4lD6IRVrVqPtSNgpRV4rYgA8AFZdkaUehV5+FCFlUqQ8lDtg6 pz0aV8Ij8+4COfxFRwtzLN3o621Pkz6IafzSyl4UsZdNWrtnolH1ktRRcgPCambWF0Af y+Y9TeZQ1VM6ls9jGh41v/bAfHdpfrXWko71ZOz9mcC0JihFPYdRC9EeRmbXqDusGT5a 0XdYL7WkHNzvyht6Q7EISyampl8lpecnkFcJE4d6BxJsC2XdhCfjlRdfNj31CkF+vd68 G9Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962091; x=1748566891; 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=SI8/xkDYLLYF6taZwnw26LAv3lyi2NrQBm7/9JBUNzc=; b=o/n8MIsVuv2jcVvSR2ABCBxVlsrexLpx48Z5ETHE6b4/VCPN7dkIn2Fg+dtUZENOpb JVsOLtoAYy4vTSI/39jnT5BwnwNuWW1MJEQOPa7PSoZJIP0b50vkMr9P39JZWtf+7em9 /6u/opBme8eZFFLpbWhdrEzAG9kQmWL6doJ69ivk76oAk9x+dZeu9dhVotK6yBDT6xHK 6Xt9BZ01wLp5FOCaNvfqddHhuSrb5ufEle3fZgkD6WMmel4UNoiFe46s25Xqxdu6qEBN oGpx2roGhk4fOqUnltrx7s2oJ0vfSxoG8MZ3QwgGVB1bvFg/fMC7c/XiBrYQOiGZ6GBu 3Fqw== X-Forwarded-Encrypted: i=1; AJvYcCVRL8nZC/4Or7jKypDYrBfInsXq5BO43MiLz3ChRWrNNAf+6ZzYeHU88rScZHBE2pPo+9Ww0ERDKKMVFSg=@vger.kernel.org X-Gm-Message-State: AOJu0Yx0ooxJLczqw//LQ8BcjCanf/Q1j7Aukqt/cyvUijc9/CqXUgRm S2siG0cNhnSIoUW3rf0naU+JilRAr6C210tAXn6rbyOCVibkpEgDQr33P2MI1tBTLBENHyTsOQi Ax0ERvA== X-Google-Smtp-Source: AGHT+IEfo9l+n8Wtly1vR+PcVTI5WpP1pj3psfcNyO0/y6H1bp5kCb0/Tv8svuMeGn96uoIkUHhB1SnsRmc= X-Received: from pjbee16.prod.google.com ([2002:a17:90a:fc50:b0:2fa:15aa:4d1e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3147:b0:309:cf0b:cb37 with SMTP id 98e67ed59e1d1-310e96b615bmr1979932a91.7.1747962091171; Thu, 22 May 2025 18:01:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:53 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-49-seanjc@google.com> Subject: [PATCH v2 48/59] KVM: x86: WARN if IRQ bypass isn't supported in kvm_pi_update_irte() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" WARN if kvm_pi_update_irte() is reached without IRQ bypass support, as the code is only reachable if the VM already has an IRQ bypass producer (see kvm_irq_routing_update()), or from kvm_arch_irq_bypass_{add,del}_producer(), which, stating the obvious, are called if and only if KVM enables its IRQ bypass hooks. Cc: David Matlack Signed-off-by: Sean Christopherson --- arch/x86/kvm/irq.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index 904aac4d5e08..af766130b650 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -517,7 +517,7 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *= irqfd, struct kvm_lapic_irq irq; int r; =20 - if (!irqchip_in_kernel(kvm) || !kvm_arch_has_irq_bypass()) + if (!irqchip_in_kernel(kvm) || WARN_ON_ONCE(!kvm_arch_has_irq_bypass())) return 0; =20 if (entry && entry->type =3D=3D KVM_IRQ_ROUTING_MSI) { --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 5527A217668 for ; Fri, 23 May 2025 01:01:33 +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=1747962095; cv=none; b=Jb7Si0N5LqmRY1DalVTEdSYJNnemc7DFFD/GLI9RNVPyIRPTNdZSF3QomdYS1drbF8Xb3uR8fEottus8njKCM2hq6Bz9sTWU4BhcsdO78AbtJSZSAeNyHZwK5LUqzyHWSwbkjSFSOWna6IHr9NAS0cWQXxHYoIet9CUA7S178eo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962095; c=relaxed/simple; bh=65fTcpLjkBtBO6xCL7Lqhp4uS5GSGwxSjcQbFJFJsvA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=k+9cDIYKhtnSaO1CDkqjVZNt2cEm5Lc8V20QcqVYXXoEPuAo1VIQoKDFYn+j68u4zumsR5fvIWxpMMiugQbtfKZAoF/CdRKMHroijs6xYSnyzEV9UJEBG84EMpMRuwE71HD0jrrzdPVi2tX2PQtn0LB5S0U6qtkWgE8jML9PD+Q= 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=mEgUxggE; 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="mEgUxggE" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b2c00e965d0so1192297a12.2 for ; Thu, 22 May 2025 18:01:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962093; x=1748566893; 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=jRZ2pwWogb5EDz58ANC8fmbpd84fTXnIK4G2GwCRUWE=; b=mEgUxggE4ANPOZJ/Yw7o/codY7ZTjLK/+INcsSrpm4Q3VH3O2wAbzsNCQD7t5Z6La4 yg9N6trVJRGGAskZksa4X7QmO7H3OnGi4DeMcfIIkqtSqS1aBkyOMxYMdfRE7/fhzqx2 K0z3m31BYTjxAD8NvxpC2asSzI53jHAwYp9k1GzKl3TStiBs2jZALIXHp9Y44XHUAODL QtAi7f2izUoIHXUIelyP8AavYfN+4NNBKc00r/IINmrbnNURmx6NKjpQFBIXfoBiYg1S oBRenKJBnQ206Z4wOxJnOB59pKPCoOiuU1Esq2zsd92B83sVyrZ/Dg9NWoqhu9CJ+p1N 70SA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962093; x=1748566893; 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=jRZ2pwWogb5EDz58ANC8fmbpd84fTXnIK4G2GwCRUWE=; b=ezxz1JQv1AB9itz/S0+ey9E0RXjfO6L7p8NU2j+87MJWXdNKidQ5eYc0n0psYwso0U aA2rjbuFPTNb5XhQVSK7fJCQpB5WiuP4cZ3sl8VUJjZP6eJfIiJyTpkpy4b2mNxoyvpL Cx1cDFw+Fr081BSMWWsHv1HT4H1g/0dzMQG7nDihcA1iTlRdO2olmH2tWOw/SdqvZ7mv LHa8UH7H1SrSXuzprzoKkNzYY1iREbDPRXwJfEZGMFEjkJQENcIliOr/D7cX2quVaQ5X zDXDhdpA3A7UyZ1JpODxPQQsEBIY8bUFhf1D8bJY0Mo1JltSmu3hFu0viuL9aHKVxCHg sVTg== X-Forwarded-Encrypted: i=1; AJvYcCWzDD/30ix+Z5zt36IB2Epqisw4VElzY7FZdbFJltuWXLEFEPXbAxf6VX/p6ej/iGwHFya25Gcxua5/EFo=@vger.kernel.org X-Gm-Message-State: AOJu0YzmMxslzBQxseQj6NyCJUVqkNyICR2l1hscGL21+ts/8yhKL4dT CiRDb2KWLWTNetLHuT/4OUb5HzczBuk0mIHtygSeOS/Pum+MHz8+OtJ/IM1Un8NQ4F3j4VkHq7d SejGl4Q== X-Google-Smtp-Source: AGHT+IFyeeumFKhc4Yu6rkIsxyO1z805cVfyQ8R0sxgmiUuh4VdwSV11ZDutI0EPEQI70l4STndzesKeIW4= X-Received: from pjbsi3.prod.google.com ([2002:a17:90b:5283:b0:2ef:7af4:5e8e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3b48:b0:2ee:d433:7c50 with SMTP id 98e67ed59e1d1-30e7d5ac7ecmr33847409a91.23.1747962092871; Thu, 22 May 2025 18:01:32 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:54 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-50-seanjc@google.com> Subject: [PATCH v2 49/59] KVM: x86: WARN if IRQ bypass routing is updated without in-kernel local APIC From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Yell if kvm_pi_update_irte() is reached without an in-kernel local APIC, as kvm_arch_irqfd_allowed() should prevent attaching an irqfd and thus any and all postable IRQs to an APIC-less VM. Signed-off-by: Sean Christopherson --- arch/x86/kvm/irq.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index af766130b650..b6263ab4b18d 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -517,8 +517,8 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *= irqfd, struct kvm_lapic_irq irq; int r; =20 - if (!irqchip_in_kernel(kvm) || WARN_ON_ONCE(!kvm_arch_has_irq_bypass())) - return 0; + if (WARN_ON_ONCE(!irqchip_in_kernel(kvm) || !kvm_arch_has_irq_bypass())) + return -EINVAL; =20 if (entry && entry->type =3D=3D KVM_IRQ_ROUTING_MSI) { kvm_set_msi_irq(kvm, entry, &irq); --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 026B72D4B7F for ; Fri, 23 May 2025 01:01:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962096; cv=none; b=rs0egFB0OoUvDesRsathii5/SJ2FcdjL/12KFnRxOm+pQiSMlGQewVZlOR5K1zOUjS4NABIzyo+UeGtpGEjGRvpqUuwMr8kw3CYzved20IsK0qmfvbTMfCjsZJLnPr9GXYMNlDI0HwN2F485fZXA/15TgvGiT7qSHPhUE++obG0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962096; c=relaxed/simple; bh=WVXURqeG3/UNWGnPr4lCT0UQc8N7VixIfadV+aR9WaQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Nu9j0NfX5pdW4UuEwDMsvKs/c9ZUj0qv0rnhlNUWpv5jXpUC6Ic6bMPyDX47u8R9gT+E0ObXm9zofm9EbdMRpaKu4xdI8MhLBZdhzt7s5nQMJ1zvH/7yM2bw5MlZk35ssdnL+5PugqD6TK2ZhYr7kq8SRllIspMnML/OnI8175k= 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=ZnwnUHdS; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZnwnUHdS" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30e895056f0so9232145a91.3 for ; Thu, 22 May 2025 18:01:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962094; x=1748566894; 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=cN9xzWmJm1SC3MYFl35NGHuTlOxpMSXq5/uxL1rDnZU=; b=ZnwnUHdSYOTeFZUfySr8NnzGRb4mgH6SojtAPcPQSmFU0k16bwxdGGUEaCxghBN9+N f2C/AY3Ldw/O4SrPcPLltKhL/vW5t7r0jb5WjkRhIHp8WenEqSmFmPvLied7ztUm3VdA WAj5lhXP1HC386y0k3ld+kEsgchqEtmjxN2Y1ZPGxkWrOVqJvpETWLinAJhNVuOqhiBh v0IJDA9+cIYWlMuJaVVKEEyG8N1sv758C3uHiRSjVjwtQ8ojsq1tGdVCUB1j0aJLjace kCMMOsphCYZ+i+wRYDqoUPvIEO55Net2irl0O908GpO0QUPY8B/d2IISjciyBeKb6ehi N2Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962094; x=1748566894; 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=cN9xzWmJm1SC3MYFl35NGHuTlOxpMSXq5/uxL1rDnZU=; b=gouyEpSPoDgIj2IiAeqnSqVswzeJkne6LvAAVdLyStu9RLqViQcUdnZCCqY44X8Mp2 jKPYpN04gfyytp7LouGeF0PckPVXG+KMfT/v0sPQFJuDDexJK/NRw0FYsDggYTI6Oa0J 4tBCgFA0xfYOYgLH2F5CecbyaH/uNkA8Ywz6GwSGAGZXIz1mklUeZir4Rcttkww6D5Xi waaLWoLcdVVozvz88y/VdDj85GNJ75ZVd5f4r4FXVr4Bi4zKlKuZ4v3BwXKmpchhGxuc rQAkBL5x45KDs+/+4+QFeanN3Z+IIKcwJA1jZIN/FKCQwWs7u2MrXOjBEybKOUgKGq1Z h7Tw== X-Forwarded-Encrypted: i=1; AJvYcCUNdt1UtDvcefpHOa67mDWPEVeIgT9FNYOs5K9Ex/sNSU03h23EtRAhMPPeO2xDsfuUPNf/uUO5q7OJdMI=@vger.kernel.org X-Gm-Message-State: AOJu0Yw9S5ZlIQ/k+xk9AmyWbk7v6LcNU1XG4g0F5ZLU3d5wlSbGQGjy V6WyAwmfT3upuTesVHuMroW8mZ4oNB0pNw/rvDU/RT0861AjH6D2nQi3oYwAjHFMtesqEhpPeHt sjlkzfw== X-Google-Smtp-Source: AGHT+IFprTymTRANY7N/PSfhfS9BlgTEeP5l7IBzmCyjwM1JbIF1IL97OX8WDdXl0zNwJCd+TJHfF3OvfiE= X-Received: from pjbnc5.prod.google.com ([2002:a17:90b:37c5:b0:30e:5bd5:880d]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3bc8:b0:2ee:c291:765a with SMTP id 98e67ed59e1d1-310e96c5a47mr1898327a91.8.1747962094614; Thu, 22 May 2025 18:01:34 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:55 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-51-seanjc@google.com> Subject: [PATCH v2 50/59] KVM: SVM: WARN if ir_list is non-empty at vCPU free From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that AVIC IRTE tracking is in a mostly sane state, WARN if a vCPU is freed with ir_list entries, i.e. if KVM leaves a dangling IRTE. Initialize the per-vCPU interrupt remapping list and its lock even if AVIC is disabled so that the WARN doesn't hit false positives (and so that KVM doesn't need to call into AVIC code for a simple sanity check). Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 5 +++-- arch/x86/kvm/svm/svm.c | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index d1f7b35c1b02..c55cbb0610b4 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -704,6 +704,9 @@ int avic_init_vcpu(struct vcpu_svm *svm) int ret; struct kvm_vcpu *vcpu =3D &svm->vcpu; =20 + INIT_LIST_HEAD(&svm->ir_list); + spin_lock_init(&svm->ir_list_lock); + if (!enable_apicv || !irqchip_in_kernel(vcpu->kvm)) return 0; =20 @@ -711,8 +714,6 @@ int avic_init_vcpu(struct vcpu_svm *svm) if (ret) return ret; =20 - INIT_LIST_HEAD(&svm->ir_list); - spin_lock_init(&svm->ir_list_lock); svm->dfr_reg =3D APIC_DFR_FLAT; =20 return ret; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 56d11f7b4bef..2cd991062acb 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1497,6 +1497,8 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm =3D to_svm(vcpu); =20 + WARN_ON_ONCE(!list_empty(&svm->ir_list)); + svm_leave_nested(vcpu); svm_free_nested(svm); =20 --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 CD0682D8DD7 for ; Fri, 23 May 2025 01:01:36 +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=1747962098; cv=none; b=JQKd8PLtdGKiBfpHV14ciOn6QFn+sPPaXcgs5ogpK0HQvYIllVrYzZh7+RmavzNDeObapzTQBCaEAF+FObL5Pqpx1KIP2uNI+uTx8kQ+BlpyViFJPg/1XlhTGo+3mL/L+0jNDa1pdRV76AIMPBBYu6gYGsWhWR5v25kMqTwIVHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962098; c=relaxed/simple; bh=Fn7KiHU3iwTubBKYwKGiV6sXLcq7ApYRqC3g9dRrbxA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=f6p5mkGP6FAia/3y3alwugTjV0nBGOeLfsmGayceNkP2M3tBByfE01m6btLyCi41o7XRE7hsMbBW5gi/jHvSqFzglc10OTzRDowpoa08qIyw8wBfPY+KFwx4dxBzy7EjZGpUIIbh9zh2eEbnc82L+foqAsDR/EQjkD5V0X6REwo= 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=T7cx3MkF; 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="T7cx3MkF" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b1f71ae2181so5500695a12.1 for ; Thu, 22 May 2025 18:01:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962096; x=1748566896; 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=2sXU5NirlWI4vXwO4O+/fMttYQH02wVCGMPtDp7+cuU=; b=T7cx3MkFa4HMkaJu6JgdZuYCTwhsWJjAjX8ep4O2dCj4Xan0j/LvkgLfGx23cJeRAW n0B/+DuQasfeFqHPEaqfK6XYiSCGtDZYDfIoWMmG+HI8IXBabwlxoB4MKEJG5D8PxW1n EM8ujuMsSvA+beEPFUImplfrp9qmoyoiVw3qSAoj6e2POdof2Mhr/846d0CyL7l0bzxi 7EYwtGxGMmKgG6z3/XOgKd2eFj2jURZ6MrMcKyjGSiVQHbHCgeNrC9zea3aaEY0kASjf UJB+ljy1Emh6zGzMyaXDsP+N2zc/cVjzV1rjS8WN3u4VdadOpmi7TRRXbRi5vcjOhfAv uNqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962096; x=1748566896; 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=2sXU5NirlWI4vXwO4O+/fMttYQH02wVCGMPtDp7+cuU=; b=q9nLIl9k2KDGCna+sVuo2IvrETSOOiTRzyuIuRNeuA5K0cepU6bi/xKq3d7tqGQxDB 8Zc7Jjx2UNwLaRyYotrvZftg06ld+aj155Uwup+THUQ7S5LP5zV//5KkrdZ/KYYJSdrv nfYW4AcKXYNeeRLBQOfDSIgDsVGsbnMGDP5c+Fw3afe6bZ7wQjDz0p1TR+GsRv/r1sY3 Sv+Khzr6DR3kWxmc6Kh1nUvB4Jpbvo0S173Wi1WQmvfShpYe3+9vt2JjaDVvkC+AjQ8r P9cK/7cpEie/9Rjud+MnmaAt4kzngPooM/niiaRv3l+xn9DSiiD8rGzWktZIcmBXmeV5 C7pw== X-Forwarded-Encrypted: i=1; AJvYcCV1CC+UtIRCx67phY1QeMm85k/bzcp0l7hlpmHDaACRm6l9Fe3ULGJe1L1Xpvf+sBd9hiIvHSytH684/rQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxQ16tx5MYcKmU69SoGsjobjIysBl8AQmjebf7hXsPC95JKPkwf bbDHsFeZS5BeOCF6BUeWAhDLt0m3c/xwQZM6otCXSO9l9tbWq8nO1fk5XOb8cHet47SU03TZWM+ 0F0QTng== X-Google-Smtp-Source: AGHT+IGiMys227SN7kU9jYy897MYWWwW5oyGCez8BLejTJH7O2I8eA/lDZQP1siXEpwe3ZlPz5yF5IFZSwI= X-Received: from pjbph7.prod.google.com ([2002:a17:90b:3bc7:b0:30a:9cb5:7622]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1645:b0:310:8d9a:ede1 with SMTP id 98e67ed59e1d1-310e96b6007mr1848663a91.4.1747962096179; Thu, 22 May 2025 18:01:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:56 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-52-seanjc@google.com> Subject: [PATCH v2 51/59] KVM: x86: Decouple device assignment from IRQ bypass From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use a dedicated counter to track the number of IRQs that can utilize IRQ bypass instead of piggybacking the assigned device count. As evidenced by commit 2edd9cb79fb3 ("kvm: detect assigned device via irqbypass manager"), it's possible for a device to be able to post IRQs to a vCPU without said device being assigned to a VM. Leave the calls to kvm_arch_{start,end}_assignment() alone for the moment to avoid regressing the MMIO stale data mitigation. KVM is abusing the assigned device count when applying mmio_stale_data_clear, and it's not at all clear if vDPA devices rely on this behavior. This will hopefully be cleaned up in the future, as the number of assigned devices is a terrible heuristic for detecting if a VM has access to host MMIO. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm-x86-ops.h | 2 +- arch/x86/include/asm/kvm_host.h | 4 +++- arch/x86/kvm/irq.c | 9 ++++++++- arch/x86/kvm/vmx/main.c | 2 +- arch/x86/kvm/vmx/posted_intr.c | 16 ++++++++++------ arch/x86/kvm/vmx/posted_intr.h | 2 +- arch/x86/kvm/x86.c | 3 +-- 7 files changed, 25 insertions(+), 13 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-= x86-ops.h index 8d50e3e0a19b..8897f509860c 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -112,7 +112,7 @@ KVM_X86_OP_OPTIONAL(update_cpu_dirty_logging) KVM_X86_OP_OPTIONAL(vcpu_blocking) KVM_X86_OP_OPTIONAL(vcpu_unblocking) KVM_X86_OP_OPTIONAL(pi_update_irte) -KVM_X86_OP_OPTIONAL(pi_start_assignment) +KVM_X86_OP_OPTIONAL(pi_start_bypass) KVM_X86_OP_OPTIONAL(apicv_pre_state_restore) KVM_X86_OP_OPTIONAL(apicv_post_state_restore) KVM_X86_OP_OPTIONAL_RET0(dy_apicv_has_pending_interrupt) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index c722adfedd96..01edcefbd937 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1382,6 +1382,8 @@ struct kvm_arch { atomic_t noncoherent_dma_count; #define __KVM_HAVE_ARCH_ASSIGNED_DEVICE atomic_t assigned_device_count; + unsigned long nr_possible_bypass_irqs; + #ifdef CONFIG_KVM_IOAPIC struct kvm_pic *vpic; struct kvm_ioapic *vioapic; @@ -1855,7 +1857,7 @@ struct kvm_x86_ops { int (*pi_update_irte)(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, struct kvm_vcpu *vcpu, u32 vector); - void (*pi_start_assignment)(struct kvm *kvm); + void (*pi_start_bypass)(struct kvm *kvm); void (*apicv_pre_state_restore)(struct kvm_vcpu *vcpu); void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu); bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index b6263ab4b18d..7586cf6f1215 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -570,10 +570,15 @@ int kvm_arch_irq_bypass_add_producer(struct irq_bypas= s_consumer *cons, spin_lock_irq(&kvm->irqfds.lock); irqfd->producer =3D prod; =20 + if (!kvm->arch.nr_possible_bypass_irqs++) + kvm_x86_call(pi_start_bypass)(kvm); + if (irqfd->irq_entry.type =3D=3D KVM_IRQ_ROUTING_MSI) { ret =3D kvm_pi_update_irte(irqfd, &irqfd->irq_entry); - if (ret) + if (ret) { + kvm->arch.nr_possible_bypass_irqs--; kvm_arch_end_assignment(irqfd->kvm); + } } spin_unlock_irq(&kvm->irqfds.lock); =20 @@ -606,6 +611,8 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass= _consumer *cons, } irqfd->producer =3D NULL; =20 + kvm->arch.nr_possible_bypass_irqs--; + spin_unlock_irq(&kvm->irqfds.lock); =20 =20 diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index d1e02e567b57..a986fc45145e 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -1014,7 +1014,7 @@ struct kvm_x86_ops vt_x86_ops __initdata =3D { .nested_ops =3D &vmx_nested_ops, =20 .pi_update_irte =3D vmx_pi_update_irte, - .pi_start_assignment =3D vmx_pi_start_assignment, + .pi_start_bypass =3D vmx_pi_start_bypass, =20 #ifdef CONFIG_X86_64 .set_hv_timer =3D vt_op(set_hv_timer), diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index 3a23c30f73cb..5671d59a6b6d 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -146,8 +146,13 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) =20 static bool vmx_can_use_vtd_pi(struct kvm *kvm) { + /* + * Note, reading the number of possible bypass IRQs can race with a + * bypass IRQ being attached to the VM. vmx_pi_start_bypass() ensures + * blockng vCPUs will see an elevated count or get KVM_REQ_UNBLOCK. + */ return irqchip_in_kernel(kvm) && kvm_arch_has_irq_bypass() && - kvm_arch_has_assigned_device(kvm); + READ_ONCE(kvm->arch.nr_possible_bypass_irqs); } =20 /* @@ -285,12 +290,11 @@ bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu) =20 =20 /* - * Bail out of the block loop if the VM has an assigned - * device, but the blocking vCPU didn't reconfigure the - * PI.NV to the wakeup vector, i.e. the assigned device - * came along after the initial check in vmx_vcpu_pi_put(). + * Kick all vCPUs when the first possible bypass IRQ is attached to a VM, = as + * blocking vCPUs may scheduled out without reconfiguring PID.NV to the wa= keup + * vector, i.e. if the bypass IRQ came along after vmx_vcpu_pi_put(). */ -void vmx_pi_start_assignment(struct kvm *kvm) +void vmx_pi_start_bypass(struct kvm *kvm) { if (!kvm_arch_has_irq_bypass()) return; diff --git a/arch/x86/kvm/vmx/posted_intr.h b/arch/x86/kvm/vmx/posted_intr.h index 94ed66ea6249..a4af39948cf0 100644 --- a/arch/x86/kvm/vmx/posted_intr.h +++ b/arch/x86/kvm/vmx/posted_intr.h @@ -17,7 +17,7 @@ bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu); int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, unsigned int host_irq, uint32_t guest_irq, struct kvm_vcpu *vcpu, u32 vector); -void vmx_pi_start_assignment(struct kvm *kvm); +void vmx_pi_start_bypass(struct kvm *kvm); =20 static inline int pi_find_highest_vector(struct pi_desc *pi_desc) { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 3966801bcb0d..3969e439a6bb 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13563,8 +13563,7 @@ bool kvm_arch_can_dequeue_async_page_present(struct= kvm_vcpu *vcpu) =20 void kvm_arch_start_assignment(struct kvm *kvm) { - if (atomic_inc_return(&kvm->arch.assigned_device_count) =3D=3D 1) - kvm_x86_call(pi_start_assignment)(kvm); + atomic_inc(&kvm->arch.assigned_device_count); } EXPORT_SYMBOL_GPL(kvm_arch_start_assignment); =20 --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 64C622DFA35 for ; Fri, 23 May 2025 01:01:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962100; cv=none; b=eBxgeNMI5VObPEfh3goTeDdgHk4/mPTxnMf/wiQWCWtADou4mu2ZhjMB7Q0UJN4USVlJ2HM7bg28jml7qaZQR/JZVPRWqlUxLnAFBhZrVpkQL+J0EdESpnrbRS8cUmXVHM8+gb6pBygAYFUKg4egvPSWVoVjEyIZE9FXKa6H+fc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962100; c=relaxed/simple; bh=NGIANfuyMzXoKJquKbW+ehrpEIHWV+x021PzJ4EknsQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EBzt196UQP8OWrdaFxgYaXK9zi8a143KnQXN8Nca9LMZuZ45WyrIiO5+b3vAzqyOsFcTWneyYN9XQ0GTAiXL4n7QBqkp7iM03JsxAGGWdK4ppLQXvVvR87nks90I0Tq38baoUb/KS/ALXOPtOIOxxtZxTtCqRo8tUpP88jNT3o4= 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=QQnuiBOm; arc=none smtp.client-ip=209.85.210.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="QQnuiBOm" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7394792f83cso6893236b3a.3 for ; Thu, 22 May 2025 18:01:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962098; x=1748566898; 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=ExoOfw9G/N1sSoTRHxtPNhWhn4iA3dUD7FqrwsLiXLk=; b=QQnuiBOmRLCBMvJSvBLZ+OwB9aToIf8vUM8eei6KvtRbOTa4rUbo3S/G7ch1tGaqkR q7hXtFw3pJPKnT/9qhY8Wuje/m5vuV53N6FhSM6qkcopmYJjrumLnLpbdhlNyIK8AOYz luQ824DvDQXnPr09zy4MtmOwwy3BFCLKOuZLXePVZY0WyWY5AhmSvjMJkdCMgmLezJ3i kO43LBGzsALU39YgYLk2kk04jIc7pZPxwqhJFcuNXzfpQ7imeMvS4iQV3z7RKeMBZkn+ ned33u0meiCi8EjrTBsHFeiPIBNBkbdEz2/qQGOjEMg0i9TnmVd1M2OnfqH2QfglOZGm yn2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962098; x=1748566898; 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=ExoOfw9G/N1sSoTRHxtPNhWhn4iA3dUD7FqrwsLiXLk=; b=CWHPQQgXJhOlCpuQcp3htldFY44Q9KSKI/HlhypJ50bHPZHeaLIFoamwCde8kjiINv jCs+zpdMcLhdr9/X9Fgonrd6LhQyc+BuKeV6qYKne90L4wkIJGEOXntUMz+uOfFIn0iv lwM9fZa8/sbTEXHO454hFSZFgk4HUILqsCQvDzwEZmjccr8anveIjYoHCEBJ3Na+BkD3 fAmO2Ef5SAj61s0aeYlKABQG+3qBnb5sC3+PyIdNiHVWNUnntmyRnDCHs3/j3y4Cz82Z 1RnJjDULJIRO1iHxBqm35mkKr1KYPNml8ni+rlbSIM2P3b9VAgBXWnKMFG/w2dIPzmRW ZvqQ== X-Forwarded-Encrypted: i=1; AJvYcCXEt8/AU5Z+U52Kz+qPGfBWZoIHB/9t2QK+ITa4PrZloK+qg6ae4je7XQ/vsr5+SEJQBw4P2h1COaotOLA=@vger.kernel.org X-Gm-Message-State: AOJu0Yy2UqYBtT+knAI3H31iXAvXMCE0C/7lcQtHebCbUfumr1KSKBPV paqSaDufjd0LzTKwEMJDWhKVoRy06hIQTTm6leJPfNB2SvoP4A70UEuJ7UTUsyvvK/NH4OJvYxE u3P+tbA== X-Google-Smtp-Source: AGHT+IHRskctku8TAbxqTc5JSLZa7cBf4pJ0pj9pJzlkOC6dQa0UxJjlReXD4jK1iH3OFzYeZWUC+JH7b6A= X-Received: from pfaq8.prod.google.com ([2002:a05:6a00:a888:b0:742:a83f:bc0f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:198e:b0:740:91e4:8107 with SMTP id d2e1a72fcca58-742a963242bmr38441580b3a.0.1747962097793; Thu, 22 May 2025 18:01:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:57 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-53-seanjc@google.com> Subject: [PATCH v2 52/59] KVM: VMX: WARN if VT-d Posted IRQs aren't possible when starting IRQ bypass From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" WARN if KVM attempts to "start" IRQ bypass when VT-d Posted IRQs are disabled, to make it obvious that the logic is a sanity check, and so that a bug related to nr_possible_bypass_irqs is more like to cause noisy failures, e.g. so that KVM doesn't silently fail to wake blocking vCPUs. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/posted_intr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index 5671d59a6b6d..4a6d9a17da23 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -296,7 +296,7 @@ bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu) */ void vmx_pi_start_bypass(struct kvm *kvm) { - if (!kvm_arch_has_irq_bypass()) + if (WARN_ON_ONCE(!vmx_can_use_vtd_pi(kvm))) return; =20 kvm_make_all_cpus_request(kvm, KVM_REQ_UNBLOCK); --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 187B02DFA59 for ; Fri, 23 May 2025 01:01:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962101; cv=none; b=QjZk78+7pASNsZzNqwwgI+iiXeXkwW4qn0PNCBbC937U4HdaRRFZo9rWHrVljtbQBIKZkRR5MdtH1anTYdzLS99aplgY40RqnkJb1BA1qtj8qEOz0mMIdkhwz09bfsxnn5drhMJk8u1rbGMBkTxGMhaSRX/8krIQzKt4SnN8bKg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962101; c=relaxed/simple; bh=NCig6YHGLRFgPfrabyTYoAuvX536eJAkulIf4F/gxYI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NUQ2nqPuT3Q+uCJ5KaTRn18oT95dDu/MFuTc6su8d9EM9INsxxVN7+WJrq/bfvj5m/yoCEqAIueOSDP9s6F1p82f7afXV3CrYUpKIDj5TpiXDwnf5apktL8wVnLnMGwZcdzLDYQbd0SEgOc1YrhMp10hFN8MloUhmyiJsYSNbwU= 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=cegDBtvO; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cegDBtvO" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30ea0e890ccso6055220a91.2 for ; Thu, 22 May 2025 18:01:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962099; x=1748566899; 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=nqWl6kKGGVzLsyDM6W+SZZXuWlFhon7/lNEzScLQdD4=; b=cegDBtvO5SBNP/7Q/U9SWPw1FtTbByhFsKKH69zAdJQ5ccAVSRnNnxnmscstx5x6a4 eSn6CmqWBxAyVqOCxOjgpbAFzT/j5GAaPXFzuok6i6VDMDE9xgMWVM+zS+jtYcMaTudw Z7m1WN8FIo3SmQwy6HFUOs1N7VjCTG8v8+rcwc9SCaHojuihZxzGxp0ThIyJYh3LyWvX aU//dqFBYfwZlfizvi5OL3FAJJDVY+woR/ylf30o1m+6b+YcfcnafZym+TvtpwWCKoWN Oey0HBbHM4i6XlAKhSHb5tcCNWzI3G3RDh8yzmJTAV4fgcArwaGNELSdSAdP1Ekp/p7Y O9LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962099; x=1748566899; 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=nqWl6kKGGVzLsyDM6W+SZZXuWlFhon7/lNEzScLQdD4=; b=NA84MKfA56baUaVa7sOZM8WYNgTWhAXeJSqhviLQ7cd3svM2y9LQr6XYaANTCqFx46 Mr2cFxWaoMmxwezUna/OLexaLoW++/29W1C8JRVRljlASeVw71i7SpjEQFsJWWJNwvWD JWRDhv4TdWYxhamVz98RXQtUTqt7GJNcaXoSQAB4LTDVFl3gvH1zklw4JnBQ5q+8R0tI KQYsEpv502kSkrJFuVnWZHgQxeXnicvBqwUu0o9IC6/A4m0SsWLtMoBpcRKKW6f/AsMs 28ymT+9Y2nfHL9YQVwsO6xuMha61Etc1e+7dYEW5zE5kPpyj46EB2UvJ0yYz51ZlcwE9 dP+Q== X-Forwarded-Encrypted: i=1; AJvYcCXa7BqgQvmf9UbMES+Cz+EYrmEkZQKiSRqtmtBazzNSUsBx914eZWRLDutTpVRGd+tcRMTaevT0KXZvlpE=@vger.kernel.org X-Gm-Message-State: AOJu0YxEIYNp6Xk7nUZHhhENqpDI16uinFaZH+MigvdqMRPMe5yCz8lw oOT6tMqTrdHR0Sf0pQpzLynyilRwHzGvGFGnJSJD+r8rLCRB3ZcYzpXBgycgQ1uWfpfKlMsn6xQ JW2c5dQ== X-Google-Smtp-Source: AGHT+IHfPxgYk0wR6t6dyJZ3yx5kSrKHj7TcYQcATL6HPxhlSG8Y4glc9acQIePiTqsfQiuWHduuc+FefIE= X-Received: from pjbpt18.prod.google.com ([2002:a17:90b:3d12:b0:2ff:6e58:8a0a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4b82:b0:2ff:7b28:a51a with SMTP id 98e67ed59e1d1-30e8314db35mr46156413a91.17.1747962099385; Thu, 22 May 2025 18:01:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:58 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-54-seanjc@google.com> Subject: [PATCH v2 53/59] KVM: SVM: Use vcpu_idx, not vcpu_id, for GA log tag/metadata From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use a vCPU's index, not its ID, for the GA log tag/metadata that's used to find and kick vCPUs when a device posted interrupt serves as a wake event. Lookups on a vCPU index are O(fast) (not sure what xa_load() actually provides), whereas a vCPU ID lookup is O(n) if a vCPU's ID doesn't match its index. Unlike the Physical APIC Table, which is accessed by hardware when virtualizing IPIs, hardware doesn't consume the GA tag, i.e. KVM _must_ use APIC IDs to fill the Physical APIC Table, but KVM has free rein over the format/meaning of the GA tag. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index c55cbb0610b4..bb74705d6cfd 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -29,36 +29,39 @@ #include "svm.h" =20 /* - * Encode the arbitrary VM ID and the vCPU's default APIC ID, i.e the vCPU= ID, - * into the GATag so that KVM can retrieve the correct vCPU from a GALog e= ntry - * if an interrupt can't be delivered, e.g. because the vCPU isn't running. + * Encode the arbitrary VM ID and the vCPU's _index_ into the GATag so that + * KVM can retrieve the correct vCPU from a GALog entry if an interrupt ca= n't + * be delivered, e.g. because the vCPU isn't running. Use the vCPU's index + * instead of its ID (a.k.a. its default APIC ID), as KVM is guaranteed a = fast + * lookup on the index, where as vCPUs whose index doesn't match their ID = need + * to walk the entire xarray of vCPUs in the worst case scenario. * - * For the vCPU ID, use however many bits are currently allowed for the max + * For the vCPU index, use however many bits are currently allowed for the= max * guest physical APIC ID (limited by the size of the physical ID table), = and * use whatever bits remain to assign arbitrary AVIC IDs to VMs. Note, the * size of the GATag is defined by hardware (32 bits), but is an opaque va= lue * as far as hardware is concerned. */ -#define AVIC_VCPU_ID_MASK AVIC_PHYSICAL_MAX_INDEX_MASK +#define AVIC_VCPU_IDX_MASK AVIC_PHYSICAL_MAX_INDEX_MASK =20 #define AVIC_VM_ID_SHIFT HWEIGHT32(AVIC_PHYSICAL_MAX_INDEX_MASK) #define AVIC_VM_ID_MASK (GENMASK(31, AVIC_VM_ID_SHIFT) >> AVIC_VM_ID_SHI= FT) =20 #define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VM_ID_SHIFT) & AVIC_VM_ID_MASK) -#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK) +#define AVIC_GATAG_TO_VCPUIDX(x) (x & AVIC_VCPU_IDX_MASK) =20 -#define __AVIC_GATAG(vm_id, vcpu_id) ((((vm_id) & AVIC_VM_ID_MASK) << AVIC= _VM_ID_SHIFT) | \ - ((vcpu_id) & AVIC_VCPU_ID_MASK)) -#define AVIC_GATAG(vm_id, vcpu_id) \ +#define __AVIC_GATAG(vm_id, vcpu_idx) ((((vm_id) & AVIC_VM_ID_MASK) << AVI= C_VM_ID_SHIFT) | \ + ((vcpu_idx) & AVIC_VCPU_IDX_MASK)) +#define AVIC_GATAG(vm_id, vcpu_idx) \ ({ \ - u32 ga_tag =3D __AVIC_GATAG(vm_id, vcpu_id); \ + u32 ga_tag =3D __AVIC_GATAG(vm_id, vcpu_idx); \ \ - WARN_ON_ONCE(AVIC_GATAG_TO_VCPUID(ga_tag) !=3D (vcpu_id)); \ + WARN_ON_ONCE(AVIC_GATAG_TO_VCPUIDX(ga_tag) !=3D (vcpu_idx)); \ WARN_ON_ONCE(AVIC_GATAG_TO_VMID(ga_tag) !=3D (vm_id)); \ ga_tag; \ }) =20 -static_assert(__AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_ID_MASK) =3D=3D -1u); +static_assert(__AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_IDX_MASK) =3D=3D -1u= ); =20 static bool force_avic; module_param_unsafe(force_avic, bool, 0444); @@ -139,16 +142,16 @@ int avic_ga_log_notifier(u32 ga_tag) struct kvm_svm *kvm_svm; struct kvm_vcpu *vcpu =3D NULL; u32 vm_id =3D AVIC_GATAG_TO_VMID(ga_tag); - u32 vcpu_id =3D AVIC_GATAG_TO_VCPUID(ga_tag); + u32 vcpu_idx =3D AVIC_GATAG_TO_VCPUIDX(ga_tag); =20 - pr_debug("SVM: %s: vm_id=3D%#x, vcpu_id=3D%#x\n", __func__, vm_id, vcpu_i= d); - trace_kvm_avic_ga_log(vm_id, vcpu_id); + pr_debug("SVM: %s: vm_id=3D%#x, vcpu_idx=3D%#x\n", __func__, vm_id, vcpu_= idx); + trace_kvm_avic_ga_log(vm_id, vcpu_idx); =20 spin_lock_irqsave(&svm_vm_data_hash_lock, flags); hash_for_each_possible(svm_vm_data_hash, kvm_svm, hnode, vm_id) { if (kvm_svm->avic_vm_id !=3D vm_id) continue; - vcpu =3D kvm_get_vcpu_by_id(&kvm_svm->kvm, vcpu_id); + vcpu =3D kvm_get_vcpu(&kvm_svm->kvm, vcpu_idx); break; } spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); @@ -785,7 +788,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, */ struct amd_iommu_pi_data pi_data =3D { .ga_tag =3D AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, - vcpu->vcpu_id), + vcpu->vcpu_idx), .is_guest_mode =3D kvm_vcpu_apicv_active(vcpu), .vapic_addr =3D avic_get_backing_page_address(to_svm(vcpu)), .vector =3D vector, --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 9247A2DFA3F for ; Fri, 23 May 2025 01:01:41 +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=1747962103; cv=none; b=a16Iq/rwANiLLKNNhHSYxdV+mQHIgffDbanZj2phWwcmh8KW7Fv8UBg/0/l2EERYf+qCh77xCpyxmstlihhwbppHmMqIMa5aSp6IlTMaSYFbAz/9NUPObjGDLjIk13AVS6ITW2gnyuZ/M5ChLsXpS9FBZF0wK6n1Ob5sE2ARB60= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962103; c=relaxed/simple; bh=afVOpJ4NkdTCnGlO7BZombLV0TeNOaBswisvqbRPcJo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ve3nBubXmVBnfWiboJTxA77/u/Xq66Nq+bJqT96XZRF3udf+Qu5+SJFcfBo1d4JljDrr0UyTOGRup9EXyKxJxl2MIqfOI6xlHPptKIb1n35mI/Yrka2qBuxSQBTuYxIvqtg1MgL8DnQR/De1Gr2zk0/QKBMnf9nBEZ+YGWGbtYo= 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=zglV9pXa; 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="zglV9pXa" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b2c0cbf9fddso406100a12.3 for ; Thu, 22 May 2025 18:01:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962101; x=1748566901; 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=IHC46AiM3qnXIL3uFh2RBrG63I23lKwAO4ZOidIFzK0=; b=zglV9pXaY6tPmhkZMteS7ceglFoiwF8Nxta//bzSZ6s/9lEARrlnJyf1AOWB8OHvg5 or9gf61AoEnyNRHYHDxKcqsgzHUCXkg/xJpqpUM1i1ocCaxiqb2sMihsCNfrSjkb26xH Q5wemGUGRAeuaAqsR/T9JKFvV4/ItKXegmRkZ+OKdwKdXwPYLISzLTpHhkI5+KNCtFZP MvA4bM/zeRHnDuw8/cX1bOrRYNcC8ZXsrnL5etOK26myO5ljtqUjZyBu7gq5EIWjG87/ Ia2/KTJLqDMt3qRHyWVB0jXC4U8OqqQflGgA46bdCkPJu0ZwDSSCliduqZ6hag3FnK/L GH3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962101; x=1748566901; 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=IHC46AiM3qnXIL3uFh2RBrG63I23lKwAO4ZOidIFzK0=; b=JL0/UXrycUKza146V/gCZxjKB95RbJ8GLPqAiWUmwWngspMfMZvVKNg8uzeg+8b6RI bwsNqZeza9pZqdF6p1T1AJRfLbd2w1157VR89ywi4l7/WkuA1PFv0iOa02eGR07QQ55T 4pzgal87TimuKTOoRISA8JNzOGddb2L4LKxz1lYOC//AeGQ+T6N5BAn/4bEbyNzcnjp0 2yoX0FDPNpuSbO0phZ2MVzQBr5OfjBF/4GGf1pA7r7OMzKjkB6gMryLnns43cRP94jQx xOICVtMQKED+wcQHoGcwaQGy65/pr+Sn0Rb+sc/4ZNxXm+h+ITGILiUJphYEJsVBUffJ IWHw== X-Forwarded-Encrypted: i=1; AJvYcCUAuHHsKi3PA/+2J6nL7q9GQBlDapNTH7WhXy+VToYslZijWHAbdhDFIc1nYL8XchUIDR2o43FOvGUbSc0=@vger.kernel.org X-Gm-Message-State: AOJu0YyD0n4H30wIhCqV1fPC3BXWuBhvGXWl3JGtC/iO5FDq+GXHGlag evovGRt9o/8TUrTyjvpzzUfFriPiwzmZ+O7kAODJBLvU8gsgO9wleFhPStntqKNt6Piw/5pGGRj URC3evw== X-Google-Smtp-Source: AGHT+IFKx4toGdoUHBPdA+oUHyZ0aWvYYcvHAK1ugv08dOHNkGrE3/X+OpJV9alJPESRVigE+cn0CzzRfh4= X-Received: from pfbhd3.prod.google.com ([2002:a05:6a00:6583:b0:742:a60b:3336]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6300:8b0f:b0:215:eb6b:8714 with SMTP id adf61e73a8af0-2170cdf1503mr41735775637.30.1747962101096; Thu, 22 May 2025 18:01:41 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 17:59:59 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-55-seanjc@google.com> Subject: [PATCH v2 54/59] iommu/amd: WARN if KVM calls GA IRTE helpers without virtual APIC support From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" WARN if KVM attempts to update IRTE entries when virtual APIC isn't fully supported, as KVM should guard all such calls on IRQ posting being enabled. Signed-off-by: Sean Christopherson --- drivers/iommu/amd/iommu.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index becef69a306d..926dcdfe08c8 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3836,8 +3836,10 @@ int amd_iommu_update_ga(int cpu, void *data) struct amd_ir_data *ir_data =3D (struct amd_ir_data *)data; struct irte_ga *entry =3D (struct irte_ga *) ir_data->entry; =20 - if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) || - !entry || !entry->lo.fields_vapic.guest_mode) + if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) + return -EINVAL; + + if (!entry || !entry->lo.fields_vapic.guest_mode) return 0; =20 if (!ir_data->iommu) @@ -3856,7 +3858,10 @@ int amd_iommu_activate_guest_mode(void *data, int cp= u) struct irte_ga *entry =3D (struct irte_ga *) ir_data->entry; u64 valid; =20 - if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) || !entry) + if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) + return -EINVAL; + + if (!entry) return 0; =20 valid =3D entry->lo.fields_vapic.valid; @@ -3885,8 +3890,10 @@ int amd_iommu_deactivate_guest_mode(void *data) struct irq_cfg *cfg =3D ir_data->cfg; u64 valid; =20 - if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) || - !entry || !entry->lo.fields_vapic.guest_mode) + if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) + return -EINVAL; + + if (!entry || !entry->lo.fields_vapic.guest_mode) return 0; =20 valid =3D entry->lo.fields_remap.valid; --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 861342E3379 for ; Fri, 23 May 2025 01:01:43 +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=1747962105; cv=none; b=JTzMcvSuW6Wy1JkuJgBluCqzoL4nCv5dyGkS+pi00FZg1xX9ZrdgqP8zHhZb3Js5RHXa2XHOcC7/BhQVNYgvMbyAWSYJ733UHT/iCCCmDiZSnR2D0ClVT5WDYOkDqmzd7jTBrtvZ1nYdyLfsj/kLXoGprsbuHC11mskzQnbObC8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962105; c=relaxed/simple; bh=KsfYh+dTBbuGfYNIW3ziFqaFVeYMMEL6fm+SPmkC5dI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MsZb02c9OcZ9tR0QJB1nD1ns6gXu2cpUjXiipPHCmvcTHRS1K9tWFyhinM1A0DEmYRf4EN9IDf320BLNRj94DQUx+6YuTHpwcEsvugGmy5hefMiByBgtOmD4NrKq4UqrhQlxjC1TT002/IcSz01NK58n0Oaj8kYqd/1mgpS3kzI= 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=XltjOMjb; 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="XltjOMjb" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b269789425bso8822135a12.0 for ; Thu, 22 May 2025 18:01:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962103; x=1748566903; 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=R3ZFTLMU7HrPafLz7H8LY3RnxdvA+oHT6/HRYUM5hWg=; b=XltjOMjb80WtYODBfejwxyX6buXksMHJ2vSyKU4N4HzI4wy1jRkX6IYdkK/YeBmhTY 0JKRPeicGVM5RRUB/AjA1pw2H454fl8Jf0l8d/3GGFRintXUsQfVpI8IIkhXNEv0nMm3 tP583wf55gAp92pAUQ15u4RsMxoRJXwuVn4pCnfj/DOu7KmVOR9u0bRcQd4WZAmRgapy MF772Wr+Fo7A7EBLoVnTcyMqrH4reh7wjkT6BOCaM7Yz2zm+wiqnPhuLpF2cOZEHyj57 Bukvvzs7PT13PPeCSskbxR468rfkCpO/j0bwQctOVldNzs1qelVdoWL7E2Ptf/RHIybu qEOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962103; x=1748566903; 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=R3ZFTLMU7HrPafLz7H8LY3RnxdvA+oHT6/HRYUM5hWg=; b=uEmJzgrtMP0g8t3FLl9GIzw0isR5EmJ1QhiH75OLcnfzPKRyXEaUnuwBFPiG5/sc69 glt1r/qvtf96EW5dLqpGmYdFJlGU38Wq9f6kCik75rb+bPGNDoNbSDfTGJFhk5/GmZyV aLH4v0Q+ZzaLk5jsNVKKEnN+ZrCsdCBKPU2j0EWFuX8KCNmZUFjO5GQOVWt4D+ca12JI cV4RbfgoocHJRQG18isz67uzwPfIa6HRiQbdv0FX3F1Jru2ZRjgTcKnF2bnNqvEZyeyC slSCV7euVM2MQYzeNUt3nafbD2KKyTsvjWUVAmlBWwQaJx2d2PaEo144Ayg0zXhWKNee Rk3w== X-Forwarded-Encrypted: i=1; AJvYcCWJH06btAcJACCJpXpG94c9koQ5gdzjBVL4OxAIsmmVPbfPvs0s9xtglcO6iov2TWiwK/q5xRW2kng+oys=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2Wn/Jj9b1T5u3sTrLuKS3Ea6xsQfGrKCsrDhWzKuY0F2Zg2Ma JGsfkt/8W4pXklpq6cTc8zuY/b5yC+pIXN0Ebbdc3fi8hH6cVkOV4X73w7e7oczmYxW+wVfP1FO DrjFCwA== X-Google-Smtp-Source: AGHT+IEtGHV3UU5r6+on0Bi3QnTx914rosO/P/5CGIo8zL7GgKodqhGaDH9iis1m/ryoELkyNhSi6H+FbVI= X-Received: from pjbsi6.prod.google.com ([2002:a17:90b:5286:b0:2fc:1158:9fe5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3f4c:b0:2ee:5958:828 with SMTP id 98e67ed59e1d1-30e7d522155mr46027060a91.9.1747962102833; Thu, 22 May 2025 18:01:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 18:00:00 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-56-seanjc@google.com> Subject: [PATCH v2 55/59] KVM: SVM: Fold avic_set_pi_irte_mode() into its sole caller From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Fold avic_set_pi_irte_mode() into avic_refresh_apicv_exec_ctrl() in anticipation of moving the __avic_vcpu_{load,put}() calls into the critical section, and because having a one-off helper with a name that's easily confused with avic_pi_update_irte() is unnecessary. No functional change intended. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 52 ++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index bb74705d6cfd..9ddec6f3ad41 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -728,34 +728,6 @@ void avic_apicv_post_state_restore(struct kvm_vcpu *vc= pu) avic_handle_ldr_update(vcpu); } =20 -static void avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) -{ - int apic_id =3D kvm_cpu_get_apicid(vcpu->cpu); - unsigned long flags; - struct vcpu_svm *svm =3D to_svm(vcpu); - struct kvm_kernel_irqfd *irqfd; - - /* - * Here, we go through the per-vcpu ir_list to update all existing - * interrupt remapping table entry targeting this vcpu. - */ - spin_lock_irqsave(&svm->ir_list_lock, flags); - - if (list_empty(&svm->ir_list)) - goto out; - - list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) { - void *data =3D irqfd->irq_bypass_data; - - if (activate) - WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apic_id)); - else - WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data)); - } -out: - spin_unlock_irqrestore(&svm->ir_list_lock, flags); -} - static void svm_ir_list_del(struct kvm_kernel_irqfd *irqfd) { struct kvm_vcpu *vcpu =3D irqfd->irq_bypass_vcpu; @@ -990,6 +962,10 @@ void avic_refresh_virtual_apic_mode(struct kvm_vcpu *v= cpu) void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) { bool activated =3D kvm_vcpu_apicv_active(vcpu); + int apic_id =3D kvm_cpu_get_apicid(vcpu->cpu); + struct vcpu_svm *svm =3D to_svm(vcpu); + struct kvm_kernel_irqfd *irqfd; + unsigned long flags; =20 if (!enable_apicv) return; @@ -1001,7 +977,25 @@ void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vc= pu) else avic_vcpu_put(vcpu); =20 - avic_set_pi_irte_mode(vcpu, activated); + /* + * Here, we go through the per-vcpu ir_list to update all existing + * interrupt remapping table entry targeting this vcpu. + */ + spin_lock_irqsave(&svm->ir_list_lock, flags); + + if (list_empty(&svm->ir_list)) + goto out; + + list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) { + void *data =3D irqfd->irq_bypass_data; + + if (activated) + WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apic_id)); + else + WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data)); + } +out: + spin_unlock_irqrestore(&svm->ir_list_lock, flags); } =20 void avic_vcpu_blocking(struct kvm_vcpu *vcpu) --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 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 40B092E62D0 for ; Fri, 23 May 2025 01:01:45 +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=1747962108; cv=none; b=AYq77HiqlXrLw+qZK6O4EKHWcPjmcJBEhx6khw5WNulU/3uJ156K8GaxYRgbnyjzEuvYrzJjcRIfxNd+3/cdhURZw2iTCHdl5Slm459MybzvDKDZfXAQP+lptQ9AXu3DwUDxaVNXczLl/VGMfBGTEdeSJwdkJJXm5RUugjJp59I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962108; c=relaxed/simple; bh=2QBb5/XIqsfvIc456zHCbrENEH2By/61zuGLujz3Rzg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sVlaWEnEJQTCI7Hkvx/69ZYE+6bpPrF3abupAWLD0vs6ac4/z7lFg8yToJ1n60Iu5tNZDwi7aV6+J0OyQw33wlZg5/YSbA7fMRbVm07K/5VlGb85nzzQKHOyQJU/l/k1gIlIgGUp0TKtd4Y3ySBvFJjqoSuI+rtDaYSAaHjuIhY= 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=QfkKzsfe; 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="QfkKzsfe" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b26e33ae9d5so8987268a12.1 for ; Thu, 22 May 2025 18:01:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962104; x=1748566904; 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=/e9UYgTv1sZ7z5b1x8uYjd+06D7YFK/4pMDs1/TTKKw=; b=QfkKzsfeFflEs6xTiI1fsNSIla/NbjaOyz1VSwJ3m5yWrSrfsG0OSbh4t2uclml7Rk D5oADYLyImCkDLjZGwX/1D/pEEODJCfsjHGJoUz1svOeZD/Q5ira51kmibOGEXInjEUI re3YjEd4m8JwnqeFlF+b+MzHMMvIS4VUDA79891LR6jk6oIavpUYcAUzkQ5Wk8Q4Yh3a v7GeI9OHf+TbUovohzYZv3r+sa6lkDNYXoy2OC4qiNyifZ457HJ5qjeeEid+bw2hY916 ZGTB5YQEPkT/+k92T2yrJDy9UAOZElTr4iWNG6q+DsJn24P5ntseEwEuB/by//9wZvaD VQ5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962104; x=1748566904; 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=/e9UYgTv1sZ7z5b1x8uYjd+06D7YFK/4pMDs1/TTKKw=; b=R6CbZrGYNxLjsNN+0bkv2cWArm8sAVkNDxRmZRzi4FJEMgbakasgXx04Ngn8IhLIqr ATzLxdI0GqV05ECAb+cvsebZ4ieISis4ZdWbS2/fLmQBLnrSbQsigVgIME8Kk3vSwbnY +vEGT7fbyj1ARYE80kGxVn9FKsrwmNDnrqm4/nRdzGwtbdbCVJd5ErSRD1o2TiwRBFUt RATtaRajPVl2KuXcUEAL6GzHtIVO115vzQXv5jqRrEgxlsGvLP525rO4zLNnHZgP+vKj iOys/ZG6guTx8YdpInu5SPdqQdbAkV/Y8YWPZGiEcHjwwUbEqNvEPb7JLFP1BSIS3IU8 aI9g== X-Forwarded-Encrypted: i=1; AJvYcCVV0lOYB0k6SxgS3jsyiRtGktaHJzn2jYImjUtDl2P1AYZlshx6T0TLQgc40vOUBu1EW7oZtk4si+Ym8/0=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7RXpwlD8eDcWeUTRUfyJgrXdDkKw0l0E7a8hLDBB6bu0ZOlgx s+qDn40vpIHzu2KGruyVlwsesaS9+yZrwJG06vx4fSnhJQS7vEpj7E1gqZ7rifEH2B0zoYFPle5 j3+cNdA== X-Google-Smtp-Source: AGHT+IEQRYTnAxDbAwwyPpwl8BUMXr/CByqDWtsE69Ozfi2WC7I9F1/uYDQUgOID+Lp8MTwFn4NoVPso1aA= X-Received: from pgbcz10.prod.google.com ([2002:a05:6a02:230a:b0:b0d:967f:23de]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:a103:b0:218:6dc8:2cb with SMTP id adf61e73a8af0-2186dc802cfmr5175120637.0.1747962104546; Thu, 22 May 2025 18:01:44 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 18:00:01 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-57-seanjc@google.com> Subject: [PATCH v2 56/59] KVM: SVM: Don't check vCPU's blocking status when toggling AVIC on/off From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Don't query a vCPU's blocking status when toggling AVIC on/off; barring KVM bugs, the vCPU can't be blocking when refreshing AVIC controls. And if there are KVM bugs, ensuring the vCPU and its associated IRTEs are in the correct state is desirable, i.e. well worth any overhead in a buggy scenario. Isolating the "real" load/put flows will allow moving the IOMMU IRTE (de)activation logic from avic_refresh_apicv_exec_ctrl() to avic_update_iommu_vcpu_affinity(), i.e. will allow updating the vCPU's physical ID entry and its IRTEs in a common path, under a single critical section of ir_list_lock. Tested-by: Sairaj Kodilkar Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 65 +++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 9ddec6f3ad41..1e6e5d1f6b4e 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -828,7 +828,7 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_= vcpu *vcpu, int cpu) WARN_ON_ONCE(amd_iommu_update_ga(cpu, irqfd->irq_bypass_data)); } =20 -void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) +static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); int h_physical_id =3D kvm_cpu_get_apicid(cpu); @@ -844,16 +844,6 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) if (WARN_ON_ONCE(vcpu->vcpu_id * sizeof(entry) >=3D PAGE_SIZE)) return; =20 - /* - * No need to update anything if the vCPU is blocking, i.e. if the vCPU - * is being scheduled in after being preempted. The CPU entries in the - * Physical APIC table and IRTE are consumed iff IsRun{ning} is '1'. - * If the vCPU was migrated, its new CPU value will be stuffed when the - * vCPU unblocks. - */ - if (kvm_vcpu_is_blocking(vcpu)) - return; - /* * Grab the per-vCPU interrupt remapping lock even if the VM doesn't * _currently_ have assigned devices, as that can change. Holding @@ -888,31 +878,33 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) spin_unlock_irqrestore(&svm->ir_list_lock, flags); } =20 -void avic_vcpu_put(struct kvm_vcpu *vcpu) +void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) +{ + /* + * No need to update anything if the vCPU is blocking, i.e. if the vCPU + * is being scheduled in after being preempted. The CPU entries in the + * Physical APIC table and IRTE are consumed iff IsRun{ning} is '1'. + * If the vCPU was migrated, its new CPU value will be stuffed when the + * vCPU unblocks. + */ + if (kvm_vcpu_is_blocking(vcpu)) + return; + + __avic_vcpu_load(vcpu, cpu); +} + +static void __avic_vcpu_put(struct kvm_vcpu *vcpu) { struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); struct vcpu_svm *svm =3D to_svm(vcpu); unsigned long flags; - u64 entry; + u64 entry =3D svm->avic_physical_id_entry; =20 lockdep_assert_preemption_disabled(); =20 if (WARN_ON_ONCE(vcpu->vcpu_id * sizeof(entry) >=3D PAGE_SIZE)) return; =20 - /* - * Note, reading the Physical ID entry outside of ir_list_lock is safe - * as only the pCPU that has loaded (or is loading) the vCPU is allowed - * to modify the entry, and preemption is disabled. I.e. the vCPU - * can't be scheduled out and thus avic_vcpu_{put,load}() can't run - * recursively. - */ - entry =3D svm->avic_physical_id_entry; - - /* Nothing to do if IsRunning =3D=3D '0' due to vCPU blocking. */ - if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) - return; - /* * Take and hold the per-vCPU interrupt remapping lock while updating * the Physical ID entry even though the lock doesn't protect against @@ -932,7 +924,24 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); =20 spin_unlock_irqrestore(&svm->ir_list_lock, flags); +} =20 +void avic_vcpu_put(struct kvm_vcpu *vcpu) +{ + /* + * Note, reading the Physical ID entry outside of ir_list_lock is safe + * as only the pCPU that has loaded (or is loading) the vCPU is allowed + * to modify the entry, and preemption is disabled. I.e. the vCPU + * can't be scheduled out and thus avic_vcpu_{put,load}() can't run + * recursively. + */ + u64 entry =3D to_svm(vcpu)->avic_physical_id_entry; + + /* Nothing to do if IsRunning =3D=3D '0' due to vCPU blocking. */ + if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) + return; + + __avic_vcpu_put(vcpu); } =20 void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu) @@ -973,9 +982,9 @@ void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) avic_refresh_virtual_apic_mode(vcpu); =20 if (activated) - avic_vcpu_load(vcpu, vcpu->cpu); + __avic_vcpu_load(vcpu, vcpu->cpu); else - avic_vcpu_put(vcpu); + __avic_vcpu_put(vcpu); =20 /* * Here, we go through the per-vcpu ir_list to update all existing --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-il1-f201.google.com (mail-il1-f201.google.com [209.85.166.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 2EF3027145C for ; Fri, 23 May 2025 01:01:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962110; cv=none; b=JgxuXrrTY0mIfQ7G1Ki8ZSqTCj4pmWgk4BmQfqYsXA9sx738lAPy0aB0LMuIQPYT4NFzhDfJID9GjLIZop8U/UXGnoCB0qLyeYLgV3aww9Tp/1KuMVzPI64BoVFn+K8dFBzh6ji9Rg41jgAdJSAicE2ZO3jG+jjV7yIR99CWhF4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962110; c=relaxed/simple; bh=cgx/76xcA3zklybCr4c/hJY0siLMm57qoWAoljueJzg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IZDa9PMLlsqldbsgxjC/j4aJ8CI5JO+NtebismU403k02W3DziGx+52XFDXiQaPakJ/vMUAvTusfDP+RjtvoJrBiRgbqU9THoIJzRjJxX+qTK+4ecQatx7kHQjI0s+ugPwok7DCTCbfSi8c7jtFVrHmdjBO+u/oMKnzkIZbtzNw= 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=qkt6KdnX; arc=none smtp.client-ip=209.85.166.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="qkt6KdnX" Received: by mail-il1-f201.google.com with SMTP id e9e14a558f8ab-3dc62c92d5cso61245655ab.0 for ; Thu, 22 May 2025 18:01:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962106; x=1748566906; 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=GOvRUDhf6WzZUhQYcj+S2GSmTkywb2uxqC7sAPGcWlY=; b=qkt6KdnXHJl/RZvfvOu+wT8fiteY2bdIDh87JbYbHNUtNzzoznJYGS99mQM7RCJBQH Z+GC6LMMg8BZNEgXwIDYFk/hdR05djv9LYnGUL5X5mV33ymPbCxdI6L1DVGvmUORdrQp w+Wtpx8vimTaI+if5C+xR9ceRe1rmHn3enVhleS2LQO+xtoT6chDSZLq9dy6uZPZik+5 oeNEvCIwLDWvUXoJslDJ4cfg9kaTAr1LihisQ9Z0Vi7GRRtxCcz4TgUBxMLh+rkViqq6 k84hhok+oQbSl3s54mCZYzUTQNcjYaMIpzClyv8AwqXk37Mj1beNfMjZcNwnUKJ0yfug KbAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962106; x=1748566906; 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=GOvRUDhf6WzZUhQYcj+S2GSmTkywb2uxqC7sAPGcWlY=; b=FAeMqnQ7i6B1VMObQmotk/S2cJfyyg0u0rMIlC26rM853srEsx1bjCnBzrPI+5mGq/ NniXpZQd05Aqddf/wswkClg1RWi7U+vZjW3A7reCFim3js+3t4Q/BvgqK5BQXcfus0oE sYQKtkSQwvU8MFxk7yIE9NgzU/9tc2poit764U2aYmn1lwhtsE0/OU/a2y6AU6dVaLfo nfmWC4dQBExTKXWzwC50ysknKEzYJcyVg/hWPFH/1uiQG/0h2fQAh4axN4Ny8SiBt++2 vE4Xgi+I04zDpfbznKmSQlHVjkn16NpGoNOfINcUdH2KHIogk2VgT8tCaRhwxOd4xkcQ CLqg== X-Forwarded-Encrypted: i=1; AJvYcCXusUyNT8j97MJqBysnPoXG9xviERrSRE7L1fC9BQjIHiSXv9Sfn0Ot/d7mQ5/a5ThTNru+M1Ujd5AnGLU=@vger.kernel.org X-Gm-Message-State: AOJu0YxOEsqrZDeBFUUo02StchTRr3IJkhVa45dKCX/7bsNvzLivJosW deUTaFOTcaf4XrUCY2hi9VGPEzzEG+PUeORQUaQauHmeg1xrJ0+RCYVjad1FjjcAbMFVO7KHT3Q cMfIqzA== X-Google-Smtp-Source: AGHT+IFDHkcBTdgDBcf9RfdN3IcCGgtfCXkYoLUg7YY7Kt3b/82DHTKb51w7MjiVGEMAMdbOkvSZTJ677AI= X-Received: from pjbsb7.prod.google.com ([2002:a17:90b:50c7:b0:30e:6bb2:6855]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6e02:743:b0:3dc:8c4e:2b8b with SMTP id e9e14a558f8ab-3dc8c4e2c10mr58843225ab.8.1747962106431; Thu, 22 May 2025 18:01:46 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 18:00:02 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-58-seanjc@google.com> Subject: [PATCH v2 57/59] KVM: SVM: Consolidate IRTE update when toggling AVIC on/off From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Fold the IRTE modification logic in avic_refresh_apicv_exec_ctrl() into __avic_vcpu_{load,put}(), and add a param to the helpers to communicate whether or not AVIC is being toggled, i.e. if IRTE needs a "full" update, or just a quick update to set the CPU and IsRun. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 85 ++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 1e6e5d1f6b4e..2e47559a4134 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -810,7 +810,28 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd= , struct kvm *kvm, return irq_set_vcpu_affinity(host_irq, NULL); } =20 -static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu) +enum avic_vcpu_action { + /* + * There is no need to differentiate between activate and deactivate, + * as KVM only refreshes AVIC state when the vCPU is scheduled in and + * isn't blocking, i.e. the pCPU must always be (in)valid when AVIC is + * being (de)activated. + */ + AVIC_TOGGLE_ON_OFF =3D BIT(0), + AVIC_ACTIVATE =3D AVIC_TOGGLE_ON_OFF, + AVIC_DEACTIVATE =3D AVIC_TOGGLE_ON_OFF, + + /* + * No unique action is required to deal with a vCPU that stops/starts + * running, as IRTEs are configured to generate GALog interrupts at all + * times. + */ + AVIC_START_RUNNING =3D 0, + AVIC_STOP_RUNNING =3D 0, +}; + +static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, + enum avic_vcpu_action action) { struct vcpu_svm *svm =3D to_svm(vcpu); struct kvm_kernel_irqfd *irqfd; @@ -824,11 +845,20 @@ static void avic_update_iommu_vcpu_affinity(struct kv= m_vcpu *vcpu, int cpu) if (list_empty(&svm->ir_list)) return; =20 - list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) - WARN_ON_ONCE(amd_iommu_update_ga(cpu, irqfd->irq_bypass_data)); + list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) { + void *data =3D irqfd->irq_bypass_data; + + if (!(action & AVIC_TOGGLE_ON_OFF)) + WARN_ON_ONCE(amd_iommu_update_ga(cpu, data)); + else if (cpu >=3D 0) + WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, cpu)); + else + WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data)); + } } =20 -static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) +static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu, + enum avic_vcpu_action action) { struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); int h_physical_id =3D kvm_cpu_get_apicid(cpu); @@ -873,7 +903,7 @@ static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int= cpu) =20 WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); =20 - avic_update_iommu_vcpu_affinity(vcpu, h_physical_id); + avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, action); =20 spin_unlock_irqrestore(&svm->ir_list_lock, flags); } @@ -890,10 +920,10 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) if (kvm_vcpu_is_blocking(vcpu)) return; =20 - __avic_vcpu_load(vcpu, cpu); + __avic_vcpu_load(vcpu, cpu, AVIC_START_RUNNING); } =20 -static void __avic_vcpu_put(struct kvm_vcpu *vcpu) +static void __avic_vcpu_put(struct kvm_vcpu *vcpu, enum avic_vcpu_action a= ction) { struct kvm_svm *kvm_svm =3D to_kvm_svm(vcpu->kvm); struct vcpu_svm *svm =3D to_svm(vcpu); @@ -915,7 +945,7 @@ static void __avic_vcpu_put(struct kvm_vcpu *vcpu) */ spin_lock_irqsave(&svm->ir_list_lock, flags); =20 - avic_update_iommu_vcpu_affinity(vcpu, -1); + avic_update_iommu_vcpu_affinity(vcpu, -1, action); =20 entry &=3D ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; svm->avic_physical_id_entry =3D entry; @@ -941,7 +971,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) return; =20 - __avic_vcpu_put(vcpu); + __avic_vcpu_put(vcpu, AVIC_STOP_RUNNING); } =20 void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu) @@ -970,41 +1000,18 @@ void avic_refresh_virtual_apic_mode(struct kvm_vcpu = *vcpu) =20 void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) { - bool activated =3D kvm_vcpu_apicv_active(vcpu); - int apic_id =3D kvm_cpu_get_apicid(vcpu->cpu); - struct vcpu_svm *svm =3D to_svm(vcpu); - struct kvm_kernel_irqfd *irqfd; - unsigned long flags; - if (!enable_apicv) return; =20 + /* APICv should only be toggled on/off while the vCPU is running. */ + WARN_ON_ONCE(kvm_vcpu_is_blocking(vcpu)); + avic_refresh_virtual_apic_mode(vcpu); =20 - if (activated) - __avic_vcpu_load(vcpu, vcpu->cpu); + if (kvm_vcpu_apicv_active(vcpu)) + __avic_vcpu_load(vcpu, vcpu->cpu, AVIC_ACTIVATE); else - __avic_vcpu_put(vcpu); - - /* - * Here, we go through the per-vcpu ir_list to update all existing - * interrupt remapping table entry targeting this vcpu. - */ - spin_lock_irqsave(&svm->ir_list_lock, flags); - - if (list_empty(&svm->ir_list)) - goto out; - - list_for_each_entry(irqfd, &svm->ir_list, vcpu_list) { - void *data =3D irqfd->irq_bypass_data; - - if (activated) - WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, apic_id)); - else - WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data)); - } -out: - spin_unlock_irqrestore(&svm->ir_list_lock, flags); + __avic_vcpu_put(vcpu, AVIC_DEACTIVATE); } =20 void avic_vcpu_blocking(struct kvm_vcpu *vcpu) @@ -1030,7 +1037,7 @@ void avic_vcpu_blocking(struct kvm_vcpu *vcpu) * CPU and cause noisy neighbor problems if the VM is sending interrupts * to the vCPU while it's scheduled out. */ - avic_vcpu_put(vcpu); + __avic_vcpu_put(vcpu, AVIC_STOP_RUNNING); } =20 void avic_vcpu_unblocking(struct kvm_vcpu *vcpu) --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4445C21FF2F for ; Fri, 23 May 2025 01:01:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962111; cv=none; b=O8HDAs1lYi78gQ8x1328/cm1CDiZe9n85i5Hcns197sU7yTvIo8z84T4ddCy7u/K1p8ZgRps9my1paV1kztapCsUlmyw/keqwlNx59nCMJ+CFov+VGMkjTxEqBn41ad06dvwR24R7eM4MVYld/yYHCVUz9e/LqQNuTInReHdHmI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962111; c=relaxed/simple; bh=hquM5XEaF1tV/UsD0O/H8Zlv63arDHb+DJY9dbNKYEo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mgPg/3B6kjWw4elPdCQktE6FDD4dbnyZCjCdFkij8jHhaMHnxfiRUEnKslDtWwTaIuCGuwDtPOe8BFmc7Ezsvv2zKJiSCs7ybydHbnKEbJ71iIio6HNendM15qZc+aatTNCdyvu7gT8Pmu4ivn6U03ZpabkUSi/Xs2KhKEtGyvM= 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=TgusC5RI; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TgusC5RI" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-231d7a20b31so69250755ad.0 for ; Thu, 22 May 2025 18:01:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962108; x=1748566908; 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=SdJtI6mzcHfEMtRnbz/7WQgOGD6qOvwTAePlcw8+5D8=; b=TgusC5RIthuGfbW6CDHfoQ8lEvwDgRo263cp8WX5bDdw2+JAbiEmudvcmQDguT4LSF u+4IWV3lCmptvL7KomcwAPWnhNtLyJDHW+dk47IfrVkVx9ISTOxiBtio+XofNSXQQPrD q7/YdyYNEffLDRLp/s63R2o0nu/jVSVzjPcSrIBFxWMw2kD6b/JjnxfwqraSoyUOgGKQ bXmxoGt0R+XXex9VB/i8607Wd+idW5ZKqydlXsYVAUkI9OUrrkhV/nqFDpPOuHN2+DOX gjkBLybEBp6QqheST+HVymwLYvcMQyAEbkDWPMOD3fVNeC07Q7Gy1Z9p5vZk81vEVyYB 5VMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962108; x=1748566908; 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=SdJtI6mzcHfEMtRnbz/7WQgOGD6qOvwTAePlcw8+5D8=; b=tzWneFQi3x9CldBEHakoaxcFgAdmLiEP3TY4GZROGOGnzQKRugF08GtgVRi98PCELF fikP0HQeNp/LVp947fC244SibfpgsVlR0bG1X7qCoBSezaLN82BVq+jfWJMfvs4BdNmx 9/aiVJa9Nob9eDSJ++3KcU9TUSFClVJDxfGpUNI0nQe0ydEhDs93VgcIYt3brrJkia2/ hN6uKTDRP6KMEdizjVHgwlZPTMx+r3Sb5EznjcjW+a4sI3fAGQKH1BwYFw+8PF0pCyfN tVXFF0hYEAHFY2Uzu9tdoSRPELYQRWyE51FBHkXq9v6zraQWdq8EcuTASMO/+aWWuOJc CcRQ== X-Forwarded-Encrypted: i=1; AJvYcCWxYFQL5Ksh+BpO5FWlCG8FLYMWchAF5pLXGdYhAmXMbIGWfcog26azt+XMjRvFDbd2E5x4WkHYPN7un4Q=@vger.kernel.org X-Gm-Message-State: AOJu0YxlK8CxOskatFt76Nwha4ENwoJg7jIfp+OExWxrVRbIu539bkoq 2uQ03kfZ7hqZPVZDFTt+jlMqWAZIgmwnowcX2wYmku7DBL88txOnlZc034zS0zTthUv5h3R7zcP pULmrQQ== X-Google-Smtp-Source: AGHT+IFroOYrJ+kcqTin7sT6kzOgrQyeQ3nstylTnZcNfGmu9/enZgKduOi8pFFg82qyNAlelMjm4Cirvic= X-Received: from pjl6.prod.google.com ([2002:a17:90b:2f86:b0:30a:7d22:be7b]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:289:b0:231:8030:74ab with SMTP id d9443c01a7336-233f25f3f59mr15398355ad.36.1747962108214; Thu, 22 May 2025 18:01:48 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 18:00:03 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-59-seanjc@google.com> Subject: [PATCH v2 58/59] iommu/amd: KVM: SVM: Allow KVM to control need for GA log interrupts From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add plumbing to the AMD IOMMU driver to allow KVM to control whether or not an IRTE is configured to generate GA log interrupts. KVM only needs a notification if the target vCPU is blocking, so the vCPU can be awakened. If a vCPU is preempted or exits to userspace, KVM clears is_run, but will set the vCPU back to running when userspace does KVM_RUN and/or the vCPU task is scheduled back in, i.e. KVM doesn't need a notification. Unconditionally pass "true" in all KVM paths to isolate the IOMMU changes from the KVM changes insofar as possible. Opportunistically swap the ordering of parameters for amd_iommu_update_ga() so that the match amd_iommu_activate_guest_mode(). Note, as of this writing, the AMD IOMMU manual doesn't list GALogIntr as a non-cached field, but per AMD hardware architects, it's not cached and can be safely updated without an invalidation. Link: https://lore.kernel.org/all/b29b8c22-2fd4-4b5e-b755-9198874157c7@amd.= com Cc: Vasant Hegde Cc: Joao Martins Signed-off-by: Sean Christopherson --- arch/x86/include/asm/irq_remapping.h | 1 + arch/x86/kvm/svm/avic.c | 10 ++++++---- drivers/iommu/amd/iommu.c | 28 +++++++++++++++++----------- include/linux/amd-iommu.h | 9 ++++----- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/arch/x86/include/asm/irq_remapping.h b/arch/x86/include/asm/ir= q_remapping.h index 4c75a17632f6..5a0d42464d44 100644 --- a/arch/x86/include/asm/irq_remapping.h +++ b/arch/x86/include/asm/irq_remapping.h @@ -36,6 +36,7 @@ struct amd_iommu_pi_data { u32 ga_tag; u32 vector; /* Guest vector of the interrupt */ int cpu; + bool ga_log_intr; bool is_guest_mode; void *ir_data; }; diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 2e47559a4134..e61ecc3514ea 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -784,10 +784,12 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqf= d, struct kvm *kvm, * is awakened and/or scheduled in. See also avic_vcpu_load(). */ entry =3D svm->avic_physical_id_entry; - if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) + if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) { pi_data.cpu =3D entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; - else + } else { pi_data.cpu =3D -1; + pi_data.ga_log_intr =3D true; + } =20 ret =3D irq_set_vcpu_affinity(host_irq, &pi_data); if (ret) @@ -849,9 +851,9 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_= vcpu *vcpu, int cpu, void *data =3D irqfd->irq_bypass_data; =20 if (!(action & AVIC_TOGGLE_ON_OFF)) - WARN_ON_ONCE(amd_iommu_update_ga(cpu, data)); + WARN_ON_ONCE(amd_iommu_update_ga(data, cpu, true)); else if (cpu >=3D 0) - WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, cpu)); + WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, cpu, true)); else WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data)); } diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 926dcdfe08c8..e79f583da36b 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3804,7 +3804,8 @@ static const struct irq_domain_ops amd_ir_domain_ops = =3D { .deactivate =3D irq_remapping_deactivate, }; =20 -static void __amd_iommu_update_ga(struct irte_ga *entry, int cpu) +static void __amd_iommu_update_ga(struct irte_ga *entry, int cpu, + bool ga_log_intr) { if (cpu >=3D 0) { entry->lo.fields_vapic.destination =3D @@ -3812,8 +3813,10 @@ static void __amd_iommu_update_ga(struct irte_ga *en= try, int cpu) entry->hi.fields.destination =3D APICID_TO_IRTE_DEST_HI(cpu); entry->lo.fields_vapic.is_run =3D true; + entry->lo.fields_vapic.ga_log_intr =3D false; } else { entry->lo.fields_vapic.is_run =3D false; + entry->lo.fields_vapic.ga_log_intr =3D ga_log_intr; } } =20 @@ -3822,16 +3825,19 @@ static void __amd_iommu_update_ga(struct irte_ga *e= ntry, int cpu) * a vCPU, without issuing an IOMMU invalidation for the IRTE. * * If the vCPU is associated with a pCPU (@cpu >=3D 0), configure the Dest= ination - * with the pCPU's APIC ID and set IsRun, else clear IsRun. I.e. treat vC= PUs - * that are associated with a pCPU as running. This API is intended to be= used - * when a vCPU is scheduled in/out (or stops running for any reason), to d= o a - * fast update of IsRun and (conditionally) Destination. + * with the pCPU's APIC ID, set IsRun, and clear GALogIntr. If the vCPU i= sn't + * associated with a pCPU (@cpu < 0), clear IsRun and set/clear GALogIntr = based + * on input from the caller (e.g. KVM only requests GALogIntr when the vCP= U is + * blocking and requires a notification wake event). I.e. treat vCPUs tha= t are + * associated with a pCPU as running. This API is intended to be used whe= n a + * vCPU is scheduled in/out (or stops running for any reason), to do a fast + * update of IsRun, GALogIntr, and (conditionally) Destination. * * Per the IOMMU spec, the Destination, IsRun, and GATag fields are not ca= ched * and thus don't require an invalidation to ensure the IOMMU consumes fre= sh * information. */ -int amd_iommu_update_ga(int cpu, void *data) +int amd_iommu_update_ga(void *data, int cpu, bool ga_log_intr) { struct amd_ir_data *ir_data =3D (struct amd_ir_data *)data; struct irte_ga *entry =3D (struct irte_ga *) ir_data->entry; @@ -3845,14 +3851,14 @@ int amd_iommu_update_ga(int cpu, void *data) if (!ir_data->iommu) return -ENODEV; =20 - __amd_iommu_update_ga(entry, cpu); + __amd_iommu_update_ga(entry, cpu, ga_log_intr); =20 return __modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, ir_data->irq_2_irte.index, entry); } EXPORT_SYMBOL(amd_iommu_update_ga); =20 -int amd_iommu_activate_guest_mode(void *data, int cpu) +int amd_iommu_activate_guest_mode(void *data, int cpu, bool ga_log_intr) { struct amd_ir_data *ir_data =3D (struct amd_ir_data *)data; struct irte_ga *entry =3D (struct irte_ga *) ir_data->entry; @@ -3871,12 +3877,11 @@ int amd_iommu_activate_guest_mode(void *data, int c= pu) =20 entry->lo.fields_vapic.valid =3D valid; entry->lo.fields_vapic.guest_mode =3D 1; - entry->lo.fields_vapic.ga_log_intr =3D 1; entry->hi.fields.ga_root_ptr =3D ir_data->ga_root_ptr; entry->hi.fields.vector =3D ir_data->ga_vector; entry->lo.fields_vapic.ga_tag =3D ir_data->ga_tag; =20 - __amd_iommu_update_ga(entry, cpu); + __amd_iommu_update_ga(entry, cpu, ga_log_intr); =20 return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, ir_data->irq_2_irte.index, entry); @@ -3947,7 +3952,8 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *= data, void *info) ir_data->ga_vector =3D pi_data->vector; ir_data->ga_tag =3D pi_data->ga_tag; if (pi_data->is_guest_mode) - ret =3D amd_iommu_activate_guest_mode(ir_data, pi_data->cpu); + ret =3D amd_iommu_activate_guest_mode(ir_data, pi_data->cpu, + pi_data->ga_log_intr); else ret =3D amd_iommu_deactivate_guest_mode(ir_data); } else { diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index c9f2df0c4596..8cced632ecd0 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -30,9 +30,8 @@ static inline void amd_iommu_detect(void) { } /* IOMMU AVIC Function */ extern int amd_iommu_register_ga_log_notifier(int (*notifier)(u32)); =20 -extern int amd_iommu_update_ga(int cpu, void *data); - -extern int amd_iommu_activate_guest_mode(void *data, int cpu); +extern int amd_iommu_update_ga(void *data, int cpu, bool ga_log_intr); +extern int amd_iommu_activate_guest_mode(void *data, int cpu, bool ga_log_= intr); extern int amd_iommu_deactivate_guest_mode(void *data); =20 #else /* defined(CONFIG_AMD_IOMMU) && defined(CONFIG_IRQ_REMAP) */ @@ -43,12 +42,12 @@ amd_iommu_register_ga_log_notifier(int (*notifier)(u32)) return 0; } =20 -static inline int amd_iommu_update_ga(int cpu, void *data) +static inline int amd_iommu_update_ga(void *data, int cpu, bool ga_log_int= r) { return 0; } =20 -static inline int amd_iommu_activate_guest_mode(void *data, int cpu) +static inline int amd_iommu_activate_guest_mode(void *data, int cpu, bool = ga_log_intr) { return 0; } --=20 2.49.0.1151.ga128411c76-goog From nobody Sun Dec 14 06:36:31 2025 Received: from mail-il1-f201.google.com (mail-il1-f201.google.com [209.85.166.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 653CF2E62A7 for ; Fri, 23 May 2025 01:01:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962113; cv=none; b=Wo73RJoaqpPzhwq2ZV3gTPHIwl2pU1osVJA4nayJCLGIDibloBpciakQXI8wx2vcxEn5UqTmbGeA2IOdOh8/icJSTTF9iwWly5QC4dUja6MTjwAk9qG8VX8LbbAyhrQwN+VBfqCOi9m2HJwh2AAsD3yov3bT4hyCNC+1u/0cHGA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747962113; c=relaxed/simple; bh=9KhTov6NigDBe98CfFfGcuC8nWRwinqRFBqUtK1H758=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iDoeVYRSWrrWi+RsbIcNXKV465KcQYWfhREY4+JPAATU6kdO5SkBVd2PGBdOdjnKboGibISwg07nYDgv90UEj3qS+GIEMGJRnvoyubEmJ1xPDnox6Yq9qzcI/d+Xgv5mSjIZH6H7V/HGybdDmiAqV8kO3SCQ1/F4lZ8Eb5sDd8c= 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=k6trBw/h; arc=none smtp.client-ip=209.85.166.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="k6trBw/h" Received: by mail-il1-f201.google.com with SMTP id e9e14a558f8ab-3dc62c92d5cso61246045ab.0 for ; Thu, 22 May 2025 18:01:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747962109; x=1748566909; 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=l2janC82c4esHYrjt7OIqyPO8MKzvHUx9JelC298Jlw=; b=k6trBw/hEMCMlw4Ji87RZRc5WITUyaquajlQ6mtVkuD+ZAtljAXXkaO9j176nnrZOn il/ptADyCObh2Wu1eJhBl1ioRb587cy/2GkZ6T2zKU24W7iFI66c/vzO5NuImlJvjc+S Wn184nidQHG5fVkxrFlHs2YQL31HOqDNbxWXW8Ig8bB5BrHQyFaNzRsjnE1HZaarufmc gbrqmZeVaCh+F1oJIqKSYrx6IuKk2sks+FBAmb7Zat/eqHzfragxVJI3ZhU5a78ZyyC5 cUuSuwabEI/hobv6sfL8K4vPvNa6NLHVwyHXWfwHhGWRAMHO0ouH6oTHL3WPX6nbGfNh /mdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747962109; x=1748566909; 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=l2janC82c4esHYrjt7OIqyPO8MKzvHUx9JelC298Jlw=; b=je+8pumXmaIEDQyGH+fSz4dnyF2AzXHzou5b5B6cESRLq0gKTeUOHESRlgkWErPPbx GPQWeCqt5pBms3yoB7KzgK4ZxFTR/BtI3THVA4oku5i2H/Mker4ds31DrsHjWRJgoj7b OWY7x+m4yxbqvTMsnbG44yVsYCmAyi3ivL1h5J0TSJ7TdCP65wDkHODX471b8I7CnCYR Q/jCzF9LqTpQuCGhMWv2BqcIdqCWQztQ5a/5njsDJx7c1oVja1VF8qcQWkQ8j0VT7jix 9qLxHH2hB1ZeU7FfoxU61m3N+16rezdUBc2XzpD0W+a0jjgGxDzK+3C1SNoZchOpGAdz DutQ== X-Forwarded-Encrypted: i=1; AJvYcCXtScPASZtqjS62iyg7cX0tiNEKkD+OqGUExf5/GbsVEEY/9FYTgSXNVWJjdj8PR3d8TK5xear2873DDeo=@vger.kernel.org X-Gm-Message-State: AOJu0YzAEzZ0RBRXMNhq25XviHr7tpIqct3UbFNeOagx9ssn4xEgZxU1 aapN7tygrc3QcElZN3DwL5v2zsVLHZ3+78L0qGgEhsyInzIZUG6TEOyeCs5OT8PW6jnREZzSSjS 1fQhgXw== X-Google-Smtp-Source: AGHT+IG7+QFXMaCOZGXhcLvVhEnNKLpzraqjuMbUJdLp1hOAQeSUFqqURcAYgABbsxGykvV+6E7RvJlYmzE= X-Received: from pji12.prod.google.com ([2002:a17:90b:3fcc:b0:2f5:63a:4513]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6e02:743:b0:3dc:8c4e:2b8b with SMTP id e9e14a558f8ab-3dc8c4e2c10mr58846455ab.8.1747962109625; Thu, 22 May 2025 18:01:49 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 22 May 2025 18:00:04 -0700 In-Reply-To: <20250523010004.3240643-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250523010004.3240643-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1151.ga128411c76-goog Message-ID: <20250523010004.3240643-60-seanjc@google.com> Subject: [PATCH v2 59/59] KVM: SVM: Generate GA log IRQs only if the associated vCPUs is blocking From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Joerg Roedel , David Woodhouse , Lu Baolu Cc: kvm@vger.kernel.org, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Sairaj Kodilkar , Vasant Hegde , Maxim Levitsky , Joao Martins , Francesco Lavra , David Matlack Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Configure IRTEs to GA log interrupts for device posted IRQs that hit non-running vCPUs if and only if the target vCPU is blocking, i.e. actually needs a wake event. If the vCPU has exited to userspace or was preempted, generating GA log entries and interrupts is wasteful and unnecessary, as the vCPU will be re-loaded and/or scheduled back in irrespective of the GA log notification (avic_ga_log_notifier() is just a fancy wrapper for kvm_vcpu_wake_up()). Use a should-be-zero bit in the vCPU's Physical APIC ID Table Entry to track whether or not the vCPU's associated IRTEs are configured to generate GA logs, but only set the synthetic bit in KVM's "cache", i.e. never set the should-be-zero bit in tables that are used by hardware. Use a synthetic bit instead of a dedicated boolean to minimize the odds of messing up the locking, i.e. so that all the existing rules that apply to avic_physical_id_entry for IS_RUNNING are reused verbatim for GA_LOG_INTR. Note, because KVM (by design) "puts" AVIC state in a "pre-blocking" phase, using kvm_vcpu_is_blocking() to track the need for notifications isn't a viable option. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/svm.h | 7 +++++ arch/x86/kvm/svm/avic.c | 63 ++++++++++++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 36f67c69ea66..ffc27f676243 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -252,6 +252,13 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define AVIC_LOGICAL_ID_ENTRY_VALID_BIT 31 #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31) =20 +/* + * GA_LOG_INTR is a synthetic flag that's never propagated to hardware-vis= ible + * tables. GA_LOG_INTR is set if the vCPU needs device posted IRQs to gen= erate + * GA log interrupts to wake the vCPU (because it's blocking or about to b= lock). + */ +#define AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR BIT_ULL(61) + #define AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK GENMASK_ULL(11, 0) #define AVIC_PHYSICAL_ID_ENTRY_BACKING_PAGE_MASK GENMASK_ULL(51, 12) #define AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK (1ULL << 62) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index e61ecc3514ea..e4e1d169577f 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -788,7 +788,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd,= struct kvm *kvm, pi_data.cpu =3D entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; } else { pi_data.cpu =3D -1; - pi_data.ga_log_intr =3D true; + pi_data.ga_log_intr =3D entry & AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR; } =20 ret =3D irq_set_vcpu_affinity(host_irq, &pi_data); @@ -825,16 +825,25 @@ enum avic_vcpu_action { =20 /* * No unique action is required to deal with a vCPU that stops/starts - * running, as IRTEs are configured to generate GALog interrupts at all - * times. + * running. A vCPU that starts running by definition stops blocking as + * well, and a vCPU that stops running can't have been blocking, i.e. + * doesn't need to toggle GALogIntr. */ AVIC_START_RUNNING =3D 0, AVIC_STOP_RUNNING =3D 0, + + /* + * When a vCPU starts blocking, KVM needs to set the GALogIntr flag + * int all associated IRTEs so that KVM can wake the vCPU if an IRQ is + * sent to the vCPU. + */ + AVIC_START_BLOCKING =3D BIT(1), }; =20 static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, enum avic_vcpu_action action) { + bool ga_log_intr =3D (action & AVIC_START_BLOCKING); struct vcpu_svm *svm =3D to_svm(vcpu); struct kvm_kernel_irqfd *irqfd; =20 @@ -851,9 +860,9 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_= vcpu *vcpu, int cpu, void *data =3D irqfd->irq_bypass_data; =20 if (!(action & AVIC_TOGGLE_ON_OFF)) - WARN_ON_ONCE(amd_iommu_update_ga(data, cpu, true)); + WARN_ON_ONCE(amd_iommu_update_ga(data, cpu, ga_log_intr)); else if (cpu >=3D 0) - WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, cpu, true)); + WARN_ON_ONCE(amd_iommu_activate_guest_mode(data, cpu, ga_log_intr)); else WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(data)); } @@ -888,7 +897,8 @@ static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int= cpu, entry =3D 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 ~(AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK | + AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR); entry |=3D (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); entry |=3D AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; =20 @@ -949,12 +959,26 @@ static void __avic_vcpu_put(struct kvm_vcpu *vcpu, en= um avic_vcpu_action action) =20 avic_update_iommu_vcpu_affinity(vcpu, -1, action); =20 + WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR); + + /* + * Keep the previous APIC ID in the entry so that a rogue doorbell from + * hardware is at least restricted to a CPU associated with the vCPU. + */ entry &=3D ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; - svm->avic_physical_id_entry =3D entry; =20 if (enable_ipiv) WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); =20 + /* + * Note! Don't set AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR in the table as + * it's a synthetic flag that usurps an unused should-be-zero bit. + */ + if (action & AVIC_START_BLOCKING) + entry |=3D AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR; + + svm->avic_physical_id_entry =3D entry; + spin_unlock_irqrestore(&svm->ir_list_lock, flags); } =20 @@ -969,11 +993,26 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) */ u64 entry =3D to_svm(vcpu)->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)) - return; + /* + * Nothing to do if IsRunning =3D=3D '0' due to vCPU blocking, i.e. if the + * vCPU is preempted while its in the process of blocking. WARN if the + * vCPU wasn't running and isn't blocking, KVM shouldn't attempt to put + * the AVIC if it wasn't previously loaded. + */ + if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) { + if (WARN_ON_ONCE(!kvm_vcpu_is_blocking(vcpu))) + return; =20 - __avic_vcpu_put(vcpu, AVIC_STOP_RUNNING); + /* + * The vCPU was preempted while blocking, ensure its IRTEs are + * configured to generate GA Log Interrupts. + */ + if (!(WARN_ON_ONCE(!(entry & AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR)))) + return; + } + + __avic_vcpu_put(vcpu, kvm_vcpu_is_blocking(vcpu) ? AVIC_START_BLOCKING : + AVIC_STOP_RUNNING); } =20 void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu) @@ -1039,7 +1078,7 @@ void avic_vcpu_blocking(struct kvm_vcpu *vcpu) * CPU and cause noisy neighbor problems if the VM is sending interrupts * to the vCPU while it's scheduled out. */ - __avic_vcpu_put(vcpu, AVIC_STOP_RUNNING); + __avic_vcpu_put(vcpu, AVIC_START_BLOCKING); } =20 void avic_vcpu_unblocking(struct kvm_vcpu *vcpu) --=20 2.49.0.1151.ga128411c76-goog