From patchwork Tue Nov 15 02:52:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043130 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 EBE7AC43217 for ; Tue, 15 Nov 2022 02:53:16 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443635.698306 (Exim 4.92) (envelope-from ) id 1oum4I-0000Km-8P; Tue, 15 Nov 2022 02:52:58 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443635.698306; Tue, 15 Nov 2022 02:52:58 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oum4I-0000Kf-4I; Tue, 15 Nov 2022 02:52:58 +0000 Received: by outflank-mailman (input) for mailman id 443635; Tue, 15 Nov 2022 02:52:56 +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 1oum4G-0000BJ-RK for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:52:56 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id 99feff1e-6490-11ed-91b6-6bf2151ebd3b; Tue, 15 Nov 2022 03:52:55 +0100 (CET) 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 D1B9913D5; Mon, 14 Nov 2022 18:52:59 -0800 (PST) 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 F37B13F73B; Mon, 14 Nov 2022 18:52:50 -0800 (PST) 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: 99feff1e-6490-11ed-91b6-6bf2151ebd3b 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 Subject: [PATCH v1 01/13] xen/arm: re-arrange the static shared memory region Date: Tue, 15 Nov 2022 10:52:23 +0800 Message-Id: <20221115025235.1378931-2-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 This commit re-arranges the static shared memory regions into a separate array shm_meminfo. And static shared memory region now would have its own structure 'shm_membank' to hold all shm-related members, like shm_id, etc, and a pointer to the normal memory bank 'membank'. This will avoid continuing to grow 'membank'. Signed-off-by: Penny Zheng --- xen/arch/arm/bootfdt.c | 40 +++++++++++++++++++------------ xen/arch/arm/domain_build.c | 35 ++++++++++++++++----------- xen/arch/arm/include/asm/kernel.h | 2 +- xen/arch/arm/include/asm/setup.h | 16 +++++++++---- 4 files changed, 59 insertions(+), 34 deletions(-) diff --git a/xen/arch/arm/bootfdt.c b/xen/arch/arm/bootfdt.c index 6014c0f852..ccf281cd37 100644 --- a/xen/arch/arm/bootfdt.c +++ b/xen/arch/arm/bootfdt.c @@ -384,6 +384,7 @@ static int __init process_shm_node(const void *fdt, int node, const __be32 *cell; paddr_t paddr, gaddr, size; struct meminfo *mem = &bootinfo.reserved_mem; + struct shm_meminfo *shm_mem = &bootinfo.shm_mem; unsigned int i; int len; bool owner = false; @@ -455,17 +456,21 @@ static int __init process_shm_node(const void *fdt, int node, return -EINVAL; } - for ( i = 0; i < mem->nr_banks; i++ ) + for ( i = 0; i < shm_mem->nr_banks; i++ ) { + paddr_t bank_start = shm_mem->bank[i].membank->start; + paddr_t bank_size = shm_mem->bank[i].membank->size; + /* * Meet the following check: * 1) The shm ID matches and the region exactly match * 2) The shm ID doesn't match and the region doesn't overlap * with an existing one */ - if ( paddr == mem->bank[i].start && size == mem->bank[i].size ) + if ( paddr == bank_start && size == bank_size ) { - if ( strncmp(shm_id, mem->bank[i].shm_id, MAX_SHM_ID_LENGTH) == 0 ) + if ( strncmp(shm_id, + shm_mem->bank[i].shm_id, MAX_SHM_ID_LENGTH) == 0 ) break; else { @@ -477,17 +482,17 @@ static int __init process_shm_node(const void *fdt, int node, else { paddr_t end = paddr + size; - paddr_t bank_end = mem->bank[i].start + mem->bank[i].size; + paddr_t bank_end = bank_start + bank_size; - if ( (end <= paddr) || (bank_end <= mem->bank[i].start) ) + if ( (end <= paddr) || (bank_end <= bank_start) ) { printk("fdt: static shared memory region %s overflow\n", shm_id); return -EINVAL; } - if ( (end <= mem->bank[i].start) || (paddr >= bank_end) ) + if ( (end <= bank_start) || (paddr >= bank_end) ) { - if ( strcmp(shm_id, mem->bank[i].shm_id) != 0 ) + if ( strcmp(shm_id, shm_mem->bank[i].shm_id) != 0 ) continue; else { @@ -499,22 +504,27 @@ static int __init process_shm_node(const void *fdt, int node, else { printk("fdt: shared memory region overlap with an existing entry %#"PRIpaddr" - %#"PRIpaddr"\n", - mem->bank[i].start, bank_end); + bank_start, bank_end); return -EINVAL; } } } - if ( i == mem->nr_banks ) + if ( i == shm_mem->nr_banks ) { - if ( i < NR_MEM_BANKS ) + if ( (i < NR_MEM_BANKS) && (mem->nr_banks < NR_MEM_BANKS) ) { /* Static shared memory shall be reserved from any other use. */ - safe_strcpy(mem->bank[mem->nr_banks].shm_id, shm_id); - mem->bank[mem->nr_banks].start = paddr; - mem->bank[mem->nr_banks].size = size; - mem->bank[mem->nr_banks].type = MEMBANK_STATIC_DOMAIN; + struct membank *membank = &mem->bank[mem->nr_banks]; + + membank->start = paddr; + membank->size = size; + membank->type = MEMBANK_STATIC_DOMAIN; mem->nr_banks++; + + safe_strcpy(shm_mem->bank[i].shm_id, shm_id); + shm_mem->bank[i].membank = membank; + shm_mem->nr_banks++; } else { @@ -527,7 +537,7 @@ static int __init process_shm_node(const void *fdt, int node, * to calculate the reference count. */ if ( !owner ) - mem->bank[i].nr_shm_borrowers++; + shm_mem->bank[i].nr_shm_borrowers++; return 0; } diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index bd30d3798c..c0fd13f6ed 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -757,20 +757,20 @@ static int __init acquire_nr_borrower_domain(struct domain *d, { unsigned int bank; - /* Iterate reserved memory to find requested shm bank. */ - for ( bank = 0 ; bank < bootinfo.reserved_mem.nr_banks; bank++ ) + /* Iterate static shared memory to find requested shm bank. */ + for ( bank = 0 ; bank < bootinfo.shm_mem.nr_banks; bank++ ) { - paddr_t bank_start = bootinfo.reserved_mem.bank[bank].start; - paddr_t bank_size = bootinfo.reserved_mem.bank[bank].size; + paddr_t bank_start = bootinfo.shm_mem.bank[bank].membank->start; + paddr_t bank_size = bootinfo.shm_mem.bank[bank].membank->size; if ( (pbase == bank_start) && (psize == bank_size) ) break; } - if ( bank == bootinfo.reserved_mem.nr_banks ) + if ( bank == bootinfo.shm_mem.nr_banks ) return -ENOENT; - *nr_borrowers = bootinfo.reserved_mem.bank[bank].nr_shm_borrowers; + *nr_borrowers = bootinfo.shm_mem.bank[bank].nr_shm_borrowers; return 0; } @@ -907,11 +907,18 @@ static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, paddr_t start, paddr_t size, const char *shm_id) { + struct membank *membank; + if ( kinfo->shm_mem.nr_banks >= NR_MEM_BANKS ) return -ENOMEM; - kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].start = start; - kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].size = size; + membank = xmalloc_bytes(sizeof(struct membank)); + if ( membank == NULL ) + return -ENOMEM; + + kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].membank = membank; + kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].membank->start = start; + kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].membank->size = size; safe_strcpy(kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].shm_id, shm_id); kinfo->shm_mem.nr_banks++; @@ -1355,7 +1362,7 @@ static int __init make_memory_node(const struct domain *d, static int __init make_shm_memory_node(const struct domain *d, void *fdt, int addrcells, int sizecells, - const struct meminfo *mem) + const struct shm_meminfo *mem) { unsigned int i = 0; int res = 0; @@ -1372,8 +1379,8 @@ static int __init make_shm_memory_node(const struct domain *d, for ( ; i < mem->nr_banks; i++ ) { - uint64_t start = mem->bank[i].start; - uint64_t size = mem->bank[i].size; + uint64_t start = mem->bank[i].membank->start; + uint64_t size = mem->bank[i].membank->size; /* Placeholder for xen-shmem@ + a 64-bit number + \0 */ char buf[27]; const char compat[] = "xen,shared-memory-v1"; @@ -1382,7 +1389,7 @@ static int __init make_shm_memory_node(const struct domain *d, __be32 *cells; unsigned int len = (addrcells + sizecells) * sizeof(__be32); - snprintf(buf, sizeof(buf), "xen-shmem@%"PRIx64, mem->bank[i].start); + snprintf(buf, sizeof(buf), "xen-shmem@%"PRIx64, start); res = fdt_begin_node(fdt, buf); if ( res ) return res; @@ -1426,7 +1433,7 @@ static int __init make_shm_memory_node(const struct domain *d, static int __init make_shm_memory_node(const struct domain *d, void *fdt, int addrcells, int sizecells, - const struct meminfo *mem) + const struct shm_meminfo *mem) { ASSERT_UNREACHABLE(); return -EOPNOTSUPP; @@ -1436,7 +1443,7 @@ static int __init make_shm_memory_node(const struct domain *d, static int __init make_resv_memory_node(const struct domain *d, void *fdt, int addrcells, int sizecells, - const struct meminfo *mem) + const struct shm_meminfo *mem) { int res = 0; /* Placeholder for reserved-memory\0 */ diff --git a/xen/arch/arm/include/asm/kernel.h b/xen/arch/arm/include/asm/kernel.h index 5bb30c3f2f..f47ba9d619 100644 --- a/xen/arch/arm/include/asm/kernel.h +++ b/xen/arch/arm/include/asm/kernel.h @@ -38,7 +38,7 @@ struct kernel_info { void *fdt; /* flat device tree */ paddr_t unassigned_mem; /* RAM not (yet) assigned to a bank */ struct meminfo mem; - struct meminfo shm_mem; + struct shm_meminfo shm_mem; /* kernel entry point */ paddr_t entry; diff --git a/xen/arch/arm/include/asm/setup.h b/xen/arch/arm/include/asm/setup.h index fdbf68aadc..2d4ae0f00a 100644 --- a/xen/arch/arm/include/asm/setup.h +++ b/xen/arch/arm/include/asm/setup.h @@ -50,10 +50,6 @@ struct membank { paddr_t start; paddr_t size; enum membank_type type; -#ifdef CONFIG_STATIC_SHM - char shm_id[MAX_SHM_ID_LENGTH]; - unsigned int nr_shm_borrowers; -#endif }; struct meminfo { @@ -61,6 +57,17 @@ struct meminfo { struct membank bank[NR_MEM_BANKS]; }; +struct shm_membank { + char shm_id[MAX_SHM_ID_LENGTH]; + unsigned int nr_shm_borrowers; + struct membank *membank; +}; + +struct shm_meminfo { + unsigned int nr_banks; + struct shm_membank bank[NR_MEM_BANKS]; +}; + /* * The domU flag is set for kernels and ramdisks of "xen,domain" nodes. * The purpose of the domU flag is to avoid getting confused in @@ -105,6 +112,7 @@ struct bootinfo { struct meminfo acpi; #endif bool static_heap; + struct shm_meminfo shm_mem; }; struct map_range_data From patchwork Tue Nov 15 02:52:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043129 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 706B2C43219 for ; Tue, 15 Nov 2022 02:53:16 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443636.698317 (Exim 4.92) (envelope-from ) id 1oum4J-0000aP-K4; Tue, 15 Nov 2022 02:52:59 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443636.698317; Tue, 15 Nov 2022 02:52:59 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oum4J-0000aI-Gp; Tue, 15 Nov 2022 02:52:59 +0000 Received: by outflank-mailman (input) for mailman id 443636; Tue, 15 Nov 2022 02:52:58 +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 1oum4I-0008Vs-A3 for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:52:58 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 9bc7c197-6490-11ed-8fd2-01056ac49cbb; Tue, 15 Nov 2022 03:52:57 +0100 (CET) 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 E92E411FB; Mon, 14 Nov 2022 18:53:02 -0800 (PST) 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 1700F3F73B; Mon, 14 Nov 2022 18:52:53 -0800 (PST) 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: 9bc7c197-6490-11ed-8fd2-01056ac49cbb 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 Subject: [PATCH v1 02/13] xen/arm: switch to use shm_membank as function parameter Date: Tue, 15 Nov 2022 10:52:24 +0800 Message-Id: <20221115025235.1378931-3-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 Instead of using multiple function parameters to deliver various shm-related info, like host physical address, SHMID, etc, and with the introduction of new struct "shm_membank", we could switch to use "shm_membank" as function parameter to replace them all, to make codes more clear and tidy. Signed-off-by: Penny Zheng --- xen/arch/arm/domain_build.c | 46 ++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index c0fd13f6ed..d2b9e60b5c 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -751,40 +751,31 @@ static void __init assign_static_memory_11(struct domain *d, } #ifdef CONFIG_STATIC_SHM -static int __init acquire_nr_borrower_domain(struct domain *d, - paddr_t pbase, paddr_t psize, - unsigned long *nr_borrowers) +static struct shm_membank * __init acquire_shm_membank(const char *shm_id) { unsigned int bank; /* Iterate static shared memory to find requested shm bank. */ for ( bank = 0 ; bank < bootinfo.shm_mem.nr_banks; bank++ ) - { - paddr_t bank_start = bootinfo.shm_mem.bank[bank].membank->start; - paddr_t bank_size = bootinfo.shm_mem.bank[bank].membank->size; - - if ( (pbase == bank_start) && (psize == bank_size) ) + if ( strcmp(shm_id, bootinfo.shm_mem.bank[bank].shm_id) == 0 ) break; - } if ( bank == bootinfo.shm_mem.nr_banks ) - return -ENOENT; - - *nr_borrowers = bootinfo.shm_mem.bank[bank].nr_shm_borrowers; + return NULL; - return 0; + return &bootinfo.shm_mem.bank[bank]; } /* * This function checks whether the static shared memory region is * already allocated to dom_io. */ -static bool __init is_shm_allocated_to_domio(paddr_t pbase) +static bool __init is_shm_allocated_to_domio(struct shm_membank *shm_membank) { struct page_info *page; struct domain *d; - page = maddr_to_page(pbase); + page = maddr_to_page(shm_membank->membank->start); d = page_get_owner_and_reference(page); if ( d == NULL ) return false; @@ -835,14 +826,17 @@ static mfn_t __init acquire_shared_memory_bank(struct domain *d, } static int __init assign_shared_memory(struct domain *d, - uint32_t addr_cells, uint32_t size_cells, - paddr_t pbase, paddr_t psize, + struct shm_membank *shm_membank, paddr_t gbase) { mfn_t smfn; int ret = 0; unsigned long nr_pages, nr_borrowers, i; struct page_info *page; + paddr_t pbase, psize; + + pbase = shm_membank->membank->start; + psize = shm_membank->membank->size; printk("%pd: allocate static shared memory BANK %#"PRIpaddr"-%#"PRIpaddr".\n", d, pbase, pbase + psize); @@ -871,9 +865,7 @@ static int __init assign_shared_memory(struct domain *d, * Get the right amount of references per page, which is the number of * borrower domains. */ - ret = acquire_nr_borrower_domain(d, pbase, psize, &nr_borrowers); - if ( ret ) - return ret; + nr_borrowers = shm_membank->nr_shm_borrowers; /* * Instead of letting borrower domain get a page ref, we add as many @@ -941,6 +933,7 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, const char *role_str; const char *shm_id; bool owner_dom_io = true; + struct shm_membank *shm_membank; if ( !dt_device_is_compatible(shm_node, "xen,domain-shared-memory-v1") ) continue; @@ -991,12 +984,20 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, } BUG_ON((strlen(shm_id) <= 0) || (strlen(shm_id) >= MAX_SHM_ID_LENGTH)); + shm_membank = acquire_shm_membank(shm_id); + if ( !shm_membank ) + { + printk("%pd: failed to acquire %s shared memory bank\n", + d, shm_id); + return -ENOENT; + } + /* * DOMID_IO is a fake domain and is not described in the Device-Tree. * Therefore when the owner of the shared region is DOMID_IO, we will * only find the borrowers. */ - if ( (owner_dom_io && !is_shm_allocated_to_domio(pbase)) || + if ( (owner_dom_io && !is_shm_allocated_to_domio(shm_membank)) || (!owner_dom_io && strcmp(role_str, "owner") == 0) ) { /* @@ -1004,8 +1005,7 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, * specified, so they should be assigned to dom_io. */ ret = assign_shared_memory(owner_dom_io ? dom_io : d, - addr_cells, size_cells, - pbase, psize, gbase); + shm_membank, gbase); if ( ret ) return ret; } From patchwork Tue Nov 15 02:52:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043128 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 EBEAFC4167B for ; Tue, 15 Nov 2022 02:53:16 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443637.698327 (Exim 4.92) (envelope-from ) id 1oum4M-0000se-Rs; Tue, 15 Nov 2022 02:53:02 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443637.698327; Tue, 15 Nov 2022 02:53:02 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oum4M-0000sV-Oi; Tue, 15 Nov 2022 02:53:02 +0000 Received: by outflank-mailman (input) for mailman id 443637; Tue, 15 Nov 2022 02:53:01 +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 1oum4L-0000BJ-F9 for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:53:01 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id 9da6ccbd-6490-11ed-91b6-6bf2151ebd3b; Tue, 15 Nov 2022 03:53:00 +0100 (CET) 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 0F65211FB; Mon, 14 Nov 2022 18:53:06 -0800 (PST) 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 2E60E3F73B; Mon, 14 Nov 2022 18:52:56 -0800 (PST) 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: 9da6ccbd-6490-11ed-91b6-6bf2151ebd3b 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 Subject: [PATCH v1 03/13] xen/arm: introduce allocate_domheap_memory and guest_physmap_memory Date: Tue, 15 Nov 2022 10:52:25 +0800 Message-Id: <20221115025235.1378931-4-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 We split the codes of allocate_bank_memory into two parts, allocate_domheap_memory and guest_physmap_memory. One is about allocating guest RAM from heap, which could be re-used later for allocating static shared memory from heap when host address is not provided. The other is building up guest P2M mapping. Signed-off-by: Penny Zheng --- xen/arch/arm/domain_build.c | 93 +++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 25 deletions(-) diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index d2b9e60b5c..92763e96fc 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -390,34 +390,18 @@ static void __init allocate_memory_11(struct domain *d, } } -static bool __init allocate_bank_memory(struct domain *d, - struct kernel_info *kinfo, - gfn_t sgfn, - paddr_t tot_size) +static bool __init allocate_domheap_memory(struct domain *d, + paddr_t tot_size, + struct meminfo *mem) { - int res; struct page_info *pg; - struct membank *bank; unsigned int max_order = ~0; - /* - * allocate_bank_memory can be called with a tot_size of zero for - * the second memory bank. It is not an error and we can safely - * avoid creating a zero-size memory bank. - */ - if ( tot_size == 0 ) - return true; - - bank = &kinfo->mem.bank[kinfo->mem.nr_banks]; - bank->start = gfn_to_gaddr(sgfn); - bank->size = tot_size; - while ( tot_size > 0 ) { unsigned int order = get_allocation_size(tot_size); order = min(max_order, order); - pg = alloc_domheap_pages(d, order, 0); if ( !pg ) { @@ -437,15 +421,74 @@ static bool __init allocate_bank_memory(struct domain *d, continue; } - res = guest_physmap_add_page(d, sgfn, page_to_mfn(pg), order); - if ( res ) - { - dprintk(XENLOG_ERR, "Failed map pages to DOMU: %d", res); + if ( mem->nr_banks == NR_MEM_BANKS ) return false; - } + + mem->bank[mem->nr_banks].start = mfn_to_maddr(page_to_mfn(pg)); + mem->bank[mem->nr_banks].size = 1UL << (PAGE_SHIFT + order); + mem->nr_banks++; + tot_size -= (1UL << (PAGE_SHIFT + order)); + } + + return true; +} + +static int __init guest_physmap_memory(struct domain *d, + const struct meminfo *mem, gfn_t sgfn) +{ + unsigned int i; + int res; + + for ( i = 0; i < mem->nr_banks; i++ ) + { + paddr_t size = mem->bank[i].size; + unsigned int order = get_order_from_bytes(size); + + /* Size must be power of two */ + BUG_ON(!size || (size & (size - 1))); + res = guest_physmap_add_page(d, sgfn, maddr_to_mfn(mem->bank[i].start), + order); + if ( res ) + return res; sgfn = gfn_add(sgfn, 1UL << order); - tot_size -= (1ULL << (PAGE_SHIFT + order)); + } + + return 0; +} + +static bool __init allocate_bank_memory(struct domain *d, + struct kernel_info *kinfo, + gfn_t sgfn, + paddr_t total_size) +{ + struct membank *bank; + struct meminfo host = {0}; + + /* + * allocate_bank_memory can be called with a total_size of zero for + * the second memory bank. It is not an error and we can safely + * avoid creating a zero-size memory bank. + */ + if ( total_size == 0 ) + return true; + + bank = &kinfo->mem.bank[kinfo->mem.nr_banks]; + bank->start = gfn_to_gaddr(sgfn); + bank->size = total_size; + + if ( !allocate_domheap_memory(d, total_size, &host) ) + { + printk(XENLOG_ERR "Failed to allocate (%"PRIpaddr"MB) pages to %pd\n", + total_size >> 20, d); + return false; + } + + if ( guest_physmap_memory(d, &host, sgfn) ) + { + printk(XENLOG_ERR "Failed to map (%"PRIpaddr"MB) pages to %pd\n", + total_size >> 20, d); + return false; } kinfo->mem.nr_banks++; From patchwork Tue Nov 15 02:52:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043126 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 6D7BEC4332F for ; Tue, 15 Nov 2022 02:53:16 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443638.698339 (Exim 4.92) (envelope-from ) id 1oum4Q-0001CE-3q; Tue, 15 Nov 2022 02:53:06 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443638.698339; Tue, 15 Nov 2022 02:53:06 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oum4Q-0001C3-0W; Tue, 15 Nov 2022 02:53:06 +0000 Received: by outflank-mailman (input) for mailman id 443638; Tue, 15 Nov 2022 02:53:04 +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 1oum4O-0008Vs-GT for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:53:04 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 9f775f60-6490-11ed-8fd2-01056ac49cbb; Tue, 15 Nov 2022 03:53:03 +0100 (CET) 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 2473F11FB; Mon, 14 Nov 2022 18:53:09 -0800 (PST) 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 460DE3F73B; Mon, 14 Nov 2022 18:53:00 -0800 (PST) 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: 9f775f60-6490-11ed-8fd2-01056ac49cbb 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 Subject: [PATCH v1 04/13] xen/arm: expand shm_membank for unprovided host address Date: Tue, 15 Nov 2022 10:52:26 +0800 Message-Id: <20221115025235.1378931-5-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 When host address is not provided in "xen,shared-mem", we let Xen automatically allocate requested static shared memory from heap, and it stands good chance of having multiple host memory banks allocated for the requested static shared memory as a result. Therefore current membank is not going to cover it. This commit introduces a new field "mem" to cover both scenarios. "struct membank" is used when host address is provided, whereas "struct meminfo" shall be used when host address not provided. Signed-off-by: Penny Zheng --- xen/arch/arm/bootfdt.c | 6 +++--- xen/arch/arm/domain_build.c | 16 ++++++++-------- xen/arch/arm/include/asm/setup.h | 17 ++++++++++++++++- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/xen/arch/arm/bootfdt.c b/xen/arch/arm/bootfdt.c index ccf281cd37..2f34a8ea83 100644 --- a/xen/arch/arm/bootfdt.c +++ b/xen/arch/arm/bootfdt.c @@ -458,8 +458,8 @@ static int __init process_shm_node(const void *fdt, int node, for ( i = 0; i < shm_mem->nr_banks; i++ ) { - paddr_t bank_start = shm_mem->bank[i].membank->start; - paddr_t bank_size = shm_mem->bank[i].membank->size; + paddr_t bank_start = shm_mem->bank[i].mem.bank->start; + paddr_t bank_size = shm_mem->bank[i].mem.bank->size; /* * Meet the following check: @@ -523,7 +523,7 @@ static int __init process_shm_node(const void *fdt, int node, mem->nr_banks++; safe_strcpy(shm_mem->bank[i].shm_id, shm_id); - shm_mem->bank[i].membank = membank; + shm_mem->bank[i].mem.bank = membank; shm_mem->nr_banks++; } else diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index 92763e96fc..fbb196d8a4 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -818,7 +818,7 @@ static bool __init is_shm_allocated_to_domio(struct shm_membank *shm_membank) struct page_info *page; struct domain *d; - page = maddr_to_page(shm_membank->membank->start); + page = maddr_to_page(shm_membank->mem.bank->start); d = page_get_owner_and_reference(page); if ( d == NULL ) return false; @@ -878,8 +878,8 @@ static int __init assign_shared_memory(struct domain *d, struct page_info *page; paddr_t pbase, psize; - pbase = shm_membank->membank->start; - psize = shm_membank->membank->size; + pbase = shm_membank->mem.bank->start; + psize = shm_membank->mem.bank->size; printk("%pd: allocate static shared memory BANK %#"PRIpaddr"-%#"PRIpaddr".\n", d, pbase, pbase + psize); @@ -951,9 +951,9 @@ static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, if ( membank == NULL ) return -ENOMEM; - kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].membank = membank; - kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].membank->start = start; - kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].membank->size = size; + kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].mem.bank = membank; + kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].mem.bank->start = start; + kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].mem.bank->size = size; safe_strcpy(kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].shm_id, shm_id); kinfo->shm_mem.nr_banks++; @@ -1422,8 +1422,8 @@ static int __init make_shm_memory_node(const struct domain *d, for ( ; i < mem->nr_banks; i++ ) { - uint64_t start = mem->bank[i].membank->start; - uint64_t size = mem->bank[i].membank->size; + uint64_t start = mem->bank[i].mem.bank->start; + uint64_t size = mem->bank[i].mem.bank->size; /* Placeholder for xen-shmem@ + a 64-bit number + \0 */ char buf[27]; const char compat[] = "xen,shared-memory-v1"; diff --git a/xen/arch/arm/include/asm/setup.h b/xen/arch/arm/include/asm/setup.h index 2d4ae0f00a..c54ffc8a5b 100644 --- a/xen/arch/arm/include/asm/setup.h +++ b/xen/arch/arm/include/asm/setup.h @@ -60,7 +60,22 @@ struct meminfo { struct shm_membank { char shm_id[MAX_SHM_ID_LENGTH]; unsigned int nr_shm_borrowers; - struct membank *membank; + struct { + /* + * When host address is provided in "xen,shared-mem", then only one + * consistent host memory bank is behind each shared memory node. + */ + struct membank *bank; + struct { + /* + * When host address is not provided in "xen,shared-mem", then + * we let Xen allocate requested memory from heap, and a shared + * memory bank could be consisted of multiple host memory banks. + */ + struct meminfo *meminfo; + unsigned long total_size; + } banks; + } mem; }; struct shm_meminfo { From patchwork Tue Nov 15 02:52:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043127 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 20091C4321E for ; Tue, 15 Nov 2022 02:53:17 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443639.698350 (Exim 4.92) (envelope-from ) id 1oum4S-0001WB-FT; Tue, 15 Nov 2022 02:53:08 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443639.698350; Tue, 15 Nov 2022 02:53:08 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oum4S-0001Vm-B3; Tue, 15 Nov 2022 02:53:08 +0000 Received: by outflank-mailman (input) for mailman id 443639; Tue, 15 Nov 2022 02:53:07 +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 1oum4R-0008Vs-Mu for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:53:07 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id a15477b6-6490-11ed-8fd2-01056ac49cbb; Tue, 15 Nov 2022 03:53:06 +0100 (CET) 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 3B75611FB; Mon, 14 Nov 2022 18:53:12 -0800 (PST) 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 5DBA03F73B; Mon, 14 Nov 2022 18:53:03 -0800 (PST) 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: a15477b6-6490-11ed-8fd2-01056ac49cbb 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 Subject: [PATCH v1 05/13] xen/arm: allocate shared memory from heap when host address not provided Date: Tue, 15 Nov 2022 10:52:27 +0800 Message-Id: <20221115025235.1378931-6-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 when host address is not provided in "xen,shared-mem", we let Xen allocate requested shared memory from heap, and once the shared memory is allocated, it will be marked as static(PGC_static), which means that it will be reserved as static memory, and will not go back to heap even on freeing. Signed-off-by: Penny Zheng --- xen/arch/arm/domain_build.c | 83 ++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index fbb196d8a4..3de96882a5 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -835,6 +835,72 @@ static bool __init is_shm_allocated_to_domio(struct shm_membank *shm_membank) return true; } +static int __init mark_shared_memory_static(struct shm_membank *shm_membank) +{ + unsigned int bank; + unsigned long i, nr_mfns; + struct page_info *pg; + struct meminfo *meminfo; + + BUG_ON(!shm_membank->mem.banks.meminfo); + meminfo = shm_membank->mem.banks.meminfo; + for ( bank = 0; bank < meminfo->nr_banks; bank++ ) + { + pg = mfn_to_page(maddr_to_mfn(meminfo->bank[bank].start)); + nr_mfns = PFN_DOWN(meminfo->bank[bank].size); + + for ( i = 0; i < nr_mfns; i++ ) + { + /* The page should be already allocated from heap. */ + if ( !pg[i].count_info & PGC_state_inuse ) + { + printk(XENLOG_ERR + "pg[%lu] MFN %"PRI_mfn" c=%#lx\n", + i, mfn_x(page_to_mfn(pg)) + i, pg[i].count_info); + goto fail; + } + + pg[i].count_info |= PGC_static; + } + } + + return 0; + + fail: + while ( bank >= 0 ) + { + while ( --i >= 0 ) + pg[i].count_info &= ~PGC_static; + i = PFN_DOWN(meminfo->bank[--bank].size); + } + + return -EINVAL; +} + +static int __init allocate_shared_memory(struct shm_membank *shm_membank, + paddr_t psize) +{ + struct meminfo *banks; + int ret; + + BUG_ON(shm_membank->mem.banks.meminfo != NULL); + + banks = xmalloc_bytes(sizeof(struct meminfo)); + if ( banks == NULL ) + return -ENOMEM; + shm_membank->mem.banks.meminfo = banks; + memset(shm_membank->mem.banks.meminfo, 0, sizeof(struct meminfo)); + + if ( !allocate_domheap_memory(NULL, psize, shm_membank->mem.banks.meminfo) ) + return -EINVAL; + + ret = mark_shared_memory_static(shm_membank); + if ( ret ) + return ret; + + return 0; +} + static mfn_t __init acquire_shared_memory_bank(struct domain *d, paddr_t pbase, paddr_t psize) { @@ -975,7 +1041,7 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, unsigned int i; const char *role_str; const char *shm_id; - bool owner_dom_io = true; + bool owner_dom_io = true, paddr_assigned = true; struct shm_membank *shm_membank; if ( !dt_device_is_compatible(shm_node, "xen,domain-shared-memory-v1") ) @@ -1035,6 +1101,21 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, return -ENOENT; } + /* + * When host address is not provided in "xen,shared-mem", + * we let Xen allocate requested memory from heap at first domain. + */ + if ( !paddr_assigned && !shm_membank->mem.banks.meminfo ) + { + ret = allocate_shared_memory(shm_membank, psize); + if ( ret ) + { + printk("%pd: failed to allocate shared memory bank(%"PRIpaddr"MB) from heap: %d\n", + d, psize >> 20, ret); + return ret; + } + } + /* * DOMID_IO is a fake domain and is not described in the Device-Tree. * Therefore when the owner of the shared region is DOMID_IO, we will From patchwork Tue Nov 15 02:52:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043131 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 D35E5C4332F for ; Tue, 15 Nov 2022 02:53:19 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443640.698361 (Exim 4.92) (envelope-from ) id 1oum4V-0001tq-OQ; Tue, 15 Nov 2022 02:53:11 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443640.698361; Tue, 15 Nov 2022 02:53:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oum4V-0001te-Kp; Tue, 15 Nov 2022 02:53:11 +0000 Received: by outflank-mailman (input) for mailman id 443640; Tue, 15 Nov 2022 02:53:10 +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 1oum4U-0000BJ-PQ for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:53:10 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id a323f596-6490-11ed-91b6-6bf2151ebd3b; Tue, 15 Nov 2022 03:53:09 +0100 (CET) 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 5309D11FB; Mon, 14 Nov 2022 18:53:15 -0800 (PST) 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 74C063F73B; Mon, 14 Nov 2022 18:53:06 -0800 (PST) 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: a323f596-6490-11ed-91b6-6bf2151ebd3b 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 Subject: [PATCH v1 06/13] xen/arm: assign shared memory to owner when host address not provided Date: Tue, 15 Nov 2022 10:52:28 +0800 Message-Id: <20221115025235.1378931-7-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 With the introduction of new scenario where host address is not provided in "xen,shared-mem", the function "assign_shared_memory" shall be adapted to it too. Shared memory will already be allocated from heap, when calling "assign_shared_memory" with unprovided host address. So in "assign_shared_memory", we just need to assign these static shared pages to its owner domain using function "assign_pages", and add as many additional reference as the number of borrowers. Signed-off-by: Penny Zheng --- xen/arch/arm/domain_build.c | 160 ++++++++++++++++++++++++++++++------ 1 file changed, 133 insertions(+), 27 deletions(-) diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index 3de96882a5..faf0784bb0 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -817,8 +817,12 @@ static bool __init is_shm_allocated_to_domio(struct shm_membank *shm_membank) { struct page_info *page; struct domain *d; + paddr_t pbase; - page = maddr_to_page(shm_membank->mem.bank->start); + pbase = shm_membank->mem.banks.meminfo ? + shm_membank->mem.banks.meminfo->bank[0].start : + shm_membank->mem.bank->start; + page = maddr_to_page(pbase); d = page_get_owner_and_reference(page); if ( d == NULL ) return false; @@ -907,6 +911,7 @@ static mfn_t __init acquire_shared_memory_bank(struct domain *d, mfn_t smfn; unsigned long nr_pfns; int res; + struct page_info *page; /* * Pages of statically shared memory shall be included @@ -922,33 +927,82 @@ static mfn_t __init acquire_shared_memory_bank(struct domain *d, d->max_pages += nr_pfns; smfn = maddr_to_mfn(pbase); - res = acquire_domstatic_pages(d, smfn, nr_pfns, 0); - if ( res ) + page = mfn_to_page(smfn); + /* + * If page is allocated from heap as static shared memory, then we just + * assign it to the owner domain + */ + if ( page->count_info == (PGC_state_inuse | PGC_static) ) { - printk(XENLOG_ERR - "%pd: failed to acquire static memory: %d.\n", d, res); - d->max_pages -= nr_pfns; - return INVALID_MFN; + res = assign_pages(page, nr_pfns, d, 0); + if ( res ) + { + printk(XENLOG_ERR + "%pd: failed to assign static memory: %d.\n", d, res); + return INVALID_MFN; + } + } + else + { + res = acquire_domstatic_pages(d, smfn, nr_pfns, 0); + if ( res ) + { + printk(XENLOG_ERR + "%pd: failed to acquire static memory: %d.\n", d, res); + d->max_pages -= nr_pfns; + return INVALID_MFN; + } } return smfn; } -static int __init assign_shared_memory(struct domain *d, - struct shm_membank *shm_membank, - paddr_t gbase) +static void __init remove_shared_memory_ref(struct page_info *page, + unsigned long nr_pages, + unsigned long nr_borrowers) { - mfn_t smfn; - int ret = 0; - unsigned long nr_pages, nr_borrowers, i; - struct page_info *page; - paddr_t pbase, psize; + while ( --nr_pages >= 0 ) + put_page_nr(page + nr_pages, nr_borrowers); +} - pbase = shm_membank->mem.bank->start; - psize = shm_membank->mem.bank->size; +static int __init add_shared_memory_ref(struct domain *d, struct page_info *page, + unsigned long nr_pages, + unsigned long nr_borrowers) +{ + unsigned int i; - printk("%pd: allocate static shared memory BANK %#"PRIpaddr"-%#"PRIpaddr".\n", - d, pbase, pbase + psize); + /* + * Instead of letting borrower domain get a page ref, we add as many + * additional reference as the number of borrowers when the owner + * is allocated, since there is a chance that owner is created + * after borrower. + * So if the borrower is created first, it will cause adding pages + * in the P2M without reference. + */ + for ( i = 0; i < nr_pages; i++ ) + { + if ( !get_page_nr(page + i, d, nr_borrowers) ) + { + printk(XENLOG_ERR + "Failed to add %lu references to page %"PRI_mfn".\n", + nr_borrowers, mfn_x(page_to_mfn(page)) + i); + goto fail; + } + } + return 0; + + fail: + remove_shared_memory_ref(page, i, nr_borrowers); + return -EINVAL; +} + +static int __init acquire_shared_memory(struct domain *d, + paddr_t pbase, paddr_t psize, + paddr_t gbase) +{ + mfn_t smfn; + int ret = 0; + unsigned long nr_pages; smfn = acquire_shared_memory_bank(d, pbase, psize); if ( mfn_eq(smfn, INVALID_MFN) ) @@ -970,6 +1024,44 @@ static int __init assign_shared_memory(struct domain *d, } } + return 0; +} + +static int __init assign_shared_memory(struct domain *d, + struct shm_membank *shm_membank, + paddr_t gbase) +{ + int ret = 0; + unsigned long nr_pages, nr_borrowers; + struct page_info *page; + unsigned int i; + struct meminfo *meminfo; + + /* Host address is not provided in "xen,shared-mem" */ + if ( shm_membank->mem.banks.meminfo ) + { + meminfo = shm_membank->mem.banks.meminfo; + for ( i = 0; i < meminfo->nr_banks; i++ ) + { + ret = acquire_shared_memory(d, + meminfo->bank[i].start, + meminfo->bank[i].size, + gbase); + if ( ret ) + return ret; + + gbase += meminfo->bank[i].size; + } + } + else + { + ret = acquire_shared_memory(d, + shm_membank->mem.bank->start, + shm_membank->mem.bank->size, gbase); + if ( ret ) + return ret; + } + /* * Get the right amount of references per page, which is the number of * borrower domains. @@ -984,23 +1076,37 @@ static int __init assign_shared_memory(struct domain *d, * So if the borrower is created first, it will cause adding pages * in the P2M without reference. */ - page = mfn_to_page(smfn); - for ( i = 0; i < nr_pages; i++ ) + if ( shm_membank->mem.banks.meminfo ) { - if ( !get_page_nr(page + i, d, nr_borrowers) ) + meminfo = shm_membank->mem.banks.meminfo; + for ( i = 0; i < meminfo->nr_banks; i++ ) { - printk(XENLOG_ERR - "Failed to add %lu references to page %"PRI_mfn".\n", - nr_borrowers, mfn_x(smfn) + i); - goto fail; + page = mfn_to_page(maddr_to_mfn(meminfo->bank[i].start)); + nr_pages = PFN_DOWN(meminfo->bank[i].size); + ret = add_shared_memory_ref(d, page, nr_pages, nr_borrowers); + if ( ret ) + goto fail; } } + else + { + page = mfn_to_page( + maddr_to_mfn(shm_membank->mem.bank->start)); + nr_pages = shm_membank->mem.bank->size >> PAGE_SHIFT; + ret = add_shared_memory_ref(d, page, nr_pages, nr_borrowers); + if ( ret ) + return ret; + } return 0; fail: while ( --i >= 0 ) - put_page_nr(page + i, nr_borrowers); + { + page = mfn_to_page(maddr_to_mfn(meminfo->bank[i].start)); + nr_pages = PFN_DOWN(meminfo->bank[i].size); + remove_shared_memory_ref(page, nr_pages, nr_borrowers); + } return ret; } From patchwork Tue Nov 15 02:52:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043132 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 D30E9C433FE for ; Tue, 15 Nov 2022 02:53:22 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443642.698372 (Exim 4.92) (envelope-from ) id 1oum4Z-0002Q4-9I; Tue, 15 Nov 2022 02:53:15 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443642.698372; Tue, 15 Nov 2022 02:53:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oum4Z-0002Pl-4T; Tue, 15 Nov 2022 02:53:15 +0000 Received: by outflank-mailman (input) for mailman id 443642; Tue, 15 Nov 2022 02:53:13 +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 1oum4X-0008Vs-QU for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:53:13 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id a509e63a-6490-11ed-8fd2-01056ac49cbb; Tue, 15 Nov 2022 03:53:12 +0100 (CET) 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 69FF711FB; Mon, 14 Nov 2022 18:53:18 -0800 (PST) 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 8C2373F73B; Mon, 14 Nov 2022 18:53:09 -0800 (PST) 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: a509e63a-6490-11ed-8fd2-01056ac49cbb 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 Subject: [PATCH v1 07/13] xen/arm: map shared memory to borrower when host address not provided Date: Tue, 15 Nov 2022 10:52:29 +0800 Message-Id: <20221115025235.1378931-8-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 With the introduction of new scenario where host address is not provided in "xen,shared-mem", the function "map_regions_p2mt" for setting up P2M foreign mapping for borrower domain shall be adapted to it too. Here we implement a new helper "borrower_physmap_add_memory" to cover both scenarios. Signed-off-by: Penny Zheng --- xen/arch/arm/domain_build.c | 54 ++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index faf0784bb0..d0f7fc8fd7 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -1110,6 +1110,51 @@ static int __init assign_shared_memory(struct domain *d, return ret; } +static int __init borrower_physmap_add_memory(struct domain *d, + struct shm_membank *shm_membank, + paddr_t gbase) +{ + int ret; + unsigned int i; + paddr_t start, size; + gfn_t sgfn; + struct meminfo *meminfo; + + /* Host address is not provided in "xen,shared-mem" */ + if ( shm_membank->mem.banks.meminfo ) + { + meminfo = shm_membank->mem.banks.meminfo; + for ( i = 0; i < meminfo->nr_banks; i++ ) + { + start = meminfo->bank[i].start; + size = meminfo->bank[i].size; + sgfn = _gfn(PFN_UP(gbase)); + + /* Set up P2M foreign mapping for borrower domain. */ + ret = map_regions_p2mt(d, sgfn, PFN_DOWN(size), _mfn(PFN_UP(start)), + p2m_map_foreign_rw); + if ( ret ) + return ret; + + sgfn = gfn_add(sgfn, PFN_DOWN(size)); + } + } + else + { + start = shm_membank->mem.bank->start; + size = shm_membank->mem.bank->size; + sgfn = _gfn(PFN_UP(gbase)); + + /* Set up P2M foreign mapping for borrower domain. */ + ret = map_regions_p2mt(d, sgfn, PFN_DOWN(size), _mfn(PFN_UP(start)), + p2m_map_foreign_rw); + if ( ret ) + return ret; + } + + return 0; +} + static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, paddr_t start, paddr_t size, const char *shm_id) @@ -1242,11 +1287,14 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, if ( owner_dom_io || (strcmp(role_str, "borrower") == 0) ) { - /* Set up P2M foreign mapping for borrower domain. */ - ret = map_regions_p2mt(d, _gfn(PFN_UP(gbase)), PFN_DOWN(psize), - _mfn(PFN_UP(pbase)), p2m_map_foreign_rw); + ret = borrower_physmap_add_memory(d, shm_membank, gbase); if ( ret ) + { + printk(XENLOG_ERR + "%pd: Failed to map foreign memory to borrower domain: %d", + d, ret); return ret; + } } /* From patchwork Tue Nov 15 02:52:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043133 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 A2E85C4332F for ; Tue, 15 Nov 2022 02:53:27 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443646.698383 (Exim 4.92) (envelope-from ) id 1oum4c-000336-Mq; Tue, 15 Nov 2022 02:53:18 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443646.698383; Tue, 15 Nov 2022 02:53:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oum4c-000324-IE; Tue, 15 Nov 2022 02:53:18 +0000 Received: by outflank-mailman (input) for mailman id 443646; Tue, 15 Nov 2022 02:53: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 1oum4b-0008Vs-5K for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:53:17 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id a6e108b4-6490-11ed-8fd2-01056ac49cbb; Tue, 15 Nov 2022 03:53:15 +0100 (CET) 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 81CFD13D5; Mon, 14 Nov 2022 18:53:21 -0800 (PST) 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 A383D3F73B; Mon, 14 Nov 2022 18:53:12 -0800 (PST) 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: a6e108b4-6490-11ed-8fd2-01056ac49cbb 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 Subject: [PATCH v1 08/13] xen/arm: use paddr_assigned to indicate whether host address is provided Date: Tue, 15 Nov 2022 10:52:30 +0800 Message-Id: <20221115025235.1378931-9-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 We use paddr_assigned to indicate whether host address is provided, by checking the length of "xen,shared-mem" property. And the shm matching criteria shall also be adapt to the new scenario, by adding when host address is not provided, if SHMID matches, the region size must exactly match too. Also, to make codes tidy and clear, we extract codes about parsing "xen,shared-mem" property from function "process_shm" and move them into a new helper "parse_shm_property". Signed-off-by: Penny Zheng --- xen/arch/arm/bootfdt.c | 87 +++++++++++++++++++-------- xen/arch/arm/domain_build.c | 114 +++++++++++++++++++++++++----------- 2 files changed, 141 insertions(+), 60 deletions(-) diff --git a/xen/arch/arm/bootfdt.c b/xen/arch/arm/bootfdt.c index 2f34a8ea83..efaf49fd56 100644 --- a/xen/arch/arm/bootfdt.c +++ b/xen/arch/arm/bootfdt.c @@ -387,7 +387,7 @@ static int __init process_shm_node(const void *fdt, int node, struct shm_meminfo *shm_mem = &bootinfo.shm_mem; unsigned int i; int len; - bool owner = false; + bool owner = false, paddr_assigned = true; const char *shm_id; if ( address_cells < 1 || size_cells < 1 ) @@ -428,7 +428,7 @@ static int __init process_shm_node(const void *fdt, int node, } /* - * xen,shared-mem = ; + * xen,shared-mem = , and paddr could be optional * Memory region starting from physical address #paddr of #size shall * be mapped to guest physical address #gaddr as static shared memory * region. @@ -439,16 +439,24 @@ static int __init process_shm_node(const void *fdt, int node, if ( len != dt_cells_to_size(address_cells + size_cells + address_cells) ) { + /* paddr is not provided in "xen,shared-mem" */ if ( len == dt_cells_to_size(size_cells + address_cells) ) - printk("fdt: host physical address must be chosen by users at the moment.\n"); - - printk("fdt: invalid `xen,shared-mem` property.\n"); - return -EINVAL; + paddr_assigned = false; + else + { + printk("fdt: invalid `xen,shared-mem` property.\n"); + return -EINVAL; + } } cell = (const __be32 *)prop->data; - device_tree_get_reg(&cell, address_cells, address_cells, &paddr, &gaddr); - size = dt_next_cell(size_cells, &cell); + if ( !paddr_assigned ) + device_tree_get_reg(&cell, address_cells, size_cells, &gaddr, &size); + else + { + device_tree_get_reg(&cell, address_cells, address_cells, &paddr, &gaddr); + size = dt_next_cell(size_cells, &cell); + } if ( !size ) { @@ -458,29 +466,45 @@ static int __init process_shm_node(const void *fdt, int node, for ( i = 0; i < shm_mem->nr_banks; i++ ) { - paddr_t bank_start = shm_mem->bank[i].mem.bank->start; - paddr_t bank_size = shm_mem->bank[i].mem.bank->size; - /* * Meet the following check: + * when host address is provided: * 1) The shm ID matches and the region exactly match * 2) The shm ID doesn't match and the region doesn't overlap * with an existing one + * when host address is not provided: + * 1) The shm ID matches and the region size exactly match */ - if ( paddr == bank_start && size == bank_size ) + if ( !paddr_assigned || (paddr == shm_mem->bank[i].mem.bank->start && + size == shm_mem->bank[i].mem.bank->size) ) { if ( strncmp(shm_id, shm_mem->bank[i].shm_id, MAX_SHM_ID_LENGTH) == 0 ) + { + if ( !paddr_assigned && + (size != shm_mem->bank[i].mem.banks.total_size) ) + { + printk("fdt: when host address is not provided, if xen,shm-id %s matches, size must stay the same too.\n", + shm_id); + return -EINVAL; + } + break; - else + } + else if ( paddr_assigned ) { printk("fdt: xen,shm-id %s does not match for all the nodes using the same region.\n", shm_id); return -EINVAL; } + + /* host address is not provided, and with different SHMID. */ + continue; } else { + paddr_t bank_start = shm_mem->bank[i].mem.bank->start; + paddr_t bank_size = shm_mem->bank[i].mem.bank->size; paddr_t end = paddr + size; paddr_t bank_end = bank_start + bank_size; @@ -512,26 +536,35 @@ static int __init process_shm_node(const void *fdt, int node, if ( i == shm_mem->nr_banks ) { - if ( (i < NR_MEM_BANKS) && (mem->nr_banks < NR_MEM_BANKS) ) + if ( i < NR_MEM_BANKS ) { - /* Static shared memory shall be reserved from any other use. */ - struct membank *membank = &mem->bank[mem->nr_banks]; + if ( !paddr_assigned ) + { + shm_mem->bank[i].mem.banks.total_size = size; + goto out; + } + else if ( mem->nr_banks < NR_MEM_BANKS ) + { + /* Static shared memory shall be reserved from any other use. */ + struct membank *membank = &mem->bank[mem->nr_banks]; - membank->start = paddr; - membank->size = size; - membank->type = MEMBANK_STATIC_DOMAIN; - mem->nr_banks++; + membank->start = paddr; + membank->size = size; + membank->type = MEMBANK_STATIC_DOMAIN; + mem->nr_banks++; + shm_mem->bank[i].mem.bank = membank; + } + else + goto fail; + out: safe_strcpy(shm_mem->bank[i].shm_id, shm_id); - shm_mem->bank[i].mem.bank = membank; shm_mem->nr_banks++; } else - { - printk("Warning: Max number of supported memory regions reached.\n"); - return -ENOSPC; - } + goto fail; } + /* * keep a count of the number of borrowers, which later may be used * to calculate the reference count. @@ -540,6 +573,10 @@ static int __init process_shm_node(const void *fdt, int node, shm_mem->bank[i].nr_shm_borrowers++; return 0; + + fail: + printk("Warning: Max number of supported memory regions reached.\n"); + return -ENOSPC; } #else static int __init process_shm_node(const void *fdt, int node, diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index d0f7fc8fd7..80d37245a8 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -1177,6 +1177,81 @@ static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, return 0; } +static int __init parse_shm_property(struct domain *d, + const struct dt_device_node *shm_node, + bool *paddr_assigned, paddr_t *pbase, + paddr_t *gbase, paddr_t *psize) +{ + const struct dt_property *prop; + const __be32 *cells; + uint32_t len; + unsigned int i; + uint32_t addr_cells, size_cells; + + addr_cells = dt_n_addr_cells(shm_node); + size_cells = dt_n_size_cells(shm_node); + + /* xen,shared-mem = ; And pbase could be optional. */ + prop = dt_find_property(shm_node, "xen,shared-mem", &len); + BUG_ON(!prop); + cells = (const __be32 *)prop->value; + + if ( len != dt_cells_to_size(addr_cells + size_cells + addr_cells) ) + { + /* pbase is not provided in "xen,shared-mem" */ + if ( len == dt_cells_to_size(size_cells + addr_cells) ) + *paddr_assigned = false; + else + { + printk("fdt: invalid `xen,shared-mem` property.\n"); + return -EINVAL; + } + } + + if ( !*paddr_assigned ) + { + device_tree_get_reg(&cells, addr_cells, size_cells, gbase, psize); + goto out; + } + else + { + device_tree_get_reg(&cells, addr_cells, addr_cells, pbase, gbase); + *psize = dt_read_number(cells, size_cells); + } + + if ( !IS_ALIGNED(*pbase, PAGE_SIZE) ) + { + printk("%pd: physical address 0x%"PRIpaddr" is not suitably aligned.\n", + d, *pbase); + return -EINVAL; + } + + for ( i = 0; i < PFN_DOWN(*psize); i++ ) + if ( !mfn_valid(mfn_add(maddr_to_mfn(*pbase), i)) ) + { + printk("%pd: invalid physical address 0x%"PRI_mfn"\n", + d, mfn_x(mfn_add(maddr_to_mfn(*pbase), i))); + return -EINVAL; + } + + out: + if ( !IS_ALIGNED(*psize, PAGE_SIZE) ) + { + printk("%pd: size 0x%"PRIpaddr" is not suitably aligned\n", + d, *psize); + return -EINVAL; + } + + if ( !IS_ALIGNED(*gbase, PAGE_SIZE) ) + { + printk("%pd: guest address 0x%"PRIpaddr" is not suitably aligned.\n", + d, *gbase); + return -EINVAL; + } + + return 0; +} + static int __init process_shm(struct domain *d, struct kernel_info *kinfo, const struct dt_device_node *node) { @@ -1184,12 +1259,8 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, dt_for_each_child_node(node, shm_node) { - const struct dt_property *prop; - const __be32 *cells; - uint32_t addr_cells, size_cells; paddr_t gbase, pbase, psize; int ret = 0; - unsigned int i; const char *role_str; const char *shm_id; bool owner_dom_io = true, paddr_assigned = true; @@ -1198,37 +1269,10 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, if ( !dt_device_is_compatible(shm_node, "xen,domain-shared-memory-v1") ) continue; - /* - * xen,shared-mem = ; - * TODO: pbase is optional. - */ - addr_cells = dt_n_addr_cells(shm_node); - size_cells = dt_n_size_cells(shm_node); - prop = dt_find_property(shm_node, "xen,shared-mem", NULL); - BUG_ON(!prop); - cells = (const __be32 *)prop->value; - device_tree_get_reg(&cells, addr_cells, addr_cells, &pbase, &gbase); - psize = dt_read_number(cells, size_cells); - if ( !IS_ALIGNED(pbase, PAGE_SIZE) || !IS_ALIGNED(gbase, PAGE_SIZE) ) - { - printk("%pd: physical address 0x%"PRIpaddr", or guest address 0x%"PRIpaddr" is not suitably aligned.\n", - d, pbase, gbase); - return -EINVAL; - } - if ( !IS_ALIGNED(psize, PAGE_SIZE) ) - { - printk("%pd: size 0x%"PRIpaddr" is not suitably aligned\n", - d, psize); - return -EINVAL; - } - - for ( i = 0; i < PFN_DOWN(psize); i++ ) - if ( !mfn_valid(mfn_add(maddr_to_mfn(pbase), i)) ) - { - printk("%pd: invalid physical address 0x%"PRI_mfn"\n", - d, mfn_x(mfn_add(maddr_to_mfn(pbase), i))); - return -EINVAL; - } + ret = parse_shm_property(d, shm_node, &paddr_assigned, + &pbase, &gbase, &psize); + if ( ret ) + return ret; /* * "role" property is optional and if it is defined explicitly, From patchwork Tue Nov 15 02:52:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043134 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 D833AC4332F for ; Tue, 15 Nov 2022 02:53:30 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443651.698394 (Exim 4.92) (envelope-from ) id 1oum4f-0003Uk-V2; Tue, 15 Nov 2022 02:53:21 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443651.698394; Tue, 15 Nov 2022 02:53:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oum4f-0003UZ-Rm; Tue, 15 Nov 2022 02:53:21 +0000 Received: by outflank-mailman (input) for mailman id 443651; Tue, 15 Nov 2022 02:53:20 +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 1oum4e-0000BJ-Lj for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:53:20 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id a8baf10a-6490-11ed-91b6-6bf2151ebd3b; Tue, 15 Nov 2022 03:53:19 +0100 (CET) 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 98F4013D5; Mon, 14 Nov 2022 18:53:24 -0800 (PST) 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 BB42C3F73B; Mon, 14 Nov 2022 18:53:15 -0800 (PST) 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: a8baf10a-6490-11ed-91b6-6bf2151ebd3b 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 Subject: [PATCH v1 09/13] xen/arm: refine docs about static shared memory Date: Tue, 15 Nov 2022 10:52:31 +0800 Message-Id: <20221115025235.1378931-10-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 This commit amends docs(booting.txt) to include the new scenario where host address is not provided in "xen,shared-mem" property, and we also add a new example to explain in details. We also fix some buggy info in the docs, like SHMID is "my-shared-mem-1", not "0x1". Signed-off-by: Penny Zheng --- docs/misc/arm/device-tree/booting.txt | 52 ++++++++++++++++++++------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/docs/misc/arm/device-tree/booting.txt b/docs/misc/arm/device-tree/booting.txt index 87eaa3e254..287898ef03 100644 --- a/docs/misc/arm/device-tree/booting.txt +++ b/docs/misc/arm/device-tree/booting.txt @@ -553,7 +553,7 @@ communication. An array takes a physical address, which is the base address of the shared memory region in host physical address space, a size, and a guest physical address, as the target address of the mapping. - e.g. xen,shared-mem = < [host physical address] [guest address] [size] > + e.g. xen,shared-mem = < [host physical address] [guest address] [size] >; It shall also meet the following criteria: 1) If the SHM ID matches with an existing region, the address range of the @@ -564,8 +564,8 @@ communication. The number of cells for the host address (and size) is the same as the guest pseudo-physical address and they are inherited from the parent node. - Host physical address is optional, when missing Xen decides the location - (currently unimplemented). + Host physical address is optional, when missing Xen decides the location. + e.g. xen,shared-mem = < [guest address] [size] > - role (Optional) @@ -592,7 +592,7 @@ chosen { role = "owner"; xen,shm-id = "my-shared-mem-0"; xen,shared-mem = <0x10000000 0x10000000 0x10000000>; - } + }; domU1 { compatible = "xen,domain"; @@ -603,25 +603,36 @@ chosen { vpl011; /* - * shared memory region identified as 0x0(xen,shm-id = <0x0>) - * is shared between Dom0 and DomU1. + * shared memory region "my-shared-mem-0" is shared + * between Dom0 and DomU1. */ domU1-shared-mem@10000000 { compatible = "xen,domain-shared-memory-v1"; role = "borrower"; xen,shm-id = "my-shared-mem-0"; xen,shared-mem = <0x10000000 0x50000000 0x10000000>; - } + }; /* - * shared memory region identified as 0x1(xen,shm-id = <0x1>) - * is shared between DomU1 and DomU2. + * shared memory region "my-shared-mem-1" is shared between + * DomU1 and DomU2. */ domU1-shared-mem@50000000 { compatible = "xen,domain-shared-memory-v1"; xen,shm-id = "my-shared-mem-1"; xen,shared-mem = <0x50000000 0x60000000 0x20000000>; - } + }; + + /* + * shared memory region "my-shared-mem-2" is shared between + * DomU1 and DomU2. + */ + domU1-shared-mem-2 { + compatible = "xen,domain-shared-memory-v1"; + xen,shm-id = "my-shared-mem-2"; + role = "owner"; + xen,shared-mem = <0x80000000 0x20000000>; + }; ...... @@ -635,14 +646,21 @@ chosen { cpus = <1>; /* - * shared memory region identified as 0x1(xen,shm-id = <0x1>) - * is shared between domU1 and domU2. + * shared memory region "my-shared-mem-1" is shared between + * domU1 and domU2. */ domU2-shared-mem@50000000 { compatible = "xen,domain-shared-memory-v1"; xen,shm-id = "my-shared-mem-1"; xen,shared-mem = <0x50000000 0x70000000 0x20000000>; - } + }; + + domU2-shared-mem-2 { + compatible = "xen,domain-shared-memory-v1"; + xen,shm-id = "my-shared-mem-2"; + role = "borrower"; + xen,shared-mem = <0x90000000 0x20000000>; + }; ...... }; @@ -662,3 +680,11 @@ shared between DomU1 and DomU2. It will get mapped at 0x60000000 in DomU1 guest physical address space, and at 0x70000000 in DomU2 guest physical address space. DomU1 and DomU2 are both the borrower domain, the owner domain is the default owner domain DOMID_IO. + +For the static shared memory region "my-shared-mem-2", since host physical +address is not provided by user, Xen will automatically allocate 512MB +from heap as static shared memory to be shared between DomU1 and DomU2. +The automatically allocated static shared memory will get mapped at +0x80000000 in DomU1 guest physical address space, and at 0x90000000 in DomU2 +guest physical address space. DomU1 is explicitly defined as the owner domain, +and DomU2 is the borrower domain. From patchwork Tue Nov 15 02:52:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043135 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 CAC18C433FE for ; Tue, 15 Nov 2022 02:53:32 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443653.698405 (Exim 4.92) (envelope-from ) id 1oum4j-00045g-9d; Tue, 15 Nov 2022 02:53:25 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443653.698405; Tue, 15 Nov 2022 02:53:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oum4j-00045W-61; Tue, 15 Nov 2022 02:53:25 +0000 Received: by outflank-mailman (input) for mailman id 443653; Tue, 15 Nov 2022 02:53: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 1oum4i-0008Vs-1f for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:53:24 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id aa98c8b3-6490-11ed-8fd2-01056ac49cbb; Tue, 15 Nov 2022 03:53:22 +0100 (CET) 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 B0A2111FB; Mon, 14 Nov 2022 18:53:27 -0800 (PST) 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 D27413F73B; Mon, 14 Nov 2022 18:53:18 -0800 (PST) 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: aa98c8b3-6490-11ed-8fd2-01056ac49cbb 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 Subject: [PATCH v1 10/13] xen/arm: introduce "xen,offset" feature Date: Tue, 15 Nov 2022 10:52:32 +0800 Message-Id: <20221115025235.1378931-11-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 We introduce "xen,offset" to handle the case where memory from the owner is shared with multiple borrowers. Each borrower would have its own offset within the region shared by the owner. Add relative check when parsing static shared memory node, to make sure that "xen,offset" could be only provided in borrower domain, and the value must be smaller than size. And include according docs in booting.txt. Signed-off-by: Penny Zheng --- docs/misc/arm/device-tree/booting.txt | 19 ++++++++++++++-- xen/arch/arm/bootfdt.c | 32 ++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/docs/misc/arm/device-tree/booting.txt b/docs/misc/arm/device-tree/booting.txt index 287898ef03..183c41e3c2 100644 --- a/docs/misc/arm/device-tree/booting.txt +++ b/docs/misc/arm/device-tree/booting.txt @@ -577,6 +577,16 @@ communication. If not specified, the default value is "borrower" and owner is DOMID_IO, a system domain. +- xen,offset (Optional) + + A 64-bit integer specifying the offset within a shared memory region. + When "xen,offset" is provided, only partial shared memory will be + mapped to the borrower domain, starting at the offset and the size + being "size - offset". + + "xen,offset" could be only provided in borrower domain, and the value + must be smaller than size. + As an example: chosen { @@ -660,6 +670,7 @@ chosen { xen,shm-id = "my-shared-mem-2"; role = "borrower"; xen,shared-mem = <0x90000000 0x20000000>; + xen,offset = <0x0 0x10000000>; }; ...... @@ -686,5 +697,9 @@ address is not provided by user, Xen will automatically allocate 512MB from heap as static shared memory to be shared between DomU1 and DomU2. The automatically allocated static shared memory will get mapped at 0x80000000 in DomU1 guest physical address space, and at 0x90000000 in DomU2 -guest physical address space. DomU1 is explicitly defined as the owner domain, -and DomU2 is the borrower domain. +guest physical address space. +For borrower domain DomU2, only partial static shared memory region +"my-shared-mem-2" gets mapped, starting at offset 0x10000000 with size of +256MB. +DomU1 is explicitly defined as the owner domain, and DomU2 is the borrower +domain. diff --git a/xen/arch/arm/bootfdt.c b/xen/arch/arm/bootfdt.c index efaf49fd56..e642e72f30 100644 --- a/xen/arch/arm/bootfdt.c +++ b/xen/arch/arm/bootfdt.c @@ -380,7 +380,7 @@ static int __init process_domain_node(const void *fdt, int node, static int __init process_shm_node(const void *fdt, int node, uint32_t address_cells, uint32_t size_cells) { - const struct fdt_property *prop, *prop_id, *prop_role; + const struct fdt_property *prop, *prop_id, *prop_role, *prop_offset; const __be32 *cell; paddr_t paddr, gaddr, size; struct meminfo *mem = &bootinfo.reserved_mem; @@ -389,6 +389,7 @@ static int __init process_shm_node(const void *fdt, int node, int len; bool owner = false, paddr_assigned = true; const char *shm_id; + uint64_t offset; if ( address_cells < 1 || size_cells < 1 ) { @@ -464,6 +465,35 @@ static int __init process_shm_node(const void *fdt, int node, return -EINVAL; } + /* + * If "xen,offset" is provided, then only partial shared memory + * shall be mapped to borrower domain. The size will be + * "psize - offset". + * "xen,offset" is a 64-bit integer and an optional property + */ + prop_offset = fdt_get_property(fdt, node, "xen,offset", NULL); + if ( prop_offset ) + { + /* + * "xen,offset" could be only provided in borrower domain, + * and the offset must be smaller than size. + */ + if ( prop_role && !strcmp(prop_role->data, "owner") ) + { + printk("fdt: \"xen,offset\" could not be provided in owner domain\n"); + return -EINVAL; + } + + cell = (const __be32 *)prop_offset->data; + offset = dt_next_cell(2, &cell); + + if ( offset >= size ) + { + printk("fdt: invalid \"xen,offset\" value\n"); + return -EINVAL; + } + } + for ( i = 0; i < shm_mem->nr_banks; i++ ) { /* From patchwork Tue Nov 15 02:52:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043153 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 B841DC43217 for ; Tue, 15 Nov 2022 03:00:46 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443684.698416 (Exim 4.92) (envelope-from ) id 1oumBf-0007Vd-Aj; Tue, 15 Nov 2022 03:00:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443684.698416; Tue, 15 Nov 2022 03:00:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oumBf-0007VQ-6k; Tue, 15 Nov 2022 03:00:35 +0000 Received: by outflank-mailman (input) for mailman id 443684; Tue, 15 Nov 2022 03:00:34 +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 1oum4k-0000BJ-TG for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:53:27 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id ac712f45-6490-11ed-91b6-6bf2151ebd3b; Tue, 15 Nov 2022 03:53:25 +0100 (CET) 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 C902C11FB; Mon, 14 Nov 2022 18:53:30 -0800 (PST) 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 EA06A3F73B; Mon, 14 Nov 2022 18:53:21 -0800 (PST) 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: ac712f45-6490-11ed-91b6-6bf2151ebd3b 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 Subject: [PATCH v1 11/13] xen/arm: implement "xen,offset" feature when host address provided Date: Tue, 15 Nov 2022 10:52:33 +0800 Message-Id: <20221115025235.1378931-12-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 When host address is provided in "xen,shared-mem" property([pbase, gbase, psize]), it is easy to decide where to map to the borrower domain if "xen,offset" is also provided. The partially-shared region shall be starting at pbase + offset, and ending at pbase + size. Signed-off-by: Penny Zheng --- xen/arch/arm/domain_build.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index 80d37245a8..95600c640c 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -1112,7 +1112,7 @@ static int __init assign_shared_memory(struct domain *d, static int __init borrower_physmap_add_memory(struct domain *d, struct shm_membank *shm_membank, - paddr_t gbase) + paddr_t gbase, paddr_t offset) { int ret; unsigned int i; @@ -1141,8 +1141,8 @@ static int __init borrower_physmap_add_memory(struct domain *d, } else { - start = shm_membank->mem.bank->start; - size = shm_membank->mem.bank->size; + start = shm_membank->mem.bank->start + offset; + size = shm_membank->mem.bank->size - offset; sgfn = _gfn(PFN_UP(gbase)); /* Set up P2M foreign mapping for borrower domain. */ @@ -1157,7 +1157,7 @@ static int __init borrower_physmap_add_memory(struct domain *d, static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, paddr_t start, paddr_t size, - const char *shm_id) + const char *shm_id, paddr_t offset) { struct membank *membank; @@ -1170,7 +1170,7 @@ static int __init append_shm_bank_to_domain(struct kernel_info *kinfo, kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].mem.bank = membank; kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].mem.bank->start = start; - kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].mem.bank->size = size; + kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].mem.bank->size = size - offset; safe_strcpy(kinfo->shm_mem.bank[kinfo->shm_mem.nr_banks].shm_id, shm_id); kinfo->shm_mem.nr_banks++; @@ -1265,6 +1265,7 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, const char *shm_id; bool owner_dom_io = true, paddr_assigned = true; struct shm_membank *shm_membank; + uint64_t offset = 0UL; if ( !dt_device_is_compatible(shm_node, "xen,domain-shared-memory-v1") ) continue; @@ -1288,6 +1289,19 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, } BUG_ON((strlen(shm_id) <= 0) || (strlen(shm_id) >= MAX_SHM_ID_LENGTH)); + /* + * If "xen,offset" is provided, then only partial shared memory + * shall be mapped to borrower domain. + * "xen,offset" is a 64-bit integer and an optional property + */ + dt_property_read_u64(shm_node, "xen,offset", &offset); + if ( !IS_ALIGNED(offset, PAGE_SIZE) ) + { + printk("%pd: \"xen,offset\" 0x%lx is not suitably aligned\n", + d, offset); + return -EINVAL; + } + shm_membank = acquire_shm_membank(shm_id); if ( !shm_membank ) { @@ -1331,7 +1345,8 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, if ( owner_dom_io || (strcmp(role_str, "borrower") == 0) ) { - ret = borrower_physmap_add_memory(d, shm_membank, gbase); + ret = borrower_physmap_add_memory(d, shm_membank, gbase, + (paddr_t)offset); if ( ret ) { printk(XENLOG_ERR @@ -1345,7 +1360,8 @@ static int __init process_shm(struct domain *d, struct kernel_info *kinfo, * Record static shared memory region info for later setting * up shm-node in guest device tree. */ - ret = append_shm_bank_to_domain(kinfo, gbase, psize, shm_id); + ret = append_shm_bank_to_domain(kinfo, gbase, psize, shm_id, + (paddr_t)offset); if ( ret ) return ret; } From patchwork Tue Nov 15 02:52:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043151 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 6BF10C4332F for ; Tue, 15 Nov 2022 03:00:45 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443689.698427 (Exim 4.92) (envelope-from ) id 1oumBg-0007mQ-Hv; Tue, 15 Nov 2022 03:00:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443689.698427; Tue, 15 Nov 2022 03:00:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oumBg-0007mH-EC; Tue, 15 Nov 2022 03:00:36 +0000 Received: by outflank-mailman (input) for mailman id 443689; Tue, 15 Nov 2022 03:00:35 +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 1oum4p-0000BJ-U2 for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:53:31 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id ae41efa1-6490-11ed-91b6-6bf2151ebd3b; Tue, 15 Nov 2022 03:53:28 +0100 (CET) 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 E07E911FB; Mon, 14 Nov 2022 18:53:33 -0800 (PST) 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 0E26B3F73B; Mon, 14 Nov 2022 18:53:24 -0800 (PST) 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: ae41efa1-6490-11ed-91b6-6bf2151ebd3b 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 Subject: [PATCH v1 12/13] xen/arm: implement "xen,offset" feature when host address not provided Date: Tue, 15 Nov 2022 10:52:34 +0800 Message-Id: <20221115025235.1378931-13-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 When host address is not provided in "xen,shared-mem" property, shared memory region is allocated from heap by Xen. It is normally not contiguous and consisted of multiple memory blocks. Under above scenario, when "xen,offset" is also offered, we need to find at which memory block the offset locates, and the borrower memory map shall start at this block offset. Signed-off-by: Penny Zheng --- xen/arch/arm/domain_build.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c index 95600c640c..494f6aff2e 100644 --- a/xen/arch/arm/domain_build.c +++ b/xen/arch/arm/domain_build.c @@ -48,6 +48,10 @@ boolean_param("ext_regions", opt_ext_regions); static u64 __initdata dom0_mem; static bool __initdata dom0_mem_set; +#ifdef CONFIG_STATIC_SHM +static uint64_t __initdata allocated = 0UL; +#endif + static int __init parse_dom0_mem(const char *s) { dom0_mem_set = true; @@ -1110,6 +1114,26 @@ static int __init assign_shared_memory(struct domain *d, return ret; } +static bool __init find_anchor_offset(paddr_t *bank_start, paddr_t *bank_size, + uint64_t offset) +{ + uint64_t bank_offset; + + allocated += *bank_size; + if ( allocated <= offset ) + return false; + + /* Find the bank when offset locates */ + bank_offset = *bank_size - (allocated - offset); + *bank_start += bank_offset; + *bank_size = allocated - offset; + + /* Reset after finding the anchor */ + allocated = 0UL; + + return true; +} + static int __init borrower_physmap_add_memory(struct domain *d, struct shm_membank *shm_membank, paddr_t gbase, paddr_t offset) @@ -1123,12 +1147,17 @@ static int __init borrower_physmap_add_memory(struct domain *d, /* Host address is not provided in "xen,shared-mem" */ if ( shm_membank->mem.banks.meminfo ) { + bool found = false; meminfo = shm_membank->mem.banks.meminfo; + sgfn = _gfn(PFN_UP(gbase)); + for ( i = 0; i < meminfo->nr_banks; i++ ) { start = meminfo->bank[i].start; size = meminfo->bank[i].size; - sgfn = _gfn(PFN_UP(gbase)); + + if ( offset && !found ) + found = find_anchor_offset(&start, &size, offset); /* Set up P2M foreign mapping for borrower domain. */ ret = map_regions_p2mt(d, sgfn, PFN_DOWN(size), _mfn(PFN_UP(start)), From patchwork Tue Nov 15 02:52:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Penny Zheng X-Patchwork-Id: 13043152 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 CE5FEC433FE for ; Tue, 15 Nov 2022 03:00:45 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.443690.698433 (Exim 4.92) (envelope-from ) id 1oumBg-0007q7-Un; Tue, 15 Nov 2022 03:00:36 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 443690.698433; Tue, 15 Nov 2022 03:00:36 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1oumBg-0007pv-O6; Tue, 15 Nov 2022 03:00:36 +0000 Received: by outflank-mailman (input) for mailman id 443690; Tue, 15 Nov 2022 03:00:35 +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 1oum4s-0000BJ-UM for xen-devel@lists.xenproject.org; Tue, 15 Nov 2022 02:53:34 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id b0baba22-6490-11ed-91b6-6bf2151ebd3b; Tue, 15 Nov 2022 03:53:32 +0100 (CET) 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 146CE11FB; Mon, 14 Nov 2022 18:53:38 -0800 (PST) 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 25DE13F73B; Mon, 14 Nov 2022 18:53:27 -0800 (PST) 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: b0baba22-6490-11ed-91b6-6bf2151ebd3b 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 , Bertrand Marquis , Volodymyr Babchuk , Penny Zheng Subject: [PATCH v1 13/13] xen: make static shared memory supported in SUPPORT.md Date: Tue, 15 Nov 2022 10:52:35 +0800 Message-Id: <20221115025235.1378931-14-Penny.Zheng@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221115025235.1378931-1-Penny.Zheng@arm.com> References: <20221115025235.1378931-1-Penny.Zheng@arm.com> MIME-Version: 1.0 After patching previous commits, we could make feature of "static shared memory" supported in SUPPORT.md. Signed-off-by: Penny Zheng --- SUPPORT.md | 2 +- xen/arch/arm/Kconfig | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SUPPORT.md b/SUPPORT.md index ab71464cf6..c9fe7daf56 100644 --- a/SUPPORT.md +++ b/SUPPORT.md @@ -316,7 +316,7 @@ Allow sharing of identical pages between guests Allow to statically set up shared memory on dom0less system, enabling domains to do shm-based communication - Status, ARM: Tech Preview + Status, ARM: Supported ### Memory Paging diff --git a/xen/arch/arm/Kconfig b/xen/arch/arm/Kconfig index 52a05f704d..e0ea6e1762 100644 --- a/xen/arch/arm/Kconfig +++ b/xen/arch/arm/Kconfig @@ -141,7 +141,7 @@ config TEE source "arch/arm/tee/Kconfig" config STATIC_SHM - bool "Statically shared memory on a dom0less system" if UNSUPPORTED + bool "Statically shared memory on a dom0less system" depends on STATIC_MEMORY help This option enables statically shared memory on a dom0less system.