From patchwork Fri Apr 4 19:38:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038823 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 A2F4A21D581 for ; Fri, 4 Apr 2025 19:39:49 +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=1743795591; cv=none; b=JfUJ/s7Y8S0QkZLdGWXVlnUTkGO6Eisi0wGgqKQS7l8V/mZpv1yHln1opvGsM2t3bbwKYcEuEmyibJnKt+3MPiTnB+VI/p4kagVAgfFfEcDcqiAtOGVdp1cPh5ggKW+lnSnjtT1SPRQzX7XO7ffdNXNDv+dKFtDcDlF+5aW6qzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795591; c=relaxed/simple; bh=IYgl0oz7WYCethX5zdLJOxQN9YYu60K0fmwKqhu1/aQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YoY+2gvIU9Xg6n5hg5g4r9tpvyVzXjEmMkBtfO0FakF9MWoLwm1c9crK9dMxByms9vUgei+lSmsalLk37PqyZQhWo4r02tfGK8MJzxEdQUJT12OhlS38thDW2naBYt5laUpkELWM+OL1NSFDBLUmIuSDwdfySCBlSo+jzsmXGT8= 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=FuMYdILA; 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="FuMYdILA" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736c89461d1so3576235b3a.3 for ; Fri, 04 Apr 2025 12:39:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795589; x=1744400389; 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=G03Ks9vMONlbd+61V/+Erd91vdrZg3/ZspQO1qdkUD0=; b=FuMYdILAQilAMy02eAsO0PfVk2TasLFUoe22iCIiS+0GnjlmAefswNlJ8OFivJ4Ymn IiviNgEl/pQ8VjiQklY2Fc5R3MiIUrd2lCEf1/CdsTlVfGdrR8aeGml1P0HVCN+9AdS7 s0MefyLD9B7HwTZJHL8aO4mfifsWDP8bS6GoP5Gy5c3dVQEPpDJMZjc9UeZ7lHI1+zWe rTVUgtMwvL2wxOjJFRXGBC1tG6TJeUhi6XpTwDs1d08VyF0cLhePXqO2I6uEGNU4AeJm iK0JAaBTs4AibCDCnAVN1wV0pDVA/MpTofb/QfiKm3BTX1LEAWWLW6/UneNsvGzB0kgR wDZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795589; x=1744400389; 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=G03Ks9vMONlbd+61V/+Erd91vdrZg3/ZspQO1qdkUD0=; b=kI5gMpu15Mdb/8aNYSD95GNHxLv7wuLWerPMQj4Q7tmyayXk3Iplai4qWT6rPqa3xV jnFM/nqS3XWtuT9Kk1Wg06bnSwMSCZbWNOb2647K0/dMjjzigcX3HeaYR49HvtE0sm+W LZwo9SEXoHxeqiGqNwxBBHdeANcZ+PO8huOEDI8hJPusEoTacoW7MPk8LsSHbP5vG2AM 8n5xiD2ntEjCYZeQB3FYKo0P+h3kELqgsJ5kerYQQ5stZZ/LgiH9Z10xs5HJXrSEyKVF aw4PyRPa9l9hxyl6vbHgzImVj1sT+5W3FpgC/af1iEiVazD9Fio3kZKq2auvJBR9zzZm 3LHQ== X-Gm-Message-State: AOJu0YwbrO69ALwNJFrs+jwP7E8jUfTjdGOUA20dOmYv3h8BqPpj4Yho 76ir0lZd0dMZbIdlGxa8thMdQxxpyRAJqqVryaFw9BJdeuJ20GbyXj6sPvVgTiQXJYe0phLFXB1 qTA== X-Google-Smtp-Source: AGHT+IEW58Ax+7aXM/+czWYm3zsW3qa9UnEokKAJbXbZCPS8kr/NGoPhwa56A0X88mWfGU8rBiSmwQt9o2Q= X-Received: from pfbhj4.prod.google.com ([2002:a05:6a00:8704:b0:730:7c5b:2e2b]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1411:b0:736:ff65:3fd0 with SMTP id d2e1a72fcca58-739e7113667mr5072766b3a.16.1743795588965; Fri, 04 Apr 2025 12:39:48 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:16 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-2-seanjc@google.com> Subject: [PATCH 01/67] KVM: SVM: Allocate IR data using atomic allocation 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, Maxim Levitsky , Joao Martins , David Matlack Allocate SVM's interrupt remapping metadata using GFP_ATOMIC as svm_ir_list_add() is called with IRQs are disabled and irqfs.lock held when kvm_irq_routing_update() reacts to GSI routing changes. Fixes: 411b44ba80ab ("svm: Implements update_pi_irte hook to setup posted interrupt") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 901d8d2dc169..a961e6e67050 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -820,7 +820,7 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) * Allocating new amd_iommu_pi_data, which will get * add to the per-vcpu ir_list. */ - ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_KERNEL_ACCOUNT); + ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_ATOMIC | __GFP_ACCOUNT); if (!ir) { ret = -ENOMEM; goto out; From patchwork Fri Apr 4 19:38:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038824 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 1826A21E091 for ; Fri, 4 Apr 2025 19:39:50 +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=1743795592; cv=none; b=WvCncUrxO7/TcQMXN1o08WpsaU7+wl5zeKWERMZDtDJ2ch+81SRgPl000mywuEN2DZKfwqH9V0xSiZeyCig4oH21D78TZ7g6t86P+7ruATux2vZOy9+rqCwMoDKhkCkFaHulxfY1o5qVeF9BAXawTgN/DxBLoWedyPAY0oZYHg8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795592; c=relaxed/simple; bh=4f35MFyB5Z66tYnl+4xh20T32heL9hmqCIpxVPqs8rM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oqPN8LqKHZumez3GxME37ZY+aSSiq26UJ7ZBSg8oBKIWIr6zQkKVTlaYgUugu2Z0hiAE5dQjoDVi1x3f0h2ExmItRcMrRmTIn8Td+ffm4ijf0Vpq6BGuQf5uaJsJPIjouXx8hYF6YcJg2oP2jbtQ/AHQUoh75DKKGgXCDUJiF88= 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=Uh88uJht; 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="Uh88uJht" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736c89461d1so3576252b3a.3 for ; Fri, 04 Apr 2025 12:39:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795590; x=1744400390; 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=HBQQg5RBDKCscBapgjJpO3Swz5o+MtJx/38zhYtsnTI=; b=Uh88uJhtM6Rk9le3L499gt4RHqSvoqNQDnJUoDFcnnmL/cXnYG6+slpA8ssKKhFZGK 7zd6v9XjF8ryaA49TDaL6JkC/D1cvPBN0wyzs6Qu1UmTLBkLcdImpUCAyid+NI+FOp2m xqBvEPnR3poYqx0odl3/mRzcvkaDtVHZzd+4OcrkLQnOFARKDWebcX8iWzDoYg7zOHIF qanaFM8GeODeAx25c6SPwPvB4Gu09SDeOTjGixgE4ct5Fl7QD7wwGKTNnsgh0LY/mrY7 5OkURNW0MM2N359MJ8ChKSUAvwP7ENFVi0C25x1unzoENGwzyDsoO5nAKnyRn5bEqmoT WG6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795590; x=1744400390; 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=HBQQg5RBDKCscBapgjJpO3Swz5o+MtJx/38zhYtsnTI=; b=R5ktvdjLfvasiaTe9KAKHIHM2enqfoCgm0zjMZiUpinEYw7WgotB//LaMwTGkhz5pf Mbvtaj6izC+DirPGg49s3cWGrErfrNcoAlD/o6n2MlASE+WIgMaf7HnHQslN//77RGGD WaSunFzG0VLuMEN5fxmHF3msccmq0qHkpfDn4su0gEJMnOSJMa0G2Q/imQmYrywO1XWg KySBIPiCv2J2NyeiykGVO2N/KPbXuKFiwAb4VyaIJUXqCYNWPeQTX+0HRq5qnGmzKz0y tUsHJcUFA63EisJ9LUcYcoptboZCW1nIjsuEEczXeFYqW2j+iT2bHdNyLIoWzKSzXBNa aPdg== X-Gm-Message-State: AOJu0YzyD3UFFixDrKvJCIutQMaTKwabv+iTdANc2fLg0FJe13ov8sI/ MtQ+1Ohk0RXt47qnJMucSMGi/hWLrsn1UGsrSFxuBr3Bo5sHjyfBotPJLxlxuCCV1Ym/4iwvOx1 IiA== X-Google-Smtp-Source: AGHT+IFD1risaw3iIrumvs5iCdJKlGSJfuM1C6nGfE4JDgOTzHXCStW3upmYfMaMHXW5LZH9425dtGtKXgg= X-Received: from pfbjc20.prod.google.com ([2002:a05:6a00:6c94:b0:736:451f:b9f4]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:230d:b0:736:35d4:f03f with SMTP id d2e1a72fcca58-739e6ff6b7fmr4703818b3a.6.1743795590459; Fri, 04 Apr 2025 12:39:50 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:17 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-3-seanjc@google.com> Subject: [PATCH 02/67] KVM: x86: Reset IRTE to host control if *new* route isn't postable 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, Maxim Levitsky , Joao Martins , David Matlack Restore an IRTE back to host control (remapped or posted MSI mode) if the *new* GSI route prevents posting the IRQ directly to a vCPU, regardless of the GSI routing type. Updating the IRTE if and only if the new GSI is an MSI results in KVM leaving an IRTE posting to a vCPU. The dangling IRTE can result in interrupts being incorrectly delivered to the guest, and in the worst case scenario can result in use-after-free, e.g. if the VM is torn down, but the underlying host IRQ isn't freed. Fixes: efc644048ecd ("KVM: x86: Update IRTE for posted-interrupts") Fixes: 411b44ba80ab ("svm: Implements update_pi_irte hook to setup posted interrupt") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 61 ++++++++++++++++++---------------- arch/x86/kvm/vmx/posted_intr.c | 28 ++++++---------- 2 files changed, 43 insertions(+), 46 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index a961e6e67050..ef08356fdb1c 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -896,6 +896,7 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq, { struct kvm_kernel_irq_routing_entry *e; struct kvm_irq_routing_table *irq_rt; + bool enable_remapped_mode = true; int idx, ret = 0; if (!kvm_arch_has_assigned_device(kvm) || !kvm_arch_has_irq_bypass()) @@ -932,6 +933,8 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq, kvm_vcpu_apicv_active(&svm->vcpu)) { struct amd_iommu_pi_data pi; + enable_remapped_mode = false; + /* Try to enable guest_mode in IRTE */ pi.base = __sme_set(page_to_phys(svm->avic_backing_page) & AVIC_HPA_MASK); @@ -950,33 +953,6 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq, */ if (!ret && pi.is_guest_mode) svm_ir_list_add(svm, &pi); - } else { - /* 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. - * - Retrieve ga_tag of prior interrupt remapping data. - */ - pi.prev_ga_tag = 0; - pi.is_guest_mode = false; - ret = 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 = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag); - struct kvm_vcpu *vcpu; - - vcpu = kvm_get_vcpu_by_id(kvm, id); - if (vcpu) - svm_ir_list_del(to_svm(vcpu), &pi); - } } if (!ret && svm) { @@ -991,7 +967,36 @@ int avic_pi_update_irte(struct kvm *kvm, unsigned int host_irq, } } - ret = 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. + * - Retrieve ga_tag of prior interrupt remapping data. + */ + pi.prev_ga_tag = 0; + pi.is_guest_mode = false; + ret = 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 = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag); + struct kvm_vcpu *vcpu; + + vcpu = kvm_get_vcpu_by_id(kvm, id); + if (vcpu) + svm_ir_list_del(to_svm(vcpu), &pi); + } + } else { + ret = 0; + } out: srcu_read_unlock(&kvm->irq_srcu, idx); return ret; diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index 16121d29dfd9..78ba3d638fe8 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -273,6 +273,7 @@ int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, { struct kvm_kernel_irq_routing_entry *e; struct kvm_irq_routing_table *irq_rt; + bool enable_remapped_mode = true; struct kvm_lapic_irq irq; struct kvm_vcpu *vcpu; struct vcpu_data vcpu_info; @@ -311,21 +312,8 @@ int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, kvm_set_msi_irq(kvm, e, &irq); if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) || - !kvm_irq_is_postable(&irq)) { - /* - * Make sure the IRTE is in remapped mode if - * we don't handle it in posted mode. - */ - ret = irq_set_vcpu_affinity(host_irq, NULL); - if (ret < 0) { - printk(KERN_INFO - "failed to back to remapped mode, irq: %u\n", - host_irq); - goto out; - } - + !kvm_irq_is_postable(&irq)) continue; - } vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); vcpu_info.vector = irq.vector; @@ -333,11 +321,12 @@ int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, vcpu_info.vector, vcpu_info.pi_desc_addr, set); - if (set) - ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); - else - ret = irq_set_vcpu_affinity(host_irq, NULL); + if (!set) + continue; + enable_remapped_mode = false; + + ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); if (ret < 0) { printk(KERN_INFO "%s: failed to update PI IRTE\n", __func__); @@ -345,6 +334,9 @@ int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, } } + if (enable_remapped_mode) + ret = irq_set_vcpu_affinity(host_irq, NULL); + ret = 0; out: srcu_read_unlock(&kvm->irq_srcu, idx); From patchwork Fri Apr 4 19:38:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038825 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 F00B5221542 for ; Fri, 4 Apr 2025 19:39:52 +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=1743795594; cv=none; b=jMa+PXL9W86OYa49sgkd/WMl5Kn57cjKQVq2+3bEgwAtqYBHFiEn0v0aIoZDNjVbKYjPYGi2ratMoJ8vFGY+OhqCD9j0uVR2+IpYfHJOWr4Wah1h+p6zDkcbjqnyir8RTb9DgmRZ+fURrpFyVDwUQ8jC1J56irdf3FBX78uHrq8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795594; c=relaxed/simple; bh=PLM9Oe14dNa7pkRo4U5ot9bvtj2ZhI28uVdi+hfPu3s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ie819QVswa48tfD4C5Ua97Ax0xKa/ehM2Xuwi9Zp5Ep2AViO0/jNW+skLVkCLmE96Lp3lERpLgQkkBUJmTYR518DhmGJeDSoxMaA5eHRwNmkHaGxs9UvGKf/wMyXPEjwhzsLwfgyO2M0PWaZH5BTtqhGaoZDlRcgp4K4omofqYU= 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=HXkQ8Wus; 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="HXkQ8Wus" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736a7d0b82fso2991469b3a.1 for ; Fri, 04 Apr 2025 12:39:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795592; x=1744400392; 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=+47iTNmbRmU5QNEmm30tIBdVVPpOMLVIBC3EbiK88yI=; b=HXkQ8WusVtNyqJRuw9YftPjh9IbqYPb5v+Lp+h9daVRcLLpmjULUtStdDtFP5EZ6Hj dHM2BTEJV8j/+OqESK3SVqcyD6bDstSXUQaYVSJ0jdwdyYNG1FTeU/U+Ct2/z9SrqMGW d+xchWymjdErk02Li73i+6RQ/xCzCDYNDpqgkvhPGxA5Nl70Ks3490lGmhRT4yLKK4QR citoaVrw2LqsOkyNgM2mVJOGu9OHq0jD2nOh/JlScWdIRD9Qg0flzfQTXRhSYdxVoJ5a k31JSsiLhMZjn5hnXVZEcP+Nan3zu1P1IKhdEtT4JJrwsF9Fg+Qo7OEgQDlKR1ZocP85 9A4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795592; x=1744400392; 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=+47iTNmbRmU5QNEmm30tIBdVVPpOMLVIBC3EbiK88yI=; b=MkN7gGKRYhyFMSWllrJDFMZMDnKlpSLBRsXqMLV/P10+HG6ka8D0FPyk2QK+d5MW8W PMQBgGxJGkyyROKF3uQV9gVQHKIIQ2vyLOtW3Kdbmh1jyS9zCupT0IaL3o6oL5d01UyP oUUAqGQ9mpdhy6ioJYi0Gt0G+s+EumWknYONmUZl1+cPKOXxgZIzfKPO7Qhy3gOocNbU 3LQKy9yxOMAr9gSX8Rakm8pdDu0tRF+PPSMiI8VhD5EPxOjKbe8rUvXc8uJbDe9nHcxT M78SWxV+u3rd0JJufqllUJkwLzeBewhZvBbSN9hKAIBGuyL1qPDlJihU15t63BTBwdlL dkHA== X-Gm-Message-State: AOJu0YyouFHsYUIe3EXOszMp+Ix/Ue5Npgp9U4LnxfBjow+6vnFaIE5P KeB6TNW3QzAFnA4BJxodo8ZVEot0vRsaWu0NdNF9843IVZvb5hfpBlYplo9bJ/Te9+ZUvVTn8/O bRg== X-Google-Smtp-Source: AGHT+IHIboGmLY9qchPfpX/OHkXoE4myUPKEgmdV2feQwjMagq50m8siYeTr/9yoolpfmy2JLnd8BDCKG1I= X-Received: from pfbln13.prod.google.com ([2002:a05:6a00:3ccd:b0:732:20df:303c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1897:b0:730:95a6:3761 with SMTP id d2e1a72fcca58-739e6fcbcebmr6002912b3a.3.1743795592333; Fri, 04 Apr 2025 12:39:52 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:18 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-4-seanjc@google.com> Subject: [PATCH 03/67] KVM: x86: Explicitly treat routing entry type changes as changes 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, Maxim Levitsky , Joao Martins , David Matlack Explicitly treat type differences as GSI routing changes, as comparing MSI data between two entries could get a false negative, e.g. if userspace changed the type but left the type-specific data as-is. Fixes: 515a0c79e796 ("kvm: irqfd: avoid update unmodified entries of the routing") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 9211344b20ae..f94f1217a087 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13615,7 +13615,8 @@ int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq, bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *old, struct kvm_kernel_irq_routing_entry *new) { - if (new->type != KVM_IRQ_ROUTING_MSI) + if (old->type != KVM_IRQ_ROUTING_MSI || + new->type != KVM_IRQ_ROUTING_MSI) return true; return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); From patchwork Fri Apr 4 19:38:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038826 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 DF53922171D for ; Fri, 4 Apr 2025 19:39:54 +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=1743795596; cv=none; b=lW/sf9LF2IQWLo8T0e8RTYtPNm44HKkW5F0FP62o/gd58Vn/62yVcGelCc+wQVKdxuFGwK2ZZQ3Yf/yX9MXk0/VU/BSQZx13Ti3BhVjHGL7uq6hvuWOdjZnEL+/yPd6VDQhDB1HkA5X3ua6wfvk/jPwgarA3D6OG+sQTvFP0ErM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795596; c=relaxed/simple; bh=zvROXmjtL1IIWkXeJFgdhMrAr1KnWI1Z/K2ADvFL0ug=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TBLVgApWn7NIpYUlaMCCvl1w1f69Qk2zz9Fxe+lhd9HvVt4I/waUTosOqmAzReBzKIE2a5Y1ZzSrV2Sk6Cfir8z4qJRuLgUp3cCvIFh96RnVh7WtPT+6RvX6QTBywLtIeV240qcG/1Fo2XbD4rRV+W+97B7xmqvviQJCuSzQxVY= 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=axIjS05k; 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="axIjS05k" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736d64c5e16so2096865b3a.3 for ; Fri, 04 Apr 2025 12:39:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795594; x=1744400394; 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=RGxOMNtpRYtvYPUfMG5TmBHR1pFGxtORoL82B7xlqeE=; b=axIjS05k0wlvUfolX7qcCU9p6AiPh/ykag8O7FX5ExQy2znj+Xqtt9ME/buLrKrVTr kRpKpLx46gQdCIR2D+RI9nfESn5bDTl4RFaYlsn/tn43pRI4qrXD+pBlwCZREsEyzNt1 vGZLPqRx+NyynVKeN67l2Mcl9SNtSCUKT09tGzx2T06zcTpnYLDZIg4b2gBX+nV7GLS/ E89tyA3i60KKk9s2EyFbSA4kUL6rf0bCFBzLm8Yx2j81aGuHxrs08ZaanW7vofwLzc9d KiGbYjNeGoxM0bu7YwQbWhBIoB27IX5tTQPwv3vqKpaAIY05S2iaRhCuWmt0FDDqlCAc ZUQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795594; x=1744400394; 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=RGxOMNtpRYtvYPUfMG5TmBHR1pFGxtORoL82B7xlqeE=; b=IdQ60Czsv8MS4dsTgWmskOonquzA7/kPy2x7o4zrH4NdfBnn0SUflQ8Npz5XLmGT+Q dGdwKQwL74zhlw7J92Posj8Ow+MsbvHn1TwWsSMOpn5SpYdP1ehDoh/fpNr2SIvhri6P mGawoAqszzU7vazgYMiNUYfuU3sUSLooWaLp+CRI/+U0b6Jps2jmXAYXKS0bZVn10Edj Ey25yBFj6UPgxKHYtJOKn1ttlqZ+ljW53Q4X1rDDxk0I9Jz62ilHjDfGVVzFZjZdjGUp VMsdEM1N9BZoJSSsgIqNldpxs73KDdoKIjnSEtvW3GoXlLPEabFenLeJPRIvA/8OWLzc Cy1w== X-Gm-Message-State: AOJu0YyGvXxDoEUF7cvX1HRF0yc2QZ69s9FerzPSM1borQ8g0Stmx4NE BRTog996ciqF9eJF2loNGbVAEVJjbbV/nc88rk22l04Cg1Esdyp0EE3YXC/QYnwybTeu5wYv7MV xWQ== X-Google-Smtp-Source: AGHT+IEQIRfv5MQyN5FPXlDH0jiZdTHciPaHWXw+Tn7fs2rQ/C2nsY1CH5202uPXIoXMn1X9vHNvV8pVCeI= X-Received: from pfbfa9.prod.google.com ([2002:a05:6a00:2d09:b0:739:8cd6:c16c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2442:b0:736:6ac4:d204 with SMTP id d2e1a72fcca58-739e4855dd7mr5053725b3a.11.1743795594255; Fri, 04 Apr 2025 12:39:54 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:19 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-5-seanjc@google.com> Subject: [PATCH 04/67] KVM: x86: Take irqfds.lock when adding/deleting IRQ bypass producer 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, Maxim Levitsky , Joao Martins , David Matlack Take irqfds.lock when adding/deleting an IRQ bypass producer to ensure irqfd->producer isn't modified while kvm_irq_routing_update() is running. The only lock held when a producer is added/removed is irqbypass's mutex. Fixes: 872768800652 ("KVM: x86: select IRQ_BYPASS_MANAGER") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f94f1217a087..dcc173852dc5 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13569,15 +13569,22 @@ int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, { struct kvm_kernel_irqfd *irqfd = container_of(cons, struct kvm_kernel_irqfd, consumer); + struct kvm *kvm = irqfd->kvm; int ret; - irqfd->producer = prod; kvm_arch_start_assignment(irqfd->kvm); + + spin_lock_irq(&kvm->irqfds.lock); + irqfd->producer = prod; + ret = kvm_x86_call(pi_update_irte)(irqfd->kvm, prod->irq, irqfd->gsi, 1); if (ret) kvm_arch_end_assignment(irqfd->kvm); + spin_unlock_irq(&kvm->irqfds.lock); + + return ret; } @@ -13587,9 +13594,9 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, int ret; struct kvm_kernel_irqfd *irqfd = container_of(cons, struct kvm_kernel_irqfd, consumer); + struct kvm *kvm = irqfd->kvm; WARN_ON(irqfd->producer != prod); - irqfd->producer = NULL; /* * When producer of consumer is unregistered, we change back to @@ -13597,12 +13604,18 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, * 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 = NULL; + ret = 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); + spin_unlock_irq(&kvm->irqfds.lock); + + kvm_arch_end_assignment(irqfd->kvm); } From patchwork Fri Apr 4 19:38:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038827 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 A9E6B221D90 for ; Fri, 4 Apr 2025 19:39: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=1743795598; cv=none; b=lVpiNIK+QaOyG3YrpOGRSoRwFtlUt0F/ld2EWROVmvGVk++r7jBwXwlXbodHBN3Jc90AhjrL7JYkFdU5XJCbeaSJ+ntdcOxnngoJI62MvlPJivriFfictyLoE00EK3hkzPhqbomvz8oKGQYE+QagMU+JsRRxXX8K/BW7h+hR+8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795598; c=relaxed/simple; bh=El/LUiSqQ2qSjpNZ7D93xQ4xLdhReczWf1b9uZIPSLc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Fmr7cVdRpxSNqt6NU1r29eXUYsTj6gVdrvEEZNf4cK7ILO26oN2LV7b8VdxhfaqTagmddPrCkPDJkCJTY4l14OHEPhEu8xSSq7DKXE7gwxVxMBU24PgUoJfqj75Zs40P3nqKY1g1pIrbsqRRccKCBNAlQU5SZY8rOaa333HhdJw= 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=pGmF37eA; 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="pGmF37eA" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-73009f59215so2923265b3a.1 for ; Fri, 04 Apr 2025 12:39:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795596; x=1744400396; 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=9j6UAaWlpuGUtNDZ5PloVVAXeMJcTkL+Ix626QW6lmA=; b=pGmF37eAJErsv3HhD+5YGlLgbZ7LcAF50/Pd3TKXyyEnyNvMEp62EDn17SrXXb5q0z RnhDFFYMko5LRt+BSGhW0yoaAIqm9spsVdxHuWuiUN9ZPG3tRTJm8v0yywuJU5p62Bpq dPiy8/QfqLrETxyrTuFaOSB+XEE5et3TQCDPax6UZ9z9MZrjikC2v2drLWV/WkJdYh5B U0Yd1IejSnIQmgmrkW96XbU9sQqKp2P5PI5Xi9fb6c7VM40w57i0EuoJX+l8njSmTR2O MkHhIfltgQ1Le6o70CLOx5Ed+xD+80s0QhuFgtiIp5CIXr0nPjSkznROBHcJcTmt1wN1 aoGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795596; x=1744400396; 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=9j6UAaWlpuGUtNDZ5PloVVAXeMJcTkL+Ix626QW6lmA=; b=AevqUc/HRxPNHtr5NOeNhe7iltRVnofNl6UpxRlk1DfSmax9psKegbtdV56JyIOzgi nZGmzRV+zukC3MMLV/qfPRbL/Si5oyGGCOox3sqQ9MgwT/j1X5XsVoAiTJsJ6S/dnbRx USD/l9ijv5E9ISbDTvOmJlfcj8K5F7JsxUzTlhK9N3JUyuIM6qFbhKluJdR97J7eNpiw HsRxFJziN4i/xaWscg/EpJgygzZ6XHWVvuFEnowXrnYD4ru4umvhbEfYBRMkSNJjeX7I H0CC3JFApUpHKSRoH1JeEGu1vDz6WXkg5HjbpDyVvj6X3u1ICOhnWhyKSVOg5ACk7uHb zWnA== X-Gm-Message-State: AOJu0YxFbuVqYTbeLs1VbS552TSsqViBAlHJ/KnYF/SncLYdaudPZbGO ywZRCTuNOCnx5MBINpjlLw9w53qEPMqlQFq1srZFAF5rAa5aL3J+EPUgOLpjq9QQriE1k1Sm+Lw 9EQ== X-Google-Smtp-Source: AGHT+IHxE6ieJP/dqM4OtZ03l/OzhOac1l4SZVj083VbfNgDZX7xJqOUlyT5SF6DOaOv2f4SNNau/XxOkn4= X-Received: from pfbjo41.prod.google.com ([2002:a05:6a00:90a9:b0:736:3cd5:ba36]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3a0f:b0:736:4e0a:7e82 with SMTP id d2e1a72fcca58-739e6ff6b10mr4756745b3a.10.1743795595718; Fri, 04 Apr 2025 12:39:55 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:20 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-6-seanjc@google.com> Subject: [PATCH 05/67] iommu/amd: Return an error if vCPU affinity is set for non-vCPU 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, Maxim Levitsky , Joao Martins , David Matlack Return -EINVAL instead of success if amd_ir_set_vcpu_affinity() is invoked without use_vapic; lying to KVM about whether or not the IRTE was configured to post IRQs is all kinds of bad. Fixes: d98de49a53e4 ("iommu/amd: Enable vAPIC interrupt remapping mode by default") Signed-off-by: Sean Christopherson --- drivers/iommu/amd/iommu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index cd5116d8c3b2..b3a01b7757ee 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3850,7 +3850,7 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info) * we should not modify the IRTE */ if (!dev_data || !dev_data->use_vapic) - return 0; + return -EINVAL; ir_data->cfg = irqd_cfg(data); pi_data->ir_data = ir_data; From patchwork Fri Apr 4 19:38:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038828 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 18A6022171D for ; Fri, 4 Apr 2025 19:39:57 +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=1743795599; cv=none; b=OrWUYTg6v3XVwX9WIHJZVGcYaxK3TTyVH0++Hv157RsRwOBebgE2ordXmWqQNoYlcYISwsuJQbYwokynq4MmnEIDx14Uis5n27EAjRZ2AsTlaNyqcAqnlgXxVyuuG8jOtMBL+UsIroO6zbE++aAOtbDYOYVkculOgMA+PV1v8EU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795599; c=relaxed/simple; bh=GCCtnDomY+lLuf5+xII3/z6zqPIPK391FSHfxII0HaA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YOVolOtGNY7jsENa8vh5EVTJfAezLIF+MudJsCzn3JOeXKSWryl0RWA3Cv2dOZj3sIJ5krIRh8ghaL8mlL2C/zocdQMWYvjaB9afP1j9hFeTUQtshRMqsNMDlAAGhKdRLgNk/WCa8MNsvdgXZm9qE1ivAm0OOIDyL85kkuZqAjc= 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=N1DGtcBz; 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="N1DGtcBz" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3032f4eacd8so2299813a91.3 for ; Fri, 04 Apr 2025 12:39:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795597; x=1744400397; 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=wBBmYaZBofsgLugSeNOxbYqcOo/hzEiN1pKLRJ98ixQ=; b=N1DGtcBzZpRbm+lZ0QBi87cktItxx/yY+eVgQPV/2zdsZyiBi3pQXbtMmL893VEvgv 1WHxRO1iz+HWjdO7FzktI1e0R8wvlSShLC2vmqJPR8nDhqcSgUaW6zFS8xEPOV3XrVjo Bve36Qe+CYyWqklDuUwQv3IN3lRldrVcc4smuuX+JrOfoEGbl74d/nqP9V13r/toaZsX Yqvs2dseKjCRXSOFUgXP4xI11CeORwGS/Nu0YTBDKQVWeNSskqZzr6ZxxNmSLYAuBXFI xUB9gVzpA989cbUTt7agWcqxsjR6aB7fDLWGfI1VogbUeUitgJAbKv3RLImHTMUdoImm 35ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795597; x=1744400397; 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=wBBmYaZBofsgLugSeNOxbYqcOo/hzEiN1pKLRJ98ixQ=; b=jT0B0DmDTOIzCXf1QgnW48e9Xy7ByvbStBobrzw5zX1qG8oq8rwddikRsXDUEa0+Du PmZxk07wMsuC5/PmYTS6pczgt6U3BZKUsqzmdJb9g2ZTch1Z4uCCrunQR68UC+Cl1grp 7tA1pfD86aD//hXHPxbpy+DDt429l1A60fBHFDltgXPeb0okPANghL0Bn/mYgoQsrchH 0TmoYYnbo7jYhDahseHPB0JU5jDbS3Bjo2rva4uN/CUZ6sVHR2dhxKCSu5V/Y9p8JHyE DL/B0gv3H6EgI1zMYSaxZpKq8RLUBEXyvElQuj2Sp7MlSDI18qSoN4CBYgbKb8fkY1O2 1aXQ== X-Gm-Message-State: AOJu0YwowkoqbAXI1hyywo0WxfwFUsMvDyUqSGIl3HJiuRtWtf58HhYN MdyZRnmymjenAmdxWsoC0HM3HvCeD5LAfCs/elLZZtIADHctdDTe6KUWmjTrWE83hpqBER+fM7b /Iw== X-Google-Smtp-Source: AGHT+IEBvDLqiwZ4BM4ogV2kWH9PVQzEHSq6D51X+1Q81b5CZqFohRw+cIfM1ejZIuAlzQtHAHcZOhjh9GM= X-Received: from pjbhl5.prod.google.com ([2002:a17:90b:1345:b0:2ff:8471:8e53]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:540f:b0:2ee:ab29:1a63 with SMTP id 98e67ed59e1d1-306a611253fmr5033572a91.3.1743795597468; Fri, 04 Apr 2025 12:39:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:21 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-7-seanjc@google.com> Subject: [PATCH 06/67] iommu/amd: WARN if KVM attempts to set vCPU affinity without posted intrrupts 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, Maxim Levitsky , Joao Martins , David Matlack WARN if KVM attempts to set vCPU affinity when posted interrupts aren't enabled, as KVM shouldn't try to enable posting when they're unsupported, and the IOMMU driver darn well should only advertise posting support when AMD_IOMMU_GUEST_IR_VAPIC() is true. Note, KVM consumes is_guest_mode only on success. Signed-off-by: Sean Christopherson --- drivers/iommu/amd/iommu.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index b3a01b7757ee..4f69a37cf143 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3852,19 +3852,12 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info) if (!dev_data || !dev_data->use_vapic) return -EINVAL; + if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) + return -EINVAL; + ir_data->cfg = irqd_cfg(data); pi_data->ir_data = ir_data; - /* Note: - * SVM tries to set up for VAPIC mode, but we are in - * legacy mode. So, we force legacy mode instead. - */ - if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) { - pr_debug("%s: Fall back to using intr legacy remap\n", - __func__); - pi_data->is_guest_mode = false; - } - pi_data->prev_ga_tag = ir_data->cached_ga_tag; if (pi_data->is_guest_mode) { ir_data->ga_root_ptr = (pi_data->base >> 12); From patchwork Fri Apr 4 19:38:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038829 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 6BA5421D5B5 for ; Fri, 4 Apr 2025 19:39:59 +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=1743795600; cv=none; b=qa7ehtXd8JvEBKjzenLE0H8cdsmCPF3PfMTG+fIref/6QroQ5FOFsWi/w1gs8uPVoj3wxzfnhU5fgcWcmDk/DmvJsd3ypraFAClG10zQGCEnBvcTXLdcUrWoXbsCsIvWOcurVrQNjS7npsRimnBfNS56WCKUdJFhU7VIoftvr9s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795600; c=relaxed/simple; bh=yOm0l7runQh/bqd/+NGHUK/eIjFj2umaZ1UvQPY4G08=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uE39UtmJK3lmm8o/+oMHkfekZimH3VnFKfqoODtEO2VfH6BHaJMjJjzoTbNT5wKo1csS7ORKzYg7lYhmzsrxuNqQSyzvBLB8xlnzBnSrW1Lt1G9h2ND+kgk/YMUNnOCodgDj+E+iZbuEm27Hhyub/DEVwJC7hK3dplCqOm42kJ0= 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=d7OZnnuy; 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="d7OZnnuy" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736cd27d51fso2260354b3a.2 for ; Fri, 04 Apr 2025 12:39:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795599; x=1744400399; 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=ksKzicOEYbjGpeVOLeT0G0MNe2BKT96MICWw9wdEzS4=; b=d7OZnnuyLEPoirHra3BvFwZao73wMFB0i/1Mq+wBjhZTxLODaZIe0YVwG/sxt+RKky n/QXIDFQsJyLjBO12rGLABUN5UGzy7Xt0ZwKO2VvJgBKSBOERH9pXgu28n0d0EAtd+Cr PB69H7c84AcDo/ZZ96qFarFF22aJf+8gLUPgsFHTsFMfUwiOVOv3GjGNdDDHcCGZdtuj 1H3oCP/Of1CbQba2hyjI2gUJlgJ80o3cRJIeyflTkYrBeMxPYOF5olwHGsApBNnFWUNP RGN5g1+cw9hbJL0Vy/2QmcHETI3wLGEnQA5CPwG5I2b3IyAm4uZD54NzEVE6BxO/gH7Y t05Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795599; x=1744400399; 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=ksKzicOEYbjGpeVOLeT0G0MNe2BKT96MICWw9wdEzS4=; b=uT5ggY5POCu8GfnFgOObx9VFe8/ZCA/RETF1nwyGu6J8AZ/M/um6QJmiRW+GZ/RvPN 5246dkWPAc4TS8ZvBQP3ViKxpifdaP0+Q1qP4YH9KssPU+TGxZ1eO5zlcKYM6B0ldW1J zEAnFLI903lFO48DhhcCnBoAlWnOh/vf1/meDAeiiKds3cYGKjLapIP+9swX6NwKpKdh ztYuXbOYrNRd4EgfB71GNpBwLyr995ujY/OpZMazLiWZObB+NryI2FytrMs9/dm5RQUe 60biKlQO/2Te5rUrPZmc6f65VclzY/izqNeCoB2Vw3GwwFvKa8hVj2oKUzYaxua3dcb7 ibDA== X-Gm-Message-State: AOJu0Yy/F37UToSJiWva/26/3TI/E3FnBwXnChkdWyEL6cnQwvCWcsSf 9JNPfLhDnVVpObsUR7INO4Wiw+8lUKjbqdn02npRzwlb87SevkcRNJWGalsgtaZ6tqzeX/3SN6+ n3w== X-Google-Smtp-Source: AGHT+IHgQd8TSnrl1N62387tBCrXt8b1iTBjftjzhSHwCWE5Yo+RGmXWAwjPpFA1Fbz5oNevpz0/JHGX4zg= X-Received: from pfax3.prod.google.com ([2002:aa7:9183:0:b0:736:47b8:9b88]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2e9d:b0:736:5f75:4a3b with SMTP id d2e1a72fcca58-739e6fdb4bemr5249939b3a.7.1743795598917; Fri, 04 Apr 2025 12:39:58 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:22 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-8-seanjc@google.com> Subject: [PATCH 07/67] KVM: SVM: WARN if an invalid posted interrupt IRTE entry is added 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, Maxim Levitsky , Joao Martins , David Matlack Now that the AMD IOMMU doesn't signal success incorrectly, WARN if KVM attempts to track an AMD IRTE entry without metadata. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index ef08356fdb1c..1708ea55125a 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -796,12 +796,15 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) struct amd_svm_iommu_ir *ir; u64 entry; + if (WARN_ON_ONCE(!pi->ir_data)) + return -EINVAL; + /** * 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->ir_data && (pi->prev_ga_tag != 0)) { + if (pi->prev_ga_tag) { struct kvm *kvm = svm->vcpu.kvm; u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag); struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); From patchwork Fri Apr 4 19:38:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038830 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 8C56D2222C6 for ; Fri, 4 Apr 2025 19:40:01 +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=1743795603; cv=none; b=Ol/G3ANjvdnMRtF2w/G01jicbiv9+S1iXr7c6aiJFgyaK+2RaQ1M2AiQGOSei9SHoeWjOEtmmUbSZWaOYhoQ6wCfx+1hNjvbA3L7lcP/H0jzySWKGwMnlcwXByOW790d7Bwfwv6V8xDq04aTtxj/ImvY1T4SKaAvLRbeu4l2CAA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795603; c=relaxed/simple; bh=XxKGAo5S/7chLOptC2l4Epw1k0hr0A2t8lxhQjwAc7g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=p5EgNAlBi9Q71Q+iEYXwiGix7FkAAtkfQ68j97TkXl+7IPxiYThQq37nmoPBbH65XhbnrHU2HpWrJx+rjSCpwurHUa0s4EYE8k/cOT1kQfQPEtDmXVwWflySeH3GONTBQRJxjH2L7sbkzOp2Dt+xNKrVYrkja8U7SsXYqPFDEe0= 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=pVNQHgzM; 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="pVNQHgzM" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-af9070a5311so1485074a12.0 for ; Fri, 04 Apr 2025 12:40:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795601; x=1744400401; 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=pR0smYfw7SLubbYd0Sbbpj1N3VRm1qYO6xSIljeIkxU=; b=pVNQHgzMFEUdgulgCmZnaexnvMOeIUKNaZG2Am6LLQw8jIiLkjOsSyBcNZv2DdPq1W nPFYij2PX5vbEg7agKViNxVJytURR92Ylqud+ncWkhLn45QMSvutNnKI+JG2V5YSBKCy Db5WU2NWCEJiQ8VfmcxiVMaNl8ueYqw6dHVMxXq10gr6dx1gRsMgIg3VK2jWEc66YpWL EBDCtj0BuLMAQbAstrdUfjfDi64i1I8xtwqMSImYTt/z/qV+FB54pi3gtRmlCzUpTAew Dgwk+96+e6SSpM+sCMVpEug/+63pNxlUEIv5Hr+gTaYabYhp/Aw4NhqI+tU/jP0ZXuI5 S1uA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795601; x=1744400401; 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=pR0smYfw7SLubbYd0Sbbpj1N3VRm1qYO6xSIljeIkxU=; b=Y8kh421Zdf+4qTRaTSZLgEQrNMnG/pNjPtbm8fSlSudjDkWC+gNeDeAnrKa6W6pYLZ jOWWtsO8zcpF/Wmd6NanTZ6N+p9ldTWrQYMDmRelHj0K6e40ouEISSOcz9YaQ0gBfQ5+ Os1TP/c79gWCJG22bZH4gYgdj/58yQ6+gJky6+55Wt1ep2pJvEsCkzHy+qw9bhBAtWNl r08JRh6Xu/+BavOF4/evVAI90b6HPWC63iTX29JB+OKEggArDXGzCNfX+38EhRigK3jB XnAvYMwg71OkNICK66DPktIQi2L3r95YCN5rbrS8RnC+JpDbBw5CCHl6Xldrj2V7psWM DTWw== X-Gm-Message-State: AOJu0YyonMUQvCYlJs1vkc8xJwiWIeb/QlUs4HDApDIHWx+yDhKnXjVt X0MzsgaYCxdTQTfT5gQJtIgOFbuN7UWicw4ymZoK3XoNPF+Ux2RlNFSHewhNEnTE2QS3oc9uvm0 PHw== X-Google-Smtp-Source: AGHT+IHQXM6RwHsmF3d6JVendNoGhD/BtEd7oD5dydB1hWqicx9/MPhQKis3fehg2sTG9mCKbtu2ZYHsxAA= X-Received: from pjd8.prod.google.com ([2002:a17:90b:54c8:b0:2e9:ee22:8881]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2f4c:b0:2ff:7b28:a51a with SMTP id 98e67ed59e1d1-306a48a4b89mr7615984a91.17.1743795600824; Fri, 04 Apr 2025 12:40:00 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:23 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-9-seanjc@google.com> Subject: [PATCH 08/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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_host.h index 6e8be274c089..54f3cf73329b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -294,6 +294,7 @@ enum x86_intercept_stage; */ #define KVM_APIC_PV_EOI_PENDING 1 +struct kvm_kernel_irqfd; struct kvm_kernel_irq_routing_entry; /* @@ -1828,8 +1829,9 @@ struct kvm_x86_ops { void (*vcpu_blocking)(struct kvm_vcpu *vcpu); void (*vcpu_unblocking)(struct kvm_vcpu *vcpu); - 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 1708ea55125a..04dfd898ea8d 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -18,6 +18,7 @@ #include #include #include +#include #include @@ -885,21 +886,14 @@ get_pi_vcpu_info(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e, return 0; } -/* - * 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 = true; + bool set = !!new; int idx, ret = 0; 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 host_irq, if (e->type != KVM_IRQ_ROUTING_MSI) continue; + 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 d4490eaed55d..294d5594c724 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -731,8 +731,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 78ba3d638fe8..1b6b655a2b8a 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 #include +#include #include #include @@ -259,17 +260,9 @@ void vmx_pi_start_assignment(struct kvm *kvm) kvm_make_all_cpus_request(kvm, KVM_REQ_UNBLOCK); } -/* - * 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; @@ -277,6 +270,7 @@ int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, struct kvm_lapic_irq irq; struct kvm_vcpu *vcpu; struct vcpu_data vcpu_info; + bool set = !!new; int idx, ret = 0; if (!vmx_can_use_vtd_pi(kvm)) @@ -294,6 +288,9 @@ int vmx_pi_update_irte(struct kvm *kvm, unsigned int host_irq, hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { if (e->type != 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 ad9116a99bcc..a586d6aaf862 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 #include +#include +#include + #include void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu); @@ -10,8 +13,9 @@ void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu); void pi_wakeup_handler(void); void __init pi_init_cpu(int cpu); 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); 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 dcc173852dc5..23376fcd928c 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13570,31 +13570,31 @@ int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, struct kvm_kernel_irqfd *irqfd = container_of(cons, struct kvm_kernel_irqfd, consumer); struct kvm *kvm = irqfd->kvm; - int ret; + int ret = 0; kvm_arch_start_assignment(irqfd->kvm); spin_lock_irq(&kvm->irqfds.lock); irqfd->producer = prod; - ret = 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 == KVM_IRQ_ROUTING_MSI) { + ret = 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) { - int ret; struct kvm_kernel_irqfd *irqfd = container_of(cons, struct kvm_kernel_irqfd, consumer); struct kvm *kvm = irqfd->kvm; + int ret; WARN_ON(irqfd->producer != prod); @@ -13607,11 +13607,13 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, spin_lock_irq(&kvm->irqfds.lock); irqfd->producer = NULL; - ret = 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 == KVM_IRQ_ROUTING_MSI) { + ret = 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); @@ -13619,10 +13621,12 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, kvm_arch_end_assignment(irqfd->kvm); } -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->irq, + irqfd->gsi, new); } 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 5438a1b446a6..2d9f3aeb766a 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2383,6 +2383,8 @@ struct kvm_vcpu *kvm_get_running_vcpu(void); struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void); #ifdef 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 *); @@ -2390,8 +2392,9 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_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 249ba5b72e9b..ad71e3e4d1c3 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -285,9 +285,9 @@ void __attribute__((weak)) kvm_arch_irq_bypass_start( { } -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) #ifdef CONFIG_HAVE_KVM_IRQ_BYPASS if (irqfd->producer && kvm_arch_irqfd_route_changed(&old, &irqfd->irq_entry)) { - int ret = kvm_arch_update_irqfd_routing( - irqfd->kvm, irqfd->producer->irq, - irqfd->gsi, 1); + int ret = kvm_arch_update_irqfd_routing(irqfd, &old, &irqfd->irq_entry); + WARN_ON(ret); } #endif From patchwork Fri Apr 4 19:38:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038831 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 0B99D222570 for ; Fri, 4 Apr 2025 19:40:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795604; cv=none; b=eYEhLv1zNdE8QPBwMli8WoryhfVzOvUIvS89+enz0dC6oiCIph2lHJpbpfKBUIi6ZncX9IuoGa86iVPJ347MloXhjn5k1ncjVAbtMf+fd6USC4Fwtr9ABP8Ah5hDSYqSfMbgphXCTdypspmYpkAhxv2+bADHOPqN5WT6iSd5/oM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795604; c=relaxed/simple; bh=Gz9aIMimbdwkNGt7OPfLnkRhIKwp18RPumjkvSLqEHM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=faqdFFfZAFIVvH0i+nueJ+0+RCqc5TJoTAJHTWLGzhkI/MybpbY3ma64//zH5Hiab4TfGsMtvVrg9I9+FilA5fjDndWIplzDA2RSCFVWIJal4QRX2CR2i1X/Kd1y2MoHTyA2tNfoCbRI60/UjnKDISNbTSDOqt/Ki4aGP0HNFBY= 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=fGegI5Dt; 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="fGegI5Dt" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736cd36189bso3496700b3a.2 for ; Fri, 04 Apr 2025 12:40:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795602; x=1744400402; 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=NJ2AwZ96IG0rnLVApDhmCehhnZWDzfbxNCDQQeJNmJo=; b=fGegI5DtjFeDLuxNLMx33FbVQzftJpufxMA6mn6QfY8vuXHcD3A60A+5PjrH8vGLOw oNp24lRYo53CZmFT61DQABQAgt/JvIoVOynPM7is8YVUAwJJhv6CNLrFxsKSm3mcxZtq juAI1P4cpta82mI2OEFWCtlq3szdrgR/KTF7xkxfk1EwtZroORfeOVi2MsS9+tVgTQts NsfbJfIr+yb4DnbGtZJW9USwIinHxmbWaPlkx79M1e2YiRVnlxXYKsB9en+GKgtLL0UW bN0OWo3aS7h32TQWS7wHvWP75OMI01chweM66d/kYisFgEJFwO2aso2QGZfCxlGnUQoG 1yYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795602; x=1744400402; 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=NJ2AwZ96IG0rnLVApDhmCehhnZWDzfbxNCDQQeJNmJo=; b=aI8KOpDeJ9ACucpKqxnkM+R/3BDssemAnl8MM+ha9peFDW6MVL+K9v56hWr2syP34s 1ZreFrHsoAmYMc0xPDASwfHUN4SuqOm07Tx822rPEFsDx76tETDOisyYliCzDOaOtCs2 WcxGs4VDVsi7PtHp9/aRITo7pQy70/NcOo0c8nLTq/3rLyPC4/M5ZAkQ6UJ0H/Q5jHtM CLA7WLze9Zm0bMQaXoSXp4ivepeTMqIPsSBELI7xQ1gTqzOtqrqfbrNVBn8jecsPHX/i CwgnOJaWZFQnpayQuH+ePsfLuoOjE8tfFiLzpxuyd4vdxJ9L1fydj7ZOzAVB9yUFbjKK JEWw== X-Gm-Message-State: AOJu0YxZt2kF3LDZpWqbc9q8GXN10iv03GfE57Cc9sp0eQ5aiX/PEvib 4I4JtEQQsss1ywDiMsTNSBcu8lLs1MMpcSP29xcZO+54bvcvVm+5zENqz7v4q1IkL/N3vHbnPPX JPw== X-Google-Smtp-Source: AGHT+IHUTfAFvmnRXzxBNmSCJ462dhgG2sdJTjm66unWjPqXxcSiOT2PYCNJZf1pPqjK0XlU7MmQ9tlS5gg= X-Received: from pfmx24.prod.google.com ([2002:a62:fb18:0:b0:736:5012:3564]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:13a6:b0:736:42a8:a742 with SMTP id d2e1a72fcca58-739e4b4aa8bmr5807564b3a.11.1743795602558; Fri, 04 Apr 2025 12:40:02 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:24 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-10-seanjc@google.com> Subject: [PATCH 09/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 49 ++++++++++++++++----------------------- include/linux/kvm_irqfd.h | 3 +++ 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 04dfd898ea8d..967618ba743a 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -774,27 +774,30 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) return ret; } -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; spin_lock_irqsave(&svm->ir_list_lock, flags); - list_for_each_entry(cur, &svm->ir_list, node) { - if (cur->data != pi->ir_data) + list_for_each_entry(cur, &svm->ir_list, vcpu_list) { + if (cur->irq_bypass_data != pi->ir_data) continue; - list_del(&cur->node); - kfree(cur); + if (WARN_ON_ONCE(cur != irqfd)) + continue; + list_del(&irqfd->vcpu_list); break; } spin_unlock_irqrestore(&svm->ir_list_lock, flags); } -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 = 0; unsigned long flags; - struct amd_svm_iommu_ir *ir; u64 entry; if (WARN_ON_ONCE(!pi->ir_data)) @@ -811,25 +814,14 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); struct vcpu_svm *prev_svm; - if (!prev_vcpu) { - ret = -EINVAL; - goto out; - } + if (!prev_vcpu) + return -EINVAL; prev_svm = to_svm(prev_vcpu); - svm_ir_list_del(prev_svm, pi); + svm_ir_list_del(prev_svm, irqfd, pi); } - /** - * Allocating new amd_iommu_pi_data, which will get - * add to the per-vcpu ir_list. - */ - ir = kzalloc(sizeof(struct amd_svm_iommu_ir), GFP_ATOMIC | __GFP_ACCOUNT); - if (!ir) { - ret = -ENOMEM; - goto out; - } - ir->data = pi->ir_data; + irqfd->irq_bypass_data = pi->ir_data; spin_lock_irqsave(&svm->ir_list_lock, flags); @@ -844,10 +836,9 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) amd_iommu_update_ga(entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK, true, pi->ir_data); - 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; } /* @@ -951,7 +942,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); } if (!ret && svm) { @@ -991,7 +982,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, vcpu = 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); } } else { ret = 0; 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; }; #endif /* __LINUX_KVM_IRQFD_H */ From patchwork Fri Apr 4 19:38:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038832 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 BD11C222599 for ; Fri, 4 Apr 2025 19:40:04 +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=1743795606; cv=none; b=JMFFr1Vym1xh33o9Ba/nr6V/ZHPLYfbog8+/GSUtgeMzaz2QlwI0e/Acs3rqbgdH1yyicJ7VEEaYbxIKzv76PVFsxIvx4tgw0jNb+W8brLswfuveohRwCGN6h7EBn9X9oYzWvimwgPXOxIt+rXnyCPOYlP4q1h/FFpDeMi2tkWg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795606; c=relaxed/simple; bh=oivgLMsYP6m5xV9KLWqFEzyIJWf/41dvsC/fZbkUGSA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cM96bd4bgfD/DZn/Ccn9eka52PQUSdmYSZjz+YYt4Vh808B2l1y3+mhJArc16xwmn9+Rxg78zeLe6Er6YlXEAQdsXgI+iCvBdI21hZNTuFw0BT7qI8fCJ5sEGbpjrD1gfakwtzHUfZczSy4Mscv8iuNQIloLxb7pzyatf4/+7gg= 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=gklIrjd/; 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="gklIrjd/" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7398d70abbfso3439769b3a.2 for ; Fri, 04 Apr 2025 12:40:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795604; x=1744400404; 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=ocleBBvlKr6feyKde42bUZgR4JDW1n63+aHvVvZVcGU=; b=gklIrjd/TRu8S+2bmwoRKc6RhdQI7TSJ9kHAVRlsp/T0vxnzkLGm2jY2XPjLu1/9+Q F5uM/L2DyXp0ZGijN6u2yEJNgwAeTdEjKeWWb1+zfSOthrRbJi2iuZEPsxYZDnpCOmvV lgqZwYiJKYgEvPsbTRz+CvXDR0EC2mR06ZTvYp0ehWdydyhcnz6p+tQsBMz7DMM6M8lZ H6Ols1R/FFWmCtf2iSYmACJuVe/v3R3+RHQBcusIerHyNtM+fccMMUf2ae/fVvSjdu+3 Tzkri9nEYLauTdO1jC149F57sisk83s7GMOEAF579tV+r4ZDwTMJiml7fa0gC6ttBkbD l/aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795604; x=1744400404; 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=ocleBBvlKr6feyKde42bUZgR4JDW1n63+aHvVvZVcGU=; b=XQE7Gw86E+BaostmmPd7+c5KR7BPG/vhBZClklNaBSdiiPirlPVOdBR5Px2nd5Cmdd 3shuXUKrI/RVx1fHvw/ngc6j9q7Qo88qbI/ZnyjEVtQf86ukS9G481nrV1e3bpDN9Vlb rg9nz6lo5Kx7mVf5JcovfeN+KB7sMiWbUrBCw1Qyd8ftz9FeiKrziEaoVRt3MXbBREsZ fVXZBaT5ObIPxavt3mga8iqaQv147DmQDJleDhkQBnHbJ4z8OFxFFKB4318tKk/st/GZ TIxmHwqxPN0tzOrDcV4IblRt2zQ/IVLTXOgb8bDE+k/9eM2zqlcN36JvfKKSf53Jwqge /WWw== X-Gm-Message-State: AOJu0Yya2jyGJeDyBiHEZlYnXrgvP3pR93rePkqHtzCSH6ycVXNNbSSL bwTRTBswruFGAbzRVA+StUvlEoe3xtcBmLDaqoIiyFie9wam6O9fg6XNA4EepVrOI7IpMhI1FSK IeA== X-Google-Smtp-Source: AGHT+IH2fhk1BClfEw9pVUM6hqP/FRNe9lFGePO9NibjY1q3A6Bve4xhICh/FTFH3jra1e+6rrGPO6LW4yo= X-Received: from pfxa29.prod.google.com ([2002:a05:6a00:1d1d:b0:736:a983:dc43]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a04:b0:736:3e50:bfec with SMTP id d2e1a72fcca58-73b6aa3d9b9mr861903b3a.8.1743795604041; Fri, 04 Apr 2025 12:40:04 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:25 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-11-seanjc@google.com> Subject: [PATCH 10/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 61 +++++++++------------------------------ include/linux/kvm_irqfd.h | 1 + 2 files changed, 15 insertions(+), 47 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 967618ba743a..02b6f0007436 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -81,6 +81,7 @@ bool x2avic_enabled; 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 */ + struct vcpu_svm *svm; }; static void avic_activate_vmcb(struct vcpu_svm *svm) @@ -774,23 +775,19 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) return ret; } -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 = irqfd->irq_bypass_vcpu; unsigned long flags; - struct kvm_kernel_irqfd *cur; - spin_lock_irqsave(&svm->ir_list_lock, flags); - list_for_each_entry(cur, &svm->ir_list, vcpu_list) { - if (cur->irq_bypass_data != pi->ir_data) - continue; - if (WARN_ON_ONCE(cur != 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 = NULL; } static int svm_ir_list_add(struct vcpu_svm *svm, @@ -803,24 +800,7 @@ static int svm_ir_list_add(struct vcpu_svm *svm, if (WARN_ON_ONCE(!pi->ir_data)) return -EINVAL; - /** - * 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 = svm->vcpu.kvm; - u32 vcpu_id = AVIC_GATAG_TO_VCPUID(pi->prev_ga_tag); - struct kvm_vcpu *prev_vcpu = kvm_get_vcpu_by_id(kvm, vcpu_id); - struct vcpu_svm *prev_svm; - - if (!prev_vcpu) - return -EINVAL; - - prev_svm = to_svm(prev_vcpu); - svm_ir_list_del(prev_svm, irqfd, pi); - } - + irqfd->irq_bypass_vcpu = &svm->vcpu; irqfd->irq_bypass_data = pi->ir_data; spin_lock_irqsave(&svm->ir_list_lock, flags); @@ -912,6 +892,8 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, WARN_ON_ONCE(new && memcmp(e, new, sizeof(*new))); + svm_ir_list_del(irqfd); + /** * Here, we setup with legacy mode in the following cases: * 1. When cannot target interrupt to a specific vcpu. @@ -969,21 +951,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, pi.prev_ga_tag = 0; pi.is_guest_mode = false; ret = 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 = AVIC_GATAG_TO_VCPUID(pi.prev_ga_tag); - struct kvm_vcpu *vcpu; - - vcpu = kvm_get_vcpu_by_id(kvm, id); - if (vcpu) - svm_ir_list_del(to_svm(vcpu), irqfd, &pi); - } } else { ret = 0; } 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; + struct kvm_vcpu *irq_bypass_vcpu; struct list_head vcpu_list; void *irq_bypass_data; }; From patchwork Fri Apr 4 19:38:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038833 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 9B576223336 for ; Fri, 4 Apr 2025 19:40:06 +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=1743795608; cv=none; b=n908ZBhokgmB0VxqHXOX6j8LdAVYM1N+VJCuJlSijOauWbQ/XsJGlDUxBUM6ElAVdtAKhw8619ZIErbcZ2ptDMQjXF8GPsrh8PFvLUjPNOBVl7u8XeUTxo3zTcWxaBRDMeBq+llR7lTDcHQkbiLNup00t/2bidqzfVB+pF+PUmE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795608; c=relaxed/simple; bh=ymgLNvDMBazjcOWHdM4TRBB8fRza/15wPWUAsosttHc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WZOxByxtx8SYKN2NN4XxwbQqxhjzA1avVeW5j1c+88VJo4T7K4Gs2wj/4DNSALJgPGoze9ha0jgtcajPrfDehAbw1jQaE86TJGr288y/skY2TqvzKeQiGGdvN62csQU/ugPWSsuzpTTG8W+Ts0fIN3GNx9EhE2zPp1010ftOh2c= 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=DKNY5UbK; 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="DKNY5UbK" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7370e73f690so2788759b3a.3 for ; Fri, 04 Apr 2025 12:40:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795606; x=1744400406; 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=FiKuf+KLGMevEl1JcUq8+wDFjGbG6tG3veWOyXB7Hl0=; b=DKNY5UbKgwbgnpH8+tO8cF4nRJPTau2T4BuMH7aMan1MGphtVL9al3OGl6W0AIthwM f/uzf7ZeT2/qX0tMsLGFMEr4JZCwJpHsHH6SqZavgvUZk2QdE3nWARBo0GtN1oIPVmGw HKo/Lio+WQXeqvCq4nlYFMxoABEO9yHbgCOG6BDG6f7Qx8Rg2p63DBh3BYLDzvbSK3pj ERuF3VPO09qeMENAHX1LxVKLoTD67zNNMO8wYb29fTFKjOtTqqBvBOQSepTHXiaCiV3X Yu5e+GFQKKMCgp00bJCf4+gVOUtWdYBc4X46HZgR9TGZbLd+no70lJ2zitDjwEqcbv47 dWIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795606; x=1744400406; 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=FiKuf+KLGMevEl1JcUq8+wDFjGbG6tG3veWOyXB7Hl0=; b=Ub7rmzi5mJBRGK4PHmxH3750YGMScxswCiHdUf0G76zfADSsiH/Nx7hM9aPUw5ONPw 4QBXB+Qc0d2x2mFfXK6q/nHG54Wg6+4M7YnhbMqnFQdGeBxoGKwCRJq2lgP1CzU9+PEu f/MMgrl7daIQw+J/XofNFJUJTNXdzCVzYjGBNQ0Tu+DjmqhPir3JCVOU864m/mwMDQjl 2V/hwvembcHfIk+NIsJK19KcESMoMD3wPVpPHE2N90LvK+1cr5t16edx3T8pj2WTJPWT y4mN0gLESztfCQgtTGqijbRLabWVFb2eKHYNuVKLN/J31QsoQxMEPu2bnbpcO7eGvJbp ddQw== X-Gm-Message-State: AOJu0YxrVH5iWDecV608XaqYcaaC2k7ZKwW6Z0PH8vYnMo3SBn3Hfov+ flsFjybRylnpfCs9PmWpbIFqwLjTIVCt+ECaCEMJ/WFPvIdHLXwMztfSgzKa+7C2lf9OuM6jVGn N6A== X-Google-Smtp-Source: AGHT+IFtyda/QQ9X0Y9KdE3cg1eDpGK8RDILaxqr6131sPdJg3+eJBnN0Hdt1DkeEPmuLwr7hr23wisO9LY= X-Received: from pfoi21.prod.google.com ([2002:aa7:87d5:0:b0:732:6c92:3f75]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1941:b0:736:4e02:c543 with SMTP id d2e1a72fcca58-73b6aa3d952mr644845b3a.9.1743795605825; Fri, 04 Apr 2025 12:40:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:26 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-12-seanjc@google.com> Subject: [PATCH 11/67] 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, Maxim Levitsky , Joao Martins , David Matlack Delete the IRTE link from the previous vCPU irrespective of the new routing state. This is a glorified nop (only the ordering changes), as both the "posting" and "remapped" mode paths pre-delete the link. 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 02b6f0007436..e9ded2488a0b 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -870,6 +870,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; + /* + * 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=%#x, guest_irq=%#x, set=%#x\n", __func__, host_irq, guest_irq, set); @@ -892,8 +898,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, WARN_ON_ONCE(new && memcmp(e, new, sizeof(*new))); - svm_ir_list_del(irqfd); - /** * Here, we setup with legacy mode in the following cases: * 1. When cannot target interrupt to a specific vcpu. From patchwork Fri Apr 4 19:38:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038834 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 65442223706 for ; Fri, 4 Apr 2025 19:40:08 +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=1743795610; cv=none; b=a6Oh4TUeprzk1r/sj1DvQu/qICS0nITHsIjR6J51/FQo5CD/UVbjxu5ajJOJoaBiBgrWBXoXACV76Gh74kcjHUTHeiFTgUOd6ouSY1LD+jR7s7V3b4LK9tdxmwawRTxiLcnGVMDOXs6cg2lpB2oRFK6DhR3rMHaSw6sqer5BoIM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795610; c=relaxed/simple; bh=uzlVEbR4BTmFkBjn23KnqXJ/yIeZSAO1hkc8JxEHwWA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VVj7EcZH93dsBFFuL6avO0A6ETH6V0U8f8Vvuu+kNsvUme341b2eVl575pUvm+OHsstAOt3RiPFfd/5enbf6PWyqmvV621vzrNsG6ku0MAB/pDw4QiqKHG+jVN4SS+m7rMh8g8k0Md90acPff49ysr+QpEWs2A3ccgKmyf2Wd4Q= 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=hiJvr4Vq; 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="hiJvr4Vq" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7377139d8b1so1924111b3a.0 for ; Fri, 04 Apr 2025 12:40:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795607; x=1744400407; 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=8U7fbiDc3fD3nWCIa5Kamzu6n5Z7wqEoDo9I5vrK7FA=; b=hiJvr4VqkVJiCmGOi2ey/1zoe8cBlUvks4xmjkFjPojsk7j91i3R5p7uRlne8h7Fc1 EcKfh1e8+f2X7IhbrasqqaO9UqwOYJXX+Dk2YfzFoFiXZhfTpkwL5e+YHEA7n1Sp2u5i G1VrBntKN9uBzIHuheyEgTTZHj6CyogUHblyygA7UypBqHE3QtaqsiUG6MjhkQHfwmbe dMVVVvwlsXlnaNaHbYm1TNi60gvHpSyH8kJVVKCaTBUMN3Xl+4rzWj8LGmaq+hs5fLIA ee+EyRS4Dcqb2RcybrB9fYPTuri967AaXQRGW04yeTuTK536U1MAF+zKdlSn2DANSOm9 QSNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795607; x=1744400407; 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=8U7fbiDc3fD3nWCIa5Kamzu6n5Z7wqEoDo9I5vrK7FA=; b=RZJzEw9h25FTHwdufV9j2UZQerFrNrcgx7v8JRXDBqMeCA+zLTdmEFkS90gpX2tHLr T9U78D7DbGOQZ6EUMBdpfhV2ay1tJj2N5Qj9d0S3LzW5GcfQGCAzpaojYhPrfGMNpfcj vpAZi6kuOQ8GAO1+Y5A8j0pRdxxdEUt7g0G3mjGQFDzZrst6mz9vptVQ95WFmT6GU/SW rz/Qe5Ri8Sja3GJ5mraC8mgccBwOqiUWuOyixaGd/6bMRGMCJP/3CLRLTHaG1Cg53too ONCZ42IZIvZVVWy3X4xskRKfdZezPRIgbgofZ2fAK7vqNRd5M6ghTo3fTibYfBQRQ58q Vu+w== X-Gm-Message-State: AOJu0YzFOGmO2fRTXnvR452j1q6IqAsu316WXSJQXOOfy5RJkb79MysM Sz4HQ5p+9UFM9oHrJcomDCyBz0cr84wpIRc9BjsBn3lzJc3OyVkSNndJZhHD6GrsIEKkA3dby8T O9w== X-Google-Smtp-Source: AGHT+IH1FWkdivy4S+bLPUdZ+zOkMlZOgSIPe0C8D80kAIF34VnktZoafbX6YO039dybyFyARTj7CbbGk/k= X-Received: from pfxa1.prod.google.com ([2002:a05:6a00:1d01:b0:730:90b2:dab]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:561a:b0:1f5:9330:2a18 with SMTP id adf61e73a8af0-2010801c42emr4738241637.23.1743795607614; Fri, 04 Apr 2025 12:40:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:27 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-13-seanjc@google.com> Subject: [PATCH 12/67] 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, Maxim Levitsky , Joao Martins , David Matlack Drop VMCB_AVIC_APIC_BAR_MASK, it's just a regurgitation of the maximum theoretical 4KiB-aligned physical address, i.e. is not novel in any way, and its only usage is to mask the default APIC base, which is 4KiB aligned and (obviously) a legal physical address. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/svm.h | 2 -- arch/x86/kvm/svm/avic.c | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 9b7fa99ae951..9d3f17732ab4 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -254,8 +254,6 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define AVIC_DOORBELL_PHYSICAL_ID_MASK GENMASK_ULL(11, 0) -#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 e9ded2488a0b..69bf82fc7890 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -253,7 +253,7 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb) vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK; vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK; vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK; - vmcb->control.avic_vapic_bar = APIC_DEFAULT_PHYS_BASE & VMCB_AVIC_APIC_BAR_MASK; + vmcb->control.avic_vapic_bar = APIC_DEFAULT_PHYS_BASE; if (kvm_apicv_activated(svm->vcpu.kvm)) avic_activate_vmcb(svm); From patchwork Fri Apr 4 19:38:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038836 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 2E204224238 for ; Fri, 4 Apr 2025 19:40:09 +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=1743795614; cv=none; b=isMZknQLtZz7lRAibxp90CVmmHQHK2TWwqyv/+4nLiohuK0w6FG+UmOJBERlt3l/K08+S4bAtHCMx/3ywJ8m3gJtCYN80pcBLZAZ0DM2X33PeHregcJxfmFUEJ19Ei6ilTGww32CTQxa7aG57drko6W5Zn+FhPvSroE9WGTAK6s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795614; c=relaxed/simple; bh=+cf2Ksm3rNxUCqZDmz14iRQrHlrznLeIaeOW6HbZuz4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PwzwDkYboS2sjSqYQkLs4P8jys6ls0bk7Icyew9UV46WCSP0VYBWUWdSlQ91eCTpe3qgYYZJIwC7ojYSIanNUv+kEt+/yoma3KFBekzLBnjRMiIvIJxgBakAcVRL28O/5BltCIYCnDAwC8yUjZ1EQBhcS4NiCxN7xn8+Y9ildz0= 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=uBsRBtPQ; 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="uBsRBtPQ" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736c7d0d35aso3405513b3a.1 for ; Fri, 04 Apr 2025 12:40:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795609; x=1744400409; 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=W9APkMUQeyaT/9vh/qCbtT1Smo+i6/9PSzpMaw1arTA=; b=uBsRBtPQhdRmOKm5QFV8rHVc7P7ZN1GMvewhb9Yiuoq+JxxwabvwlAHnSiFk3E0FLF Jza08AZGFCHCokFkYFLaqygxNAXHP6fLRYOvlqPnLB6rADMG2cOwJfITjclVYHMpIhxy MY5JYdsESJ+Q8wX1+iNveBOUb0m2U83OxrjVBK1eyI3ctP1d1/4DI3GSDraa6WePaH3K Z7PhqpO32iVx4cSg3eBnKLLnctPkaWZ8VWBJqGpPjQidmjhf0L4Rir1ntiIsAwKarHPb OHz3PgQIAAvYVOY3AwpLc7WAv2YxR/lCNzQsFwXVLrtT2W+GM/SJLXSPowQUmG3dfD9O aRgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795609; x=1744400409; 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=W9APkMUQeyaT/9vh/qCbtT1Smo+i6/9PSzpMaw1arTA=; b=ICLHGhWdwO89doQiQldL1Ps168do0+gta6eBJNWXBm5CtW141YJ/WxJRBb5414rtwg PXmm+A5DgUs/K7HznTwKWrWL2NyDBXWHb7wHlZkmcHJm1Ev/YkRyw8EJhPajsaNvaHWB 06IJGi2Zq218VYHng/YBEqaAJBYqOgy8nD2kWLWwE53lIi3iEGG192Ry2tcKFUGADQw9 8XPasibuv4kRW2ulojb12t9WYXJA5oi1NQemsTbB6wMv9w2JXkMgw76z7LMxvYdfttSW EQJYm1sYjt0BoHORXMNArrz2eni1uuxy82BAwub9D+VQEibmMctyTKb4O7V2bvcxd06E xv2w== X-Gm-Message-State: AOJu0YxFI1CMhpxvQIpxFCg3RxtzMYeekGU6P2b1ngvsxvT3uKxI5jxC h7Wj05c85nItjn/tLpbCJShKMbPF4w98zw0YQCmaGfiqe6x4u6ETUW1jYvMnV1oZpGIN6EMz4nz p/w== X-Google-Smtp-Source: AGHT+IHalJ84y5RZE6lIh9ca7jwC0g2+DKaK5EknSYSwFsFGl6+cpbF1yHJUby80bAdxdbQykyr6DzCtB1E= X-Received: from pfbhq12.prod.google.com ([2002:a05:6a00:680c:b0:736:ae72:7543]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1308:b0:736:bfc4:ef2c with SMTP id d2e1a72fcca58-73b69a3ef7emr853006b3a.0.1743795609327; Fri, 04 Apr 2025 12:40:09 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:28 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-14-seanjc@google.com> Subject: [PATCH 13/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. 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 9d3f17732ab4..8b07939ef3b9 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -247,7 +247,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31) #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) @@ -282,8 +282,6 @@ enum avic_ipi_failure_cause { static_assert((AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == AVIC_MAX_PHYSICAL_ID); static_assert((X2AVIC_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AVIC_MAX_PHYSICAL_ID); -#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 69bf82fc7890..f04010f66595 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -250,9 +250,9 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb) phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page)); phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page)); - vmcb->control.avic_backing_page = bpa & AVIC_HPA_MASK; - vmcb->control.avic_logical_id = lpa & AVIC_HPA_MASK; - vmcb->control.avic_physical_id = ppa & AVIC_HPA_MASK; + vmcb->control.avic_backing_page = bpa; + vmcb->control.avic_logical_id = lpa; + vmcb->control.avic_physical_id = ppa; vmcb->control.avic_vapic_bar = APIC_DEFAULT_PHYS_BASE; if (kvm_apicv_activated(svm->vcpu.kvm)) @@ -310,9 +310,12 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) if (!entry) return -EINVAL; - new_entry = __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 = __sme_set(page_to_phys(svm->avic_backing_page)) | + AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; WRITE_ONCE(*entry, new_entry); svm->avic_physical_id_cache = entry; @@ -912,8 +915,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, enable_remapped_mode = false; /* Try to enable guest_mode in IRTE */ - pi.base = __sme_set(page_to_phys(svm->avic_backing_page) & - AVIC_HPA_MASK); + pi.base = __sme_set(page_to_phys(svm->avic_backing_page)); pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode = true; From patchwork Fri Apr 4 19:38:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038835 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 B3538224889 for ; Fri, 4 Apr 2025 19:40:11 +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=1743795613; cv=none; b=sTV9sZ8lA2Wg8VI5JO7qo5CwdXTHMD/KCmkZCHu/HCnfCB7s49XD21I9YlLX1o+BQpekCKYAnKBhgnE/bnVaf7vBHDVCbbeMlalsbKR5sS2UBGmY/30zbMn6RNq4v+989A2dwzZx/GZwMtHpGQ4qDvtSidaMAr3QKLL16hSPGA4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795613; c=relaxed/simple; bh=Ny0M/z6O5m5Da4fXxsggB+MSjOkMNLlZ/4JPgRgDcHg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mMtUl6v0Frjzo6wYbuLX64UAIrK973ikp0T68+JaMMLtQyEV2dY0hYaClP8lkU1WZ9gFX0f2GwO4auZbN2IVv1+ftvhj1yBVti+yTKv8WxwqNZZoAMez8UANrcgY0DgkGCx6N0aG79bu2MS55daLIn+yyUkORJs6aZfZcy8n2ks= 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=qWKJJMy9; 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="qWKJJMy9" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736d30d2570so2142187b3a.1 for ; Fri, 04 Apr 2025 12:40:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795611; x=1744400411; 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=V9WmlXKIl3pG/vGS1FFPkbRmx6wE+LY/6fyIbxpmuWU=; b=qWKJJMy9VKEWK18gw3mQOnKr2/Ourf+dkZ5/sYkUBxtxGS/dA3Nz0zqQa22hzGD2SF azdp0HhQRNa2xIexptajFj6xMNIA4/xhYpCltgjPAaCH7CYlrg9Bgeyd36JDXUzoLJu4 bBKi6ibpHzNxkJqjsoVE96Nqbmg6r1iF9Oy4liQDiYazCebU2eL+GOOxY8TfRNN7L5oX qDjFnlClnhiPxtwfiq087Rh5uVS7DydC5Ut/e3S9lntiWNTRpdagOI1coS9J1mfEp/mY n4NCckNQEaZpsRL8mZWSDUcRr2ZCl+lJXzNBqDSD20W7BUTRRL1wgqGPc2ZzSiz9evzT LdiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795611; x=1744400411; 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=V9WmlXKIl3pG/vGS1FFPkbRmx6wE+LY/6fyIbxpmuWU=; b=Uyxk69jQBzfdrkEKthahsa7rVSXmXZpmv3Dlc7lwCH/2wdKqqQ6PerR88bP/xKG6RN IGfil/oHPqVNjO/K4CtWqQHU7G0kFEXJMenytmi/oiYthPVELsYTrjJ5GmR+y+JdIOra 61D/dsA5VErRD8lLrfLnvyOaSWFePpQFZkqf1jtTEgB6wx7rfSRJ7nlCm1tbpVanK1ox 8Vx7u2vGkvpNYVY7ar3W7ZEeAT+seB3jFQEkz5AEgVipcdrKIzNySulcV4VYMVu8b7yT vLWRlIo3L7UxLwg2S9yxQ2DJjBGpN0fbkYpCHcEYVgNYG6TZH7LYfFi32gMjxiLpUauF +nGw== X-Gm-Message-State: AOJu0Yz+o0r4KTxJymel6IBjeKPoYvOPXCrPXKvTC3QHXatInY4q6bOV Hzl5KQdS4ZxwPO9VS6iG8eeaUmMXZENZxNRtA5FgKnb+aVnEtrkrQFeyEawRHbFTC5FlxQSMNL5 MUA== X-Google-Smtp-Source: AGHT+IE27zbdMk+l2PrFIwUw1sH9cB4J4cN7CaOyjTIEfbMCPGg6FI6XQmyQTP9lTfdwAAei1fZlh+dM+/8= X-Received: from pfbjc3.prod.google.com ([2002:a05:6a00:6c83:b0:736:4313:e6bc]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:888c:0:b0:739:4a93:a5df with SMTP id d2e1a72fcca58-73b6aa6c7c8mr897212b3a.12.1743795611081; Fri, 04 Apr 2025 12:40:11 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:29 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-15-seanjc@google.com> Subject: [PATCH 14/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 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 f04010f66595..a1f4a08d35f5 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -243,14 +243,18 @@ int avic_vm_init(struct kvm *kvm) return err; } +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 = to_kvm_svm(svm->vcpu.kvm); - phys_addr_t bpa = __sme_set(page_to_phys(svm->avic_backing_page)); phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page)); phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page)); - vmcb->control.avic_backing_page = bpa; + vmcb->control.avic_backing_page = avic_get_backing_page_address(svm); vmcb->control.avic_logical_id = lpa; vmcb->control.avic_physical_id = ppa; vmcb->control.avic_vapic_bar = APIC_DEFAULT_PHYS_BASE; @@ -314,7 +318,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)); - new_entry = __sme_set(page_to_phys(svm->avic_backing_page)) | + new_entry = avic_get_backing_page_address(svm) | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; WRITE_ONCE(*entry, new_entry); @@ -854,7 +858,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 = to_svm(vcpu); - vcpu_info->pi_desc_addr = __sme_set(page_to_phys((*svm)->avic_backing_page)); + vcpu_info->pi_desc_addr = avic_get_backing_page_address(*svm); vcpu_info->vector = irq.vector; return 0; @@ -915,7 +919,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, enable_remapped_mode = false; /* Try to enable guest_mode in IRTE */ - pi.base = __sme_set(page_to_phys(svm->avic_backing_page)); + pi.base = avic_get_backing_page_address(svm); pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode = true; From patchwork Fri Apr 4 19:38:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038837 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 8DF87224B0E for ; Fri, 4 Apr 2025 19:40:13 +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=1743795616; cv=none; b=EOOTMkvZTHxkYQUxZlD94drLfrQiI6pqPkWLDbEiR22nlLiQVWSwKlt5/mK49xiYWWGwSGYOsgZnWUheBz5fbLme9IzOAeNhJIIVCPJ9qdo9kuc1MrMKtJJAiNLadRvKIj2duVl4eqJR0ILncN5puRlyHFYxoQmtnqO2Rd4Em/E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795616; c=relaxed/simple; bh=5nhNidq5kKCw8cPZzC/2EhhNf9DP5bsK6q2EHVi+AUQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uysOYWePuTfCA7rPqZ6n/CmpE5Nq+PFHPIccbZUuojtrWYQUVYgev8pUUWH5n09uOyAVy7lySR6WFTF68XVmA2ZhzzGHQH3Ku+tzUUC2XTmlaJGdgXcLl6nipxZzrLIz/kW/3aonqdBFaP8UT1HSYOXhb82PzosGEmnsdYNcmss= 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=qe3qkxU4; 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="qe3qkxU4" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736c89461d1so3576554b3a.3 for ; Fri, 04 Apr 2025 12:40:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795613; x=1744400413; 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=7R6CjC9nxAlVYVI0vNC7khUHTwDmf+yRrkIu6OvCEfg=; b=qe3qkxU4whFzZfvuVAUo68SzHiPxCNb1tSZP0UHpTxHI53jqfQ2vmfuwFi1cp0vJRw yt212uHXGS7Nh6OGVE2GRQetvbv/uc8koH7TvAuF6xyS4gn+D6lIN7pJFj8HcWJni/GQ oslqDAgbbI79V4bkPsvcSgsy8cy+5xc39tWc80+TfhG5mi5f/G3scCRW173+l1TIhEYU G6YGPynDrPmZK/xiLJOUmJpIQF/av8FJW1V9SoS9wt1vT8Vo+DOowW9Ke53GX0fxho6F UCF0PlYHx3i0HdDjVlk3N+zC2wfvVYh97sP8dDtESWkpWeoEdNQ3BPjx2cV55SbgINqX jL9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795613; x=1744400413; 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=7R6CjC9nxAlVYVI0vNC7khUHTwDmf+yRrkIu6OvCEfg=; b=UiNg53tNvTXqnMr06ehDYz0A0S6Fzk2Y896bR7baFkl8L6xoP6D2BxIBSMqbtXvk6q wP/5cS1Cd/C808zuHKJOHXOFPNmLoEKD8S/ER8atjURuVqPo9PDeiw7VRFN8BXfaxz0I bpL61w2x7fl3sL33ChZs8rIgKWpS39hbz1USNCDqOTLlpoQWjnsrITgMJXm/6R063sUO HXAIBiqlNoor88N+bEtzz9f9y+mPVTB4SDQQPJy3XkG8UcKLmUc6Re89bb+E0mgh/mTe 27RNqGVvL8j4vlNjEed2HTlWdIrjm0cy5xfI8TNzUf8qZMmr8tl9NId3P3L8cPd6+08T Y0qQ== X-Gm-Message-State: AOJu0YzO5fP/fs4WPUlUmpEHDhpqen7o9bOOVqBdY02yLCOvTCT1uJwz mEgRz9ps8mejEFFR5JdBxmr9Pijc7myBMVWqC0fqxdW/GHsbqlZHqcGncS6AuWIa9YmUM6MznR/ ubA== X-Google-Smtp-Source: AGHT+IE1EIppngaCdboWmAsyGLdJBGl6DmfjRI9oduZtmRDlzdb3XCSaEM1nhSyczDVhV3HThe+yTT0lId8= X-Received: from pfblm21.prod.google.com ([2002:a05:6a00:3c95:b0:739:45ba:a49a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4606:b0:736:592e:795f with SMTP id d2e1a72fcca58-739e6ff6b02mr4668862b3a.9.1743795612853; Fri, 04 Apr 2025 12:40:12 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:30 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-16-seanjc@google.com> Subject: [PATCH 15/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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=y 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. 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 a1f4a08d35f5..c8ba2ce4cfd8 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -245,7 +245,7 @@ int avic_vm_init(struct kvm *kvm) 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)); } void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb) @@ -290,7 +290,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) (id > X2AVIC_MAX_PHYSICAL_ID)) return -EINVAL; - if (!vcpu->arch.apic->regs) + if (WARN_ON_ONCE(!vcpu->arch.apic->regs)) return -EINVAL; if (kvm_apicv_activated(vcpu->kvm)) { @@ -307,8 +307,6 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) return ret; } - svm->avic_backing_page = virt_to_page(vcpu->arch.apic->regs); - /* Setting AVIC backing page address in the phy APIC ID table */ entry = 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 294d5594c724..1cc4e145577c 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -301,7 +301,6 @@ struct vcpu_svm { u32 ldr_reg; u32 dfr_reg; - struct page *avic_backing_page; u64 *avic_physical_id_cache; /* From patchwork Fri Apr 4 19:38:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038838 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 432A92206A6 for ; Fri, 4 Apr 2025 19:40:14 +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=1743795617; cv=none; b=gj+V4LYMHVQ5G6V49RkmDPCG5XsZD1EJzE5l7QWpUISz4m8byuwPxwqZREPrs6e4DBwag28p2oMNSWgezxjSrsHjrf1EWzZnkZVzmueYbNR0rzREINF4uKD6Y1sYU6+6eLcneO1qjbqOe8icKDjqP6hvAqyHxDg7AUAyKfVt2RU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795617; c=relaxed/simple; bh=5pcNK2/F2mUD0tglxtkkvS4t6gF1WjE0uv3B3h/LLIU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pI7msuywo0+CCoOWKPuye8Qpky99vGtiu2V/j6BKkE/jMqgPFsCW/oUl2nnSEkq+1+7JGaCHlSEKAKgewDl7X+MeD9Dqnfuy7MJqOfBrZm1AwqCzSjiwb0xMcygIijYaYi07k5T5uBaCmpvrz87/PUFkcefxLPmK7c4i9OQfX6A= 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=Sm8m+KMp; 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="Sm8m+KMp" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2265a09dbfcso29811935ad.0 for ; Fri, 04 Apr 2025 12:40:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795614; x=1744400414; 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=xGc410CdItV9i0XHHpZ/RMSaoI91XXBZ8UWJ2P2MUyk=; b=Sm8m+KMpEXHRY0LbMZcaS4gqHs9inVmDUniOFX9zK6bkWNwtjozjkZgBZLZ3E4iOdP WRqYj2izdeIED27bH7h+HA8nH46gmuuAdMvBD512JQn7gH/Le83cKWBDk+zzUyuRUNqt PTzgQrC6urgwyPleQMDal3bdVUJ3m78TBWSl1fVpwrgUD1NcBQLkm1fzEXa6fFgJiHx7 C18xVSI2lwQB5GWn1lld1cCsEYwgP8pvmUvaZ73mLicsBFHaxlj5GTEQcnogrAJokoOa X4cdRsabQKbIN/seASHMrVCDfoSWHlSIbN8RIYFuMeidHWMU+ZKz21drSMogKp9mG7Mx SXzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795614; x=1744400414; 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=xGc410CdItV9i0XHHpZ/RMSaoI91XXBZ8UWJ2P2MUyk=; b=XztJ2GGj+jYJUravfz1/dGZME3SNhu45VLEHRA2hsbsLxZBW95k7wlla43YEWfuCQJ o+CBPmk7bt8/5iaSKFsj3zGo7CpSMgY3QTwHypkkn+cbZ/4wP6d4nfp2Uvyuu9TE+OU+ D157QcU91L8UdnuvwxZRW8zmERmiR3oM+xSh6Y2GErlL+y5Lef4XpWesmgd21cdeQ8H0 +JPI7KcbnWEhkqdObA2cOgIF0BLI4G2KJgFX7g939buksmn9Ss0gl2Ctx37VF2xArJZF d9IziAQI4czs516A0Jde6xVrZMj1cKxGECveliVbSDu0HDgm4BJj2oNdw6kt5ljpcpwG Oinw== X-Gm-Message-State: AOJu0YwnRwsee8aXBmUthktOHDKk7h5kXrbb3PRvh/tNqLrxoDwpvc+r m9rOids6zy0uEQoLntX6MySkPDHyjdUUopKX2g7zrdhxBYInFutq1YDVtDOG+lP8f27ct6au6ot zzw== X-Google-Smtp-Source: AGHT+IHtyu5/n45SoUCdmyRwx78ZExMmcLv2DVkaCFXoZBFEW/oA9gYZTnVIAZvyMAiWmV0cdXBxBQzi80A= X-Received: from pfks21.prod.google.com ([2002:a05:6a00:1955:b0:730:848d:a5a3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:41d0:b0:223:4bd6:3869 with SMTP id d9443c01a7336-22a8a85adcbmr56111145ad.15.1743795614550; Fri, 04 Apr 2025 12:40:14 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:31 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-17-seanjc@google.com> Subject: [PATCH 16/67] 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, Maxim Levitsky , Joao Martins , David Matlack Inhibit AVIC with a new "ID too big" flag if userspace creates a vCPU with an ID that is too big, but otherwise allow vCPU creation to succeed. Rejecting KVM_CREATE_VCPU with EINVAL violates KVM's ABI as KVM advertises that the max vCPU ID is 4095, but disallows creating vCPUs with IDs bigger than 254 (AVIC) or 511 (x2AVIC). Alternatively, KVM could advertise an accurate value depending on which AVIC mode is in use, but that wouldn't really solve the underlying problem, e.g. would be a breaking change if KVM were to ever try and enable AVIC or x2AVIC by default. Cc: Maxim Levitsky Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 9 ++++++++- arch/x86/kvm/svm/avic.c | 16 ++++++++++++++-- arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 54f3cf73329b..0583d8a9c8d4 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1304,6 +1304,12 @@ enum kvm_apicv_inhibit { */ APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED, + /* + * AVIC is disabled because the vCPU's APIC ID is beyond the max + * supported by AVIC/x2AVIC, i.e. the vCPU is unaddressable. + */ + APICV_INHIBIT_REASON_PHYSICAL_ID_TOO_BIG, + NR_APICV_INHIBIT_REASONS, }; @@ -1322,7 +1328,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) 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 c8ba2ce4cfd8..ba8dfc8a12f4 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -286,9 +286,21 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) int id = vcpu->vcpu_id; struct vcpu_svm *svm = to_svm(vcpu); + /* + * Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC + * hardware. Do so immediately, i.e. don't defer the update via a + * request, as avic_vcpu_load() expects to be called if and only if the + * vCPU has fully initialized AVIC. Immediately clear apicv_active, + * as avic_vcpu_load() assumes avic_physical_id_cache is valid, i.e. + * waiting until KVM_REQ_APICV_UPDATE is processed on the first KVM_RUN + * will result in an NULL pointer deference when loading the vCPU. + */ 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_BIG); + vcpu->arch.apic->apicv_active = false; + return 0; + } 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 1cc4e145577c..7af28802ebee 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -715,7 +715,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) \ ) bool avic_hardware_setup(void); From patchwork Fri Apr 4 19:38:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038839 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 BC65522578D for ; Fri, 4 Apr 2025 19:40:16 +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=1743795619; cv=none; b=Pd3MgJbTbyrJnph4DJ4wojBV+PNTyg0hK9eevGii81wF7bRr3DPxE5stA3NBgo+8uTT/dghzZXAw6OsaFSObwCErtWoFi3yifKpID4lcVrTKto02Lr/cDHVdRpqr0t7CRPXhCjegKlL5red3pFEziDx3Lt9cpR9CdU+pXadruOg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795619; c=relaxed/simple; bh=8CNUPuQxWruAqazLiQ2U7Se/O49wV/nzU7fAMxK6IVw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KBJz1oGtbOgmvR7g7rowtMkIuL3JIMYRD1SSIWyBdRqcvZ9xsBY7CLGicIPNfYaxmyjTs2/0Ifov3KJjFORJc1ahwAx9wBpPTaOSfXFukTgRb2tZVwXc+NpN5E0gKd1reoxspM8WWYaC7jw2UqYAbDLBS9s6sQk7aA+fyIVyKww= 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=1pN65nSV; 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="1pN65nSV" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736c0306242so3285780b3a.1 for ; Fri, 04 Apr 2025 12:40:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795616; x=1744400416; 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=HPznh6CGxUFhqaGN+tuF1KZuEdkc3IM8iRC+hka4d9I=; b=1pN65nSVS2GahCTQdEV5p277Xunym8GA5pPvb3mn/dhq/gkiSaQhCvoTJzMv8QU/7H zUPbLfqdmda8r8bbWRYv6NS0eO2czKXIfhp9d5oqXhMAW4mO+DnCQKFjOsxmyRXIIDL2 cHIO59zrJ9i7PnPN/wsPDdd55ZuU2UNGTH4mFao5NFM93BvKo0OnBTQ6By9Y/y2Nqwh/ WxFKuu/x5xYPjmlG5+NTD/7mKfhidUoxjA1Zi8N/nuBKHSyAHT+0pej2Q3b0TWRxYi0z s/rIQpF8ELF0IleRujmCThprLJ2ryfnlvZ2cteXjrEQ/MCV2qAajZnUXW1lxCDgKfbD6 hNQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795616; x=1744400416; 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=HPznh6CGxUFhqaGN+tuF1KZuEdkc3IM8iRC+hka4d9I=; b=HoZm1l7hSabfKbZI7mL2/iWXiz4SuJ/vt1q5rUdbYj1/nVjd93/pBfyIK0W58ygobC XJjfjOgWJj4y63eysyPGkQfh59F0tKfzgXT+ZZjHTpSRyyK+B8aD6RRPowNqHzmUTBvI tDxSoQweqb248BumcMSpqmI6akDZcgzNX2dHkE8Mo6bzfNUlESqKaKZrolC8EftNjMj1 oCVXOCCPJOM3AYLnmG8cmR9fydX9OohRPBXsWIGH803KUeithnTTzbXBQMyWlCrMxIwP s5DXOFE1dlYZmAyckWb+aZkt53Mk9Un39oC+g31Z/2xWvD+ArtrrrLzwgfzn+z5fPe2N uI3A== X-Gm-Message-State: AOJu0YwVWFDFgiwLS63x/mSlnJLSwwriUFYqYdz7k3E7esqOyGPG+Cgj WTk1nxUxEp+iNXxjfkQuYiI1wRNVk34bhQcOGAuc6DSDYHUsHmfKrW/PdMzdwilAAuEWqt/e29G WVQ== X-Google-Smtp-Source: AGHT+IHdDhY3q1PoCzEN393Qre591g1t5JRHt7vREsmAZfu9y83GvrelcK4mphnOUkWNilXZrFXu1h6Tod0= X-Received: from pfbhb19.prod.google.com ([2002:a05:6a00:8593:b0:737:79:e096]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:244c:b0:736:546c:eb69 with SMTP id d2e1a72fcca58-739e492c849mr5818791b3a.9.1743795616257; Fri, 04 Apr 2025 12:40:16 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:32 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-18-seanjc@google.com> Subject: [PATCH 17/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 47 +++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index ba8dfc8a12f4..344541e418c3 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -265,35 +265,19 @@ void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb) avic_deactivate_vmcb(svm); } -static u64 *avic_get_physical_id_entry(struct kvm_vcpu *vcpu, - unsigned int index) -{ - u64 *avic_physical_id_table; - struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); - - if ((!x2avic_enabled && index > AVIC_MAX_PHYSICAL_ID) || - (index > X2AVIC_MAX_PHYSICAL_ID)) - return NULL; - - avic_physical_id_table = page_address(kvm_svm->avic_physical_id_table_page); - - return &avic_physical_id_table[index]; -} - static int avic_init_backing_page(struct kvm_vcpu *vcpu) { - u64 *entry, new_entry; - int id = vcpu->vcpu_id; + struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); struct vcpu_svm *svm = to_svm(vcpu); + u32 id = vcpu->vcpu_id; + u64 *table, new_entry; /* * Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC - * hardware. Do so immediately, i.e. don't defer the update via a - * request, as avic_vcpu_load() expects to be called if and only if the - * vCPU has fully initialized AVIC. Immediately clear apicv_active, - * as avic_vcpu_load() assumes avic_physical_id_cache is valid, i.e. - * waiting until KVM_REQ_APICV_UPDATE is processed on the first KVM_RUN - * will result in an NULL pointer deference when loading the vCPU. + * 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)) { @@ -302,6 +286,9 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) return 0; } + 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; @@ -320,9 +307,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) } /* Setting AVIC backing page address in the phy APIC ID table */ - entry = avic_get_physical_id_entry(vcpu, id); - if (!entry) - return -EINVAL; + table = page_address(kvm_svm->avic_physical_id_table_page); /* Note, fls64() returns the bit position, +1. */ BUILD_BUG_ON(__PHYSICAL_MASK_SHIFT > @@ -330,9 +315,9 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) new_entry = avic_get_backing_page_address(svm) | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; - WRITE_ONCE(*entry, new_entry); + WRITE_ONCE(table[id], new_entry); - svm->avic_physical_id_cache = entry; + svm->avic_physical_id_cache = &table[id]; return 0; } @@ -1018,6 +1003,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; + 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 @@ -1058,6 +1046,9 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) lockdep_assert_preemption_disabled(); + 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 From patchwork Fri Apr 4 19:38:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038840 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 A8D7F226CE6 for ; Fri, 4 Apr 2025 19:40: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=1743795621; cv=none; b=G9wi8LlkU49myV8Rbhqla8TqGODY4nT7GKLJKGQEgInbma3EcnDgYOwgwcUGAjz3IYkffsBuNiRuueU7Pls2jXQ0Yj9RhQFDgw/HHj5CQx6ZlA+VUCk6fAt0Tbj0+x2ZWDc6KsHGSvnTGdRzUmCIZkjaaw6WHY+XJ7UiYIptKEU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795621; c=relaxed/simple; bh=J6mC0nFXHJAOtYEIFo2PwmBBEXnUBJqNHuBrgJLfJvQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VJcn+dsPjTB8oZW5pT4If38AdnPAuSxoyog0FUOEuiHoseAY8raNwbGEFKLPI8qcojUawqrF4p9kdCHosG9oDyGKWwfOZ3miR556rGEORwfsbSJmGw+rtU8ncwxmsvHjd+npHTP2b2uxnw9fBRdwupoR4WG+0WmYZIt3htcX/vs= 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=Zu7aIEIt; 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="Zu7aIEIt" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736abba8c5cso3046515b3a.2 for ; Fri, 04 Apr 2025 12:40:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795618; x=1744400418; 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=mBYvsppdkEsCC9jB3KHtPaQFQe07zBHNgFlVF26LQEY=; b=Zu7aIEItRjtzR4mWiLHiOOd7nBetF5U1ujJKr0QwTk9LtGLuwq8Cz/mgCemJpHQD9d 4ugvCDkjGuTJpFbrR7n7u8gVuITVr1aTXo5+TV2tC1ORHJYat7HCbieD8o8Y4CLdckCi FA/h5k3KGmyIghcP37lTGYJY2w2zM2WG1cfdJsPhPj9h4mhXVJ9+RElB2EBxG9at8Sqp VvdT6tjzndhcEvPd3iflnq34QtbiHByjJGwY8TFtLguJXrk4Y8ruXvUqLxkku2vIQ2a2 9gag6WlKM8RSmPUAmndJnzKO8msOTWgm+dGIv3iSUaReMJTVcpit9oXoJuFtV5Wde8s6 xYQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795618; x=1744400418; 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=mBYvsppdkEsCC9jB3KHtPaQFQe07zBHNgFlVF26LQEY=; b=wdhHq5uRaCg4xkWdgCo6dN5J7OiiMcdzG8pHGpaFI3qfqQEPAAjwTYJ4UcvpekgDXI nX6skdevlqgdMTcRYNO2SIP7U+p0oANc2WnAwkSVmzVQXrWsw3GfkT4PzE0f6VhglOFy KfRr41ZPKBC6Xozmts+E+Qqo6SWRzATkx8180mHfKmyD+U8kl9xymOK1o3lq+ROi+leT C6Tf0nJd1/pN8UfR3yKiGhXBHCv8vJL9tG1OkIMEW5/9PdQ+Uugm0dw+xjV7qs/HTY9m PumeTyQLrDGmLjo32IWc92sTv0G61ut8HcU7ddDQefONNBYAcXTYbISsuKkrkWbHr8FH 0l9A== X-Gm-Message-State: AOJu0Yz73E+W7XAd0YQdTQ0vGtKpu507sQe7X4l41/nrvKA3Wnvsgcmp /AU2pv90Y7mgfnvzOdV9hJ/OBNE8ipzT9P72Z6dlU8fO4e2mRm85+OC8YRcqdktKXybhVWZdZXJ hkg== X-Google-Smtp-Source: AGHT+IHlJ1S/97WVKpR7s7eWnhf8FHowO+clanPhx/S3rl8+pkCvYLrtJLRFmIybzKrDhNaukmJeObbrPxI= X-Received: from pfbjc20.prod.google.com ([2002:a05:6a00:6c94:b0:736:451f:b9f4]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4606:b0:736:592e:795f with SMTP id d2e1a72fcca58-739e6ff6b02mr4669074b3a.9.1743795618072; Fri, 04 Apr 2025 12:40:18 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:33 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-19-seanjc@google.com> Subject: [PATCH 18/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. 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 344541e418c3..ae6d2c00397f 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -181,10 +181,8 @@ void avic_vm_destroy(struct kvm *kvm) if (!enable_apicv) return; - 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); spin_lock_irqsave(&svm_vm_data_hash_lock, flags); hash_del(&kvm_svm->hnode); @@ -197,27 +195,19 @@ int avic_vm_init(struct kvm *kvm) int err = -ENOMEM; struct kvm_svm *kvm_svm = to_kvm_svm(kvm); struct kvm_svm *k2; - struct page *p_page; - struct page *l_page; u32 vm_id; if (!enable_apicv) return 0; - /* Allocating physical APIC ID table (4KB) */ - p_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); - if (!p_page) + kvm_svm->avic_physical_id_table = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); + if (!kvm_svm->avic_physical_id_table) goto free_avic; - kvm_svm->avic_physical_id_table_page = p_page; - - /* Allocating logical APIC ID table (4KB) */ - l_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); - if (!l_page) + kvm_svm->avic_logical_id_table = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); + if (!kvm_svm->avic_logical_id_table) goto free_avic; - kvm_svm->avic_logical_id_table_page = l_page; - spin_lock_irqsave(&svm_vm_data_hash_lock, flags); again: vm_id = next_vm_id = (next_vm_id + 1) & AVIC_VM_ID_MASK; @@ -251,12 +241,10 @@ static phys_addr_t avic_get_backing_page_address(struct vcpu_svm *svm) void avic_init_vmcb(struct vcpu_svm *svm, struct vmcb *vmcb) { struct kvm_svm *kvm_svm = to_kvm_svm(svm->vcpu.kvm); - phys_addr_t lpa = __sme_set(page_to_phys(kvm_svm->avic_logical_id_table_page)); - phys_addr_t ppa = __sme_set(page_to_phys(kvm_svm->avic_physical_id_table_page)); vmcb->control.avic_backing_page = avic_get_backing_page_address(svm); - vmcb->control.avic_logical_id = lpa; - vmcb->control.avic_physical_id = ppa; + vmcb->control.avic_logical_id = __sme_set(__pa(kvm_svm->avic_logical_id_table)); + vmcb->control.avic_physical_id = __sme_set(__pa(kvm_svm->avic_physical_id_table)); vmcb->control.avic_vapic_bar = APIC_DEFAULT_PHYS_BASE; if (kvm_apicv_activated(svm->vcpu.kvm)) @@ -270,7 +258,7 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); struct vcpu_svm *svm = to_svm(vcpu); u32 id = vcpu->vcpu_id; - u64 *table, new_entry; + u64 new_entry; /* * Inhibit AVIC if the vCPU ID is bigger than what is supported by AVIC @@ -286,8 +274,8 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) return 0; } - 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); if (WARN_ON_ONCE(!vcpu->arch.apic->regs)) return -EINVAL; @@ -306,18 +294,16 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) return ret; } - /* Setting AVIC backing page address in the phy APIC ID table */ - table = 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)); + /* Setting AVIC backing page address in the phy APIC ID table */ new_entry = 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); - svm->avic_physical_id_cache = &table[id]; + svm->avic_physical_id_cache = &kvm_svm->avic_physical_id_table[id]; return 0; } @@ -451,7 +437,7 @@ static int avic_kick_target_vcpus_fast(struct kvm *kvm, struct kvm_lapic *source if (apic_x2apic_mode(source)) avic_logical_id_table = NULL; else - avic_logical_id_table = page_address(kvm_svm->avic_logical_id_table_page); + avic_logical_id_table = kvm_svm->avic_logical_id_table; /* * AVIC is inhibited if vCPUs aren't mapped 1:1 with logical @@ -553,7 +539,6 @@ unsigned long avic_vcpu_get_apicv_inhibit_reasons(struct kvm_vcpu *vcpu) static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat) { struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); - u32 *logical_apic_id_table; u32 cluster, index; ldr = GET_APIC_LOGICAL_ID(ldr); @@ -574,9 +559,7 @@ static u32 *avic_get_logical_id_entry(struct kvm_vcpu *vcpu, u32 ldr, bool flat) return NULL; index += (cluster << 2); - logical_apic_id_table = (u32 *) page_address(kvm_svm->avic_logical_id_table_page); - - return &logical_apic_id_table[index]; + return &kvm_svm->avic_logical_id_table[index]; } static void avic_ldr_write(struct kvm_vcpu *vcpu, u8 g_physical_id, u32 ldr) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 7af28802ebee..4c83b6b73714 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -119,8 +119,8 @@ struct kvm_svm { /* 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; struct kvm_sev_info sev_info; From patchwork Fri Apr 4 19:38:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038841 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 82EB4226D14 for ; Fri, 4 Apr 2025 19:40:20 +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=1743795622; cv=none; b=WtPBppXwRbihWoqh+gCRi6zGh+yR35l9uzA/czAEvAPoRGIzCMUmkJ0UBw9zNDRNCvZfZXBvS9knX9kPen9HP4q8y6w2437HR47Enth5POuuZjM86bOKbecP9J/BTUZWkud0QOcHqCoMz2MJvYR6QvGzRr6kUwS+RZmRwdUrUTQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795622; c=relaxed/simple; bh=B5b++zvRQrYCt3P1/JdYmNo4yfWlq1mTjCkluzYI5Uk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Qs0W4TfiAu5qYHbUdLtM14mZZTK9n6BiULKg8tYaLdaqt1ro9UZqPFJ2IJMJj8WZ3i+ARomjhFsRP9kTfM1nA1s8wOwTI9Tw0tRy9pT2sEmlPU6Y9uOAui02K2OkafkU3zuS6Z0as+KjQ9gu9R0C8tDRldNs38RpZGFlhfy6Sb8= 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=2NAJuUib; 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="2NAJuUib" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736cd27d51fso2260589b3a.2 for ; Fri, 04 Apr 2025 12:40:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795620; x=1744400420; 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=zH81SK0PLF89dcoZf/Fkldanv1dFQfCPkTGC85oMJ5g=; b=2NAJuUibHLrgzeFJQYjSYbDHMD1v+XPDcikljFesIHoD9x2G+p6+Q0qZ+ksCJIr6M3 ijvOjIuQRuLtHF48D39bmkP5lrVZug9Aow+vxYATz8uDYxZBg5RsX/JB5hSnH/xOh0kS X524Iv+LSdsv4eh/GMDFjdapQcqXui/DPMObhJthe51STtjXoER+rdrezFA8skUwsbMb GcIR7jkIMA0Ef+aIaIpKNMoxxy/ux4Y1Ttgf34vLoMWSWcRY5PIiOkkDlG9bpqR9YGWT q+o48OOIWSud0YkkQ8uH/M9LJRafR2XOhlbku/sUcCPW9K6QxXI/aQ9gnRj65cPcmI34 jNuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795620; x=1744400420; 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=zH81SK0PLF89dcoZf/Fkldanv1dFQfCPkTGC85oMJ5g=; b=CtqldSxPJK4ssN7S1+6ovw2AMGlH3YTOldxyiRD9NEgxs8sr50JmdDWQ3IygrCBEGt PxD+zw0Tu/f/zeDiomDpTWbJpdugnevvzC5O4QYa5iOArOr2n/wSbbTNnUT7wHsVEVil EQ17j7AcEaNASzAOvA/V43Tdwyq+/hGJlvqrsOooyvQ+eithmchsmZ5tQsGhbf9nykAh g3scVoPmpbUz8AN47fzfNoBAlh18ytYz4U7D1N1WDvn5Q9Fx4lY+HbRDrC8lhR9rtb0J gIAg6FRF4wz33X7/HrqQ+CngNxXY5ueKstTsN81ahjjt+uhmO3++kXDo3qFvnCjJpUNM VPNw== X-Gm-Message-State: AOJu0YyBi014FN+wJYcz4T4xcT50kU9V0b1oqztvWObfRuwpBpEuN8GP d1fP6DLO7irfHSKahkfzj2KcTDLTvfe1o4ZRMtFvsC63D2zfECVVqDrkwa+Wfli5/MIZjE9DAK7 XRg== X-Google-Smtp-Source: AGHT+IGVSydf5w/q8WpKQOUOH+7EuLOse28u3r7PdtsTQmzo1sfNdIuVWViLN0PPNIxHwzX4Yipa+c/f+dQ= X-Received: from pfmx24.prod.google.com ([2002:a62:fb18:0:b0:736:5012:3564]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2e06:b0:736:2a73:675b with SMTP id d2e1a72fcca58-739e7102e92mr4843046b3a.19.1743795619827; Fri, 04 Apr 2025 12:40:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:34 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-20-seanjc@google.com> Subject: [PATCH 19/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 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 ae6d2c00397f..c4e6c97b736f 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -303,8 +303,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); - svm->avic_physical_id_cache = &kvm_svm->avic_physical_id_table[id]; - return 0; } @@ -779,13 +777,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 = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + struct kvm_svm *kvm_svm = to_kvm_svm(kvm); unsigned long flags; u64 entry; if (WARN_ON_ONCE(!pi->ir_data)) return -EINVAL; - irqfd->irq_bypass_vcpu = &svm->vcpu; + irqfd->irq_bypass_vcpu = vcpu; irqfd->irq_bypass_data = pi->ir_data; spin_lock_irqsave(&svm->ir_list_lock, flags); @@ -796,7 +797,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 = READ_ONCE(*(svm->avic_physical_id_cache)); + entry = 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); @@ -976,17 +977,18 @@ avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r) void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { - u64 entry; + struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); int h_physical_id = kvm_cpu_get_apicid(cpu); struct vcpu_svm *svm = to_svm(vcpu); unsigned long flags; + u64 entry; lockdep_assert_preemption_disabled(); if (WARN_ON(h_physical_id & ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK)) return; - if (WARN_ON_ONCE(!svm->avic_physical_id_cache)) + if (WARN_ON_ONCE(vcpu->vcpu_id * sizeof(entry) >= PAGE_SIZE)) return; /* @@ -1008,14 +1010,14 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) */ spin_lock_irqsave(&svm->ir_list_lock, flags); - entry = READ_ONCE(*(svm->avic_physical_id_cache)); + entry = READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]); WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); entry |= 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); avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, true); spin_unlock_irqrestore(&svm->ir_list_lock, flags); @@ -1023,13 +1025,14 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) void avic_vcpu_put(struct kvm_vcpu *vcpu) { - u64 entry; + struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); struct vcpu_svm *svm = to_svm(vcpu); unsigned long flags; + u64 entry; lockdep_assert_preemption_disabled(); - if (WARN_ON_ONCE(!svm->avic_physical_id_cache)) + if (WARN_ON_ONCE(vcpu->vcpu_id * sizeof(entry) >= PAGE_SIZE)) return; /* @@ -1039,7 +1042,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 = READ_ONCE(*(svm->avic_physical_id_cache)); + entry = READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]); /* Nothing to do if IsRunning == '0' due to vCPU blocking. */ if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) @@ -1058,7 +1061,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) avic_update_iommu_vcpu_affinity(vcpu, -1, 0); entry &= ~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); spin_unlock_irqrestore(&svm->ir_list_lock, flags); diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 4c83b6b73714..e223e57f7def 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -301,7 +301,6 @@ struct vcpu_svm { u32 ldr_reg; u32 dfr_reg; - u64 *avic_physical_id_cache; /* * Per-vcpu list of struct amd_svm_iommu_ir: From patchwork Fri Apr 4 19:38:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038842 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 37016224AEE for ; Fri, 4 Apr 2025 19:40:22 +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=1743795623; cv=none; b=KGDc6TRRTeGaB/Wykddn46XEzdBlKw6rxkZYVnuXEi05XWAjTgAjicyZaeGHPOLsU6lgVR42PKm9ggPColcqfzD978Ozw+QJhJECpsyvtM0BRAP5qjgxCKjio15giv9xZrN188gHlsoSOs0syVJTFYfvKU00a1XSXKR2DrmQvo4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795623; c=relaxed/simple; bh=ncnSIRMStjRgoaLxhUROgr9SOC8QWiUmuvo8MAqoaW0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MDeqEfqzC2o7XgST57DbqHXu1PEKagVS89kISzDZF/YhsLZF6XW9xNGij1Ayp89JAe15XE9NDgZJauGryZSh7wACqF1BiCKWopYJxdCvBxViUsd3UzK8xBk5rPa50OXBD2woabi6/ztIUyT0gsQwOTMgz/gZyi/k4Lw8hP01QS8= 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=0zxUXS5V; 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="0zxUXS5V" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2241e7e3addso21001175ad.1 for ; Fri, 04 Apr 2025 12:40:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795621; x=1744400421; 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=VCMo/RGlrbOJASvtC7+TsYQlxqkzXA/5CTBjvXO9fqA=; b=0zxUXS5VvheRhTDTt4E5k3pwatpRkZyKHSMSZmD4QylBBBgMk3QgSgF9mp30QgaqA4 JPg/4E5AfGRMSRolkKQOz5OnTdCae9BAUJX9AB3nhLoO7BTImC4pecF331gQKauUAxpH LM+xJFyC7Rt7QaoepixUnrBcYnct6Yz/NHrf4zGvegLNTslPJwZbEWgkuG+FkhM2lt7T uHF6HKYsNyyGuF2aqM30nF7QsgDNyaO0h+u2AepYWuk1EYlqp4kJBh9hVR9VLa/S5Vbp WBW6K7v7wccXnb8lAPNERJAB9ngA5dck6Dyt92ojZ+bJKMMnMeL/OjIXMtiMJPl1duTJ fbgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795621; x=1744400421; 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=VCMo/RGlrbOJASvtC7+TsYQlxqkzXA/5CTBjvXO9fqA=; b=vdimBa3nlpEzMIVfSZ5pJynGTjpb+KmiPVUQTAX0MP9QpWfGvH7ZQ04R2cNdSPmgTD Y+o7FYICJUK4lQynazpSB/BuDvMWVQ1/JGPS9MpHV5mfZXgkfEy4NvMhZ2fcAJugh1wo 5wbUMGjdY0T0eyxkkwGrJRgM1NTNDa989ptJqc46AxyW9PVDlYDuqL1V6jwSDL/2FWNy oGimmB9x4VSqqhqHuotfa7+JyptgxfqNPtvwB4ZlxpzeIgsdxg1HaqZ1I3CZ6yhLzwA0 i7hET+Ej0WRSUbSIxKz0fCdBnKXtJAufy68H+IVz2vU9sq805Vy/WKeVuZ/vCjZiCaXP Wzpg== X-Gm-Message-State: AOJu0YyVbOQAL8TL5L7SaQo3OS1q0zCrP5EVWqq1tVKTjr1VlU31/Vzl DN56NXD7mD/tQbNr8Uqvhtw7mBwnjzdLAk6TxlsTnLWYnoDtcJkXaLuDOxVG6wU2nFyRmoAqDjz I8w== X-Google-Smtp-Source: AGHT+IE5XbiMqRGlDARtEBjYgWidaO1ecxsBHLfL544ofCTr9/DpC8oice4f7Zykt01Y3R60ZjSJF5IkHjo= X-Received: from pfbbe6.prod.google.com ([2002:a05:6a00:1f06:b0:736:aeb1:8ace]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ccd2:b0:224:1af1:87f4 with SMTP id d9443c01a7336-22a8a06b3d0mr64838025ad.22.1743795621644; Fri, 04 Apr 2025 12:40:21 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:35 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-21-seanjc@google.com> Subject: [PATCH 20/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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_host.h index 0583d8a9c8d4..85f45fc5156d 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1932,6 +1932,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; diff --git a/arch/x86/kvm/vmx/capabilities.h b/arch/x86/kvm/vmx/capabilities.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; #define PT_MODE_SYSTEM 0 diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index ac7f1df612e8..56b68db345a7 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -111,8 +111,6 @@ static bool __read_mostly fasteoi = 1; module_param(fasteoi, bool, 0444); module_param(enable_apicv, bool, 0444); - -bool __read_mostly enable_ipiv = true; module_param(enable_ipiv, bool, 0444); module_param(enable_device_posted_irqs, bool, 0444); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 23376fcd928c..52d8d0635603 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -227,6 +227,9 @@ EXPORT_SYMBOL_GPL(allow_smaller_maxphyaddr); bool __read_mostly enable_apicv = true; EXPORT_SYMBOL_GPL(enable_apicv); +bool __read_mostly enable_ipiv = true; +EXPORT_SYMBOL_GPL(enable_ipiv); + bool __read_mostly enable_device_posted_irqs = true; EXPORT_SYMBOL_GPL(enable_device_posted_irqs); From patchwork Fri Apr 4 19:38:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038843 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 EFF5A227EBD for ; Fri, 4 Apr 2025 19:40: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=1743795627; cv=none; b=sNm8ylHm9XIb5mimH+++oS6NnmSpB5gkb2RZaCsdFKoOtbZrIVtYkUueqPzNBSHVt45kaMqytw6kUxAN3LceCYTo8TA5LVk+PiaIFrfI/bs2JnnextcGo5e5IhAB4uRkeel/VGkmNDxDR8PqcyRfQvzi8PwZaDivScOyKQhlC4Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795627; c=relaxed/simple; bh=5pXZ3KhcB5o+xbYbyX54m7GfflzbLKKvgK9HfTcDpYY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mccG9k6OpjgsZhSSoezNnQrdIUk6Qo3mTcGOYxmzjlJf3K7/M25UOhHx22qJOT+cQBHuUqaM8fSHtS7g/OOtDaBSuLdA1saQzrE5mhrjBQmYqZjdPdhc2HHrSZ5ClPiqJ7RVk1pjGxarggfKoQoZ5eogheuSqORb7yScoTumv/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=fDmLFnyb; 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="fDmLFnyb" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ff6943febeso1850847a91.0 for ; Fri, 04 Apr 2025 12:40:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795623; x=1744400423; 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=0B7t2SIwhqflBHejHEDsNeeI2B/K5AMETQZXAxeeglw=; b=fDmLFnyb9Uni0x9nQAewF0eP68Oy3fZawvq2BR4YfbPO2p0x8Y9CZIOVpOhxs+u4YX CjFH4Bsjye7/O3Tb9y7pOqHw7OBbKYdPM9vfSkSK3Pt0i1L5izYcEVRL5ZaNenccrbrE qotfm5VOUSR1HzJYAXOmuxerXsyz37UUFwEI7Eegq7+aZJGXU1htXgXKatkaK7fPFKZ3 VY9syC+5kkI811tLmMO9DZJPZ5XIin2rU5kk+03bySbR9bZ17SR5ZHNxBKW0QXl9i0oE jR31YhNxaU1wwRwdMYk0mWIfv7qb2j8QSoa9uRXhm4kypZldzpShFkKXKUguFMDvTBgH Vcgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795623; x=1744400423; 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=0B7t2SIwhqflBHejHEDsNeeI2B/K5AMETQZXAxeeglw=; b=FDl2FbstAv3XyQwxuEFlBAaTbtXZrEdeossaCq13GVrgK8vWBKR8jbuLAoS7CzLPuh KnZMC0YmsssUF5NjkOThwmlzt2vgn2Xaahd6Wml9QNk8x6rFWp9OXclRTyCRMcffvAUq hPd0fdJEBXl1lTzAN4DWirVk9fjdJB//+PLeGNZ3okABD13cmZBaMgj3T9QR5I7TCXZX +SzKCcZ5qhbrbz2Jlr0/7MAB+Ydm/tM9hnVuV10ocwmC4zughytXHJvZJWAjBd5/kPux dtELTR3eXaHbq0CEI9DyEtI0GFRkWn/4zBlhbP/rOK75Wh1k6DewuH+bfinz+n1XJxEw KYAQ== X-Gm-Message-State: AOJu0Yx3nzaxBPbDKNsleGJS7oGkXOERctCMHA/xAiiUIFzT3Wni7XPn 5EpTAi5CZtz2050bioFa+Sl7KZRKt4MDkmFj7RrZLvRsiLqwdbKOV840LqYnMe+zn3MTqR/9W2G 1SQ== X-Google-Smtp-Source: AGHT+IE/kINVfoZrJ4qY9CHehtr/vzhchg2tVy6B+yeCOOea3l97XGuuqzOWftR5UeAUcXh68E3v1s4JzR8= X-Received: from pfbbe17.prod.google.com ([2002:a05:6a00:1f11:b0:730:9951:c9ea]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5387:b0:2fa:3b6b:3370 with SMTP id 98e67ed59e1d1-3057a68c61amr11505416a91.16.1743795623377; Fri, 04 Apr 2025 12:40:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:36 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-22-seanjc@google.com> Subject: [PATCH 21/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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=1, 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 | 9 +++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index c4e6c97b736f..eea362cd415d 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -301,6 +301,13 @@ static int avic_init_backing_page(struct kvm_vcpu *vcpu) /* Setting AVIC backing page address in the phy APIC ID table */ new_entry = avic_get_backing_page_address(svm) | AVIC_PHYSICAL_ID_ENTRY_VALID_MASK; + svm->avic_physical_id_entry = 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); return 0; @@ -778,8 +785,6 @@ static int svm_ir_list_add(struct vcpu_svm *svm, struct amd_iommu_pi_data *pi) { struct kvm_vcpu *vcpu = &svm->vcpu; - struct kvm *kvm = vcpu->kvm; - struct kvm_svm *kvm_svm = to_kvm_svm(kvm); unsigned long flags; u64 entry; @@ -797,7 +802,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 = READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]); + entry = 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); @@ -1010,14 +1015,26 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) */ spin_lock_irqsave(&svm->ir_list_lock, flags); - entry = READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]); + entry = svm->avic_physical_id_entry; WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; + svm->avic_physical_id_entry = entry; + + /* + * If IPI virtualization is disabled, clear IsRunning when updating the + * actual Physical ID table, so that the CPU never sees IsRunning=1. + * 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 &= ~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); spin_unlock_irqrestore(&svm->ir_list_lock, flags); @@ -1042,7 +1059,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 = READ_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id]); + entry = svm->avic_physical_id_entry; /* Nothing to do if IsRunning == '0' due to vCPU blocking. */ if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) @@ -1061,7 +1078,10 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) avic_update_iommu_vcpu_affinity(vcpu, -1, 0); entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; - WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); + svm->avic_physical_id_entry = entry; + + if (enable_ipiv) + WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); spin_unlock_irqrestore(&svm->ir_list_lock, flags); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index def76e63562d..43c4933d7da6 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -230,6 +230,7 @@ module_param(tsc_scaling, int, 0444); */ static bool avic; module_param(avic, bool, 0444); +module_param(enable_ipiv, bool, 0444); module_param(enable_device_posted_irqs, bool, 0444); @@ -5440,6 +5441,7 @@ static __init int svm_hardware_setup(void) enable_apicv = avic = avic && avic_hardware_setup(); if (!enable_apicv) { + enable_ipiv = false; svm_x86_ops.vcpu_blocking = NULL; svm_x86_ops.vcpu_unblocking = NULL; svm_x86_ops.vcpu_get_apicv_inhibit_reasons = NULL; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index e223e57f7def..6ad0aa86f78d 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -302,6 +302,15 @@ struct vcpu_svm { u32 ldr_reg; u32 dfr_reg; + /* + * 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=1. + */ + u64 avic_physical_id_entry; + /* * Per-vcpu list of struct amd_svm_iommu_ir: * This is used mainly to store interrupt remapping information used From patchwork Fri Apr 4 19:38:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038844 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 D3A23229B05 for ; Fri, 4 Apr 2025 19:40:25 +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=1743795627; cv=none; b=FlGNTjUD3KVe1myMPCp0B0DJNxnqaF6rkvCEcvj5N6Lb1x+JiJqbaVme0CGvyzeXW27FHITJ/zxGOTn9ckz4t1B1SaGsC0HtkC9UYIjUxr6wA403z38tVoz12Ag+uL/tTUBWLuq3RyFQgVlZmL8u/X0ZfVc6iSCUtQEX70uD79s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795627; c=relaxed/simple; bh=UU0Srm9ir/IuonBFXeQo8x1rxrol77G2fsdv1A/P49Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ffynnwqvb+licYAt3UBK/IktgDyhK3156wNbyMBLge2HHw2qrp6ndkzPrT3WhNrjoh16yXVyY+mn146m78GtVgt4vNjO2EKpJ5cfDkBk+13C7HTLOL5zr9i6ft+J5BJDouyja1No3n7tTSZVvlpdKRm6uI4PVd+nqv8AH7eCUXo= 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=Oo5T1hzw; 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="Oo5T1hzw" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736d30d2570so2142335b3a.1 for ; Fri, 04 Apr 2025 12:40:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795625; x=1744400425; 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=lSWfLEFHgxrxwFBenufoQIAU5IDy2E3h/30rrI47RK0=; b=Oo5T1hzw84VCnxnTNyMZ6ZFbTGbtZmeLfjnaFyPQb+A7th3QBQHsteKE1qDfNvykkC hXbJoONJPeQGlnsSRcJS97R3s1j2xZtU1cUq6i4nSkOzo2qL35Lapy7Iflw9fIdKN42b nFVxosqDrVbVcHDH27Z0DVFTQzh+Ohl6ihaCskRKSdftD+RR2Oo067CxwJMc0EKC8vgF ysI3Dwl22BvYaS/DHRU7pTitbqUliYcHdxNzrSfjebibxAobXAWOV27ON4zuq714CDWP BFW+dnOnX2qiYfLf9nF53sz04Ryt22Cb42iYd+QdNWJ7SpzD3Y/LoWpDiA+Ow21UIrAS lFqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795625; x=1744400425; 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=lSWfLEFHgxrxwFBenufoQIAU5IDy2E3h/30rrI47RK0=; b=cHuRb2nGPR3i6QDUQyWwBetBEbRzCZe7UjXih1JrMWFUaJFGW8kvLCVy1MB03RGQ0p FKGiDcI4xp+MRZbv5gplyUN4G0Zo57usZRRwZ+8aN4LiMs+l64RF7vpIfdAMIrAkloB6 lkAaKzkYI8pkpFcMtg5xpCTFNxSYtczfHFGYaQ23aR4nveGHzngZe1mulRi2Wr4TkGMu LePWcBxkcwUiauUvddHCQ9T8DhjAaV2NdYdtj+Wnbrd+wIdN9bLc5zG0emrnjQ9l8F4r LKi2yMGt4t7CtvVMbUBqsCMA3jxc3NblOl5N2SgAqkhEgFYuxWoLBg5vFKJhrVjL8E+v 9Afg== X-Gm-Message-State: AOJu0YxEf7keqXr61nCeKL7TnZTsLIRY7a9p2kyrCUFExp0INjGu2twI l1lNA5wpJHmNNB1WlcRsiRHdfQNQvDUMZELSgaeVTnf9vATmbEyZqnOEDWm8kx4MgPyEHUjAZcu rpw== X-Google-Smtp-Source: AGHT+IEtlaa9i6xhVrmng7fmvBFIIniBMBstQlCiIyFf9+8ses6o9CMRLUsjMBJaIcUpUnWLOYu2dil1vbU= X-Received: from pgc8.prod.google.com ([2002:a05:6a02:2f88:b0:af8:c3c6:e3f1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:168e:b0:1f5:59e5:8ad2 with SMTP id adf61e73a8af0-20113c7136bmr786221637.24.1743795625080; Fri, 04 Apr 2025 12:40:25 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:37 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-23-seanjc@google.com> Subject: [PATCH 22/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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=1. 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 eea362cd415d..aba3f9d2ad02 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -1199,6 +1199,14 @@ bool avic_hardware_setup(void) if (x2avic_enabled) pr_info("x2AVIC enabled\n"); + /* + * 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 = enable_ipiv && boot_cpu_data.x86 != 0x17; + amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); return true; From patchwork Fri Apr 4 19:38:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038845 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 A299C22A1C5 for ; Fri, 4 Apr 2025 19:40:27 +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=1743795629; cv=none; b=ocM17O/Ei+bPqw/lgUZP8ozWQd4lqa5E2c9pkqImwFIcV99HXESADf+cKv02NMsoadAP1eYvNuV+aPIVKegLBCQ6FxV98Uf85O44yqGR9F7BBREXVDN7UBv/b2G55NjyI2fPUmy+bjeWc1Ko6Iukft3uloM4kbKlamO6OE96Cxo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795629; c=relaxed/simple; bh=gWYzHGiCCpynm45E+mo9zGhawwFX+0w4rciPYVdijpk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=C32L/abrvsFVOF1LPwRpWDzsjSzQLakqeMYLQGv6Muf6hJEhKbtA5laEXVHahJeZnO/M48QepCuk0a+eIxPXgj3wlirgvvokSYlBYDlwc7/I1Go8ovbiYM8e1PemJEwQf4/wMzS1A5/pV3t8lLSiRmyazVTqKxaXoEB6xs/UiSA= 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=hAxT7ZHf; 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="hAxT7ZHf" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736a7d0b82fso2991956b3a.1 for ; Fri, 04 Apr 2025 12:40:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795627; x=1744400427; 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=0lUzTUillvMMJvhugb+YDr3wCuyyvIR5aJ56lReG+IQ=; b=hAxT7ZHfmuKQI07JLu9HawDR6cShos1idFMF1aSCFblsa6J+ZYnNV+V7jarW4ggIdm OOxbK8PRKWhgDJy7MfGnRpxHHHumBZ5s6+P3EjZ8BjvM3cFvbaXqgnx8wUztouBL8tvt v14PVfefzBDX9SB296vJmk61juCce/dBaSYpn09HMmxawKjaACH0jPbjo8mZQGklFywD 5ltgPSRedAu5SjWT/TfAObnQFukT6NbRMqit1b/DFeiEK35dHNBHjCoP0DoPgq7yx797 HGymLQeWzqSauWrXzfofnmlsIEBNqjOteYNek2wgoMnlNrapx448EFo3QPoRpqJ2DeML B2+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795627; x=1744400427; 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=0lUzTUillvMMJvhugb+YDr3wCuyyvIR5aJ56lReG+IQ=; b=v/kaZ+TmFksK9d5LoDXJMnKFLGQtoP7686r4z9ncxwXLmaLZGAbGJxxeVhoUWZU5O5 Wxm81kWGSoQwaXsQYUpt9J8r0PQsrsiWxrJgh00jRnGSlgLuWfCjALSXA/nEnk359YRu o+t4iScuSJkGZ/9sjj5szAXc33iy4k/ZR9DdVb40DQsS7w+f86fnZwy3fhGLyKqI0YIF 0oA0cWBYkKKXCqNm41Fj/26Qs2U1RrFmGtQSGG4xgHCFJz8HcBx9/NvYSKVyI490Idd3 0Toz/B4qhnLTFmEcgiqES2ExTQ/SN8EMc7Dg42JZPLDSkTi44IF+OLOPAHSQhEu4iEVX 4HxA== X-Gm-Message-State: AOJu0YwaeCu98FGJptIytQIpHFwcEVH3adAxIxn9VaBuKOlTKcf1HFht ln69NjFoQrbzcTzYW0TQaMdfLjXxom1PHzCzggcpjLwmuljSvZCyTQFhyHkBB06GlMN8y7UR9ww 1iQ== X-Google-Smtp-Source: AGHT+IFzppEVq+Fwd1V/1T9ksyQMe/xxnCf7osfKT4Gx98SbS26q4xCE/xkKDFomooyIE6RxvAG1qp7KzN4= X-Received: from pgac11.prod.google.com ([2002:a05:6a02:294b:b0:af9:5602:ea50]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3116:b0:1f5:8c86:5e2f with SMTP id adf61e73a8af0-20108188bdemr5973192637.37.1743795626887; Fri, 04 Apr 2025 12:40:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:38 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-24-seanjc@google.com> Subject: [PATCH 23/67] 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, Maxim Levitsky , Joao Martins , David Matlack Suppress posted interrupt notifications (set PID.SN=1) 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=0, 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 | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index 1b6b655a2b8a..00818ca30ee0 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -70,13 +70,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 != POSTED_INTR_WAKEUP_VECTOR && vcpu->cpu == 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; @@ -200,15 +197,22 @@ void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) if (!vmx_needs_pi_wakeup(vcpu)) return; - if (kvm_vcpu_is_blocking(vcpu) && !vmx_interrupt_blocked(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) && + !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); } From patchwork Fri Apr 4 19:38:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038846 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 2C14A22A4ED for ; Fri, 4 Apr 2025 19:40:29 +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=1743795630; cv=none; b=o0OGdplkmNjF9aiL+3i66QJpJSkjvKUzsHhVWFb9A359AsAsnuHniuKfVb8lPdg5Ime9zdb2hXzmDC2e225nGJROTr+2Mt70Nj4h0JFB/YneaQ8/+GLCv0AKNN95UwKf4D2Ro7uKi0zKp2Z0K//qUgx4n4gJCbKI9pbiBR9arEQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795630; c=relaxed/simple; bh=rV/TxCC8hJCF5joZAJwXSPguE6CTa8m1jVL3SS0Cme0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kzmjh03uboHZLZzq/5JP/QbHJLlV40CK5BRh7KIzaSMEMWdyhAaU59ZkaEwrFAixqsbe/fRcfYu7qs4ebwlTsqAxvCfvLWcPKSekAztapj1fVksf2Nd5TDEbaF9Ww56S6b5e48aX6hQvuo6MzmS9CEQkTOelUI288r4vcnfyaEE= 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=oI1pLrrr; 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="oI1pLrrr" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736c135f695so1931265b3a.0 for ; Fri, 04 Apr 2025 12:40:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795629; x=1744400429; 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=sjXqQKjnqBAECPoVdGNanQSTET+5wQh3LMPwBf9sL3o=; b=oI1pLrrrr0x6nNNuAZ9ctxzqrY+WwOMj8qkoYSdocaFDjT2XekgYu/IUI3mynd1aSl pF5q/c/Sln/3Dg91msaC5qu7s+HXeXKc5UX4TKTurMY5hTQrgo2iNFdBB2R6DfNCDOUB cav4hcV4QBAo7aKkZi3q7GKFV7H6KKH5n80NrpIyyPqxt/eZPBuXtMxWbpXpmGiIoUMf pZa6LTZWErVv2yXKbAL3zFh5v48En9bI3RKn5ozNbWyyrNR4hgCqj7yDj8RKAqjcXGh3 B7o7C5veNLeUxgrPitwPyt34rEDUnJ/aMZ6MTPm79XMphA03whhG0Idhmy2XBg0mBQhX JDHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795629; x=1744400429; 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=sjXqQKjnqBAECPoVdGNanQSTET+5wQh3LMPwBf9sL3o=; b=AQHwN1/cgzitS71RVEOZEz6Sk/2rC7Dxydv+nZnGkUgHd+9U+lnFZJQw/kaUrZZ7GJ Ndra6v4grlxfl72/qEAuQkTVLzjDxwDJy7s4uwPRQ5uX66tzNCdAGbv4L/XoFv6521X/ 5YeIxFPl6ZGdgVCEo/tZ8+TLW+/RviudoRr5g9WiWS6B+w6UoWIvGKJhDSntA0/7GjkE 6ubbLmKSl4SIbNPIHFm3UcrCuOOirx/XcKzLUmq0FZQEzm5myoxC11NdS/2fWIZNFA4m dCWP4MzBzKKLdJ99DFUZKTAgJb416vWuYCL3nECyXSmcZpVXrkKwjn8DeqPzM+KeI6wG C2Cw== X-Gm-Message-State: AOJu0YyluV1EtZCIEoG9KI8WwPfjY8gtoNmHpxtMccNjG/saaBPBWzd/ LOXWVLte8bhxueHvMD09th7XZc1j2EHtU+dwPtuz9jQzK3LkqjbCzXe999J3sRtpDvo7ZmZAwh1 j1w== X-Google-Smtp-Source: AGHT+IEUqaCv0aXLLYu+YUsh9QZmyBiS+o0qosR4x1hBOl7XnC5m3r8JWP1RCDtDT+Khu5sOogVzwMt6Cak= X-Received: from pfbci5.prod.google.com ([2002:a05:6a00:28c5:b0:736:79d0:fd28]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:94c8:b0:1f3:1eb8:7597 with SMTP id adf61e73a8af0-2010479b14amr7567439637.35.1743795628671; Fri, 04 Apr 2025 12:40:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:39 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-25-seanjc@google.com> Subject: [PATCH 24/67] 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, Maxim Levitsky , Joao Martins , David Matlack Add a comment to explain why KVM clears IsRunning when putting a vCPU, even though leaving IsRunning=1 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 aba3f9d2ad02..60e6e82fe41f 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -1133,19 +1133,24 @@ void avic_vcpu_blocking(struct kvm_vcpu *vcpu) if (!kvm_vcpu_apicv_active(vcpu)) return; - /* - * Unload the AVIC when the vCPU is about to block, _before_ - * the vCPU actually blocks. - * - * Any IRQs that arrive before IsRunning=0 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=0 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=0 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=0 + * 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=0 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); } From patchwork Fri Apr 4 19:38:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038847 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 14A7E22A80D for ; Fri, 4 Apr 2025 19:40:30 +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=1743795632; cv=none; b=SoL+w7e2++a6ZSifqzFloSRRcMoErJmZC2wUvtvO9wviZK2T6zwat8OCGFJuQ7rojv7tyDOuoY0mdcJiOflVXhL4TxW4QcWgZdU6j3NGa54aEvrzyZ9Qoq9Egtg/GB5nUyYpp6Nbj7WG5vKRSdiW6RK1F6sCNy5a8BSoVvu9EUc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795632; c=relaxed/simple; bh=GcrLyi9xBIguGpiJMp1JCAHPfFvSGIO53FCtS2La3bs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cHC3nqomU6Y66Sf4x/6JoKELNGYw/vq0Jmo6+HHR6NaLyHovtzhFda9l3rFSkaTUWfemjed5/tGBZhMS7WznWXc28KdPyMqCPCD5+ESXxH9c6Ei+StkCPTUYIVb13B9o83ylkAjTP/tzr2W0fC1HYjp1TthQKpv+4ru3iSUa+JQ= 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=2npXXXri; 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="2npXXXri" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7398d70abbfso3440142b3a.2 for ; Fri, 04 Apr 2025 12:40:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795630; x=1744400430; 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=jTJa+0GMh1FOB5V2QWRVxqPwy6X90M/TcdXJcK5fQGI=; b=2npXXXriy13TgtYdd+uu8SmyQNRvbfZAncgrfct1dygmRAAvZ1xLEsrZReYpYDk1Fj /MDxfX/KX+n6Pfya2UFpE+V413pM1Gyq0Z6u4PeRXDDVKZcGvF4JFbkgk3toBGKtGCzF dej/+5VkXUYLbiTyALOfzs7uCATCWubEdloYuFDP7RcQ96hvfXch1T4QG25D88IxqME0 iGBrY/4Y7Y7/7Hsn1bwAfbXjjrZ/YD8vcLjdRQLqj+8bWU/B73mSXcOQrigYjvu2MPA7 z3hxXh4nzPKFWFH8DLMYu0VcXxUmK/8R3gM0ZFioJErxH4icLPBcw3bR6UMv/SUfbgci 7hpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795630; x=1744400430; 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=jTJa+0GMh1FOB5V2QWRVxqPwy6X90M/TcdXJcK5fQGI=; b=JZvcmEozU/tlVDss3F4wKnknAFK/V1VzWi6u+n+tX/akcavsl6nO4sYCzrW0heUr87 MzkK3rj+Se9GL5nzMNK92KKv9HWwm9BKo8d2/qm6nSXUWYWEV4G0OmeJZXEjcOTXsymV ejh0mYLej/5VfM5O/rwtQBhVj7olriCaYQUvc1eQnziVGWTB1io5a1autz8LX+OV72fk EjylbHj+ad/Q65mFsUv8q3IEEBgzxKtR2DHOJlKQpWVqeAkLEwtI4vRRfuUYiDHbYNg9 +gXTjWce5PeFVGv3fi2R0dBkoOi45crzr3OI5j3cHw27/3FGwz5I033VZC29lFLN5YJG fZjA== X-Gm-Message-State: AOJu0YxbBm5IkYw81Mo/YrGzLJOKUj6yhysHNhQa/PmJIKTlhxGZvGuY rTbNj6Zw4YIJ/+tZ1jOQcIC/UWXslquhcWotc46440MmEWfaFPldHOvnTp/e/LnE0dbcECzxkbz JFw== X-Google-Smtp-Source: AGHT+IGhg+PLOqwTVlNOH/ZHSestonJxLIS9XPyg7d4ccwd3Cs3DaoNuv6SgS+NEw0wff6EG1vq5naWWQyE= X-Received: from pfxa29.prod.google.com ([2002:a05:6a00:1d1d:b0:736:a983:dc43]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:888c:0:b0:736:4644:86ee with SMTP id d2e1a72fcca58-73b6aa72dedmr737441b3a.14.1743795630419; Fri, 04 Apr 2025 12:40:30 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:40 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-26-seanjc@google.com> Subject: [PATCH 25/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 7 +++++-- drivers/iommu/amd/iommu.c | 2 +- include/linux/amd-iommu.h | 1 - 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 60e6e82fe41f..9024b9fbca53 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -902,8 +902,11 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, enable_remapped_mode = false; - /* Try to enable guest_mode in IRTE */ - pi.base = 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 = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, svm->vcpu.vcpu_id); pi.is_guest_mode = true; diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 4f69a37cf143..635774642b89 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3860,7 +3860,7 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info) pi_data->prev_ga_tag = ir_data->cached_ga_tag; if (pi_data->is_guest_mode) { - ir_data->ga_root_ptr = (pi_data->base >> 12); + ir_data->ga_root_ptr = (vcpu_pi_info->pi_desc_addr >> 12); ir_data->ga_vector = vcpu_pi_info->vector; ir_data->ga_tag = pi_data->ga_tag; ret = amd_iommu_activate_guest_mode(ir_data); diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index 062fbd4c9b77..4f433ef39188 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -20,7 +20,6 @@ struct amd_iommu; struct amd_iommu_pi_data { u32 ga_tag; u32 prev_ga_tag; - u64 base; bool is_guest_mode; struct vcpu_data *vcpu_data; void *ir_data; From patchwork Fri Apr 4 19:38:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038848 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 D2C3222156B for ; Fri, 4 Apr 2025 19:40:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795634; cv=none; b=b+DlvCp4VgobQzhdeOmnepooQLJf5W0ox73lbbqDZxqVGzE6AWIfnisn/dTtiZkTjjG0PwvdgMig5SOGFJUhlwq6nS9Yv3ZkG5s0Mlxz9DkL/cFTgONGnIh+x/E8yZyTNSpgOWX1THC/oOoJxh68n9ioOP1C+SHXnIf3CtGGnVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795634; c=relaxed/simple; bh=numHIJ2fVUixTwPl70w3f1Z9LTsloAbGJqKA3GVrevU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JcHL+ZrUyJKj7CCGP+11IhzogxLL1i8fz7DXjli6t3dik4zay7CcghEnmBjYANoh86CTAJ70Sf8eJUwde2Yl4Ej4wyc/VELrXzeaKyBpPySBixKp4S43ulwVdgjEJCpFfleMrK5htEdKPBKyOKrLCCg8LVbqVnSOSLDt5FpcBMY= 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=q0CE4mLS; 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="q0CE4mLS" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-22403329f9eso20050535ad.3 for ; Fri, 04 Apr 2025 12:40:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795632; x=1744400432; 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=VyaPnhLqGsMwpjiZdqn/1mWwUn+QLwb4TwRlvb1iqoM=; b=q0CE4mLS1t+wNDlrPgZ3F5a6jicDZRs3RPVTNmQHdl9z53ZRBw7FCXkHj0iFOLa0lN o+kB6S4tIhspRsedPGkm5ydFFVnAheT19OZLIrXSfZLTSlgexwRx5xRMLLAFOmc58AYY w4hL2jkKQ0Np/DEJp39FBTqPavSsMIsMB8Vhih8jzf+0c/HfA7zwItO4SWsprJ9KT2J2 yG1JG1Av87szK4CQkK5Icl5WsRmi/dZVGmTcfpIvO8vSuYTK8/bvP9RWAiikAWmwFnbc ogIEoc8TrexvQzwJkGKhRbaJVT6qR9j8u0YWSat3oo7XVegMq69FVVqLLPENf0aLm2mQ sVLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795632; x=1744400432; 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=VyaPnhLqGsMwpjiZdqn/1mWwUn+QLwb4TwRlvb1iqoM=; b=WiAryo5n+XJnmbB7cGDCJR1zk5PM7w/vcfVyfMk+dI1HFvMFIPAcD8buf0hbJ0Sirn F0BlhkFkw55kmw1qHQRHsOuw504dHvlthC2ACDdwq0TeYcBwFiTfiFTtIm2HMKSqr8// Ri7sBgkwkXa/BLLQRGiCo4Z53CZ0HxSyUn4SgaSAKZ6W56YjCRrHvVAfNenEMYESDBbP SALE3FInRQYaMEa1ToeHdqF015PrMiYUhba4dORbotjwulQC+J1PY0CE/LxpZSYhnX22 TD5hBn7ITv6vtl03fny/zWtM6KM8WdNH/aCoUUJeq6OLZVpXxdF2FNwzdD1NTUFMjGWO UjAA== X-Gm-Message-State: AOJu0YzdHHKUE3YeUqxU2iCHFT8GwQZdrTaeZD8OsLNMvoLmnlPazaTJ W3MD5Y3uWyNvK1WAZlyF14nmDKi6LotlOUngRI+wLfgyEj+4TBnv7h8DKv4D0kxeZt5eY3zElJG +pg== X-Google-Smtp-Source: AGHT+IGMwxs0YOBPs8h+elgyRVkuAbPHKTSrU7awNNYaCT+HPFTcP/hO7N3HdKAFdFfYL9/K+0CGHpH7MBY= X-Received: from pfop9.prod.google.com ([2002:a05:6a00:b49:b0:739:9e9:feea]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f605:b0:220:e9f5:4b7c with SMTP id d9443c01a7336-22a8a865825mr59456785ad.17.1743795632180; Fri, 04 Apr 2025 12:40:32 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:41 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-27-seanjc@google.com> Subject: [PATCH 26/67] 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, Maxim Levitsky , Joao Martins , David Matlack Delete the amd_ir_data.prev_ga_tag field now that all usage is superfluous. 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 | 1 - 4 files changed, 14 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 9024b9fbca53..7f0f6a9cd2e8 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -943,9 +943,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 = 0; pi.is_guest_mode = false; ret = irq_set_vcpu_affinity(host_irq, &pi); } else { diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h index 23caea22f8dc..319a1b650b3b 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 { }; 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 635774642b89..3c40bc9980b7 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3858,23 +3858,13 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info) ir_data->cfg = irqd_cfg(data); pi_data->ir_data = ir_data; - pi_data->prev_ga_tag = ir_data->cached_ga_tag; if (pi_data->is_guest_mode) { ir_data->ga_root_ptr = (vcpu_pi_info->pi_desc_addr >> 12); ir_data->ga_vector = vcpu_pi_info->vector; ir_data->ga_tag = pi_data->ga_tag; ret = amd_iommu_activate_guest_mode(ir_data); - if (!ret) - ir_data->cached_ga_tag = pi_data->ga_tag; } else { ret = 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 = 0; } return ret; diff --git a/include/linux/amd-iommu.h b/include/linux/amd-iommu.h index 4f433ef39188..deeefc92a5cf 100644 --- a/include/linux/amd-iommu.h +++ b/include/linux/amd-iommu.h @@ -19,7 +19,6 @@ struct amd_iommu; */ struct amd_iommu_pi_data { u32 ga_tag; - u32 prev_ga_tag; bool is_guest_mode; struct vcpu_data *vcpu_data; void *ir_data; From patchwork Fri Apr 4 19:38:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038849 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 54A7922B598 for ; Fri, 4 Apr 2025 19:40:34 +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=1743795636; cv=none; b=HDLuoUfZH/RrMbeDwb4Z6+BIHCiGpbSJBBjY4/svo3Qp9QnZioijQKm0l4zosU6nLs20oiDXLD0gaZgWKLlllCVormUUjNhAILjdISG6OP3U8coI78MR4oj3400fdDhRUrUVOBNvG9x/pZjBFx7aHGBCs8dCWluLfIFdWGueWhY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795636; c=relaxed/simple; bh=V0MGfeZG66UFt76kNvNPvs2l8e+FatrRv8SBCWV6kxk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ev2GSlruZn0um+GF6soQg8w1OXKepN9q3Gjui97h8FE5am215wZ8OcckcwtB0Q1MilYKrw23YWA7Zk39kwjBd6vQATrSmTH3fp2KsxTkAdmmUbltX4LD/ouXq+u/BOYym7dUAcuBusG1F9dGoVkC5r0ac90bT0l2t2J9NTpTIDc= 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=vUX8LCPH; 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="vUX8LCPH" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736b431ee0dso1963728b3a.0 for ; Fri, 04 Apr 2025 12:40:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795634; x=1744400434; 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=+TG2faHkEqaXMPy8//eGevjiJYlH47y/jdqu1B7aYBw=; b=vUX8LCPH0zzXLht1JUdfQ0tfqBOjbxCqaSyHRzKmD/ZJ10Hy71rpHif8tMNKpKR+wC PH9LeBVUkW8UruhC8vMKY0wFCs6WpolNmtl1ybe5eaBF2GI38adlmn32E3PCZ6997a0v rRUnec/AROksMFveNBRhUhMXyWn1UVp35s0Qldhc+zEnCN83nx0CiKb0lXuWT/u9T6lt w6GxeFjtLazdlsgWtkOqzkL1EUXLEbVa7jl5C7msDO/swYWOUvFAl98v6zGvFitUlZ+/ Qa+NesKlHA4354c+jnvTqQ2uPmRkqzGRXcDNrzqJo3WMhPysjyuxVbjWBivUP/Ywrf9l 7zcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795634; x=1744400434; 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=+TG2faHkEqaXMPy8//eGevjiJYlH47y/jdqu1B7aYBw=; b=lzwJXrgCLtjpIT3c3Hk81S62tTqwW82n9dbx0093N+FIvQEkLOOEhsiYR30o5VBqUt 7RN32BN9S5avzG+nN9eOObUQ2jzSUPpbKmMxhZ7Wkj8adjFPHh7IP05vKx9utYGnrD9X R5gTJKhPseEjhokBOwBkQGnPboDRwMBafBmDvVhqHbOaLEcWe+ibknor9/cDo41ocBzA FSU5vnjKtTFOLsiX3dm4u5F9OSCEK33PwFGq8lS3EFXYH/2AbzT3wHOo7WJdMTAJeao0 f3IBVXxoD9Xp3kgYDO5GMsDeIks77E+9rRTM6jeZ6o+LcI6KsRU1CaSDzGPVMSW3lLGV 9OSQ== X-Gm-Message-State: AOJu0YzEcYsmwLlycS+3L+WXbYs/ECU8bUA7ZQC1AsB2BBeORozHxRtx uWaeUEq8zNeVZ6OwveVXXPH0Is54nX7Tvvzuo4FNWhxElWjtu7ZvMPeGy70b2OD9z8sfr1BI93E R8w== X-Google-Smtp-Source: AGHT+IGLDYZyajkylbmX0c1w0MumABkRavq1xwq7nD0DOCpebCVgFI0dxpSvqG/AK8qTMUuH0twfv9e2Cs0= X-Received: from pfxa13.prod.google.com ([2002:a05:6a00:1d0d:b0:736:a68b:88f8]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1397:b0:736:51ab:7ae1 with SMTP id d2e1a72fcca58-739e4be4210mr5216803b3a.16.1743795633653; Fri, 04 Apr 2025 12:40:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:42 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-28-seanjc@google.com> Subject: [PATCH 27/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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). Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 15 +++------------ drivers/iommu/amd/iommu.c | 10 +++++++--- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 7f0f6a9cd2e8..9c789c288314 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -936,19 +936,10 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, } } - 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 = false; - ret = irq_set_vcpu_affinity(host_irq, &pi); - } else { + if (enable_remapped_mode) + ret = irq_set_vcpu_affinity(host_irq, NULL); + else ret = 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 3c40bc9980b7..08c4fa31da5d 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3835,7 +3835,6 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info) { int ret; struct amd_iommu_pi_data *pi_data = vcpu_info; - struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data; struct amd_ir_data *ir_data = data->chip_data; struct irq_2_irte *irte_info = &ir_data->irq_2_irte; struct iommu_dev_data *dev_data; @@ -3856,9 +3855,14 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info) return -EINVAL; ir_data->cfg = irqd_cfg(data); - pi_data->ir_data = ir_data; - if (pi_data->is_guest_mode) { + if (pi_data) { + struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data; + + pi_data->ir_data = ir_data; + + WARN_ON_ONCE(!pi_data->is_guest_mode); + ir_data->ga_root_ptr = (vcpu_pi_info->pi_desc_addr >> 12); ir_data->ga_vector = vcpu_pi_info->vector; ir_data->ga_tag = pi_data->ga_tag; From patchwork Fri Apr 4 19:38:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038850 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 F000522B8B2 for ; Fri, 4 Apr 2025 19:40:35 +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=1743795637; cv=none; b=izdn3SOPRsgH3bTEiIznDOVvsABJry2EN0ThjSmlnLFrpxCloMQCUOjac+nJw2rl5qtU9jh85c8SDK/UfR6h+EGF21Uf99Fa9f34ChaR9anx1V95cgyELSkHxS5VBgvLqcU6TOz/2haHfYaoLl/EacBX6I9kYqw5qH19IJtO5FY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795637; c=relaxed/simple; bh=t3mwELhAcKd1FG0Vgyh+Vr0Nt27BO998JuBjMUh3xzA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uBXv38yCrWeJgBveyseDEHwkIanrQ85Mt1Jn44Y2OxnwortYGbai1fJVsfTRKMr6zCp39yh9LQ5CM4nlF7UNEkk2tzxasQ2ammVTSVHB29CjBzSRwxb9O0MeUDCS1EBOuWtqXwD3QFW3RVJtt0Pg5+kB06O6Ywrh0Qb1plCkreI= 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=ATB9yvaA; 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="ATB9yvaA" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736d30d2570so2142510b3a.1 for ; Fri, 04 Apr 2025 12:40:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795635; x=1744400435; 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=JCbQ/uPtm4GA9Jd+dczlO9sqKxeGX1Ak3ycPsl+3KAs=; b=ATB9yvaABISON25xFvatCZEJCKtZuWnlOD176S/yALBtezMElcUhPoWKGaq+bd+wt4 Z7K6wIs/bGNsR4tjVqKFVySWaHSrjbaFRGO9Xv1/aisfieapwmwrNe+XR/tJabzvhgjA fumDfwTKabCcOyf6ZMoJJM0IpPT+mOE7Kd6CX/y87kY6tR+p68yjNmC4xDmQEjhae93I 7TRwP2cps2+7RuG6+sff/B0vjAWxsGA66fBp2VBywh7qPaDIH+dZzrInpKeYUPL+g4P2 TjWpwvLHop6gVg6SPP5koL0Qv5wAVzmvQcWJuIL4FOBjo2kXNAy3B0C1zbL8vjTfGaxP jjRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795635; x=1744400435; 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=JCbQ/uPtm4GA9Jd+dczlO9sqKxeGX1Ak3ycPsl+3KAs=; b=Q/CG4JTYeqiSR/iQDFSv94P2GEDAJR+dvtHBfZMCjtNg1z0YHcwLhL4ufcUiLvTwm/ PBa37oqkvCoKynNK5Xmdc+QWh9su0qn7+2zd1rgKcW/35gLWT/vpQMTQ9et3qSqw0gh3 OIJHk4GveM+uwf/ETDp28/gQ69uS5gRS3i6qggDsT/3qmJE4FhostXSZfwmKBnvZO23k FdkqVsMwanf8yRoj4DQ+uzxtEcb2mfqz3oueqAocL2J01G6+dMp+Jiq+omfY081Rw/A9 f7P69dg4QDaEhadTwVukuYiE6ZHZK8UiJiu8DveQ8dFpbu7or4MeGv5FHDZhG5/93Opz gQFw== X-Gm-Message-State: AOJu0Ywf9vUtvBsyOSLYi0/XoI1mjUjY0Vx7+z+U6eLkziwDxLrJL3Ht j4E7AgP2whIVxetXWBrisByCBfi9fSnbIfEQa2AjRyCISd7+EP2GCu2L/lsxBeHj2Jiy05KI/ye lFA== X-Google-Smtp-Source: AGHT+IF4DCxN4WQm8X4YFDxAo5hJ4BKHPkaXMYPBbUUX5BoVrFQSM6Xqq/UtaAv66x4ahce5XCI5nOVMSUc= X-Received: from pfoi21.prod.google.com ([2002:aa7:87d5:0:b0:732:6c92:3f75]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:7f9f:b0:1f5:52fe:dcf8 with SMTP id adf61e73a8af0-20113d27d58mr891016637.26.1743795635469; Fri, 04 Apr 2025 12:40:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:43 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-29-seanjc@google.com> Subject: [PATCH 28/67] KVM: SVM: Get vCPU info for IRTE using new routing entry 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, Maxim Levitsky , Joao Martins , David Matlack Explicitly get the vCPU information for a GSI routing entry from the new (or current) entry provided by common KVM. This is subtly a nop, as KVM allows at most one MSI per GSI, i.e. the for-loop can only ever process one entry, and that entry is the new/current entry. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 9c789c288314..eb6017b01c5f 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -855,7 +855,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e; struct kvm_irq_routing_table *irq_rt; bool enable_remapped_mode = true; - bool set = !!new; int idx, ret = 0; if (!kvm_arch_has_assigned_device(kvm) || !kvm_arch_has_irq_bypass()) @@ -868,7 +867,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, svm_ir_list_del(irqfd); pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n", - __func__, host_irq, guest_irq, set); + __func__, host_irq, guest_irq, !!new); idx = srcu_read_lock(&kvm->irq_srcu); irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); @@ -896,7 +895,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, * 3. APIC virtualization is disabled for the vcpu. * 4. IRQ has incompatible delivery mode (SMI, INIT, etc) */ - if (!get_pi_vcpu_info(kvm, e, &vcpu_info, &svm) && set && + if (new && !get_pi_vcpu_info(kvm, new, &vcpu_info, &svm) && kvm_vcpu_apicv_active(&svm->vcpu)) { struct amd_iommu_pi_data pi; @@ -927,7 +926,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, 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); + vcpu_info.pi_desc_addr, !!new); } if (ret < 0) { From patchwork Fri Apr 4 19:38:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038851 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 8445A22C34A for ; Fri, 4 Apr 2025 19:40:37 +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=1743795640; cv=none; b=KtD0m8cff8UWwV9dVq/rN6+tCtiXeMGXErEKGKYBJg4uDCZzK3QwAUbU3bT5e4/9zQtAx+EJKxzTqVEEmuCT/z+33yQhIi7GRnrB3MNMervjatZjq1z0COib4iAuCn5AQcivH9je+gZgG1ZWYBQ8kBvLlwqTSqZbwYo6rjHuQ8g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795640; c=relaxed/simple; bh=cdKlTVqFAw8ApvEuQ7prWTD3bUNet1piEa49vQ1EufI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AZAMtB5QBnSoq5Z5U0dFjcRDaTbFf8ONsSp0LEKiqoDvRAzkHWwaeJ7ev2k8SV3mjAGKcODjD4A07HxPwMI33HTG+66KRysxS2LpReU7SqeIvvLtkbhLwPz4RxTpfdptTW6YFePlbxdP/kE06UY5QxvCCPqhzc//ikC8BWAa3WE= 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=z0GBCpx4; 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="z0GBCpx4" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7377139d8b1so1924390b3a.0 for ; Fri, 04 Apr 2025 12:40:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795637; x=1744400437; 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=1QbRZKRmRkfriBxkLy7PYXuhu9Zt47DLy3JPC1CCZP0=; b=z0GBCpx4MaMZxljY9dGLNUf9NudFLzm9lH+toFsTmtQ7SdWNK/MFLPcYEUeiSCM1cz Vu+ABra7HvAVD3NlSpPrF5Zn8o2GhHWEDm/QJZ/sTZErj7Yl9jlRvMhP3I9jJd/3ypmi btK/6EAI2prH8/isNVw1jRkR8lb7+rRqWWjFG0+feUbXlzWJIwsDaxB5XCYT1BB7zeaq emAV4V+H36oA7GBXn1G5L3KYfjqguB9x59V/9VL54qxT2vzYmu+WL4BkQi81v26Eapvr PPxk/Wh5X13jeedIASTYiMLripuS9JA2hCub5aHX4o8ZjjCtjmiPanRJ7cQcKIUAwd7r 97yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795637; x=1744400437; 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=1QbRZKRmRkfriBxkLy7PYXuhu9Zt47DLy3JPC1CCZP0=; b=erGlVUsxnsAw0mdfWxxWn4tkRRgGb0Iib9+h6VFS7IiE+M4pxZy2nMjrpFEaAH9Wtq yI9aWv2fwd034xZ2/Bx0yNcwxdPg7w9NfmS+tm6kR4awV4ffVxtYUD3hjgmOuzbGgNDS JOtDyyf5G8lP0jZlI6QWDD4wz7kh9xGu7fZEU1H3Aw9x1KqmOmGlo0kjcOWPk5auITa1 XTE1psCPbCjaTKqVd8IXgDYquoDJCY5cs+LbRLHkzVMeXAU42AWeesgRN6OhtI7/F6Qs 27WtpSpYvtKV9mQdB8HN1yOXsVz2tFY2i95hAnN7vwDpOcO+OeoXZDWbHXnWU5Ca+aEc qKOQ== X-Gm-Message-State: AOJu0Yz/wCClpsf1DWm1PabOogoujftnVJi4JQ+hLYewMFpVLEr5cS2m I12JnHb+KYoWW4ZsN39fwtke/eCgknp3skMBl2X57IGpWi9vWOUA+qxFafN8cu6p7tW3geDYb7Y tXQ== X-Google-Smtp-Source: AGHT+IF7hVw0fjyQPYLVoXo+ylFst6APnbo8/gAtHy3E1LET5Ypzmt4DGEcGNQw0IysuKrRghF3jHnzmiw0= X-Received: from pfbic5.prod.google.com ([2002:a05:6a00:8a05:b0:736:3a40:5df5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:240d:b0:736:4d44:8b77 with SMTP id d2e1a72fcca58-739e6fdea83mr6214670b3a.8.1743795637056; Fri, 04 Apr 2025 12:40:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:44 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-30-seanjc@google.com> Subject: [PATCH 29/67] 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, Maxim Levitsky , Joao Martins , David Matlack Now that KVM SVM simply uses the provided 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. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 106 ++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 63 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index eb6017b01c5f..685a7b01194b 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -852,10 +852,10 @@ 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 = true; - int idx, ret = 0; + struct vcpu_data vcpu_info; + struct vcpu_svm *svm = NULL; + int ret = 0; if (!kvm_arch_has_assigned_device(kvm) || !kvm_arch_has_irq_bypass()) return 0; @@ -869,70 +869,51 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n", __func__, host_irq, guest_irq, !!new); - idx = srcu_read_lock(&kvm->irq_srcu); - irq_rt = 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 == 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; - if (guest_irq >= 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 = false; - hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { - struct vcpu_data vcpu_info; - struct vcpu_svm *svm = NULL; - - if (e->type != 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 = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, + svm->vcpu.vcpu_id); + pi.is_guest_mode = true; + pi.vcpu_data = &vcpu_info; + ret = irq_set_vcpu_affinity(host_irq, &pi); /** - * 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 (new && !get_pi_vcpu_info(kvm, new, &vcpu_info, &svm) && - kvm_vcpu_apicv_active(&svm->vcpu)) { - struct amd_iommu_pi_data pi; - - enable_remapped_mode = 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 = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, - svm->vcpu.vcpu_id); - pi.is_guest_mode = true; - pi.vcpu_data = &vcpu_info; - ret = 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, !!new); - } - - if (ret < 0) { - pr_err("%s: failed to update PI IRTE\n", __func__); - goto out; - } + if (!ret) + ret = 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; } if (enable_remapped_mode) @@ -940,7 +921,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, else ret = 0; out: - srcu_read_unlock(&kvm->irq_srcu, idx); return ret; } From patchwork Fri Apr 4 19:38:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038852 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 450EC22CBFE for ; Fri, 4 Apr 2025 19:40:39 +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=1743795640; cv=none; b=RanYTssmGPr2j4yu8f7cHJr5lgVkk9ATmIl+iW5YfdBZg8b1rmGapCUHtVZFfgL7YX8vLj4orGYUfuQEqnDDmYIWFNMqFYhQ8iTGJjhB6ifqBhSQLNDHO7fShrKe5hxXIiQEFdaukSgzLW5jfc5hBJ00swRLvly78rMd3b8DWnc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795640; c=relaxed/simple; bh=j0lOZuhbz70jbLWfgFT5/V4b+6QYeIWdwS85dC5urqk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rcbZPFQfYAMe6YnUWAzM7K2n4m47v99/8ZjKKQfSB0/L6ZMbRbW1HodDLIt8/tNDTJ5r1TkHfS3Mn+9Hi54e/JbOM5p0QjenbzQvl+PweKDTk7I5kXlie5jSiUM8cvTiI3Qqnh4gx3c6018W8Sl/0sTZLlqabBth4ytklkJULVA= 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=uZQlnXwF; 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="uZQlnXwF" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736c7df9b6cso3109445b3a.3 for ; Fri, 04 Apr 2025 12:40:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795639; x=1744400439; 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=+J7W2nDY+Ue7Y7BGjJEg97tl/zKnaD8eUMgfL49nt4g=; b=uZQlnXwF/MP7dla1flR4m8UocQB31q3V1yXXXmEPWHiVzfzPR/wCiXpnTzp3CaChhh fmI3PEf7dz/E2++I6I4VGueZvKogMfxNJEndWbddtSTjP4Dy3pjGyXE9ikH5kjceZrh0 QDnmGETDhjiE19KfSAZ5qTBDFs5hamemMBHIvHEKL9xL92sB0YseXrV5ynsZzAAl2V4T 2VJIydPu6IRokiiOIChzlOSH2FCR2GNYbNyvpPZmTxldAwxaYF6U0uWvqNP/xJL1QtDC 89HfQkXVjtj9tN16wr7lMuvMo3AMRbFzWcr33ZTlEXJmZKdg/SVIuFm5WUH2bI2WVYzG e6Zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795639; x=1744400439; 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=+J7W2nDY+Ue7Y7BGjJEg97tl/zKnaD8eUMgfL49nt4g=; b=qa1zMpCoP2S7XCLxp4blIzYqMKtmAxkCmos58DaGc8sLAHjx6GSr17W76Cva386fWh nkK/LHS6FfidWgEWDkpo2zlX1RuZiKCoaWiEQr/VaJY4cu0ZtPrbb/O49hXFYsIrqw/Z zC0IXDbxaNnBWyYrwAu1eOSoE0h9FIQ6SPUbOzDCpv8YsxbInHKzIiPhlypHz0pnM35H YpH8OCOxYLfT6yqaXVHBNyRIQC1dOoBijAoqypai715nWE1NGDRgR42drD6ZVMDHqjng 2Atsku6h4Do7cYFWPFEWlr36ewN35m2P46t7YxSqEiMEUeG/dlsO3e6gedvduYfeuse/ wSTQ== X-Gm-Message-State: AOJu0YxGnntSw2dQPf0Jrz0xrsEQkCfY/MwzhFX/3Gbeob9We9f8aohD HCoFIT6NGE5Z9kQZpcxfarCjbBoE3R+ngucK5nHgm9dKMJ4S/RrEHi59a8CPzu7g7dpfkrYkfnc /rg== X-Google-Smtp-Source: AGHT+IGanN3xsN+gLuRMmt8yMi/eMlmDRdh9+UjevAcx02mvA0AncoJ4GOFGbRA31c3qdevPhRDPgyEMWD0= X-Received: from pgaa38.prod.google.com ([2002:a63:1a26:0:b0:af2:3c1d:c04a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:cd91:b0:1f5:8a1d:3904 with SMTP id adf61e73a8af0-2010458cc7emr7552343637.7.1743795638804; Fri, 04 Apr 2025 12:40:38 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:45 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-31-seanjc@google.com> Subject: [PATCH 30/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 00818ca30ee0..786912cee3f8 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -268,78 +268,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 = true; struct kvm_lapic_irq irq; struct kvm_vcpu *vcpu; struct vcpu_data vcpu_info; - bool set = !!new; - int idx, ret = 0; if (!vmx_can_use_vtd_pi(kvm)) return 0; - idx = srcu_read_lock(&kvm->irq_srcu); - irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); - if (guest_irq >= 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 != 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 = __pa(vcpu_to_pi_desc(vcpu)); - vcpu_info.vector = 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 = false; - - ret = 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 = irq_set_vcpu_affinity(host_irq, NULL); - - ret = 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 != 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 = __pa(vcpu_to_pi_desc(vcpu)); + vcpu_info.vector = 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); } From patchwork Fri Apr 4 19:38:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038853 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 4DE8322D4EF for ; Fri, 4 Apr 2025 19:40:41 +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=1743795643; cv=none; b=VtKbCsIu0SoNHB7vD5Vme9U7BxvZp/ciCMJ+q6QIdyNrC5HtPD9Ltk7YOw4hY2dLtSSGe9IXKQFZIx2i6t0Yj89B3Q/xUm0Uiowxsbra0ZLG4FQiOwq3DTxAGkwbk3IO4s26RqQ1SVmJp8P5FMY85F6vsnwjDERjIJMu7PfwJ4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795643; c=relaxed/simple; bh=E1NTSYzgy3mSFQ63wZRuWK8nkdF2D1eAwMnNqU4u4CA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LY5azeVk5dUt50CMlTRlCX+dNDEzQ/OBV8e2WjuymcGQdfq8JmGfNVp93mc8BiDhCnUr7ILFlMA71cBHKREfZUpoQ6sw0v33QpGOtv2TmAYijSp/Oh2lxBCFzKRCQBj1dNsyEr0J/JN9Fh6MjCi6zT2tfsfqM9gQVUJhnC8Z88I= 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=DV3SkB0s; 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="DV3SkB0s" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ff52e1c56fso3249978a91.2 for ; Fri, 04 Apr 2025 12:40:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795640; x=1744400440; 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=PKAF7zUVxQgoLKs13cGz/YuTc8j2bNHXwpMbzBSsKQY=; b=DV3SkB0sUF8pxIoS1k2zYDbccdAWQTugYWebAzsA/Cp0vIfGV3DuvwzQP8w1xcfhsE Ijg9CA7Avwy0+xjGpsGTY+3ZRZqLbMON0gsPRcG+cyMbbtoJLsL+h5kGq5qO/xzFE//E IFxF0yp1V2Olp6JEMSSu3x8bcCzgx0cNSLNVKI3Hc2jP1nG1z1i5k4+50UItsnuKjf3I GMqeW42XYv78B5N4rm/fCDnrXUC5y+vuUcQ5/FWDnJtu95QcKzlgPr8e4dFOqpKUUlqh uKr1YmbRqsu6V/pfhnIrDsb5uiud6QzMZlKP1gQt8Dww50N7geFlfLlywGcM46p1RygD CV4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795640; x=1744400440; 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=PKAF7zUVxQgoLKs13cGz/YuTc8j2bNHXwpMbzBSsKQY=; b=c6W/KfrayyBFOprWZn4D80KpBeSFZgIms63JwPpclSIJ+tlvO6Akq9SxNdDvZn1oDz fF57C6wGlAjVfxhF1+aGlVNFEM7aZGuZZ7GGmXR3h05C/YJwEbb5Pqph+gRHT0jzBVNc jlLVRtaefmM/aBFvk/2RTdckr9Ri4j608IgiHRcDu1P3/KpRxF1j+Zu1AtBKNQMQddNe gpb9fG5fruBuP6l3SvSZmz3T82NHYLNV7Mbh1qTuj8JY+EGBYFVDPIoIO0lXtWco/YUk gVM+baItI4iNAscuUNTG9x0lwcM19aB70Fhg6PcBiO7/I4kBlU/vz8MNH0esHb/vhJZL g1TQ== X-Gm-Message-State: AOJu0YxviH0MmT8uxigNCqtY0ruxUbaum2GAP8kcrJGSaS7n+gdxfR47 80bzWjB1kWAfMFX3eQBdD+WhS3CtwDR/2EXS9/i6+h6C8k1gEsN3I20Pl7w8eRUp0OMQdKbEpbk BGg== X-Google-Smtp-Source: AGHT+IF7aV7vIgQiVQyfCnIlHhs7ynxCvi33qThdlWSuQSHsGSJr9KXKrmMS3Or+kU5DYfPu8Q+fOei9d1c= X-Received: from pjbsk12.prod.google.com ([2002:a17:90b:2dcc:b0:2ff:5516:6add]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:f950:b0:2fe:b907:562f with SMTP id 98e67ed59e1d1-306af732f86mr787774a91.14.1743795640626; Fri, 04 Apr 2025 12:40:40 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:46 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-32-seanjc@google.com> Subject: [PATCH 31/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 685a7b01194b..ea6eae72b941 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -825,14 +825,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 = NULL; + *vcpu = NULL; kvm_set_msi_irq(kvm, e, &irq); - 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); @@ -841,8 +841,6 @@ 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 = to_svm(vcpu); - vcpu_info->pi_desc_addr = avic_get_backing_page_address(*svm); vcpu_info->vector = irq.vector; return 0; @@ -854,7 +852,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, { bool enable_remapped_mode = true; struct vcpu_data vcpu_info; - struct vcpu_svm *svm = NULL; + struct kvm_vcpu *vcpu = NULL; int ret = 0; if (!kvm_arch_has_assigned_device(kvm) || !kvm_arch_has_irq_bypass()) @@ -876,20 +874,21 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, * 3. APIC virtualization is disabled for the vcpu. * 4. IRQ has incompatible delivery mode (SMI, INIT, etc) */ - if (new && new->type == KVM_IRQ_ROUTING_MSI && - !get_pi_vcpu_info(kvm, new, &vcpu_info, &svm) && - kvm_vcpu_apicv_active(&svm->vcpu)) { + if (new && new && new->type == 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 = false; + vcpu_info.pi_desc_addr = 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 = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, - svm->vcpu.vcpu_id); + pi.ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id); pi.is_guest_mode = true; pi.vcpu_data = &vcpu_info; ret = irq_set_vcpu_affinity(host_irq, &pi); @@ -902,11 +901,11 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, * scheduling information in IOMMU irte. */ if (!ret) - ret = svm_ir_list_add(svm, irqfd, &pi); + ret = svm_ir_list_add(to_svm(vcpu), irqfd, &pi); } - 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); } From patchwork Fri Apr 4 19:38:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038854 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 DD41D22D79A for ; Fri, 4 Apr 2025 19:40:42 +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=1743795644; cv=none; b=ZouFG3q/TrPpfV5SDHiVdXOdFtUjcDCt0oo/nupghUWPN9OWUvVgp2Q4dnvxQElYgDPmk7aNApaDyhmJGjSM4QKDcgGq39FX7YSv/tTHDwHJc457MQe14oJWS1v4QUOR1+9YQ8zjKfgR4tU0vAZytvENgIGHW/0fDD0FePW5x+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795644; c=relaxed/simple; bh=NSXiAizTL3IE0tHV1Xvoqym6aO30iQa3+NNj5eRfrOk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZXrv2zyQLzkqMSULELhjLENL2MTEafKIXwefwLCaeX7dw4u3ZzlDvuExdKXAIF4FflQYPJq9qzQHdlFdORu3cjZSeVjEKQPXaJ5RGCp1eGBrI0lQPAnwH5pbvzfamC6OoJAmP3eFPNWtgDJE/7dcVfMyRg3AVTf5gyfZ0HXhRww= 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=Y3AZY5Pg; 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="Y3AZY5Pg" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736fff82264so1863153b3a.1 for ; Fri, 04 Apr 2025 12:40:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795642; x=1744400442; 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=4tJsBeG4t1G+TPijwvZhdI7bM9ZuzjVrHlwd3hub/NM=; b=Y3AZY5PgV+xWcumpNe9bf9R4aCotdPjQwxL0h7WAiUe8o8krSrcXjpo9XkeWvEDcXr oWXKllbnWpZJk4zGCLDplLQGM9a1u6WZNF0gBBjPvmlG6vzzpyr4VVIjNHQIpvQ/VeHb M4rpVgTMMW0e50+neZFRkS9HFe6KcTIqAioFUHyZuSR91A+bXZx5Ymbd8WgI3IbJDm8X 7CZXC5UsCIFVEJzF8WLb5oR7b3F+pn2qPCf95PfasGgZpbsqadqNkCkrWVoRDW9Y7Y2k vEDcCrtg/76wD7dv0G9tG0Nj1dDkG5UN5Hnirt29keZOCvCsEZAmU7cp9/Tz6ejLKDuA ictQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795642; x=1744400442; 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=4tJsBeG4t1G+TPijwvZhdI7bM9ZuzjVrHlwd3hub/NM=; b=lbFMI82ZwxG5DyEodVWsNXvB4SixBiTGlv5IlvOt9aONsS+K2O14YISBxsxKz2A7Z6 GxSMmCL1bnJZqJQnOGSYSXim8vt6gxrd8BlrreEiZKVG9fNAhA+XBPpAJSYkYqZ3dLF5 cP4Wb9pbuhXGqvp2JwYCV7t/UeWi+GMi3HyRy1YpxCYpjdb9OztKIXV6xQ9J+rxSqLSW SdqOGyJ9Xy2DDyauBzuosSh8O4On5zxWib0SY4Wd8ewhVNNDYyMRIhKMMCyTYz3gBA0e OXq5/zqOcGwJpkt1PWgfWhMVVAHO4/dNZrH/t30iYUEuLVR/9+YJFXrb1MDtmzW13zrO SfDA== X-Gm-Message-State: AOJu0Yzpat4jaFhosf2plwgH0ltg6A+oss9Bgt/ZK7aV1OA1eCf1VPmM QHFOmn1qzIGHmrRn0XhkNUSdXcn0gZbg1wVQXOXnAfNiPJq3Xqunm5aucSOUN7ffRhAfUJJV5CM SLw== X-Google-Smtp-Source: AGHT+IEScd25fZRgSV6IjIzWui2u8dQv3YbcYlwFjvNFPD/+/iSxPK7V9mIz4jseM9arXmR9ykpZDrdm3xw= X-Received: from pfoc24.prod.google.com ([2002:aa7:8818:0:b0:736:a1eb:1520]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3996:b0:730:9637:b2ff with SMTP id d2e1a72fcca58-739e5a567admr6139702b3a.7.1743795642377; Fri, 04 Apr 2025 12:40:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:47 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-33-seanjc@google.com> Subject: [PATCH 32/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 52d8d0635603..b8b259847d05 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13608,7 +13608,6 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, * int this case doesn't want to receive the interrupts. */ spin_lock_irq(&kvm->irqfds.lock); - irqfd->producer = NULL; if (irqfd->irq_entry.type == KVM_IRQ_ROUTING_MSI) { ret = kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, @@ -13617,6 +13616,7 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, pr_info("irq bypass consumer (token %p) unregistration fails: %d\n", irqfd->consumer.token, ret); } + irqfd->producer = NULL; spin_unlock_irq(&kvm->irqfds.lock); From patchwork Fri Apr 4 19:38:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038855 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 BC6F322DF81 for ; Fri, 4 Apr 2025 19:40:44 +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=1743795646; cv=none; b=mfHBvkMiXh9plf8EasdkYY+WKVHGKqpjT50KNlJKNLZMcenXHGYpZefNj7c0J/2M5UgotbCfThCrbqDqY+yH6Ur18UtwW8yYDnkj9I7VdmKqCWzpXhAz8v8yMglsl21bv0Qs0QSlD0Y5fpsWydW8GOl2YlFEzTPkamTM0yRg53U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795646; c=relaxed/simple; bh=gAMzCvcXzkDJ9F9l0lrjivM69IhB1IaN/+MJTI/UKb8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dk3QZglvTAzcpUW2NKZUE3WtRxoZDL4+sPi8w9EWMfhV4BW3Dd0gDYMB3kJiRXpl7Wq/bmW24FXPLBBbyGTl00zdiJ4uIDsa/OdUHEbFc2qq/pKE7BVhoaQ5ZC1utGCuPlGolZRXjOJIXVhK020Op2pBU/WwjVPpSrjlEFNoGqk= 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=Ii1y3gX4; 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="Ii1y3gX4" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736b431ee0dso1963799b3a.0 for ; Fri, 04 Apr 2025 12:40:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795644; x=1744400444; 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=H2RYMqQTeJsr/S1FidZ9b5PuuDedAtkVn2laGJYfLbg=; b=Ii1y3gX4ZjGU1Kmp6UZcQLZHbdMaPYrVvBJ5rF1xxmBHWLWykvvxcFqhr1MCZqL8zh s0ZzwaEh8qCF8eVs2naFwfLC2ojzjsO91SPY6pFpGWV9meEOYTQywDellDxzqVNBLzAE TfK7uytC9ZFGpjwsLoAKiR2BQc2NpuW28urBE/VStfmmwTaYNADrlt3CRXINrIFxft6I D0dmEBT77UjjlDQXEIf2R/9fyMpbZyQ+7wtdMX2d/lhj3iMVluHvmw7A9JfHrATwkKRM uC3qPWDQUpSPbpBraC1KCKH1QHQzN8qWUavDH4BVg/D52sk7ZRT6oV+WNhTBCNQkghEA HCOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795644; x=1744400444; 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=H2RYMqQTeJsr/S1FidZ9b5PuuDedAtkVn2laGJYfLbg=; b=JNiblwGMb9oRXRT/GlVjDwDQZpkuwCyq32qf+ZvN4XpqdbOcYRUVMgxoilbSoq+exi L/PsXENtXflHjlxukWrusnQfyKq6iM1FJ777C/8bDAePrm+cuwAKFtETEtUye4xusQOH ekZ9XHATqPWl83mZLhYWIN/ke4b+RNbODrn4Xt8URZVKtAfGxrdq3DW2tEoJyFV/w1xC atJ8kjEDof/+IqyhYoaKPtQsRY0jBvS+ZhK3/lBVY3UIMbtf7NV26wQ5fOvGepO9IuuH W1cj8UhRet+Ao+lW8ML8O8dv3HbjCpRiD+0KPAO9ex8ONS8QgimpTS7uZ4D1Qojltxrf du1w== X-Gm-Message-State: AOJu0YyTw4GeNtw6dEcumoIB2kqmqvK5B3kAbQT4c+nft3xC7oU+VMoq FTjv1El2uVDlaKoFz72qQC8J9ntQ9pbPhbowccvxWONQziGrDfeTmPGSW5HCMsFdELF59qU931I zVQ== X-Google-Smtp-Source: AGHT+IFNJHAsoiO6n3VwVWgJkyAkhRtsy6on7q+rDtSQTDsyUctiC80j/CViWl2oBMuVNP5BPeL148rFMw0= X-Received: from pfmy23.prod.google.com ([2002:aa7:8057:0:b0:736:b37b:f363]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1397:b0:736:51ab:7ae1 with SMTP id d2e1a72fcca58-739e4be4210mr5217175b3a.16.1743795644331; Fri, 04 Apr 2025 12:40:44 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:48 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-34-seanjc@google.com> Subject: [PATCH 33/67] 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, Maxim Levitsky , Joao Martins , David Matlack Hoist the logic for identifying the target vCPU for a posted interrupt into common x86. The code is functionally identical between Intel and AMD. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/svm/avic.c | 83 ++++++++------------------------- arch/x86/kvm/svm/svm.h | 3 +- arch/x86/kvm/vmx/posted_intr.c | 56 ++++++---------------- arch/x86/kvm/vmx/posted_intr.h | 3 +- arch/x86/kvm/x86.c | 46 +++++++++++++++--- 6 files changed, 81 insertions(+), 113 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 85f45fc5156d..cb98d8d3c6c2 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1838,7 +1838,8 @@ 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_kernel_irq_routing_entry *new); + 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/svm/avic.c b/arch/x86/kvm/svm/avic.c index ea6eae72b941..666f518340a7 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -812,52 +812,13 @@ static int svm_ir_list_add(struct vcpu_svm *svm, return 0; } -/* - * 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 = 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 = 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_kernel_irq_routing_entry *new, + struct kvm_vcpu *vcpu, u32 vector) { - bool enable_remapped_mode = true; - struct vcpu_data vcpu_info; - struct kvm_vcpu *vcpu = NULL; int ret = 0; - 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. @@ -865,7 +826,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, svm_ir_list_del(irqfd); pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#x\n", - __func__, host_irq, guest_irq, !!new); + __func__, host_irq, guest_irq, !!vcpu); /** * Here, we setup with legacy mode in the following cases: @@ -874,23 +835,23 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, * 3. APIC virtualization is disabled for the vcpu. * 4. IRQ has incompatible delivery mode (SMI, INIT, etc) */ - if (new && new && new->type == 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 = false; - - vcpu_info.pi_desc_addr = 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 = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id); - pi.is_guest_mode = true; - pi.vcpu_data = &vcpu_info; + struct vcpu_data vcpu_info = { + .pi_desc_addr = avic_get_backing_page_address(to_svm(vcpu)), + .vector = vector, + }; + + struct amd_iommu_pi_data pi = { + .ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id), + .is_guest_mode = true, + .vcpu_data = &vcpu_info, + }; + ret = irq_set_vcpu_affinity(host_irq, &pi); /** @@ -902,12 +863,11 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, */ if (!ret) ret = svm_ir_list_add(to_svm(vcpu), irqfd, &pi); - } - 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 = irq_set_vcpu_affinity(host_irq, NULL); } if (ret < 0) { @@ -915,10 +875,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, goto out; } - if (enable_remapped_mode) - ret = irq_set_vcpu_affinity(host_irq, NULL); - else - ret = 0; + ret = 0; out: return ret; } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 6ad0aa86f78d..5ce240085ee0 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -741,7 +741,8 @@ 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_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 *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 786912cee3f8..fd5f6a125614 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -266,46 +266,20 @@ void vmx_pi_start_assignment(struct kvm *kvm) 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 *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 != 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 = __pa(vcpu_to_pi_desc(vcpu)); - vcpu_info.vector = 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 = { + .pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)), + .vector = 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 a586d6aaf862..ee3e19e976ac 100644 --- a/arch/x86/kvm/vmx/posted_intr.h +++ b/arch/x86/kvm/vmx/posted_intr.h @@ -15,7 +15,8 @@ void __init pi_init_cpu(int cpu); 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_kernel_irq_routing_entry *new, + struct kvm_vcpu *vcpu, u32 vector); void vmx_pi_start_assignment(struct kvm *kvm); 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 b8b259847d05..0ab818bba743 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13567,6 +13567,43 @@ bool kvm_arch_has_irq_bypass(void) } EXPORT_SYMBOL_GPL(kvm_arch_has_irq_bypass); +static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd, + struct kvm_kernel_irq_routing_entry *old, + struct kvm_kernel_irq_routing_entry *new) +{ + struct kvm *kvm = irqfd->kvm; + struct kvm_vcpu *vcpu = 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 (new && new->type == KVM_IRQ_ROUTING_MSI) { + kvm_set_msi_irq(kvm, new, &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 = NULL; + } + + return kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, irqfd->producer->irq, + irqfd->gsi, new, vcpu, irq.vector); +} + int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, struct irq_bypass_producer *prod) { @@ -13581,8 +13618,7 @@ int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, irqfd->producer = prod; if (irqfd->irq_entry.type == KVM_IRQ_ROUTING_MSI) { - ret = kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, - irqfd->gsi, &irqfd->irq_entry); + ret = kvm_pi_update_irte(irqfd, NULL, &irqfd->irq_entry); if (ret) kvm_arch_end_assignment(irqfd->kvm); } @@ -13610,8 +13646,7 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, spin_lock_irq(&kvm->irqfds.lock); if (irqfd->irq_entry.type == KVM_IRQ_ROUTING_MSI) { - ret = kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, prod->irq, - irqfd->gsi, NULL); + ret = kvm_pi_update_irte(irqfd, &irqfd->irq_entry, NULL); if (ret) pr_info("irq bypass consumer (token %p) unregistration fails: %d\n", irqfd->consumer.token, ret); @@ -13628,8 +13663,7 @@ 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->irq, - irqfd->gsi, new); + return kvm_pi_update_irte(irqfd, old, new); } bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *old, From patchwork Fri Apr 4 19:38:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038856 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 9F84122172A for ; Fri, 4 Apr 2025 19:40:46 +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=1743795648; cv=none; b=KNTHhAGvPd7YNW5X9ie9/mcF71nZh2usjlz9gRTkbchNIa6v5EbdYewrXqzEstzNOqtaeEqDYfyItMj+iIawK1Mvuh0S7euCAdgGNDsUboiszbxrpptmcZQRFvelPG6Ey8uRZgjAk1o4FoclCHin2kZPQo4wHPP3DgSP1RFO78c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795648; c=relaxed/simple; bh=S1IQCpBa9erCP4/lm9a+T/slv15e6a/B9F7tRfkzXTs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pip+PwwKMjZRsV3LiF33y3sdZDXY4XEdR7UGuIhrMvKnwlywwFZQctJl6ehzydiGMmvAyHRjcNo+kuDohcWC/wupPZ6glbwnsHFFF7DLQ2wkeV8wASHE0pxVG8+nIQ5w9CGMaLgJY6s2yL1Zf2PwP3LTgVgQW5YIYcy7HHHpo4c= 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=CiJ/bJcR; 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="CiJ/bJcR" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-739071bdf2eso1813657b3a.2 for ; Fri, 04 Apr 2025 12:40:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795646; x=1744400446; 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=uaVpxlbnDgtNQ9eArDqZS6RnH4UAPqkG1v5XnP6kRe0=; b=CiJ/bJcRDGfhiMp8nRs17hBZl77Hnzs9FB3jdyAVsfJmhIodgAfvt+b1dg53t2DOux SQ7Dp4OXeVssclGWIDlz9gAec09GWcDzw+/LRgXMT3bVf0MNy5JkuQY/UrdwcBqRgYRz z2SWr5rZBzoVoWObigTimfkpDTM47u33SzasjmwkMyeicwvjwmiUVKhEdX96g97ikPMg Tbwx0v16Z1KSLFg2EEvm7YZB7+ov2UbHJvqV3iPJ+yXMOyvmtdsOTPLRxgpn+c6zDC0O MqyfGCaIZR9wdw035d10GY/+WD25cjIqcQlJ3evh7Mmak6FGbz6lyIoZ30/ujBZaTgbA pchA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795646; x=1744400446; 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=uaVpxlbnDgtNQ9eArDqZS6RnH4UAPqkG1v5XnP6kRe0=; b=hiSrj5pePqteDuLdWNqQgl9eVSoqg+jEQbULVCcmaiSMVbah/pg3DrOAXolWIDhVoF S3eOmeJo/moQclg3dddm04ow/dO+NMOFVQhELxhS7nGHdJUJcjHVEXIzWVnZZwWqvsm+ 4gsZwLVgv3xM1rZ9xmK4IWMes0ApDhUsCTNy/1D0PgVjuLIORR9Or3KEgmfXORUU2L8u AiUxxQxdTCjGrIvPDfCfRwEPKhEiY8EkmY34F5bnj07NxOLCx+ZQTI6jS1YrmpxPIQO7 xDyUCYKj4/ZYwXgE+luNhiYnqq7rQCD58thnZ2Dvc0SPpNDe+AF9zjKJOYZdT2KUpd5s Lj5Q== X-Gm-Message-State: AOJu0YzTFPdkBML4kfKXsa0O0Lx+ITbPyDwOsyuIAb2T4Ap1QmyMugjZ F2Dw9T3eRmkUQsxoMB3IQLoCA6sc1zb98FotlbGN223zJKpJLIJFRGyLBKVEJSc6QHLfsTW/V6w Emg== X-Google-Smtp-Source: AGHT+IEaPgjswzlO8HHW+mHK05gIj6Syjqlu81aOJEReqz978dEgQfNJanfgf5aiklETS63POvgyQk3Fmxw= X-Received: from pfhh19.prod.google.com ([2002:a05:6a00:2313:b0:736:ab5f:21f4]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2289:b0:736:755b:8317 with SMTP id d2e1a72fcca58-73b6b8f8f45mr861882b3a.21.1743795645870; Fri, 04 Apr 2025 12:40:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:49 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-35-seanjc@google.com> Subject: [PATCH 34/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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/svm/avic.c | 6 ------ arch/x86/kvm/trace.h | 19 +++++++------------ arch/x86/kvm/vmx/posted_intr.c | 3 --- arch/x86/kvm/x86.c | 12 +++++++++--- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 666f518340a7..dcfe908f5b98 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -825,9 +825,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, */ svm_ir_list_del(irqfd); - pr_debug("SVM: %s: host_irq=%#x, guest_irq=%#x, set=%#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. @@ -863,9 +860,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, */ if (!ret) ret = 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 = irq_set_vcpu_affinity(host_irq, NULL); } diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index ccda95e53f62..be4f55c23ec7 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -1089,37 +1089,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), 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 ) ), TP_fast_assign( __entry->host_irq = host_irq; - __entry->vcpu_id = vcpu_id; + __entry->vcpu_id = vcpu ? vcpu->vcpu_id : -1; __entry->gsi = gsi; __entry->gvec = gvec; - __entry->pi_desc_addr = pi_desc_addr; __entry->set = set; ), - 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) ); /* diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index fd5f6a125614..baf627839498 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -275,9 +275,6 @@ int vmx_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, .vector = 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/x86.c b/arch/x86/kvm/x86.c index 0ab818bba743..a20d461718cc 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13571,9 +13571,11 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm_kernel_irq_routing_entry *old, struct kvm_kernel_irq_routing_entry *new) { + unsigned int host_irq = irqfd->producer->irq; struct kvm *kvm = irqfd->kvm; struct kvm_vcpu *vcpu = NULL; struct kvm_lapic_irq irq; + int r; if (!irqchip_in_kernel(kvm) || !kvm_arch_has_irq_bypass() || @@ -13600,8 +13602,13 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd, vcpu = NULL; } - return kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, irqfd->producer->irq, - irqfd->gsi, new, vcpu, irq.vector); + r = kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, host_irq, irqfd->gsi, + new, vcpu, irq.vector); + if (r) + return r; + + trace_kvm_pi_irte_update(host_irq, vcpu, irqfd->gsi, irq.vector, !!vcpu); + return 0; } int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, @@ -14074,7 +14081,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); From patchwork Fri Apr 4 19:38:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038857 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 3168722E3E7 for ; Fri, 4 Apr 2025 19:40:47 +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=1743795650; cv=none; b=pQvFWxqFv9Iqve0cre/gkcW9VORorAc8gZNAiQbqp3FWFpoNLED3CAVagaM76OMOSvWweN5eyp/fBX7J6nItb6VuY7GUyqGx8HynyPfLLcxd/aMP4lW70TRrJtrSs6AMe0lwq+ochshL6qDf5O/GakLYISxOOFUohNkAwcm3uII= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795650; c=relaxed/simple; bh=IzCJp621ChpF3CQ8d6txaWCGos/Pxg81tWUUPEDnhso=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TM/5OWUOLmJJ7Ubnqp11AiEHqIMN5QGFUh1kVUUpHhNEQE//yzeRPCF/oIQbyJBxZPInsjztAmioag0Mk46UJgU2Esop+UJZvRlHOmFW9Kx6baOyeQNcrCEZQRNnUQWJNzMmO6j4L4IJP36x2i7HtVXC6EYZ5KnrgDDKQQ5OvyA= 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=3xG6Nmqk; 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="3xG6Nmqk" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-af972dd0cd6so1649024a12.1 for ; Fri, 04 Apr 2025 12:40:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795647; x=1744400447; 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=KYDMvc/wv5loDVXS9Aa0v44uXiIbogLzadPCYMuhYAo=; b=3xG6Nmqk78M9AzuOJ84gJPfIrCZLuI66N1ff2VqpwIamutnGdCwQXFgchYlnOC4NpX j/+mtg6U7dSmjRMpnN69JGGe0v84VufjIrtxDhhIzoPzW4r5tLFLW3oOXqyuaSSekHJS fssa/owanQ9Ws/tZBt77rtURM1oCttbFMbMs7WO8HLlBUfSJELGJM8kBI43uH/rG0vPi AUHK4IAyhap4o0hrNazfGL9VW/fM9dqVH30Uy5UV4BuVmjfhla8TJ8Qv6QN/nN2UxzRI gvagR20jugrjFM0FGY6o/pRK1qQyaicNcTZPAbjcpjX0GEsQDdnWn3Eu6xJYXGgLam3h a3pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795647; x=1744400447; 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=KYDMvc/wv5loDVXS9Aa0v44uXiIbogLzadPCYMuhYAo=; b=ABteTtEcpWU1ExljJwdFMPercu/6ldaNvhbo5PYNPHP05RioNtzyzVpVm8q6CScTPM IhMVELt3qp6GXOcUmZfPSrmfACv22iacS4YRLp8SboZNy90Itf8baf5w/ZFFJHLKrTUP /EYrma66xbmRyrHd3f23YCHoOpJGzYzRDvmijhBsys8QVuEuJJxXFjwWtk7NMYZ+5G/r TD8LmTcreV2FxK6LL6v2jCU6Z/lQpXLnqo4w84vjOQ5b1ZJn0Pr+ZmV8GD2o1HfjCrZ3 uC6kjtKWMXffRdrPatMzSy0DewfPVhAKEst7666zUnpx4+gNW0I6PyZqgxVxDkDCDNzS 8sVw== X-Gm-Message-State: AOJu0YzXaUOoMoXE59I3xvdvjxT6Nw6gfRJxAc4iW42X9aalC5Jirjcn PmMVwg6B7/mNNamsf1nxb9Y4aUQBD4d9UroRnDN+68Pt03Fz0JHIAYw/PKLn4889rR7uax34V5M A6A== X-Google-Smtp-Source: AGHT+IErXqsvd0WDpARxVffQQIxJmZ1gHBUNSi2cCsl6CNRLxY8uKCXZMQn+YonfMwuOH2OZCDTOEAintT4= X-Received: from pfbay40.prod.google.com ([2002:a05:6a00:3028:b0:730:7b0c:592c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3102:b0:1f5:80eb:846d with SMTP id adf61e73a8af0-20107efed2bmr5565765637.10.1743795647339; Fri, 04 Apr 2025 12:40:47 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:50 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-36-seanjc@google.com> Subject: [PATCH 35/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. 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 dcfe908f5b98..4382ab2eaea6 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -817,8 +817,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, struct kvm_kernel_irq_routing_entry *new, struct kvm_vcpu *vcpu, u32 vector) { - int ret = 0; - /* * If the IRQ was affined to a different vCPU, remove the IRTE metadata * from the *previous* vCPU's list. @@ -848,8 +846,11 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, .is_guest_mode = true, .vcpu_data = &vcpu_info, }; + int ret; ret = irq_set_vcpu_affinity(host_irq, &pi); + if (ret) + return ret; /** * Here, we successfully setting up vcpu affinity in @@ -858,20 +859,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 = svm_ir_list_add(to_svm(vcpu), irqfd, &pi); - } else { - ret = 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 = 0; -out: - return ret; + return irq_set_vcpu_affinity(host_irq, NULL); } static inline int From patchwork Fri Apr 4 19:38:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038858 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 9DBA522F14D for ; Fri, 4 Apr 2025 19:40:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795651; cv=none; b=BTpyzv0+2ar/Zn8TIo7h9lPl188AT/E58SzVnLLQ/ibm8cE++Vik58gOYwy71ezzsl6wfFLEYIz/75OQVSfRAALnma1ZLj2z3A/5W3S6lsZJ0yrpZ28USLCt87qrvBnJtf5KVm0sLM+SjSijfzcD+D9zj+LXrVRqoW8PMs0BgCQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795651; c=relaxed/simple; bh=QVytKcLMLwjvtacZRhoTDTCcvk9oIVDgKzcE8SfOka4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YIztyuGQ6UB26RIsego6574Qj5K47U9cf8QjLiog9R83Wj1kO2s02jCDVtj8MvQsRPSDoh3X+s1iaTVjNyMThUzO1Tor/iM5Xc0cBXl0FaBhUUSHohZuY5BYqfnv7bmlmNOISG7MZbGt4npLcUpiVpZHprQ2+mDt6phdmgccvPc= 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=KP4Kulsa; 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="KP4Kulsa" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ff52e1c56fso3250114a91.2 for ; Fri, 04 Apr 2025 12:40:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795649; x=1744400449; 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=7/Cs2Fc53f29HQTq4mv2ZlQ9k1Jo4Mf2ffuO1b42Tf4=; b=KP4KulsaF067wttd3x6ZXg2SDKqFTql2hl+rCvJV3nljCsJVKvA5zQOWcL4SN18Lq+ q5Xmw5unxcfKiC/BhEGOxEo7CeQpjJXim29AZSOdCFWe7wGBAOw35c02xQ9+1QzuUU2d f5mfIe/cLBMuT0bNbfLp7SLGHvPskSlb6NJnZYBGjj+226I4p3j7fZqj3xUdtqCPWonF 0bK6sOjRGWA9xdygP3yag3tTZ383iltfq4CiNpJpwj2RqxFji3A5h0/7FFNQdrtShl1t ddtbM5is419sGO5FtXd1mleQ96S91wBIxpAYb5PJ/WgfkBHCJBLoraIZEbpfhVi7jySm A09Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795649; x=1744400449; 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=7/Cs2Fc53f29HQTq4mv2ZlQ9k1Jo4Mf2ffuO1b42Tf4=; b=t+8Se7ADFaj7nHyNCCtLxUK2xkkK1KKEPaG32KLlAdQA9RA7EMd2999jM4HUkBN4gA TquVv+q8k26FKVtUZvS6HiR+Atl4Z0OeV9cyHtMqeJ9o4EyUerTGD84yJLqxG7W4fmzs wuw/ZigyYIwFc9LPXWAcSRj/9xSiZ9a9z/XEjzRpjvq6JOIS1eRylHf9C452AX+ozSKL wNwZGzw9FSZvckxeuns3SxDGYwX4Gw5uBoAgOe52R30oZViEo7IECuKHZ1Uv/ikul2s5 eUJpnkX+zpB1gumtSoHGf4/LxLW7yzFqa9PAeNUUDSjBh1CKSHlDdlP28hFIRfVAYJHT eshA== X-Gm-Message-State: AOJu0YwUxDgV/g9hZ01phmP3mqjUNQW1XSxypfMkxHqkjtt2GSK/qSqZ XW1DHVRpTepnbUIKjzki3+syLaG+qnTckYT62WYVQDFypCg8J38+YkqWOl3mBFhqy9CIR+lexFX 33g== X-Google-Smtp-Source: AGHT+IH58D3xxc8J66XG4flIppBKdnX8bdKHTWvQ5Mizc35RqXeILg635z0uscQCli4EDu258GtvZcGvkSI= X-Received: from pjbeu7.prod.google.com ([2002:a17:90a:f947:b0:2fa:15aa:4d2b]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:6c6:b0:301:98fc:9b5a with SMTP id 98e67ed59e1d1-306af704d02mr660629a91.6.1743795648884; Fri, 04 Apr 2025 12:40:48 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:51 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-37-seanjc@google.com> Subject: [PATCH 36/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. 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/irq_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, }; -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 4382ab2eaea6..355673f95b70 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -832,23 +832,18 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, 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 = { - .pi_desc_addr = avic_get_backing_page_address(to_svm(vcpu)), - .vector = vector, - }; - - struct amd_iommu_pi_data pi = { - .ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id), + struct amd_iommu_pi_data pi_data = { + .ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, + vcpu->vcpu_id), .is_guest_mode = true, - .vcpu_data = &vcpu_info, + .vapic_addr = avic_get_backing_page_address(to_svm(vcpu)), + .vector = vector, }; int ret; - ret = irq_set_vcpu_affinity(host_irq, &pi); + ret = irq_set_vcpu_affinity(host_irq, &pi_data); if (ret) return ret; @@ -859,7 +854,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 baf627839498..2958b631fde8 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -270,12 +270,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 = { + struct intel_iommu_pi_data pi_data = { .pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)), .vector = vector, }; - 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 08c4fa31da5d..bc6f7eb2f04b 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3831,10 +3831,10 @@ int amd_iommu_deactivate_guest_mode(void *data) } EXPORT_SYMBOL(amd_iommu_deactivate_guest_mode); -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 = vcpu_info; + struct amd_iommu_pi_data *pi_data = info; struct amd_ir_data *ir_data = data->chip_data; struct irq_2_irte *irte_info = &ir_data->irq_2_irte; struct iommu_dev_data *dev_data; @@ -3857,14 +3857,10 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info) ir_data->cfg = irqd_cfg(data); if (pi_data) { - struct vcpu_data *vcpu_pi_info = pi_data->vcpu_data; - pi_data->ir_data = ir_data; - WARN_ON_ONCE(!pi_data->is_guest_mode); - - ir_data->ga_root_ptr = (vcpu_pi_info->pi_desc_addr >> 12); - ir_data->ga_vector = vcpu_pi_info->vector; + ir_data->ga_root_ptr = (pi_data->vapic_addr >> 12); + ir_data->ga_vector = pi_data->vector; ir_data->ga_tag = pi_data->ga_tag; ret = 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 ad795c772f21..8ccec30e5f45 100644 --- a/drivers/iommu/intel/irq_remapping.c +++ b/drivers/iommu/intel/irq_remapping.c @@ -1236,10 +1236,10 @@ static void intel_ir_compose_msi_msg(struct irq_data *irq_data, static int intel_ir_set_vcpu_affinity(struct irq_data *data, void *info) { struct intel_ir_data *ir_data = data->chip_data; - struct vcpu_data *vcpu_pi_info = info; + struct intel_iommu_pi_data *pi_data = info; /* stop posting interrupts, back to the default mode */ - if (!vcpu_pi_info) { + if (!pi_data) { modify_irte(&ir_data->irq_2_iommu, &ir_data->irte_entry); } else { struct irte irte_pi; @@ -1257,10 +1257,10 @@ static int intel_ir_set_vcpu_affinity(struct irq_data *data, void *info) /* Update the posted mode fields */ irte_pi.p_pst = 1; irte_pi.p_urgent = 0; - irte_pi.p_vector = vcpu_pi_info->vector; - irte_pi.pda_l = (vcpu_pi_info->pi_desc_addr >> + irte_pi.p_vector = pi_data->vector; + irte_pi.pda_l = (pi_data->pi_desc_addr >> (32 - PDA_LOW_BIT)) & ~(-1UL << PDA_LOW_BIT); - irte_pi.pda_h = (vcpu_pi_info->pi_desc_addr >> 32) & + irte_pi.pda_h = (pi_data->pi_desc_addr >> 32) & ~(-1UL << PDA_HIGH_BIT); modify_irte(&ir_data->irq_2_iommu, &irte_pi); 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 @@ struct amd_iommu; -/* - * 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 struct task_struct; From patchwork Fri Apr 4 19:38:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038859 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 1CDF522F3A8 for ; Fri, 4 Apr 2025 19:40:51 +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=1743795652; cv=none; b=X6fY4hgs1RQiIMTy+ueY0T+mFXNMNh8MzQAHYP41IYJjXaeq92gotZS5YD0JSQim5sLgwv9ILMJuhXMMrsxJ7X4NPQyHVkoHeroOg0YzBNMIeAIhQ4w0GJoNN9MOyuXT3EwpUhnF6xXMbtl45q9KJQjJnZhDhOka4MOofvO2YOI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795652; c=relaxed/simple; bh=7/g8u2TqM7J8Cn/U12YGDIH4z8F9epXVF7IH/1LlmGo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G8GdytYxpykkV62NnGgY4/ZJlNkpp4dV0BR21VKuvXPNJYIQTCMzo4GSecWPMuo9kr/fYFNgqyiPFA/nFnhaJCLib1/MrIYPre07+Vyqv3U3LdQLxCvJRZoeXg3ibYoAj5HC9phjrdV7GCGtqLAiKV4zjQpsvTN/Ar6ZaPMKMvM= 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=UE+4Io9/; 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="UE+4Io9/" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736c7d0d35aso3406154b3a.1 for ; Fri, 04 Apr 2025 12:40:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795650; x=1744400450; 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=SjSWA34yg4j7nAkSg9OWc1nxrjTv2EBF262ygp4BvLg=; b=UE+4Io9/+SEz4Iv0RtegKSYtIvcGTSQO0yqbJUzm4RF8CtPis9YlL7fax36VeLk9Fs SXUTfLWrFoxRGXhwEfNNmybamkhlxgChcHbLghKsVFWFL8ChIZpUIYRQNi+v6+69xOux OBAUV/45HPdnMIXnVCuPmPUGmOVz2qqDBhxDT0cHX9pasaPnE2Fi2/GPJfM2gmJp94/p p5zDc4LsLq3dn4sQ8qdUnMwuzZb3zidlobgp9BSDY9WbPbAxm488VKV+AKuhm7lTndtd S2CzLyRQ1/terZxUjcbwAc/umHd3wMK0kTb6N3JpK3ROBsj4PUxmISBLPgwywfbRe6N6 nF1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795650; x=1744400450; 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=SjSWA34yg4j7nAkSg9OWc1nxrjTv2EBF262ygp4BvLg=; b=qNBP6nDNwx3eWDpJH/2u0IJ1RUajOHgLgYcMKqQ8Zk8tM8q8IUcTpeMSjOiMh4b2h2 fGHYuzjs7njqgcnUfNMJ4IhkSl4Aaja1WZEzwBqWxPF4bD+IDVVGeJvJ7dhcFgERwLFJ tLc5L+Jmc78NRu/ciej02qyl7/gDOhc3FCbXh56HP0w5XHfEfWRJtX3ZYVuYgTHx7ZMW DfLBPwo/HRB5xyJmg7teinw2CXI36Xn9KSGlZBSUHDEZ8CH82RpcSiMppsl9M0hVN7RV 9XuKyowmljyhb/iZJYZeWyUevLBkiWAQux1g0QJO5DoHHHeSh1vWvSAjW6bm7OHGOdwF 84Aw== X-Gm-Message-State: AOJu0Yws3m0IffTGPq+IThph2SpUhexBYNfP8N4qZX2JWRkeFf/OXj0x AXhuMbsm+xlk9stz4UMTRv8O3L+INXesccl7MONqF5I0qXnRPHhe5tqqcdvB94qwl80J7yBJkF2 TiQ== X-Google-Smtp-Source: AGHT+IHbhyhf2PL38jR1CfEUWzgJOUIR/lwrsr+RqHeIsaC+HvnuthN2QMdbVQyQ8EZYibpHmZF2PVBlImU= X-Received: from pfud4.prod.google.com ([2002:a05:6a00:10c4:b0:739:8c87:ed18]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a04:b0:736:3e50:bfec with SMTP id d2e1a72fcca58-73b6aa3d9b9mr864255b3a.8.1743795650653; Fri, 04 Apr 2025 12:40:50 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:52 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-38-seanjc@google.com> Subject: [PATCH 37/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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/x86.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/x86.c b/arch/x86/kvm/x86.c index a20d461718cc..c2c102f23fa7 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13666,11 +13666,11 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, 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) +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, old, new); + kvm_pi_update_irte(irqfd, old, new); } 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 2d9f3aeb766a..7e8f5cb4fc9a 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2392,9 +2392,9 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_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 ad71e3e4d1c3..7ccdaa4071c8 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -285,11 +285,11 @@ void __attribute__((weak)) kvm_arch_irq_bypass_start( { } -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; + } bool __attribute__((weak)) kvm_arch_irqfd_route_changed( @@ -618,11 +618,8 @@ void kvm_irq_routing_update(struct kvm *kvm) #ifdef CONFIG_HAVE_KVM_IRQ_BYPASS if (irqfd->producer && - kvm_arch_irqfd_route_changed(&old, &irqfd->irq_entry)) { - int ret = kvm_arch_update_irqfd_routing(irqfd, &old, &irqfd->irq_entry); - - WARN_ON(ret); - } + kvm_arch_irqfd_route_changed(&old, &irqfd->irq_entry)) + kvm_arch_update_irqfd_routing(irqfd, &old, &irqfd->irq_entry); #endif } From patchwork Fri Apr 4 19:38:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038860 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 C9C8722FF37 for ; Fri, 4 Apr 2025 19:40:52 +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=1743795654; cv=none; b=MkbSEN/BiiyINT9NM7/MQ0YEYU7Xfq43dlpeF//BmonwovnZw2fFeFTp8pe7RYWMlYYehogvk2uaWQCgk2rOxlkIYZNOahDmeL0NltD/eF/XO0VywIb+xt8izVBPGbdy6o2CIZY7AAvhjeW0qoSm9YAQ0S3aJ1kJQRXOw7PDGZI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795654; c=relaxed/simple; bh=7OuU0INrb67bLssASox/0pk9s35pF8A3UD4uJddP1+8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ob6+lEtmgRf76Wv8CvIml0ikkSBmZC7eyk8CQW1A6AGZm5S+8bwMe6b02jQWdOHFCw/intqdbV+mqDJ9dUXd+0bl3PXYdWYs1SWjCx32Qm7CF2IaJrYZFn/a35EbsRPMeNZhYEUGx90QPOIQL9PWqi6kwPeWyN/zShrmD543HxI= 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=ETENYD6m; 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="ETENYD6m" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736b431ee0dso1963869b3a.0 for ; Fri, 04 Apr 2025 12:40:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795652; x=1744400452; 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=to5q3LlJ1MsoyXzAExk8MPpf5aUTO++pOYAb5QB6TBk=; b=ETENYD6m6eGNFOVkDnt4PkFO8OGz61tNaMTwmfcdK17lJen4EuMqlgA37ITT9eOJJ9 aR6p9EEVdOhv+5pq6RSTjsORxXnoTV/O56W9N/s5Dm8Lc8tZhk+dsjuI5kUpIGrZgKMH JttGowXcywKTNsI+YkUtrTlbKTvIX02WURz6KPdvD++AlxzRlPDVGA2PybXyekEXEa5d kgxIZmuMbKSTuRK5oVeiUk6/TBMwLFC5D3rjjzKF6JGF3L5cCRanxrkJV+VAvrscNKjZ sOLMVWL+KcX9XAuctTbLQxCB56QsY2lB7DLuEL6qlBDTur307DWlNyptXIHoQhDbhF2R gl8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795652; x=1744400452; 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=to5q3LlJ1MsoyXzAExk8MPpf5aUTO++pOYAb5QB6TBk=; b=VKme6BcthPQEx2DqAwVKoUzCEuh4G/AkcHFrrKHFVyw0q2T0xT0TnJzvCsOq1EPMpP h75+cXm5ejKIxAs810Adc+EVyqnqk4FJTCtQQRiub+4Ah3Vatd5FTRYxtSXFZuEzKbEj Bnh1LB9+umP3PNpDFP8ro7XpIeMbxUjAhRtznZ+Ra4IySzej496Yihx4HYCOZVRS0+Q4 9zPOaYdCJmW41OddNcrEhC3izShHPNXoDLbW49Wt41U/gsRX6NKRLx2NTcpS+1BYctG3 bqoB4JVj7v+puHCvT5xcSjA1aCGNqUBh4GqTnRrtHkzQcJ7AXj2gFdlHxfuxVO3leUUX mNOA== X-Gm-Message-State: AOJu0YwYTMPLtdbajjjGLmnJNzwrl8j0lM1tv64k4P8FOH0m7ePXAKIj 1cXCaAj8wwnpz6bLAVVjAVBKBZge0p78ionM5EB/rYXcWMXMde+NXhZxRPhzPPMXPkOLhcCTEzH kkQ== X-Google-Smtp-Source: AGHT+IFBZzBWaHbwwFG6Bp3HiVISkrZHpjTmYRH9gm8aunapY90POvgNg06gKNVSWknhYJ3e06jWFbrM4bY= X-Received: from pfiu14.prod.google.com ([2002:a05:6a00:124e:b0:736:af6b:e58d]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1701:b0:736:ab1d:83c4 with SMTP id d2e1a72fcca58-739e46ba78cmr5647788b3a.0.1743795652384; Fri, 04 Apr 2025 12:40:52 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:53 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-39-seanjc@google.com> Subject: [PATCH 38/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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/x86.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/x86.c b/arch/x86/kvm/x86.c index c2c102f23fa7..36d4a9ed144d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13670,19 +13670,14 @@ 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 == KVM_IRQ_ROUTING_MSI && + new->type == KVM_IRQ_ROUTING_MSI && + !memcmp(&old->msi, &new->msi, sizeof(new->msi))) + return; + kvm_pi_update_irte(irqfd, old, new); } -bool kvm_arch_irqfd_route_changed(struct kvm_kernel_irq_routing_entry *old, - struct kvm_kernel_irq_routing_entry *new) -{ - if (old->type != KVM_IRQ_ROUTING_MSI || - new->type != KVM_IRQ_ROUTING_MSI) - return true; - - return !!memcmp(&old->msi, &new->msi, sizeof(new->msi)); -} - bool kvm_vector_hashing_enabled(void) { return vector_hashing; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7e8f5cb4fc9a..d1a41c40ae79 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2395,8 +2395,6 @@ void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *); 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 */ #ifdef CONFIG_HAVE_KVM_INVALID_WAKEUPS diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 7ccdaa4071c8..b9810c3654f5 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -291,13 +291,6 @@ void __weak kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd, { } - -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 static int @@ -617,8 +610,7 @@ void kvm_irq_routing_update(struct kvm *kvm) irqfd_update(kvm, irqfd); #ifdef 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 } From patchwork Fri Apr 4 19:38:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038861 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 A37AB22173D for ; Fri, 4 Apr 2025 19:40:54 +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=1743795656; cv=none; b=btjD+5uorYgbPviwvGElnqrGDDgoLlHZR2rGiUdw+ZFM0CiO8pAm+z7OrlcN4X8E8tLk1af/HiMm5jf2QZwp0AZ9pcm5y8aRCNBi462YWfujMA3EdArhuLRUgm9w79NsIrvp5/cSaayX1srZm9R/PyGl77jabTOIahYY6rj8LCM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795656; c=relaxed/simple; bh=7iUSFx5D3yR25IHThTgAc/IOAW4zYfJ9SgblmRez7ms=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Jh72X8sP2YDNc6tls+sofbIVHGJQShNgUr22uPQ1VFwlJJ4vE28i+NL7XtgvmOG+wiMkIau3w8cPPn6Y9xRqKVsfI395uTfuuhljGsuOErdzyuO1bpj6r+QNm1XiwLHlgD1ZZDYXwpUSlg49BzOg/aaclvZXRl7eQhg+B4Htiu4= 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=3j+Tjnhz; 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="3j+Tjnhz" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ff58318acaso3859465a91.0 for ; Fri, 04 Apr 2025 12:40:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795654; x=1744400454; 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=4e9ffwdEp7Gqw+G1mVFy+m5vTrdwBux5yCWrxBIAEu0=; b=3j+Tjnhz8z7z2FPkWIJ8C7hlfq8ctahfr5jeWFcYVCJoJIODcyQL3hir0LYRfPKa6F 3r45gAHbDP3tGpZlqgOl70ylZrxke5A8E2ycuOs1WtHCwkUfaer6urwWNa81EJ/4w//A 6jueSE00wS8RSFvhdSXy/mt0zKH1SSkRxOMmAaSSay/4xdON8qV8Ug7ghZwyGrgDuMNV 69HYIu8utRw0G5dR3Cjvexa2sTC8gpAqy9dHcyM4S68nWp4Q/pOqeItpYxZJP0wcTNqJ bazR+1EUr9Q3gn88fE0AE3q4JzugSbNVUusayJ8unhlJQ6OssWLen6ANSdPro8/OZk8a eIDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795654; x=1744400454; 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=4e9ffwdEp7Gqw+G1mVFy+m5vTrdwBux5yCWrxBIAEu0=; b=JNG7tb+HgmJkZbfLGu9TrBADXMvMg6QovVjboR23GMggrqIWugxD5EM5j8CxJ+Thxp v6pRvekwPCJQq8/QSXb7+/XlNg7BZ1ckdj0djhcXorFk89mIEfyLAjX5g6Z9Gt7U3oqh YCbG+7K6HbpfagV9LuOe+VZWoq0QK4z26e9joBJQfk9WkOaXOmFxsT8vWEZqSDX+3QWM B1+MvWwyLTiDVdMBPORwp6ZENGWlsZcp4yYlYJnyc8gqDFA/V0KBfmY18nijYxz+9YNr HUBu42R0T069nY+OcMIUXlvfkM3WowOM/Rme+BzPPv40ibS/hJrD3ph1V/a3tlwA0+lW WpGQ== X-Gm-Message-State: AOJu0YyXsEYtN1ASyQEn+MEHxJW/xfjyEBhQ27g4N9aqeoiZSPxkJoRV GwrTwIgpovpJA6pW2wWExQpt6ok0Ww0UR5+AeOER6m/ghSv0V9jDVCJ70dLu6bDLx5T2v4BTyuW 2fw== X-Google-Smtp-Source: AGHT+IHq0WDQdQXdmkR68srWyYUi8Jk0ER178ssR+69T3RU1Hrt5+d4NUY61J9tgaeNmx2ouyINmHwrWyBg= X-Received: from pjbqn3.prod.google.com ([2002:a17:90b:3d43:b0:2fc:c98:ea47]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:c2d0:b0:2ee:d193:f3d5 with SMTP id 98e67ed59e1d1-306a6120999mr5163910a91.7.1743795654132; Fri, 04 Apr 2025 12:40:54 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:54 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-40-seanjc@google.com> Subject: [PATCH 39/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 4 ---- arch/x86/kvm/x86.c | 7 ++++++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 355673f95b70..bd1fcf2ea1e5 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -776,22 +776,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 = NULL; } 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 = &svm->vcpu; unsigned long flags; u64 entry; if (WARN_ON_ONCE(!pi->ir_data)) return -EINVAL; - irqfd->irq_bypass_vcpu = vcpu; irqfd->irq_bypass_data = pi->ir_data; spin_lock_irqsave(&svm->ir_list_lock, flags); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 36d4a9ed144d..0d9bd8535f61 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13604,8 +13604,13 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd, r = kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, host_irq, irqfd->gsi, new, vcpu, irq.vector); - if (r) + if (r) { + WARN_ON_ONCE(irqfd->irq_bypass_vcpu && !vcpu); + irqfd->irq_bypass_vcpu = NULL; return r; + } + + irqfd->irq_bypass_vcpu = vcpu; trace_kvm_pi_irte_update(host_irq, vcpu, irqfd->gsi, irq.vector, !!vcpu); return 0; From patchwork Fri Apr 4 19:38:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038862 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 236A9230D11 for ; Fri, 4 Apr 2025 19:40:55 +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=1743795657; cv=none; b=MwaxKlTjMz9G2JRv8qcCrMWYjFyaB8d+p6/O30mp/qdsEM9VetAbx1BbbGtwlEQEmmd70Qhb0XWuIEE0Xv4uI5n2Zw7PTM17GwuqcPPLFu+xL7KtMcThuWDnaukEd9dmbgKLXNyPh2TCHgmAOBpqsg7Xf5Dp0li1+DLAKhBvY10= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795657; c=relaxed/simple; bh=rAx4qAmD55M9tfIGXl5YoCVA83RQWFiazrLX3izC4S0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Wtw/InzoAT0wZDmeS7S0GdOPXnqEKYynbWMMp6VC1aFoOif3QHcvL0HpU+OS3Y4k23sD3brzrERBcrQMTUnThoLG/ogYtkjJokHHUJMfEbEqg9phWlSmoZHR4a9Ch1T/1b5ATpOr3vscML1Zehr5fDbYw0wvFWqiFxIBvucSuVs= 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=d39jnbMn; 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="d39jnbMn" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-af51d92201fso2724505a12.0 for ; Fri, 04 Apr 2025 12:40:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795655; x=1744400455; 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=bZC+Ik33WL0W2RRbulovk+dibdQdMDMKKv8TRtkvhyw=; b=d39jnbMnmHb2v/Qoz4d4/kycFUFsD2IYWQW6J/HgRN2IpucxpZH5ptv1UMmxKqv825 nggjK6zAGDNBzuS5wbE9gp2fC/X/NvTvV9dcroek82ZXsTOymkCdhqwPmVHO5b5XQqIK tOe3VYlpZs8hdycNJso6FwnFY2dTh9Eeif3GhdiiL9oTx1KsPTNR/JyJuUxujcQFzb1C E8kiTVm2JwnixZsU1vDSVGQRlpkmONz5Db6EP4Xy/fnlAcKO+hjE/+5SmVmQs9+2Ojbb clJ2H0YZhigDWwJa9lcOxfe705u++sPOxS1qXJIANZvm+Z7FbA1N0vqLEZCq0jmnoxSo ncVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795655; x=1744400455; 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=bZC+Ik33WL0W2RRbulovk+dibdQdMDMKKv8TRtkvhyw=; b=cHikoFY+0UkTg9hwIOr7V0C5kxMzUhCt24KHilBhqVPLAHzT/jJ+k+pgBAIbkdTF6F QOTdOCY8IQHmscNOFfFypMPyy53ervAZhYY/P8549c0bB9r6h+xwsjpG6uhDkx98Dcxm UUQHjjQ+qKxd1L+i+uRmgGVLx99lNQ/ecgrkA0Je1UiccgSmB6QFmv03HMLq0GxZu2ul CL12CP2ulWdyTtS3MCk8fJl2PAXk1+Nap3D4mb5PKd5Xx4GK1CR9nqrlX3GC66dtM0T4 sYLyCZ2bJm9P979UtPNXrHkV8YoSMOrEIVNqILD2J4y59FiryQS+/heAlicv8u1Sae9P X2BQ== X-Gm-Message-State: AOJu0Yz2MzU8gUQ8k/lLulvQ8Us4bQHrAqvLn8u+zqMrKobwHoctBaiV wdz1icvBgmZ8m/dq2NoORO7m0o+HxA0n+H4RgNj+nbbHwDz/6oL3XQgNOF0noe8YbSGDtoGQGjg Iyw== X-Google-Smtp-Source: AGHT+IFQWL9LigLNP0a/K8uREP9gQEOYzG81Hr4sts6sxEYIr+yeo+mdbybE7LvF1mbotb2pbsREFg7UZT0= X-Received: from pgc1.prod.google.com ([2002:a05:6a02:2f81:b0:af9:553c:ea73]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:c78e:b0:1f5:7007:9ea5 with SMTP id adf61e73a8af0-20113c0964bmr1123119637.2.1743795655565; Fri, 04 Apr 2025 12:40:55 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:55 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-41-seanjc@google.com> Subject: [PATCH 40/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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/x86.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0d9bd8535f61..8325a908fa25 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13602,6 +13602,9 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd, vcpu = NULL; } + if (!irqfd->irq_bypass_vcpu && !vcpu) + return 0; + r = kvm_x86_call(pi_update_irte)(irqfd, irqfd->kvm, host_irq, irqfd->gsi, new, vcpu, irq.vector); if (r) { From patchwork Fri Apr 4 19:38:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038863 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 CD2DF2343B6 for ; Fri, 4 Apr 2025 19:40:57 +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=1743795659; cv=none; b=rhBmscZ1yplGYG+tQXg84JWI56Px+gXxXdD+tUrvy/M47l9OctMcmY7zUjpjnzmqqZp2gqHCP3U97cYEzeHbWxAo+p/0N3Scv98lB6x2ZGbYPcBqtqa//SB/4dcYmbK+QFSgXNw92r/4h0Z9t2HZNQcQ0S15I4sLjEdbD6Q4tGA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795659; c=relaxed/simple; bh=qRJSMg/wJdVt+yvoikFVSNe2Xy3I3T24yQmnIrUC1yE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=C8yC4pZXIDF6tCXwI9iS2299oWDT4r+P7r/glJIn6BfQ6GYjcUbQg5MkpAurn/THEYHHwCukWtsCA2cI40uUz4fw5t055OTmz1RhxsVuH7esxpQv2h9mPvwf6lbeZ0Tkbt3yJQ934vimxDQJdJ/OocowoB/PDiBt+WAV0dEYIy8= 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=qO70dyr8; 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="qO70dyr8" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736cb72efd5so2196528b3a.3 for ; Fri, 04 Apr 2025 12:40:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795657; x=1744400457; 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=kz4x5Upf+TkOuVEZrNth6TOEjNFtX/45/lAAaTZzYMc=; b=qO70dyr8qbrEFudkBQBPW17woNOmd46w9P+8oZE/ZjbgtA/9eD+bHDZFkkIgvwS8ay myWnqd8sUBjAdiJHcd9b489jk8qYStgsm72dOwrbI3E6jE2XxPDPLO8KgNJk8pyi/gkU MkP1zBkWZv6SNN61T0hqE+Eew09xWRrn1I1B0DzuweN/ehWf6QYdkD5H0a+X4tMBS3Mj 8olPNb4B2NOx5hmDG94Elaf9rSzbiVYHwyEN9cSrOG41SZFae/XTPcc4dHoupVaHJt0g sEgWIb3ZyYuSLn1cOBnAn3zsE72GgUdL7SfEYTo3U0FlhLfQgWYxFeLNmD+3ro+75sTh l97A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795657; x=1744400457; 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=kz4x5Upf+TkOuVEZrNth6TOEjNFtX/45/lAAaTZzYMc=; b=EXkxvJ6kvgztPrFZQNolUCfMVZh8jE9JOm8WPGz1lYmJViLTM4meJ3qOcniIjOUpl+ mZwYmosk2iR5+Gay5Gu07AFdrca8AH0Rqt439KxR0yaaO9X2mz38CP0fGSYxRfZUhy3p SBXuJcaP2nNFZA13hou3PgeXZw8Yq7zgHQlmykvXSp/NTlnsHnrqeDzsdOEwQHsA/Uee qflut4GYZx5/LWsL4igfQvsTOJcqGgrkZQ/iDNdki1qW8mAc7/E2SufoJVGu4naHl+KC Cx//8nw2CQyL+v+MlgN37CYH9wgtFrU09POknCU+TW/VfFbm3QXMx0W+0cGoa6MGQGhh wC3A== X-Gm-Message-State: AOJu0YxL1lP9dWgu3tCt3kyP/9Ir6OczTKPIqG11VJ7gFN7a+K2yudHC +qP6B8InvadDTKpr6QDx0xktnzkpCmK1TGcuCJF3l/gr1ATqOClv5byq8GfdYbbK8ep20Re/5AO viQ== X-Google-Smtp-Source: AGHT+IHkLFUYrRFyJCAgYTx0bID7tXQUTRqySewXCzFOP3XCxELB+3z+iKZV1p70EJLDf5uCBKxj0CFwan0= X-Received: from pfxa13.prod.google.com ([2002:a05:6a00:1d0d:b0:736:a68b:88f8]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:3d8d:b0:1f5:93b1:6a58 with SMTP id adf61e73a8af0-20107eacc49mr5032422637.8.1743795657302; Fri, 04 Apr 2025 12:40:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:56 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-42-seanjc@google.com> Subject: [PATCH 41/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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/x86.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 8325a908fa25..0dc3b45cb664 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13678,6 +13678,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 (new->type != KVM_IRQ_ROUTING_MSI && + old->type != KVM_IRQ_ROUTING_MSI) + return; + if (old->type == KVM_IRQ_ROUTING_MSI && new->type == KVM_IRQ_ROUTING_MSI && !memcmp(&old->msi, &new->msi, sizeof(new->msi))) From patchwork Fri Apr 4 19:38:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038864 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 B1321236A84 for ; Fri, 4 Apr 2025 19:40:59 +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=1743795661; cv=none; b=aZYtbxs2dhaA99ActnvlwY+RGglMssDmwbdTzG0lQEDx5rfsrBrEuCsomZZIDhUOKobqls1VJgqr+NlNiNknFXOCRU8V2e7kdgjamQuM2BLI4jcpmep+p9jSQbAqM7mu20yfaHi3dNtaOR+F3p55Z2hve4KRBo6DcrQtUcoegGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795661; c=relaxed/simple; bh=mZVHycZOmEtySRxY0gXT8uvd2my3ZEIARWSKiopHcCE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Tew/exT1Jl7MPsz0/hoQm2kzlKtAbyQX0p9RbGAbLm1DbovvnD1dmKWJu2q2hunB1JnoeNzpt0J7J+LAE06U8qu8yHzuUB0iX4+3wkNKfB3Vg5oyq/55HeC/JYs2GGzGo1E/eNBfQE69MKy257oFOIqCBUFqjmu6125g0HfewAg= 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=knRpO/Td; 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="knRpO/Td" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736b2a25d9fso1983543b3a.0 for ; Fri, 04 Apr 2025 12:40:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795659; x=1744400459; 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=7rkr9hfugytVpCiSi0z4IyfwHexjORIFyMklekHAhN8=; b=knRpO/Tdb+S0BupljJx0CRhxTvzxuxIE9YmGWy9Zt54MsDRrPNvTAAZTu+v46GN2bG 4/9TxEkg9FQiDfioPMCS9n5IEXec/YR0culbARlp123+1EV9V0lSJ/TpRulHmgHD2lX/ 7cBwX3NuQPlahrVUqH7QodGnMjp4ovK9jChp8VwlasegAmQ67AjdLlwOi5UvPkAZMf02 DjIdpSqltu0VsC3MUJcHkSnSehpTR6G5C4MYOGxu9GHOyFCXl2/WiUH7oUUm4XLxGhth Pe+1sIWDEjsNrdkfZ6ZFq06yKRVbk3vP6tnO2q69DdaUFi7pDA7DCRZWIEQCrRASNgNp TOoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795659; x=1744400459; 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=7rkr9hfugytVpCiSi0z4IyfwHexjORIFyMklekHAhN8=; b=ZuhnLDIfATOc44YPf4yEdKQgqx23kieCdhNmOZHSWU2l6+iv87/Z8AHxp6oouLCpa4 /LnbQXUyxy1df+4vxWvcZ6TuXNYXOwW8wfquSM8HPyA8uEKRc9GYBakezH2m4ucOaFeu wFEPVHJ76dGq//bCVtGRQRJxt66UyZ9Bbhtxu6hFpj2j+OizC5sTOUmSe3LOzNh7zaM0 AhKthstYXhGbfzc4aU+Q2Eb+n/13A2RhLKCqCMxq1E8jkD1foGKmoz+Ni4EY74aTg3Fl wTV5QQTbnD74PGaUfM7EJMC4H6URwAqg4OwoCFKOXhlkK6KIlwi8LO/7PxHGv9Jr7M0S +nLg== X-Gm-Message-State: AOJu0Yyo/Ep5CkpMDK6AbGDqTNixC6L4Gx4jds+mZnov5i/JA7qNeQlY QiDgaRB4HGvOnSmB4fb0cpep4BA0dRKTgMRs7aIkzmyvLRLttokS/jipwyzxXroBTFDcpn2fYeI Lxw== X-Google-Smtp-Source: AGHT+IEMUGCkESn/3vt+yaprJmAWrzZtMoy46YE4JHJL2Qn/BHhy2Ab6QzLZUQwjmyC7XuNZfNUe6UyzE3o= X-Received: from pgbcq10.prod.google.com ([2002:a05:6a02:408a:b0:ad5:4c03:2b16]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:4392:b0:1f5:6c94:2cc1 with SMTP id adf61e73a8af0-20108011008mr6057448637.21.1743795659307; Fri, 04 Apr 2025 12:40:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:57 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-43-seanjc@google.com> Subject: [PATCH 42/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 bd1fcf2ea1e5..22fa49fc9717 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -778,16 +778,13 @@ static void svm_ir_list_del(struct kvm_kernel_irqfd *irqfd) spin_unlock_irqrestore(&to_svm(vcpu)->ir_list_lock, flags); } -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; - if (WARN_ON_ONCE(!pi->ir_data)) - return -EINVAL; - irqfd->irq_bypass_data = pi->ir_data; spin_lock_irqsave(&svm->ir_list_lock, flags); @@ -805,7 +802,6 @@ static int svm_ir_list_add(struct vcpu_svm *svm, list_add(&irqfd->vcpu_list, &svm->ir_list); spin_unlock_irqrestore(&svm->ir_list_lock, flags); - return 0; } int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, @@ -843,6 +839,16 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, if (ret) return ret; + /* + * 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 @@ -850,7 +856,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); } From patchwork Fri Apr 4 19:38:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038865 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 9A100238D22 for ; Fri, 4 Apr 2025 19:41:01 +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=1743795663; cv=none; b=KP//TeYW8YUU+lkrpHVtl5m9HrYL6n6CrC1b7wFJbel/rE+hE//+JGroOUNVDpIdA6cIzb1U9R9/g9wJquijbpu9YUCJUWJnGgwxWnvj3ch3DqByJz3Joh5h4YN4kSBT8OQUQiVE1qE+40CfSsWNN6KV7E/sAWvMUU5X0PbIYZ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795663; c=relaxed/simple; bh=7VgjiMaGTFBOMGgiyqbv9zdOnH8E7mg/DuwwYhcUp9g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TxUxcAmO+5qmpqXxSLl5i43EusFnqpEHFEMo85Yh+mqqO/acBk2sIK+Pw1aE8Fp+aTHusKKiAa/KyA1CAJgpp4vbAKvB+qkVKo3Lnae65Kt4Btja1UK/bPti1NrVaSIo5zw/7rE9WE3WY/l8+2b2tgdJ/CRn7q5ADknz6kDWRNg= 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=C30Ju5lJ; 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="C30Ju5lJ" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-225974c6272so19010675ad.1 for ; Fri, 04 Apr 2025 12:41:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795661; x=1744400461; 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=h02XDjR76RTBA1GQO1S8bkavF6bTLez7Lecbdt0M7Kc=; b=C30Ju5lJnOdN+aZrtTVvxLcpeQphpV9GRM2tvQb7MS/xe0KwAwVbsrxkm3dt3/tIeX wgMFIk53meFe4A9CfzYiZOwC3NdSmNgYcr2GQFT5Sr2V5lNsQnW0FHJBw6PYEGfQoMjg eoLVPAkIsxY0wjbk8ncvjTW+9ZytnMrG33QkrBmgr+mO2wCSdF4Qp72QsBMciFDwhTRO O/AcF7tIgnH6Xozclm5haGELTErC/2w9yJIXOvn25C++eJcrPoJ5KyG6+XVJyYbtOpfP YBsfpoyu0vGjrle60ilCP67/FWZEXFmyQiUwWCLl4VWeTjClGUFmsvzVdnpLDrch0guN MXzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795661; x=1744400461; 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=h02XDjR76RTBA1GQO1S8bkavF6bTLez7Lecbdt0M7Kc=; b=Ngok4WhXlIgTng72EFH9tuYLJMgnT13ZWG3vV4KuSiGIkq5MXxNcI1PVMP4ARwN4MQ GoAr/BWdPE+PWb0nRSsAw729qOwLOR7Lmvvq9lUdNnYlb60oUNASMycT3yqHo/yB0E9Z byWs38kaWe2nb5+6tBG1QpcZXzkvLBjNgf8ihMKl3Di99dI/D2WwsGSbvGbWt9fEV6OY 563cwV1hhpgDPyn14qf3bnTMhxr/0LyiLVKnPayComCrY1pwQVqP62+UJKxBlC662dhs BZ4VXT4yK1NYr03tLt9QAngXPdRVFc6TFppT3PLM2AX6WZIN90wzKTWGsXiocQMyliII bI2g== X-Gm-Message-State: AOJu0Yyuip/cfyWkj+7VKuQ95rysPl0/68lQPb+V2ASTPz1Koe6AZgYm 89G+AwrDBedE/VroMz0Lydg90fqQNLhSGlgGIpSSX+6FlKf1nauKGmWudH1x0B2fwQvPFIeX2Zr /5g== X-Google-Smtp-Source: AGHT+IEi0ecP0t6WaN3K44IuMTG4PJkM768UFLXNdGKuUWq2dVHcpJjFVEGtG9zdypCiTJZBV1oUciTfEUw= X-Received: from pfbgq31.prod.google.com ([2002:a05:6a00:3bdf:b0:736:3e92:66d7]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:18e:b0:21f:6dcf:fd2b with SMTP id d9443c01a7336-229765a4fd7mr111593955ad.1.1743795661006; Fri, 04 Apr 2025 12:41:01 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:58 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-44-seanjc@google.com> Subject: [PATCH 43/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 22fa49fc9717..4dbbb5a6cacc 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -778,32 +778,6 @@ static void svm_ir_list_del(struct kvm_kernel_irqfd *irqfd) spin_unlock_irqrestore(&to_svm(vcpu)->ir_list_lock, flags); } -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 = 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 = 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_kernel_irq_routing_entry *new, @@ -833,8 +807,18 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, .vapic_addr = avic_get_backing_page_address(to_svm(vcpu)), .vector = vector, }; + struct vcpu_svm *svm = to_svm(vcpu); + u64 entry; int ret; + /* + * 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 = irq_set_vcpu_affinity(host_irq, &pi_data); if (ret) return ret; @@ -849,14 +833,19 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, return -EIO; } - /** - * 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 = 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_data.ir_data); + + irqfd->irq_bypass_data = pi_data.ir_data; + list_add(&irqfd->vcpu_list, &svm->ir_list); return 0; } return irq_set_vcpu_affinity(host_irq, NULL); From patchwork Fri Apr 4 19:38:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038866 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 687EC23A986 for ; Fri, 4 Apr 2025 19:41:03 +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=1743795664; cv=none; b=tKiA+XEMdys5v9oIx5fmkYDqIwRF4Kg9UOF4jMpTnWDgbkQq2+L5TJZ9qFMp1bCSybzKmfdWaox/A3bBwNJyXqL6XOc4JZTVwRSB6w5/O1akDtgbrRMgDnbuPgO6Q/fAfkwb0IeRPOaJt4E7CjIVVEzxMVYEhp2EVKVD2fK2OiE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795664; c=relaxed/simple; bh=SdRm1GRIbm33d/nHibJXYjLPmG0T0NZRcamdnee5V5M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CVb3wTXZicGUyCzcePUskBEzgPN/nFC8TWtGQ1GsuJkbHgaVmw7Z91pvYCdg2hNT5ry9iI94IMhWuZJEs132MjAjeTrMnYJmki0ETK9YjtGEdDecOGV9QoNr0kT4398FDM/sgmhHnONdLR/sPfZRXXJ44jxzpNbOwsTmT64Hpho= 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=knO9S7xd; 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="knO9S7xd" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-af5310c1ac1so1591817a12.2 for ; Fri, 04 Apr 2025 12:41:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795663; x=1744400463; 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=h0hbazAE8dMJrPUHRRf7zZrCtHTbdiwGcNR7wF/0toU=; b=knO9S7xdlRKDfS59wDf7utU8YQSX72GO44xHN1ooyBKlpXzNGgwrAONSkQuRIT0mMi sqG7axwEAQiuQcDXHKTXsf7Qak6CM5aQLSpa3Y+pb8tPon0vx0dJ7bi7RyBkrrYr9PBv RmjuQKI53yMVhw3ige005ulk4rCw8Zgwf3QgLSRBOC0eTRhztYHHSPQjOZ9Gl6JIfIre euaRAzDRhdAKVaG/GXlV4hmYceoUM+NJtjgt/++FAHWKzwyO26+AwU4lLdigTQ/vtG+3 YSnuUJXH2A64i5cvh1IHnvpeh2pbpl/AuAHsTCnXnX74c4+nNSZRYj1vVqm+HhYPL0Pq PB0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795663; x=1744400463; 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=h0hbazAE8dMJrPUHRRf7zZrCtHTbdiwGcNR7wF/0toU=; b=oVOD7aySQR8VBHPdNYsF4d83wD2DVxofRhguUx/OGLCnM98oFQi3wsNIJt3L2E0Wey jnLbb8wYHlePL1PYTkd80ggvAcYOxYwlEN4FXaNwzU2B22htMQ9hhDvSubijhBI+6UN+ yMelyFTK3D/fcfPQGo9pBFlKfm+ZFuM9L/+inxeY/3ud9wIl3Btb85OVxCbitSBYP1nI a1I2AWXNC9SEfKKNVZOO7Pk8o9ca7XNZ2l//FatVLng8hjtgLfDmUujIHV8+KK6k4njT vAB8UqYVphejSHokwGS92UFp+EuBkEBrdGe7N7wSKwxwWQCWdK4Re7lz081W3H1HB+HB TXIQ== X-Gm-Message-State: AOJu0YyQtOT+0kAATdEP8dXaGUUmtordgfY/fBlvslskiidZE6CHZybP HeTPwSBBTYGFUUkA7wiiexoEInB5Css+3v9ULb/o6BdUJhwjVt+fXiUA8Z+AROFCDTdWwRuTG8i xkg== X-Google-Smtp-Source: AGHT+IGWtLpM72ibr6u8G3+P4C5G/mdxZQlLn+wbJokB4hzvBLYZDfOpzpP/P7/kdQcTFYiTDJkPje5dvF0= X-Received: from pfbdr9.prod.google.com ([2002:a05:6a00:4a89:b0:737:6e43:8e34]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6002:b0:1f5:7c6f:6c8a with SMTP id adf61e73a8af0-20104751ab1mr7764832637.35.1743795662762; Fri, 04 Apr 2025 12:41:02 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:38:59 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-45-seanjc@google.com> Subject: [PATCH 44/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 11 +++++------ drivers/iommu/amd/iommu.c | 6 ++++-- include/linux/amd-iommu.h | 6 ++---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 4dbbb5a6cacc..3fcec297e3e3 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -842,7 +842,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, entry = 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_data.ir_data); + pi_data.ir_data); irqfd->irq_bypass_data = pi_data.ir_data; list_add(&irqfd->vcpu_list, &svm->ir_list); @@ -851,8 +851,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, return irq_set_vcpu_affinity(host_irq, NULL); } -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, int cpu) { int ret = 0; struct amd_svm_iommu_ir *ir; @@ -871,7 +870,7 @@ avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, bool r) return 0; list_for_each_entry(ir, &svm->ir_list, node) { - ret = amd_iommu_update_ga(cpu, r, ir->data); + ret = amd_iommu_update_ga(cpu, ir->data); if (ret) return ret; } @@ -933,7 +932,7 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); - avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, true); + avic_update_iommu_vcpu_affinity(vcpu, h_physical_id); spin_unlock_irqrestore(&svm->ir_list_lock, flags); } @@ -973,7 +972,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) */ spin_lock_irqsave(&svm->ir_list_lock, flags); - avic_update_iommu_vcpu_affinity(vcpu, -1, 0); + avic_update_iommu_vcpu_affinity(vcpu, -1); entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; svm->avic_physical_id_entry = entry; diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index bc6f7eb2f04b..ba3a1a403cb2 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3957,7 +3957,7 @@ int amd_iommu_create_irq_domain(struct amd_iommu *iommu) return 0; } -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 = (struct amd_ir_data *)data; struct irte_ga *entry = (struct irte_ga *) ir_data->entry; @@ -3974,8 +3974,10 @@ int amd_iommu_update_ga(int cpu, bool is_run, void *data) APICID_TO_IRTE_DEST_LO(cpu); entry->hi.fields.destination = APICID_TO_IRTE_DEST_HI(cpu); + entry->lo.fields_vapic.is_run = true; + } else { + entry->lo.fields_vapic.is_run = false; } - entry->lo.fields_vapic.is_run = is_run; 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)); -extern int -amd_iommu_update_ga(int cpu, bool is_run, void *data); +extern int amd_iommu_update_ga(int cpu, void *data); 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; } -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; } From patchwork Fri Apr 4 19:39:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038867 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 E2201221DA2 for ; Fri, 4 Apr 2025 19:41:04 +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=1743795666; cv=none; b=KXreNwVLGq5HPfM+1hkbaM4FyuKyzJIEVm9kV7VgWjig2Xkdo4quzfL+8oZXrGoNpTir9W+8rgKP1oXh40CwN5P9JoUTp+TjTEP0+PL5g1Nxr0wQzuI4gGjN8N0BkXxxUT6YbaB9f69biGEKM1xfk3Z8FTyoTNkp5n94HchT6k4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795666; c=relaxed/simple; bh=l9/TMQHoV8daxs6LkFNlEupntQ06coEMa2x+aKyzlWY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=l3OyfxdR2/L+z569gsGjgq1becsGSexmGfwbgY83lhDE04U+S5d+2kFuQRHPxrOcS1IQRQbxWSEdFNNtFi+EjdhVefvl2vHMPzwX0kW5eIzWoOvs0AyQ3h8h/VQCJSt1AoypiVofNFsVHfm9ZQsZUsp7nAQKQ8qCrgC06fARtqw= 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=GDNv8tyl; 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="GDNv8tyl" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736c0306242so3286580b3a.1 for ; Fri, 04 Apr 2025 12:41:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795664; x=1744400464; 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=l67GfpOlJHxEJdXm0d3y4sV8FlCkfwhTj3jmH6dDpac=; b=GDNv8tylaFqQpdGTp7C+N42x09fGsCELW7nUl/z2BsNVn3hv4yMiwzq+5GIqy2L5dX AzL9ATqXTGbjWKKp2qSN6mNdS+L9kQ1jApCcPKJDAzxv8DP42hapRIQNK2LFjPv/ja9f OEhYzcYKFgyKBlVBikVLBjZxOA2n6CTBMwPkDz6glvz4yQp6JieI0Hh7Mm/7bmNVg3H8 iDBHnweBRMq6e+9ZMkUykJ6rS2MBdKr87MbxJ4Jhh5kAuxpBgmfAl8LahzvLZIXIgt8v wZxUR1ZMNJnm1mYkFgnEqxMDtBDoZtEBh2kBKvEoUPaYKn4/73UkexFrQgPcX9qLNCVs rm0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795664; x=1744400464; 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=l67GfpOlJHxEJdXm0d3y4sV8FlCkfwhTj3jmH6dDpac=; b=uIKasnjhhVlXkxSFVtQ0rncjXMK1Ip6m+OpndGJ0H+Zq24sDGvrRuhWiNFe+VZzHi0 9pcQCCCdGTzX4fyiiUA9obA00ufiyoLBe3LKA1ctiav3eGJZtrn9tystJrU5GC/CjPBz DccC9F4Op+za/ZwkmqqFnYyNC6nTnLIbEG3OTGDPLJdotln8OTlC+MMmEvfrBgU2IWTt urSRnRRDH43ghjD+IPkIQW5Qu5953Smx4zTpwOqzI64nLiwpHhpHLpuQnPan1EBsJX29 /9YNQRp5pLRqzt6eVSLlbl32RbY2kiWgTQ6wWlNJ0NA+K0TVXb1HlYBp/RYNHzUUbbqY BpgA== X-Gm-Message-State: AOJu0Yw38mGeJFyX6k9gb9HekzzqTZR0fwVUmUOOuHV8TyR7V1xjwgU2 qCuIU35sMgpFWOUsQ203iBGkgtdOACt9ia+Y19fqIM3KfSntdNcIilSXftfA6eaE3+xV9fL5+fp /xA== X-Google-Smtp-Source: AGHT+IEsFzK6cRrJ4WqDr7v3u7M9JaAhLsSDAqEgoXQfJ0GjLsWP9yEnMTwux+uXm8Wz38v+7YUWG83LmH0= X-Received: from pfgs23.prod.google.com ([2002:a05:6a00:1797:b0:732:6425:de9a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:398f:b0:730:9752:d02a with SMTP id d2e1a72fcca58-739e48cefc2mr6715054b3a.4.1743795664464; Fri, 04 Apr 2025 12:41:04 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:00 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-46-seanjc@google.com> Subject: [PATCH 45/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 | 59 +++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index ba3a1a403cb2..4fdf1502be69 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3775,6 +3775,38 @@ static const struct irq_domain_ops amd_ir_domain_ops = { .deactivate = irq_remapping_deactivate, }; +static void __amd_iommu_update_ga(struct irte_ga *entry, int cpu) +{ + if (cpu >= 0) { + entry->lo.fields_vapic.destination = + APICID_TO_IRTE_DEST_LO(cpu); + entry->hi.fields.destination = + APICID_TO_IRTE_DEST_HI(cpu); + entry->lo.fields_vapic.is_run = true; + } else { + entry->lo.fields_vapic.is_run = false; + } +} + +int amd_iommu_update_ga(int cpu, void *data) +{ + struct amd_ir_data *ir_data = (struct amd_ir_data *)data; + struct irte_ga *entry = (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 = (struct amd_ir_data *)data; @@ -3956,31 +3988,4 @@ int amd_iommu_create_irq_domain(struct amd_iommu *iommu) return 0; } - -int amd_iommu_update_ga(int cpu, void *data) -{ - struct amd_ir_data *ir_data = (struct amd_ir_data *)data; - struct irte_ga *entry = (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 >= 0) { - entry->lo.fields_vapic.destination = - APICID_TO_IRTE_DEST_LO(cpu); - entry->hi.fields.destination = - APICID_TO_IRTE_DEST_HI(cpu); - entry->lo.fields_vapic.is_run = true; - } else { - entry->lo.fields_vapic.is_run = 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 From patchwork Fri Apr 4 19:39:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038868 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 D55C423BD1C for ; Fri, 4 Apr 2025 19:41:06 +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=1743795668; cv=none; b=tPggezw+jGN8AdzYsH8U+NFFWNm58hwuOmXvQBP7Vp7MLAgumJqLOWadOAVMsPf90/rgVCPczf2Up1mNI0f0Pvry8BF2DJqh40b6yoykcI6Uy6obbjsJvn8Ze9saZX/7B6cCQFfwgBGdtS2XlElalL5fu/+ucySL+AoI4W8xPrI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795668; c=relaxed/simple; bh=vEYwD1yOWLfk/95pT3cNRoQCpvLsFYMJf/mNwwjdg6w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gaDUQt6j6E6trvYpmyxsJssYM9SaPfzCjifpZZfaUMgi5/qq3fyS1cSeMUCRtUtVAFofZLb4CdH5fN6brT6FtWkF4rNjiGb1MsML+qMoi/hfgfwKPudUmpc2qyfIsvYPpd4+JmNg6FrpJcmYvETryc80uBfVoiwCTShKR83yxeA= 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=RHIN6lgr; 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="RHIN6lgr" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736b2a25d9fso1983611b3a.0 for ; Fri, 04 Apr 2025 12:41:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795666; x=1744400466; 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=fCu4znq/izmNOP+TGp2LQJl6nRBB96ndlJ2G/FSp1oU=; b=RHIN6lgrIfeFHt2loEhTGwv5J4IOocqS1BmpajAT2NECGOQJECqTISX1590E8yip8y Q7FJBWXqk+Klh4YbzjW0ifdmcbVy/jAoAAtNySD//zHoG7mEkNMi65zgaYwc5q/0nwOg M+atOXn7V3czvZVSCZKJu0m7LRYkC1JISB1iup9xal/w7D17iTvrKZxAsWHqyD726DBp 3q0uqpkaoLjCH+l0BoupCJNdR64mnZ+q1qxDK7GAmgIR1tM69uVYpBqUULD1OL/06QZr ycez5fCeCpZhklAlcI85JAhsINKtMIx5zqikI14fzXysh6yznuVY9Vk3UYSL2JFs5lqI wCnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795666; x=1744400466; 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=fCu4znq/izmNOP+TGp2LQJl6nRBB96ndlJ2G/FSp1oU=; b=e8Qm4hac6uCqMkl9/FLEQ8wqUDCei5bIfNqs216au9GVX0TuR56f90YKGkH7nCk9mJ /03D0Ez9Y6xUaUsJqP88HfjxgRZFW665SfcTHc2UBuXtmOOqgV4VrXbj2cWF+QgoL+km V4EcHNeduvOE8myC1G762/Q+p7JgAXKq8tYnyUEOp50ahXCqaPGP78yOXB/keRSi7BwG beiW7lNXkAHYjWbGtBtPllTT7NfzBHKJ8YXJptRv5W+/y+z3zOnzQxOX/U58MjVW0sav RRD49uLCSst8xxZbRJUHnw3bvpqf2razDw/mvO3pwjq5O1OZ85TF8MlYqQQMa1eJWMF8 C2uw== X-Gm-Message-State: AOJu0Yw1HMEGlVa7Z6MP4+QKYzWSzXwa7qCe4HEetGJ7of+VRiaOdjYe 3nEndkY5sXdzkNgu7wrOkVk4fakk2TE/sWlGmuuuCSz+ky2kC927ot4afokVMJVJRr1RdYDs+JW DrQ== X-Google-Smtp-Source: AGHT+IGPj22UXTH/Zcy5qZIqn9/BjQASYjiq8B8h2ade9XrhvSEXkbVGkfxvkxUFnqdy7NHyCw+NgEfc9co= X-Received: from pfch7.prod.google.com ([2002:a05:6a00:1707:b0:736:aaee:120e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3cc2:b0:730:75b1:7219 with SMTP id d2e1a72fcca58-739e70575e6mr4840675b3a.12.1743795666219; Fri, 04 Apr 2025 12:41:06 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:01 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-47-seanjc@google.com> Subject: [PATCH 46/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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/irq_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 3fcec297e3e3..086139e85242 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -735,6 +735,7 @@ void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu) static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) { + int apic_id = kvm_cpu_get_apicid(vcpu->cpu); int ret = 0; unsigned long flags; struct amd_svm_iommu_ir *ir; @@ -754,7 +755,7 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) list_for_each_entry(ir, &svm->ir_list, node) { if (activate) - ret = amd_iommu_activate_guest_mode(ir->data); + ret = amd_iommu_activate_guest_mode(ir->data, apic_id); else ret = amd_iommu_deactivate_guest_mode(ir->data); if (ret) @@ -819,6 +820,18 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, */ guard(spinlock_irqsave)(&svm->ir_list_lock); + /* + * 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 = svm->avic_physical_id_entry; + if (entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK) + pi_data.cpu = entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; + else + pi_data.cpu = -1; + ret = irq_set_vcpu_affinity(host_irq, &pi_data); if (ret) return ret; @@ -833,17 +846,6 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, return -EIO; } - /* - * 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 = 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, - pi_data.ir_data); - irqfd->irq_bypass_data = 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 4fdf1502be69..b0b4c5ca16a8 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3807,7 +3807,7 @@ int amd_iommu_update_ga(int cpu, void *data) } EXPORT_SYMBOL(amd_iommu_update_ga); -int amd_iommu_activate_guest_mode(void *data) +int amd_iommu_activate_guest_mode(void *data, int cpu) { struct amd_ir_data *ir_data = (struct amd_ir_data *)data; struct irte_ga *entry = (struct irte_ga *) ir_data->entry; @@ -3828,6 +3828,8 @@ int amd_iommu_activate_guest_mode(void *data) entry->hi.fields.vector = ir_data->ga_vector; entry->lo.fields_vapic.ga_tag = ir_data->ga_tag; + __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); } @@ -3894,7 +3896,7 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *info) ir_data->ga_root_ptr = (pi_data->vapic_addr >> 12); ir_data->ga_vector = pi_data->vector; ir_data->ga_tag = pi_data->ga_tag; - ret = amd_iommu_activate_guest_mode(ir_data); + ret = amd_iommu_activate_guest_mode(ir_data, pi_data->cpu); } else { ret = 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 (*notifier)(u32)); extern int amd_iommu_update_ga(int cpu, void *data); -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); #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; } -static inline int amd_iommu_activate_guest_mode(void *data) +static inline int amd_iommu_activate_guest_mode(void *data, int cpu) { return 0; } From patchwork Fri Apr 4 19:39:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038869 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 7D28F221DAA for ; Fri, 4 Apr 2025 19:41:08 +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=1743795670; cv=none; b=VMmAURu7HeDO9XtOljGV5LrGnV+oNXhIqXvySP59+DW4diDR7T5vZYSiCAO8P9BFItd0TZ4UuHEIsIsTngA3EzgYbbNxtsH3R90tZPlBkQ2cvq2Pc9yEBfLvCIUiwBCmhds9Qr5KqSRL4/hmay+4TRAAxqXzNsP2SGU59JAqpyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795670; c=relaxed/simple; bh=Gs/w7b1TpQ2M7g5MgA9NNIvjCHFPtQtwURjHeBU9J9A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=r47tz80yvOwKYPgmE+b+pcGpxsMJhbyV8NA+4zPP8QL3BxGLlk+1gxNv6v/TomuN21wYJVPxMTeP+05Id0AYhkhNrBS7oJyy9XtbDiWHuYVKva0o2dFQvGKlZP8t+hJcfVpVjBb3lfhAU8FvKPwP/U4UHSt47MLrjRZfi/IZfm8= 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=E+N5REh0; 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="E+N5REh0" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736c7d0d35aso3406497b3a.1 for ; Fri, 04 Apr 2025 12:41:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795668; x=1744400468; 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=PKGLHehhFYZS7RFg1K2PZNaed8s88CqZbtq5pq5GKIE=; b=E+N5REh0WKQZpugrVbhv1ef447RAFeK+DrrVDWbrqamdfv+j9JAxFAzO15P2TxIjkD tx7nrf3gBHnX3MTPU9pVSPQmHmbx/RVFV/cgphXLB2smw/dBwcUPm+Zv2SLsz7iLmcBS aY8f4E2ixwYFHJAJcuFhrGuKLzKgYuKeIUwvxJpvvf7GQvyPeZGkl2JulA2am8cWu/1m NwZ+/9enMY+QzeLVp2m4BNTJw+26jF1djzMRPF5KV2NHd1/ZFi8uldxWQI/tH23SYjOG zfNxYO5TskWh8pqymPjq5KPg26xU1QVZICrwJiYEQLy6hEX6P3N8lq2A5wDJV7lQx1HM 8xVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795668; x=1744400468; 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=PKGLHehhFYZS7RFg1K2PZNaed8s88CqZbtq5pq5GKIE=; b=N+zPJrDn+sHMpWVSZ8JEsfHqTJaWfK32zUyv7O16KBsK9oTj3mB1WH7Q4DKIoGrSiw 86wx6Bdt1nFsrPshT2P1ZEYgyafaBvSyPmXSSihFew3g/j0n1fyUiVFnLRDn/Yh9FgDT FW/mZq16RnoeTLgQtWzlAA3IiYSNeUDrcV8jDMe7/KPeb7aOj33hTH+tAjxak9zyhY1M rT8E+0ReeV2XaOYaZEhx/kKt48NWNuCKBpo2CowlhMiPcqm7QN7+4hmnzgit/rTaxYae JRGZ0tqWk2N8phdabtmqymapx5G40H4ouiJh/hVtC/l8/ljZeJGqiNaPLgL3qG7QZqSz Z6hA== X-Gm-Message-State: AOJu0YysRK2TWlujjNI2DuFk9gU2LULUrduPRqRb1hf1UGzLxv6QYvH8 6f4o3ejAi/nI0dVB7GonLynPTfuPjuEW32wXcBzW9l2zRLwDisGdXD+mMabl51R57EImvIPoEPH 2aQ== X-Google-Smtp-Source: AGHT+IHe6zs/5LhCfrFgK+vj2rDDG19lUEDx00Bzgp892zED1081EdFE7G+DtTelTSRWbal8s7FZTzV1M7s= X-Received: from pfch7.prod.google.com ([2002:a05:6a00:1707:b0:736:aaee:120e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:88c9:0:b0:736:6043:69f9 with SMTP id d2e1a72fcca58-73b6b8c2c45mr719867b3a.19.1743795667980; Fri, 04 Apr 2025 12:41:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:02 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-48-seanjc@google.com> Subject: [PATCH 47/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 => 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 086139e85242..04bc1aa88dcc 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -790,21 +790,17 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, */ svm_ir_list_del(irqfd); - /** - * 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 = { .ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, vcpu->vcpu_id), - .is_guest_mode = true, + .is_guest_mode = kvm_vcpu_apicv_active(vcpu), .vapic_addr = avic_get_backing_page_address(to_svm(vcpu)), .vector = vector, }; diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index b0b4c5ca16a8..a881fad027fd 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3896,7 +3896,10 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *info) ir_data->ga_root_ptr = (pi_data->vapic_addr >> 12); ir_data->ga_vector = pi_data->vector; ir_data->ga_tag = pi_data->ga_tag; - ret = amd_iommu_activate_guest_mode(ir_data, pi_data->cpu); + if (pi_data->is_guest_mode) + ret = amd_iommu_activate_guest_mode(ir_data, pi_data->cpu); + else + ret = amd_iommu_deactivate_guest_mode(ir_data); } else { ret = amd_iommu_deactivate_guest_mode(ir_data); } From patchwork Fri Apr 4 19:39:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038870 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 C790123E227 for ; Fri, 4 Apr 2025 19:41:10 +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=1743795672; cv=none; b=RJDbP4ZiZbnqIc31mR9NpYZ4Z4Rl267Qr5ip/sFqwodeP7HrGF/qc7vXqNZ+BpzvxHZg3XQ9D7l/aEi1TF1AkAB2FTvN3BVE1nxc3aRFcfyX7cyrNhIZdb7/wT8I+NYKRi09/SBL0sEYoPdqRotSW5JByugaUmTcp1UcTBqRNyk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795672; c=relaxed/simple; bh=Jfmk9VzNRAADpmMGwdwa2xyHxXIJHx3CDbGZTsxqH1I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kieJefsDzMA6cOlK3BiPXXgP8YlDKoX9vn4NtgxCYCSWPs7v8pJywdlZhXlMCA6xEGihs+AQttt6ghZ7syRWosPuZ4vvowdvw263Y1zGvd38VD41hA1qDNYnqYyZO9H5MDfq0VoTAUng826AgYj9GyaM/iyzwV2M6/laV30IaEw= 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=NCQmVOih; 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="NCQmVOih" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736c89461d1so3577802b3a.3 for ; Fri, 04 Apr 2025 12:41:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795670; x=1744400470; 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=8P0F5+eRvzI1lxgpOCwvPttxPqLphUaIWfDndZq8iLM=; b=NCQmVOih0uFjaC3BZm4oIoGms8lCTgr7CPB7ZBCfkMX/jsgiI54Inp39yCveA01mra 3XqKGJtIRuDCWOnYfm7j1FUPHA4feu6YJpl++1iZQLSzT3k22Fzp2yQOhn9Xkv4UkA5l tlTPhEm/QIPRsgwFcKNlI66rmWUUaqEi/57mr4lvkHT8uHodfexgtdzbn2GrBzynrmTf HgZRU4QdkYIDdEuRCKgaIimDTmEpnmdYLCNZXLamD+Ob3KtEWEahgjmEayYzNPxMmOPY SmA8SKAHdbc+aBAyWO0m4LZfZsrQaEods+8U/3PTfHUUY51SEDDeu1NKG16Xr4fY0w02 JdJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795670; x=1744400470; 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=8P0F5+eRvzI1lxgpOCwvPttxPqLphUaIWfDndZq8iLM=; b=FkyAa47LmU01otXFJ5qhL9ymaZdaac8+38rO95NOG/egSaFq6KHDoeFbSb0NdVmkOE To8jBpMsgZaA3qs7KCyEA+crGyeqc5+hwmhsbqS2El2411L2ohY3BowCGGLjDytUUSoo lshr2wNVOj/DTNmeQwwN0C2u5h2tUPk+d/C8jTBtwE2tzZRkerq6rE8b5fJFJkQ00di/ QfCwkabj32OpajF7ZVomq0GD0DAtK0KQ6CDr9Kl4BKCk6b46RdmklfLfOPBjkWbxYhg3 QEhXXODNecrjEXMZSiS2hBDGMSHYG/O8x0xEUgGvKSfrD/eC+9b7fAZU073bO5wLu21t C17A== X-Gm-Message-State: AOJu0YyAMY0zu+MlyFyGY5bMJT8+RBjzSogDUhk4Ey0+e2g8mwUsasD5 MHZtSX2WbgCvaoMUyA7dNR8xf9PeIMJccKP0xPhevmhSOM431xzwbfDzCl1jC3DECb7KkUbXWsO E3A== X-Google-Smtp-Source: AGHT+IFM3a33R0/JqO8ZtaGkbV0mf76n3eZC+Ea2CS6FZilzIUeDNAaxDWzQnYTCiWaEXSrdeYzp/CG211k= X-Received: from pfbho2.prod.google.com ([2002:a05:6a00:8802:b0:736:b315:f15e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:aa7:8893:0:b0:736:4704:d5da with SMTP id d2e1a72fcca58-739e7161560mr4999839b3a.22.1743795669704; Fri, 04 Apr 2025 12:41:09 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:03 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-49-seanjc@google.com> Subject: [PATCH 48/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 04bc1aa88dcc..fc06bb9cad88 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -857,9 +857,6 @@ static inline int avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu lockdep_assert_held(&svm->ir_list_lock); - 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. From patchwork Fri Apr 4 19:39:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038871 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 28553221542 for ; Fri, 4 Apr 2025 19:41:11 +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=1743795673; cv=none; b=HUQkmQHSXQATkjsd5OswXcqzQQfZZ3PJwAJUmmWvY4OPXdqOWi+w4F0X4N/fJZz8q8lZGrU4yG782LEMtVq4b+55nZuS7nnPUye0PbEA4CIuPqZq4iBX08/avxh0uHN3R0YWwyIOjQ0TXvwtIUsyW9HFbZDnaUVgttt3xg5qpxg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795673; c=relaxed/simple; bh=KYPkJmO2WKGa6Hqkv2jcYDUkv0LZapz25ye+QSx983M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PZl20tAQJXfAtMWMCUcGK0jzFpq/r2rWKKnvkAdliOv40JyY7CyDDD9zqEFeqPNYgwQlhJakE4/zfR2y91uKE4WKGEk11PgYHjnT0cLK9B0JbZ4Beyebm0JO+ptEHT09inevHvjWGSJ4mAgu0Be2OM7A9gHmyHZsZUbxAx74bNY= 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=emZHNJ4B; 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="emZHNJ4B" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736d30d2570so2142922b3a.1 for ; Fri, 04 Apr 2025 12:41:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795671; x=1744400471; 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=BuUmn/v/nxN4sDhjuaKZdNs4Kj9VqwHUaANP0XC6c7s=; b=emZHNJ4Bgx+OH+ONebVt6qqPMrMZOspA40S2cgRxABBCl5kNVYE+36Xm4TnuhvHrO4 NGhQ8eVaZKB4m8b5/MidNdMgq1OQXxfVthYmQhDoN+LJ5mx0yCaWHLof6yaMC4/T/CGr njlCZbdEBVWIV18/1MQKhCclAL2ntNrDNL5c055zPjkEaNqKb915HHYPuJOmMFpIsVfj w+ROL/kzIajjWBWBFrmLnTklHjyUEiZncCp3Nl4sL8PR6w1fOHHLhJbGmXUJQfdTSKDQ WJHAkVualMB/8lwGJ5Kh/xpR0PRMzBAdJw+Ntbq9+z+TVbJyk9DRsE3iRVyew8TI9mVN 13ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795671; x=1744400471; 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=BuUmn/v/nxN4sDhjuaKZdNs4Kj9VqwHUaANP0XC6c7s=; b=MstKck3Mx/+Vt5WmqE99g6LfCUZW9gUTZKwljtZUGFZ0RDeNuPTFa8VQhACP2Exyye HBcSAKKlNOS/j4cmnDAjQHmNTkhDq8gHtLE0ZfSjKdFASfLQ9PUtC85V3VDmIPTKllD8 Tv9k59TLnBO+sF/ISoFcNcZQhNPkqcux41BNbjs/RAA0qRwuzL0rdI5l/hOnC9s7vSjz mOUebqoAwUIv6c+diRKraxaeIUz9CsbDT/OYxIKYNJap8w1KlSS6mUs0BT8PJCuuUJAG XCwYW3yBBjUa0qjUaJO9ZUhQQFtXOhguBiQex0UMnPij4xC5SHXUyOjchf5880cLB0v5 Fb3A== X-Gm-Message-State: AOJu0Yw84TqqWfHAo9W4PduXZg8mLu3ZslvUxYVZvQS0O+AnynKA8C+3 kfITMutwkwqOm6U7RTBOn/nwCz87I94MoMgo/1Nz00s38RFdUPf7++DGa9XsqReGylqWxJ2NW/a +dg== X-Google-Smtp-Source: AGHT+IGn6VyFDYgNExRPxop+TDl4ttE1iIR8bH26Vp9Pjy1Orp1wUriDPA6oU50ESKKN1WjXv/GYXHS2yHo= X-Received: from pfbjw34.prod.google.com ([2002:a05:6a00:92a2:b0:730:5761:84af]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4653:b0:736:a540:c9ad with SMTP id d2e1a72fcca58-73b6b8f82e9mr711650b3a.20.1743795671518; Fri, 04 Apr 2025 12:41:11 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:04 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-50-seanjc@google.com> Subject: [PATCH 49/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 fc06bb9cad88..620772e07993 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -741,9 +741,6 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) struct amd_svm_iommu_ir *ir; struct vcpu_svm *svm = to_svm(vcpu); - 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. From patchwork Fri Apr 4 19:39:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038872 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 D7FFA23E357 for ; Fri, 4 Apr 2025 19:41:13 +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=1743795675; cv=none; b=sR7fprKzDvOB3arKsTulGksJ34/lACJIEro5iDdYPwmRI4gs4KsEJs6ia5EFJVkwup/BDO2zUe+uP8bd33GzU3uXtY/Fzq60t+I/IPkLHvpseGMK78e7L34zUNZnUX0hUObJUMy6ZzY3eBXtZVY127HO2sOOdIOoEKw1kk5VCpc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795675; c=relaxed/simple; bh=3h3/C9YU3IZR0FXEihfejCqK39egcENO1wsqs30Mu1Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Dc0DwNzRSrgIxxYlxpqF52Kv2xBYxYV92zQEUnfn8zb/nzwhbBFwfb3SrIJPRmBNHXOmZrbRkBYZpJhQejXO9brZgFtg7O/by9OAb9nwyYMO8zUEPEMd7nNpe0LmA5sA64lbdvFdxobS3Bw9Iv16pWz35NRm9HZWUGe3d3/Jy3k= 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=M0VtUjTP; 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="M0VtUjTP" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-227ed471999so21095975ad.3 for ; Fri, 04 Apr 2025 12:41:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795673; x=1744400473; 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=uuRDDxMTWx+BF71z7vaA3qXiBqaodPZrLAgFj2n/0Bs=; b=M0VtUjTPI7gNh3IjWYvYrIqrSXc9rqvu1c+x1cbmUA1oo+ee2hW+5Ee6C888m8FVXa HDAJlSYpO19/MQkBlbj34mrDULte2hjVD4cEfOe85ZoSYumBBQZHRIhKiPTgWqzg6evz B4cDij71rXgL7TtxKc+ZQ905JGvTuMhLyTg/apf8QnqnMwX3Z4wZXfeNTnsPQQJ7aQW4 zfZXVsAEjanIcRTC7NFupOY8XwnDYXC9tJQ8AZbAggzAvjqkqyjMDtjNG0xhMXXYSAFB Fv6zydlNZ0md++tgQ899M27F/NWL1KTkC22NXUpWSWdunTZEx54SPQtCBXlkYLxebcmX aydw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795673; x=1744400473; 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=uuRDDxMTWx+BF71z7vaA3qXiBqaodPZrLAgFj2n/0Bs=; b=BzCvJlPtvm7QcCAtyRsfOyRP326ECwsYA4CNVmLla/GAD2veESBtoS+VokI4pCgesS PEBg2XCGYNUaVtTZ9uh0mCo9vOKB8c/ip30O8PLRg5VuNYzXRTVMrtXAFmjKK1kH+Rff 9CZHzVEsf5boKwmr+tQX6EnnKiKufNCgR3FmPr5N5d7MD2ZZerBVDqLjzV/h7ObD5tTc LlD+4jqyFxxmRR7qwKMJGJqd6b9lUXpXVQCXNe+dNm6JJbw46ABYng2slzYyZ4QNeGGy 3EwwpkJiAWa2sf6HI7uQkx9HRVrwaWtb8qCTGJdUQ4CM9nzU4CdPUZR+YarCPg+QKZGA 7ojA== X-Gm-Message-State: AOJu0Yw4GBbEBHOniPf/sPbd5UK+iEgtpRVK/tEjwqHsNyJ1MjUxtSNK 1V72MueqUgf8Hf5HkkcDs6TcPbI67QSI/jRB5gLIS8W6GG3dvYeXkMB22iz7Uzpz9eIA53p6jgx Kow== X-Google-Smtp-Source: AGHT+IFSp8K6EjfmJQcrgpgFBVLlZAq7RswmqqBmwpyX1fWpzhgHdhXeMKdwMt6K9nj4ZqJ5N5KBfrQfg5c= X-Received: from pfbfd38.prod.google.com ([2002:a05:6a00:2ea6:b0:736:9c55:9272]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:230f:b0:223:517c:bfa1 with SMTP id d9443c01a7336-22a8a0a3a59mr56780835ad.38.1743795673360; Fri, 04 Apr 2025 12:41:13 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:05 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-51-seanjc@google.com> Subject: [PATCH 50/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 620772e07993..5f5022d12b1b 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -733,10 +733,9 @@ void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu) avic_handle_ldr_update(vcpu); } -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 = kvm_cpu_get_apicid(vcpu->cpu); - int ret = 0; unsigned long flags; struct amd_svm_iommu_ir *ir; struct vcpu_svm *svm = to_svm(vcpu); @@ -752,15 +751,12 @@ static int avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) list_for_each_entry(ir, &svm->ir_list, node) { if (activate) - ret = amd_iommu_activate_guest_mode(ir->data, apic_id); + WARN_ON_ONCE(amd_iommu_activate_guest_mode(ir->data, apic_id)); else - ret = amd_iommu_deactivate_guest_mode(ir->data); - if (ret) - break; + WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(ir->data)); } out: spin_unlock_irqrestore(&svm->ir_list_lock, flags); - return ret; } static void svm_ir_list_del(struct kvm_kernel_irqfd *irqfd) From patchwork Fri Apr 4 19:39:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038873 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 EC68523F427 for ; Fri, 4 Apr 2025 19:41:15 +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=1743795678; cv=none; b=muDtfdx9S3pXcjXwykJN2At3yFwY/fg1rkwCjP/1M8ffkv0May4cSiOvtqEisiVDuaP/1WypU631ERX7W7TefwNVqjNBEX2gzEoyPHj3SdsFv2T4YpfyF3DI+IBJdsrXJG8L8TJkPP0VjKpOm1Q92d267uv9dN5JbCIqIlBmFuI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795678; c=relaxed/simple; bh=bysiSM9qAdo7w3jTZoibNt/lKzOp1jzLwij4XBANYyU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DNn6TN2OUHOkc1E3u8/zqpSQXG3c2YV3q/K8rAUZx9UUqVVezQNK6gqxi6SGgIgXumIcbWa6OjbOlL7BZpa9MgPI45ovATopeQexZnzBNC0ChgNx2L6UJDBgqAm23BF7CWRrywIZpmVYFwctznEnaC79hzELCN9mjWlxIF1hW1c= 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=WkrHZhfY; 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="WkrHZhfY" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-af2387f48c3so1631767a12.0 for ; Fri, 04 Apr 2025 12:41:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795675; x=1744400475; 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=PxawxOY/h734lZVFn8ksHoL6g5gCibnUodY00H7f/TU=; b=WkrHZhfYe1iUaHMHjDItttTvLzKSkVsJJUSGRvxNkgSWZIl3o6p/KnSb8TDip3Km5J WGnwufhyvRwOu6lKJKihS6Otwe3GSyIjHV8KGgfxFJiGBEpOXiGqh278KHE7j9jWreFc GYrcys/j7DadpbZX80V04iZLI1ZTFJ/LRPDjvzZN2zPKxsBMphn3EIXUInsi59BQ/WLU 1QA+AgtErReP3H+/WpbR4D48Anxdghp50PMnfYeVUydNA8huN8kzp4lavDM6JFA793mt inRPEDqvOXBZ8VAoWYtcM4QHiq8X5QVFSa6DWpaquHViIy8m8hiDiVUaKCrodGNTJsqr zUBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795675; x=1744400475; 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=PxawxOY/h734lZVFn8ksHoL6g5gCibnUodY00H7f/TU=; b=T0zU/rcZiJbWDPdpyLgEqZwg85Ux6hOEIJ/DSAsRpfetTVChJjZHw9elWb33IzC2E5 mf0E1NRBZSdL14Lj1F3uRX0oFtD4W8HZJgufwc1tStoNsRy6jLwK1OI/XVYwUZGSrlw9 gRyJJn3PyB1mi9Z0cuXNYEas+Pdd1E8lb15+vAVrtxb+qs29NZvmRRiarmL9kdmGQK6b sK5TwWq6r1rtruNvt7kd55U8FD4fA6mvH/qzoEemh06CvaoaHtSekhwk3Diu3PkFE2/b QuJXiQiex+zMe+YPtTSglCzdSmHy5Y8EdXCDhetFD5tz7psgg59UgXfdWaYccR7a2l3v EOsQ== X-Gm-Message-State: AOJu0YwgailtDSqytvgIUhhrH5dQVP1l7tAJJzAGUpIus0JKFwSwjqBc ERab5z5sWJL1G9r1HA16uCEL/flMP96uuOAErg4VlAeGJu1CuID2Np6Qv/JrrvWEehIavJZMU0I 3xg== X-Google-Smtp-Source: AGHT+IF0dzvMV/vshjJPGocWxq0fkVPPJN838E/ImYVh1WQmz0SFa2CAc3x62alWuXzKlUZkk5y4MEI9SnQ= X-Received: from pfbgs17.prod.google.com ([2002:a05:6a00:4d91:b0:736:56a2:99ac]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:3c66:b0:224:191d:8a87 with SMTP id d9443c01a7336-22a8a8739e2mr55191535ad.26.1743795675258; Fri, 04 Apr 2025 12:41:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:06 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-52-seanjc@google.com> Subject: [PATCH 51/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 5f5022d12b1b..5544e8e88926 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -857,12 +857,10 @@ static inline int avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu if (list_empty(&svm->ir_list)) return 0; - list_for_each_entry(ir, &svm->ir_list, node) { + list_for_each_entry(ir, &svm->ir_list, node) ret = amd_iommu_update_ga(cpu, ir->data); - if (ret) - return ret; - } - return 0; + + return ret; } void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) From patchwork Fri Apr 4 19:39:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038874 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 94DEB241103 for ; Fri, 4 Apr 2025 19:41:17 +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=1743795679; cv=none; b=SnmQ0InEpe0YhsvKiooXrJocU+iNObWIKD71F0QfTUbraRTBdj0qafiJy+jy89hZai/sKHSWU+cV7Y9DfN3ZoY1zmcnVjbVXbHXyZRSPbBNzIIKghg8lWAJCNr7mxD3wXZbHjIHc8zyWFJThsPyUcePfTK5B9fjtk2/PtoUq+mM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795679; c=relaxed/simple; bh=pUv+Xc96YlFs/6EQOBDk/1ZAyvlUymEt7XHqJtNT7IA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pbmu1m/tb4Cm+BuktYLn6oqn2TV9/KHElfdgin6kxooR+bNHGOtbqJIHwN/MkulVzDdERqCAIaWha3TNj+HHrQO8U3VTtiNxaGbaEKX6R2/YQESpjRmBbucy8e0XSB5n/SsY9/efZNr3feWYqUGv/KgXKRRhxBLVKLC6HEyObrQ= 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=sPslsL06; 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="sPslsL06" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-736bf7eb149so1784620b3a.0 for ; Fri, 04 Apr 2025 12:41:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795677; x=1744400477; 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=2g+yUGZiG4yfMHeUUYgs9UInpjAc1fZElggf5I4uJ3E=; b=sPslsL06yXOu4BpH0Gqk5VC5Y9RqsAoPXwFRGOxfOGnFmr9vaAOv4ZEfF/ax5zgPXv PYiBqHqdKYv2ztvZTRp2/9hVd7Art7wXctEPySKHRMHteUrBM1cOPiZi03fJ/Jbay9EV uBFwphIicHq96xLpsSNiEVwHrT5DjyHZbkxnjSZ6dH7iNRcW78huTBZdIw/bOKy2cgvz t/reNhNpRX7vrm0ycQW4t9TeBo1HyTDaZjp/qj8TnlhVdg3PrWRqqxXRYx8tw6quyNmC HSqypZBjQBtM4HUcGRUrxxHrhEq22Wt5uQxxyqDVlVjnxJhSZb50onzkXDEvhjsK2wmC QhgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795677; x=1744400477; 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=2g+yUGZiG4yfMHeUUYgs9UInpjAc1fZElggf5I4uJ3E=; b=W0qF7b1ic71Jix/LddHCI+XDGYtu9S60n5gaQsA1u+o/alyH4PBdWeuu1lBrSmtwGj b1hPReloF1WqMcaNfcpz1iaxmLdVHSwI3hfalBcTpvVDd0+e1+FLeJCs40Zn2WYXLH6G eaQcBHRJBIMMkr2MN8iiIgpe5OZaRGoTAuBO8kEKLMcJqtHvUDDeEh8ktb/CeWJ93caU IwG+PR5p7i5+VD0CFN7BZ/fPhMqwIzPvDxrLYHaDSSjGC0I0EtyKG96YNy5GnQcIhhpz KruLf75ZeOt/sN6toqRjhd+8MVxg5aazhgX/utANDKR27AIHMJHsufeF4orWs0H0x6X9 0TnA== X-Gm-Message-State: AOJu0YxdfXzTg/pLtFxcq2t10Q6ZoBVT8IE5phlW+NuCMxs6s/spOQCR 27Hht+EavEzDt9SSmYUoiazREKSGrrAJSMNgdV1vH97snmCQQnyZNaQk2587KbgdtnTNZpUdRrd gTQ== X-Google-Smtp-Source: AGHT+IGWwBLEvimkzUQGQ+6a6AS7T1fRIlofnqD1LzuuuvVqr1b/zIjzTOQVerQA0BKJhk3Q69vR5NQVKu4= X-Received: from pfbjw34.prod.google.com ([2002:a05:6a00:92a2:b0:730:5761:84af]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:9c99:b0:1f5:769a:a4c0 with SMTP id adf61e73a8af0-20113c71809mr1061441637.22.1743795677060; Fri, 04 Apr 2025 12:41:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:07 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-53-seanjc@google.com> Subject: [PATCH 52/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 5544e8e88926..a932eba1f42c 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -842,9 +842,8 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, return irq_set_vcpu_affinity(host_irq, NULL); } -static inline int avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu) +static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu) { - int ret = 0; struct amd_svm_iommu_ir *ir; struct vcpu_svm *svm = to_svm(vcpu); @@ -855,12 +854,10 @@ static inline int avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu * interrupt remapping table entry targeting this vcpu. */ if (list_empty(&svm->ir_list)) - return 0; + return; list_for_each_entry(ir, &svm->ir_list, node) - ret = amd_iommu_update_ga(cpu, ir->data); - - return ret; + WARN_ON_ONCE(amd_iommu_update_ga(cpu, ir->data)); } void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) From patchwork Fri Apr 4 19:39:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038875 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 963D52417D9 for ; Fri, 4 Apr 2025 19:41:19 +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=1743795681; cv=none; b=is3gforSjWyWYcw+NJrND7UTJJFWe3UCIW9LqdkpSKY2PJyQQCp4NeImaZEq+RXOsMAbeoEC4cfDbVkj/W1xGTAWwQ96p49bI7SO5XNJqEyG/HqPYODbJXNNcx+kjJcPRtEEv6lyEbAFLQ7xctkL4cO1rYIqI61R0a/CAH0Ce9Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795681; c=relaxed/simple; bh=qXJGgjzw+0hjm8DJW3ZeQr+f2lNwkUO68ihRipJZBpo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NxAE2uqGlfNrPpjVcJMKcyq3COBn0P+OjZld2FR1xmVFiiuBYt32uIrlLduh95vNbAUD7YYKAwo8uNm/y0Sl9HImlGuyxkCzCNwd6pLYosaAxq9V1cnXNU1FozXJpIkbNvTGyXrMWtUqPWj1BUPA15U7VJS8vvig1PQO7SNDirg= 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=hOnh9GmF; 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="hOnh9GmF" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7370e73f690so2789878b3a.3 for ; Fri, 04 Apr 2025 12:41:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795679; x=1744400479; 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=7ygW7G5zQGkMJuNregye/u1PpXkO7G3h8KYVfV+6r8Y=; b=hOnh9GmFOOnmAEUGBi5c+9YXtFxOtffEzrCc/ri/jRaFXAI4fuiFum0uOYmOdewoHa 4QNKHVDhb/n+sxQ/3ZrzCbeStzMRfWSTRPLEvDzoVfZyBbjTyHn8oVKerZWHUGNrUrEP dBrsDjTHMQMfOE0HTTCMoTnIqyAfG9mBMBvSk3gUoLv4cTjKQu8qp6F3vtYds7x5acaB rhxOIOhzZLk4M5t+/TtoZIHiQg77D7I+4YK/YeKoYVIVmTRtPnX2gfwWOMc/kB5XeHUo XoYHYaCkyGPWQ0672OmBMNvmGhDcSAz/nKgIkmr/+PxTOXrpYGXJaEhDUUc3V9H4K3b2 ijNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795679; x=1744400479; 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=7ygW7G5zQGkMJuNregye/u1PpXkO7G3h8KYVfV+6r8Y=; b=fWvFTRSQKYX9AqGjKltqM1KS1QW2GlA2cWC7WhJRW90seKsUzoQltTXwMgjhtC/bWc EMyc78BEZIZWukjbeNAXzPfy2I0eubrd9HKMmrOvNu3+8qyyDDO8MwMniADY2MM6oxcC ppc5fnmqVexviqrt+Zwb05esWsbwyPlBWWvIi+jCtXGdj2k4WjVjkKUyoAx/7h7MY8eq nZBaVS5uj2Z2pzAk7KxE2QOPITYnoSauq4wSWER1XXDDU6DC9IWUshnRlev+BZ6mSIYC mj/l8DnAFavaWs1V3OIUGG7GSFe4h1q2vIJFdZ7ttQ7nljYnzFMronyVhqPaTcDLaX6R YNFg== X-Gm-Message-State: AOJu0YzZqr2UQqoBz0CVlTGdCKycoONJhWCxBNUOvPH7N9/kwl36XWh8 7XoqEZ0ORAenfRkLvtXQZ2vWM5qyq8R4JpDm5w5reqHwVLV1t0flGpaWANebsCHVmreic/kHra5 VEA== X-Google-Smtp-Source: AGHT+IHERn8C/szUZcLSKQMAG6WxKIsTl8yFgLvt40FKuJ8sJxPaz/AKn88FnY3pmMlvJjyBpp9M9NdBiow= X-Received: from pfbdf1.prod.google.com ([2002:a05:6a00:4701:b0:737:69cc:5b41]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a04:b0:736:3e50:bfec with SMTP id d2e1a72fcca58-73b6aa3d9b9mr865356b3a.8.1743795678916; Fri, 04 Apr 2025 12:41:18 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:08 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-54-seanjc@google.com> Subject: [PATCH 53/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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/x86.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0dc3b45cb664..513307952089 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13577,9 +13577,7 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm_lapic_irq irq; int r; - 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; if (new && new->type == KVM_IRQ_ROUTING_MSI) { From patchwork Fri Apr 4 19:39:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038876 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 10693244186 for ; Fri, 4 Apr 2025 19:41:21 +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=1743795682; cv=none; b=OnlixEKyMA9axium/dpDomkwIY9O5YZ4gFPgG8NhfkKnDHpQ0HV6KQOmQrstGhrKpOA5eGew4yppfwaOSFK7iboWeb4pxhuDp4dHvWcytV6SWEkIBVfmbPDKzpN2U6r7EJqLeIQRnV6oaXVmYqA8etgzxqyqVzoRZyYVXvOTEaY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795682; c=relaxed/simple; bh=J9UHey9VIc8H5csAImsrOgY9HzHt7VovS5PXam2uYYc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Na/x1GVcArgV0RzD/XRwnVS/TzeSrLvnDOG55P/9hLzEthwm61d8VuaHbk5WyNyMKGja3VmALe0QvW6vvaRUmYzVipR7JumdAopB+ituiahUH5ePAzHsYEz11SjrPu9PsMcmaEpQLLek/CpfrZ3j5NOdDXYCbXpSWxZCfYCWUPg= 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=BXoF0jPd; 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="BXoF0jPd" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-73917303082so1720461b3a.3 for ; Fri, 04 Apr 2025 12:41:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795680; x=1744400480; 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=IDnMFjaPTKzyH9hORg6vpYe0KsdQWNftCfPGoTAAZqo=; b=BXoF0jPdCKtBR6Zo6Zp3U5xfdlFDmX+FCrw6bIIRH8TXm4t4EGMfgwxanXRRSz1cjY U70zMZIHKvbNUM8jM+icL51CJIw5Ev/zfg7VjhWNXKBYZ2IBzKz+euL8L6q6L+92+B3m kk+d+xSd17t4KSfOzoHu38d4u/VApxwHN5UqOM2/Pg+MyxbObyWmUvLpUgVCHww0fCFl D4HuIA/0H7AZaxiWfZ0mzQCbHxyC+QKVp+UvumgRBTdY7KoPWRVwb7dY9OmJycCSMzeM sdjSjCmRoTp9V68oYzi+DEkRi4i4QTdw0YItMfwTXWgagUvRJu71kgzLRz6LZzDzzC0/ sC8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795680; x=1744400480; 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=IDnMFjaPTKzyH9hORg6vpYe0KsdQWNftCfPGoTAAZqo=; b=f1M4PQtDj/b/JQSeQTLcPkoOfvX/WSwz8iqM9WI0AxR49wPxJmKJR6H3H4olSOvrSf z1fg0SoXsJ5/1geAbgZO9Htl8X4TxVFtjwCibVzh6KucetWy+wKMieo17jcpnFXg2zHR OsFu4d3vUah51BU/qjG5L85OBv5YQ3tsxJBu8jcYTXHi6TeMLxQ5c5iffcKw7lPyBynt CSu/KjcSxPUsKU/CHttURHZ65SBIQ0TSfjWz9ADbyLDnmU16jZRxYDRRBDpMokcRkg7r wbI0I0QX0Jen+0hePXrzVAniDKSl1E0AHsBsS6au/jbvxkiu2M73yaBgc/AlWgbXuH1p u95A== X-Gm-Message-State: AOJu0Yzf/wC5dcYknX6iGBtWpAr5eVlXD+u1NTwHv7WzGDlsGsyWi3sM PBVFdYGSDkvI68o+7wRnJeG0/pN/ebMpRoWZ1Fl53STFru93ypUXSdyNk6jeDf82NIrBRvJPXI8 W9g== X-Google-Smtp-Source: AGHT+IFagPC1G+waMHkD10dBI62GEqlh5wfS6CoEd/HxTrdq6pLQQhec1FekF/zXyYVkQR5iehzlh0kK5pU= X-Received: from pfbhw1.prod.google.com ([2002:a05:6a00:8901:b0:736:7da2:53ab]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1148:b0:736:a682:deb8 with SMTP id d2e1a72fcca58-739e493138dmr7548244b3a.8.1743795680602; Fri, 04 Apr 2025 12:41:20 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:09 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-55-seanjc@google.com> Subject: [PATCH 54/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 513307952089..d05bffef88b7 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13577,7 +13577,7 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm_lapic_irq irq; int r; - 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; if (new && new->type == KVM_IRQ_ROUTING_MSI) { From patchwork Fri Apr 4 19:39:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038877 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 F2C9D2459CA for ; Fri, 4 Apr 2025 19:41:22 +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=1743795684; cv=none; b=EaERXFvgbNk4fuZ0TSbHIw+wLz5G2kbOmYNF4KMU7bC3E6FOwu6KDsXKZJTi6M/In8Bxt+ct+wSK2Hen34Ky7KU01am2TlJlpmhq2AW2ULh6DmtKkhUv0SpP5GPpR67cqLsjkWu+8psFvXraJoUtg0yvm10rBgWk1cqn9GrFsek= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795684; c=relaxed/simple; bh=O8Jtgnd04lzwalcRZpXtJREHuwR/9U8Wy9xXm5Q/t8M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ei3TSvpaXkbk3UuQKTP1mqwg1SEpWIVQGOq1i/cCYiS8rI99HViUqx0M+qGIjKaVhcxSxE7YDjqQQe31ayza7mpOO9QEJrOCyvAgfVQlZVnpEY1uuE8MJVVSqId5JnGlPvOPQS2QlegM/10DX9OGxi2cpezydmywCcIszByMXEg= 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=3aaVAhyB; 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="3aaVAhyB" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-896c1845cb9so2215507a12.1 for ; Fri, 04 Apr 2025 12:41:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795682; x=1744400482; 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=5ipvbDW4TsHmSl/et8/oy0jhI2aY6XeoFvTlaXxC+Z0=; b=3aaVAhyBhj5FMhPX+gvUdAiyjviZ0HuyfmLV+BsodTGOsc69jlMB0AuqWL9aErd1fE hhQX9rBM6te4ZSzT0FLWQuLKEUQyQQSVL5Sw/0I2+XxRjKaeyAz/1/7TciN1OOR0/rSL nmFVlgagw4e3MR7ElU+puOMTNovtsl7vI7eQoEFXFGlcE4tVzxrcN7DUSgJk9lkwkZ1+ 5CJ71/n+C9XH9SwFXw9WmLzTOGxbcqbHDhW73oK/GBZHTx8OrXBumVebTE+NxUefQJmg 7S38cwf0KWn14TfRfjsnfiMSqPCtVSstwigVY0vxNq0ke/PaRwz21j9bZjyQNjZYFKFC X2bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795682; x=1744400482; 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=5ipvbDW4TsHmSl/et8/oy0jhI2aY6XeoFvTlaXxC+Z0=; b=P2+8khwacZh56U0xqteSvDieM5pEdc6pyXSsZAKTwZRl9Sgms9yDzvgAqXlMORmYdy ADthuyhmopbrGY3BrQRjxD9JWwnePu22dMlsNEcRH7aA1oK8AWyT/64CoZqhGuVlrEt2 ZDJn2ANVwjbvfoPaKLesPRCa0G0QgjdhlvVmaEX3gTnY55UqDqPpIkyKeQhdemJHi6OA G8Y0I4estPG9rMtbSy7zHDLFCF3ZYJ9R3ElpYjNPzbTxRd3wWm4z0I/83uLLL4GcdAGY PxWNtzbR6QQMq7V0Gmr4Z5CtYVmkp0M8LZIDVcL27mpdoVAQOKb9ngg66ua0hmRpq8IZ 6TrA== X-Gm-Message-State: AOJu0YygG1AfqMFLNlmVUXcKN/HfIRIA62v+AS/v2MSeOAnksqXzlyaz N2Xa3vupGy2F831eJQHblojs+xvJL15tjdsyJvSixxWIACq/v+/xHHhgi7w0SaPWQjakYSi4uup PoQ== X-Google-Smtp-Source: AGHT+IF8vEX1BRq7iqbtezxSkX3cXerhUEkRR8Wpf5vTHNdIS8Aw+5Af2q/0JmJ4ODDazjlc49NfshTSImw= X-Received: from pfbfb23.prod.google.com ([2002:a05:6a00:2d97:b0:736:47b8:9b89]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:ef4d:b0:220:ff82:1c60 with SMTP id d9443c01a7336-229765d1b70mr135066285ad.14.1743795682327; Fri, 04 Apr 2025 12:41:22 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:10 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-56-seanjc@google.com> Subject: [PATCH 55/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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/x86.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d05bffef88b7..49c3360eb4e8 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13577,8 +13577,8 @@ static int kvm_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm_lapic_irq irq; int r; - 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; if (new && new->type == KVM_IRQ_ROUTING_MSI) { kvm_set_msi_irq(kvm, new, &irq); From patchwork Fri Apr 4 19:39:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038878 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 42D452459EA for ; Fri, 4 Apr 2025 19:41:24 +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=1743795685; cv=none; b=mBha8VmfuLGGVO6mM3CGFMgT7hSLr8Hraop211114xr65BVx6gXEsFnYmlXYW04svOpuN2cXpHnpFSVFZSCokiIdBWg83p3j6rTzti3D9tFdVyfZ0CMahSFJWXCC9lfqeJIX6oBZyrHFxFj3AXyoPJPwGBMsKLrGInq1U8GBiJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795685; c=relaxed/simple; bh=mBywmRA4/NC4/3ktj7swW5HzxdRaFgk8AKVqnst2exc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bQntZGaw0r8zToVQyhY2R6P0vwpviPFjCWSWtgO4/awVt/OFCrT6lhmXcM9eSpwE6j810/jlO2u6BnXPke8t/jro1GGjL6k0IONp422VRzz0z+h/RdBobjd32uT953Xczn8BMkqORdUncb2OMJ97Equ+Bd+1RfKvqIOIvqTnulk= 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=X1by/HJM; 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="X1by/HJM" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-739515de999so1998340b3a.1 for ; Fri, 04 Apr 2025 12:41:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795684; x=1744400484; 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=BWR485RXwpxC9/TJjlnr5W4WV3qeOulIgWhB0Aow57s=; b=X1by/HJMe+mJG9IaI14nurtnK0RBLXmXZccWzR/PTdN/dnn7kzXmaKM7UMYMVuyRzE LNdw2c9rOgPMHRjuYVx2e9SHD+qGZUOOv4TWl3nJrop/i81L8O3GRw07hunskMqJ/qK3 IkD7eD+cYi9P2hz6fp8p8JtGfvQV2azQgB25AvCGsqkeGaKAvjg5cgdIWcw9wRVfRVum 78RbxTkRUzolHN/EaxnFRyPUkiD9sKam/l84bkjk8EXLgFS9J4AH7vCck6SthVdlWg0Y 15virr83/BxpgXdYglI45gHrzErS6bLb8ahnBI18xFP56nea66vHcdyv42Z039sI1i1F hHfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795684; x=1744400484; 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=BWR485RXwpxC9/TJjlnr5W4WV3qeOulIgWhB0Aow57s=; b=jWG8jUwADW7Y9xCuHKO+tvzayGGIKtUmhm0Da+Kh2V/+dfGutnz+wq+szcYnnAhwpW oCIUTsyTpzA3GTRKT1h0ayhwEaMxzGzlOMHunoFbM9VO3rVsyYHV+HbKPjUEMyGjeJQ5 eRvdZCeIQ0Zl0H+hcmZR+yDFbyZyZybR2JI8kCXZgYKa2lXklKD3GzrOO2FdGb4AMxDF pNJCSRq4Ky5IdmrD4+vfEjPkJqT6hiiiJA4cqRN4wq+mSI1Wsh8BstrBbcKrLPMKY4Rj qo4zVddAGYN/XRoB2upS/c4kDeS9MoC3UgW4KGGI4zY+cFlV9CHGdf/o4u+rq4lOznQ2 yzPA== X-Gm-Message-State: AOJu0YyVFBmCcPTYBEXIBeLitX0VWXJm5XdmZ0ejxSmRjOoVP2L+dF2V 1KBnv9qB2NQ4siYu7HOZHcrDyMHPlWhelWtAW9PLoYRv4PbcOOg0TXj2uH1f82+o5VO0Q0rghmM Y+A== X-Google-Smtp-Source: AGHT+IFrhugesHcYwD2EtEVR8p7v599AXO0yZ1dLVE7YlsaYY7QqtLthcLHvdmvsan5d8gnX4rE1BdgAjQI= X-Received: from pgc8.prod.google.com ([2002:a05:6a02:2f88:b0:af8:c3c6:e3f1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:1304:b0:1ee:ce5b:853d with SMTP id adf61e73a8af0-201081b2d99mr5653839637.39.1743795683722; Fri, 04 Apr 2025 12:41:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:11 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-57-seanjc@google.com> Subject: [PATCH 56/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 a932eba1f42c..d2cbb7ac91f4 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -713,6 +713,9 @@ int avic_init_vcpu(struct vcpu_svm *svm) int ret; struct kvm_vcpu *vcpu = &svm->vcpu; + INIT_LIST_HEAD(&svm->ir_list); + spin_lock_init(&svm->ir_list_lock); + if (!enable_apicv || !irqchip_in_kernel(vcpu->kvm)) return 0; @@ -720,8 +723,6 @@ int avic_init_vcpu(struct vcpu_svm *svm) if (ret) return ret; - INIT_LIST_HEAD(&svm->ir_list); - spin_lock_init(&svm->ir_list_lock); svm->dfr_reg = APIC_DFR_FLAT; return ret; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 43c4933d7da6..71b52ad13577 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1499,6 +1499,8 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); + WARN_ON_ONCE(!list_empty(&svm->ir_list)); + /* * The vmcb page can be recycled, causing a false negative in * svm_vcpu_load(). So, ensure that no logical CPU has this From patchwork Fri Apr 4 19:39:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038879 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 97FCA248191 for ; Fri, 4 Apr 2025 19:41:25 +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=1743795687; cv=none; b=bDGttZYaW7NIivdUAwljzJxmnoMN7YWU9YwD/COBj+otZxflfJ7aZhRHHsxlVkdtUd6KiidivwxbpYvCe4Of8YZP1Kga3HvRLIe150g3zcUrbyUC45gIqN43svIkONnlIeRQURNkPyrX1OZbspsmiVnjAWopKiFtUcoMMExmJeI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795687; c=relaxed/simple; bh=CJbM5ki6wbRFycki9+F8amVo8XEDR5j/tEEiEsSHYK4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Q/1zpTE0JAqse2vkDiMI968Jn3r/p4fK0aPi+0kgTkoWILP+Ll/xTgOGSl7rEWqB3pHneFlW+D0tNwnKbDorrj23ApQTGoYsDqtXDX/NFxaPcfplNxnh1f4/gRkufIxzhDLeq58ZdmvLyfwMvJjP2gehjBGCqTZ8JVfp+lrZ2cU= 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=uwR6Q7e9; 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="uwR6Q7e9" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ff78bd3026so2295923a91.1 for ; Fri, 04 Apr 2025 12:41:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795685; x=1744400485; 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=4PrXQXNJnenxPucuA/yaggd1GFmcPYwGnyvurjMeKTM=; b=uwR6Q7e9Trwz05/kEicsgJC1kcNkci0WLv6X7ApIqiwVaQ4AJK6ZFE3pGkl+8TasiM evqDkS9GZ+SoIiRVMZ/MZKa3DwhYHOBiuixyXkHTMpe+ws1Z5HxEnBFNUHuYi3CYIFZl aoIoGcwgaqRXktEb37WMZF6z+3GGTkEC7c8vgDEgAIGCvMd4QB38L5k3ZtjMjGB1ma0U LSEUjBLV9eo6x+szQrSWSGnSSDvaj768AP6P5CQ1POOX5UbkUz98urbHoJDBQGijPbvp L6M+lLDrZer5mcO0/jkjiP7JvH5JBo5Al917dBeXVBtTXs+uw6SlEFm5miqYU+qG4TMD AZUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795685; x=1744400485; 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=4PrXQXNJnenxPucuA/yaggd1GFmcPYwGnyvurjMeKTM=; b=mmDNLN4HUA5F7Nu3aaFTu6PCTKtyyaixKjTAAhIw5lWV69CGaBobGJ7UkZR/dj3Vfy ElR5B+mcHQWx5Dc3PCcBXLsEZTS3eB7dCgChqmHTlXaHHzqaHbTvGaQN5CyEfsD8Y2VT 7vGorYuCubUTNswoAsFP6xa6z57sSxd5ROot+4hTgaSbOvISSW4NKN8XcUF9TGAD92TU CUILFcgK5os8jWOuU/eqTgHrHWzWxDxt1X3Zh3yMZ211kT5gsnJa+b4tmL00/sgEYe89 EdktZkrq7fUOEiWKVeggFjKqWdHV0oWdvpXh6V50ThE9RgHG1NnP2c3SHZjPhBtBsfHl LUdw== X-Gm-Message-State: AOJu0YwKpj5mVZtxNVB+fxiMU41pUelRdbkZLgznhpBOxOIXevQzAZhH hsOz7N92/vnsxFJUKd+9gyzoMU5IcGUHPcfIAR/LnDLqcltS/svdOwRQyQA6+vxoMgZ3cXIhhZ7 g0Q== X-Google-Smtp-Source: AGHT+IHQ6ptEcPjIqq3mLe7k3SWbX89UMoO78N6d4VQjJKucTdQNZ9R4r8s6zKowOLbjvdi2tyWJH46WVgk= X-Received: from pjbsd12.prod.google.com ([2002:a17:90b:514c:b0:2ff:6e58:89f7]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4d0b:b0:2ff:6bd0:ff26 with SMTP id 98e67ed59e1d1-306af7b7889mr596948a91.34.1743795685197; Fri, 04 Apr 2025 12:41:25 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:12 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-58-seanjc@google.com> Subject: [PATCH 57/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 | 3 ++- 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 | 12 +++++++++--- 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 823c0434bbad..435b9b76e464 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -111,7 +111,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_host.h index cb98d8d3c6c2..88b842cd8959 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1372,6 +1372,7 @@ 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; struct kvm_pic *vpic; struct kvm_ioapic *vioapic; struct kvm_pit *vpit; @@ -1840,7 +1841,7 @@ struct kvm_x86_ops { 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 (*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/vmx/main.c b/arch/x86/kvm/vmx/main.c index 43ee9ed11291..95371f26ce20 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -133,7 +133,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .nested_ops = &vmx_nested_ops, .pi_update_irte = vmx_pi_update_irte, - .pi_start_assignment = vmx_pi_start_assignment, + .pi_start_bypass = vmx_pi_start_bypass, #ifdef CONFIG_X86_64 .set_hv_timer = vmx_set_hv_timer, diff --git a/arch/x86/kvm/vmx/posted_intr.c b/arch/x86/kvm/vmx/posted_intr.c index 2958b631fde8..457a5b21c9d3 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -132,8 +132,13 @@ void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) 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); } /* @@ -251,12 +256,11 @@ bool pi_has_pending_interrupt(struct kvm_vcpu *vcpu) /* - * 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 wakeup + * 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 ee3e19e976ac..c3f12a35a7c1 100644 --- a/arch/x86/kvm/vmx/posted_intr.h +++ b/arch/x86/kvm/vmx/posted_intr.h @@ -17,7 +17,7 @@ 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); +void vmx_pi_start_bypass(struct kvm *kvm); 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 49c3360eb4e8..fec43d6a2b63 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13511,8 +13511,7 @@ bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) void kvm_arch_start_assignment(struct kvm *kvm) { - if (atomic_inc_return(&kvm->arch.assigned_device_count) == 1) - kvm_x86_call(pi_start_assignment)(kvm); + atomic_inc(&kvm->arch.assigned_device_count); } EXPORT_SYMBOL_GPL(kvm_arch_start_assignment); @@ -13630,10 +13629,15 @@ int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, spin_lock_irq(&kvm->irqfds.lock); irqfd->producer = prod; + if (!kvm->arch.nr_possible_bypass_irqs++) + kvm_x86_call(pi_start_bypass)(kvm); + if (irqfd->irq_entry.type == KVM_IRQ_ROUTING_MSI) { ret = kvm_pi_update_irte(irqfd, NULL, &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); @@ -13666,6 +13670,8 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, } irqfd->producer = NULL; + kvm->arch.nr_possible_bypass_irqs--; + spin_unlock_irq(&kvm->irqfds.lock); From patchwork Fri Apr 4 19:39:13 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038880 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 ABB45221F32 for ; Fri, 4 Apr 2025 19:41:27 +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=1743795689; cv=none; b=t7AfIu+DULmJe6/jGUWwwgFf7n/hgyCOnoXTeZ9PNrCYapmkcb4ABA98tFXFYaDn1R/dSW8BDfFiPrJNA7g67mXwqlKD338kNcYfqgBpEP+5bqK0VKDuBDoy0awQxaGyJi26zl/rx/WqwwFxUSqW/gMG8s/yXuITAyL84Af9q80= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795689; c=relaxed/simple; bh=oILWHlZoymAyC2X3yedwJsSZpaT6PkdFe55df8DN2A8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lL2xADxif2HfzeKz6pfOeAEY9/NRyC0WA4wkh4Ow4gAoxByUgFVqSEutvlFfsLa7o264dt5LqKGAoEM3AHlv1dMw1Fqrdo8gqCfMluW853zv4PNmiH324wLFGg4N2vow6kYSyegGFU25fzLSZZxvOWLPCr40QLPdXEVSYHFhmjo= 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=N6vMQf7Q; 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="N6vMQf7Q" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2240c997059so29575645ad.0 for ; Fri, 04 Apr 2025 12:41:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795687; x=1744400487; 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=tpUGu5WACeQY+DXaIgbcGZ/fnmY4/g2Rj2L2cuTjvC4=; b=N6vMQf7QHggN6Cyrqc0cLgrW3AiYuo5pLuNmm9hDNe4D9ctk1c5e0vFhRmqCdgj40Z 8LhMwJ3ZT/eztpCXhBBCaeT9EfnJVvbFqpAXG/a1p19LPWntBT9qTwAJOVNFH8KsMwPL R1a7nCdUupH9jz4J8Fiygs1BNZb4daEr4MfgjGII46vXivbJ/NaRpL9crbH9uyIfCI07 aN5qk0l97tXAGAyvpYmVq3YlbFjwwAMusuYUIaF2rxI4Fd0EpA9Hpr/xUHG05qBOcA9q c0B/hjRQXBmJHPtoFqHyNbLDYoRzd4fnS90+va4it3MmYK4BwSd+oZ84G9bsjc6vLu6E uHRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795687; x=1744400487; 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=tpUGu5WACeQY+DXaIgbcGZ/fnmY4/g2Rj2L2cuTjvC4=; b=AveRAgcZJ2nVvDnJlYn13ia4x1pMW4hVsyZNtUZ1hHGhQ4hpNHt1+JkwgVfZmd9g6E pfaL/sesOVLJtb2aTtbeZSKpEwlbOq8F327t5e64vb4WGJuSTfUazdd/6uipEo0Bc8W7 vk6r49LK5xC99H2ARBjIT87Nt3Y27PDKvajxJAEM4oKZq4BsiVYbTBQQ3HKF4Nz2pXNe UjaAtZVwe64jGRKLTrRHl7XPiCGP/BTjpot80cYuklqbJtd9vNYx2I1U2slI32hb4rde nQwiDRoQljGZ153nB19dtRCS5pA41lmx5AuAWtXxqsQS+Y8yQ6an4ududELqtScWVi/2 gIzw== X-Gm-Message-State: AOJu0Yx3nXEgCrjpKzErY1cwf/tJ+kRy6UzwZXkSCdL1eDTMlJa97hsI FYH+3KFhSI9CeDDeQHwGqzinb9Jg58RFxhHUks2Lontp/qFIT6hj1de0myedrl5dkNV5gd6Ce0O I9A== X-Google-Smtp-Source: AGHT+IGWIJW5oXdpll4D4kyUc6CqYVXv1H9LdsgiZOQiFY8S/63oNQDk+1VKkmtwRbHnjm+dtN9Wq0qn+Vk= X-Received: from plbkx14.prod.google.com ([2002:a17:902:f94e:b0:223:f59e:ae50]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d589:b0:224:76f:9e44 with SMTP id d9443c01a7336-22a954f9e33mr7330205ad.8.1743795686999; Fri, 04 Apr 2025 12:41:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:13 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-59-seanjc@google.com> Subject: [PATCH 58/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 457a5b21c9d3..29804dfa826c 100644 --- a/arch/x86/kvm/vmx/posted_intr.c +++ b/arch/x86/kvm/vmx/posted_intr.c @@ -262,7 +262,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; kvm_make_all_cpus_request(kvm, KVM_REQ_UNBLOCK); From patchwork Fri Apr 4 19:39:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038881 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 4244724C099 for ; Fri, 4 Apr 2025 19:41:29 +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=1743795690; cv=none; b=ZPab5FY6RhmFkD+0VCYjvvemZxmA1zMVawqkc4mLjt7thBYadhvL+JT7QCzTjHQBSu6JXY4FVsjuTiDKgPsuJd8wLbM15Tjq9krJ/Tv/SaFGAeY78V9I4ZyHtuVUQ88AWfnUBILjYR+nUm8NXyT3n4Upw5TJhxFfJM/xcXlKXPQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795690; c=relaxed/simple; bh=U7Bv3Mou19gPLSngEjr+1RsKLIdn0l5YTp9cdhCcRAg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NKUovkx1run3pRus8ZX5Ue9CKb/jgoXdZYIBpZSX1Yq1y4LBfTTDGCaC21zD/4NzAnutSl+54sicQOJXoznA/mTydS/uGiVHtLXOQ977+Y//l+S4ezy9TYIrcYxUcbZ+Q6ztU2fWBKPtAGUqdjXmcftM/NIZAvWkNAgOTOOQ7f0= 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=m1gZrpLm; 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="m1gZrpLm" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736d64c5e16so2097623b3a.3 for ; Fri, 04 Apr 2025 12:41:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795689; x=1744400489; 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=mt07Qxkb97s5FkZiQg7yKT2x8NR3KghJCfR+NZxwIRc=; b=m1gZrpLmuzLk2+Q7XZiElC4WNAo3q94iiOcWVxm9o009DCtc5TiPC5tGRvMHS3TkHP auwSvrur18wQbPFnY+s8NTtOWzHA9bNYyhgdp9u0WMnmrcJvLfLzh3TyO1W+c8Jxsng3 4VT1APrBNGKz/PfDTYdTOd+MgsaZLL7tOgEkas88Q4VAOCZ411fe6gArtzo0rtOszYqq wahV17wVJdWrkDCdudDBrufQKi2ojDkMxe56H1zaOh1HO+EzidFU4RjnRQ8yt5Dr9GMC ij26bY6+HWaQNwD7FkOVjoousWF6kKImvXPmAFBRhMc4zLkh7a7pfFutnbNcM9plx/p+ VF+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795689; x=1744400489; 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=mt07Qxkb97s5FkZiQg7yKT2x8NR3KghJCfR+NZxwIRc=; b=Pn3c9RHio2hfq9dPXMEWWw5E48H22zb/6nGOMxrJM8ehGgJXTl8+6U5o4IXv9+wori p1/jg8jA9wxDMDrPQNTMOwaqptYSk0RSyofLKHapR8h8Ots4nnRHT+U2/77W8nWcy/1J YJVa/l4ibcASWFPmddobZH0vXj+rxNXpWsHjQyeW6xXAkXpL9s9vVkdYSigz6yfBI96v 1jUfbytMBRTfzCPxq2OcaN/gE3bVs163WS4Cr62FNEl5+O1ng3T17/bMNZPPOn4GMQBx 8IFsM78yf8eeUcsQ1qr/7Ja6Z72xBx+xZAs3T5P+ZVNbbnxyIPMJXeh3wQQeNKSvrvwx NljA== X-Gm-Message-State: AOJu0YyodSQRupSlj/ZETuvZRKte/japQDXbxaV2WY344/pSpheD3Zlg FjGwBGbe3K/REDq4b8kufRfTEJbHGkj96xF+erP8ue7p+p4lO9tlVoI/Cr0irM+J3O1mP99fd7h 7fw== X-Google-Smtp-Source: AGHT+IHKRRqtQ+5sxIr+azJfU1pTEj52X3kWKBhqR+uBWAf1O7lXz3snAlLOZvwHP7CdcZyeKm5xblX214Q= X-Received: from pfbdf8.prod.google.com ([2002:a05:6a00:4708:b0:737:6b9f:8ab4]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:10cb:b0:736:5486:7820 with SMTP id d2e1a72fcca58-739e48573c2mr5416904b3a.13.1743795688779; Fri, 04 Apr 2025 12:41:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:14 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-60-seanjc@google.com> Subject: [PATCH 59/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. 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 d2cbb7ac91f4..d567d62463ac 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -29,36 +29,39 @@ #include "svm.h" /* - * 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 entry - * 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 can'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 value * 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 #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_SHIFT) #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) -#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) << AVIC_VM_ID_SHIFT) | \ + ((vcpu_idx) & AVIC_VCPU_IDX_MASK)) +#define AVIC_GATAG(vm_id, vcpu_idx) \ ({ \ - u32 ga_tag = __AVIC_GATAG(vm_id, vcpu_id); \ + u32 ga_tag = __AVIC_GATAG(vm_id, vcpu_idx); \ \ - WARN_ON_ONCE(AVIC_GATAG_TO_VCPUID(ga_tag) != (vcpu_id)); \ + WARN_ON_ONCE(AVIC_GATAG_TO_VCPUIDX(ga_tag) != (vcpu_idx)); \ WARN_ON_ONCE(AVIC_GATAG_TO_VMID(ga_tag) != (vm_id)); \ ga_tag; \ }) -static_assert(__AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_ID_MASK) == -1u); +static_assert(__AVIC_GATAG(AVIC_VM_ID_MASK, AVIC_VCPU_IDX_MASK) == -1u); static bool force_avic; module_param_unsafe(force_avic, bool, 0444); @@ -148,16 +151,16 @@ int avic_ga_log_notifier(u32 ga_tag) struct kvm_svm *kvm_svm; struct kvm_vcpu *vcpu = NULL; u32 vm_id = AVIC_GATAG_TO_VMID(ga_tag); - u32 vcpu_id = AVIC_GATAG_TO_VCPUID(ga_tag); + u32 vcpu_idx = AVIC_GATAG_TO_VCPUIDX(ga_tag); - pr_debug("SVM: %s: vm_id=%#x, vcpu_id=%#x\n", __func__, vm_id, vcpu_id); - trace_kvm_avic_ga_log(vm_id, vcpu_id); + pr_debug("SVM: %s: vm_id=%#x, vcpu_idx=%#x\n", __func__, vm_id, vcpu_idx); + trace_kvm_avic_ga_log(vm_id, vcpu_idx); 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 != vm_id) continue; - vcpu = kvm_get_vcpu_by_id(&kvm_svm->kvm, vcpu_id); + vcpu = kvm_get_vcpu(&kvm_svm->kvm, vcpu_idx); break; } spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); @@ -793,7 +796,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, */ struct amd_iommu_pi_data pi_data = { .ga_tag = AVIC_GATAG(to_kvm_svm(kvm)->avic_vm_id, - vcpu->vcpu_id), + vcpu->vcpu_idx), .is_guest_mode = kvm_vcpu_apicv_active(vcpu), .vapic_addr = avic_get_backing_page_address(to_svm(vcpu)), .vector = vector, From patchwork Fri Apr 4 19:39:15 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038882 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 16761221F3D for ; Fri, 4 Apr 2025 19:41:30 +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=1743795692; cv=none; b=WGNR5BIVkADxTd2At2Y/wCxqD4tvlytfD7BeRzNTSgWOAnMDexjt//+DvuoihDfmOqR0QDO7Vho2KGtVngHJTb3vdXc33J3JL2B6XqMLa3eRy7EKhZ4kREAx+0Kwoy5X4S1nr3DLjE1POH3v6Sc1ob7+BLvaBXmjk01Isx4j3JU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795692; c=relaxed/simple; bh=LM/a78fswUxarIaB9yJdchQ9AVFZwyJ7vf9uPLLFAxs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LlaKd25FrQklQGf2xkCyU/aX25BiYgJjCgsgwu7ZigLImGb5XP7iSchQ51VeBGRh9sJmCB6YcCCY5iy/GpF8oVY1ovIcdjJAXgxmVYpCQmah+mTsqH8VfV3g4ACPWsSfMOKuctWbWOSDGdV81ySmpTQldnzz7Th911N61AvcksU= 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=fail (0-bit key) header.d=google.com header.i=@google.com header.b=IsVNqCQr reason="key not found in DNS"; 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=fail reason="key not found in DNS" (0-bit key) header.d=google.com header.i=@google.com header.b="IsVNqCQr" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-739525d4d7bso1907968b3a.2 for ; Fri, 04 Apr 2025 12:41:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795690; x=1744400490; 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=WUo/P/nbSyKSB7hDI08Ajk4szbLYpfhAgRQ1J5OUkMU=; b=IsVNqCQr+F3fdQUnityMBvVU2Jl2S0/RjIgEADXtzlT7I5ZvpQ3e81+BbkKJJZSMEc 8/5i4ww2Af+xLJ2JWrryZoHNYNR+CWuZZbw5yU/9kR/cIF/xEm9tnKB5EbB/m7eOM+Zr t9Nbx1KR3I8D9c9a/gUJ0nvge/qq3kC3t0UR5MadRoDgwaZhDDgwBAG5rIACDRzqL5GB FtUA/C6LugbqUQ8Trgw/KhwQ2Vvozcro2VqHe47vecLWKfkhvMULWEdMCCBncTfDsDCQ wDhQ+PAbSECCAxTlRKN1ZXXqLvWf/5LCK1J74ZF7S1/K9sBFpJsOT/JDwxT+OBUOA5FN OXag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795690; x=1744400490; 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=WUo/P/nbSyKSB7hDI08Ajk4szbLYpfhAgRQ1J5OUkMU=; b=wvgr5PeNPUIgCqWxqQbJRQAn9oholYle/pxEGZwLB1i3FuBCu6RQD6LjddmwtHk0Z2 VmxUhFG+t4cA4E3JQfhNfbMLOGLEPJGTntGEbtSGJ3jFURhJcSYcfJ8kyBmVlIG81xi0 Yol9qryirx1fOVfGEFpKupK+WvNURMArqMLzO1hX6Jkrzf2z047RLWt5hWFUbKAGZCLe 3PpApt0BVXhp82/nN7wX2wKfd8mDFP1pCxDob0uqafySH7SjiBIUGkKF8DcVZSl/P9+z tm4pMrbDi2WNLGCisDIagv8kWoLe4LR7b4jTceUiNJdieB3pSw9dYFMBjSxFg2eHyW+9 3t6g== X-Gm-Message-State: AOJu0YxAeUiFk/iJje1UMWlJmRGN4xYKDYYGEn4V5P3oVw/ITfZBjbbq MnRwCqWvM0fd88nFIV1uCjpwFh+Vmi8BdHG4rM0P5f2EUpXRSctAqdFwez3cSSgYwFNstmiG1dm 2bQ== X-Google-Smtp-Source: AGHT+IFrffkAJKPjpzU/UB+RZgd+9eYfpIMh5YaMNutNyipzajp2Id7VaNjJc8C8Wc5drn94Lg1LW0x5Xkw= X-Received: from pfbld6.prod.google.com ([2002:a05:6a00:4f86:b0:737:30c9:fe46]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:130a:b0:725:96f2:9e63 with SMTP id d2e1a72fcca58-739e4b7b2bdmr6930633b3a.24.1743795690483; Fri, 04 Apr 2025 12:41:30 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:15 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-61-seanjc@google.com> Subject: [PATCH 60/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 a881fad027fd..2e016b98fa1b 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3793,8 +3793,10 @@ int amd_iommu_update_ga(int cpu, void *data) struct amd_ir_data *ir_data = (struct amd_ir_data *)data; struct irte_ga *entry = (struct irte_ga *) ir_data->entry; - 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; if (!ir_data->iommu) @@ -3813,7 +3815,10 @@ int amd_iommu_activate_guest_mode(void *data, int cpu) struct irte_ga *entry = (struct irte_ga *) ir_data->entry; u64 valid; - 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; valid = entry->lo.fields_vapic.valid; @@ -3842,8 +3847,10 @@ int amd_iommu_deactivate_guest_mode(void *data) struct irq_cfg *cfg = ir_data->cfg; u64 valid; - 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; valid = entry->lo.fields_remap.valid; From patchwork Fri Apr 4 19:39:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038883 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 CD4E92505A5 for ; Fri, 4 Apr 2025 19:41:32 +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=1743795694; cv=none; b=LcymPp5P1TLj6QsRZLLZn3cshGEYzx5XIIw+GEgnvcvt6pa4e67bJBZc/zDijfKBHimK32J+Zx7L3+kpfv6QGmM/JTa7w03XzGcVs7e0fltShJyKP3uZlnm1G3E7hC37WN3KY/DpglfYgdQd1M9ZeGZX3+WzK+yXW9EEAARUzxI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795694; c=relaxed/simple; bh=R9sMWALkBgwfi6cvHRh8b/xeGw/exHZkwCm6FFV2BPA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EX8XWkVPDrQGswXMoD3/sxOJO3V7Z68aTXoQmN/xEl2J/e/MOvDNOg8ngxydWOWIXFyUZ2ypf02KlE1LZ6OzJDae6C6gfbjGrh7gUAJtxb+o0J/2nyjL7bTjLwNmPoKgYL3iYiNgQ6jxmsC7t0w0g0yBuwpCbi2jDNzMFAN+DnI= 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=fail (0-bit key) header.d=google.com header.i=@google.com header.b=jHI+gb6B reason="key not found in DNS"; 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=fail reason="key not found in DNS" (0-bit key) header.d=google.com header.i=@google.com header.b="jHI+gb6B" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7394792f83cso1891536b3a.3 for ; Fri, 04 Apr 2025 12:41:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795692; x=1744400492; 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=yvqzDsLCWBVkAFBTdSkT6iL07PKtulllv44RSpoEpAk=; b=jHI+gb6BwmJk8l9Im1yWMUtUDSrH5crqtj26cdWM2Pz0dvWJftFdkXgclxceg7s0yA mqtKqSGqTax+JucmuJ75VWBGex520U1dvG44G7TfMqehzIqu5N5kytmF1z3rUWh9Q18G vSDKdIp4ozFMEBMztR1T1msaW8IXIAcLCw5MPnjFKUNiwMiULkb5lATUuOqsbFTdZpQL PB1XgNFMfNCVQx/BgCHpAPdOs2epoeJfq8J/401xT1Y1RJHr2CNUm9SBdDwB9YmlFuP+ djfaQZBlPstNFreHZXvjVIgtwfjoVWLvplSS9yT18tcZpWkhEglmFBwXMBjSPMD5+RlG 6+Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795692; x=1744400492; 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=yvqzDsLCWBVkAFBTdSkT6iL07PKtulllv44RSpoEpAk=; b=DukN9/yggK5yqceEeA31KxkTP9MXCzkulNW1xe5YD8swHh2IdSkJzqGsp8dAnkPaJ6 dI3WBsjQ0+b4Q6V1+hRNbmKatO5Q+v3sozF0wuNw+QAd/F+DkocWGmMLLIP85teNixTt cXnJDtP42DFF8y3YCMEvNOcJrf3a7nm7K3gBCElHva/38HbI7elnvHgkmLkXM5Ele0ej A+ckpYgIeT0jQP7zbkVeDc/nCprbi9uT/OShC8G04IxpDrOgPq3cxbN+PyCpPGa+8VCX eSslD8zVF96UoczofJD7E/3fxEMstYK7dPUqSx6uJCDxoJODIA/sETZ4SXUcp5aP899C RuNw== X-Gm-Message-State: AOJu0Yw2Vo1D74rikaFqLwuzpHa7B3Np7SkMP+6WYLVsMnJ75RFl/fOa WtaCiMnlYzT+M/5VDOcaHGdMqIbu+3r8/so/DpQmxSYXdc3614Z1j7rN1GeJl9eP9hmajBIAXg7 01w== X-Google-Smtp-Source: AGHT+IE79fYw6sxc2mtR0ZTsjUgMLzDtlDBHKt8S//qI+fFV8JgTCb/EHs15a16b5eLDWcD9q+k3TdFqdb0= X-Received: from pfbef12.prod.google.com ([2002:a05:6a00:2c8c:b0:730:796b:a54a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4653:b0:736:a540:c9ad with SMTP id d2e1a72fcca58-73b6b8f82e9mr712492b3a.20.1743795692201; Fri, 04 Apr 2025 12:41:32 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:16 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-62-seanjc@google.com> Subject: [PATCH 61/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 48 ++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index d567d62463ac..0425cc374a79 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -737,32 +737,6 @@ void avic_apicv_post_state_restore(struct kvm_vcpu *vcpu) avic_handle_ldr_update(vcpu); } -static void avic_set_pi_irte_mode(struct kvm_vcpu *vcpu, bool activate) -{ - int apic_id = kvm_cpu_get_apicid(vcpu->cpu); - unsigned long flags; - struct amd_svm_iommu_ir *ir; - struct vcpu_svm *svm = to_svm(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(ir, &svm->ir_list, node) { - if (activate) - WARN_ON_ONCE(amd_iommu_activate_guest_mode(ir->data, apic_id)); - else - WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(ir->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 = irqfd->irq_bypass_vcpu; @@ -998,6 +972,10 @@ void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu) void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) { bool activated = kvm_vcpu_apicv_active(vcpu); + int apic_id = kvm_cpu_get_apicid(vcpu->cpu); + struct vcpu_svm *svm = to_svm(vcpu); + struct amd_svm_iommu_ir *ir; + unsigned long flags; if (!enable_apicv) return; @@ -1009,7 +987,23 @@ void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) else avic_vcpu_put(vcpu); - 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(ir, &svm->ir_list, node) { + if (activated) + WARN_ON_ONCE(amd_iommu_activate_guest_mode(ir->data, apic_id)); + else + WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(ir->data)); + } +out: + spin_unlock_irqrestore(&svm->ir_list_lock, flags); } void avic_vcpu_blocking(struct kvm_vcpu *vcpu) From patchwork Fri Apr 4 19:39:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038884 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 54978250C1E for ; Fri, 4 Apr 2025 19:41:34 +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=1743795696; cv=none; b=g21ssgyd0+x9eFnEebbU1UUUsULriRizglanKKjeDRw1m2dX6eNuSWulkiiReG2ms4feDF992xeLu+K+9M2kRNvUdabvpLu7QciJn8T+Kr8ndHwxA9nnNRHzR658vf6wSb3Yet2uQ0N2sl02xydEfoSbgbxJxYTQ4cEww1TnMRw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795696; c=relaxed/simple; bh=jVZErwdal797VpSsrwTL71Rrrie0hFV8uvPJVa7d+Ug=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G3spmNlIbXIB85MnZsPOW783f1JOHqtU+3otCrK/lIXdqjbI1fGDVzd2rN9hNIDnVo0itTabhkIBAEP9/ENf8d/CkNGUZwIkuT5eMU4D8DC8tS4pD3eD5Ph6vc8Rjk+OzD/mPOETe1Fe6H+LL6yim4xT6UQYenViRGbF4yqbPdo= 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=hyBULlxD; 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="hyBULlxD" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736b5f9279cso2256589b3a.2 for ; Fri, 04 Apr 2025 12:41:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795694; x=1744400494; 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=H1ACwNhde2OYKIaCE2zz1dgH1XDk00/KwfgDPrzpopE=; b=hyBULlxDoWQiiD0nsirhWlqiz05A8c9jXi4fXenGaUdaYYmEPstm5ACmQjtQM5eETa LDMW8u8tvOhWrCgulnAPLw+PAR6OzO8hnxlzgg05hMmgJtbtSBmwoY3/Lz2420fakCKQ z1gct5L+5JRATa0qTw/fGcjAWo0TbFbicEovonA9E/1jATIRkJfqydpJ5h8gOiOUSvZB e4KzTxh1iIHf+0Rwo4Uius9OXpYvuH651HMqEz+YFnnKtEohfj08OXGF49NgsGdhcbeH UDaTrZVE3uFDuR0wM5Db1l1Btx0YUwUDa4FjgAwzoNfIqk2UuEbRLiOXpseAzIC1JiRH adVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795694; x=1744400494; 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=H1ACwNhde2OYKIaCE2zz1dgH1XDk00/KwfgDPrzpopE=; b=hyhF5c8doNCt8voKtXens7+s0QzGYkMmC7oeI76OBOJheGKhe2rf0UWEylYuCOgXfp ZKZxm1u2WhwmPF7+MKUcwVc6p26+1hWFakFhqDV6lfa219RdKr+kA8em/1w1va5X+UJr cNFEGnNqGig4HUssOL3rUCa7TH9k+CvrspZyORErxZA9Z2PQWS6YKHuYqlB8DhWX6ETp nfO/RmQBwJcei9tka7YU8t+JQxAAuWG8kLaQyZ36Muw4XU9rrhLsSaViMhUkskVaSf8s 1TgW+kSxZfAcW2j2BHtyfqP4QUZILVGjDOvBA2y0LyeRhzeUQY6jEloqh71gQ0srBVX2 crMg== X-Gm-Message-State: AOJu0Yw7D40bnX4z1czgd/eIWqMp3Lmgu+gPbeXsLYb7rHNbz5jzjPWz HsBqXAYCQnoS8VuP8W8KftuDVvDUgYcPQXFQTI6tq47u1vGt95+MQGDGdnHytXEy3JyDL6VsoxG yXQ== X-Google-Smtp-Source: AGHT+IGbZ24Q6rwJO75l7fVdcdLxJb2hwrofRssz7VDHE5j8x4DF+N+5JpsqYoKoJNTaXTZKLWKOQNskhGw= X-Received: from pfhh19.prod.google.com ([2002:a05:6a00:2313:b0:736:ab5f:21f4]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2e14:b0:736:4bd3:ffab with SMTP id d2e1a72fcca58-739e4be89demr4703540b3a.17.1743795693901; Fri, 04 Apr 2025 12:41:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:17 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-63-seanjc@google.com> Subject: [PATCH 62/67] 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, Maxim Levitsky , Joao Martins , David Matlack Don't query a vCPU's blocking status when toggling AVIC on/off; barring KVM bugs, the vCPU can't be blocking when refrecing 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. 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 0425cc374a79..d5fa915d0827 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -838,7 +838,7 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu) WARN_ON_ONCE(amd_iommu_update_ga(cpu, ir->data)); } -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 = to_kvm_svm(vcpu->kvm); int h_physical_id = kvm_cpu_get_apicid(cpu); @@ -854,16 +854,6 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) if (WARN_ON_ONCE(vcpu->vcpu_id * sizeof(entry) >= PAGE_SIZE)) 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 - * 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 @@ -898,31 +888,33 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) spin_unlock_irqrestore(&svm->ir_list_lock, flags); } -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 = to_kvm_svm(vcpu->kvm); struct vcpu_svm *svm = to_svm(vcpu); unsigned long flags; - u64 entry; + u64 entry = svm->avic_physical_id_entry; lockdep_assert_preemption_disabled(); if (WARN_ON_ONCE(vcpu->vcpu_id * sizeof(entry) >= PAGE_SIZE)) 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 - * 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 = svm->avic_physical_id_entry; - - /* Nothing to do if IsRunning == '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 @@ -942,7 +934,24 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); spin_unlock_irqrestore(&svm->ir_list_lock, flags); +} +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 = to_svm(vcpu)->avic_physical_id_entry; + + /* Nothing to do if IsRunning == '0' due to vCPU blocking. */ + if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) + return; + + __avic_vcpu_put(vcpu); } void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu) @@ -983,9 +992,9 @@ void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) avic_refresh_virtual_apic_mode(vcpu); if (activated) - avic_vcpu_load(vcpu, vcpu->cpu); + __avic_vcpu_load(vcpu, vcpu->cpu); else - avic_vcpu_put(vcpu); + __avic_vcpu_put(vcpu); /* * Here, we go through the per-vcpu ir_list to update all existing From patchwork Fri Apr 4 19:39:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038885 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 4A2C02528FD for ; Fri, 4 Apr 2025 19:41:35 +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=1743795699; cv=none; b=h6G7oQHhUYpTqOtCC4CGXzdlIpi7Tb7zk8XgH5O75+EnyIVleef/ckG7EV/ET/YbPjKk8VSMD7sZkWHnSlPOisAh3y4DjQYcGujb+C/ujb7jMtrDYLFvHedgJ4TuBwfUMC4A61oy6K4Gqh6YaRjoNLRnh+i69DPbs5ZqtRmd0gk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795699; c=relaxed/simple; bh=XeOgBYSVftBtJy/M3mgh+l9BlLK/PoI1PFhoIB5PCXs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kzGuVqbfWvTlCYkQUIeymaqVZmOzxDB6eSrDoifsOBUIV8n+rXwgtqCpQ9/YakhNlxHQAhDhjjZt/yh3P0+jpAfh2D1eWxYeZm3G+fisyYZL/B+QogDxIktzZQZHjWvHws3fcn9JDfeCEtu2BhLcMdBhM2cDYKOLUFyTp6XJLyQ= 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=OEwTvkn7; 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="OEwTvkn7" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-736b431ee0dso1964199b3a.0 for ; Fri, 04 Apr 2025 12:41:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795695; x=1744400495; 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=gDdOE96/ndlhSLSZ4xilIdGO5S14+if5sDDWyRab2v0=; b=OEwTvkn78vyydATz8/iiEiX0FjIzhOOTEOiVrRSDPgS6lM0io8KwAnCXqufS4MEOb4 yxBjY72ZGUYYGX/WmWxlWEQ2W7DqyOmBBUdwEaPPrZall9bJ8xPx3Hr9E/RSonqw4XER Ifm/I+31coqI7QyRLqbcIsv8xYddGtGRt3Z0nnBFtK5qyIzV6ARQ7x2bjvaoq1y4qvSU LUKLiMVSaDg3lhcl+4HtipvyeNKJsPqnD3lQYOq4izMYHZzb5tcxzY1OdXp0TfjPy0xn Uc8YqrVoo84V7FJEI14+yn492vj75Vv+Fw2ArblzweVAgTIDN1Rg+qknexQWLbR8x6Z1 00zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795695; x=1744400495; 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=gDdOE96/ndlhSLSZ4xilIdGO5S14+if5sDDWyRab2v0=; b=o1VTWb35dbBoFbStBPsvvLY6y83NyafYib3Y7udsAHKryxvKz5fVudoP3Fs2UzrTnm jif1D54tvMx2GgBTtiBoW5tpasjciERXwlGt3Q63IX4Z9alisaD/Y+g6Kcx1z9zwQnv4 OJGooiEimCq+M5ELR7iosVu+337y+MhbArqmiDfi/IY69/FQjLxQZx1NdrC/IX+Fq+in YYKz7EAJepZOLNkq1/9IWeQsXwieI0hOJmRnV48ixi0rdLIulblqtbZ7BYGwxXSAAThP qeIZNX8wXZQwaIBN9MTvUtCPPB20IbnykkBuuYDFCLr5mFWXQ71fQnXItZLOs8nXwQ1h LyJQ== X-Gm-Message-State: AOJu0YwKXB0qg+hT/BWnKq+ncywFj42dFyEumTBoQJfkae0dmyESZ4QV jBrX/UxJ1BHuMdq6T81bubohSAdVeeDKni6ow1ezrDWHH/i/DRnf992Y62wiT6quqyFAmyr29zZ Izg== X-Google-Smtp-Source: AGHT+IGBe+lm54I+yMwAzZHJq8Q74YtEHX3DjuXO0rhTwsnSv0p1gLbz4L+6vCz0PER0l1cbP14ndaZf4xw= X-Received: from pfjq14.prod.google.com ([2002:a05:6a00:88e:b0:736:b2a2:5bfe]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:2d24:b0:1f3:293b:7aa with SMTP id adf61e73a8af0-2010447cc25mr7173734637.4.1743795695588; Fri, 04 Apr 2025 12:41:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:18 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-64-seanjc@google.com> Subject: [PATCH 63/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 | 55 ++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index d5fa915d0827..c896f00f901c 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -820,7 +820,8 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, return irq_set_vcpu_affinity(host_irq, NULL); } -static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu) +static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, + bool toggle_avic) { struct amd_svm_iommu_ir *ir; struct vcpu_svm *svm = to_svm(vcpu); @@ -834,11 +835,17 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu) if (list_empty(&svm->ir_list)) return; - list_for_each_entry(ir, &svm->ir_list, node) - WARN_ON_ONCE(amd_iommu_update_ga(cpu, ir->data)); + list_for_each_entry(ir, &svm->ir_list, node) { + if (!toggle_avic) + WARN_ON_ONCE(amd_iommu_update_ga(cpu, ir->data)); + else if (cpu >= 0) + WARN_ON_ONCE(amd_iommu_activate_guest_mode(ir->data, cpu)); + else + WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(ir->data)); + } } -static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) +static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu, bool toggle_avic) { struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); int h_physical_id = kvm_cpu_get_apicid(cpu); @@ -883,7 +890,7 @@ static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); - avic_update_iommu_vcpu_affinity(vcpu, h_physical_id); + avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, toggle_avic); spin_unlock_irqrestore(&svm->ir_list_lock, flags); } @@ -900,10 +907,10 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) if (kvm_vcpu_is_blocking(vcpu)) return; - __avic_vcpu_load(vcpu, cpu); + __avic_vcpu_load(vcpu, cpu, false); } -static void __avic_vcpu_put(struct kvm_vcpu *vcpu) +static void __avic_vcpu_put(struct kvm_vcpu *vcpu, bool toggle_avic) { struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); struct vcpu_svm *svm = to_svm(vcpu); @@ -925,7 +932,7 @@ static void __avic_vcpu_put(struct kvm_vcpu *vcpu) */ spin_lock_irqsave(&svm->ir_list_lock, flags); - avic_update_iommu_vcpu_affinity(vcpu, -1); + avic_update_iommu_vcpu_affinity(vcpu, -1, toggle_avic); entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; svm->avic_physical_id_entry = entry; @@ -951,7 +958,7 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) return; - __avic_vcpu_put(vcpu); + __avic_vcpu_put(vcpu, false); } void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu) @@ -980,39 +987,15 @@ void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu) void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) { - bool activated = kvm_vcpu_apicv_active(vcpu); - int apic_id = kvm_cpu_get_apicid(vcpu->cpu); - struct vcpu_svm *svm = to_svm(vcpu); - struct amd_svm_iommu_ir *ir; - unsigned long flags; - if (!enable_apicv) return; avic_refresh_virtual_apic_mode(vcpu); - if (activated) - __avic_vcpu_load(vcpu, vcpu->cpu); + if (kvm_vcpu_apicv_active(vcpu)) + __avic_vcpu_load(vcpu, vcpu->cpu, true); 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(ir, &svm->ir_list, node) { - if (activated) - WARN_ON_ONCE(amd_iommu_activate_guest_mode(ir->data, apic_id)); - else - WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(ir->data)); - } -out: - spin_unlock_irqrestore(&svm->ir_list_lock, flags); + __avic_vcpu_put(vcpu, true); } void avic_vcpu_blocking(struct kvm_vcpu *vcpu) From patchwork Fri Apr 4 19:39:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038886 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 E8B74253353 for ; Fri, 4 Apr 2025 19:41:37 +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=1743795699; cv=none; b=NTMux6P552BkMr+mtX4smIKmhsgmavgETk6BLqDaQ4fGTgdFhY/NYgBNgAMR1GH/Qm5wl1r2IqitF/tbmnDq+O+SJ+Mk4++DgxFJtC0LhXCaKRptDzNYoAT5vyGxJJGMKThI5ie/WN6ZefXOzre8KW2c16PFcMANVM4gLN0sR1Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795699; c=relaxed/simple; bh=jY+2nzVN06DMGMLsSMn2WmOE/1Wygg/5G8qjzC8xCTw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=noncsV4FLEXlI25r2DwfHTcCXzbxG1i8tRN8kXRNqtXcSqjvXeMqRrf61TDH7Nd1S/bh4OSkPAWUdVvIKa3k89XRx24vX+jl7CzI/CBDOZLA8J320kpSwxVc6xIrcVyJKmhiXWB3OIxppoQJZeI4jKZ5uzninAvNnfEGAjvNSJw= 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=gf8dO0Qc; 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="gf8dO0Qc" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7398d70abbfso3441400b3a.2 for ; Fri, 04 Apr 2025 12:41:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795697; x=1744400497; 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=Ep0zYMwQk7PHwLo351NMM2Hs6HgJxECJlzek6kNKg+8=; b=gf8dO0QcbabDIm8CdLLVJ/g21O7pLuMHL3jYDo/T/IXq4GTTBSpzGyjSrGZ/f7m56Y zhZ6zZpHjB1d2ceacquYwdnk5GYj/sdF9tsXTvvR0rgJvPFlVqGlt6P1hZQr7/65so5c 5j25O+8Rzp87zVZII5JzszNK39/4a9QevU0PAZYQC3eH/57zhJkbB++Hh4qXv9sPaXkY GUK83xybTBBkZuGq5InbwdRk3yjZjE/gXF2hI+FJ4YBWHT0Sal2AofKWjT7ymjB0Rmm7 cB9aSXf7YCujzktvu3RRATO5Jl0auVA0YxIbxERDb2BUK4kGhJ7izxBirJryKX1/IIaK 4J8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795697; x=1744400497; 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=Ep0zYMwQk7PHwLo351NMM2Hs6HgJxECJlzek6kNKg+8=; b=gA4gbiibkoUfHDPnHC8Sqc8UCynkzm8qoB0upd3sGH6Q5FRtgu4Hd+uqpp6dGe2hoj DSl/N3BZvdYv9Ool0UKEM7b8IpKeq6nDq9CVxnz7ZrQRNiXDSu4Tkpaa4zo7I3baDErt 9Yi53V37e6dtpDpr+2nftthw3Tfta/l3mVxCp5s9Yc6uR+r1F4Oo6X3fWJGKqDOcxUFV s5F1Ap+Fcl4ai5t/CJ+j3OO1NVvWMxiXX+GCs89hLsXzQxLXX1rb8k1n6BnlgRSl1LIY zbRVJ/3hftswq9oinqbx2B7FZED+N4ibkK4IVWy6SP6xrqbnjBtuPyV8U8lkqH6H63hu 6ajQ== X-Gm-Message-State: AOJu0Yx5QGSfjCbTaiyP5BuFcQFyn7XoXe0sNlhXXbOVD/uLGYcb2poR AdSDtLtI2alYcGmAtjGs82nbBFYFcA6bJC/FnKeKHpU3/vIQj3dAsg6BNfNaz2cld8yH3cc+HQG iyg== X-Google-Smtp-Source: AGHT+IFmbGhp+Z2Q3iEhmqPOb4c8JbJPwoNpBaO41LBdBcOtaCAkgkkCsoxNppdx0bYM0erihZvIs9VkUcQ= X-Received: from pfbdf8.prod.google.com ([2002:a05:6a00:4708:b0:737:6b9f:8ab4]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:ccc:b0:736:6202:3530 with SMTP id d2e1a72fcca58-73b6b8f7617mr741609b3a.22.1743795697337; Fri, 04 Apr 2025 12:41:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:19 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-65-seanjc@google.com> Subject: [PATCH 64/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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(). 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 | 17 ++++++++++------- include/linux/amd-iommu.h | 9 ++++----- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/arch/x86/include/asm/irq_remapping.h b/arch/x86/include/asm/irq_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 c896f00f901c..1466e66cca6c 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -794,10 +794,12 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, * is awakened and/or scheduled in. See also avic_vcpu_load(). */ entry = 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 = entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; - else + } else { pi_data.cpu = -1; + pi_data.ga_log_intr = true; + } ret = irq_set_vcpu_affinity(host_irq, &pi_data); if (ret) @@ -837,9 +839,9 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, list_for_each_entry(ir, &svm->ir_list, node) { if (!toggle_avic) - WARN_ON_ONCE(amd_iommu_update_ga(cpu, ir->data)); + WARN_ON_ONCE(amd_iommu_update_ga(ir->data, cpu, true)); else if (cpu >= 0) - WARN_ON_ONCE(amd_iommu_activate_guest_mode(ir->data, cpu)); + WARN_ON_ONCE(amd_iommu_activate_guest_mode(ir->data, cpu, true)); else WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(ir->data)); } diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 2e016b98fa1b..27b03e718980 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3775,7 +3775,8 @@ static const struct irq_domain_ops amd_ir_domain_ops = { .deactivate = irq_remapping_deactivate, }; -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 >= 0) { entry->lo.fields_vapic.destination = @@ -3783,12 +3784,14 @@ static void __amd_iommu_update_ga(struct irte_ga *entry, int cpu) entry->hi.fields.destination = APICID_TO_IRTE_DEST_HI(cpu); entry->lo.fields_vapic.is_run = true; + entry->lo.fields_vapic.ga_log_intr = false; } else { entry->lo.fields_vapic.is_run = false; + entry->lo.fields_vapic.ga_log_intr = ga_log_intr; } } -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 = (struct amd_ir_data *)data; struct irte_ga *entry = (struct irte_ga *) ir_data->entry; @@ -3802,14 +3805,14 @@ int amd_iommu_update_ga(int cpu, void *data) if (!ir_data->iommu) return -ENODEV; - __amd_iommu_update_ga(entry, cpu); + __amd_iommu_update_ga(entry, cpu, ga_log_intr); 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, int cpu) +int amd_iommu_activate_guest_mode(void *data, int cpu, bool ga_log_intr) { struct amd_ir_data *ir_data = (struct amd_ir_data *)data; struct irte_ga *entry = (struct irte_ga *) ir_data->entry; @@ -3828,12 +3831,11 @@ int amd_iommu_activate_guest_mode(void *data, int cpu) entry->lo.fields_vapic.valid = valid; entry->lo.fields_vapic.guest_mode = 1; - entry->lo.fields_vapic.ga_log_intr = 1; entry->hi.fields.ga_root_ptr = ir_data->ga_root_ptr; entry->hi.fields.vector = ir_data->ga_vector; entry->lo.fields_vapic.ga_tag = ir_data->ga_tag; - __amd_iommu_update_ga(entry, cpu); + __amd_iommu_update_ga(entry, cpu, ga_log_intr); return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, ir_data->irq_2_irte.index, entry); @@ -3904,7 +3906,8 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *info) ir_data->ga_vector = pi_data->vector; ir_data->ga_tag = pi_data->ga_tag; if (pi_data->is_guest_mode) - ret = amd_iommu_activate_guest_mode(ir_data, pi_data->cpu); + ret = amd_iommu_activate_guest_mode(ir_data, pi_data->cpu, + pi_data->ga_log_intr); else ret = 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)); -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); #else /* defined(CONFIG_AMD_IOMMU) && defined(CONFIG_IRQ_REMAP) */ @@ -43,12 +42,12 @@ amd_iommu_register_ga_log_notifier(int (*notifier)(u32)) return 0; } -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_intr) { return 0; } -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; } From patchwork Fri Apr 4 19:39:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038887 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 D2A6D253B5F for ; Fri, 4 Apr 2025 19:41:39 +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=1743795701; cv=none; b=RwBiUVh9KLdjBUaKqPu3bBT+bcDpqYIpcjBwcC9Sdzb1vP8pNDfkfp7TzJiSQq38U/9TpGe2JFfwDY5Mmx+b89QDYN2t+OWSG2sYBFPizY0ZlCiB+fPOkE75QxZyzgNSEwcaVJOzKTTLehWNSHJpvSUDsR4Vkrc5VI5KWpqoUsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795701; c=relaxed/simple; bh=Sb4bsa6vo9IYveps+nL7XukC6432K1ulPMRWWP6K8jc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=G2N1M1AiF2wqFpwBgCuP7INkesOrky2gXym8sSGi7WJreYpKHVdvFaovq2HEhWoVs21ALpyo6Kkj9qwlVbdn/ZCxmZCsWg4Ggw26rtYgYYjKzSe8ZOaRa/o/asn6bF+vU7qE16Jo2U8NNvgQoZifjAoRTB7qXnHd7yeu5696HGo= 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=k4iZ8UZf; 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="k4iZ8UZf" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-73691c75863so2948465b3a.0 for ; Fri, 04 Apr 2025 12:41:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795699; x=1744400499; 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=xa2Ve4DPvmz0R4op/e/h5NsuopJcYdvF3WY5dpywXT4=; b=k4iZ8UZfVj3zLvO1UaZK2JSqoK2ZWIpmUKwEWOVKD2npRm0W44t066vIIJjr9/sbjX XRoJxyLH5l9P5fT+gMWsYgvnM/a2LAiRhvs8GkGtt3DTCTC+C5+/Sp3bU4tbJLeKmbqk BsjUApDCDjD872P6jmlBobuMWgI6PQzHnjn92tN96Xryg8AkxtdsZAwnUgGSwvpLR+C2 QwusmiWAxjSH6hDHSjCWkN8Zk1J9xBQBbacGnbxs30rF9mb9TcsTgmQ6gI7MdScuvlbj R0O5IZWdPrGr9PDA6gV2LxEslJIr3JS3R1EkwtgHxnVzW9GB4yeJLVQioU5LbDCm8zlY R45g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795699; x=1744400499; 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=xa2Ve4DPvmz0R4op/e/h5NsuopJcYdvF3WY5dpywXT4=; b=M4fvnP4aBw8pLdB6CKu5d9alJknjB55F7Ar5QSdeCeN1XOf1UUfRwKOhsnolrOSsGe Jqi0DTmXyZXKVF0MeAVdr6q5L5lztZkH8XIEtcLyCSik+83ve/KZ04iePRWKpiarR2OW w8+SW33TfHc7HPxI2V7xJKZbtwptoBfOKX+rID9WOh9HO1sDGK4T10yOQnBeXiHQPNix uixSwZjZ78bNHbmRoDRZ0dVBqv6ivd28wLoUXJ+yCrOcVvkMxexKYtMlcdSJkEVZumml qdbUCrqMLUG1dSLzJ1xriKd3UrTW0mcoyCyxU7NxIcyZJpzej2wn1i3ACWVUi/k5hpNi yfwQ== X-Gm-Message-State: AOJu0YyZu2RfZjJflHvnVVSqdD0wWsRNDFHAef4of38aHJ9D+5fponGY icnP9VEtdIozZp1vPqFsWEKXxj9IjNFPKoNB4pRqKlZdXsb5N+7eI9ioprqgDtIJpAo7lW10yKu ldA== X-Google-Smtp-Source: AGHT+IGYu/yYBkSf+6Tq2bLTJpJywO70pYjALzEs/kBx6J6iNAtgjLKV0RF85EP9hys6qFZmYCGLhDM/+UY= X-Received: from pfbho2.prod.google.com ([2002:a05:6a00:8802:b0:736:b315:f15e]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:10ce:b0:736:5b85:a911 with SMTP id d2e1a72fcca58-739e6ff6b96mr5876430b3a.8.1743795699069; Fri, 04 Apr 2025 12:41:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:20 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-66-seanjc@google.com> Subject: [PATCH 65/67] 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, Maxim Levitsky , Joao Martins , David Matlack 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 | 49 +++++++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 8b07939ef3b9..be6e833bf92c 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -246,6 +246,13 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define AVIC_LOGICAL_ID_ENTRY_VALID_BIT 31 #define AVIC_LOGICAL_ID_ENTRY_VALID_MASK (1 << 31) +/* + * GA_LOG_INTR is a synthetic flag that's never propagated to hardware-visible + * tables. GA_LOG_INTR is set if the vCPU needs device posted IRQs to generate + * GA log interrupts to wake the vCPU (because it's blocking or about to block). + */ +#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 1466e66cca6c..0d2a17a74be6 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -798,7 +798,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, pi_data.cpu = entry & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; } else { pi_data.cpu = -1; - pi_data.ga_log_intr = true; + pi_data.ga_log_intr = entry & AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR; } ret = irq_set_vcpu_affinity(host_irq, &pi_data); @@ -823,7 +823,7 @@ int avic_pi_update_irte(struct kvm_kernel_irqfd *irqfd, struct kvm *kvm, } static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, - bool toggle_avic) + bool toggle_avic, bool ga_log_intr) { struct amd_svm_iommu_ir *ir; struct vcpu_svm *svm = to_svm(vcpu); @@ -839,9 +839,9 @@ static void avic_update_iommu_vcpu_affinity(struct kvm_vcpu *vcpu, int cpu, list_for_each_entry(ir, &svm->ir_list, node) { if (!toggle_avic) - WARN_ON_ONCE(amd_iommu_update_ga(ir->data, cpu, true)); + WARN_ON_ONCE(amd_iommu_update_ga(ir->data, cpu, ga_log_intr)); else if (cpu >= 0) - WARN_ON_ONCE(amd_iommu_activate_guest_mode(ir->data, cpu, true)); + WARN_ON_ONCE(amd_iommu_activate_guest_mode(ir->data, cpu, ga_log_intr)); else WARN_ON_ONCE(amd_iommu_deactivate_guest_mode(ir->data)); } @@ -875,7 +875,8 @@ static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu, bool toggle_avic) entry = svm->avic_physical_id_entry; WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK); - entry &= ~AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK; + entry &= ~(AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK | + AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR); entry |= (h_physical_id & AVIC_PHYSICAL_ID_ENTRY_HOST_PHYSICAL_ID_MASK); entry |= AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; @@ -892,7 +893,7 @@ static void __avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu, bool toggle_avic) WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); - avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, toggle_avic); + avic_update_iommu_vcpu_affinity(vcpu, h_physical_id, toggle_avic, false); spin_unlock_irqrestore(&svm->ir_list_lock, flags); } @@ -912,7 +913,8 @@ void avic_vcpu_load(struct kvm_vcpu *vcpu, int cpu) __avic_vcpu_load(vcpu, cpu, false); } -static void __avic_vcpu_put(struct kvm_vcpu *vcpu, bool toggle_avic) +static void __avic_vcpu_put(struct kvm_vcpu *vcpu, bool toggle_avic, + bool is_blocking) { struct kvm_svm *kvm_svm = to_kvm_svm(vcpu->kvm); struct vcpu_svm *svm = to_svm(vcpu); @@ -934,14 +936,28 @@ static void __avic_vcpu_put(struct kvm_vcpu *vcpu, bool toggle_avic) */ spin_lock_irqsave(&svm->ir_list_lock, flags); - avic_update_iommu_vcpu_affinity(vcpu, -1, toggle_avic); + avic_update_iommu_vcpu_affinity(vcpu, -1, toggle_avic, is_blocking); + WARN_ON_ONCE(entry & AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR); + + /* + * Keep the previouv APIC ID in the entry so that a rogue doorbell from + * hardware is at least restricted to a CPU associated with the vCPU. + */ entry &= ~AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK; - svm->avic_physical_id_entry = entry; if (enable_ipiv) WRITE_ONCE(kvm_svm->avic_physical_id_table[vcpu->vcpu_id], entry); + /* + * Note! Don't set AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR in the table as + * it's a synthetic flag that usurps an unused a should-be-zero bit. + */ + if (is_blocking) + entry |= AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR; + + svm->avic_physical_id_entry = entry; + spin_unlock_irqrestore(&svm->ir_list_lock, flags); } @@ -957,10 +973,15 @@ void avic_vcpu_put(struct kvm_vcpu *vcpu) u64 entry = to_svm(vcpu)->avic_physical_id_entry; /* Nothing to do if IsRunning == '0' due to vCPU blocking. */ - if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) - return; + if (!(entry & AVIC_PHYSICAL_ID_ENTRY_IS_RUNNING_MASK)) { + if (WARN_ON_ONCE(!kvm_vcpu_is_blocking(vcpu))) + return; - __avic_vcpu_put(vcpu, false); + if (!(WARN_ON_ONCE(!(entry & AVIC_PHYSICAL_ID_ENTRY_GA_LOG_INTR)))) + return; + } + + __avic_vcpu_put(vcpu, false, kvm_vcpu_is_blocking(vcpu)); } void avic_refresh_virtual_apic_mode(struct kvm_vcpu *vcpu) @@ -997,7 +1018,7 @@ void avic_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) if (kvm_vcpu_apicv_active(vcpu)) __avic_vcpu_load(vcpu, vcpu->cpu, true); else - __avic_vcpu_put(vcpu, true); + __avic_vcpu_put(vcpu, true, true); } void avic_vcpu_blocking(struct kvm_vcpu *vcpu) @@ -1023,7 +1044,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, false, true); } void avic_vcpu_unblocking(struct kvm_vcpu *vcpu) From patchwork Fri Apr 4 19:39:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038888 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 67D04253F16 for ; Fri, 4 Apr 2025 19:41: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=1743795703; cv=none; b=QsUW7Qy7YJWUhrgE6MKuJgf5suWS7TWwehS0Bai2lhbDc9ec2NYvrJt29QqBdt2GfBRup8Wyz+Ja8iRTjhY7ZVMhq1+0s4xSrg8X3fcBihDyiJ+LZb8XE4pALGG3h2wV1pVxH0KGjIkkRSAO1Ca9DvwXGB8LzWIoQtbg/wutCEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795703; c=relaxed/simple; bh=p4lMpZzNOfopLHtzNwpOCYJrrKVo7rhURos1txCwlVo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uo82yxeqSbOyOxhT52p5ayxmtAzwBaPWNlgC7fkTDmCTQh4AHkAuCa5rxA7Sn0ahfgXkVLOol2l7PD+5NHYuH3lXZVC+Or1sEXI9bWPVsNpUCSIWs7hnKApxMysrUhq7gdnopJ6PI1Lo365XPLjWqukF7uk85BoJ9hMBCMGIUEA= 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=EVHorfie; 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="EVHorfie" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-af5310c1ac1so1592112a12.2 for ; Fri, 04 Apr 2025 12:41:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795701; x=1744400501; 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=QUrU9L/DFULymetyem/EQ8okUR5K5rpIbjia6VAlcZA=; b=EVHorfie+lW0ARLOWD8X8MaWeXkHbsKCUv+wMdki7Z9lM53/OIo1/3cIHGiXa2+Jrp oRH/QcuptfOCpim7i8DDJSf0m5hGypys6MhGBeEfGeIWuNaJ1S8BT11Ls6c9ZyUzA8ur eYkj2e64owHRLLROPUwoyzg8KCNcIxHjz+5lvjV1i9tVDW/dQMzTnI9esD5DlJzq3S9v sSxPmPp6gUuAnAFJXhhxg5zNrk9vt5oWttmnRhkfg1OOwnCkRLJXgH6HfiYxbznX6iAd s1mlrEkWPVYFdQTftlouKTswiqp3LXhZr69JiDkZATlxCRh4zItzzIQEUC+LA3p8udrU P2mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795701; x=1744400501; 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=QUrU9L/DFULymetyem/EQ8okUR5K5rpIbjia6VAlcZA=; b=HksvfSGVnbuxy6DGUFXn4hfOKxli6pfYJAZNsw5rTvUG3BkgsBsyb4eKjPiTNkqqKc mvbZFRt+dhLfxDikvwyH9PJaHTeQimeEWGLfdw9aDAxLpRozXsSeBs0YVvZxV9MipNl/ frGZJHr239rmYDXeqiZ/a3H4gDlao9lWydU9xq4xUWPH3sOFU/f39aTr4Bnsg/Lki7DW 3IdjvJd2RQa9IiwY3/bGuA6bhSfd5+Xk5OWOAuYbyJykYC0xv0f7K/aL+gngFF+PNHz9 MW+k5+86K1HXLKdowiCvA05tVL2tqM8094nZp2ytBjp8cKNWhfUBe/liD7UOeAaWfH1a K2+g== X-Gm-Message-State: AOJu0YwvG3jAkHgV+wfF8CCVzitW8GdjdSWEY9wyWSnrLtHixHADP9Wr /TqCwm8Tfxpr6K1OPy/en9CVEtzbjkoIc7I6yfWRtMDH+ERlAn0EbIQZ9xFN8aM60NPDcMvB72q GVw== X-Google-Smtp-Source: AGHT+IH5SF1xKDOV0AlfkEHS8vEc8UnE03fOtnqwKFwUtSTDcFy4r+xX9b4aP/SLO3FeSmcsDhP1YeFdzC0= X-Received: from pfoi21.prod.google.com ([2002:aa7:87d5:0:b0:732:6c92:3f75]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:ce43:b0:1f3:41d5:6608 with SMTP id adf61e73a8af0-2010472d8b1mr6203969637.26.1743795700728; Fri, 04 Apr 2025 12:41:40 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:21 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-67-seanjc@google.com> Subject: [PATCH 66/67] *** DO NOT MERGE *** iommu/amd: Hack to fake IRQ posting 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, Maxim Levitsky , Joao Martins , David Matlack Hack the IOMMU half of AMD device posted IRQ support to allow testing a decent chunk of the related code on systems with AVIC capable CPUs, but no IOMMU virtual APIC support. E.g. some Milan CPUs allow enabling AVIC even though it's not advertised as being supported, but the IOMMU unfortunately doesn't allow the same shenanigans. Not-signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/avic.c | 76 ++++++++++++++++++++++++++++++++++++--- arch/x86/kvm/svm/svm.c | 2 ++ drivers/iommu/amd/init.c | 8 +++-- drivers/iommu/amd/iommu.c | 50 +++++++++++++++++++++++++- 4 files changed, 128 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/svm/avic.c b/arch/x86/kvm/svm/avic.c index 0d2a17a74be6..425674e1a04c 100644 --- a/arch/x86/kvm/svm/avic.c +++ b/arch/x86/kvm/svm/avic.c @@ -28,6 +28,8 @@ #include "irq.h" #include "svm.h" +#include "../../../drivers/iommu/amd/amd_iommu_types.h" + /* * 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 can't @@ -141,11 +143,7 @@ static void avic_deactivate_vmcb(struct vcpu_svm *svm) svm_set_x2apic_msr_interception(svm, true); } -/* Note: - * This function is called from IOMMU driver to notify - * SVM to schedule in a particular vCPU of a particular VM. - */ -int avic_ga_log_notifier(u32 ga_tag) +static struct kvm_vcpu *avic_ga_log_get_vcpu(u32 ga_tag) { unsigned long flags; struct kvm_svm *kvm_svm; @@ -165,6 +163,17 @@ int avic_ga_log_notifier(u32 ga_tag) } spin_unlock_irqrestore(&svm_vm_data_hash_lock, flags); + return vcpu; +} + +/* Note: + * This function is called from IOMMU driver to notify + * SVM to schedule in a particular vCPU of a particular VM. + */ +int avic_ga_log_notifier(u32 ga_tag) +{ + struct kvm_vcpu *vcpu = avic_ga_log_get_vcpu(ga_tag); + /* Note: * At this point, the IOMMU should have already set the pending * bit in the vAPIC backing page. So, we just need to schedule @@ -750,6 +759,8 @@ static void svm_ir_list_del(struct kvm_kernel_irqfd *irqfd) spin_unlock_irqrestore(&to_svm(vcpu)->ir_list_lock, flags); } +extern struct amd_iommu_pi_data amd_iommu_fake_irte; + 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, @@ -1055,6 +1066,58 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu) avic_vcpu_load(vcpu, vcpu->cpu); } +static void avic_pi_handler(void) +{ + struct amd_iommu_pi_data pi; + struct kvm_vcpu *vcpu; + + memcpy(&pi, &amd_iommu_fake_irte, sizeof(pi)); + + if (!pi.is_guest_mode) { + pr_warn("IRQ %u arrived with !is_guest_mode\n", pi.vector); + return; + } + + vcpu = avic_ga_log_get_vcpu(pi.ga_tag); + if (!vcpu) { + pr_warn("No vCPU for IRQ %u\n", pi.vector); + return; + } + WARN_ON_ONCE(pi.vapic_addr << 12 != avic_get_backing_page_address(to_svm(vcpu))); + + /* + * When updating a vCPU's IRTE, the fake posted IRQ can race with the + * IRTE update. Take ir_list_lock so that the IRQ can be processed + * atomically. In real hardware, the IOMMU will complete IRQ delivery + * before accepting the new IRTE. + */ + guard(spinlock_irqsave)(&to_svm(vcpu)->ir_list_lock); + + if (amd_iommu_fake_irte.ga_tag != pi.ga_tag) { + WARN_ON_ONCE(amd_iommu_fake_irte.is_guest_mode); + return; + } + + memcpy(&pi, &amd_iommu_fake_irte, sizeof(pi)); + +#if 0 + pr_warn("In PI handler, guest = %u, cpu = %d, tag = %x, intr = %u, vector = %u\n", + pi.is_guest_mode, pi.cpu, + pi.ga_tag, pi.ga_log_intr, pi.vector); +#endif + + if (!pi.is_guest_mode) + return; + + kvm_lapic_set_irr(pi.vector, vcpu->arch.apic); + smp_mb__after_atomic(); + + if (pi.cpu >= 0) + avic_ring_doorbell(vcpu); + else if (pi.ga_log_intr) + avic_ga_log_notifier(pi.ga_tag); +} + /* * Note: * - The module param avic enable both xAPIC and x2APIC mode. @@ -1107,5 +1170,8 @@ bool avic_hardware_setup(void) amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier); + pr_warn("Register AVIC PI wakeup handler\n"); + kvm_set_posted_intr_wakeup_handler(avic_pi_handler); + return true; } diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 71b52ad13577..b8adeb87e800 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1122,6 +1122,8 @@ static void svm_hardware_unsetup(void) { int cpu; + kvm_set_posted_intr_wakeup_handler(NULL); + sev_hardware_unsetup(); for_each_possible_cpu(cpu) diff --git a/drivers/iommu/amd/init.c b/drivers/iommu/amd/init.c index cb536d372b12..28cc8552ca95 100644 --- a/drivers/iommu/amd/init.c +++ b/drivers/iommu/amd/init.c @@ -2863,8 +2863,12 @@ static void enable_iommus_vapic(void) return; } - if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) && - !check_feature(FEATURE_GAM_VAPIC)) { + if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir)) + return; + + if (!check_feature(FEATURE_GAM_VAPIC)) { + pr_warn("IOMMU lacks GAM_VAPIC, fudging IRQ posting\n"); + amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP); amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA; return; } diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 27b03e718980..f2bd262330fa 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -3775,6 +3775,15 @@ static const struct irq_domain_ops amd_ir_domain_ops = { .deactivate = irq_remapping_deactivate, }; +struct amd_iommu_pi_data amd_iommu_fake_irte; +EXPORT_SYMBOL_GPL(amd_iommu_fake_irte); + +static bool amd_iommu_fudge_pi(void) +{ + return irq_remapping_cap(IRQ_POSTING_CAP) && + !AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir); +} + static void __amd_iommu_update_ga(struct irte_ga *entry, int cpu, bool ga_log_intr) { @@ -3796,6 +3805,12 @@ int amd_iommu_update_ga(void *data, int cpu, bool ga_log_intr) struct amd_ir_data *ir_data = (struct amd_ir_data *)data; struct irte_ga *entry = (struct irte_ga *) ir_data->entry; + if (amd_iommu_fudge_pi()) { + amd_iommu_fake_irte.cpu = cpu; + amd_iommu_fake_irte.ga_log_intr = ga_log_intr; + return 0; + } + if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) return -EINVAL; @@ -3818,6 +3833,26 @@ int amd_iommu_activate_guest_mode(void *data, int cpu, bool ga_log_intr) struct irte_ga *entry = (struct irte_ga *) ir_data->entry; u64 valid; + if (amd_iommu_fudge_pi()) { + if (WARN_ON_ONCE(!entry->lo.fields_remap.valid)) + return -EINVAL; + + if (WARN_ON_ONCE(entry->lo.fields_remap.int_type != APIC_DELIVERY_MODE_FIXED)) + return -EINVAL; + + amd_iommu_fake_irte.cpu = cpu; + amd_iommu_fake_irte.vapic_addr = ir_data->ga_root_ptr; + amd_iommu_fake_irte.vector = ir_data->ga_vector; + amd_iommu_fake_irte.ga_tag = ir_data->ga_tag; + amd_iommu_fake_irte.ga_log_intr = ga_log_intr; + amd_iommu_fake_irte.is_guest_mode = true; + + entry->hi.fields.vector = POSTED_INTR_WAKEUP_VECTOR; + + return modify_irte_ga(ir_data->iommu, ir_data->irq_2_irte.devid, + ir_data->irq_2_irte.index, entry); + } + if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) return -EINVAL; @@ -3849,12 +3884,18 @@ int amd_iommu_deactivate_guest_mode(void *data) struct irq_cfg *cfg = ir_data->cfg; u64 valid; + if (amd_iommu_fudge_pi() && entry) { + memset(&amd_iommu_fake_irte, 0, sizeof(amd_iommu_fake_irte)); + goto fudge; + } + 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; +fudge: valid = entry->lo.fields_remap.valid; entry->lo.val = 0; @@ -3891,12 +3932,19 @@ static int amd_ir_set_vcpu_affinity(struct irq_data *data, void *info) * This device has never been set up for guest mode. * we should not modify the IRTE */ - if (!dev_data || !dev_data->use_vapic) + if (!dev_data) + return -EINVAL; + + if (amd_iommu_fudge_pi()) + goto fudge; + + if (!dev_data->use_vapic) return -EINVAL; if (WARN_ON_ONCE(!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))) return -EINVAL; +fudge: ir_data->cfg = irqd_cfg(data); if (pi_data) { From patchwork Fri Apr 4 19:39:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 14038889 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 30DC4254840 for ; Fri, 4 Apr 2025 19:41: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=1743795705; cv=none; b=TByzm/JfvHjFjlNhdfY12JsMOmtd273dYOPsLi/lxuNAiSajiKbMztsmrEQV89A63XFSsbhkkMX1UJjqNg1I//hvhJ+OwaTwsxh51UrfA4fMrfUJEi3ebkuNGHJMbP5E4e8JgSOLyZxflwUd976wio3ctWTrdoINfdxwiiLzWmY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743795705; c=relaxed/simple; bh=GrR7H4RmB+od4casAPRpt3gnDwjfh8mPMr/HODGBiFA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IKxH6A0OkoCaUbUWfmYS9SxWqoo1u1yqqw1gI1aKa6i/kpmJRYGNhZGZx/21qg6EvqCIj9rZkFrWR3pYomsVFsIzoR8e+WBADjORG2e7UgixGO4AxOcIdEqfMQQ0YT1vRMb14Sv6HjjEaBD5Wlv1Przu9JzyNdweCfT8uXQoKC8= 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=NZ/p00yx; 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="NZ/p00yx" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-af972dd0cd6so1649456a12.1 for ; Fri, 04 Apr 2025 12:41:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743795702; x=1744400502; 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=CchHgbdkU85V21Tg2RoLnh3SW6H2rHKoJBvsnARZ2sw=; b=NZ/p00yximP2RpkXOCAC93JLw8XHynM6zCuge8onffUZ6BYSd+Eic9x6ZHoBApf2Eb nWRHDp9KtH98VMBX4hgWbBljYinsmGkHadvPS2LmBHWLZMgIJcL6c88x94Myz5r1t4hW bBp4nQ4hkwaGL2JhQQjbRFY3W10leMlh33Ho1eH+7CoiG/VtJvWvcqBB+9eiYyG2/PqE BBc8ME9M+H9mETooYHyo/rdXWgODLSZZFsYK+1DMx5wz5dLPyo5ZRTz6hENL1uIjmhR/ 4iCk/If20Ya6xAkhMLozrzVjs9X/hdtF9mo1Pomvnndotbv8M+LxY39Xv/o8vBipCXxO tE+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743795702; x=1744400502; 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=CchHgbdkU85V21Tg2RoLnh3SW6H2rHKoJBvsnARZ2sw=; b=Od+xyABa4K/XKjvKj5zyqIS0OMH/27Ohu4k3jhNdDeSOvUaAgFOv1uSezAmWNofS4y rgZ1jNUTVk9ibwNI/6JO44tzydpvJdE828/Nj/QUaUj0Wmcn3Qb3y2JguHVB/n/WpvwB ADcqDjIAyfom8dlhm++4FdmXSYVLfBkcyw7LH4hET+a3d/ZdV4KV147JB5TnGqtFbNuC ryeHn+jyl8PS+LofWgmXH8BVGEnfuXn7kgze7j0cVqrcgzpLb2sCADGZepfljqtPaK4O UMqEW0zzJtSZxjaqIg3o9aggLk2gdw0lNrfgVldMUSKZLze3d96OFE+i4GKcF2YIsFDj fPMA== X-Gm-Message-State: AOJu0YxqyrxQnOvVF0msJaHbt3bP3L0+Nt4U/Hk/XDWlAxvWPsAYeF+y j7PXI6jysW5+LFSrFkRSm/xLkLfXSmiMGWdQdfJOOGLNHCBjtL/A+OHZMq/8IQ40nCfqJyuqtnk x1Q== X-Google-Smtp-Source: AGHT+IESNzAoFEjQQTPfehzk55GF3aFqjfkUVb4xQqmA4zoG2du8f0bXh5+kaR4evFBRnH7os/w/UOsfz4E= X-Received: from pgct21.prod.google.com ([2002:a05:6a02:5295:b0:af2:3b16:9767]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:12d4:b0:1f5:8605:9530 with SMTP id adf61e73a8af0-2010818982emr5025411637.28.1743795702576; Fri, 04 Apr 2025 12:41:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 4 Apr 2025 12:39:22 -0700 In-Reply-To: <20250404193923.1413163-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250404193923.1413163-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250404193923.1413163-68-seanjc@google.com> Subject: [PATCH 67/67] *** DO NOT MERGE *** KVM: selftests: WIP posted interrupts test 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, Maxim Levitsky , Joao Martins , David Matlack Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/Makefile.kvm | 2 + .../selftests/kvm/include/vfio_pci_util.h | 149 ++++++ .../selftests/kvm/include/x86/processor.h | 21 + .../testing/selftests/kvm/lib/vfio_pci_util.c | 201 ++++++++ tools/testing/selftests/kvm/mercury_device.h | 118 +++++ tools/testing/selftests/kvm/vfio_irq_test.c | 429 ++++++++++++++++++ 6 files changed, 920 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/vfio_pci_util.h create mode 100644 tools/testing/selftests/kvm/lib/vfio_pci_util.c create mode 100644 tools/testing/selftests/kvm/mercury_device.h create mode 100644 tools/testing/selftests/kvm/vfio_irq_test.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm index f773f8f99249..8f017b858d4b 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -15,6 +15,7 @@ LIBKVM += lib/sparsebit.c LIBKVM += lib/test_util.c LIBKVM += lib/ucall_common.c LIBKVM += lib/userfaultfd_util.c +LIBKVM += lib/vfio_pci_util.c LIBKVM_STRING += lib/string_override.c @@ -133,6 +134,7 @@ TEST_GEN_PROGS_x86 += mmu_stress_test TEST_GEN_PROGS_x86 += rseq_test TEST_GEN_PROGS_x86 += set_memory_region_test TEST_GEN_PROGS_x86 += steal_time +TEST_GEN_PROGS_x86 += vfio_irq_test TEST_GEN_PROGS_x86 += kvm_binary_stats_test TEST_GEN_PROGS_x86 += system_counter_offset_test TEST_GEN_PROGS_x86 += pre_fault_memory_test diff --git a/tools/testing/selftests/kvm/include/vfio_pci_util.h b/tools/testing/selftests/kvm/include/vfio_pci_util.h new file mode 100644 index 000000000000..2a697dcb741e --- /dev/null +++ b/tools/testing/selftests/kvm/include/vfio_pci_util.h @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2022, Google LLC. + */ + +#ifndef SELFTEST_KVM_VFIO_UTIL_H +#define SELFTEST_KVM_VFIO_UTIL_H + +#include +#include + +#include "kvm_util.h" +#include "test_util.h" + +struct vfio_pci_dev { + int fd; + int group_fd; + int container_fd; +}; + +struct vfio_pci_dev *__vfio_pci_init(const char *bdf, unsigned long iommu_type); +void vfio_pci_free(struct vfio_pci_dev *dev); + +static inline struct vfio_pci_dev *vfio_pci_init(const char *bdf) +{ + return __vfio_pci_init(bdf, VFIO_TYPE1v2_IOMMU); +} + +#define __vfio_ioctl(vfio_fd, cmd, arg) \ +({ \ + __kvm_ioctl(vfio_fd, cmd, arg); \ +}) + +#define vfio_ioctl(vfio_fd, cmd, arg) \ +({ \ + int ret = __vfio_ioctl(vfio_fd, cmd, arg); \ + \ + TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ +}) + +static inline uint32_t vfio_pci_get_nr_irqs(struct vfio_pci_dev *dev, + uint32_t irq_type) +{ + struct vfio_irq_info irq_info = { + .argsz = sizeof(struct vfio_irq_info), + .index = irq_type, + }; + + vfio_ioctl(dev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info); + + TEST_ASSERT(irq_info.flags & VFIO_IRQ_INFO_EVENTFD, + "eventfd signalling unsupported by IRQ type '%u'", irq_type); + return irq_info.count; +} + +static inline uint32_t vfio_pci_get_nr_msi_irqs(struct vfio_pci_dev *dev) +{ + return vfio_pci_get_nr_irqs(dev, VFIO_PCI_MSI_IRQ_INDEX); +} + +static inline uint32_t vfio_pci_get_nr_msix_irqs(struct vfio_pci_dev *dev) +{ + return vfio_pci_get_nr_irqs(dev, VFIO_PCI_MSIX_IRQ_INDEX); +} + +static inline void __vfio_pci_irq_eventfd(struct vfio_pci_dev *dev, int eventfd, + uint32_t irq_type, uint32_t set) +{ + struct { + struct vfio_irq_set vfio; + uint32_t eventfd; + } buffer = {}; + + memset(&buffer, 0, sizeof(buffer)); + buffer.vfio.argsz = sizeof(buffer); + buffer.vfio.flags = set | VFIO_IRQ_SET_ACTION_TRIGGER; + buffer.vfio.index = irq_type; + buffer.vfio.count = 1; + buffer.eventfd = eventfd; + + vfio_ioctl(dev->fd, VFIO_DEVICE_SET_IRQS, &buffer.vfio); +} + +static inline void vfio_pci_assign_irq_eventfd(struct vfio_pci_dev *dev, + int eventfd, uint32_t irq_type) +{ + __vfio_pci_irq_eventfd(dev, eventfd, irq_type, VFIO_IRQ_SET_DATA_EVENTFD); +} + +static inline void vfio_pci_assign_msix(struct vfio_pci_dev *dev, int eventfd) +{ + vfio_pci_assign_irq_eventfd(dev, eventfd, VFIO_PCI_MSIX_IRQ_INDEX); +} + +static inline void vfio_pci_release_irq_eventfds(struct vfio_pci_dev *dev, + uint32_t irq_type) +{ + struct vfio_irq_set vfio = { + .argsz = sizeof(struct vfio_irq_set), + .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER, + .index = irq_type, + .count = 0, + }; + + vfio_ioctl(dev->fd, VFIO_DEVICE_SET_IRQS, &vfio); +} + +static inline void vfio_pci_release_msix(struct vfio_pci_dev *dev) +{ + vfio_pci_release_irq_eventfds(dev, VFIO_PCI_MSIX_IRQ_INDEX); +} + +static inline void vfio_pci_send_irq_eventfd(struct vfio_pci_dev *dev, + int eventfd, uint32_t irq_type) +{ + __vfio_pci_irq_eventfd(dev, eventfd, irq_type, VFIO_IRQ_SET_DATA_NONE); +} + +static inline void vfio_pci_send_msix(struct vfio_pci_dev *dev, int eventfd) +{ + vfio_pci_send_irq_eventfd(dev, eventfd, VFIO_PCI_MSIX_IRQ_INDEX); +} + +void *vfio_pci_map_bar(struct vfio_pci_dev *dev, unsigned int bar_idx, + uint64_t *size); + +void vfio_pci_read_config_data(struct vfio_pci_dev *dev, size_t offset, + size_t size, void *data); + +static inline uint16_t vfio_pci_config_read_u16(struct vfio_pci_dev *dev, + size_t offset) +{ + uint16_t val; + + vfio_pci_read_config_data(dev, offset, sizeof(val), &val); + return le16toh(val); +} + +static inline uint16_t vfio_pci_get_vendor_id(struct vfio_pci_dev *dev) +{ + return vfio_pci_config_read_u16(dev, PCI_VENDOR_ID); +} + +static inline uint16_t vfio_pci_get_device_id(struct vfio_pci_dev *dev) +{ + return vfio_pci_config_read_u16(dev, PCI_DEVICE_ID); +} + +#endif /* SELFTEST_KVM_VFIO_UTIL_H */ diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h index 32ab6ca7ec32..251dcc074503 100644 --- a/tools/testing/selftests/kvm/include/x86/processor.h +++ b/tools/testing/selftests/kvm/include/x86/processor.h @@ -19,6 +19,27 @@ #include "kvm_util.h" #include "ucall_common.h" + +static inline void writel(uint32_t val, volatile void *addr) +{ + *(volatile uint32_t *)addr = val; +} + +static inline uint32_t readl(volatile void *addr) +{ + return *(volatile uint32_t *)addr; +} + +static inline void writeq(uint64_t val, volatile void *addr) +{ + *(volatile uint64_t *)addr = val; +} + +static inline uint64_t readq(volatile void *addr) +{ + return *(volatile uint64_t *)addr; +} + extern bool host_cpu_is_intel; extern bool host_cpu_is_amd; extern uint64_t guest_tsc_khz; diff --git a/tools/testing/selftests/kvm/lib/vfio_pci_util.c b/tools/testing/selftests/kvm/lib/vfio_pci_util.c new file mode 100644 index 000000000000..878d91be2212 --- /dev/null +++ b/tools/testing/selftests/kvm/lib/vfio_pci_util.c @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "test_util.h" +#include "kvm_util.h" +#include "vfio_pci_util.h" + +#define VFIO_DEV_PATH "/dev/vfio/vfio" +#define PCI_SYSFS_PATH "/sys/bus/pci/devices/" + +void *vfio_pci_map_bar(struct vfio_pci_dev *dev, unsigned int bar_idx, + uint64_t *size) +{ + struct vfio_region_info info = { + .argsz = sizeof(struct vfio_region_info), + .index = bar_idx, + }; + int fd = dev->fd; + void *bar; + int prot; + + TEST_ASSERT(bar_idx <= VFIO_PCI_BAR5_REGION_INDEX, + "Invalid BAR index: %d", bar_idx); + + /* Currently only support the cases where the BAR can be mmap-ed */ + vfio_ioctl(fd, VFIO_DEVICE_GET_REGION_INFO, &info); + TEST_ASSERT(info.flags & VFIO_REGION_INFO_FLAG_MMAP, + "BAR%d doesn't support mmap", bar_idx); + + TEST_ASSERT(info.flags & VFIO_REGION_INFO_FLAG_READ, + "BAR%d doesn't support read?", bar_idx); + + prot = PROT_READ; + if (info.flags & VFIO_REGION_INFO_FLAG_WRITE) + prot |= PROT_WRITE; + + bar = mmap(NULL, info.size, prot, MAP_FILE | MAP_SHARED, fd, info.offset); + TEST_ASSERT(bar != MAP_FAILED, "mmap(BAR%d) failed", bar_idx); + + *size = info.size; + return bar; +} + +/* + * Read the PCI config space data + * + * Input Args: + * vfio_pci: Pointer to struct vfio_pci_dev + * config: The config space field's offset to read from (eg: PCI_VENDOR_ID) + * size: The size to read from the config region (could be one or more fields). + * data: Pointer to the region where the read data is to be copied into + * + * The data returned is in little-endian format, which is the standard for PCI config space. + */ +void vfio_pci_read_config_data(struct vfio_pci_dev *dev, size_t offset, + size_t size, void *data) +{ + struct vfio_region_info info = { + .argsz = sizeof(struct vfio_region_info), + .index = VFIO_PCI_CONFIG_REGION_INDEX, + }; + int ret; + + vfio_ioctl(dev->fd, VFIO_DEVICE_GET_REGION_INFO, &info); + + TEST_ASSERT(offset + size <= PCI_CFG_SPACE_EXP_SIZE, + "Requested config (%lu) and size (%lu) is out of bounds (%u)", + offset, size, PCI_CFG_SPACE_EXP_SIZE); + + ret = pread(dev->fd, data, size, info.offset + offset); + TEST_ASSERT(ret == size, "Failed to read the PCI config: 0x%lx\n", offset); +} + +static unsigned int vfio_pci_get_group_from_dev(const char *bdf) +{ + char dev_iommu_group_path[PATH_MAX] = {0}; + unsigned int pci_dev_sysfs_path_len; + char *pci_dev_sysfs_path; + unsigned int group; + int ret; + + pci_dev_sysfs_path_len = strlen(PCI_SYSFS_PATH) + strlen("DDDD:BB:DD.F/iommu_group") + 1; + + pci_dev_sysfs_path = calloc(1, pci_dev_sysfs_path_len); + TEST_ASSERT(pci_dev_sysfs_path, "Insufficient memory for pci dev sysfs path"); + + snprintf(pci_dev_sysfs_path, pci_dev_sysfs_path_len, + "%s%s/iommu_group", PCI_SYSFS_PATH, bdf); + + ret = readlink(pci_dev_sysfs_path, dev_iommu_group_path, + sizeof(dev_iommu_group_path)); + TEST_ASSERT(ret != -1, "Failed to get IOMMU group for device: %s", bdf); + + ret = sscanf(basename(dev_iommu_group_path), "%u", &group); + TEST_ASSERT(ret == 1, "Failed to get IOMMU group for device: %s", bdf); + + free(pci_dev_sysfs_path); + return group; +} + +static void vfio_pci_setup_group(struct vfio_pci_dev *dev, const char *bdf) +{ + char group_path[32]; + struct vfio_group_status group_status = { + .argsz = sizeof(group_status), + }; + int group; + + group = vfio_pci_get_group_from_dev(bdf); + snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group); + + dev->group_fd = open(group_path, O_RDWR); + TEST_ASSERT(dev->group_fd >= 0, + "Failed to open the VFIO group %d for device: %s\n", group, bdf); + + __vfio_ioctl(dev->group_fd, VFIO_GROUP_GET_STATUS, &group_status); + TEST_ASSERT(group_status.flags & VFIO_GROUP_FLAGS_VIABLE, + "Group %d for device %s not viable. Ensure all devices are bound to vfio-pci", + group, bdf); + + vfio_ioctl(dev->group_fd, VFIO_GROUP_SET_CONTAINER, &dev->container_fd); +} + +static void vfio_pci_set_iommu(struct vfio_pci_dev *dev, unsigned long iommu_type) +{ + TEST_ASSERT_EQ(__vfio_ioctl(dev->container_fd, VFIO_CHECK_EXTENSION, (void *)iommu_type), 1); + vfio_ioctl(dev->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); +} + +static void vfio_pci_open_device(struct vfio_pci_dev *dev, const char *bdf) +{ + struct vfio_device_info dev_info = { + .argsz = sizeof(dev_info), + }; + + dev->fd = __vfio_ioctl(dev->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf); + TEST_ASSERT(dev->fd >= 0, "Failed to get the device fd\n"); + + vfio_ioctl(dev->fd, VFIO_DEVICE_GET_INFO, &dev_info); + + TEST_ASSERT(!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET), + "If VFIO tries to reset the VF, it will fail."); + + /* Require at least all BAR regions and the config space. */ + TEST_ASSERT(dev_info.num_regions >= VFIO_PCI_CONFIG_REGION_INDEX, + "Required number regions not supported (%d) for device: %s", + dev_info.num_regions, bdf); + + /* Check for at least VFIO_PCI_MSIX_IRQ_INDEX irqs */ + TEST_ASSERT(dev_info.num_irqs >= VFIO_PCI_MSIX_IRQ_INDEX, + "MSI-X IRQs (%d) not supported for device: %s", + dev_info.num_irqs, bdf); +} + +/* bdf: PCI device's Domain:Bus:Device:Function in "DDDD:BB:DD.F" format */ +struct vfio_pci_dev *__vfio_pci_init(const char *bdf, unsigned long iommu_type) +{ + struct vfio_pci_dev *dev; + int vfio_version; + + TEST_ASSERT(bdf, "PCI BDF not supplied\n"); + + dev = calloc(1, sizeof(*dev)); + TEST_ASSERT(dev, "Insufficient memory for vfio_pci_dev"); + + dev->container_fd = open_path_or_exit(VFIO_DEV_PATH, O_RDWR); + + vfio_version = __vfio_ioctl(dev->container_fd, VFIO_GET_API_VERSION, NULL); + TEST_REQUIRE(vfio_version == VFIO_API_VERSION); + + + vfio_pci_setup_group(dev, bdf); + vfio_pci_set_iommu(dev, iommu_type); + vfio_pci_open_device(dev, bdf); + + return dev; +} + +void vfio_pci_free(struct vfio_pci_dev *dev) +{ + close(dev->fd); + vfio_ioctl(dev->group_fd, VFIO_GROUP_UNSET_CONTAINER, NULL); + + close(dev->group_fd); + close(dev->container_fd); + + free(dev); +} diff --git a/tools/testing/selftests/kvm/mercury_device.h b/tools/testing/selftests/kvm/mercury_device.h new file mode 100644 index 000000000000..fd4a3a5bac25 --- /dev/null +++ b/tools/testing/selftests/kvm/mercury_device.h @@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2022, Google LLC. + */ + +#ifndef SELFTEST_KVM_MERCURY_DEVICE_H +#define SELFTEST_KVM_MERCURY_DEVICE_H + +#include "processor.h" +#include "test_util.h" + +#define MERCURY_VENDOR_ID 0x1ae0 +#define MERCURY_DEVICE_ID 0x0050 + +/* The base registers of the mercury device begin at the below offset from BAR0 */ +#define MERCURY_BASE_OFFSET (768 * 1024) + +#define MERCURY_MSIX_VECTOR 0 +#define MERCURY_MSIX_COUNT 1 /* Currently, only 1 vector is assigned to mercury */ + +#define MERCURY_DMA_MAX_BUF_SIZE_BYTES SZ_8K +#define MERCURY_DMA_MEMCPY_MAX_BUF_SIZE_BYTES SZ_1G + +/* Mercury device accepts the DMA size as double-word (4-bytes) */ +#define MERCURY_DMA_SIZE_STRIDE 4 + +#define MERCURY_ABI_VERSION 0 + +/* Register Offsets relative to MERCURY_BASE_OFFSET */ +/* Unless otherwise specified, all the registers are 32-bits */ +#define MERCURY_REG_VERSION 0x0 /* Read-only */ +#define MERCURY_REG_COMMAND 0x04 /* Write-only */ +#define MERCURY_REG_STATUS 0x08 /* Read-only, 64-bit register */ +#define MERCURY_REG_DMA_SRC_ADDR 0x10 /* Read/Write, 64-bit register */ +#define MERCURY_REG_DMA_DEST_ADDR 0x18 /* Read/Write, 64-bit register */ +#define MERCURY_REG_DMA_DW_LEN 0x20 /* Read/Write */ +#define MERCURY_REG_SCRATCH_REG0 0x24 /* Read/Write */ +#define MERCURY_REG_SCRATCH_REG1 0x1000 /* Read/Write */ + +/* Bit positions of the STATUS register */ +enum mercury_status_bit { + MERCURY_STATUS_BIT_READY = 0, + MERCURY_STATUS_BIT_DMA_FROM_DEV_COMPLETE = 1, + MERCURY_STATUS_BIT_DMA_TO_DEV_COMPLETE = 2, + MERCURY_STATUS_BIT_DMA_MEMCPY_COMPLETE = 3, + MERCURY_STATUS_BIT_FORCE_INTERRUPT = 4, + MERCURY_STATUS_BIT_INVAL_DMA_SIZE = 5, + MERCURY_STATUS_BIT_DMA_ERROR = 6, + MERCURY_STATUS_BIT_CMD_ERR_INVAL_CMD = 7, + MERCURY_STATUS_BIT_CMD_ERR_DEV_NOT_READY = 8, +}; + +/* List of mercury commands that can be written into MERCURY_REG_COMMAND register */ +enum mercury_command { + MERCURY_COMMAND_RESET = 0, + MERCURY_COMMAND_TRIGGER_DMA_FROM_DEV = 1, + MERCURY_COMMAND_TRIGGER_DMA_TO_DEV = 2, + MERCURY_COMMAND_TRIGGER_DMA_MEMCPY = 3, + MERCURY_COMMAND_FORCE_INTERRUPT = 4, +}; + +static inline void mercury_write_reg64(void *bar0, uint32_t reg_off, uint64_t val) +{ + void *reg = bar0 + MERCURY_BASE_OFFSET + reg_off; + + writeq(val, reg); +} + +static inline void mercury_write_reg32(void *bar0, uint32_t reg_off, uint32_t val) +{ + void *reg = bar0 + MERCURY_BASE_OFFSET + reg_off; + + writel(val, reg); +} + +static inline uint32_t mercury_read_reg32(void *bar0, uint32_t reg_off) +{ + void *reg = bar0 + MERCURY_BASE_OFFSET + reg_off; + + return readl(reg); +} + +static inline uint64_t mercury_read_reg64(void *bar0, uint32_t reg_off) +{ + void *reg = bar0 + MERCURY_BASE_OFFSET + reg_off; + + return readq(reg); +} + +static inline uint64_t mercury_get_status(void *bar0) +{ + return mercury_read_reg64(bar0, MERCURY_REG_STATUS); +} + +static inline void mercury_issue_command(void *bar0, enum mercury_command cmd) +{ + mercury_write_reg32(bar0, MERCURY_REG_COMMAND, cmd); +} + +static inline void mercury_issue_reset(void *bar0) +{ + mercury_issue_command(bar0, MERCURY_COMMAND_RESET); +} + +static inline void mercury_force_irq(void *bar0) +{ + mercury_issue_command(bar0, MERCURY_COMMAND_FORCE_INTERRUPT); +} + +static inline void mercury_set_dma_size(void *bar0, size_t sz_bytes) +{ + /* Convert the DMA size from bytes to DWORDS, as accepted by the device */ + size_t sz_dwords = sz_bytes / MERCURY_DMA_SIZE_STRIDE; + + mercury_write_reg32(bar0, MERCURY_REG_DMA_DW_LEN, sz_dwords); +} + +#endif /* SELFTEST_KVM_MERCURY_DEVICE_H */ diff --git a/tools/testing/selftests/kvm/vfio_irq_test.c b/tools/testing/selftests/kvm/vfio_irq_test.c new file mode 100644 index 000000000000..1cdc6fee9e9a --- /dev/null +++ b/tools/testing/selftests/kvm/vfio_irq_test.c @@ -0,0 +1,429 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "apic.h" +#include "processor.h" +#include "test_util.h" +#include "kvm_util.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "vfio_pci_util.h" +#include "mercury_device.h" + +#define MERCURY_GSI 32 +#define MERCURY_IRQ_VECTOR 0x80 + +#define MERCURY_BAR0_GPA 0xc0000000ul +#define MERCURY_BAR0_SLOT 10 + +/* Shared variables. */ +static bool do_guest_irq = true; + +/* Guest-only variables, shared across vCPUs. */ +static int irqs_received; +static int irqs_sent; + +/* Host-only variables, shared across threads. */ +static cpu_set_t possible_mask; +static int min_cpu, max_cpu; +static bool done; +static struct kvm_vcpu *target_vcpu; +static sem_t do_irq; + +static bool x2apic; + +static void guest_irq_handler(struct ex_regs *regs) +{ + WRITE_ONCE(irqs_received, irqs_received + 1); + + if (x2apic) + x2apic_write_reg(APIC_EOI, 0); + else + xapic_write_reg(APIC_EOI, 0); +} + +static void guest_nmi_handler(struct ex_regs *regs) +{ + WRITE_ONCE(irqs_received, irqs_received + 1); +} + +#define GUEST_VERIFY_IRQS() \ +do { \ + int __received; \ + \ + __received = READ_ONCE(irqs_received); \ + __GUEST_ASSERT(__received == irqs_sent, \ + "Sent %u IRQ, received %u IRQs", irqs_sent, __received);\ +} while (0) + +#define GUEST_WAIT_FOR_IRQ() \ +do { \ + safe_halt(); \ + GUEST_VERIFY_IRQS(); \ + cli(); \ +} while (0) + +static void guest_code(uint32_t vcpu_id) +{ + /* GPA is identity mapped. */ + void *mercury_bar0 = (void *)MERCURY_BAR0_GPA; + uint64_t status; + int i; + + cli(); + + if (x2apic) { + x2apic_enable(); + GUEST_ASSERT(x2apic_read_reg(APIC_ID) == vcpu_id); + } else { + xapic_enable(); + GUEST_ASSERT(xapic_read_reg(APIC_ID) >> 24 == vcpu_id); + } + + if (vcpu_id == 0) { + irqs_sent++; + GUEST_ASSERT(READ_ONCE(do_guest_irq)); + mercury_issue_reset(mercury_bar0); + GUEST_WAIT_FOR_IRQ(); + + status = mercury_get_status(mercury_bar0); + __GUEST_ASSERT(status & BIT(MERCURY_STATUS_BIT_READY), + "Expected device ready after reset"); + GUEST_SYNC(irqs_received); + } + + for ( ; !READ_ONCE(done); ) { + irqs_sent++; + if (READ_ONCE(do_guest_irq)) + mercury_force_irq(mercury_bar0); + GUEST_WAIT_FOR_IRQ(); + GUEST_SYNC(irqs_received); + } + + sti_nop(); + + for (i = 0; i < 1000; i++) { + mercury_force_irq(mercury_bar0); + cpu_relax(); + } + + GUEST_VERIFY_IRQS(); + GUEST_SYNC(irqs_received); +} + +static void *irq_worker(void *mercury_bar0) +{ + struct kvm_vcpu *vcpu; + + for (;;) { + sem_wait(&do_irq); + + if (READ_ONCE(done)) + break; + + vcpu = READ_ONCE(target_vcpu); + while (!vcpu_get_stat(vcpu, blocking)) + cpu_relax(); + + mercury_force_irq(mercury_bar0); + } + return NULL; +} + +static int next_cpu(int cpu) +{ + /* + * Advance to the next CPU, skipping those that weren't in the original + * affinity set. Sadly, there is no CPU_SET_FOR_EACH, and cpu_set_t's + * data storage is considered as opaque. Note, if this task is pinned + * to a small set of discontiguous CPUs, e.g. 2 and 1023, this loop will + * burn a lot cycles and the test will take longer than normal to + * complete. + */ + do { + cpu++; + if (cpu > max_cpu) { + cpu = min_cpu; + TEST_ASSERT(CPU_ISSET(cpu, &possible_mask), + "Min CPU = %d must always be usable", cpu); + break; + } + } while (!CPU_ISSET(cpu, &possible_mask)); + + return cpu; +} + +static void *migration_worker(void *__guest_tid) +{ + pid_t guest_tid = (pid_t)(unsigned long)__guest_tid; + cpu_set_t allowed_mask; + int r, i, cpu; + + CPU_ZERO(&allowed_mask); + + for (i = 0, cpu = min_cpu; !READ_ONCE(done); i++, cpu = next_cpu(cpu)) { + CPU_SET(cpu, &allowed_mask); + + r = sched_setaffinity(guest_tid, sizeof(allowed_mask), &allowed_mask); + TEST_ASSERT(!r, "sched_setaffinity failed, errno = %d (%s)", + errno, strerror(errno)); + + CPU_CLR(cpu, &allowed_mask); + + usleep((i % 10) + 10); + } + return NULL; +} + +static void calc_min_max_cpu(void) +{ + int i, cnt, nproc; + + TEST_REQUIRE(CPU_COUNT(&possible_mask) >= 2); + + /* + * CPU_SET doesn't provide a FOR_EACH helper, get the min/max CPU that + * this task is affined to in order to reduce the time spent querying + * unusable CPUs, e.g. if this task is pinned to a small percentage of + * total CPUs. + */ + nproc = get_nprocs_conf(); + min_cpu = -1; + max_cpu = -1; + cnt = 0; + + for (i = 0; i < nproc; i++) { + if (!CPU_ISSET(i, &possible_mask)) + continue; + if (min_cpu == -1) + min_cpu = i; + max_cpu = i; + cnt++; + } + + __TEST_REQUIRE(cnt >= 2, "Only one usable CPU, task migration not possible"); +} + +static void sanity_check_mercury_device(struct vfio_pci_dev *dev, void *bar0) +{ + uint16_t vendor_id, device_id; + uint32_t version; + + vendor_id = vfio_pci_get_vendor_id(dev); + device_id = vfio_pci_get_device_id(dev); + + TEST_ASSERT(vendor_id == MERCURY_VENDOR_ID && + device_id == MERCURY_DEVICE_ID, + "Mercury vendor-id/device-id mismatch. " + "Expected vendor: 0x%04x, device: 0x%04x. " + "Got vendor: 0x%04x, device: 0x%04x", + MERCURY_VENDOR_ID, MERCURY_DEVICE_ID, + vendor_id, device_id); + + version = mercury_read_reg32(bar0, MERCURY_REG_VERSION); + TEST_ASSERT_EQ(version, MERCURY_ABI_VERSION); +} + +static void set_empty_routing(struct kvm_vm *vm, struct kvm_irq_routing *routing) +{ + routing->nr = 0; + routing->entries[0].gsi = MERCURY_GSI; + routing->entries[0].type = KVM_IRQ_ROUTING_IRQCHIP; + routing->entries[0].flags = 0; + routing->entries[0].u.msi.address_lo = 0; + routing->entries[0].u.msi.address_hi = 0; + routing->entries[0].u.msi.data = 0xfe; + vm_ioctl(vm, KVM_SET_GSI_ROUTING, routing); +} + +static void set_gsi_dest(struct kvm_vcpu *vcpu, struct kvm_irq_routing *routing, + bool do_nmi) +{ + routing->nr = 1; + routing->entries[0].gsi = MERCURY_GSI; + routing->entries[0].type = KVM_IRQ_ROUTING_MSI; + routing->entries[0].flags = 0; + routing->entries[0].u.msi.address_lo = (vcpu->id << 12); + routing->entries[0].u.msi.address_hi = 0; + if (do_nmi) + routing->entries[0].u.msi.data = NMI_VECTOR | (4 << 8); + else + routing->entries[0].u.msi.data = MERCURY_IRQ_VECTOR; + vm_ioctl(vcpu->vm, KVM_SET_GSI_ROUTING, routing); +} + +static void vcpu_run_and_verify(struct kvm_vcpu *vcpu, int nr_irqs) +{ + struct ucall uc; + + vcpu_run(vcpu); + TEST_ASSERT_EQ(get_ucall(vcpu, &uc), UCALL_SYNC); + TEST_ASSERT_EQ(uc.args[1], nr_irqs); +} + +int main(int argc, char *argv[]) +{ + bool migrate = false, nmi = false, async = false, empty = false; + pthread_t migration_thread, irq_thread; + struct kvm_irq_routing *routing; + struct vfio_pci_dev *dev; + struct kvm_vcpu *vcpus[2]; + int opt, r, eventfd, i; + int nr_irqs = 10000; + struct kvm_vm *vm; + uint64_t bar_size; + char *bdf = NULL; + void *bar; + + sem_init(&do_irq, 0, 0); + + while ((opt = getopt(argc, argv, "had:ei:mnx")) != -1) { + switch (opt) { + case 'a': + async = true; + break; + case 'd': + bdf = strdup(optarg); + break; + case 'e': + empty = true; + break; + case 'i': + nr_irqs = atoi_positive("Number of IRQs", optarg); + break; + case 'm': + migrate = true; + break; + case 'n': + nmi = true; + break; + case 'x': + x2apic = false; + break; + case 'h': + default: + pr_info("Usage: %s [-h] <-d pci-bdf>\n\n", argv[0]); + pr_info("\t-d: PCI Domain, Bus, Device, Function in the format DDDD:BB:DD.F\n"); + pr_info("\t-h: print this help screen\n"); + exit(KSFT_SKIP); + } + } + + __TEST_REQUIRE(bdf, "Required argument -d missing"); + + dev = vfio_pci_init(bdf); + bar = vfio_pci_map_bar(dev, VFIO_PCI_BAR0_REGION_INDEX, &bar_size); + sanity_check_mercury_device(dev, bar); + + vm = vm_create_with_vcpus(ARRAY_SIZE(vcpus), guest_code, vcpus); + vm_install_exception_handler(vm, MERCURY_IRQ_VECTOR, guest_irq_handler); + vm_install_exception_handler(vm, NMI_VECTOR, guest_nmi_handler); + + vcpu_args_set(vcpus[0], 1, 0); + vcpu_args_set(vcpus[1], 1, 1); + + virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA); + + vm_set_user_memory_region(vm, MERCURY_BAR0_SLOT, 0, MERCURY_BAR0_GPA, + bar_size, bar); + virt_map(vm, MERCURY_BAR0_GPA, MERCURY_BAR0_GPA, + vm_calc_num_guest_pages(VM_MODE_DEFAULT, bar_size)); + + routing = kvm_gsi_routing_create(); + + eventfd = kvm_new_eventfd(); + vfio_pci_assign_msix(dev, eventfd); + kvm_assign_irqfd(vm, MERCURY_GSI, eventfd); + + r = sched_getaffinity(0, sizeof(possible_mask), &possible_mask); + TEST_ASSERT(!r, "sched_getaffinity failed, errno = %d (%s)", errno, + strerror(errno)); + + if (migrate) { + calc_min_max_cpu(); + + pthread_create(&migration_thread, NULL, migration_worker, + (void *)(unsigned long)syscall(SYS_gettid)); + } + + if (nmi || async) + pthread_create(&irq_thread, NULL, irq_worker, bar); + + set_gsi_dest(vcpus[0], routing, false); + vcpu_run_and_verify(vcpus[0], 1); + +#if 0 + /* + * Hack if the user wants to manually mess with interrupt routing while + * the test is running, e.g. by modifying smp_affinity in the host. + */ + for (i = 1; i < nr_irqs; i++) { + usleep(1000 * 1000); + vcpu_run_and_verify(vcpus[0], i + 1); + } +#endif + + for (i = 1; i < nr_irqs; i++) { + struct kvm_vcpu *vcpu = vcpus[!!(i & BIT(1))]; + const bool do_nmi = nmi && (i & BIT(2)); + const bool do_empty = empty && (i & BIT(3)); + const bool do_async = nmi || async; + + if (do_empty) + set_empty_routing(vm, routing); + + set_gsi_dest(vcpu, routing, do_nmi); + + WRITE_ONCE(do_guest_irq, !do_async); + sync_global_to_guest(vm, do_guest_irq); + + if (do_async) { + WRITE_ONCE(target_vcpu, vcpu); + sem_post(&do_irq); + } + + vcpu_run_and_verify(vcpu, i + 1); + } + + WRITE_ONCE(done, true); + sync_global_to_guest(vm, done); + sem_post(&do_irq); + + for (i = 0; empty && i < ARRAY_SIZE(vcpus); i++) { + struct kvm_vcpu *vcpu = vcpus[i]; + + if (!i) + set_gsi_dest(vcpu, routing, false); + set_empty_routing(vm, routing); + vcpu_run_and_verify(vcpu, nr_irqs); + } + + set_gsi_dest(vcpus[0], routing, false); + + if (migrate) + pthread_join(migration_thread, NULL); + + if (nmi || async) + pthread_join(irq_thread, NULL); + + r = munmap(bar, bar_size); + TEST_ASSERT(!r, __KVM_SYSCALL_ERROR("munmap()", r)); + + vfio_pci_free(dev); + + return 0; +}