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=-12.4 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,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 5A2C0C433DB for ; Thu, 11 Feb 2021 10:37:20 +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 0B06B64E95 for ; Thu, 11 Feb 2021 10:37:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0B06B64E95 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.83816.156910 (Exim 4.92) (envelope-from ) id 1lA9LL-0004Ox-UG; Thu, 11 Feb 2021 10:37:03 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 83816.156910; Thu, 11 Feb 2021 10:37:03 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lA9LL-0004Oq-R7; Thu, 11 Feb 2021 10:37:03 +0000 Received: by outflank-mailman (input) for mailman id 83816; Thu, 11 Feb 2021 10:37:02 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lA9LK-0004Ol-H0 for xen-devel@lists.xenproject.org; Thu, 11 Feb 2021 10:37:02 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 2ecda26f-8d8f-4e22-bd78-d1343834501c; Thu, 11 Feb 2021 10:37:01 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 7E8EBADCD; Thu, 11 Feb 2021 10:37:00 +0000 (UTC) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 2ecda26f-8d8f-4e22-bd78-d1343834501c X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1613039820; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KIKt2IbxDJ+JR+5PH3MQz09qkcO3HbCdRbYVSX8ZwhI=; b=j4y2aDoWqmd7hjQAQh7dqAwxVyn+H5MqPwjIcLe03Qtc3Z1vlFRUJiHQsxx7OPoGzQBUad aEWlD4y7vM7I/ohV/TBF9YyksvvYuF5ycROe3jkagkKC/r8QHmZ0THYkHHxY0kWVuonqB3 Le5eRlkjdg9WzspkPk53ZgKhSFsalsU= Subject: Re: [PATCH] VMX: use a single, global APIC access page To: =?UTF-8?Q?Roger_Pau_Monn=c3=a9?= Cc: "xen-devel@lists.xenproject.org" , Kevin Tian , Julien Grall , Andrew Cooper , Wei Liu References: <1b6a411b-84e7-bfb1-647e-511a13df838c@suse.com> From: Jan Beulich Message-ID: <7abc515b-d652-3d39-6038-99966deafdf8@suse.com> Date: Thu, 11 Feb 2021 11:36:59 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit On 11.02.2021 09:45, Roger Pau Monné wrote: > On Wed, Feb 10, 2021 at 05:48:26PM +0100, Jan Beulich wrote: >> I did further consider not allocating any real page at all, but just >> using the address of some unpopulated space (which would require >> announcing this page as reserved to Dom0, so it wouldn't put any PCI >> MMIO BARs there). But I thought this would be too controversial, because >> of the possible risks associated with this. > > No, Xen is not capable of allocating a suitable unpopulated page IMO, > so let's not go down that route. Wasting one RAM page seems perfectly > fine to me. Why would Xen not be able to, in principle? It may be difficult, but there may also be pages we could declare we know we can use for this purpose. >> @@ -3216,53 +3211,28 @@ static int vmx_alloc_vlapic_mapping(stru >> if ( !cpu_has_vmx_virtualize_apic_accesses ) >> return 0; >> >> - pg = alloc_domheap_page(d, MEMF_no_refcount); >> + pg = alloc_domheap_page(NULL, 0); >> if ( !pg ) >> return -ENOMEM; >> >> - if ( !get_page_and_type(pg, d, PGT_writable_page) ) >> - { >> - /* >> - * The domain can't possibly know about this page yet, so failure >> - * here is a clear indication of something fishy going on. >> - */ >> - domain_crash(d); >> - return -ENODATA; >> - } >> - >> mfn = page_to_mfn(pg); >> clear_domain_page(mfn); >> - d->arch.hvm.vmx.apic_access_mfn = mfn; >> + apic_access_mfn = mfn; >> >> - return set_mmio_p2m_entry(d, gaddr_to_gfn(APIC_DEFAULT_PHYS_BASE), mfn, >> - PAGE_ORDER_4K); >> -} >> - >> -static void vmx_free_vlapic_mapping(struct domain *d) >> -{ >> - mfn_t mfn = d->arch.hvm.vmx.apic_access_mfn; >> - >> - d->arch.hvm.vmx.apic_access_mfn = _mfn(0); >> - if ( !mfn_eq(mfn, _mfn(0)) ) >> - { >> - struct page_info *pg = mfn_to_page(mfn); >> - >> - put_page_alloc_ref(pg); >> - put_page_and_type(pg); >> - } >> + return 0; >> } >> >> static void vmx_install_vlapic_mapping(struct vcpu *v) >> { >> paddr_t virt_page_ma, apic_page_ma; >> >> - if ( mfn_eq(v->domain->arch.hvm.vmx.apic_access_mfn, _mfn(0)) ) >> + if ( mfn_eq(apic_access_mfn, _mfn(0)) ) > > You should check if the domain has a vlapic I think, since now > apic_access_mfn is global and will be set regardless of whether the > domain has vlapic enabled or not. > > Previously apic_access_mfn was only allocated if the domain had vlapic > enabled. Oh, indeed - thanks for spotting. And also in domain_creation_finished(). >> return; >> >> ASSERT(cpu_has_vmx_virtualize_apic_accesses); >> >> virt_page_ma = page_to_maddr(vcpu_vlapic(v)->regs_page); >> - apic_page_ma = mfn_to_maddr(v->domain->arch.hvm.vmx.apic_access_mfn); >> + apic_page_ma = mfn_to_maddr(apic_access_mfn); >> >> vmx_vmcs_enter(v); >> __vmwrite(VIRTUAL_APIC_PAGE_ADDR, virt_page_ma); > > I would consider setting up the vmcs and adding the page to the p2m in > the same function, and likely call it from vlapic_init. We could have > a domain_setup_apic in hvm_function_table that takes care of all this. Well, I'd prefer to do this just once per domain without needing to special case this on e.g. vCPU 0. >> --- a/xen/include/asm-x86/p2m.h >> +++ b/xen/include/asm-x86/p2m.h >> @@ -935,6 +935,9 @@ static inline unsigned int p2m_get_iommu >> flags = IOMMUF_readable; >> if ( !rangeset_contains_singleton(mmio_ro_ranges, mfn_x(mfn)) ) >> flags |= IOMMUF_writable; >> + /* VMX'es APIC access page is global and hence has no owner. */ >> + if ( mfn_valid(mfn) && !page_get_owner(mfn_to_page(mfn)) ) >> + flags = 0; > > Is it fine to have this page accessible to devices if the page tables > are shared between the CPU and the IOMMU? No, it's not, but what do you do? As said elsewhere, devices gaining more access than is helpful is the price we pay for being able to share page tables. But ... > Is it possible for devices to write to it? ... thinking about it - they would be able to access it only when interrupt remapping is off. Otherwise the entire range 0xFEExxxxx gets treated differently altogether by the IOMMU, and is not subject to DMA remapping. IOW it shouldn't even matter what flags we put in there (and I'd be far less concerned about the no-intremap case). What this change matters for then is only to avoid an unnecessary call to iommu_map() (and, for small enough domain, extra intermediate page tables to be allocated). But let me really split this off of this patch. Jan