From nobody Mon Apr 29 04:34:24 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 1660617476943729.2178496112082; Mon, 15 Aug 2022 19:37:56 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.387835.624287 (Exim 4.92) (envelope-from ) id 1oNmSI-0007Vj-59; Tue, 16 Aug 2022 02:37:22 +0000 Received: by outflank-mailman (output) from mailman id 387835.624287; Tue, 16 Aug 2022 02:37:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oNmSI-0007Vc-2U; Tue, 16 Aug 2022 02:37:22 +0000 Received: by outflank-mailman (input) for mailman id 387835; Tue, 16 Aug 2022 02:37:20 +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 1oNmSG-0007FY-7i for xen-devel@lists.xenproject.org; Tue, 16 Aug 2022 02:37:20 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 5916026d-1d0c-11ed-924f-1f966e50362f; Tue, 16 Aug 2022 04:37:19 +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 51D36106F; Mon, 15 Aug 2022 19:37:19 -0700 (PDT) Received: from a011292.shanghai.arm.com (a011292.shanghai.arm.com [10.169.190.94]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9DC5D3F70D; Mon, 15 Aug 2022 19:37:14 -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: 5916026d-1d0c-11ed-924f-1f966e50362f From: Penny Zheng To: xen-devel@lists.xenproject.org Cc: wei.chen@arm.com, Penny Zheng , Stefano Stabellini , Julien Grall , Bertrand Marquis , Volodymyr Babchuk , Andrew Cooper , George Dunlap , Jan Beulich , Wei Liu , Penny Zheng , Julien Grall Subject: [PATCH v10 1/9] xen/arm: rename PGC_reserved to PGC_static Date: Tue, 16 Aug 2022 10:36:50 +0800 Message-Id: <20220816023658.3551936-2-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220816023658.3551936-1-Penny.Zheng@arm.com> References: <20220816023658.3551936-1-Penny.Zheng@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1660617478759100001 Content-Type: text/plain; charset="utf-8" PGC_reserved could be ambiguous, and we have to tell what the pages are reserved for, so this commit intends to rename PGC_reserved to PGC_static, which clearly indicates the page is reserved for static memory. Signed-off-by: Penny Zheng Acked-by: Jan Beulich Acked-by: Julien Grall --- v10 changes: - no change --- v9 changes: - no change --- v8 changes: - no change --- v7 changes: - no change --- v6 changes: - rename PGC_staticmem to PGC_static --- v5 changes: - new commit --- xen/arch/arm/include/asm/mm.h | 6 +++--- xen/common/page_alloc.c | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/xen/arch/arm/include/asm/mm.h b/xen/arch/arm/include/asm/mm.h index 6c0a3c789f..da25251cda 100644 --- a/xen/arch/arm/include/asm/mm.h +++ b/xen/arch/arm/include/asm/mm.h @@ -121,9 +121,9 @@ struct page_info /* Page is Xen heap? */ #define _PGC_xen_heap PG_shift(2) #define PGC_xen_heap PG_mask(1, 2) - /* Page is reserved */ -#define _PGC_reserved PG_shift(3) -#define PGC_reserved PG_mask(1, 3) + /* Page is static memory */ +#define _PGC_static PG_shift(3) +#define PGC_static PG_mask(1, 3) /* ... */ /* Page is broken? */ #define _PGC_broken PG_shift(7) diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 8bdaffeb3d..00fa24e330 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -151,8 +151,8 @@ #define p2m_pod_offline_or_broken_replace(pg) BUG_ON(pg !=3D NULL) #endif =20 -#ifndef PGC_reserved -#define PGC_reserved 0 +#ifndef PGC_static +#define PGC_static 0 #endif =20 #ifndef PGT_TYPE_INFO_INITIALIZER @@ -2342,7 +2342,7 @@ int assign_pages( =20 for ( i =3D 0; i < nr; i++ ) { - ASSERT(!(pg[i].count_info & ~(PGC_extra | PGC_reserved))); + ASSERT(!(pg[i].count_info & ~(PGC_extra | PGC_static))); if ( pg[i].count_info & PGC_extra ) extra_pages++; } @@ -2402,7 +2402,7 @@ int assign_pages( page_set_owner(&pg[i], d); smp_wmb(); /* Domain pointer must be visible before updating refcn= t. */ pg[i].count_info =3D - (pg[i].count_info & (PGC_extra | PGC_reserved)) | PGC_allocate= d | 1; + (pg[i].count_info & (PGC_extra | PGC_static)) | PGC_allocated = | 1; =20 page_list_add_tail(&pg[i], page_to_list(d, &pg[i])); } @@ -2708,8 +2708,8 @@ void __init free_staticmem_pages(struct page_info *pg= , unsigned long nr_mfns, scrub_one_page(pg); } =20 - /* In case initializing page of static memory, mark it PGC_reserve= d. */ - pg[i].count_info |=3D PGC_reserved; + /* In case initializing page of static memory, mark it PGC_static.= */ + pg[i].count_info |=3D PGC_static; } } =20 @@ -2738,8 +2738,8 @@ static struct page_info * __init acquire_staticmem_pa= ges(mfn_t smfn, =20 for ( i =3D 0; i < nr_mfns; i++ ) { - /* The page should be reserved and not yet allocated. */ - if ( pg[i].count_info !=3D (PGC_state_free | PGC_reserved) ) + /* The page should be static and not yet allocated. */ + if ( pg[i].count_info !=3D (PGC_state_free | PGC_static) ) { printk(XENLOG_ERR "pg[%lu] Static MFN %"PRI_mfn" c=3D%#lx t=3D%#x\n", @@ -2753,10 +2753,10 @@ static struct page_info * __init acquire_staticmem_= pages(mfn_t smfn, &tlbflush_timestamp); =20 /* - * Preserve flag PGC_reserved and change page state + * Preserve flag PGC_static and change page state * to PGC_state_inuse. */ - pg[i].count_info =3D PGC_reserved | PGC_state_inuse; + pg[i].count_info =3D PGC_static | PGC_state_inuse; /* Initialise fields which have other uses for free pages. */ pg[i].u.inuse.type_info =3D PGT_TYPE_INFO_INITIALIZER; page_set_owner(&pg[i], NULL); @@ -2778,7 +2778,7 @@ static struct page_info * __init acquire_staticmem_pa= ges(mfn_t smfn, =20 out_err: while ( i-- ) - pg[i].count_info =3D PGC_reserved | PGC_state_free; + pg[i].count_info =3D PGC_static | PGC_state_free; =20 spin_unlock(&heap_lock); =20 --=20 2.25.1 From nobody Mon Apr 29 04:34:24 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 1660617478210702.5309572315583; Mon, 15 Aug 2022 19:37:58 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.387836.624299 (Exim 4.92) (envelope-from ) id 1oNmSM-0007nN-Cl; Tue, 16 Aug 2022 02:37:26 +0000 Received: by outflank-mailman (output) from mailman id 387836.624299; Tue, 16 Aug 2022 02:37:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oNmSM-0007nF-9l; Tue, 16 Aug 2022 02:37:26 +0000 Received: by outflank-mailman (input) for mailman id 387836; Tue, 16 Aug 2022 02:37:24 +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 1oNmSK-0007FY-LP for xen-devel@lists.xenproject.org; Tue, 16 Aug 2022 02:37:24 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 5ba08e45-1d0c-11ed-924f-1f966e50362f; Tue, 16 Aug 2022 04:37:23 +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 B6C16106F; Mon, 15 Aug 2022 19:37:23 -0700 (PDT) Received: from a011292.shanghai.arm.com (a011292.shanghai.arm.com [10.169.190.94]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 087D63F70D; Mon, 15 Aug 2022 19:37:18 -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: 5ba08e45-1d0c-11ed-924f-1f966e50362f From: Penny Zheng To: xen-devel@lists.xenproject.org Cc: wei.chen@arm.com, Penny Zheng , Stefano Stabellini , Julien Grall , Bertrand Marquis , Volodymyr Babchuk , Andrew Cooper , George Dunlap , Jan Beulich , Wei Liu , Penny Zheng , Julien Grall Subject: [PATCH v10 2/9] xen: do not free reserved memory into heap Date: Tue, 16 Aug 2022 10:36:51 +0800 Message-Id: <20220816023658.3551936-3-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220816023658.3551936-1-Penny.Zheng@arm.com> References: <20220816023658.3551936-1-Penny.Zheng@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1660617478771100002 Content-Type: text/plain; charset="utf-8" Pages used as guest RAM for static domain, shall be reserved to this domain only. So in case reserved pages being used for other purpose, users shall not free them back to heap, even when last ref gets dropped. This commit introduces a new helper free_domstatic_page to free static page in runtime, and free_staticmem_pages will be called by it in runtime, so let's drop the __init flag. Wrapper #ifdef CONFIG_STATIC_MEMORY around function declaration( free_staticmem_pages, free_domstatic_page, etc) is kinds of redundant, so we decide to remove it here. Signed-off-by: Penny Zheng Reviewed-by: Jan Beulich Reviewed-by: Julien Grall --- v10 changes: - let Arm keep #define PGC_static 0 private, with the generic fallback remaining in page_alloc.c - change ASSERT(d) to ASSERT_UNREACHABLE() to be more robust looking forward, and also add a printk() to log the problem - mention the removal of #ifdef CONFIG_STATIC_MEMORY in commit message --- v9 changes: - move free_domheap_page into else-condition - considering scrubbing static pages, domain dying case and opt_scrub_domhe= ap both donot apply to static pages. - as unowned static pages don't make themselves to free_domstatic_page at the moment, remove else-condition and add ASSERT(d) at the top of the function --- v8 changes: - introduce new helper free_domstatic_page - let put_page call free_domstatic_page for static page, when last ref drops - #define PGC_static zero when !CONFIG_STATIC_MEMORY, as it is used outside page_alloc.c --- v7 changes: - protect free_staticmem_pages with heap_lock to match its reverse function acquire_staticmem_pages --- v6 changes: - adapt to PGC_static - remove #ifdef aroud function declaration --- v5 changes: - In order to avoid stub functions, we #define PGC_staticmem to non-zero on= ly when CONFIG_STATIC_MEMORY - use "unlikely()" around pg->count_info & PGC_staticmem - remove pointless "if", since mark_page_free() is going to set count_info to PGC_state_free and by consequence clear PGC_staticmem - move #define PGC_staticmem 0 to mm.h --- v4 changes: - no changes --- v3 changes: - fix possible racy issue in free_staticmem_pages() - introduce a stub free_staticmem_pages() for the !CONFIG_STATIC_MEMORY case - move the change to free_heap_pages() to cover other potential call sites - fix the indentation --- v2 changes: - new commit --- --- xen/arch/arm/include/asm/mm.h | 6 +++++- xen/arch/arm/mm.c | 5 ++++- xen/common/page_alloc.c | 39 ++++++++++++++++++++++++++++++++--- xen/include/xen/mm.h | 3 +-- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/xen/arch/arm/include/asm/mm.h b/xen/arch/arm/include/asm/mm.h index da25251cda..749fbefa0c 100644 --- a/xen/arch/arm/include/asm/mm.h +++ b/xen/arch/arm/include/asm/mm.h @@ -121,9 +121,13 @@ struct page_info /* Page is Xen heap? */ #define _PGC_xen_heap PG_shift(2) #define PGC_xen_heap PG_mask(1, 2) - /* Page is static memory */ +#ifdef CONFIG_STATIC_MEMORY +/* Page is static memory */ #define _PGC_static PG_shift(3) #define PGC_static PG_mask(1, 3) +#else +#define PGC_static 0 +#endif /* ... */ /* Page is broken? */ #define _PGC_broken PG_shift(7) diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c index b42cddb1b4..fbdab5598c 100644 --- a/xen/arch/arm/mm.c +++ b/xen/arch/arm/mm.c @@ -1496,7 +1496,10 @@ void put_page(struct page_info *page) =20 if ( unlikely((nx & PGC_count_mask) =3D=3D 0) ) { - free_domheap_page(page); + if ( unlikely(nx & PGC_static) ) + free_domstatic_page(page); + else + free_domheap_page(page); } } =20 diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 00fa24e330..5e97dcaa26 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -2692,12 +2692,14 @@ struct domain *get_pg_owner(domid_t domid) =20 #ifdef CONFIG_STATIC_MEMORY /* Equivalent of free_heap_pages to free nr_mfns pages of static memory. */ -void __init free_staticmem_pages(struct page_info *pg, unsigned long nr_mf= ns, - bool need_scrub) +void free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns, + bool need_scrub) { mfn_t mfn =3D page_to_mfn(pg); unsigned long i; =20 + spin_lock(&heap_lock); + for ( i =3D 0; i < nr_mfns; i++ ) { mark_page_free(&pg[i], mfn_add(mfn, i)); @@ -2708,9 +2710,40 @@ void __init free_staticmem_pages(struct page_info *p= g, unsigned long nr_mfns, scrub_one_page(pg); } =20 - /* In case initializing page of static memory, mark it PGC_static.= */ pg[i].count_info |=3D PGC_static; } + + spin_unlock(&heap_lock); +} + +void free_domstatic_page(struct page_info *page) +{ + struct domain *d =3D page_get_owner(page); + bool drop_dom_ref; + + if ( unlikely(!d) ) + { + ASSERT_UNREACHABLE(); + printk("The about-to-free static page %"PRI_mfn" must be owned by = a domain\n", + mfn_x(page_to_mfn(page))); + return; + } + + ASSERT_ALLOC_CONTEXT(); + + /* NB. May recursively lock from relinquish_memory(). */ + spin_lock_recursive(&d->page_alloc_lock); + + arch_free_heap_page(d, page); + + drop_dom_ref =3D !domain_adjust_tot_pages(d, -1); + + spin_unlock_recursive(&d->page_alloc_lock); + + free_staticmem_pages(page, 1, scrub_debug); + + if ( drop_dom_ref ) + put_domain(d); } =20 /* diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index 35b065146f..deadf4b2a1 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -85,13 +85,12 @@ bool scrub_free_pages(void); } while ( false ) #define FREE_XENHEAP_PAGE(p) FREE_XENHEAP_PAGES(p, 0) =20 -#ifdef CONFIG_STATIC_MEMORY /* These functions are for static memory */ void free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns, bool need_scrub); +void free_domstatic_page(struct page_info *page); int acquire_domstatic_pages(struct domain *d, mfn_t smfn, unsigned int nr_= mfns, unsigned int memflags); -#endif =20 /* Map machine page range in Xen virtual address space. */ int map_pages_to_xen( --=20 2.25.1 From nobody Mon Apr 29 04:34:24 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 166061748088463.137012730550396; Mon, 15 Aug 2022 19:38:00 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.387837.624310 (Exim 4.92) (envelope-from ) id 1oNmSO-00085P-K9; Tue, 16 Aug 2022 02:37:28 +0000 Received: by outflank-mailman (output) from mailman id 387837.624310; Tue, 16 Aug 2022 02:37:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oNmSO-00085E-Gr; Tue, 16 Aug 2022 02:37:28 +0000 Received: by outflank-mailman (input) for mailman id 387837; Tue, 16 Aug 2022 02:37:28 +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 1oNmSO-0007FY-62 for xen-devel@lists.xenproject.org; Tue, 16 Aug 2022 02:37:28 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 5df339fd-1d0c-11ed-924f-1f966e50362f; Tue, 16 Aug 2022 04:37: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 91C27106F; Mon, 15 Aug 2022 19:37:27 -0700 (PDT) Received: from a011292.shanghai.arm.com (a011292.shanghai.arm.com [10.169.190.94]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 677453F70D; Mon, 15 Aug 2022 19:37:23 -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: 5df339fd-1d0c-11ed-924f-1f966e50362f From: Penny Zheng To: xen-devel@lists.xenproject.org Cc: wei.chen@arm.com, Penny Zheng , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu , Julien Grall , Penny Zheng Subject: [PATCH v10 3/9] xen: do not merge reserved pages in free_heap_pages() Date: Tue, 16 Aug 2022 10:36:52 +0800 Message-Id: <20220816023658.3551936-4-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220816023658.3551936-1-Penny.Zheng@arm.com> References: <20220816023658.3551936-1-Penny.Zheng@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1660617482804100001 Content-Type: text/plain; charset="utf-8" The code in free_heap_pages() will try to merge pages with the successor/predecessor if pages are suitably aligned. So if the pages reserved are right next to the pages given to the heap allocator, free_heap_pages() will merge them, and give the reserved pages to heap allocator accidentally as a result. So in order to avoid the above scenario, this commit updates free_heap_page= s() to check whether the predecessor and/or successor has PGC_static set, when trying to merge the about-to-be-freed chunk with the predecessor and/or successor. Suggested-by: Julien Grall Signed-off-by: Penny Zheng Reviewed-by: Jan Beulich Reviewed-by: Julien Grall --- v10 changes: - commit message typo fix --- v9 changes: - no change --- v8 changes: - no change --- v7 changes: - no change --- v6 changes: - adapt to PGC_static --- v5 changes: - change PGC_reserved to adapt to PGC_staticmem --- v4 changes: - no changes --- v3 changes: - no changes --- v2 changes: - new commit --- xen/common/page_alloc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 5e97dcaa26..1be7f671dc 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -1483,6 +1483,7 @@ static void free_heap_pages( /* Merge with predecessor block? */ if ( !mfn_valid(page_to_mfn(predecessor)) || !page_state_is(predecessor, free) || + (predecessor->count_info & PGC_static) || (PFN_ORDER(predecessor) !=3D order) || (phys_to_nid(page_to_maddr(predecessor)) !=3D node) ) break; @@ -1506,6 +1507,7 @@ static void free_heap_pages( /* Merge with successor block? */ if ( !mfn_valid(page_to_mfn(successor)) || !page_state_is(successor, free) || + (successor->count_info & PGC_static) || (PFN_ORDER(successor) !=3D order) || (phys_to_nid(page_to_maddr(successor)) !=3D node) ) break; --=20 2.25.1 From nobody Mon Apr 29 04:34:24 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 1660617474144692.9452994757764; Mon, 15 Aug 2022 19:37:54 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.387839.624321 (Exim 4.92) (envelope-from ) id 1oNmST-0008TI-U2; Tue, 16 Aug 2022 02:37:33 +0000 Received: by outflank-mailman (output) from mailman id 387839.624321; Tue, 16 Aug 2022 02:37:33 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oNmST-0008TB-RF; Tue, 16 Aug 2022 02:37:33 +0000 Received: by outflank-mailman (input) for mailman id 387839; Tue, 16 Aug 2022 02:37:32 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oNmSS-0008P8-S5 for xen-devel@lists.xenproject.org; Tue, 16 Aug 2022 02:37:32 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id 6099ba52-1d0c-11ed-bd2e-47488cf2e6aa; Tue, 16 Aug 2022 04:37:31 +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 F0B3A106F; Mon, 15 Aug 2022 19:37:31 -0700 (PDT) Received: from a011292.shanghai.arm.com (a011292.shanghai.arm.com [10.169.190.94]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 486FE3F70D; Mon, 15 Aug 2022 19:37:27 -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: 6099ba52-1d0c-11ed-bd2e-47488cf2e6aa From: Penny Zheng To: xen-devel@lists.xenproject.org Cc: wei.chen@arm.com, Penny Zheng , Stefano Stabellini , Julien Grall , Bertrand Marquis , Volodymyr Babchuk , Andrew Cooper , George Dunlap , Jan Beulich , Wei Liu , Penny Zheng , Julien Grall Subject: [PATCH v10 4/9] xen: add field "flags" to cover all internal CDF_XXX Date: Tue, 16 Aug 2022 10:36:53 +0800 Message-Id: <20220816023658.3551936-5-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220816023658.3551936-1-Penny.Zheng@arm.com> References: <20220816023658.3551936-1-Penny.Zheng@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1660617475098100001 Content-Type: text/plain; charset="utf-8" With more and more CDF_xxx internal flags in and to save the space, this commit introduces a new field "flags" in struct domain to store CDF_* internal flags directly. Another new CDF_xxx will be introduced in the next patch. Signed-off-by: Penny Zheng Acked-by: Julien Grall --- v10 changes: - no change --- v9 changes: - no change --- v8 changes: - no change --- v7 changes: - no change --- v6 changes: - no change --- v5 changes: - no change --- v4 changes: - no change --- v3 changes: - change fixed width type uint32_t to unsigned int - change "flags" to a more descriptive name "cdf" --- v2 changes: - let "flags" live in the struct domain. So other arch can take advantage of it in the future - fix coding style --- xen/arch/arm/domain.c | 2 -- xen/arch/arm/include/asm/domain.h | 3 +-- xen/common/domain.c | 3 +++ xen/include/xen/sched.h | 3 +++ 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/xen/arch/arm/domain.c b/xen/arch/arm/domain.c index 2cd481979c..a963884e81 100644 --- a/xen/arch/arm/domain.c +++ b/xen/arch/arm/domain.c @@ -712,8 +712,6 @@ int arch_domain_create(struct domain *d, ioreq_domain_init(d); #endif =20 - d->arch.directmap =3D flags & CDF_directmap; - /* p2m_init relies on some value initialized by the IOMMU subsystem */ if ( (rc =3D iommu_domain_init(d, config->iommu_opts)) !=3D 0 ) goto fail; diff --git a/xen/arch/arm/include/asm/domain.h b/xen/arch/arm/include/asm/d= omain.h index cd9ce19b4b..26a8348eed 100644 --- a/xen/arch/arm/include/asm/domain.h +++ b/xen/arch/arm/include/asm/domain.h @@ -29,7 +29,7 @@ enum domain_type { #define is_64bit_domain(d) (0) #endif =20 -#define is_domain_direct_mapped(d) (d)->arch.directmap +#define is_domain_direct_mapped(d) ((d)->cdf & CDF_directmap) =20 /* * Is the domain using the host memory layout? @@ -104,7 +104,6 @@ struct arch_domain void *tee; #endif =20 - bool directmap; } __cacheline_aligned; =20 struct arch_vcpu diff --git a/xen/common/domain.c b/xen/common/domain.c index 618410e3b2..7062393e37 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -567,6 +567,9 @@ struct domain *domain_create(domid_t domid, /* Sort out our idea of is_system_domain(). */ d->domain_id =3D domid; =20 + /* Holding CDF_* internal flags. */ + d->cdf =3D flags; + /* Debug sanity. */ ASSERT(is_system_domain(d) ? config =3D=3D NULL : config !=3D NULL); =20 diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index e2b3b6daa3..1cf629e7ec 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -596,6 +596,9 @@ struct domain struct ioreq_server *server[MAX_NR_IOREQ_SERVERS]; } ioreq_server; #endif + + /* Holding CDF_* constant. Internal flags for domain creation. */ + unsigned int cdf; }; =20 static inline struct page_list_head *page_to_list( --=20 2.25.1 From nobody Mon Apr 29 04:34:24 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 1660617478691791.6700530204229; Mon, 15 Aug 2022 19:37:58 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.387841.624332 (Exim 4.92) (envelope-from ) id 1oNmSY-0000TM-9L; Tue, 16 Aug 2022 02:37:38 +0000 Received: by outflank-mailman (output) from mailman id 387841.624332; Tue, 16 Aug 2022 02:37:38 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oNmSY-0000T5-4V; Tue, 16 Aug 2022 02:37:38 +0000 Received: by outflank-mailman (input) for mailman id 387841; Tue, 16 Aug 2022 02:37:36 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oNmSW-0008P8-UF for xen-devel@lists.xenproject.org; Tue, 16 Aug 2022 02:37:36 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id 63280e86-1d0c-11ed-bd2e-47488cf2e6aa; Tue, 16 Aug 2022 04:37:36 +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 5B9CF106F; Mon, 15 Aug 2022 19:37:36 -0700 (PDT) Received: from a011292.shanghai.arm.com (a011292.shanghai.arm.com [10.169.190.94]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id A71253F70D; Mon, 15 Aug 2022 19:37:31 -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: 63280e86-1d0c-11ed-bd2e-47488cf2e6aa From: Penny Zheng To: xen-devel@lists.xenproject.org Cc: wei.chen@arm.com, Penny Zheng , Stefano Stabellini , Julien Grall , Bertrand Marquis , Volodymyr Babchuk , Andrew Cooper , George Dunlap , Jan Beulich , Wei Liu , Penny Zheng , Julien Grall Subject: [PATCH v10 5/9] xen/arm: introduce CDF_staticmem Date: Tue, 16 Aug 2022 10:36:54 +0800 Message-Id: <20220816023658.3551936-6-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220816023658.3551936-1-Penny.Zheng@arm.com> References: <20220816023658.3551936-1-Penny.Zheng@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1660617480608100007 Content-Type: text/plain; charset="utf-8" In order to have an easy and quick way to find out whether this domain memo= ry is statically configured, this commit introduces a new flag CDF_staticmem a= nd a new helper is_domain_using_staticmem() to tell. Signed-off-by: Penny Zheng Acked-by: Julien Grall Acked-by: Jan Beulich --- v10 changes: - no change --- v9 changes: - no change --- v8 changes: - #ifdef-ary around is_domain_using_staticmem() is not needed anymore --- v7 changes: - IS_ENABLED(CONFIG_STATIC_MEMORY) would not be needed anymore --- v6 changes: - move non-zero is_domain_using_staticmem() from ARM header to common header --- v5 changes: - guard "is_domain_using_staticmem" under CONFIG_STATIC_MEMORY - #define is_domain_using_staticmem zero if undefined --- v4 changes: - no changes --- v3 changes: - change name from "is_domain_static()" to "is_domain_using_staticmem" --- v2 changes: - change name from "is_domain_on_static_allocation" to "is_domain_static() --- xen/arch/arm/domain_build.c | 5 ++++- xen/include/xen/domain.h | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index 3fd1186b53..b76a84e8f5 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -3287,9 +3287,12 @@ void __init create_domUs(void) if ( !dt_device_is_compatible(node, "xen,domain") ) continue; =20 + if ( dt_find_property(node, "xen,static-mem", NULL) ) + flags |=3D CDF_staticmem; + if ( dt_property_read_bool(node, "direct-map") ) { - if ( !IS_ENABLED(CONFIG_STATIC_MEMORY) || !dt_find_property(no= de, "xen,static-mem", NULL) ) + if ( !(flags & CDF_staticmem) ) panic("direct-map is not valid for domain %s without stati= c allocation.\n", dt_node_name(node)); =20 diff --git a/xen/include/xen/domain.h b/xen/include/xen/domain.h index 628b14b086..2c8116afba 100644 --- a/xen/include/xen/domain.h +++ b/xen/include/xen/domain.h @@ -35,6 +35,14 @@ void arch_get_domain_info(const struct domain *d, /* Should domain memory be directly mapped? */ #define CDF_directmap (1U << 1) #endif +/* Is domain memory on static allocation? */ +#ifdef CONFIG_STATIC_MEMORY +#define CDF_staticmem (1U << 2) +#else +#define CDF_staticmem 0 +#endif + +#define is_domain_using_staticmem(d) ((d)->cdf & CDF_staticmem) =20 /* * Arch-specifics. --=20 2.25.1 From nobody Mon Apr 29 04:34:24 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 1660617483017891.9323123760873; Mon, 15 Aug 2022 19:38:03 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.387845.624343 (Exim 4.92) (envelope-from ) id 1oNmSc-00014l-LL; Tue, 16 Aug 2022 02:37:42 +0000 Received: by outflank-mailman (output) from mailman id 387845.624343; Tue, 16 Aug 2022 02:37:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oNmSc-00014Z-Hi; Tue, 16 Aug 2022 02:37:42 +0000 Received: by outflank-mailman (input) for mailman id 387845; Tue, 16 Aug 2022 02:37:41 +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 1oNmSa-0007FY-R0 for xen-devel@lists.xenproject.org; Tue, 16 Aug 2022 02:37:40 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 6555659d-1d0c-11ed-924f-1f966e50362f; Tue, 16 Aug 2022 04:37:39 +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 01C66113E; Mon, 15 Aug 2022 19:37:40 -0700 (PDT) Received: from a011292.shanghai.arm.com (a011292.shanghai.arm.com [10.169.190.94]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 140743F70D; Mon, 15 Aug 2022 19:37:35 -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: 6555659d-1d0c-11ed-924f-1f966e50362f From: Penny Zheng To: xen-devel@lists.xenproject.org Cc: wei.chen@arm.com, Penny Zheng , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu , Penny Zheng Subject: [PATCH v10 6/9] xen: unpopulate memory when domain is static Date: Tue, 16 Aug 2022 10:36:55 +0800 Message-Id: <20220816023658.3551936-7-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220816023658.3551936-1-Penny.Zheng@arm.com> References: <20220816023658.3551936-1-Penny.Zheng@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1660617484814100003 Content-Type: text/plain; charset="utf-8" Today when a domain unpopulates the memory on runtime, they will always hand the memory back to the heap allocator. And it will be a problem if dom= ain is static. Pages as guest RAM for static domain shall be reserved to only this domain and not be used for any other purposes, so they shall never go back to heap allocator. This commit puts reserved pages on the new list resv_page_list only after having taken them off the "normal" list, when the last ref dropped. Signed-off-by: Penny Zheng Acked-by: Jan Beulich Acked-by: Julien Grall --- v10 change: - Do not skip the list addition in that one special case --- v9 change: - remove macro helper put_static_page, and just expand its code inside free_domstatic_page --- v8 changes: - adapt this patch for newly introduced free_domstatic_page - order as a parameter is not needed here, as all staticmem operations are limited to order-0 regions - move d->page_alloc_lock after operation on d->resv_page_list --- v7 changes: - Add page on the rsv_page_list *after* it has been freed --- v6 changes: - refine in-code comment - move PGC_static !CONFIG_STATIC_MEMORY definition to common header --- v5 changes: - adapt this patch for PGC_staticmem --- v4 changes: - no changes --- v3 changes: - have page_list_del() just once out of the if() - remove resv_pages counter - make arch_free_heap_page be an expression, not a compound statement. --- v2 changes: - put reserved pages on resv_page_list after having taken them off the "normal" list --- xen/common/domain.c | 4 ++++ xen/common/page_alloc.c | 7 +++++-- xen/include/xen/sched.h | 3 +++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/xen/common/domain.c b/xen/common/domain.c index 7062393e37..c23f449451 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -604,6 +604,10 @@ struct domain *domain_create(domid_t domid, INIT_PAGE_LIST_HEAD(&d->page_list); INIT_PAGE_LIST_HEAD(&d->extra_page_list); INIT_PAGE_LIST_HEAD(&d->xenpage_list); +#ifdef CONFIG_STATIC_MEMORY + INIT_PAGE_LIST_HEAD(&d->resv_page_list); +#endif + =20 spin_lock_init(&d->node_affinity_lock); d->node_affinity =3D NODE_MASK_ALL; diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 1be7f671dc..25521af600 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -2740,10 +2740,13 @@ void free_domstatic_page(struct page_info *page) =20 drop_dom_ref =3D !domain_adjust_tot_pages(d, -1); =20 - spin_unlock_recursive(&d->page_alloc_lock); - free_staticmem_pages(page, 1, scrub_debug); =20 + /* Add page on the resv_page_list *after* it has been freed. */ + page_list_add_tail(page, &d->resv_page_list); + + spin_unlock_recursive(&d->page_alloc_lock); + if ( drop_dom_ref ) put_domain(d); } diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 1cf629e7ec..956e0f9dca 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -381,6 +381,9 @@ struct domain struct page_list_head page_list; /* linked list */ struct page_list_head extra_page_list; /* linked list (size extra_page= s) */ struct page_list_head xenpage_list; /* linked list (size xenheap_pages= ) */ +#ifdef CONFIG_STATIC_MEMORY + struct page_list_head resv_page_list; /* linked list */ +#endif =20 /* * This field should only be directly accessed by domain_adjust_tot_pa= ges() --=20 2.25.1 From nobody Mon Apr 29 04:34:24 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 166061758859178.71156027169707; Mon, 15 Aug 2022 19:39:48 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.387872.624365 (Exim 4.92) (envelope-from ) id 1oNmUL-0003SH-9f; Tue, 16 Aug 2022 02:39:29 +0000 Received: by outflank-mailman (output) from mailman id 387872.624365; Tue, 16 Aug 2022 02:39:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oNmUL-0003SA-6S; Tue, 16 Aug 2022 02:39:29 +0000 Received: by outflank-mailman (input) for mailman id 387872; Tue, 16 Aug 2022 02:39:28 +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 1oNmSe-0007FY-NQ for xen-devel@lists.xenproject.org; Tue, 16 Aug 2022 02:37:44 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 67a0112f-1d0c-11ed-924f-1f966e50362f; Tue, 16 Aug 2022 04:37:43 +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 D42D8106F; Mon, 15 Aug 2022 19:37:43 -0700 (PDT) Received: from a011292.shanghai.arm.com (a011292.shanghai.arm.com [10.169.190.94]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id A9D753F70D; Mon, 15 Aug 2022 19:37:39 -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: 67a0112f-1d0c-11ed-924f-1f966e50362f From: Penny Zheng To: xen-devel@lists.xenproject.org Cc: wei.chen@arm.com, Penny Zheng , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu , Penny Zheng , Julien Grall Subject: [PATCH v10 7/9] xen: introduce prepare_staticmem_pages Date: Tue, 16 Aug 2022 10:36:56 +0800 Message-Id: <20220816023658.3551936-8-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220816023658.3551936-1-Penny.Zheng@arm.com> References: <20220816023658.3551936-1-Penny.Zheng@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1660617589258100001 Content-Type: text/plain; charset="utf-8" Later, we want to use acquire_domstatic_pages() for populating memory for static domain on runtime, however, there are a lot of pointless work (checking mfn_valid(), scrubbing the free part, cleaning the cache...) considering we know the page is valid and belong to the guest. This commit splits acquire_staticmem_pages() in two parts, and introduces prepare_staticmem_pages to bypass all "pointless work". Signed-off-by: Penny Zheng Acked-by: Jan Beulich Acked-by: Julien Grall --- v10 changes: - no change --- v9 changes: - no change --- v8 changes: - no change --- v7 changes: - no change --- v6 changes: - adapt to PGC_static --- v5 changes: - new commit --- xen/common/page_alloc.c | 61 ++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 25521af600..0ee697705c 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -2751,26 +2751,13 @@ void free_domstatic_page(struct page_info *page) put_domain(d); } =20 -/* - * Acquire nr_mfns contiguous reserved pages, starting at #smfn, of - * static memory. - * This function needs to be reworked if used outside of boot. - */ -static struct page_info * __init acquire_staticmem_pages(mfn_t smfn, - unsigned long nr_= mfns, - unsigned int memf= lags) +static bool __init prepare_staticmem_pages(struct page_info *pg, + unsigned long nr_mfns, + unsigned int memflags) { bool need_tlbflush =3D false; uint32_t tlbflush_timestamp =3D 0; unsigned long i; - struct page_info *pg; - - ASSERT(nr_mfns); - for ( i =3D 0; i < nr_mfns; i++ ) - if ( !mfn_valid(mfn_add(smfn, i)) ) - return NULL; - - pg =3D mfn_to_page(smfn); =20 spin_lock(&heap_lock); =20 @@ -2781,7 +2768,7 @@ static struct page_info * __init acquire_staticmem_pa= ges(mfn_t smfn, { printk(XENLOG_ERR "pg[%lu] Static MFN %"PRI_mfn" c=3D%#lx t=3D%#x\n", - i, mfn_x(smfn) + i, + i, mfn_x(page_to_mfn(pg)) + i, pg[i].count_info, pg[i].tlbflush_timestamp); goto out_err; } @@ -2805,6 +2792,38 @@ static struct page_info * __init acquire_staticmem_p= ages(mfn_t smfn, if ( need_tlbflush ) filtered_flush_tlb_mask(tlbflush_timestamp); =20 + return true; + + out_err: + while ( i-- ) + pg[i].count_info =3D PGC_static | PGC_state_free; + + spin_unlock(&heap_lock); + + return false; +} + +/* + * Acquire nr_mfns contiguous reserved pages, starting at #smfn, of + * static memory. + * This function needs to be reworked if used outside of boot. + */ +static struct page_info * __init acquire_staticmem_pages(mfn_t smfn, + unsigned long nr_= mfns, + unsigned int memf= lags) +{ + unsigned long i; + struct page_info *pg; + + ASSERT(nr_mfns); + for ( i =3D 0; i < nr_mfns; i++ ) + if ( !mfn_valid(mfn_add(smfn, i)) ) + return NULL; + + pg =3D mfn_to_page(smfn); + if ( !prepare_staticmem_pages(pg, nr_mfns, memflags) ) + return NULL; + /* * Ensure cache and RAM are consistent for platforms where the guest * can control its own visibility of/through the cache. @@ -2813,14 +2832,6 @@ static struct page_info * __init acquire_staticmem_p= ages(mfn_t smfn, flush_page_to_ram(mfn_x(smfn) + i, !(memflags & MEMF_no_icache_flu= sh)); =20 return pg; - - out_err: - while ( i-- ) - pg[i].count_info =3D PGC_static | PGC_state_free; - - spin_unlock(&heap_lock); - - return NULL; } =20 /* --=20 2.25.1 From nobody Mon Apr 29 04:34:24 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 1660617592223839.6358856203166; Mon, 15 Aug 2022 19:39:52 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.387881.624376 (Exim 4.92) (envelope-from ) id 1oNmUN-0003l3-Gv; Tue, 16 Aug 2022 02:39:31 +0000 Received: by outflank-mailman (output) from mailman id 387881.624376; Tue, 16 Aug 2022 02:39: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 1oNmUN-0003ks-Cz; Tue, 16 Aug 2022 02:39:31 +0000 Received: by outflank-mailman (input) for mailman id 387881; Tue, 16 Aug 2022 02:39: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 1oNmSi-0007FY-9L for xen-devel@lists.xenproject.org; Tue, 16 Aug 2022 02:37:48 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 69bc66e1-1d0c-11ed-924f-1f966e50362f; Tue, 16 Aug 2022 04:37:47 +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 767A0106F; Mon, 15 Aug 2022 19:37:47 -0700 (PDT) Received: from a011292.shanghai.arm.com (a011292.shanghai.arm.com [10.169.190.94]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 8A7B33F70D; Mon, 15 Aug 2022 19:37:43 -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: 69bc66e1-1d0c-11ed-924f-1f966e50362f From: Penny Zheng To: xen-devel@lists.xenproject.org Cc: wei.chen@arm.com, Penny Zheng , Andrew Cooper , George Dunlap , Jan Beulich , Julien Grall , Stefano Stabellini , Wei Liu , Penny Zheng Subject: [PATCH v10 8/9] xen: retrieve reserved pages on populate_physmap Date: Tue, 16 Aug 2022 10:36:57 +0800 Message-Id: <20220816023658.3551936-9-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220816023658.3551936-1-Penny.Zheng@arm.com> References: <20220816023658.3551936-1-Penny.Zheng@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1660617593250100001 When a static domain populates memory through populate_physmap at runtime, it shall retrieve reserved pages from resv_page_list to make sure that guest RAM is still restricted in statically configured memory regions. This commit also introduces a new helper acquire_reserved_page to make it w= ork. Signed-off-by: Penny Zheng --- v10 changes: - add lock on the fail path --- v9 changes: - Use ASSERT_ALLOC_CONTEXT() in acquire_reserved_page - Add free_staticmem_pages to undo prepare_staticmem_pages when assign_domstatic_pages - Remove redundant static in error message --- v8 changes: - As concurrent free/allocate could modify the resv_page_list, we still need the lock --- v7 changes: - remove the lock, since we add the page to rsv_page_list after it has been totally freed. --- v6 changes: - drop the lock before returning --- v5 changes: - extract common codes for assigning pages into a helper assign_domstatic_p= ages - refine commit message - remove stub function acquire_reserved_page - Alloc/free of memory can happen concurrently. So access to rsv_page_list needs to be protected with a spinlock --- v4 changes=EF=BC=9A - miss dropping __init in acquire_domstatic_pages - add the page back to the reserved list in case of error - remove redundant printk - refine log message and make it warn level --- v3 changes: - move is_domain_using_staticmem to the common header file - remove #ifdef CONFIG_STATIC_MEMORY-ary - remove meaningless page_to_mfn(page) in error log --- v2 changes: - introduce acquire_reserved_page to retrieve reserved pages from resv_page_list - forbid non-zero-order requests in populate_physmap - let is_domain_static return ((void)(d), false) on x86 --- xen/common/memory.c | 23 +++++++++++++ xen/common/page_alloc.c | 76 ++++++++++++++++++++++++++++++++--------- xen/include/xen/mm.h | 1 + 3 files changed, 83 insertions(+), 17 deletions(-) diff --git a/xen/common/memory.c b/xen/common/memory.c index bc89442ba5..ae8163a738 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -245,6 +245,29 @@ static void populate_physmap(struct memop_args *a) =20 mfn =3D _mfn(gpfn); } + else if ( is_domain_using_staticmem(d) ) + { + /* + * No easy way to guarantee the retrieved pages are contig= uous, + * so forbid non-zero-order requests here. + */ + if ( a->extent_order !=3D 0 ) + { + gdprintk(XENLOG_WARNING, + "Cannot allocate static order-%u pages for %p= d\n", + a->extent_order, d); + goto out; + } + + mfn =3D acquire_reserved_page(d, a->memflags); + if ( mfn_eq(mfn, INVALID_MFN) ) + { + gdprintk(XENLOG_WARNING, + "%pd: failed to retrieve a reserved page\n", + d); + goto out; + } + } else { page =3D alloc_domheap_pages(d, a->extent_order, a->memfla= gs); diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 0ee697705c..9c6d369d10 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -2751,9 +2751,8 @@ void free_domstatic_page(struct page_info *page) put_domain(d); } =20 -static bool __init prepare_staticmem_pages(struct page_info *pg, - unsigned long nr_mfns, - unsigned int memflags) +static bool prepare_staticmem_pages(struct page_info *pg, unsigned long nr= _mfns, + unsigned int memflags) { bool need_tlbflush =3D false; uint32_t tlbflush_timestamp =3D 0; @@ -2834,21 +2833,9 @@ static struct page_info * __init acquire_staticmem_p= ages(mfn_t smfn, return pg; } =20 -/* - * Acquire nr_mfns contiguous pages, starting at #smfn, of static memory, - * then assign them to one specific domain #d. - */ -int __init acquire_domstatic_pages(struct domain *d, mfn_t smfn, - unsigned int nr_mfns, unsigned int memf= lags) +static int assign_domstatic_pages(struct domain *d, struct page_info *pg, + unsigned int nr_mfns, unsigned int memfl= ags) { - struct page_info *pg; - - ASSERT_ALLOC_CONTEXT(); - - pg =3D acquire_staticmem_pages(smfn, nr_mfns, memflags); - if ( !pg ) - return -ENOENT; - if ( !d || (memflags & (MEMF_no_owner | MEMF_no_refcount)) ) { /* @@ -2867,6 +2854,61 @@ int __init acquire_domstatic_pages(struct domain *d,= mfn_t smfn, =20 return 0; } + +/* + * Acquire nr_mfns contiguous pages, starting at #smfn, of static memory, + * then assign them to one specific domain #d. + */ +int __init acquire_domstatic_pages(struct domain *d, mfn_t smfn, + unsigned int nr_mfns, unsigned int memf= lags) +{ + struct page_info *pg; + + ASSERT_ALLOC_CONTEXT(); + + pg =3D acquire_staticmem_pages(smfn, nr_mfns, memflags); + if ( !pg ) + return -ENOENT; + + if ( assign_domstatic_pages(d, pg, nr_mfns, memflags) ) + return -EINVAL; + + return 0; +} + +/* + * Acquire a page from reserved page list(resv_page_list), when populating + * memory for static domain on runtime. + */ +mfn_t acquire_reserved_page(struct domain *d, unsigned int memflags) +{ + struct page_info *page; + + ASSERT_ALLOC_CONTEXT(); + + /* Acquire a page from reserved page list(resv_page_list). */ + spin_lock(&d->page_alloc_lock); + page =3D page_list_remove_head(&d->resv_page_list); + spin_unlock(&d->page_alloc_lock); + if ( unlikely(!page) ) + return INVALID_MFN; + + if ( !prepare_staticmem_pages(page, 1, memflags) ) + goto fail; + + if ( assign_domstatic_pages(d, page, 1, memflags) ) + goto fail_assign; + + return page_to_mfn(page); + + fail_assign: + free_staticmem_pages(page, 1, memflags & MEMF_no_scrub); + fail: + spin_lock(&d->page_alloc_lock); + page_list_add_tail(page, &d->resv_page_list); + spin_unlock(&d->page_alloc_lock); + return INVALID_MFN; +} #endif =20 /* diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index deadf4b2a1..5d29aea7ad 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -198,6 +198,7 @@ struct npfec { #else #define MAX_ORDER 20 /* 2^20 contiguous pages */ #endif +mfn_t acquire_reserved_page(struct domain *d, unsigned int memflags); =20 /* Private domain structs for DOMID_XEN, DOMID_IO, etc. */ extern struct domain *dom_xen, *dom_io; --=20 2.25.1 From nobody Mon Apr 29 04:34:24 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 1660617496473334.75176461127216; Mon, 15 Aug 2022 19:38:16 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.387861.624354 (Exim 4.92) (envelope-from ) id 1oNmSn-00028b-UH; Tue, 16 Aug 2022 02:37:53 +0000 Received: by outflank-mailman (output) from mailman id 387861.624354; Tue, 16 Aug 2022 02:37:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oNmSn-00028U-Qv; Tue, 16 Aug 2022 02:37:53 +0000 Received: by outflank-mailman (input) for mailman id 387861; Tue, 16 Aug 2022 02:37:52 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oNmSm-0008P8-Ej for xen-devel@lists.xenproject.org; Tue, 16 Aug 2022 02:37:52 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id 6c45e917-1d0c-11ed-bd2e-47488cf2e6aa; Tue, 16 Aug 2022 04:37:51 +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 9630C106F; Mon, 15 Aug 2022 19:37:51 -0700 (PDT) Received: from a011292.shanghai.arm.com (a011292.shanghai.arm.com [10.169.190.94]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2CE423F70D; Mon, 15 Aug 2022 19:37:46 -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: 6c45e917-1d0c-11ed-bd2e-47488cf2e6aa From: Penny Zheng To: xen-devel@lists.xenproject.org Cc: wei.chen@arm.com, Penny Zheng , Stefano Stabellini , Julien Grall , Bertrand Marquis , Volodymyr Babchuk , Andrew Cooper , George Dunlap , Jan Beulich , Wei Liu , Penny Zheng Subject: [PATCH v10 9/9] xen: rename free_staticmem_pages to unprepare_staticmem_pages Date: Tue, 16 Aug 2022 10:36:58 +0800 Message-Id: <20220816023658.3551936-10-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220816023658.3551936-1-Penny.Zheng@arm.com> References: <20220816023658.3551936-1-Penny.Zheng@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1660617496769100001 Content-Type: text/plain; charset="utf-8" The name of free_staticmem_pages is inappropriate, considering it is the opposite of function prepare_staticmem_pages. Rename free_staticmem_pages to unprepare_staticmem_pages. Signed-off-by: Penny Zheng Acked-by: Jan Beulich --- v10 changes: - new commit --- xen/arch/arm/setup.c | 3 ++- xen/common/page_alloc.c | 15 +++++++++------ xen/include/xen/mm.h | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c index 500307edc0..4662997c7e 100644 --- a/xen/arch/arm/setup.c +++ b/xen/arch/arm/setup.c @@ -639,7 +639,8 @@ static void __init init_staticmem_pages(void) if ( mfn_x(bank_end) <=3D mfn_x(bank_start) ) return; =20 - free_staticmem_pages(mfn_to_page(bank_start), bank_pages, fals= e); + unprepare_staticmem_pages(mfn_to_page(bank_start), + bank_pages, false); } } #endif diff --git a/xen/common/page_alloc.c b/xen/common/page_alloc.c index 9c6d369d10..7306d69129 100644 --- a/xen/common/page_alloc.c +++ b/xen/common/page_alloc.c @@ -2693,9 +2693,12 @@ struct domain *get_pg_owner(domid_t domid) } =20 #ifdef CONFIG_STATIC_MEMORY -/* Equivalent of free_heap_pages to free nr_mfns pages of static memory. */ -void free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns, - bool need_scrub) +/* + * It is the opposite of prepare_staticmem_pages, and it aims to unprepare + * nr_mfns pages of static memory. + */ +void unprepare_staticmem_pages(struct page_info *pg, unsigned long nr_mfns, + bool need_scrub) { mfn_t mfn =3D page_to_mfn(pg); unsigned long i; @@ -2740,7 +2743,7 @@ void free_domstatic_page(struct page_info *page) =20 drop_dom_ref =3D !domain_adjust_tot_pages(d, -1); =20 - free_staticmem_pages(page, 1, scrub_debug); + unprepare_staticmem_pages(page, 1, scrub_debug); =20 /* Add page on the resv_page_list *after* it has been freed. */ page_list_add_tail(page, &d->resv_page_list); @@ -2848,7 +2851,7 @@ static int assign_domstatic_pages(struct domain *d, s= truct page_info *pg, =20 if ( assign_pages(pg, nr_mfns, d, memflags) ) { - free_staticmem_pages(pg, nr_mfns, memflags & MEMF_no_scrub); + unprepare_staticmem_pages(pg, nr_mfns, memflags & MEMF_no_scrub); return -EINVAL; } =20 @@ -2902,7 +2905,7 @@ mfn_t acquire_reserved_page(struct domain *d, unsigne= d int memflags) return page_to_mfn(page); =20 fail_assign: - free_staticmem_pages(page, 1, memflags & MEMF_no_scrub); + unprepare_staticmem_pages(page, 1, memflags & MEMF_no_scrub); fail: spin_lock(&d->page_alloc_lock); page_list_add_tail(page, &d->resv_page_list); diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index 5d29aea7ad..a925028ab3 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -86,8 +86,8 @@ bool scrub_free_pages(void); #define FREE_XENHEAP_PAGE(p) FREE_XENHEAP_PAGES(p, 0) =20 /* These functions are for static memory */ -void free_staticmem_pages(struct page_info *pg, unsigned long nr_mfns, - bool need_scrub); +void unprepare_staticmem_pages(struct page_info *pg, unsigned long nr_mfns, + bool need_scrub); void free_domstatic_page(struct page_info *page); int acquire_domstatic_pages(struct domain *d, mfn_t smfn, unsigned int nr_= mfns, unsigned int memflags); --=20 2.25.1