Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp473369imn; Thu, 28 Jul 2022 06:29:16 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uhdoDvuyWg5OH2Uuglp4axDBEIY02vqd3rYRlhnw7Ikp9Hr2oMuGtOAvPO2lfkqPo6MNBf X-Received: by 2002:a17:903:191:b0:16c:3d49:b0b3 with SMTP id z17-20020a170903019100b0016c3d49b0b3mr26912105plg.174.1659014956195; Thu, 28 Jul 2022 06:29:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659014956; cv=none; d=google.com; s=arc-20160816; b=pR+U7wsIXLxSqOz+TIkN1DDYMukJjaPjVGqmwuvQ7k6/hj9DixsgU/ydlwuRjZCPxo S8PyAx0lTwAzPmAl2cbP09vJ//8DLDIgYuNDrkmnLHMRla4TJ9qhJ0eUrQbUc5p8MJZL 1vZ9antxOrUH0CB6YwSmItvIgRWjcwJxG66l6RRVCE279U3G49AZPM4L292no1h4WRUc s/jZIgiroK/1Yowjo+ET6g6esYIA1eqi4c3Jl07dUC7WS3Oyw7GQaYVNIbVmsr05ncHo K9j+0fZyiZlWC+3fZ4QSnTiW7LGLUa9ALFkp45aoxCnfHgmPCg6WBwwiQ1fjjlNWoZFO uYsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature; bh=xZG31C6skTudY8fojKHpm7IaHPOBj5dwvHAPfHZzfuc=; b=uY82uOzimYKd7XSLKSiNd0ZI/EWkIuYMfSuVk2gJwrfTDHO81voSaUak/QYEd7VAk5 uVc8v5a7d2jZwsITlCsT4K5lAbhNAnwtdSpR/2iPBHADpMLxnR47rqvYL9Yq7aVwMFep UepYADSaRHsTyYPA4nipnnRlud8SttRQLIPzRP0xo4iveCAUaqe9rSXSmPd03zqWrUqM CeI22BnyxZhHS/mZq36i9rnjrXkZZC+e/caxPg+6mbitzy7W7tfGfm6diWYE0OfjfHGK PI/VePIkAMTxjkf/3/8ylWQuk2sT7Mjmlqlt/Nb5KZYvwbw7SghSsJ48jFtp4YG6hgfA WVrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=T8SMEHy5; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s80-20020a632c53000000b0040cf043ed35si1059239pgs.814.2022.07.28.06.29.00; Thu, 28 Jul 2022 06:29:16 -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=@kernel.org header.s=k20201202 header.b=T8SMEHy5; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239139AbiG1NOc (ORCPT + 99 others); Thu, 28 Jul 2022 09:14:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236620AbiG1NOa (ORCPT ); Thu, 28 Jul 2022 09:14:30 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5ECF82CC8B for ; Thu, 28 Jul 2022 06:14:29 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 17C1DB82444 for ; Thu, 28 Jul 2022 13:14:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C123EC433C1; Thu, 28 Jul 2022 13:14:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1659014066; bh=zXjDJxQ311o3IVYcPBceCf8E6XYgNC1pAoh4HZnuioE=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=T8SMEHy5uY7dVQ7QwcGeWG1wcpquH+oxSmFvuL8AmgememDdM+ytjbKYqhY1hExaF 3KbV0HGxfE/p4Nqu9YGSH0LoeI33q+r75gmwvG3gC33FNhT4joEtCpSchazW1+HiBB /x/xUvhmMKO66LOaA3gDCafvRutA/uBUprp7CjzkLG08QSZGETPZ0YTV+pa/wk6rSG Hre1WvwCqKik/qXRxbVFU6vcvcAiV8/L6luHG8JbzC8u1g5cq2QsF9lVIXqXfICLkv KPepuDBo4v84OWqVsRkIHat340RosZFO9MBFq3Lqw4gMz4crEXY4bYKO/BPKYFmwCv AD9SjQ5YUvYFg== Received: from sofa.misterjones.org ([185.219.108.64] helo=why.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1oH3LM-00Afg5-Bh; Thu, 28 Jul 2022 14:14:24 +0100 Date: Thu, 28 Jul 2022 14:14:24 +0100 Message-ID: <87v8rhwf9r.wl-maz@kernel.org> From: Marc Zyngier To: Johan Hovold Cc: Johan Hovold , Thomas Gleixner , Greg Kroah-Hartman , Rob Herring , linux-kernel@vger.kernel.org, Dmitry Torokhov , Jon Hunter Subject: Re: [PATCH] irqdomain: Fix mapping-creation race In-Reply-To: References: <20220728092710.21190-1-johan+linaro@kernel.org> <87wnbxwj94.wl-maz@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: johan@kernel.org, johan+linaro@kernel.org, tglx@linutronix.de, gregkh@linuxfoundation.org, robh@kernel.org, linux-kernel@vger.kernel.org, dtor@chromium.org, jonathanh@nvidia.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham 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 On Thu, 28 Jul 2022 13:56:41 +0100, Johan Hovold wrote: > > On Thu, Jul 28, 2022 at 12:48:23PM +0100, Marc Zyngier wrote: > > On Thu, 28 Jul 2022 10:27:10 +0100, > > Johan Hovold wrote: > > > > > > Parallel probing (e.g. due to asynchronous probing) of devices that share > > > interrupts can currently result in two mappings for the same hardware > > > interrupt to be created. > > > > And I thought nobody would be using shared interrupts anymore. Turns > > out people are still building braindead HW... :-/ > > > > > > > > Add a serialising mapping mutex so that looking for an existing mapping > > > before creating a new one is done atomically. > > > > > > Note that serialising the lookup and creation in > > > irq_create_mapping_affinity() would have been enough to prevent the > > > duplicate mapping, but that could instead cause > > > irq_create_fwspec_mapping() to fail when there is a race. > > > > > > Fixes: 765230b5f084 ("driver-core: add asynchronous probing support for drivers") > > > Fixes: b62b2cf5759b ("irqdomain: Fix handling of type settings for existing mappings") > > > Cc: Dmitry Torokhov > > > Cc: Jon Hunter > > > Signed-off-by: Johan Hovold > > > --- > > > kernel/irq/irqdomain.c | 46 +++++++++++++++++++++++++++++++----------- > > > 1 file changed, 34 insertions(+), 12 deletions(-) > > > > > > diff --git a/kernel/irq/irqdomain.c b/kernel/irq/irqdomain.c > > > index 8fe1da9614ee..d263a7dd4170 100644 > > > --- a/kernel/irq/irqdomain.c > > > +++ b/kernel/irq/irqdomain.c > > > @@ -22,6 +22,7 @@ > > > > > > static LIST_HEAD(irq_domain_list); > > > static DEFINE_MUTEX(irq_domain_mutex); > > > +static DEFINE_MUTEX(irq_mapping_mutex); > > > > I'd really like to avoid a global mutex. At the very least this should > > be a per-domain mutex, otherwise this will serialise a lot more than > > what is needed. > > Yeah, I considered that too, but wanted to get your comments on this > first. > > Also note that the likewise global irq_domain_mutex (and > sparse_irq_lock) are taken in some of these paths so perhaps using finer > locking won't actually matter that much as this is mostly for parallel > probing. It will be a good opportunity to make the locking suck a bit less, like in irq_domain_associate(). > > > } else { > > > /* Create mapping */ > > > - virq = irq_create_mapping(domain, hwirq); > > > + virq = __irq_create_mapping_affinity(domain, hwirq, NULL); > > > > This rechecks for the existence of the mapping. Surely we can do a bit > > better by rejigging this (admittedly bitrotting) code. > > I'm sure we can. Should I try to fix the race first with a patch like > this one that can potentially be backported, and then see what I can do > about cleaning this up? > > After all it has looked like this for the past eight years since when > this code was first merged. No, let's put the code in shape *first*, then add work on the locking, as it should make the patch simpler. Backports aren't my concern, really. Thanks, M. -- Without deviation from the norm, progress is not possible.