From nobody Wed Feb 11 23:06:37 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A5E8C77B71 for ; Fri, 14 Apr 2023 18:05:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229744AbjDNSF2 (ORCPT ); Fri, 14 Apr 2023 14:05:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229479AbjDNSF0 (ORCPT ); Fri, 14 Apr 2023 14:05:26 -0400 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B42B735B0; Fri, 14 Apr 2023 11:05:25 -0700 (PDT) Received: from skinsburskii.localdomain (c-67-170-100-148.hsd1.wa.comcast.net [67.170.100.148]) by linux.microsoft.com (Postfix) with ESMTPSA id A7DD22179262; Fri, 14 Apr 2023 11:05:24 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A7DD22179262 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1681495525; bh=7TWgMCwTIchIJki1b6WkkqtvzdzihOoHkeu7FSM1cGI=; h=Subject:From:Cc:Date:From; b=kUZBbC0uf3JACWAKeVn2KDZBX2MgO5n+huVUR6gkRASjspJ7VNiMfNiXiHHYYs1Zy EGSXjjOSSJZ0b4sO/4obctT6uobJVkW/KqXeIj6LaTtZT5wDsQh6cQUnTnDLVDcmc/ pDL7zSwAuj2iilCHVOMaFVk/2vxjX7aC7+nqYyuI= Subject: [PATCH] x86/hyperv: Fix IRQ effective cpu discovery for the interrupts unmasking From: Stanislav Kinsburskii Cc: Stanislav Kinsburskii , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Joerg Roedel , Will Deacon , Robin Murphy , linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev Date: Wed, 12 Apr 2023 13:29:20 -0700 Message-ID: <168133133232.4448.8053082360972165835.stgit@skinsburskii.localdomain> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org As of today, the existent code uses conjunction of IRQ affinity mask and cpu online mask to find the cpu id to map an interrupt to. I looks like the intention was to make sure that and IRQ won't be mapped to= an offline CPU. Although it works correctly today, there are two problems with it: 1. IRQ affinity mask already consists only of online cpus, thus matching it to the mask on online cpus is redundant. 2. cpumask_first_and() can return nr_cpu_ids in case of IRQ affinity containing offline cpus in future, and in this case current implementation will likely lead to kernel crash in hv_map_interrupt due to an attempt to u= se invalid cpu id for getting vp set. This patch fixes this logic by taking the first bit from the affinity mask as the cpu to map the IRQ to. It also adds a paranoia WARN_ON_ONCE for the case when the affinity mask contains offline cpus. Signed-off-by: Stanislav Kinsburskii CC: "K. Y. Srinivasan" CC: Haiyang Zhang CC: Wei Liu CC: Dexuan Cui CC: Thomas Gleixner CC: Ingo Molnar CC: Borislav Petkov CC: Dave Hansen CC: x86@kernel.org CC: "H. Peter Anvin" CC: Joerg Roedel CC: Will Deacon CC: Robin Murphy CC: linux-hyperv@vger.kernel.org CC: linux-kernel@vger.kernel.org CC: iommu@lists.linux.dev --- arch/x86/hyperv/irqdomain.c | 7 ++++--- drivers/iommu/hyperv-iommu.c | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/arch/x86/hyperv/irqdomain.c b/arch/x86/hyperv/irqdomain.c index 42c70d28ef27..759774b5ab2f 100644 --- a/arch/x86/hyperv/irqdomain.c +++ b/arch/x86/hyperv/irqdomain.c @@ -192,7 +192,6 @@ static void hv_irq_compose_msi_msg(struct irq_data *dat= a, struct msi_msg *msg) struct pci_dev *dev; struct hv_interrupt_entry out_entry, *stored_entry; struct irq_cfg *cfg =3D irqd_cfg(data); - const cpumask_t *affinity; int cpu; u64 status; =20 @@ -204,8 +203,10 @@ static void hv_irq_compose_msi_msg(struct irq_data *da= ta, struct msi_msg *msg) return; } =20 - affinity =3D irq_data_get_effective_affinity_mask(data); - cpu =3D cpumask_first_and(affinity, cpu_online_mask); + cpu =3D cpumask_first(irq_data_get_effective_affinity_mask(data)); + + /* Paranoia check: the cpu must be online */ + WARN_ON_ONCE(!cpumask_test_cpu(cpu, cpu_online_mask)); =20 if (data->chip_data) { /* diff --git a/drivers/iommu/hyperv-iommu.c b/drivers/iommu/hyperv-iommu.c index 8302db7f783e..632e9c123bbf 100644 --- a/drivers/iommu/hyperv-iommu.c +++ b/drivers/iommu/hyperv-iommu.c @@ -197,15 +197,16 @@ hyperv_root_ir_compose_msi_msg(struct irq_data *irq_d= ata, struct msi_msg *msg) u32 vector; struct irq_cfg *cfg; int ioapic_id; - const struct cpumask *affinity; int cpu; struct hv_interrupt_entry entry; struct hyperv_root_ir_data *data =3D irq_data->chip_data; struct IO_APIC_route_entry e; =20 cfg =3D irqd_cfg(irq_data); - affinity =3D irq_data_get_effective_affinity_mask(irq_data); - cpu =3D cpumask_first_and(affinity, cpu_online_mask); + cpu =3D cpumask_first(irq_data_get_effective_affinity_mask(irq_data)); + + /* Paranoia check: the cpu must be online */ + WARN_ON_ONCE(!cpumask_test_cpu(cpu, cpu_online_mask)); =20 vector =3D cfg->vector; ioapic_id =3D data->ioapic_id;