Received: by 10.223.185.116 with SMTP id b49csp819038wrg; Sat, 3 Mar 2018 08:11:03 -0800 (PST) X-Google-Smtp-Source: AG47ELtoNOAP7kS9Gu7cO0n8tQWRp/AiNq/2sxIJjLT3zMfzo9Y2Nya0miATon3rjNBv8WW4vhLW X-Received: by 2002:a17:902:7d93:: with SMTP id a19-v6mr4721662plm.160.1520093463603; Sat, 03 Mar 2018 08:11:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520093463; cv=none; d=google.com; s=arc-20160816; b=E/s4H53TQz1j/87GDDdUWI51U+sWh2bKp9LsaKTMKykVRYKFmp/jIgIyAVqb3+f4+O nZkqXmdUIp1CfJ5MaSru2M5TnNj70KaN24uIfxBeV+ty84mZxe8W+y4QSOOv+4kQmG8H EYQW4+p2nLTtNDfpAKbs1cG8nOI42b6LhP8uhMw8M+AfkQh5i+7izAyFxk8viVy+avhU c5EPMHiUF9OiKYN4/+FTM2oBEjheHlyU/U2UYGDXVxJ1plUj1wbcpa20XHJ75tImaWb+ 89/NOFwcWEcILQ3MNLIqaYbGbIynhQ5OhQFAdOsEnUERmAbAsZhYPFOA0F2q7t2SWn9f PKsQ== 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:msip_labels :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=IO4GbDmLvDsRM91EIhrkanqlNtm0ZdbmiQsNWLrK1dM=; b=ReFqK/bqH5SR/3rKCcOHlfrfcD4CRo4dxTb+VYhomDjNID3mS975udSoswjSbulmLu +Nnnx3WH4+ntZ0z6D46HiwfXtLYawbuXRMY6usaJh6Oo/+F4SE2GfHdpavo86WAfIi1N NQCI2OG/18i46Fzd/DiaQBgw/8R0vIrqcTGp/2d35CFbUqkg5NT3yXQ4HVM4tfk2WQzH oJyI+J3b7nv7mKrbVbuhepqehKy4XJTVGoZGnXHXOdryMD/kySXstejaOGX/pT14K/4j Zq4zfww/Tbf4TSKCk+G0R+kJezv1FK0IWRsMphYgR8nf6G0MesbaKTreObptkdYllezA Jynw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=CI+qVFFC; 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 h2si5672417pgv.201.2018.03.03.08.10.48; Sat, 03 Mar 2018 08:11:03 -0800 (PST) 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=CI+qVFFC; 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 S932141AbeCCQJv (ORCPT + 99 others); Sat, 3 Mar 2018 11:09:51 -0500 Received: from mail-cys01nam02on0125.outbound.protection.outlook.com ([104.47.37.125]:22304 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932111AbeCCQJt (ORCPT ); Sat, 3 Mar 2018 11:09:49 -0500 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=IO4GbDmLvDsRM91EIhrkanqlNtm0ZdbmiQsNWLrK1dM=; b=CI+qVFFCU+WARSrZ1qRGn5UMaTkUzH5wF7futESjjV1HNaUrOuNp6mL8iVDkR6AZvA6CR2jqDNHKR3Wfv3Ak1yv4HRnOW5dYEv+iURdUevOsw5id2sqCQ+GM0pKCo4prbRdu9eupok8MZBNQgcvUlgmk5PF3qJ9MwrO+J15yJkQ= Received: from DM5PR2101MB1030.namprd21.prod.outlook.com (52.132.128.11) by DM5PR2101MB0886.namprd21.prod.outlook.com (52.132.132.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.567.2; Sat, 3 Mar 2018 16:09:46 +0000 Received: from DM5PR2101MB1030.namprd21.prod.outlook.com ([fe80::14f:785d:71f2:d396]) by DM5PR2101MB1030.namprd21.prod.outlook.com ([fe80::14f:785d:71f2:d396%2]) with mapi id 15.20.0567.006; Sat, 3 Mar 2018 16:09:46 +0000 From: "Michael Kelley (EOSG)" To: Dexuan Cui , "bhelgaas@google.com" , "linux-pci@vger.kernel.org" , KY Srinivasan , Stephen Hemminger CC: "linux-kernel@vger.kernel.org" , "driverdev-devel@linuxdriverproject.org" , Haiyang Zhang , "olaf@aepfle.de" , "apw@canonical.com" , "jasowang@redhat.com" , "vkuznets@redhat.com" , "marcelo.cerri@canonical.com" , Dexuan Cui , Jack Morgenstein , "stable@vger.kernel.org" Subject: RE: [PATCH 2/3] PCI: hv: serialize the present/eject work items Thread-Topic: [PATCH 2/3] PCI: hv: serialize the present/eject work items Thread-Index: AQHTsoWGFBWjb5SxZ0KePr9iZ4nXX6O+pZGw Date: Sat, 3 Mar 2018 16:09:46 +0000 Message-ID: References: <20180303001947.20564-1-decui@microsoft.com> <20180303001947.20564-2-decui@microsoft.com> In-Reply-To: <20180303001947.20564-2-decui@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_Enabled=True; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_SiteId=72f988bf-86f1-41af-91ab-2d7cd011db47; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_Owner=mikelley@ntdev.microsoft.com; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_SetDate=2018-03-03T16:09:44.3000079Z; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_Name=General; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_Application=Microsoft Azure Information Protection; MSIP_Label_f42aa342-8706-4288-bd11-ebb85995028c_Extended_MSFT_Method=Automatic; Sensitivity=General x-originating-ip: [24.22.167.197] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1;DM5PR2101MB0886;7:jyo/tXMn/37H+yVSxt6KJaPygdXGMP7muIFM2s08+YQU0DPQWj4+ebZYA1TcSguWdby5b4vWNjXlY/PQnot4EEI5niA/0/uO/W5rcjP4eFnJZmVRxE6Jf5liUHmnfbvnPvTNpzzWL1r5Atz/udHoeZ2laZHXRKbigxnAUQSN31zkOhiWbmEpcU3KR2GR0V/VTPL9uUcb+100DINPUgVCJuvjzqRRW/Tw2A3AUjcrQVUL8FkFlM138Fa97OWuCMqG x-ms-exchange-antispam-srfa-diagnostics: SOS; x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 40931249-6c1a-46e1-321d-08d581212ed9 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(2017052603307)(7193020);SRVR:DM5PR2101MB0886; x-ms-traffictypediagnostic: DM5PR2101MB0886: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Michael.H.Kelley@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(9452136761055)(211936372134217)(198206253151910); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040501)(2401047)(5005006)(8121501046)(10201501046)(3002001)(3231220)(944501244)(52105095)(93006095)(93001095)(6055026)(61426038)(61427038)(6041288)(20161123560045)(20161123558120)(20161123562045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(6072148)(201708071742011);SRVR:DM5PR2101MB0886;BCL:0;PCL:0;RULEID:;SRVR:DM5PR2101MB0886; x-forefront-prvs: 0600F93FE1 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(39380400002)(346002)(39860400002)(376002)(396003)(366004)(189003)(199004)(13464003)(14454004)(59450400001)(6116002)(33656002)(305945005)(6246003)(76176011)(2501003)(7416002)(7696005)(72206003)(53546011)(6506007)(102836004)(6436002)(99286004)(26005)(86612001)(9686003)(74316002)(5250100002)(55016002)(2906002)(478600001)(22452003)(53936002)(7736002)(5660300001)(3846002)(3280700002)(10290500003)(2900100001)(66066001)(86362001)(10090500001)(316002)(54906003)(110136005)(8990500004)(6636002)(106356001)(2201001)(1511001)(229853002)(68736007)(97736004)(8676002)(2950100002)(25786009)(8936002)(105586002)(4326008)(3660700001)(81156014)(81166006);DIR:OUT;SFP:1102;SCL:1;SRVR:DM5PR2101MB0886;H:DM5PR2101MB1030.namprd21.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: pFdqgI7bsuZK6Xh+L3vMk/4CsDlvYJfhuMKPIigGuUFcxrh7UbS3hKeDU9TPcc/kTrqvajdOAhaTRka0Tug3/lSbXGzJ5e/FxheWUixBmuZaey98W8/Z4rmRcBegXNt7qbv/zls8mc9MF9P8mvn5a9uPsdC0CTD1bIBuBjGOd/E= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 40931249-6c1a-46e1-321d-08d581212ed9 X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Mar 2018 16:09:46.2058 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB0886 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > -----Original Message----- > From: linux-kernel-owner@vger.kernel.org On Behalf > Of Dexuan Cui > Sent: Friday, March 2, 2018 4:21 PM > To: bhelgaas@google.com; linux-pci@vger.kernel.org; KY Srinivasan ; > Stephen Hemminger > Cc: linux-kernel@vger.kernel.org; driverdev-devel@linuxdriverproject.org;= Haiyang Zhang > ; olaf@aepfle.de; apw@canonical.com; jasowang@red= hat.com; > vkuznets@redhat.com; marcelo.cerri@canonical.com; Dexuan Cui ; > Jack Morgenstein ; stable@vger.kernel.org > Subject: [PATCH 2/3] PCI: hv: serialize the present/eject work items >=20 > When we hot-remove the device, we first receive a PCI_EJECT message and > then receive a PCI_BUS_RELATIONS message with bus_rel->device_count =3D= =3D 0. >=20 > The first message is offloaded to hv_eject_device_work(), and the second > is offloaded to pci_devices_present_work(). Both the paths can be running > list_del(&hpdev->list_entry), causing general protection fault, because > system_wq can run them concurrently. >=20 > The patch eliminates the race condition. With this patch, the enum_sem field in struct hv_pcibus_device is no longer needed. The semaphore serializes execution in hv_pci_devices_present_work(), and that serialization is now done with the ordered workqueue. Also, the last paragraph of the top level comment for hv_pci_devices_present_work() should be updated to reflect the new ordering assumptions. Separately, an unrelated bug: At the top of hv_eject_device_work(), the first test may do a put_pcichild() and return. This exit path also needs to do put_hvpcibus() to balance the ref counts, or do a goto the last two lines at the bottom of the function. >=20 > Signed-off-by: Dexuan Cui > Tested-by: Adrian Suhov > Tested-by: Chris Valean > Cc: Vitaly Kuznetsov > Cc: Jack Morgenstein > Cc: stable@vger.kernel.org > Cc: Stephen Hemminger > Cc: K. Y. Srinivasan > --- > drivers/pci/host/pci-hyperv.c | 19 ++++++++++++++++--- > 1 file changed, 16 insertions(+), 3 deletions(-) >=20 > diff --git a/drivers/pci/host/pci-hyperv.c b/drivers/pci/host/pci-hyperv.= c > index 1233300f41c6..57b1fb3ebdb9 100644 > --- a/drivers/pci/host/pci-hyperv.c > +++ b/drivers/pci/host/pci-hyperv.c > @@ -461,6 +461,8 @@ struct hv_pcibus_device { > struct retarget_msi_interrupt retarget_msi_interrupt_params; >=20 > spinlock_t retarget_msi_interrupt_lock; > + > + struct workqueue_struct *wq; > }; >=20 > /* > @@ -1770,7 +1772,7 @@ static void hv_pci_devices_present(struct hv_pcibus= _device *hbus, > spin_unlock_irqrestore(&hbus->device_list_lock, flags); >=20 > get_hvpcibus(hbus); > - schedule_work(&dr_wrk->wrk); > + queue_work(hbus->wq, &dr_wrk->wrk); This invocation of get_hvpcibus() and queue_work() could be made conditional on whether the preceding list_add_tail() transitioned the list from empty to non-empty. If the list was already non-empty, a previously queued invocation of hv_pci_devices_present_work() will find the new entry and process it. But this is an optimization in a non-perf sensitive code path, so may not be worth it. > } >=20 > /** > @@ -1848,7 +1850,7 @@ static void hv_pci_eject_device(struct hv_pci_dev *= hpdev) > get_pcichild(hpdev, hv_pcidev_ref_pnp); > INIT_WORK(&hpdev->wrk, hv_eject_device_work); > get_hvpcibus(hpdev->hbus); > - schedule_work(&hpdev->wrk); > + queue_work(hpdev->hbus->wq, &hpdev->wrk); > } >=20 > /** > @@ -2463,11 +2465,17 @@ static int hv_pci_probe(struct hv_device *hdev, > spin_lock_init(&hbus->retarget_msi_interrupt_lock); > sema_init(&hbus->enum_sem, 1); > init_completion(&hbus->remove_event); > + hbus->wq =3D alloc_ordered_workqueue("hv_pci_%x", 0, > + hbus->sysdata.domain); > + if (!hbus->wq) { > + ret =3D -ENOMEM; > + goto free_bus; > + } >=20 > ret =3D vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0= , > hv_pci_onchannelcallback, hbus); > if (ret) > - goto free_bus; > + goto destroy_wq; >=20 > hv_set_drvdata(hdev, hbus); >=20 > @@ -2536,6 +2544,9 @@ static int hv_pci_probe(struct hv_device *hdev, > hv_free_config_window(hbus); > close: > vmbus_close(hdev->channel); > +destroy_wq: > + drain_workqueue(hbus->wq); The drain_workqueue() call isn't necessary. destroy_workqueue() calls drain_workqueue() and there better not be anything in the workqueue anyway since all the ref counts are zero. > + destroy_workqueue(hbus->wq); > free_bus: > free_page((unsigned long)hbus); > return ret; > @@ -2615,6 +2626,8 @@ static int hv_pci_remove(struct hv_device *hdev) > irq_domain_free_fwnode(hbus->sysdata.fwnode); > put_hvpcibus(hbus); > wait_for_completion(&hbus->remove_event); > + drain_workqueue(hbus->wq); Same here -- drain_workqueue() isn't needed. The workqueue must be empty anyway since the remove_event has completed and the ref counts will all be zero. > + destroy_workqueue(hbus->wq); > free_page((unsigned long)hbus); > return 0; > } > -- > 2.7.4