From patchwork Tue Sep 6 07:08:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ajay Kaher X-Patchwork-Id: 12966941 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4F616C6FA83 for ; Tue, 6 Sep 2022 07:09:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233019AbiIFHJR (ORCPT ); Tue, 6 Sep 2022 03:09:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232675AbiIFHJP (ORCPT ); Tue, 6 Sep 2022 03:09:15 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2075.outbound.protection.outlook.com [40.107.94.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0297565663; Tue, 6 Sep 2022 00:09:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dY1wPXHnrMEnxZciiGfS4XSgRUh/IXWahcmOFtN/+OdybMsqd+W9x/lxWaIUsqZoTHsH4rsItI3kf/WVJyG9N4L1/3650PE0b5ekY7q7/TkyYk1kaFTRV71pcqZg0cYrsNOH50wqJfo+3oaq1+hLEdabZ+g0ZXt8ot9tZR/4nLnlzrX7nNcByzU06t59VYkKLaeR5Ca7notRErd3djgctnEd0RhZjM82yjSahbJ+wXD8lh6GP3XvVrWWQZ0NK6ie5ynRxZ4jtoffE5znaOCBXc1E5adrXc9H20HBkP+h+M2P4AsoPbXJZVz0STnp89ZuoKucMcF56QktfLboXeanuA== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=tVqjBGkFgXrs31AA2OHw7MU01gBpvRs6t5y6CM6f2Tg=; b=lOjDBVufiEWaMCHFThqVBpo69/Ca31qNmAVV5ydA1N/za1AnL70f597eaeTUBLXh7c5GOQ8Cx1iP3NgeHOT/gpLn9pkmeyCzmcVRxIDxq3GRLtBeaYyTT+bQZt4vVfZGcjnMHn5XMX5nisIerqCYxNqztaQBm686q3OA1EwFNuEh4Uo0fxQoJN1wvBrLQcVSnIaEJZLsd+aYS94sZ8qLcjGPPwAKg/8L83hf9DmRz+kTHdVJEaVbrIdJxk9/TIuA21lA8Db30Mp7jWNW0m40fbeWAT4dB4iZbyzWe270UXcfEKsTJfKpSGMP7n8rD821EzIvoB4/5Vw7pE3xM0srww== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vmware.com; dmarc=pass action=none header.from=vmware.com; dkim=pass header.d=vmware.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vmware.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=tVqjBGkFgXrs31AA2OHw7MU01gBpvRs6t5y6CM6f2Tg=; b=zn+MsRJlDS55rB9koppwTf9JIKLqRyfwc9/3fsa+EyQq1LsG/Ej88qKLWxqlQ9HMBg9TQdzHm5bHYB8dNb5HZ77UVn+5BWw2vEp6rM6MeB/yDVsMEcgd9VZRx+u8GRb4FhMwPCoBW+1jbfjxu8akK3rfxsEI3r68Bek3vxInxPI= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vmware.com; Received: from PH0PR05MB8703.namprd05.prod.outlook.com (2603:10b6:510:bd::5) by DM6PR05MB4970.namprd05.prod.outlook.com (2603:10b6:5:31::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5612.5; Tue, 6 Sep 2022 07:09:11 +0000 Received: from PH0PR05MB8703.namprd05.prod.outlook.com ([fe80::95af:33a4:b350:c335]) by PH0PR05MB8703.namprd05.prod.outlook.com ([fe80::95af:33a4:b350:c335%8]) with mapi id 15.20.5612.012; Tue, 6 Sep 2022 07:09:11 +0000 From: Ajay Kaher To: helgaas@kernel.org, bhelgaas@google.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com Cc: x86@kernel.org, hpa@zytor.com, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, rostedt@goodmis.org, srivatsab@vmware.com, srivatsa@csail.mit.edu, amakhalov@vmware.com, vsirnapalli@vmware.com, er.ajay.kaher@gmail.com, willy@infradead.org, namit@vmware.com, linux-hyperv@vger.kernel.org, kvm@vger.kernel.org, jailhouse-dev@googlegroups.com, xen-devel@lists.xenproject.org, acrn-dev@lists.projectacrn.org Subject: [PATCH v2] x86/PCI: Prefer MMIO over PIO on VMware hypervisor Date: Tue, 6 Sep 2022 12:38:37 +0530 Message-Id: <1662448117-10807-1-git-send-email-akaher@vmware.com> X-Mailer: git-send-email 2.7.4 X-ClientProxiedBy: BYAPR05CA0061.namprd05.prod.outlook.com (2603:10b6:a03:74::38) To PH0PR05MB8703.namprd05.prod.outlook.com (2603:10b6:510:bd::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH0PR05MB8703:EE_|DM6PR05MB4970:EE_ X-MS-Office365-Filtering-Correlation-Id: 361688ab-6f30-4422-58aa-08da8fd6b2f1 X-LD-Processed: b39138ca-3cee-4b4a-a4d6-cd83d9dd62f0,ExtAddr X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rpEmG/ZTZD41ci8rzga319nfcfWs6F4whtCEkJI1igtHShj/5o1VLFdArTGqtjMdJUQ3j1333o5vV/Px1O0GG2Uj3zj4cq7EBbEGguwtuYnG5WIzK014XG1a0SNRRgHxy+QGhgx78DPW/4hP/rPKdkm46Fq32f/JlnYXfu9fAgz3YWw6gWVRZ1uLE9Wk41ANTglPoqfvyd1FcExNpN3JePJe4sH7NLghX6okgRdiytGbNtziY91ftsvWi29UMKDMINRi6zUmWJZpuv5MPmedTsHEu0m1L1QttboRINoi06f0XUdZXfLYw5MTbIsktHtLW33/6Y46vb4nt2jCe269qOC9cXOy4nQHmZTBL7j4jq4mYUEtRuiKN51HVsLHxts5DVJxTmG3pK/89SJKCisLEDJ6zIC/ib/p3bZ2vvQyJKUfxzcfkErOwElh4SEXu5yJ7swQ8PiEghfIm0g2kc8nFLG3skw8+p01cV3BkoKkxArc7qtWQUDYczpG+TJEs+gZXNp2TfgFvX69zGcuhV16L05T83k9fi5MHBweme39oWZYLD1PXSJ4DBHvkTEXmMrK+OPt6NjIbuCdWOty8rltMwG+zYD4+ZaCzZobICxj/++mWLqADs7AixCOHeN9sNIplsC374NGmwCnH1vmWGSVj29LORst5AuIwRxWUKbF92Q4ee4VRAjEGZOlb+Qd5wTOhuAtFMiJrTWcvOMzkK/VbE/ocOYSloUCw7SUZd4AUcgfrKYFhnkJgpTdnwMIh3oVz6eR6dz2TT9zucNl6YOmTGKhfQFshJnL4y0VEi1NzK3XWey4TwqPevATsZhAuBHb X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH0PR05MB8703.namprd05.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230016)(4636009)(39860400002)(346002)(366004)(376002)(136003)(396003)(38350700002)(38100700002)(4326008)(8676002)(66946007)(66556008)(66476007)(316002)(2906002)(7416002)(26005)(5660300002)(8936002)(6512007)(52116002)(83380400001)(2616005)(186003)(6666004)(41300700001)(6486002)(6506007)(86362001)(36756003)(478600001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?rkatwK4U0iXPSO3KfsiD0wziQJJ3?= =?utf-8?q?pEyUgL6XO4cdd94w2cI2EhfVF7u6rwUgYQ26CA3ugSVodMY2aSX39Cittk5Yi4QNs?= =?utf-8?q?Y5F1MbjrlvWd4y6zY4+LGpFDYskBwedUurhrxxUphQ64cBxv1Hd+4EI8YOOw3E51D?= =?utf-8?q?w9VaRASZgZI85Je+Oq3ajspOhiGIZ6mob+gXzHDsH266/hheZ4pHKTQ4/X4p2jcRJ?= =?utf-8?q?20pzMizlSnQhRAcIYpadKbHSlC4WoHzVN2UqRl2fTDpMIbT6245QRKCr8aAugASZP?= =?utf-8?q?E0zx6YtmGo4Tv6PeeEXLqVPZ2IA/x2479kVhs0+iw9wQ8d3ObBa8mEz3W0PbxG+Tm?= =?utf-8?q?sC87FLYUf14S7BsvBFSdH8YbnQtkPrXu6pc/F3bc/ZK57mqnIiVgFDONt4kZm+8nL?= =?utf-8?q?7euHWZOW4WWIZXMjIIo+tpPrz90H435Wo97E0Bk9uxZrLitXqorqShejKZR9AFXMJ?= =?utf-8?q?6oBUf8cXiERwUZ1gVshybGEMYn1Li7TtKfutv5cVrSEE/RbW6uTfzA4d/lF9AfP74?= =?utf-8?q?XsbQw8PP3XclrTxIAk2oZ4C46bPfkrVS/+g7ndc2p/XF6SLk++5ZLeZFb/FSbiYtH?= =?utf-8?q?ekrDhcdyrwiV1hgi57tRBaAHJMGk8Bz2ISNtaEGEg8AS90zCMmeaxlTpE6phWofIu?= =?utf-8?q?g4Mevb5/+HqDJjY1+umA3PLJQKODZPS3diaPh5/BqDYsIkVFrwDMI3Nh1zmPRRV08?= =?utf-8?q?Noi5GLS7+pZhxqz9wLoqodzuaTQOrwQ8ibDyNis3uFbJar4VwuZmmo6uh5CCz4SWx?= =?utf-8?q?qq4qLk2nHtyAO15u+58TE41Wf3pqI1hR7l9NUzFIlqUPI0WmCtk+Wn5xrzRX90yxM?= =?utf-8?q?9m6Q3LtvRHRq7VxBarCqg0Z8mMbscUxJy6PIGlezbtSlmQIuKtT8otUH1rMNxe8EV?= =?utf-8?q?G/dW/xIOgnVrNMa4ZkyiCDbXVgP4Jn2/rWuabaOUThSQNbChNEEFAcnxiDTfl8zpV?= =?utf-8?q?u5q6PE6DOyxgeZcuJC//DOGwh1iMGc2+8c+4xU92DNAqRJrOheHFjOVExzELlioJd?= =?utf-8?q?HomYp4pzqpB/6abg5Xif13mv/MXDRUGSbeSZi1k9ITr95d83DM4edJk5fM0GxPjoP?= =?utf-8?q?+0C3HsNYIGblO3vtNTKPlh43dwpcZ753Sk7e4V1n+RXAkxejaIOtMlHbhKTjhY9iJ?= =?utf-8?q?XkmckZ5VIiezb31QtfqjqRzjQaYXoTMCRQ9NbXZwssyvqmU5RPISaeseGqdUodZv6?= =?utf-8?q?i4cmcbfgU5ioKwLuxguVqaMwdD/shKH1gXolezCMPtWQmPpSs/Q4tMo0dIiD6OBXa?= =?utf-8?q?hR5XYtVkhsZGBxKg4XJJ2vTC1OFwAq2I0cfjiE0NVZGB8J/eDwyRcb6Jjrz4QDIN+?= =?utf-8?q?4xxtQel0dmspthl8NVCH/wDXkTJVyd8DAqeDokcNAGcab5LYjPmIlrHroSYDlkUya?= =?utf-8?q?LpsbKoA5syg40oI1H7C0EUoji2wy5NQVcBr5VbpB6HCS6aZUMP71oFZs6ymLgJtve?= =?utf-8?q?3K1fg65rGAcAbnHVPurtQYTXmL6Kd5zzd4HNTePQTQAHwVD5yd8t3d+euH4uv/iBa?= =?utf-8?q?1JTsHAqiuoX8?= X-OriginatorOrg: vmware.com X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR05MB4970 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org During boot-time there are many PCI config reads, these could be performed either using Port IO instructions (PIO) or memory mapped I/O (MMIO). PIO are less efficient than MMIO, they require twice as many PCI accesses and PIO instructions are serializing. As a result, MMIO should be preferred when possible over PIO. Virtual Machine test result using VMware hypervisor 1 hundred thousand reads using raw_pci_read() took: PIO: 12.809 seconds MMIO: 8.517 seconds (~33.5% faster then PIO) Currently, when these reads are performed by a virtual machine, they all cause a VM-exit, and therefore each one of them induces a considerable overhead. This overhead can be further improved, by mapping MMIO region of virtual machine to memory area that holds the values that the “emulated hardware” is supposed to return. The memory region is mapped as "read-only” in the NPT/EPT, so reads from these regions would be treated as regular memory reads. Writes would still be trapped and emulated by the hypervisor. Virtual Machine test result with above changes in VMware hypervisor 1 hundred thousand read using raw_pci_read() took: PIO: 12.809 seconds MMIO: 0.010 seconds This helps to reduce virtual machine PCI scan and initialization time by ~65%. In our case it reduced to ~18 mSec from ~55 mSec. MMIO is also faster than PIO on bare-metal systems, but due to some bugs with legacy hardware and the smaller gains on bare-metal, it seems prudent not to change bare-metal behavior. Signed-off-by: Ajay Kaher --- v1 -> v2: Limit changes to apply only to VMs [Matthew W.] --- arch/x86/pci/common.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/arch/x86/pci/common.c b/arch/x86/pci/common.c index ddb7986..1e5a8f7 100644 --- a/arch/x86/pci/common.c +++ b/arch/x86/pci/common.c @@ -20,6 +20,7 @@ #include #include #include +#include unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2 | PCI_PROBE_MMCONF; @@ -57,14 +58,58 @@ int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn, return -EINVAL; } +#ifdef CONFIG_HYPERVISOR_GUEST +static int vm_raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn, + int reg, int len, u32 *val) +{ + if (raw_pci_ext_ops) + return raw_pci_ext_ops->read(domain, bus, devfn, reg, len, val); + if (domain == 0 && reg < 256 && raw_pci_ops) + return raw_pci_ops->read(domain, bus, devfn, reg, len, val); + return -EINVAL; +} + +static int vm_raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn, + int reg, int len, u32 val) +{ + if (raw_pci_ext_ops) + return raw_pci_ext_ops->write(domain, bus, devfn, reg, len, val); + if (domain == 0 && reg < 256 && raw_pci_ops) + return raw_pci_ops->write(domain, bus, devfn, reg, len, val); + return -EINVAL; +} +#endif /* CONFIG_HYPERVISOR_GUEST */ + static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value) { +#ifdef CONFIG_HYPERVISOR_GUEST + /* + * MMIO is faster than PIO, but due to some bugs with legacy + * hardware, it seems prudent to prefer MMIO for VMs and PIO + * for bare-metal. + */ + if (!hypervisor_is_type(X86_HYPER_NATIVE)) + return vm_raw_pci_read(pci_domain_nr(bus), bus->number, + devfn, where, size, value); +#endif /* CONFIG_HYPERVISOR_GUEST */ + return raw_pci_read(pci_domain_nr(bus), bus->number, devfn, where, size, value); } static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value) { +#ifdef CONFIG_HYPERVISOR_GUEST + /* + * MMIO is faster than PIO, but due to some bugs with legacy + * hardware, it seems prudent to prefer MMIO for VMs and PIO + * for bare-metal. + */ + if (!hypervisor_is_type(X86_HYPER_NATIVE)) + return vm_raw_pci_write(pci_domain_nr(bus), bus->number, + devfn, where, size, value); +#endif /* CONFIG_HYPERVISOR_GUEST */ + return raw_pci_write(pci_domain_nr(bus), bus->number, devfn, where, size, value); }