Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp899831rwe; Fri, 14 Apr 2023 11:08:51 -0700 (PDT) X-Google-Smtp-Source: AKy350ZajD34g9o1t2maCY7VIMmHONydAPK9/EH84hAj4MpWnoAuEyobARqBfONWRPLjrbAQz558 X-Received: by 2002:a05:6a00:4502:b0:63b:6373:786b with SMTP id cw2-20020a056a00450200b0063b6373786bmr4612618pfb.2.1681495731371; Fri, 14 Apr 2023 11:08:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681495731; cv=none; d=google.com; s=arc-20160816; b=qwSzoLGRhwOBnlw5+8BD1QHymw3INF7i1km+YUA6BiIVNO54NxR1ZYkJzQkCzgJRH7 Sg1qYX06224NtE8KEgJ7VHiFL/QvKzu6AD7w4L56OhdJN038mxu7u7Y2Zio7QE2mx7uo AsVUxRsRU9pnxtGmP4GiUVs1aqE1t8qYHUu8M514BS/r9+1yIa3KV33064NlG3XM4+Fj MCq6yTx0i1EEPOi/Y70jAyk/7SjGzmKDjxb7VueDNUkDXnJBXWIsm4ejA/WS7cPXs8pT r2R2Pj7ZsyiQLxnDrZrW8/+OJWrl1x8yhumo27VbwRb1cFGUoAUwBv86aG/z4cB2l3yr 5dsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:content-transfer-encoding:mime-version :user-agent:message-id:date:cc:from:subject:dkim-signature :dkim-filter; bh=7TWgMCwTIchIJki1b6WkkqtvzdzihOoHkeu7FSM1cGI=; b=aeZgWiHx2kStZCYmTH59JdlkND8Z+9UexkE8xm6QtyD4+nMvcldR84XAI6vC6LJe8V LpB2yG7evR5JQF6W/yW0YCskei/ew65vaOjXGXaor81jIJOkvMvNfUdnzxy+I4a7GaO4 I4nPDyHL0W9BPvv2FwVVVLDHUqoJUS+KhF9uGWBNjqSL/ErqFYvRkb5ytaBQXrXb/2Ys Me3Rztw56wJ1EgwrktoOO6wTa8eldwy71Xw5mxtHW5vvYS0XFNyZCHToDcQvKvS9An/9 YotvN9IyVrxa2h5c2xdDtAOhQozv4AHXVxnONBT7JPBO8A5AAfMQanl+liv8nDB87QZm daPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=kUZBbC0u; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u15-20020a63470f000000b0051b280be4a2si4920265pga.486.2023.04.14.11.08.36; Fri, 14 Apr 2023 11:08:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=kUZBbC0u; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229636AbjDNSF1 (ORCPT + 99 others); Fri, 14 Apr 2023 14:05:27 -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: 7bit X-Spam-Status: No, score=-17.5 required=5.0 tests=BAYES_00,DATE_IN_PAST_24_48, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,ENV_AND_HDR_SPF_MATCH, MISSING_HEADERS,RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 use 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 *data, struct msi_msg *msg) struct pci_dev *dev; struct hv_interrupt_entry out_entry, *stored_entry; struct irq_cfg *cfg = irqd_cfg(data); - const cpumask_t *affinity; int cpu; u64 status; @@ -204,8 +203,10 @@ static void hv_irq_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) return; } - affinity = irq_data_get_effective_affinity_mask(data); - cpu = cpumask_first_and(affinity, cpu_online_mask); + cpu = 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)); 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_data, 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 = irq_data->chip_data; struct IO_APIC_route_entry e; cfg = irqd_cfg(irq_data); - affinity = irq_data_get_effective_affinity_mask(irq_data); - cpu = cpumask_first_and(affinity, cpu_online_mask); + cpu = 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)); vector = cfg->vector; ioapic_id = data->ioapic_id;