Received: by 10.213.65.68 with SMTP id h4csp2119764imn; Sun, 8 Apr 2018 20:11:37 -0700 (PDT) X-Google-Smtp-Source: AIpwx49ChMNK18HHwQ+LExq3zsbyMjAEqDX3EncBH0E41H6jE1OOvSfT+oKufk/N5De/677gnhHM X-Received: by 10.101.89.6 with SMTP id f6mr23337675pgu.178.1523243409616; Sun, 08 Apr 2018 20:10:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523243409; cv=none; d=google.com; s=arc-20160816; b=QbBNCAJST0R4z2o2zYdkXGDvAWvY1LdetvuVw8kOP5dDDt1xWXmQ0Ra4mjKUhDHOE8 YHqEPrRgKmn8ki4ZSjedwRaf8jSyFGvZuibe/Mdqi+ODhsGb0hvTwpomp37q2pzzuK5r asPQJwai3vnv0X/ghbCDm4uW5nkvqoBUEbzCY49A2A0SEmcLheGaEgDB/ypJ81G7wmGB +fhVsJM2zY/CnJ0lRQkz3h28P7r0xQQ2/rBvj3T8iMSWnXMKu5SqzjwmX4jH9uI8UB+p 1ufYT/hzmYDm6nHSHO2kvjFzaQdTNjyvBjMBu/gRVrIQkKO5aCVC693YuLy+eqSNv65m fwUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=9TN035mVTl63mIPtNY2WnomuQntZqv2/0BXNZ5++kLA=; b=jMmF/VQCjD5V5HCkU52UyItmFlxeMreRNDs+aRb78kge1cZK3jv+mG9q3R1VZo//aB HHtg8s0IxRtwa376GjC6KNJZ3RO++pZchayVGJRUbtoO4ji4PkKaLapig18FoANlrPDY l5X2poUNjz8p9rPe4bgm2IxRUJbmpujSlYDpMQli8EiodNDgZ+e3Gthw4hSmTHrovrvW ftJ1lSyWjchVxdQwXJTQ7Rz/TOS1ageZOxDuJ4l255V8cr/vvcyvIuW9HGCPMGEe/mGk aBKJYYDd1Ta6Ch60+5Pd3sbt9uW5NyYdSJCjWLQLKR9sTeD2FQobkKTS8hZzxi5MRuHZ 7c3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=e9RzMC6C; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r6-v6si13052544pls.527.2018.04.08.20.09.32; Sun, 08 Apr 2018 20:10:09 -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=@microsoft.com header.s=selector1 header.b=e9RzMC6C; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932728AbeDIBbg (ORCPT + 99 others); Sun, 8 Apr 2018 21:31:36 -0400 Received: from mail-sn1nam02on0132.outbound.protection.outlook.com ([104.47.36.132]:23728 "EHLO NAM02-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1756607AbeDIAfv (ORCPT ); Sun, 8 Apr 2018 20:35:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=9TN035mVTl63mIPtNY2WnomuQntZqv2/0BXNZ5++kLA=; b=e9RzMC6CMR5Xe4hAHKb7qdJcX2ZDK6dbe3NR1N8DhGUSqMFgE6K7hv6uMmzy3NcIAwvD5TimEB/6s0nsgt+PZC/lIRvriarY8eQKdcsHAa17LKI4/2jI4+1dpLNxaHLWJ1NFDDPQWdCf9OAnLwy575xFE5offxxH/Vw4T/VSS2U= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB0725.namprd21.prod.outlook.com (10.167.107.167) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.696.0; Mon, 9 Apr 2018 00:35:49 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::8109:aef0:a777:7059%2]) with mapi id 15.20.0696.003; Mon, 9 Apr 2018 00:35:49 +0000 From: Sasha Levin To: "stable@vger.kernel.org" , "linux-kernel@vger.kernel.org" CC: Christoph Hellwig , Bjorn Helgaas , Sasha Levin Subject: [PATCH AUTOSEL for 4.4 038/162] PCI: Protect pci_error_handlers->reset_notify() usage with device_lock() Thread-Topic: [PATCH AUTOSEL for 4.4 038/162] PCI: Protect pci_error_handlers->reset_notify() usage with device_lock() Thread-Index: AQHTz5mjT5QLJNv2uU+LQ41gaNP04w== Date: Mon, 9 Apr 2018 00:28:10 +0000 Message-ID: <20180409002738.163941-38-alexander.levin@microsoft.com> References: <20180409002738.163941-1-alexander.levin@microsoft.com> In-Reply-To: <20180409002738.163941-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB0725;7:Uh6uLqysISR1h/hH+Fha+9attBDNI1ObnkldNXGkRQ8u0asPFwhF/NWBxANjyUQNiO4ruQ7hJBnTevfxikCQEAi1lwIyKTulnlnmihra5VE2ifxYIbvXZx3FvyYFzK9pqjVmzXNwVwEbIgzVGlg0ukHBsey5j8XMcegwumSDZ5CHz73cqOF2EPbqiPZn9IFGPvcIR1rxk31+2KGvUGzgcYuady3Lgq1TesZ2yteXTg6wg0cyNt+jqpsB8IF4/7tT;20:sMIyZAzSHqbtO8TMMTHCamCP7qncmnKq4QFqX8gxCc/O5L1rXK5WXS6j2SYggqFa1BB8uUFs0XPBvlZ5JVMg98fQHQ8zY65gDaG9zzkALkHkZzHeRalyI6UfMugxYHsw735/NTr0O8k7KMiXzYxMucsxLtdGYJVPPuBC7uXoZQw= x-ms-office365-filtering-ht: Tenant X-MS-Office365-Filtering-Correlation-Id: 90742c00-2f79-403b-6201-08d59db1d794 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020);SRVR:DM5PR2101MB0725; x-ms-traffictypediagnostic: DM5PR2101MB0725: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(211936372134217)(42068640409301)(153496737603132)(146099531331640)(17755550239193); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040522)(2401047)(5005006)(8121501046)(10201501046)(3231221)(944501327)(52105095)(93006095)(93001095)(3002001)(6055026)(61426038)(61427038)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(20161123562045)(20161123558120)(20161123560045)(6072148)(201708071742011);SRVR:DM5PR2101MB0725;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB0725; x-forefront-prvs: 0637FCE711 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39860400002)(396003)(376002)(39380400002)(346002)(366004)(189003)(199004)(54534003)(86612001)(102836004)(5660300001)(14454004)(476003)(68736007)(11346002)(66066001)(446003)(99286004)(76176011)(486006)(10090500001)(3660700001)(110136005)(305945005)(106356001)(316002)(105586002)(3280700002)(59450400001)(3846002)(10290500003)(6116002)(22452003)(6506007)(2616005)(1076002)(7736002)(54906003)(478600001)(2900100001)(2501003)(5250100002)(8936002)(25786009)(81156014)(8676002)(6666003)(6436002)(4326008)(6306002)(26005)(6512007)(6486002)(81166006)(107886003)(53936002)(2906002)(966005)(86362001)(186003)(97736004)(36756003)(72206003)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB0725;H:DM5PR2101MB1032.namprd21.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: YA9xzn0D06pbNvTvrebrqiGi1OZmSOYk1ZjBvmzhqOVX+ktdqsKcGh8KGDL6YUL2ywYvepf7lvKliqnxmVScQIBaw5tNQH1Ipu+RN/4rGRtBGu68Y3eR2YUhcwlWUWpHzGJtNCcqpdZwgjFT+SAuC6/kIufKL/giDciKN+XzAszVCJxnjT0QTOCfpA0/1t6Mr8wOU+rNH47jy5hbSuoh8DHLErB1oXz6dlIgUVUO4g4y8/7X1SULBf4OITosIYwyOQ+j72F3dPTmWgbzR28FVYtPmMMNt17y6ldDl4UWueFIGjmRjGgI0X6Jcfb4hm/TWJTMgGcgRiq9ZYP1J7MBrI1bjeW727lkEqwFYEdqdJLfrtZeylnHZ/Z2G01nnW7Hitk3CQ3clwlwP+Q8pzWlacCOy6UH4LSR+b/55mc3F8I= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 90742c00-2f79-403b-6201-08d59db1d794 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Apr 2018 00:28:10.3955 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0725 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Christoph Hellwig [ Upstream commit b014e96d1abbd67404bbe2018937b46466299e9e ] Every method in struct device_driver or structures derived from it like struct pci_driver MUST provide exclusion vs the driver's ->remove() method, usually by using device_lock(). Protect use of pci_error_handlers->reset_notify() by holding the device lock while calling it. Note: - pci_dev_lock() calls device_lock() in addition to blocking user-space config accesses. - pci_err_handlers->reset_notify() is used inside pci_dev_save_and_disable() and pci_dev_restore(). We could hold the device lock directly in pci_reset_notify(), but we expand the region since we have several calls following each other. Without this, ->reset_notify() may race with ->remove() calls, which can be easily triggered in NVMe. [bhelgaas: changelog, add pci_reset_notify() comment] [bhelgaas: fold in fix from Dan Carpenter : http://lkml.kernel.org/r/20170701135323.x5vaj4e2wcs2mcro@mwanda] Link: http://lkml.kernel.org/r/20170601111039.8913-2-hch@lst.de Reported-by: Rakesh Pandit Tested-by: Rakesh Pandit Signed-off-by: Christoph Hellwig Signed-off-by: Bjorn Helgaas Signed-off-by: Sasha Levin --- drivers/pci/pci.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 295bf1472d02..84854f522291 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -3672,6 +3672,12 @@ static void pci_reset_notify(struct pci_dev *dev, bo= ol prepare) { const struct pci_error_handlers *err_handler =3D dev->driver ? dev->driver->err_handler : NULL; + + /* + * dev->driver->err_handler->reset_notify() is protected against + * races with ->remove() by the device lock, which must be held by + * the caller. + */ if (err_handler && err_handler->reset_notify) err_handler->reset_notify(dev, prepare); } @@ -3807,11 +3813,13 @@ int pci_reset_function(struct pci_dev *dev) if (rc) return rc; =20 + pci_dev_lock(dev); pci_dev_save_and_disable(dev); =20 - rc =3D pci_dev_reset(dev, 0); + rc =3D __pci_dev_reset(dev, 0); =20 pci_dev_restore(dev); + pci_dev_unlock(dev); =20 return rc; } @@ -3831,16 +3839,14 @@ int pci_try_reset_function(struct pci_dev *dev) if (rc) return rc; =20 - pci_dev_save_and_disable(dev); + if (!pci_dev_trylock(dev)) + return -EAGAIN; =20 - if (pci_dev_trylock(dev)) { - rc =3D __pci_dev_reset(dev, 0); - pci_dev_unlock(dev); - } else - rc =3D -EAGAIN; + pci_dev_save_and_disable(dev); + rc =3D __pci_dev_reset(dev, 0); + pci_dev_unlock(dev); =20 pci_dev_restore(dev); - return rc; } EXPORT_SYMBOL_GPL(pci_try_reset_function); @@ -3994,7 +4000,9 @@ static void pci_bus_save_and_disable(struct pci_bus *= bus) struct pci_dev *dev; =20 list_for_each_entry(dev, &bus->devices, bus_list) { + pci_dev_lock(dev); pci_dev_save_and_disable(dev); + pci_dev_unlock(dev); if (dev->subordinate) pci_bus_save_and_disable(dev->subordinate); } @@ -4009,7 +4017,9 @@ static void pci_bus_restore(struct pci_bus *bus) struct pci_dev *dev; =20 list_for_each_entry(dev, &bus->devices, bus_list) { + pci_dev_lock(dev); pci_dev_restore(dev); + pci_dev_unlock(dev); if (dev->subordinate) pci_bus_restore(dev->subordinate); } --=20 2.15.1