From patchwork Sun May 19 22:28:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haiyang Zhang X-Patchwork-Id: 10949769 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2EF661398 for ; Sun, 19 May 2019 22:28:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1D20D28434 for ; Sun, 19 May 2019 22:28:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 10EFD285BE; Sun, 19 May 2019 22:28:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1216028434 for ; Sun, 19 May 2019 22:28:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726901AbfESW2m (ORCPT ); Sun, 19 May 2019 18:28:42 -0400 Received: from mail-eopbgr760123.outbound.protection.outlook.com ([40.107.76.123]:40590 "EHLO NAM02-CY1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726090AbfESW2l (ORCPT ); Sun, 19 May 2019 18:28:41 -0400 ARC-Seal: i=1; a=rsa-sha256; s=testarcselector01; d=microsoft.com; cv=none; b=rEMp14igXQgK7DCdnuWvF6qPMMd6nhZjEMwxhbMYAtw+moZQ48LSGZQY7iADumeaUQQ7KPkStVJaOSLwb5obsQQOobvmj5PEVsX7Y8G6KTeXO65sqkEHjmTJkSnVfmRyt1vIc9cFJ+AUepIZdmRRnzTojKu6pj9qRmPrp++ZUO0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=testarcselector01; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XIY2Wuf7nQ2iUXq4J0g5adPbzJJzHb7VWLYnHkwg8Lk=; b=XI9NI5L/eWqRsc8QzdLI/akDLSUqJImIvirK52NEvb4l7cFwlZBu/JaWpxCIfpgqc6eI7+fHuVQmETMgJU4f/b9zHhakmot18mviAzfEumQ1wBHxUAVqQruwQS+H5x/3uftw55Tr3ZbC4zlD8f4soy3vHgCJDi8ldVy1ghTS7s4= ARC-Authentication-Results: i=1; test.office365.com 1;spf=none;dmarc=none;dkim=none;arc=none 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:X-MS-Exchange-SenderADCheck; bh=XIY2Wuf7nQ2iUXq4J0g5adPbzJJzHb7VWLYnHkwg8Lk=; b=CxAqMQm6KBx+WCcQRwYYcs781duXLtKp/7bNXdPAOF+t9/+TrleleLYPU9x/LeGN+i2RCM/rcUEgp7rkZ4DZH1SIo35oXrlXP5rl2MTEqzGEVikLLiRGjxaNMA4MexsKOn32rMnk4m74Wbw0gzgkCoiz4uMQ8C5IyKZIF+q9aiA= Received: from BYAPR21MB1240.namprd21.prod.outlook.com (2603:10b6:a03:108::12) by BYAPR21MB1238.namprd21.prod.outlook.com (2603:10b6:a03:107::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1922.6; Sun, 19 May 2019 22:28:37 +0000 Received: from BYAPR21MB1240.namprd21.prod.outlook.com ([fe80::4d4f:7413:1ec:d039]) by BYAPR21MB1240.namprd21.prod.outlook.com ([fe80::4d4f:7413:1ec:d039%8]) with mapi id 15.20.1922.013; Sun, 19 May 2019 22:28:37 +0000 From: Haiyang Zhang To: "sashal@kernel.org" , "bhelgaas@google.com" , "lorenzo.pieralisi@arm.com" , "linux-hyperv@vger.kernel.org" , "linux-pci@vger.kernel.org" CC: Haiyang Zhang , KY Srinivasan , Stephen Hemminger , "olaf@aepfle.de" , vkuznets , "linux-kernel@vger.kernel.org" Subject: [PATCH] PCI: hv: Detect and fix Hyper-V PCI domain number collision Thread-Topic: [PATCH] PCI: hv: Detect and fix Hyper-V PCI domain number collision Thread-Index: AQHVDpIz+mZnowKmCU+oYiS1gYdC2A== Date: Sun, 19 May 2019 22:28:37 +0000 Message-ID: <1558304821-36038-1-git-send-email-haiyangz@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: CO2PR18CA0046.namprd18.prod.outlook.com (2603:10b6:104:2::14) To BYAPR21MB1240.namprd21.prod.outlook.com (2603:10b6:a03:108::12) authentication-results: spf=none (sender IP is ) smtp.mailfrom=lkmlhyz@microsoft.com; x-ms-exchange-messagesentrepresentingtype: 2 x-mailer: git-send-email 1.8.3.1 x-originating-ip: [13.77.154.182] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 6b51ce76-2bd2-4892-117e-08d6dca9558e x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600141)(711020)(4605104)(4618075)(2017052603328)(7193020);SRVR:BYAPR21MB1238; x-ms-traffictypediagnostic: BYAPR21MB1238: x-ld-processed: 72f988bf-86f1-41af-91ab-2d7cd011db47,ExtAddr x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:3044; x-forefront-prvs: 00429279BA x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(136003)(39860400002)(396003)(346002)(376002)(366004)(199004)(189003)(2501003)(6116002)(186003)(3846002)(25786009)(5660300002)(10290500003)(22452003)(2201001)(71200400001)(4720700003)(8676002)(4326008)(478600001)(86612001)(52396003)(2906002)(6512007)(99286004)(486006)(71190400001)(6486002)(52116002)(316002)(110136005)(73956011)(54906003)(386003)(66476007)(68736007)(66556008)(66066001)(6506007)(50226002)(14444005)(64756008)(256004)(26005)(14454004)(66446008)(476003)(2616005)(81156014)(36756003)(102836004)(10090500001)(8936002)(305945005)(81166006)(7846003)(66946007)(6392003)(7736002)(6436002)(53936002);DIR:OUT;SFP:1102;SCL:1;SRVR:BYAPR21MB1238;H:BYAPR21MB1240.namprd21.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: vmhUBJ1fF9SgD8DXxo7vjlNaDe+OKk3jh4TzA7HdK3ssK5AsOw0/qNjTqqIbCAzXWrmtJ9nWRZ/iQLWS6z4tZhUX3uj8aO85xxLALX5emwst5e3llFcJwuEG1+St/+zXqMYf/6aAcXiTaUt31qJi/thEfok2ySEtmCHbZ3Ev0qZjFAxxGzp8Zyg3ffP2gvUH8zUxNcy190sXuubIO9mLKki0wBLTpKXk3uw95sjQ0UZ3Kj1sSGCT9HmKEG3FyKsusfdap4MHxREM8eHVXKAPVXmCOs9VXY3nU6yiYsDS7KGdp1+hCMnj7cP2dsdoj168aZG9GbXHSD92o/4EqQCrkDDabQ3DlyQ5O4ziO+H/yHPrFhZJCj1yPCN2lSbyN1KCfikEFyAckUMoQTcgKAg8ESRFGx/X068xnQJ4RiB7vIA= MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6b51ce76-2bd2-4892-117e-08d6dca9558e X-MS-Exchange-CrossTenant-originalarrivaltime: 19 May 2019 22:28:37.2395 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: lkmlhyz@microsoft.com X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR21MB1238 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Due to Azure host agent settings, the device instance ID's bytes 8 and 9 are no longer unique. This causes some of the PCI devices not showing up in VMs with multiple passthrough devices, such as GPUs. So, as recommended by Azure host team, we now use the bytes 4 and 5 which usually provide unique numbers. In the rare cases of collision, we will detect and find another number that is not in use. Thanks to Michael Kelley for proposing this idea. Signed-off-by: Haiyang Zhang Reviewed-by: Dexuan Cui --- drivers/pci/controller/pci-hyperv.c | 91 +++++++++++++++++++++++++++++++------ 1 file changed, 78 insertions(+), 13 deletions(-) diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c index 82acd61..6b9cc6e60a 100644 --- a/drivers/pci/controller/pci-hyperv.c +++ b/drivers/pci/controller/pci-hyperv.c @@ -37,6 +37,8 @@ * the PCI back-end driver in Hyper-V. */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + #include #include #include @@ -2507,6 +2509,47 @@ static void put_hvpcibus(struct hv_pcibus_device *hbus) complete(&hbus->remove_event); } +#define HVPCI_DOM_MAP_SIZE (64 * 1024) +static DECLARE_BITMAP(hvpci_dom_map, HVPCI_DOM_MAP_SIZE); + +/* PCI domain number 0 is used by emulated devices on Gen1 VMs, so define 0 + * as invalid for passthrough PCI devices of this driver. + */ +#define HVPCI_DOM_INVALID 0 + +/** + * hv_get_dom_num() - Get a valid PCI domain number + * Check if the PCI domain number is in use, and return another number if + * it is in use. + * + * @dom: Requested domain number + * + * return: domain number on success, HVPCI_DOM_INVALID on failure + */ +static u16 hv_get_dom_num(u16 dom) +{ + unsigned int i; + + if (test_and_set_bit(dom, hvpci_dom_map) == 0) + return dom; + + for_each_clear_bit(i, hvpci_dom_map, HVPCI_DOM_MAP_SIZE) { + if (test_and_set_bit(i, hvpci_dom_map) == 0) + return i; + } + + return HVPCI_DOM_INVALID; +} + +/** + * hv_put_dom_num() - Mark the PCI domain number as free + * @dom: Domain number to be freed + */ +static void hv_put_dom_num(u16 dom) +{ + clear_bit(dom, hvpci_dom_map); +} + /** * hv_pci_probe() - New VMBus channel probe, for a root PCI bus * @hdev: VMBus's tracking struct for this root PCI bus @@ -2518,6 +2561,7 @@ static int hv_pci_probe(struct hv_device *hdev, const struct hv_vmbus_device_id *dev_id) { struct hv_pcibus_device *hbus; + u16 dom_req, dom; int ret; /* @@ -2532,19 +2576,32 @@ static int hv_pci_probe(struct hv_device *hdev, hbus->state = hv_pcibus_init; /* - * The PCI bus "domain" is what is called "segment" in ACPI and - * other specs. Pull it from the instance ID, to get something - * unique. Bytes 8 and 9 are what is used in Windows guests, so - * do the same thing for consistency. Note that, since this code - * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee - * that (1) the only domain in use for something that looks like - * a physical PCI bus (which is actually emulated by the - * hypervisor) is domain 0 and (2) there will be no overlap - * between domains derived from these instance IDs in the same - * VM. + * The PCI bus "domain" is what is called "segment" in ACPI and other + * specs. Pull it from the instance ID, to get something usually + * unique. In rare cases of collision, we will find out another number + * not in use. + * Note that, since this code only runs in a Hyper-V VM, Hyper-V + * together with this guest driver can guarantee that (1) The only + * domain used by Gen1 VMs for something that looks like a physical + * PCI bus (which is actually emulated by the hypervisor) is domain 0. + * (2) There will be no overlap between domains (after fixing possible + * collisions) in the same VM. */ - hbus->sysdata.domain = hdev->dev_instance.b[9] | - hdev->dev_instance.b[8] << 8; + dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4]; + dom = hv_get_dom_num(dom_req); + + if (dom == HVPCI_DOM_INVALID) { + pr_err("Unable to use dom# 0x%hx or other numbers", + dom_req); + ret = -EINVAL; + goto free_bus; + } + + if (dom != dom_req) + pr_info("PCI dom# 0x%hx has collision, using 0x%hx", + dom_req, dom); + + hbus->sysdata.domain = dom; hbus->hdev = hdev; refcount_set(&hbus->remove_lock, 1); @@ -2559,7 +2616,7 @@ static int hv_pci_probe(struct hv_device *hdev, hbus->sysdata.domain); if (!hbus->wq) { ret = -ENOMEM; - goto free_bus; + goto free_dom; } ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0, @@ -2636,6 +2693,8 @@ static int hv_pci_probe(struct hv_device *hdev, vmbus_close(hdev->channel); destroy_wq: destroy_workqueue(hbus->wq); +free_dom: + hv_put_dom_num(hbus->sysdata.domain); free_bus: free_page((unsigned long)hbus); return ret; @@ -2717,6 +2776,9 @@ static int hv_pci_remove(struct hv_device *hdev) put_hvpcibus(hbus); wait_for_completion(&hbus->remove_event); destroy_workqueue(hbus->wq); + + hv_put_dom_num(hbus->sysdata.domain); + free_page((unsigned long)hbus); return 0; } @@ -2744,6 +2806,9 @@ static void __exit exit_hv_pci_drv(void) static int __init init_hv_pci_drv(void) { + /* Set the invalid domain number's bit, so it will not be used */ + set_bit(HVPCI_DOM_INVALID, hvpci_dom_map); + return vmbus_driver_register(&hv_pci_drv); }