From patchwork Tue Sep 6 08:59:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 12968215 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BB0AFC6FA86 for ; Tue, 6 Sep 2022 21:37:27 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.400071.641702 (Exim 4.92) (envelope-from ) id 1oVgFz-0002bi-9x; Tue, 06 Sep 2022 21:37:19 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 400071.641702; Tue, 06 Sep 2022 21:37:19 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oVgFz-0002ad-4V; Tue, 06 Sep 2022 21:37:19 +0000 Received: by outflank-mailman (input) for mailman id 400071; Tue, 06 Sep 2022 21:37:17 +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 1oVgFx-0000Cs-C6 for xen-devel@lists.xenproject.org; Tue, 06 Sep 2022 21:37:17 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 4b380e1a-2dc2-11ed-af93-0125da4c0113; Tue, 06 Sep 2022 11:00:03 +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 D92E31756; Tue, 6 Sep 2022 02:00:12 -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 BF17D3F7B4; Tue, 6 Sep 2022 02:00:03 -0700 (PDT) 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: 4b380e1a-2dc2-11ed-af93-0125da4c0113 From: Penny Zheng To: xen-devel@lists.xenproject.org Cc: wei.chen@arm.com, Penny Zheng , Stefano Stabellini , Julien Grall , Bertrand Marquis , Volodymyr Babchuk , Penny Zheng , Julien Grall Subject: [PATCH v7 4/9] xen/arm: introduce put_page_nr and get_page_nr Date: Tue, 6 Sep 2022 16:59:36 +0800 Message-Id: <20220906085941.944592-5-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220906085941.944592-1-Penny.Zheng@arm.com> References: <20220906085941.944592-1-Penny.Zheng@arm.com> MIME-Version: 1.0 Later, we need to add the right amount of references, which should be the number of borrower domains, to the owner domain. Since we only have get_page() to increment the page reference by 1, a loop is needed per page, which is inefficient and time-consuming. To save the loop time, this commit introduces a set of new helpers put_page_nr() and get_page_nr() to increment/drop the page reference by nr. Signed-off-by: Penny Zheng Reviewed-by: Stefano Stabellini Reviewed-by: Julien Grall --- v6 change: - no change --- v5 change: - no change --- v4 changes: - fix the assert about checking overflow to make sure that the right equation return is at least equal to nr - simplify the assert about checking the underflow --- v3 changes: - check overflow with "n" - remove spurious change - bring back the check that we enter the loop only when count_info is greater than 0 --- v2 change: - new commit --- xen/arch/arm/include/asm/mm.h | 4 ++++ xen/arch/arm/mm.c | 42 +++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/xen/arch/arm/include/asm/mm.h b/xen/arch/arm/include/asm/mm.h index da25251cda..765aeddb4b 100644 --- a/xen/arch/arm/include/asm/mm.h +++ b/xen/arch/arm/include/asm/mm.h @@ -356,6 +356,10 @@ void free_init_memory(void); int guest_physmap_mark_populate_on_demand(struct domain *d, unsigned long gfn, unsigned int order); +extern bool get_page_nr(struct page_info *page, const struct domain *domain, + unsigned long nr); +extern void put_page_nr(struct page_info *page, unsigned long nr); + extern void put_page_type(struct page_info *page); static inline void put_page_and_type(struct page_info *page) { diff --git a/xen/arch/arm/mm.c b/xen/arch/arm/mm.c index c81c706c8b..71942f15e7 100644 --- a/xen/arch/arm/mm.c +++ b/xen/arch/arm/mm.c @@ -1461,21 +1461,29 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg) return 0; } -struct domain *page_get_owner_and_reference(struct page_info *page) +static struct domain *page_get_owner_and_nr_reference(struct page_info *page, + unsigned long nr) { unsigned long x, y = page->count_info; struct domain *owner; + /* Restrict nr to avoid "double" overflow */ + if ( nr >= PGC_count_mask ) + { + ASSERT_UNREACHABLE(); + return NULL; + } + do { x = y; /* * Count == 0: Page is not allocated, so we cannot take a reference. * Count == -1: Reference count would wrap, which is invalid. */ - if ( unlikely(((x + 1) & PGC_count_mask) <= 1) ) + if ( unlikely(((x + nr) & PGC_count_mask) <= nr) ) return NULL; } - while ( (y = cmpxchg(&page->count_info, x, x + 1)) != x ); + while ( (y = cmpxchg(&page->count_info, x, x + nr)) != x ); owner = page_get_owner(page); ASSERT(owner); @@ -1483,14 +1491,19 @@ struct domain *page_get_owner_and_reference(struct page_info *page) return owner; } -void put_page(struct page_info *page) +struct domain *page_get_owner_and_reference(struct page_info *page) +{ + return page_get_owner_and_nr_reference(page, 1); +} + +void put_page_nr(struct page_info *page, unsigned long nr) { unsigned long nx, x, y = page->count_info; do { - ASSERT((y & PGC_count_mask) != 0); + ASSERT((y & PGC_count_mask) >= nr); x = y; - nx = x - 1; + nx = x - nr; } while ( unlikely((y = cmpxchg(&page->count_info, x, nx)) != x) ); @@ -1500,19 +1513,30 @@ void put_page(struct page_info *page) } } -bool get_page(struct page_info *page, const struct domain *domain) +void put_page(struct page_info *page) +{ + put_page_nr(page, 1); +} + +bool get_page_nr(struct page_info *page, const struct domain *domain, + unsigned long nr) { - const struct domain *owner = page_get_owner_and_reference(page); + const struct domain *owner = page_get_owner_and_nr_reference(page, nr); if ( likely(owner == domain) ) return true; if ( owner != NULL ) - put_page(page); + put_page_nr(page, nr); return false; } +bool get_page(struct page_info *page, const struct domain *domain) +{ + return get_page_nr(page, domain, 1); +} + /* Common code requires get_page_type and put_page_type. * We don't care about typecounts so we just do the minimum to make it * happy. */