From nobody Fri Nov 22 12:50:31 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; 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=fail(p=none dis=none) header.from=arm.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1716901011503504.989676313308; Tue, 28 May 2024 05:56:51 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.731130.1136468 (Exim 4.92) (envelope-from ) id 1sBwNT-0003BK-HL; Tue, 28 May 2024 12:56:31 +0000 Received: by outflank-mailman (output) from mailman id 731130.1136468; Tue, 28 May 2024 12:56:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sBwNT-0003BD-DO; Tue, 28 May 2024 12:56:31 +0000 Received: by outflank-mailman (input) for mailman id 731130; Tue, 28 May 2024 12:56:30 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sBwNS-0003B7-LT for xen-devel@lists.xenproject.org; Tue, 28 May 2024 12:56:30 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id b1ece388-1cf1-11ef-b4bb-af5377834399; Tue, 28 May 2024 14:56:27 +0200 (CEST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D6F56339; Tue, 28 May 2024 05:56:50 -0700 (PDT) Received: from e125770.cambridge.arm.com (e125770.arm.com [10.1.199.43]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9991E3F641; Tue, 28 May 2024 05:56:25 -0700 (PDT) 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: b1ece388-1cf1-11ef-b4bb-af5377834399 From: Luca Fancellu To: xen-devel@lists.xenproject.org Cc: Penny Zheng , Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , Penny Zheng Subject: [PATCH v4.2] xen/p2m: put reference for level 2 superpage Date: Tue, 28 May 2024 13:56:03 +0100 Message-Id: <20240528125603.2467640-1-luca.fancellu@arm.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1716901013306100001 Content-Type: text/plain; charset="utf-8" From: Penny Zheng We are doing foreign memory mapping for static shared memory, and there is a great possibility that it could be super mapped. But today, p2m_put_l3_page could not handle superpages. This commits implements a new function p2m_put_l2_superpage to handle level 2 superpages, specifically for helping put extra references for foreign superpages. Modify relinquish_p2m_mapping as well to take into account preemption when we have a level-2 foreign mapping. Currently level 1 superpages are not handled because Xen is not preemptible and therefore some work is needed to handle such superpages, for which at some point Xen might end up freeing memory and therefore for such a big mapping it could end up in a very long operation. Signed-off-by: Penny Zheng Signed-off-by: Luca Fancellu Reviewed-by: Julien Grall --- v4.2 changes: - rework commit message to don't explicit say the size of the mapping but only say the level, remove unnecessary p2m_is_superpage() check in p2m_put_page, remove (level >=3D 2) condition to call p2m_put_page inside p2m_free_entry because the code in that function can already handle the levels, move TODO comment inside p2m_put_page, avoid mentioning 2MB in the comments. (Julien) - This patch is meant to superseed the homonymous patch in the serie: https://patchwork.kernel.org/project/xen-devel/cover/20240524124055.3871= 399-1-luca.fancellu@arm.com/ v4 changes: - optimised the path to call put_page() on the foreign mapping as Julien suggested. Add a comment in p2m_put_l2_superpage to state that any changes needs to take into account some change in the relinquish code. (Julien) v3 changes: - Add reasoning why we don't support now 1GB superpage, remove level_order variable from p2m_put_l2_superpage, update TODO comment inside p2m_free_entry, use XEN_PT_LEVEL_ORDER(2) instead of value 9 inside relinquish_p2m_mapping. (Michal) v2: - Do not handle 1GB super page as there might be some issue where a lot of calls to put_page(...) might be issued which could lead to free memory that is a long operation. v1: - patch from https://patchwork.kernel.org/project/xen-devel/patch/20231206= 090623.1932275-9-Penny.Zheng@arm.com/ --- xen/arch/arm/mmu/p2m.c | 81 +++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 20 deletions(-) diff --git a/xen/arch/arm/mmu/p2m.c b/xen/arch/arm/mmu/p2m.c index 41fcca011cf4..1725cca649b5 100644 --- a/xen/arch/arm/mmu/p2m.c +++ b/xen/arch/arm/mmu/p2m.c @@ -753,34 +753,72 @@ static int p2m_mem_access_radix_set(struct p2m_domain= *p2m, gfn_t gfn, return rc; } =20 -/* - * Put any references on the single 4K page referenced by pte. - * TODO: Handle superpages, for now we only take special references for le= af - * pages (specifically foreign ones, which can't be super mapped today). - */ -static void p2m_put_l3_page(const lpae_t pte) +static void p2m_put_foreign_page(struct page_info *pg) { - mfn_t mfn =3D lpae_get_mfn(pte); - - ASSERT(p2m_is_valid(pte)); - /* - * TODO: Handle other p2m types - * * It's safe to do the put_page here because page_alloc will * flush the TLBs if the page is reallocated before the end of * this loop. */ - if ( p2m_is_foreign(pte.p2m.type) ) + put_page(pg); +} + +/* Put any references on the single 4K page referenced by mfn. */ +static void p2m_put_l3_page(mfn_t mfn, p2m_type_t type) +{ + /* TODO: Handle other p2m types */ + if ( p2m_is_foreign(type) ) { ASSERT(mfn_valid(mfn)); - put_page(mfn_to_page(mfn)); + p2m_put_foreign_page(mfn_to_page(mfn)); } /* Detect the xenheap page and mark the stored GFN as invalid. */ - else if ( p2m_is_ram(pte.p2m.type) && is_xen_heap_mfn(mfn) ) + else if ( p2m_is_ram(type) && is_xen_heap_mfn(mfn) ) page_set_xenheap_gfn(mfn_to_page(mfn), INVALID_GFN); } =20 +/* Put any references on the superpage referenced by mfn. */ +static void p2m_put_l2_superpage(mfn_t mfn, p2m_type_t type) +{ + struct page_info *pg; + unsigned int i; + + /* + * TODO: Handle other p2m types, but be aware that any changes to hand= le + * different types should require an update on the relinquish code to = handle + * preemption. + */ + if ( !p2m_is_foreign(type) ) + return; + + ASSERT(mfn_valid(mfn)); + + pg =3D mfn_to_page(mfn); + + for ( i =3D 0; i < XEN_PT_LPAE_ENTRIES; i++, pg++ ) + p2m_put_foreign_page(pg); +} + +/* Put any references on the page referenced by pte. */ +static void p2m_put_page(const lpae_t pte, unsigned int level) +{ + mfn_t mfn =3D lpae_get_mfn(pte); + + ASSERT(p2m_is_valid(pte)); + + /* + * TODO: Currently we don't handle level 1 super-page, Xen is not + * preemptible and therefore some work is needed to handle such + * superpages, for which at some point Xen might end up freeing memory + * and therefore for such a big mapping it could end up in a very long + * operation. + */ + if ( level =3D=3D 2 ) + return p2m_put_l2_superpage(mfn, pte.p2m.type); + else if ( level =3D=3D 3 ) + return p2m_put_l3_page(mfn, pte.p2m.type); +} + /* Free lpae sub-tree behind an entry */ static void p2m_free_entry(struct p2m_domain *p2m, lpae_t entry, unsigned int level) @@ -809,9 +847,9 @@ static void p2m_free_entry(struct p2m_domain *p2m, #endif =20 p2m->stats.mappings[level]--; - /* Nothing to do if the entry is a super-page. */ - if ( level =3D=3D 3 ) - p2m_put_l3_page(entry); + + p2m_put_page(entry, level); + return; } =20 @@ -1558,9 +1596,12 @@ int relinquish_p2m_mapping(struct domain *d) =20 count++; /* - * Arbitrarily preempt every 512 iterations. + * Arbitrarily preempt every 512 iterations or when we have a leve= l-2 + * foreign mapping. */ - if ( !(count % 512) && hypercall_preempt_check() ) + if ( (!(count % 512) || + (p2m_is_foreign(t) && (order > XEN_PT_LEVEL_ORDER(2)))) && + hypercall_preempt_check() ) { rc =3D -ERESTART; break; --=20 2.34.1