From nobody Mon May 6 20:09:11 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1619175196; cv=none; d=zohomail.com; s=zohoarc; b=h1qpgh3kOHmxSfF6HjM5bD8L0F4I/QAaP3wo/nuCk1Qo87Y466rSNp7KsIWzhJfLFrUAu+UyoPUzFpAfgddcs3LU2eQegt2o2WMEr7MxfPfeQ3fMVhUB5L64YOofhyiQrkKHhUaQ94hqLkR8Y18hVaHf0u0GHehu9ElLzPy8QQk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1619175196; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=oKJUzjD5NEc7cfDAEFdzVdDF07cDm6q66hm4fqeNQ88=; b=aWvEQ++2QLHLLc/BueSvyHDTbRf+IX/D5tQ+ZhmIz3gnOycC3aCp/k4tGJJyYHQ6/SHmeAb/bF7iUJrsiaZ9V6RLi6VnTe98tLT66/aDtYoBkCNe+7g+8KdEPC3A5T+GfdaqaUr9oX1TxBz5znrGLY/uOEozKSSoeSJyq+XUQbI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1619175196658528.3725223303571; Fri, 23 Apr 2021 03:53:16 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.116145.221724 (Exim 4.92) (envelope-from ) id 1lZtQj-0003qm-3y; Fri, 23 Apr 2021 10:53:01 +0000 Received: by outflank-mailman (output) from mailman id 116145.221724; Fri, 23 Apr 2021 10:53:01 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lZtQj-0003qf-0L; Fri, 23 Apr 2021 10:53:01 +0000 Received: by outflank-mailman (input) for mailman id 116145; Fri, 23 Apr 2021 10:53:00 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lZtQi-0003qX-6D for xen-devel@lists.xenproject.org; Fri, 23 Apr 2021 10:53:00 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 022f5376-5ce4-4f98-9068-a092707b59a9; Fri, 23 Apr 2021 10:52:58 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id A44CBB05E; Fri, 23 Apr 2021 10:52:57 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version 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: 022f5376-5ce4-4f98-9068-a092707b59a9 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=1619175177; 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=oKJUzjD5NEc7cfDAEFdzVdDF07cDm6q66hm4fqeNQ88=; b=ueTKK1qvt3muiPrrKn9vxDRIoE6oahxpmJAm1xLQ7x7YEG7/xJha88yqbaeX1gvhoaDWZp pYwmtPO7Wzq8ekiGGIEXzAe0xF14miX5N9zcy4pVcB1OpdE4FXsHJSOZRzMtI6AiN+K8jZ zW077bAdySPLvJtVeb5rmk5c+ynpvPM= Subject: [PATCH v4 1/3] VMX: use a single, global APIC access page From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Wei Liu , =?UTF-8?Q?Roger_Pau_Monn=c3=a9?= , George Dunlap , Kevin Tian , Jun Nakajima , Tim Deegan References: <4731a3a3-906a-98ac-11ba-6a0723903391@suse.com> <3e7ad009-f062-d58a-9380-103ce1573a73@suse.com> Message-ID: <837a427a-a74e-00e9-70c7-1d3cfa28b30c@suse.com> Date: Fri, 23 Apr 2021 12:52:57 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <3e7ad009-f062-d58a-9380-103ce1573a73@suse.com> Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" The address of this page is used by the CPU only to recognize when to access the virtual APIC page instead. No accesses would ever go to this page. It only needs to be present in the (CPU) page tables so that address translation will produce its address as result for respective accesses. By making this page global, we also eliminate the need to refcount it, or to assign it to any domain in the first place. Signed-off-by: Jan Beulich Acked-by: Tim Deegan Reviewed-by: Kevin Tian Reviewed-by: Roger Pau Monn=C3=A9 --- v5: Init apic_access_mfn to INVALID_MFN. Move assignment out of if() condition. Introduce page_suppress_refcounting() and page_refcounting_suppressed(). v4: Set PGC_extra on the page. Make shadow mode work. v3: Split p2m insertion change to a separate patch. v2: Avoid insertion when !has_vlapic(). Split off change to p2m_get_iommu_flags(). --- 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. --- a/xen/arch/x86/hvm/vmx/vmx.c +++ b/xen/arch/x86/hvm/vmx/vmx.c @@ -66,8 +66,7 @@ boolean_param("force-ept", opt_force_ept static void vmx_ctxt_switch_from(struct vcpu *v); static void vmx_ctxt_switch_to(struct vcpu *v); =20 -static int vmx_alloc_vlapic_mapping(struct domain *d); -static void vmx_free_vlapic_mapping(struct domain *d); +static int alloc_vlapic_mapping(void); static void vmx_install_vlapic_mapping(struct vcpu *v); static void vmx_update_guest_cr(struct vcpu *v, unsigned int cr, unsigned int flags); @@ -78,6 +77,8 @@ static int vmx_msr_read_intercept(unsign static int vmx_msr_write_intercept(unsigned int msr, uint64_t msr_content); static void vmx_invlpg(struct vcpu *v, unsigned long linear); =20 +static mfn_t __read_mostly apic_access_mfn =3D INVALID_MFN_INITIALIZER; + /* Values for domain's ->arch.hvm_domain.pi_ops.flags. */ #define PI_CSW_FROM (1u << 0) #define PI_CSW_TO (1u << 1) @@ -401,7 +402,6 @@ static int vmx_domain_initialise(struct .to =3D vmx_ctxt_switch_to, .tail =3D vmx_do_resume, }; - int rc; =20 d->arch.ctxt_switch =3D &csw; =20 @@ -411,28 +411,22 @@ static int vmx_domain_initialise(struct */ d->arch.hvm.vmx.exec_sp =3D is_hardware_domain(d) || opt_ept_exec_sp; =20 - if ( !has_vlapic(d) ) - return 0; - - if ( (rc =3D vmx_alloc_vlapic_mapping(d)) !=3D 0 ) - return rc; - return 0; } =20 -static void vmx_domain_relinquish_resources(struct domain *d) +static void domain_creation_finished(struct domain *d) { - if ( !has_vlapic(d) ) + gfn_t gfn =3D gaddr_to_gfn(APIC_DEFAULT_PHYS_BASE); + uint8_t ipat; + + if ( !has_vlapic(d) || mfn_eq(apic_access_mfn, INVALID_MFN) ) return; =20 - vmx_free_vlapic_mapping(d); -} + ASSERT(epte_get_entry_emt(d, gfn_x(gfn), apic_access_mfn, 0, &ipat, + true) =3D=3D MTRR_TYPE_WRBACK); + ASSERT(ipat); =20 -static void domain_creation_finished(struct domain *d) -{ - if ( has_vlapic(d) && !mfn_eq(d->arch.hvm.vmx.apic_access_mfn, _mfn(0)= ) && - set_mmio_p2m_entry(d, gaddr_to_gfn(APIC_DEFAULT_PHYS_BASE), - d->arch.hvm.vmx.apic_access_mfn, PAGE_ORDER_4K= ) ) + if ( set_mmio_p2m_entry(d, gfn, apic_access_mfn, PAGE_ORDER_4K) ) domain_crash(d); } =20 @@ -2415,7 +2409,6 @@ static struct hvm_function_table __initd .cpu_up_prepare =3D vmx_cpu_up_prepare, .cpu_dead =3D vmx_cpu_dead, .domain_initialise =3D vmx_domain_initialise, - .domain_relinquish_resources =3D vmx_domain_relinquish_resources, .domain_creation_finished =3D domain_creation_finished, .vcpu_initialise =3D vmx_vcpu_initialise, .vcpu_destroy =3D vmx_vcpu_destroy, @@ -2662,7 +2655,7 @@ const struct hvm_function_table * __init { set_in_cr4(X86_CR4_VMXE); =20 - if ( vmx_vmcs_init() ) + if ( vmx_vmcs_init() || alloc_vlapic_mapping() ) { printk("VMX: failed to initialise.\n"); return NULL; @@ -3224,7 +3217,7 @@ gp_fault: return X86EMUL_EXCEPTION; } =20 -static int vmx_alloc_vlapic_mapping(struct domain *d) +static int __init alloc_vlapic_mapping(void) { struct page_info *pg; mfn_t mfn; @@ -3232,52 +3225,34 @@ static int vmx_alloc_vlapic_mapping(stru if ( !cpu_has_vmx_virtualize_apic_accesses ) return 0; =20 - pg =3D alloc_domheap_page(d, MEMF_no_refcount); + pg =3D alloc_domheap_page(NULL, 0); if ( !pg ) return -ENOMEM; =20 - 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; - } + /* + * Signal to shadow code that this page cannot be refcounted. This also + * makes epte_get_entry_emt() recognize this page as "special". + */ + page_suppress_refcounting(pg); =20 mfn =3D page_to_mfn(pg); clear_domain_page(mfn); - d->arch.hvm.vmx.apic_access_mfn =3D mfn; + apic_access_mfn =3D mfn; =20 return 0; } =20 -static void vmx_free_vlapic_mapping(struct domain *d) -{ - mfn_t mfn =3D d->arch.hvm.vmx.apic_access_mfn; - - d->arch.hvm.vmx.apic_access_mfn =3D _mfn(0); - if ( !mfn_eq(mfn, _mfn(0)) ) - { - struct page_info *pg =3D mfn_to_page(mfn); - - put_page_alloc_ref(pg); - put_page_and_type(pg); - } -} - static void vmx_install_vlapic_mapping(struct vcpu *v) { paddr_t virt_page_ma, apic_page_ma; =20 - if ( mfn_eq(v->domain->arch.hvm.vmx.apic_access_mfn, _mfn(0)) ) + if ( !has_vlapic(v->domain) || mfn_eq(apic_access_mfn, INVALID_MFN) ) return; =20 ASSERT(cpu_has_vmx_virtualize_apic_accesses); =20 virt_page_ma =3D page_to_maddr(vcpu_vlapic(v)->regs_page); - apic_page_ma =3D mfn_to_maddr(v->domain->arch.hvm.vmx.apic_access_mfn); + apic_page_ma =3D mfn_to_maddr(apic_access_mfn); =20 vmx_vmcs_enter(v); __vmwrite(VIRTUAL_APIC_PAGE_ADDR, virt_page_ma); --- a/xen/arch/x86/mm/shadow/set.c +++ b/xen/arch/x86/mm/shadow/set.c @@ -94,6 +94,15 @@ shadow_get_page_from_l1e(shadow_l1e_t sl ASSERT(!sh_l1e_is_magic(sl1e)); ASSERT(shadow_mode_refcounts(d)); =20 + /* + * Check whether refcounting is suppressed on this page. For example, + * VMX'es APIC access MFN is just a surrogate page. It doesn't actual= ly + * get accessed, and hence there's no need to refcount it. + */ + mfn =3D shadow_l1e_get_mfn(sl1e); + if ( mfn_valid(mfn) && page_refcounting_suppressed(mfn_to_page(mfn)) ) + return 0; + res =3D get_page_from_l1e(sl1e, d, d); =20 /* --- a/xen/arch/x86/mm/shadow/types.h +++ b/xen/arch/x86/mm/shadow/types.h @@ -276,9 +276,16 @@ int shadow_set_l4e(struct domain *d, sha static void inline shadow_put_page_from_l1e(shadow_l1e_t sl1e, struct domain *d) { + mfn_t mfn; + if ( !shadow_mode_refcounts(d) ) return; =20 + if ( mfn_valid(mfn =3D shadow_l1e_get_mfn(sl1e)) && + /* See the respective comment in shadow_get_page_from_l1e(). */ + page_refcounting_suppressed(mfn_to_page(mfn)) ) + return; + put_page_from_l1e(sl1e, d); } =20 --- a/xen/include/asm-x86/hvm/vmx/vmcs.h +++ b/xen/include/asm-x86/hvm/vmx/vmcs.h @@ -58,7 +58,6 @@ struct ept_data { #define _VMX_DOMAIN_PML_ENABLED 0 #define VMX_DOMAIN_PML_ENABLED (1ul << _VMX_DOMAIN_PML_ENABLED) struct vmx_domain { - mfn_t apic_access_mfn; /* VMX_DOMAIN_* */ unsigned int status; =20 --- a/xen/include/asm-x86/mm.h +++ b/xen/include/asm-x86/mm.h @@ -82,7 +82,7 @@ #define PGC_state_offlined PG_mask(2, 9) #define PGC_state_free PG_mask(3, 9) #define page_state_is(pg, st) (((pg)->count_info&PGC_state) =3D=3D PGC_sta= te_##st) -/* Page is not reference counted */ +/* Page is not reference counted (see below for caveats) */ #define _PGC_extra PG_shift(10) #define PGC_extra PG_mask(1, 10) =20 @@ -374,6 +374,24 @@ void zap_ro_mpt(mfn_t mfn); =20 bool is_iomem_page(mfn_t mfn); =20 +/* + * Pages with no owner which may get passed to functions wanting to + * refcount them can be marked PGC_extra to bypass this refcounting (which + * would fail due to the lack of an owner). + * + * (For pages with owner PGC_extra has different meaning.) + */ +static inline void page_suppress_refcounting(struct page_info *pg) +{ + ASSERT(!page_get_owner(pg)); + pg->count_info |=3D PGC_extra; +} + +static inline bool page_refcounting_suppressed(const struct page_info *pg) +{ + return !page_get_owner(pg) && (pg->count_info & PGC_extra); +} + struct platform_bad_page { unsigned long mfn; unsigned int order; From nobody Mon May 6 20:09:11 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1619175230; cv=none; d=zohomail.com; s=zohoarc; b=f/H7LGIDHw8mFF+Dwydm1zO7ckLgUNtbV0mhJG9U4c+aGSKskbB4LyKO8N8PSof6MgmynDRflrokTExTbbaciTaia13xI6foyE7Q0mcOLr5RMBZa4s8UzWK0tCeRQMeY1kudoOk5ZXyANutGA+IYwozrcEGmVdytv5qx9osRH78= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1619175230; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=oo1/XTyys+4AMwPBg+4apC2PyGiHm7JIfunlGllfB28=; b=GSVlysHiMI14Bv4PNOWUxvGGdvxQFHt9uvEk4anSigOmR3qR1AvWcAFnlluBY8AfAUvrYVT4xvmsA4vpp8PJLVPZx7LPQG41EWlNBKHqOlYpwVdGgyXPXUEybmalsHsHyxL85jbIOVO5VZIpBYDYAXDK0KxWSV1etCF6heeAn88= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1619175230732340.64381934602307; Fri, 23 Apr 2021 03:53:50 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.116149.221736 (Exim 4.92) (envelope-from ) id 1lZtR9-0003xz-GI; Fri, 23 Apr 2021 10:53:27 +0000 Received: by outflank-mailman (output) from mailman id 116149.221736; Fri, 23 Apr 2021 10:53:27 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lZtR9-0003xs-Cs; Fri, 23 Apr 2021 10:53:27 +0000 Received: by outflank-mailman (input) for mailman id 116149; Fri, 23 Apr 2021 10:53:26 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lZtR8-0003xd-0P for xen-devel@lists.xenproject.org; Fri, 23 Apr 2021 10:53:26 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 8e0f1d37-1d03-4cf3-acfd-84135c3bb88f; Fri, 23 Apr 2021 10:53:25 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 5DF4DB05D; Fri, 23 Apr 2021 10:53:24 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version 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: 8e0f1d37-1d03-4cf3-acfd-84135c3bb88f 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=1619175204; 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=oo1/XTyys+4AMwPBg+4apC2PyGiHm7JIfunlGllfB28=; b=SwpxV/hzcMlbMTpBYIWcnUMWSkoATeEy4BcOU4v1PPN4SDWPyT4+1c2ljejP5bqJZLXtJN M4DyyXXDJ5VAqV0U9bHLO6V/1Cm391oLlXydDfUGNvNT1yZ3LBMCsR72QYzdme9a2tRgyY IusHiwVWVlWEgNyWpZpOb/DDzIElLx4= Subject: [PATCH v4 2/3] x86/shadow: re-use variables in shadow_get_page_from_l1e() From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Wei Liu , =?UTF-8?Q?Roger_Pau_Monn=c3=a9?= , George Dunlap , Tim Deegan References: <4731a3a3-906a-98ac-11ba-6a0723903391@suse.com> <3e7ad009-f062-d58a-9380-103ce1573a73@suse.com> Message-ID: Date: Fri, 23 Apr 2021 12:53:24 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <3e7ad009-f062-d58a-9380-103ce1573a73@suse.com> Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" There's little point in doing multiple mfn_to_page() or page_get_owner() on all the same MFN. Calculate them once at the start of the function. Signed-off-by: Jan Beulich Acked-by: Tim Deegan --- v5: Integrate into series. Re-base. v2: Re-base. --- a/xen/arch/x86/mm/shadow/set.c +++ b/xen/arch/x86/mm/shadow/set.c @@ -88,19 +88,25 @@ static int inline shadow_get_page_from_l1e(shadow_l1e_t sl1e, struct domain *d, p2m_type_t t= ype) { int res; - mfn_t mfn; - struct domain *owner; + mfn_t mfn =3D shadow_l1e_get_mfn(sl1e); + const struct page_info *pg =3D NULL; + struct domain *owner =3D NULL; =20 ASSERT(!sh_l1e_is_magic(sl1e)); ASSERT(shadow_mode_refcounts(d)); =20 + if ( mfn_valid(mfn) ) + { + pg =3D mfn_to_page(mfn); + owner =3D page_get_owner(pg); + } + /* * Check whether refcounting is suppressed on this page. For example, * VMX'es APIC access MFN is just a surrogate page. It doesn't actual= ly * get accessed, and hence there's no need to refcount it. */ - mfn =3D shadow_l1e_get_mfn(sl1e); - if ( mfn_valid(mfn) && page_refcounting_suppressed(mfn_to_page(mfn)) ) + if ( pg && page_refcounting_suppressed(pg) ) return 0; =20 res =3D get_page_from_l1e(sl1e, d, d); @@ -111,9 +117,7 @@ shadow_get_page_from_l1e(shadow_l1e_t sl */ if ( unlikely(res < 0) && !shadow_mode_translate(d) && - mfn_valid(mfn =3D shadow_l1e_get_mfn(sl1e)) && - (owner =3D page_get_owner(mfn_to_page(mfn))) && - (d !=3D owner) ) + owner && (d !=3D owner) ) { res =3D xsm_priv_mapping(XSM_TARGET, d, owner); if ( !res ) @@ -136,9 +140,8 @@ shadow_get_page_from_l1e(shadow_l1e_t sl * already have checked that we're supposed to have access, so * we can just grab a reference directly. */ - mfn =3D shadow_l1e_get_mfn(sl1e); - if ( mfn_valid(mfn) ) - res =3D get_page_from_l1e(sl1e, d, page_get_owner(mfn_to_page(= mfn))); + if ( owner ) + res =3D get_page_from_l1e(sl1e, d, owner); } =20 if ( unlikely(res < 0) ) From nobody Mon May 6 20:09:11 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1619175262; cv=none; d=zohomail.com; s=zohoarc; b=Gy4niJMe6ohqIkOz3QDM3nFvXjXLcnop9NDqMnw9+S7Z4tKjm2xB623FoPRXq3R4p0cPjiM6KUfON9LyhXFy5RBOzrxFXZu2T7nKHpcKSPU16HPad0aZQsrbhG1iDr2kpetLKcHDf1z40GgbA4uxYQP97u6PewKYYlqlVxNs530= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1619175262; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=V9AlDGFgRhWxlW+/r9dPVuhktXxXHQ12DuVMyZCakP0=; b=ddIcUvTRitoMyX5/h8rxNlfkluFy+DK2VZqBOc1Z49zhRATy6wgmKDndmUumkxvwPYfZrZ6K+SxQ6iCkm/ASxtaoKXr1/O/DMKRuLQxX52LuQTaPYuqXNOdNxzHZ9hiVWfiG5coOTvLftIcI5OMVaWL/hbpVURXiTkU/98YURNA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1619175262968830.4146546239272; Fri, 23 Apr 2021 03:54:22 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.116155.221748 (Exim 4.92) (envelope-from ) id 1lZtRn-00045v-Qe; Fri, 23 Apr 2021 10:54:07 +0000 Received: by outflank-mailman (output) from mailman id 116155.221748; Fri, 23 Apr 2021 10:54:07 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lZtRn-00045o-Mv; Fri, 23 Apr 2021 10:54:07 +0000 Received: by outflank-mailman (input) for mailman id 116155; Fri, 23 Apr 2021 10:54:06 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lZtRm-00045f-FJ for xen-devel@lists.xenproject.org; Fri, 23 Apr 2021 10:54:06 +0000 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 0b73f536-da90-4842-a39f-c4a55e978512; Fri, 23 Apr 2021 10:54:05 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 4DAD8B04F; Fri, 23 Apr 2021 10:54:04 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version 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: 0b73f536-da90-4842-a39f-c4a55e978512 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=1619175244; 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=V9AlDGFgRhWxlW+/r9dPVuhktXxXHQ12DuVMyZCakP0=; b=HGHudFs4r1jEE227WLPQfklyR9dbDX55518tTBQFY7hcFR7Dfhu5nOVq2QrBXHoouiTNWs PU4XTGECr+X9jrKeniBkEa6cbPQejODVPreogIt1hyH9mFMkMonSojHTu2d8/2FpAd/TEH Aa9+BxJsnDPoS70z4oD5ONlecWDpt3A= Subject: [PATCH v4 3/3] x86/shadow: streamline shadow_get_page_from_l1e() From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Wei Liu , =?UTF-8?Q?Roger_Pau_Monn=c3=a9?= , George Dunlap , Tim Deegan References: <4731a3a3-906a-98ac-11ba-6a0723903391@suse.com> <3e7ad009-f062-d58a-9380-103ce1573a73@suse.com> Message-ID: Date: Fri, 23 Apr 2021 12:54:04 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <3e7ad009-f062-d58a-9380-103ce1573a73@suse.com> Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) Content-Type: text/plain; charset="utf-8" Trying get_page_from_l1e() up to three times isn't helpful; in debug builds it may lead to log messages making things look as if there was a problem somewhere. And there's no need to have more than one try: The function can only possibly succeed for one domain passed as 3rd argument (unless the page is an MMIO one to which both have access, but MMIO pages should be "got" by specifying the requesting domain anyway). Re-arrange things so just the one call gets made which has a chance of succeeding. The code could in principle be arranged such that there's only a single call to get_page_from_l1e(), but the conditional would become pretty complex then and hence hard to follow / understand / adjust. The redundant (with shadow_mode_refcounts()) shadow_mode_translate() gets dropped. Signed-off-by: Jan Beulich Acked-by: Tim Deegan --- v5: Integrate into series. Re-base. --- a/xen/arch/x86/mm/shadow/set.c +++ b/xen/arch/x86/mm/shadow/set.c @@ -109,40 +109,36 @@ shadow_get_page_from_l1e(shadow_l1e_t sl if ( pg && page_refcounting_suppressed(pg) ) return 0; =20 - res =3D get_page_from_l1e(sl1e, d, d); + if ( owner =3D=3D dom_io ) + owner =3D NULL; =20 /* * If a privileged domain is attempting to install a map of a page it = does * not own, we let it succeed anyway. */ - if ( unlikely(res < 0) && - !shadow_mode_translate(d) && - owner && (d !=3D owner) ) + if ( owner && (d !=3D owner) && + !(res =3D xsm_priv_mapping(XSM_TARGET, d, owner)) ) { - res =3D xsm_priv_mapping(XSM_TARGET, d, owner); - if ( !res ) - { - res =3D get_page_from_l1e(sl1e, d, owner); - SHADOW_PRINTK("privileged %pd installs map of mfn %"PRI_mfn" o= wned by %pd: %s\n", - d, mfn_x(mfn), owner, - res >=3D 0 ? "success" : "failed"); - } + res =3D get_page_from_l1e(sl1e, d, owner); + SHADOW_PRINTK("privileged %pd installs map of %pd's mfn %"PRI_mfn"= : %s\n", + d, owner, mfn_x(mfn), + res >=3D 0 ? "success" : "failed"); } - /* Okay, it might still be a grant mapping PTE. Try it. */ - if ( unlikely(res < 0) && - (type =3D=3D p2m_grant_map_rw || - (type =3D=3D p2m_grant_map_ro && - !(shadow_l1e_get_flags(sl1e) & _PAGE_RW))) ) + else if ( owner && + (type =3D=3D p2m_grant_map_rw || + (type =3D=3D p2m_grant_map_ro && + !(shadow_l1e_get_flags(sl1e) & _PAGE_RW))) ) { /* * It's a grant mapping. The grant table implementation will * already have checked that we're supposed to have access, so * we can just grab a reference directly. */ - if ( owner ) - res =3D get_page_from_l1e(sl1e, d, owner); + res =3D get_page_from_l1e(sl1e, d, owner); } + else + res =3D get_page_from_l1e(sl1e, d, d); =20 if ( unlikely(res < 0) ) {