From nobody Fri Nov 22 18:37:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=arm.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1716364342661684.3541696040103; Wed, 22 May 2024 00:52:22 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.727263.1131689 (Exim 4.92) (envelope-from ) id 1s9glZ-0005dO-4A; Wed, 22 May 2024 07:52:05 +0000 Received: by outflank-mailman (output) from mailman id 727263.1131689; Wed, 22 May 2024 07:52:05 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1s9glZ-0005cP-08; Wed, 22 May 2024 07:52:05 +0000 Received: by outflank-mailman (input) for mailman id 727263; Wed, 22 May 2024 07:52:03 +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 1s9glX-0005YY-M3 for xen-devel@lists.xenproject.org; Wed, 22 May 2024 07:52:03 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id 2c9faaab-1810-11ef-90a0-e314d9c70b13; Wed, 22 May 2024 09:52:02 +0200 (CEST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CF224DA7; Wed, 22 May 2024 00:52:25 -0700 (PDT) Received: from e125770.cambridge.arm.com (e125770.arm.com [10.1.199.43]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E8DDB3F766; Wed, 22 May 2024 00:52:00 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 2c9faaab-1810-11ef-90a0-e314d9c70b13 From: Luca Fancellu To: xen-devel@lists.xenproject.org Cc: Stefano Stabellini , Julien Grall , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk Subject: [PATCH v3 1/7] xen/arm: Lookup bootinfo shm bank during the mapping Date: Wed, 22 May 2024 08:51:45 +0100 Message-Id: <20240522075151.3373899-2-luca.fancellu@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240522075151.3373899-1-luca.fancellu@arm.com> References: <20240522075151.3373899-1-luca.fancellu@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1716364343329100003 Content-Type: text/plain; charset="utf-8" The current static shared memory code is using bootinfo banks when it needs to find the number of borrowers, so every time assign_shared_memory is called, the bank is searched in the bootinfo.shmem structure. There is nothing wrong with it, however the bank can be used also to retrieve the start address and size and also to pass less argument to assign_shared_memory. When retrieving the information from the bootinfo bank, it's also possible to move the checks on alignment to process_shm_node in the early stages. So create a new function find_shm_bank_by_id() which takes a 'struct shared_meminfo' structure and the shared memory ID, to look for a bank with a matching ID, take the physical host address and size from the bank, pass the bank to assign_shared_memory() removing the now unnecessary arguments and finally remove the acquire_nr_borrower_domain() function since now the information can be extracted from the passed bank. Move the "xen,shm-id" parsing early in process_shm to bail out quickly in case of errors (unlikely), as said above, move the checks on alignment to process_shm_node. Drawback of this change is that now the bootinfo are used also when the bank doesn't need to be allocated, however it will be convenient later to use it as an argument for assign_shared_memory when dealing with the use case where the Host physical address is not supplied by the user. Signed-off-by: Luca Fancellu Reviewed-by: Michal Orzel --- v3 changes: - switch strncmp with strcmp in find_shm_bank_by_id, fix commit msg typo, add R-by Michal. v2 changes: - fix typo commit msg, renamed find_shm() to find_shm_bank_by_id(), swap region size check different from zero and size alignment, remove not necessary BUGON(). (Michal) --- xen/arch/arm/static-shmem.c | 100 +++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 47 deletions(-) diff --git a/xen/arch/arm/static-shmem.c b/xen/arch/arm/static-shmem.c index 78881dd1d3f7..0a1c327e90ea 100644 --- a/xen/arch/arm/static-shmem.c +++ b/xen/arch/arm/static-shmem.c @@ -19,29 +19,21 @@ static void __init __maybe_unused build_assertions(void) offsetof(struct shared_meminfo, bank))); } =20 -static int __init acquire_nr_borrower_domain(struct domain *d, - paddr_t pbase, paddr_t psize, - unsigned long *nr_borrowers) +static const struct membank __init * +find_shm_bank_by_id(const struct membanks *shmem, const char *shm_id) { - const struct membanks *shmem =3D bootinfo_get_shmem(); unsigned int bank; =20 - /* Iterate reserved memory to find requested shm bank. */ for ( bank =3D 0 ; bank < shmem->nr_banks; bank++ ) { - paddr_t bank_start =3D shmem->bank[bank].start; - paddr_t bank_size =3D shmem->bank[bank].size; - - if ( (pbase =3D=3D bank_start) && (psize =3D=3D bank_size) ) + if ( strcmp(shm_id, shmem->bank[bank].shmem_extra->shm_id) =3D=3D = 0 ) break; } =20 if ( bank =3D=3D shmem->nr_banks ) - return -ENOENT; + return NULL; =20 - *nr_borrowers =3D shmem->bank[bank].shmem_extra->nr_shm_borrowers; - - return 0; + return &shmem->bank[bank]; } =20 /* @@ -103,14 +95,18 @@ static mfn_t __init acquire_shared_memory_bank(struct = domain *d, return smfn; } =20 -static int __init assign_shared_memory(struct domain *d, - paddr_t pbase, paddr_t psize, - paddr_t gbase) +static int __init assign_shared_memory(struct domain *d, paddr_t gbase, + const struct membank *shm_bank) { mfn_t smfn; int ret =3D 0; unsigned long nr_pages, nr_borrowers, i; struct page_info *page; + paddr_t pbase, psize; + + pbase =3D shm_bank->start; + psize =3D shm_bank->size; + nr_borrowers =3D shm_bank->shmem_extra->nr_shm_borrowers; =20 printk("%pd: allocate static shared memory BANK %#"PRIpaddr"-%#"PRIpad= dr".\n", d, pbase, pbase + psize); @@ -135,14 +131,6 @@ static int __init assign_shared_memory(struct domain *= d, } } =20 - /* - * Get the right amount of references per page, which is the number of - * borrower domains. - */ - ret =3D acquire_nr_borrower_domain(d, pbase, psize, &nr_borrowers); - if ( ret ) - return ret; - /* * Instead of letting borrower domain get a page ref, we add as many * additional reference as the number of borrowers when the owner @@ -199,6 +187,7 @@ int __init process_shm(struct domain *d, struct kernel_= info *kinfo, =20 dt_for_each_child_node(node, shm_node) { + const struct membank *boot_shm_bank; const struct dt_property *prop; const __be32 *cells; uint32_t addr_cells, size_cells; @@ -212,6 +201,23 @@ int __init process_shm(struct domain *d, struct kernel= _info *kinfo, if ( !dt_device_is_compatible(shm_node, "xen,domain-shared-memory-= v1") ) continue; =20 + if ( dt_property_read_string(shm_node, "xen,shm-id", &shm_id) ) + { + printk("%pd: invalid \"xen,shm-id\" property", d); + return -EINVAL; + } + BUG_ON((strlen(shm_id) <=3D 0) || (strlen(shm_id) >=3D MAX_SHM_ID_= LENGTH)); + + boot_shm_bank =3D find_shm_bank_by_id(bootinfo_get_shmem(), shm_id= ); + if ( !boot_shm_bank ) + { + printk("%pd: static shared memory bank not found: '%s'", d, sh= m_id); + return -ENOENT; + } + + pbase =3D boot_shm_bank->start; + psize =3D boot_shm_bank->size; + /* * xen,shared-mem =3D ; * TODO: pbase is optional. @@ -221,20 +227,7 @@ int __init process_shm(struct domain *d, struct kernel= _info *kinfo, prop =3D dt_find_property(shm_node, "xen,shared-mem", NULL); BUG_ON(!prop); cells =3D (const __be32 *)prop->value; - device_tree_get_reg(&cells, addr_cells, addr_cells, &pbase, &gbase= ); - psize =3D dt_read_paddr(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; - } + gbase =3D dt_read_paddr(cells + addr_cells, addr_cells); =20 for ( i =3D 0; i < PFN_DOWN(psize); i++ ) if ( !mfn_valid(mfn_add(maddr_to_mfn(pbase), i)) ) @@ -251,13 +244,6 @@ int __init process_shm(struct domain *d, struct kernel= _info *kinfo, if ( dt_property_read_string(shm_node, "role", &role_str) =3D=3D 0= ) owner_dom_io =3D false; =20 - if ( dt_property_read_string(shm_node, "xen,shm-id", &shm_id) ) - { - printk("%pd: invalid \"xen,shm-id\" property", d); - return -EINVAL; - } - BUG_ON((strlen(shm_id) <=3D 0) || (strlen(shm_id) >=3D MAX_SHM_ID_= LENGTH)); - /* * DOMID_IO is a fake domain and is not described in the Device-Tr= ee. * Therefore when the owner of the shared region is DOMID_IO, we w= ill @@ -270,8 +256,8 @@ int __init process_shm(struct domain *d, struct kernel_= info *kinfo, * We found the first borrower of the region, the owner was not * specified, so they should be assigned to dom_io. */ - ret =3D assign_shared_memory(owner_dom_io ? dom_io : d, - pbase, psize, gbase); + ret =3D assign_shared_memory(owner_dom_io ? dom_io : d, gbase, + boot_shm_bank); if ( ret ) return ret; } @@ -439,12 +425,32 @@ int __init process_shm_node(const void *fdt, int node= , uint32_t address_cells, device_tree_get_reg(&cell, address_cells, address_cells, &paddr, &gadd= r); size =3D dt_next_cell(size_cells, &cell); =20 + if ( !IS_ALIGNED(paddr, PAGE_SIZE) ) + { + printk("fdt: physical address 0x%"PRIpaddr" is not suitably aligne= d.\n", + paddr); + return -EINVAL; + } + + if ( !IS_ALIGNED(gaddr, PAGE_SIZE) ) + { + printk("fdt: guest address 0x%"PRIpaddr" is not suitably aligned.\= n", + gaddr); + return -EINVAL; + } + if ( !size ) { printk("fdt: the size for static shared memory region can not be z= ero\n"); return -EINVAL; } =20 + if ( !IS_ALIGNED(size, PAGE_SIZE) ) + { + printk("fdt: size 0x%"PRIpaddr" is not suitably aligned\n", size); + return -EINVAL; + } + end =3D paddr + size; if ( end <=3D paddr ) { --=20 2.34.1