From patchwork Wed Mar 22 12:45:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?TWFyay1QSyBUc2FpICjolKHmspvliZsp?= X-Patchwork-Id: 73409 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp2326547wrt; Wed, 22 Mar 2023 06:00:18 -0700 (PDT) X-Google-Smtp-Source: AK7set9h+3xbE9ysy6DPD41P/T558u2T3pvLk/2zQzqC0l0uLPS/QhiB2SXb8JilEQi0jbCUBKZk X-Received: by 2002:a05:6402:1350:b0:4fc:9329:bd0c with SMTP id y16-20020a056402135000b004fc9329bd0cmr6357522edw.12.1679490018670; Wed, 22 Mar 2023 06:00:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679490018; cv=none; d=google.com; s=arc-20160816; b=C/+HBZXZWSCTL8Nzl0QpYWh2yv2vrPQR9id3HVttS7e/vk+ETSmwI/mT9twQUqngfh k4Pnp71675WKm9B8Cvw8teaejKl0VoczrZaUYp8Ywufm/c3YDZc5uSX16OIIUll1v5k8 8ZAythO0lXWYWD5K/OAkBuzirzeNqvo9fpe+MqjcgDkvf4y/Zc9FC7rpBRyeDm7jtjL/ DA3gnSS4yHvKLqL9tL0HzkMYzctkazDAZ8J4jr6fyBa7bQHf4Av2NrSxQ9XA0/YlMwAP 8SuMM3lA8orj6z4VZRG3rHh7EZ4tXyfyhd2djn42zB+kMV2dWottDd1DQ3ZhKp92aFay EXOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from :dkim-signature; bh=iWutgyze2foXifBJdblIUuyRcM/hTwcNm07Al/lIIAo=; b=0BfJHAIJL1w7g3qw7qSuVZEf6gck5WwwyNZqxr/abWTwfA5Yx1rBuEqJgy50pEeWvA EEscSfe5lcW/0w20WVJ2i6il1cjjnQeMik+pek7oqg/PIKLx/DkqSJXs4WpTxXcGMy5c X8R+0Km/rc//B9u59crH/47XLIb/v+6Jc+jf4nFgdb7YyqU2aZyGMITPrSNkh648Z273 /F30MO4/rSkwC915dJFvPsEgHfh8tPhAerk6LnUA0hKvn2CdrzKEIbZroLoM5pUcvT4G NWdwoc5QTFgNqSgaRkLKhbyJN7Q5SUuq5m5mJcBr8RAKAtqK5NN1246zOvrROTR/nLKV uOdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=qPEbrPnm; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g19-20020aa7c593000000b004fd25330f52si16686812edq.117.2023.03.22.05.59.54; Wed, 22 Mar 2023 06:00:18 -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=@mediatek.com header.s=dk header.b=qPEbrPnm; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230389AbjCVMqE (ORCPT + 99 others); Wed, 22 Mar 2023 08:46:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229865AbjCVMqB (ORCPT ); Wed, 22 Mar 2023 08:46:01 -0400 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F246A28874; Wed, 22 Mar 2023 05:45:58 -0700 (PDT) X-UUID: 7b495aa6c8af11edb6b9f13eb10bd0fe-20230322 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=iWutgyze2foXifBJdblIUuyRcM/hTwcNm07Al/lIIAo=; b=qPEbrPnmNITgjZM3JN8X7z6eJBAqYZK/nL+vLQ4xpiCpdtlQnYi949b48tqF9ZsCKW6CDZ/zGEVRr49/hDxrnG9hwxICXixCT6YXPwqcMvC8NUMq69bDjyg/y57nROwe2+BJg/qvoPpBWUDIhi4U13JNczyV6Y0GUYDlaivkSYk=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.22,REQID:40f6bbec-6616-4ab7-9e6c-5455a575e2ef,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:120426c,CLOUDID:2f032829-564d-42d9-9875-7c868ee415ec,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:1,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-UUID: 7b495aa6c8af11edb6b9f13eb10bd0fe-20230322 Received: from mtkmbs13n2.mediatek.inc [(172.21.101.108)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1200817029; Wed, 22 Mar 2023 20:45:53 +0800 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by mtkmbs11n1.mediatek.inc (172.21.101.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Wed, 22 Mar 2023 20:45:51 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Wed, 22 Mar 2023 20:45:51 +0800 From: Mark-PK Tsai To: Marc Zyngier , Thomas Gleixner , Matthias Brugger CC: Johan Hovold , , Greg Kroah-Hartman , Mark-PK Tsai , , , Subject: [PATCH 5.4 1/3] irqdomain: Look for existing mapping only once Date: Wed, 22 Mar 2023 20:45:48 +0800 Message-ID: <20230322124550.29812-1-mark-pk.tsai@mediatek.com> X-Mailer: git-send-email 2.18.0 MIME-Version: 1.0 X-MTK: N X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS, UNPARSEABLE_RELAY,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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?1761072925686455613?= X-GMAIL-MSGID: =?utf-8?q?1761072925686455613?= From: Johan Hovold commit 6e6f75c9c98d2d246d90411ff2b6f0cd271f4cba upstream. Avoid looking for an existing mapping twice when creating a new mapping using irq_create_fwspec_mapping() by factoring out the actual allocation which is shared with irq_create_mapping_affinity(). The new helper function will also be used to fix a shared-interrupt mapping race, hence the Fixes tag. Fixes: b62b2cf5759b ("irqdomain: Fix handling of type settings for existing mappings") Cc: stable@vger.kernel.org # 4.8 Tested-by: Hsin-Yi Wang Tested-by: Mark-PK Tsai Signed-off-by: Johan Hovold Signed-off-by: Marc Zyngier Link: https://lore.kernel.org/r/20230213104302.17307-5-johan+linaro@kernel.org Signed-off-by: Greg Kroah-Hartman Signed-off-by: Mark-PK Tsai --- kernel/irq/irqdomain.c | 62 +++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/kernel/irq/irqdomain.c b/kernel/irq/irqdomain.c index d40ae18fe661..8d85eeb0f078 100644 --- a/kernel/irq/irqdomain.c +++ b/kernel/irq/irqdomain.c @@ -672,6 +672,34 @@ unsigned int irq_create_direct_mapping(struct irq_domain *domain) } EXPORT_SYMBOL_GPL(irq_create_direct_mapping); +static unsigned int __irq_create_mapping_affinity(struct irq_domain *domain, + irq_hw_number_t hwirq, + const struct irq_affinity_desc *affinity) +{ + struct device_node *of_node = irq_domain_get_of_node(domain); + int virq; + + pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq); + + /* Allocate a virtual interrupt number */ + virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node), + affinity); + if (virq <= 0) { + pr_debug("-> virq allocation failed\n"); + return 0; + } + + if (irq_domain_associate(domain, virq, hwirq)) { + irq_free_desc(virq); + return 0; + } + + pr_debug("irq %lu on domain %s mapped to virtual irq %u\n", + hwirq, of_node_full_name(of_node), virq); + + return virq; +} + /** * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space * @domain: domain owning this hardware interrupt or NULL for default domain @@ -684,49 +712,27 @@ EXPORT_SYMBOL_GPL(irq_create_direct_mapping); * on the number returned from that call. */ unsigned int irq_create_mapping_affinity(struct irq_domain *domain, - irq_hw_number_t hwirq, - const struct irq_affinity_desc *affinity) + irq_hw_number_t hwirq, + const struct irq_affinity_desc *affinity) { - struct device_node *of_node; int virq; - pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq); - - /* Look for default domain if nececssary */ + /* Look for default domain if necessary */ if (domain == NULL) domain = irq_default_domain; if (domain == NULL) { WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq); return 0; } - pr_debug("-> using domain @%p\n", domain); - - of_node = irq_domain_get_of_node(domain); /* Check if mapping already exists */ virq = irq_find_mapping(domain, hwirq); if (virq) { - pr_debug("-> existing mapping on virq %d\n", virq); + pr_debug("existing mapping on virq %d\n", virq); return virq; } - /* Allocate a virtual interrupt number */ - virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node), - affinity); - if (virq <= 0) { - pr_debug("-> virq allocation failed\n"); - return 0; - } - - if (irq_domain_associate(domain, virq, hwirq)) { - irq_free_desc(virq); - return 0; - } - - pr_debug("irq %lu on domain %s mapped to virtual irq %u\n", - hwirq, of_node_full_name(of_node), virq); - - return virq; + return __irq_create_mapping_affinity(domain, hwirq, affinity); } EXPORT_SYMBOL_GPL(irq_create_mapping_affinity); @@ -866,7 +872,7 @@ unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec) return 0; } else { /* Create mapping */ - virq = irq_create_mapping(domain, hwirq); + virq = __irq_create_mapping_affinity(domain, hwirq, NULL); if (!virq) return virq; } From patchwork Wed Mar 22 12:45:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?TWFyay1QSyBUc2FpICjolKHmspvliZsp?= X-Patchwork-Id: 73410 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp2327734wrt; Wed, 22 Mar 2023 06:02:05 -0700 (PDT) X-Google-Smtp-Source: AK7set90yMb7zf7zPhHi0IJxYTLiImssqKXx1pSzgEEkRXIoP+WWJOy8ZXa65mH0lmeDtU3mghJK X-Received: by 2002:a17:906:a84d:b0:931:99b5:6791 with SMTP id dx13-20020a170906a84d00b0093199b56791mr5600239ejb.72.1679490125542; Wed, 22 Mar 2023 06:02:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679490125; cv=none; d=google.com; s=arc-20160816; b=KVqqo+1Nca+08eEgtWRfRG6hQx3eQ+7tDCgl5s9a3gBu/ohniKY+hw5p5IXgJyd+8+ 9to2eNLzdwpqu9lU16QWFv4BSFicOx6V2oSElinlObIF8hmE4r/yA4VCQ9cKOpu8yLW/ EZVTanjsB9KWTG9uVcAsmGFUNiD5MR0mwrClwHWcoTL6Wo+0knerh5XJvlVjT8wKrUNA KOOL4O1Rda/D9CM2rZoogUbPYTaYRFoiDlV2WKLJWedulq9wO/sXlvY4hcv+EIFLa9sw cOpjqwYBG1XNJWtwFdD1Cr+zWXFERJwXOL4PBwOFZBMFjyPfrkFOU0cFuMPMOgBVGrcZ 5ifg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=uIkxZI9w7v3VN7h5BVP7NSur8e4gufG83WcXLCeQmhg=; b=QWDMCM2Lt4Xz/d9O06RBxou6h//I7ikCza+DaGK+4r3lYmItoutYQGZqUanws60nnR 9dIcBLh6Xdu8UWh3Gx7En8jjJcZz+0MTLSvHdikr563wIAX6k+AfGoPVEJG1JEPdrp2Q CcyPA3u8sYt0Gccnku/G496XbLhNmWGa+XKIYE9D/ls3q3/QSzC/wRpGY+qY+blTkNk1 vTXLN721gUC63fhDn6nROnOmPfuk2uhZz9Ld4JWUyRAzLX7MTiljlfZ5DUyJn+2usRLP YAQvWs9GV4VbDPRumELoI4MfNgNSsbew+UWPt9grMl3xEilkmJ1BHsdmaYbPq4210fAy F9WQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=HHFbIHga; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id vg4-20020a170907d30400b009377c5b6b0asi6904662ejc.275.2023.03.22.06.01.42; Wed, 22 Mar 2023 06:02:05 -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=@mediatek.com header.s=dk header.b=HHFbIHga; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230413AbjCVMqK (ORCPT + 99 others); Wed, 22 Mar 2023 08:46:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230346AbjCVMqE (ORCPT ); Wed, 22 Mar 2023 08:46:04 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D89AD28874; Wed, 22 Mar 2023 05:46:01 -0700 (PDT) X-UUID: 7c7e39bec8af11eda9a90f0bb45854f4-20230322 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=uIkxZI9w7v3VN7h5BVP7NSur8e4gufG83WcXLCeQmhg=; b=HHFbIHgaqpJSUyTNnu4tUpsRD3ccujz29fqSstjgmzbSzQRqN21lVUiCtdZBSctgvQADzVTVKrVtoAedEjGCyjt36J99ik24sr2WYLhYKb43f7O6C/UTt9Ix0egqgKFAuiRxrc+Nii6gUvpxD06F1YlYDMbik6/FQ+qO6K53f8s=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.22,REQID:d3ad7e5b-f100-4dda-b8f6-71ddce3523f0,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:70 X-CID-INFO: VERSION:1.1.22,REQID:d3ad7e5b-f100-4dda-b8f6-71ddce3523f0,IP:0,URL :0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Spam_GS981B3D,ACTI ON:quarantine,TS:70 X-CID-META: VersionHash:120426c,CLOUDID:69032829-564d-42d9-9875-7c868ee415ec,B ulkID:230322204557JQ9QSFXQ,BulkQuantity:0,Recheck:0,SF:38|29|28|17|19|48,T C:nil,Content:0,EDM:-3,IP:nil,URL:1,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0 ,OSI:0,OSA:0,AV:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-UUID: 7c7e39bec8af11eda9a90f0bb45854f4-20230322 Received: from mtkmbs10n1.mediatek.inc [(172.21.101.34)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 585552375; Wed, 22 Mar 2023 20:45:55 +0800 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by mtkmbs11n1.mediatek.inc (172.21.101.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Wed, 22 Mar 2023 20:45:54 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Wed, 22 Mar 2023 20:45:53 +0800 From: Mark-PK Tsai To: Marc Zyngier , Thomas Gleixner , Matthias Brugger CC: Johan Hovold , , Sasha Levin , Mark-PK Tsai , , , Subject: [PATCH 5.4 2/3] irqdomain: Refactor __irq_domain_alloc_irqs() Date: Wed, 22 Mar 2023 20:45:49 +0800 Message-ID: <20230322124550.29812-2-mark-pk.tsai@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230322124550.29812-1-mark-pk.tsai@mediatek.com> References: <20230322124550.29812-1-mark-pk.tsai@mediatek.com> MIME-Version: 1.0 X-MTK: N X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS, UNPARSEABLE_RELAY,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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?1761073038017842011?= X-GMAIL-MSGID: =?utf-8?q?1761073038017842011?= From: Johan Hovold [ Upstream commit d55f7f4c58c07beb5050a834bf57ae2ede599c7e ] Refactor __irq_domain_alloc_irqs() so that it can be called internally while holding the irq_domain_mutex. This will be used to fix a shared-interrupt mapping race, hence the Fixes tag. Fixes: b62b2cf5759b ("irqdomain: Fix handling of type settings for existing mappings") Cc: stable@vger.kernel.org # 4.8 Tested-by: Hsin-Yi Wang Tested-by: Mark-PK Tsai Signed-off-by: Johan Hovold Signed-off-by: Marc Zyngier Link: https://lore.kernel.org/r/20230213104302.17307-6-johan+linaro@kernel.org Signed-off-by: Sasha Levin Signed-off-by: Mark-PK Tsai --- kernel/irq/irqdomain.c | 74 ++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/kernel/irq/irqdomain.c b/kernel/irq/irqdomain.c index 8d85eeb0f078..1eb34eaeaa94 100644 --- a/kernel/irq/irqdomain.c +++ b/kernel/irq/irqdomain.c @@ -1350,6 +1350,45 @@ int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain, return domain->ops->alloc(domain, irq_base, nr_irqs, arg); } +static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base, + unsigned int nr_irqs, int node, void *arg, + bool realloc, const struct irq_affinity_desc *affinity) +{ + int i, ret, virq; + + if (realloc && irq_base >= 0) { + virq = irq_base; + } else { + virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node, + affinity); + if (virq < 0) { + pr_debug("cannot allocate IRQ(base %d, count %d)\n", + irq_base, nr_irqs); + return virq; + } + } + + if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) { + pr_debug("cannot allocate memory for IRQ%d\n", virq); + ret = -ENOMEM; + goto out_free_desc; + } + + ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg); + if (ret < 0) + goto out_free_irq_data; + for (i = 0; i < nr_irqs; i++) + irq_domain_insert_irq(virq + i); + + return virq; + +out_free_irq_data: + irq_domain_free_irq_data(virq, nr_irqs); +out_free_desc: + irq_free_descs(virq, nr_irqs); + return ret; +} + /** * __irq_domain_alloc_irqs - Allocate IRQs from domain * @domain: domain to allocate from @@ -1376,7 +1415,7 @@ int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base, unsigned int nr_irqs, int node, void *arg, bool realloc, const struct irq_affinity_desc *affinity) { - int i, ret, virq; + int ret; if (domain == NULL) { domain = irq_default_domain; @@ -1384,40 +1423,11 @@ int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base, return -EINVAL; } - if (realloc && irq_base >= 0) { - virq = irq_base; - } else { - virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node, - affinity); - if (virq < 0) { - pr_debug("cannot allocate IRQ(base %d, count %d)\n", - irq_base, nr_irqs); - return virq; - } - } - - if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) { - pr_debug("cannot allocate memory for IRQ%d\n", virq); - ret = -ENOMEM; - goto out_free_desc; - } - mutex_lock(&irq_domain_mutex); - ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg); - if (ret < 0) { - mutex_unlock(&irq_domain_mutex); - goto out_free_irq_data; - } - for (i = 0; i < nr_irqs; i++) - irq_domain_insert_irq(virq + i); + ret = irq_domain_alloc_irqs_locked(domain, irq_base, nr_irqs, node, arg, + realloc, affinity); mutex_unlock(&irq_domain_mutex); - return virq; - -out_free_irq_data: - irq_domain_free_irq_data(virq, nr_irqs); -out_free_desc: - irq_free_descs(virq, nr_irqs); return ret; } From patchwork Wed Mar 22 12:45:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?TWFyay1QSyBUc2FpICjolKHmspvliZsp?= X-Patchwork-Id: 73408 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp2325791wrt; Wed, 22 Mar 2023 05:58:22 -0700 (PDT) X-Google-Smtp-Source: AK7set9zscb0qsflV4ViVynZJR6hXdQCxH0ZG/YtoiqLcdv5BJhwjxabb0OL+7gtnSW8fPVf1S/G X-Received: by 2002:aa7:da0a:0:b0:4fb:ec6a:2357 with SMTP id r10-20020aa7da0a000000b004fbec6a2357mr6708664eds.34.1679489902296; Wed, 22 Mar 2023 05:58:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679489902; cv=none; d=google.com; s=arc-20160816; b=OsJDbz/NmJsU7/rJiDRIxtEp8dQH3pJTVKv6WzREw+qCsFGWEH8caoiNM2rX/Y0QOL hpNdXQApaW9dtDANU7CY8cQu+mg0lczxlj7sLqmp7d5ujUqMxwzxoeYNulN/b7XdaLR9 GcAfhKLpyO04r9sa/iIwqtA8mGeoJ159ijf0PANQ5gWYJNoJx+Vi+/Gz30Dd3AosO7G7 MNkp38TVbFb7UYTjCtreIQbp9XNosh8QV7tTZI8OR8k8zvsljBFC+0EMUwpf7aOZOJX6 PaVbnQCu7AKughYhbo7b8OAioumdo5iJZRuPJTizAjCkqBQvjNnloz1RmA/Y9H3crrNw HvEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=RtlyoNDumdqdrT5xCZJJmPiYA26C5IwVsi00DfMAgpU=; b=mCX4Toov+6ry8h5nJAK3gvpoyN+iSOa0VjtW+yJ9iXTQioy2DURNrM95h04OiioOE/ tQZf1ERlrstSo1tZc7krG0w976VHltA41Ki6drzsqgRtdr6NJjSJ7dvDlSIChQygwXL6 95NdNGq3aqPDX9rZvQ1ouH1R9zx6X3Cp//43EbSfHcPopaKQdAhuZAhRQZ+lEhPPa7RE OMPspwdOK5kfCVV/Tgt2ogQMravOloTexVF3X+7VEWJjf6g0idpx/igmp5GMhjfkAzbQ ErbJNKwEDFG4gKn8z1LsGShEl4CcUMWR5J2N0UofPix6FaBuZ41napSpSIUwM/hEp8np 6yOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=MRSgzKhz; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k17-20020aa7c391000000b004fc7b78f055si17152122edq.481.2023.03.22.05.57.57; Wed, 22 Mar 2023 05:58:22 -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=@mediatek.com header.s=dk header.b=MRSgzKhz; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230399AbjCVMqH (ORCPT + 99 others); Wed, 22 Mar 2023 08:46:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230384AbjCVMqE (ORCPT ); Wed, 22 Mar 2023 08:46:04 -0400 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDA743B221; Wed, 22 Mar 2023 05:46:01 -0700 (PDT) X-UUID: 7d5277e2c8af11edb6b9f13eb10bd0fe-20230322 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=RtlyoNDumdqdrT5xCZJJmPiYA26C5IwVsi00DfMAgpU=; b=MRSgzKhzlx8ebdbfs9nk4vNLc888IUTSHoRxBs/h8Q+TJcqsJN/fEQF1xcljrlvTY/SOa9ygKjoYFaKPkoRNrtjhoIrPhBsxKTY4TribcNkxfojwlIN33KuTprG5iJacG+Wud74l6Q+Z+RedO7kCpVXnet/C0XYSbuLTS72nXeI=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.22,REQID:26fcac05-bc6d-4d79-9807-94cb12049e1b,IP:0,U RL:0,TC:0,Content:-5,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION :release,TS:-5 X-CID-META: VersionHash:120426c,CLOUDID:5b032829-564d-42d9-9875-7c868ee415ec,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:1,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-UUID: 7d5277e2c8af11edb6b9f13eb10bd0fe-20230322 Received: from mtkmbs11n2.mediatek.inc [(172.21.101.187)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 808491197; Wed, 22 Mar 2023 20:45:56 +0800 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by mtkmbs13n2.mediatek.inc (172.21.101.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Wed, 22 Mar 2023 20:45:55 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Wed, 22 Mar 2023 20:45:55 +0800 From: Mark-PK Tsai To: Marc Zyngier , Thomas Gleixner , Matthias Brugger CC: Johan Hovold , , Dmitry Torokhov , Jon Hunter , Sasha Levin , Mark-PK Tsai , , , Subject: [PATCH 5.4 3/3] irqdomain: Fix mapping-creation race Date: Wed, 22 Mar 2023 20:45:50 +0800 Message-ID: <20230322124550.29812-3-mark-pk.tsai@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230322124550.29812-1-mark-pk.tsai@mediatek.com> References: <20230322124550.29812-1-mark-pk.tsai@mediatek.com> MIME-Version: 1.0 X-MTK: N X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS, UNPARSEABLE_RELAY,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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?1761072803792950623?= X-GMAIL-MSGID: =?utf-8?q?1761072803792950623?= From: Johan Hovold [ Upstream commit 601363cc08da25747feb87c55573dd54de91d66a ] Parallel probing of devices that share interrupts (e.g. when a driver uses asynchronous probing) can currently result in two mappings for the same hardware interrupt to be created due to missing serialisation. Make sure to hold the irq_domain_mutex when creating mappings so that looking for an existing mapping before creating a new one is done atomically. Fixes: 765230b5f084 ("driver-core: add asynchronous probing support for drivers") Fixes: b62b2cf5759b ("irqdomain: Fix handling of type settings for existing mappings") Link: https://lore.kernel.org/r/YuJXMHoT4ijUxnRb@hovoldconsulting.com Cc: stable@vger.kernel.org # 4.8 Cc: Dmitry Torokhov Cc: Jon Hunter Tested-by: Hsin-Yi Wang Tested-by: Mark-PK Tsai Signed-off-by: Johan Hovold Signed-off-by: Marc Zyngier Link: https://lore.kernel.org/r/20230213104302.17307-7-johan+linaro@kernel.org Signed-off-by: Sasha Levin Signed-off-by: Mark-PK Tsai --- kernel/irq/irqdomain.c | 64 ++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/kernel/irq/irqdomain.c b/kernel/irq/irqdomain.c index 1eb34eaeaa94..a5f1dd7b6dc3 100644 --- a/kernel/irq/irqdomain.c +++ b/kernel/irq/irqdomain.c @@ -25,6 +25,9 @@ static DEFINE_MUTEX(irq_domain_mutex); static struct irq_domain *irq_default_domain; +static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base, + unsigned int nr_irqs, int node, void *arg, + bool realloc, const struct irq_affinity_desc *affinity); static void irq_domain_check_hierarchy(struct irq_domain *domain); struct irqchip_fwid { @@ -672,9 +675,9 @@ unsigned int irq_create_direct_mapping(struct irq_domain *domain) } EXPORT_SYMBOL_GPL(irq_create_direct_mapping); -static unsigned int __irq_create_mapping_affinity(struct irq_domain *domain, - irq_hw_number_t hwirq, - const struct irq_affinity_desc *affinity) +static unsigned int irq_create_mapping_affinity_locked(struct irq_domain *domain, + irq_hw_number_t hwirq, + const struct irq_affinity_desc *affinity) { struct device_node *of_node = irq_domain_get_of_node(domain); int virq; @@ -689,7 +692,7 @@ static unsigned int __irq_create_mapping_affinity(struct irq_domain *domain, return 0; } - if (irq_domain_associate(domain, virq, hwirq)) { + if (irq_domain_associate_locked(domain, virq, hwirq)) { irq_free_desc(virq); return 0; } @@ -725,14 +728,20 @@ unsigned int irq_create_mapping_affinity(struct irq_domain *domain, return 0; } + mutex_lock(&irq_domain_mutex); + /* Check if mapping already exists */ virq = irq_find_mapping(domain, hwirq); if (virq) { pr_debug("existing mapping on virq %d\n", virq); - return virq; + goto out; } - return __irq_create_mapping_affinity(domain, hwirq, affinity); + virq = irq_create_mapping_affinity_locked(domain, hwirq, affinity); +out: + mutex_unlock(&irq_domain_mutex); + + return virq; } EXPORT_SYMBOL_GPL(irq_create_mapping_affinity); @@ -834,6 +843,8 @@ unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec) if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK)) type &= IRQ_TYPE_SENSE_MASK; + mutex_lock(&irq_domain_mutex); + /* * If we've already configured this interrupt, * don't do it again, or hell will break loose. @@ -846,7 +857,7 @@ unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec) * interrupt number. */ if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq)) - return virq; + goto out; /* * If the trigger type has not been set yet, then set @@ -854,35 +865,45 @@ unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec) */ if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) { irq_data = irq_get_irq_data(virq); - if (!irq_data) - return 0; + if (!irq_data) { + virq = 0; + goto out; + } irqd_set_trigger_type(irq_data, type); - return virq; + goto out; } pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n", hwirq, of_node_full_name(to_of_node(fwspec->fwnode))); - return 0; + virq = 0; + goto out; } if (irq_domain_is_hierarchy(domain)) { - virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec); - if (virq <= 0) - return 0; + virq = irq_domain_alloc_irqs_locked(domain, -1, 1, NUMA_NO_NODE, + fwspec, false, NULL); + if (virq <= 0) { + virq = 0; + goto out; + } } else { /* Create mapping */ - virq = __irq_create_mapping_affinity(domain, hwirq, NULL); + virq = irq_create_mapping_affinity_locked(domain, hwirq, NULL); if (!virq) - return virq; + goto out; } irq_data = irq_get_irq_data(virq); - if (WARN_ON(!irq_data)) - return 0; + if (WARN_ON(!irq_data)) { + virq = 0; + goto out; + } /* Store trigger type */ irqd_set_trigger_type(irq_data, type); +out: + mutex_unlock(&irq_domain_mutex); return virq; } @@ -1788,6 +1809,13 @@ void irq_domain_set_info(struct irq_domain *domain, unsigned int virq, irq_set_handler_data(virq, handler_data); } +static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base, + unsigned int nr_irqs, int node, void *arg, + bool realloc, const struct irq_affinity_desc *affinity) +{ + return -EINVAL; +} + static void irq_domain_check_hierarchy(struct irq_domain *domain) { }