From patchwork Wed Apr 12 20:29:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Kinsburskii X-Patchwork-Id: 83557 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp582157vqo; Fri, 14 Apr 2023 11:31:28 -0700 (PDT) X-Google-Smtp-Source: AKy350adjvnsHPWWVU/UeMwD7p6e3UtsE9Vs7McLmAg9a6xDgOjN5NDHvB+OhOnnjmiRD42xNO1x X-Received: by 2002:a17:903:2341:b0:19e:b2ed:6fff with SMTP id c1-20020a170903234100b0019eb2ed6fffmr3513020plh.31.1681497088057; Fri, 14 Apr 2023 11:31:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681497088; cv=none; d=google.com; s=arc-20160816; b=RHtN4+1TLH4+AP9y1j+2ZR0i3b06j/CI+Vwx+G+NW8gpIUviosXYSO+1oKklsQKtm4 S9gNAXpP5TJrokBQgQgDt7Tx4RBax0cr/FqL1W3PAdPEbAAe/c/8FjddE8wy/vCq8mZx e6maRY1nPWern5bhv/jooMPBnW1gSuA0a5MCDn492I5godfO1qZK4Nv+wADssXn8GuYf hIzyE4riOpadX+ytrqTtWCJ6FsNsby7BMB1ohDHQqNtAw/Oe4EyAnKb5oPL4gTIDAhn8 3KYH8E7BNRHThAquuvVRN70YQNrHURgyfiER6tVBXRe2p4jSbHtQugmyjITQ28upET2K /F/Q== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id iw18-20020a170903045200b001a64a25c7fbsi4963061plb.23.2023.04.14.11.31.15; Fri, 14 Apr 2023 11:31:28 -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 S229647AbjDNSF1 (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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763177490573969898?= X-GMAIL-MSGID: =?utf-8?q?1763177490573969898?= 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;