Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1990706ybe; Thu, 12 Sep 2019 02:46:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqzkXW9CDj3ZthFqt2RCTrDtsre5ZCRo35tOHff5+fEEdDUBSxYbIifwuGKgs+kRRPm9BeOD X-Received: by 2002:a17:906:6403:: with SMTP id d3mr24073092ejm.99.1568281607649; Thu, 12 Sep 2019 02:46:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1568281607; cv=pass; d=google.com; s=arc-20160816; b=KAzSp+23LDKmTbBY17e0bDP+9pnAOKJCqNvreJ1HLcgS6nFyV5+Dw8Ru83Ahps6JrL bzysaYQksX8gv00lcagKePpjmwWI4W+R0rJiC+K3HXcH7Zcbk4XiJ1QOYp7JxV/MEDLI 4/0GAutvV/003rVaYr7QoWjOx1b1g7nzcUHU+kW0xr8SKZqN6NyzMqF1EOpj95S7ERl/ gSAnpGAnIh3zxPaoUP2UotB2RlWDIIekcmGS5iJoMhvxYRKAeoRrd+DL1t4Hn+sFmf6Q rD8rBgyOSZBjc/bU6QZW8O/AvCiXY3PZQulYcPDyLAzeMq7j0LQw9k1wxBX79hq9nOlI J3pw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from:dkim-signature; bh=UE+HEtwhHIsezNX6gn/hDBiF4Y2+YR4kyHGqo5ONTkE=; b=aSfUlIFSrI2yp9GANP+R4yYyGoLDnrA6M/CmaZrrr3RMrzCoEkTBPxVvseZ2dibj0K tqNdCA2N9Gj9Ev1NY4NIJbQ+FSabLB1HNYEowK75H7crGF7peSgJBylor2EcAKUFOaAB rgwCh5T1I3as08leg6r357VS7L+UDW90WneDn4/C/m0zNpaGAVuT8HECkBigRD1Llwe2 QYJAU9MPsAsN2qQB39LXU5Xg1/zc1uj+gJ4zGbO9xetvamPmD428u5JgwL56otiUTOQi gnYhbkFxdcZqUaobfqQ0RWm+yI/lXkHR6GFoiR7Np810mbIhmY/9pyxNTJJxFCA3vxWJ hIAg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@nokia.onmicrosoft.com header.s=selector1-nokia-onmicrosoft-com header.b=GRWPuOpP; arc=pass (i=1 spf=pass spfdomain=nokia.com dkim=pass dkdomain=nokia.com dmarc=pass fromdomain=nokia.com); spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nokia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n22si8230607edq.33.2019.09.12.02.46.24; Thu, 12 Sep 2019 02:46:47 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@nokia.onmicrosoft.com header.s=selector1-nokia-onmicrosoft-com header.b=GRWPuOpP; arc=pass (i=1 spf=pass spfdomain=nokia.com dkim=pass dkdomain=nokia.com dmarc=pass fromdomain=nokia.com); spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nokia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730942AbfILJoQ (ORCPT + 99 others); Thu, 12 Sep 2019 05:44:16 -0400 Received: from mail-eopbgr30094.outbound.protection.outlook.com ([40.107.3.94]:55461 "EHLO EUR03-AM5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730835AbfILJoO (ORCPT ); Thu, 12 Sep 2019 05:44:14 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aA+OG/XIFSl08Ww+otKdIwdNuFia0oP2YR/0w/IwyAvfywmyeVqhR0HQXFoba43Q28lzdfOsoIeDKj00LgkcuoDiFRzADlfTppLBcEdCbPEgaHkjAT2rk5B74HncA4Q9N7onG9hHL5rtYavd+KD8Xt3pSJvp85uocGmcNsLQ76FhYvweiNIr24RqZP4nnhkU6AfqIRFvYGYrcXP7jVbALJRgzuS3e6Gb0NULTx43uqKStOO0+ZNlR4ChFwhWpbJ9Qheh9gu8H8pX+jvKhELkfDOULh4gmXXaxlmFLjXYcCG3gcdtJS+D35vO8R0ete7ZmZLIix0YHEyjRu4vY29yPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UE+HEtwhHIsezNX6gn/hDBiF4Y2+YR4kyHGqo5ONTkE=; b=GLO8B1WXENoW4VtFOzGxd3kI0QyJ6fqSPrTU5d8x0uBTng9IrpmWsl8wy4/hw+idgRZkDA1oTi176khdEre4N0ANbEsFj79bWOW1a12RQzTLiucEbAO+g4nsFv1LGQFd/qCtOl8wpmmuG0HCd/vEP9JSgCVW1N3qNUCMWkBFDG3IICNmaNnlNAP582Hvcii0ipvdFo4MXfk0WReP5WfJ/h3wW2p1aUgEK1EK0v9RwVNQ3X8ptdhcuYGpzpWb50g4m/hoCVpP2GqCil5zuOcLcMX4polCLvMNquhw8+ARLbC1G2TF4KSWP0ZoVaHD/857P8xi/7ascSIGmU6UY8QHUg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nokia.com; dmarc=pass action=none header.from=nokia.com; dkim=pass header.d=nokia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nokia.onmicrosoft.com; s=selector1-nokia-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UE+HEtwhHIsezNX6gn/hDBiF4Y2+YR4kyHGqo5ONTkE=; b=GRWPuOpPdWlrKV37hZzW+Qn5YIeCR2PwUBtB4JsE4Nm5NVXXKTe+xztxnu/A0AM2Nu7ONrdyHF1TcW5wEkEzUmK01BxKkJqXsMUBlls0C3aYuKsAkHg84ACGP1ciua/5YDHnuwazQZk+oyTpi2OUHg8oMYo0ya+OQTkiy++Hw3Y= Received: from AM6PR0702MB3527.eurprd07.prod.outlook.com (52.133.24.149) by AM6PR0702MB3573.eurprd07.prod.outlook.com (52.133.19.14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2263.10; Thu, 12 Sep 2019 09:44:10 +0000 Received: from AM6PR0702MB3527.eurprd07.prod.outlook.com ([fe80::892c:2b90:e54f:ab56]) by AM6PR0702MB3527.eurprd07.prod.outlook.com ([fe80::892c:2b90:e54f:ab56%3]) with mapi id 15.20.2263.016; Thu, 12 Sep 2019 09:44:10 +0000 From: "Sverdlin, Alexander (Nokia - DE/Ulm)" To: Marc Zyngier , Thomas Gleixner , "linux-kernel@vger.kernel.org" , Grant Likely CC: "Sverdlin, Alexander (Nokia - DE/Ulm)" , Mark Brown , Jon Hunter , "Glavinic-Pecotic, Matija (EXT - DE/Ulm)" , "Adamski, Krzysztof (Nokia - PL/Wroclaw)" , "stable@vger.kernel.org" Subject: [PATCH 3/3] genirq/irqdomain: Detect type race in irq_create_fwspec_mapping() Thread-Topic: [PATCH 3/3] genirq/irqdomain: Detect type race in irq_create_fwspec_mapping() Thread-Index: AQHVaU6gc4wd5DvxjEeG+LE441tLCA== Date: Thu, 12 Sep 2019 09:44:10 +0000 Message-ID: <20190912094343.5480-4-alexander.sverdlin@nokia.com> References: <20190912094343.5480-1-alexander.sverdlin@nokia.com> In-Reply-To: <20190912094343.5480-1-alexander.sverdlin@nokia.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [131.228.2.21] x-mailer: git-send-email 2.23.0 x-clientproxiedby: HE1PR05CA0342.eurprd05.prod.outlook.com (2603:10a6:7:92::37) To AM6PR0702MB3527.eurprd07.prod.outlook.com (2603:10a6:209:11::21) authentication-results: spf=none (sender IP is ) smtp.mailfrom=alexander.sverdlin@nokia.com; x-ms-exchange-messagesentrepresentingtype: 1 x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 9fc3c94c-aa8d-41e5-aac3-08d73765c351 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600166)(711020)(4605104)(1401327)(4618075)(2017052603328)(7193020);SRVR:AM6PR0702MB3573; x-ms-traffictypediagnostic: AM6PR0702MB3573: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:862; x-forefront-prvs: 01583E185C x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(4636009)(39860400002)(136003)(396003)(366004)(346002)(376002)(199004)(189003)(102836004)(2501003)(76176011)(71190400001)(66066001)(3846002)(50226002)(2616005)(6512007)(446003)(7736002)(66446008)(486006)(476003)(6486002)(6436002)(186003)(1076003)(305945005)(71200400001)(11346002)(14454004)(64756008)(86362001)(8676002)(66476007)(66946007)(99286004)(8936002)(6116002)(53936002)(52116002)(110136005)(36756003)(25786009)(386003)(54906003)(26005)(81156014)(66556008)(2906002)(14444005)(6506007)(256004)(478600001)(4326008)(81166006)(316002)(5660300002);DIR:OUT;SFP:1102;SCL:1;SRVR:AM6PR0702MB3573;H:AM6PR0702MB3527.eurprd07.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: nokia.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: GzVQaQ9xnxDF9MXlJ6Tgif4JC+W4tdDSlTcdquXyv4ZIGF/AD60wLEL1Rsb7SoEGf9cqtvt6qI842nigWRl5KNzKGLLUiAj8l6/PjmgJ/VruPgyhWMQyrHcW4uX0i9vCPIARyCQo9W+gDh6UsbmbGu3xkIBmzOCY8sR2WYpDgFzy9LPHaIWH6d+CbQCeTclgs2PqqdIynPy706pXEsg5wqz7MHXlRdAhdQR4hsV80KcZKpNnyoPXZKQhQDJx+1EL5BzGwGk8VTuhYxfeRCNKWgGRFB0tfDPKV9XPIvPMXb28KI0CdOOFuJaLLNWN5iXo9O3GxUiXJ7cuZAAIYHAvRawy98iSPKFngtMJkcuqGrpfKY2nSI5oFgRm1HseV46tEqUh8U1N8vGQWHoJqghjMGevwLYJ3B/aUum1AP1tMtg= Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: nokia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9fc3c94c-aa8d-41e5-aac3-08d73765c351 X-MS-Exchange-CrossTenant-originalarrivaltime: 12 Sep 2019 09:44:10.5987 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 5d471751-9675-428d-917b-70f44f9630b0 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: Z8zs8wMgHUORkafQt+GETME1BicfxLtjbWyI1gV/IgAkRwQzqbtlLVUcaQIlZsgFhxAC9Ja8XZ1qSsM+o3KChJWlsbm2XM1fEmrs1ZwnkIA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR0702MB3573 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexander Sverdlin irq_create_fwspec_mapping() can race with itself during IRQ trigger type configuration. Possible scenarios include: - Mapping exists, two irq_create_fwspec_mapping() running in parallel do not detect type mismatch, IRQ remains configured with one of the different trigger types randomly - Second call to irq_create_fwspec_mapping() sees existing mapping just created by first call, but earlier irqd_set_trigger_type() call races with later irqd_set_trigger_type() =3D> totally undetected, IRQ type is being set randomly to either one or another type Introduce helper function to detect parallel changes to IRQ type. Cc: stable@vger.kernel.org Signed-off-by: Alexander Sverdlin --- kernel/irq/irqdomain.c | 66 +++++++++++++++++++++++++++++-----------------= ---- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/kernel/irq/irqdomain.c b/kernel/irq/irqdomain.c index 176f2cc..af4d30c 100644 --- a/kernel/irq/irqdomain.c +++ b/kernel/irq/irqdomain.c @@ -764,10 +764,45 @@ static void of_phandle_args_to_fwspec(struct device_n= ode *np, const u32 *args, fwspec->param[i] =3D args[i]; } =20 +/* Detect races during IRQ type setting */ +static int irq_set_trigger_type_locked(unsigned int virq, unsigned int typ= e, + irq_hw_number_t hwirq, + const struct irq_fwspec *fwspec) +{ + struct irq_data *irq_data; + int ret =3D 0; + + mutex_lock(&irq_domain_mutex); + /* + * If the trigger type is not specified or matches the current trigger + * type then we are done. + */ + if (type =3D=3D IRQ_TYPE_NONE || type =3D=3D irq_get_trigger_type(virq)) + goto unlock; + + /* If the trigger type has not been set yet, then set it now */ + if (irq_get_trigger_type(virq) !=3D IRQ_TYPE_NONE) { + pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n", + hwirq, of_node_full_name(to_of_node(fwspec->fwnode))); + ret =3D -EINVAL; + goto unlock; + } + + irq_data =3D irq_get_irq_data(virq); + if (!irq_data) { + ret =3D -ENOENT; + goto unlock; + } + irqd_set_trigger_type(irq_data, type); + +unlock: + mutex_unlock(&irq_domain_mutex); + return ret; +} + unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec) { struct irq_domain *domain; - struct irq_data *irq_data; irq_hw_number_t hwirq; unsigned int type =3D IRQ_TYPE_NONE; int virq; @@ -802,29 +837,8 @@ unsigned int irq_create_fwspec_mapping(struct irq_fwsp= ec *fwspec) */ virq =3D irq_find_mapping(domain, hwirq); if (virq) { - /* - * If the trigger type is not specified or matches the - * current trigger type then we are done so return the - * interrupt number. - */ - if (type =3D=3D IRQ_TYPE_NONE || type =3D=3D irq_get_trigger_type(virq)) - return virq; - - /* - * If the trigger type has not been set yet, then set - * it now and return the interrupt number. - */ - if (irq_get_trigger_type(virq) =3D=3D IRQ_TYPE_NONE) { - irq_data =3D irq_get_irq_data(virq); - if (!irq_data) - return 0; - - irqd_set_trigger_type(irq_data, type); + if (!irq_set_trigger_type_locked(virq, type, hwirq, fwspec)) return virq; - } - - pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n", - hwirq, of_node_full_name(to_of_node(fwspec->fwnode))); return 0; } =20 @@ -839,8 +853,7 @@ unsigned int irq_create_fwspec_mapping(struct irq_fwspe= c *fwspec) return virq; } =20 - irq_data =3D irq_get_irq_data(virq); - if (!irq_data) { + if (irq_set_trigger_type_locked(virq, type, hwirq, fwspec)) { if (irq_domain_is_hierarchy(domain)) irq_domain_free_irqs(virq, 1); else @@ -848,9 +861,6 @@ unsigned int irq_create_fwspec_mapping(struct irq_fwspe= c *fwspec) return 0; } =20 - /* Store trigger type */ - irqd_set_trigger_type(irq_data, type); - return virq; } EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping); --=20 2.4.6