Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC357C47247 for ; Fri, 8 May 2020 12:44:07 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8243624958 for ; Fri, 8 May 2020 12:44:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8243624958 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jX2M1-0002zo-1F; Fri, 08 May 2020 12:43:49 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1jX2Lz-0002zj-7Z for xen-devel@lists.xenproject.org; Fri, 08 May 2020 12:43:47 +0000 X-Inumbo-ID: 8dc88ee2-9129-11ea-9887-bc764e2007e4 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 8dc88ee2-9129-11ea-9887-bc764e2007e4; Fri, 08 May 2020 12:43:45 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 9DCE1ACC2; Fri, 8 May 2020 12:43:46 +0000 (UTC) To: "xen-devel@lists.xenproject.org" From: Jan Beulich Subject: [PATCH] x86/PVH: PHYSDEVOP_pci_mmcfg_reserved should not blindly register a region Message-ID: <2ee1a3cb-3b40-6e6d-5308-1cdf9f6c521e@suse.com> Date: Fri, 8 May 2020 14:43:38 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Andrew Cooper , Paul Durrant , Wei Liu , =?UTF-8?Q?Roger_Pau_Monn=c3=a9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" The op has a register/unregister flag, and hence registration shouldn't happen unilaterally. Introduce unregister_vpci_mmcfg_handler() to handle this case. Fixes: eb3dd90e4089 ("x86/physdev: enable PHYSDEVOP_pci_mmcfg_reserved for PVH Dom0") Signed-off-by: Jan Beulich --- a/xen/arch/x86/hvm/io.c +++ b/xen/arch/x86/hvm/io.c @@ -558,6 +558,47 @@ int register_vpci_mmcfg_handler(struct d return 0; } +int unregister_vpci_mmcfg_handler(struct domain *d, paddr_t addr, + unsigned int start_bus, unsigned int end_bus, + unsigned int seg) +{ + struct hvm_mmcfg *mmcfg; + int rc = -ENOENT; + + ASSERT(is_hardware_domain(d)); + + if ( start_bus > end_bus ) + return -EINVAL; + + write_lock(&d->arch.hvm.mmcfg_lock); + + list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next ) + if ( mmcfg->addr == addr + (start_bus << 20) && + mmcfg->segment == seg && + mmcfg->start_bus == start_bus && + mmcfg->size == ((end_bus - start_bus + 1) << 20) ) + { + list_del(&mmcfg->next); + if ( !list_empty(&d->arch.hvm.mmcfg_regions) ) + xfree(mmcfg); + else + { + /* + * Cannot unregister the MMIO handler - leave a fake entry + * on the list. + */ + memset(mmcfg, 0, sizeof(*mmcfg)); + list_add(&mmcfg->next, &d->arch.hvm.mmcfg_regions); + } + rc = 0; + break; + } + + write_unlock(&d->arch.hvm.mmcfg_lock); + + return rc; +} + void destroy_vpci_mmcfg(struct domain *d) { struct list_head *mmcfg_regions = &d->arch.hvm.mmcfg_regions; --- a/xen/arch/x86/physdev.c +++ b/xen/arch/x86/physdev.c @@ -559,12 +559,18 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_H if ( !ret && has_vpci(currd) ) { /* - * For HVM (PVH) domains try to add the newly found MMCFG to the - * domain. + * For HVM (PVH) domains try to add/remove the reported MMCFG + * to/from the domain. */ - ret = register_vpci_mmcfg_handler(currd, info.address, - info.start_bus, info.end_bus, - info.segment); + if ( info.flags & XEN_PCI_MMCFG_RESERVED ) + ret = register_vpci_mmcfg_handler(currd, info.address, + info.start_bus, info.end_bus, + info.segment); + else + ret = unregister_vpci_mmcfg_handler(currd, info.address, + info.start_bus, + info.end_bus, + info.segment); } break; --- a/xen/include/asm-x86/hvm/io.h +++ b/xen/include/asm-x86/hvm/io.h @@ -178,6 +178,9 @@ void register_vpci_portio_handler(struct int register_vpci_mmcfg_handler(struct domain *d, paddr_t addr, unsigned int start_bus, unsigned int end_bus, unsigned int seg); +int unregister_vpci_mmcfg_handler(struct domain *d, paddr_t addr, + unsigned int start_bus, unsigned int end_bus, + unsigned int seg); /* Destroy tracked MMCFG areas. */ void destroy_vpci_mmcfg(struct domain *d);