From nobody Sun May 12 15:40:25 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 1675217751664881.3735715258676; Tue, 31 Jan 2023 18:15:51 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.488036.755904 (Exim 4.92) (envelope-from ) id 1pN2ep-0001QG-4v; Wed, 01 Feb 2023 02:15:31 +0000 Received: by outflank-mailman (output) from mailman id 488036.755904; Wed, 01 Feb 2023 02:15:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pN2ep-0001Q7-21; Wed, 01 Feb 2023 02:15:31 +0000 Received: by outflank-mailman (input) for mailman id 488036; Wed, 01 Feb 2023 02:15:29 +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 1pN2en-000179-HZ for xen-devel@lists.xenproject.org; Wed, 01 Feb 2023 02:15:29 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-sth1.inumbo.com (Halon) with ESMTP id 4bbd7035-a1d6-11ed-933c-83870f6b2ba8; Wed, 01 Feb 2023 03:15: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 208284B3; Tue, 31 Jan 2023 18:16:10 -0800 (PST) Received: from a015966.shanghai.arm.com (a015966.shanghai.arm.com [10.169.190.24]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 979223F882; Tue, 31 Jan 2023 18:15:25 -0800 (PST) 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: 4bbd7035-a1d6-11ed-933c-83870f6b2ba8 From: Henry Wang To: xen-devel@lists.xenproject.org Cc: Henry Wang , Stefano Stabellini , Julien Grall , Wei Chen , Bertrand Marquis , Volodymyr Babchuk Subject: [PATCH v4 1/3] xen/arm: Add memory overlap check for bootinfo.reserved_mem Date: Wed, 1 Feb 2023 10:15:11 +0800 Message-Id: <20230201021513.336837-2-Henry.Wang@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230201021513.336837-1-Henry.Wang@arm.com> References: <20230201021513.336837-1-Henry.Wang@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1675217754416100001 Content-Type: text/plain; charset="utf-8" As we are having more and more types of static region, and all of these static regions are defined in bootinfo.reserved_mem, it is necessary to add the overlap check of reserved memory regions in Xen, because such check will help user to identify the misconfiguration in the device tree at the early stage of boot time. Currently we have 3 types of static region, namely (1) static memory (2) static heap (3) static shared memory (1) and (2) are parsed by the function `device_tree_get_meminfo()` and (3) is parsed using its own logic. All of parsed information of these types will be stored in `struct meminfo`. Therefore, to unify the overlap checking logic for all of these types, this commit firstly introduces a helper `meminfo_overlap_check()` and a function `check_reserved_regions_overlap()` to check if an input physical address range is overlapping with the existing memory regions defined in bootinfo. After that, use `check_reserved_regions_overlap()` in `device_tree_get_meminfo()` to do the overlap check of (1) and (2) and replace the original overlap check of (3) with `check_reserved_regions_overlap()`. Signed-off-by: Henry Wang Reviewed-by: Stefano Stabellini --- Stefano - I kept your reviewed-by tag since there is no actual code change in v4. Hope you'd be fine with this and thanks for the review. v3 -> v4: 1. Correct printk error message, end should be exclusive. 2. Add comment explaining the unhandled case where '*_end' could be 0 if the bank/region is at the end of the physical address space. 3. Add Stefano's reviewed-by tag. v2 -> v3: 1. Use "[start, end]" format in printk error message. 2. Change the return type of helper functions to bool. 3. Use 'start' and 'size' in helper functions to describe a region. v1 -> v2: 1. Split original `overlap_check()` to `meminfo_overlap_check()`. 2. Rework commit message. --- xen/arch/arm/bootfdt.c | 13 +++++---- xen/arch/arm/include/asm/setup.h | 2 ++ xen/arch/arm/setup.c | 45 ++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/xen/arch/arm/bootfdt.c b/xen/arch/arm/bootfdt.c index 0085c28d74..e2f6c7324b 100644 --- a/xen/arch/arm/bootfdt.c +++ b/xen/arch/arm/bootfdt.c @@ -88,6 +88,9 @@ static int __init device_tree_get_meminfo(const void *fdt= , int node, for ( i =3D 0; i < banks && mem->nr_banks < NR_MEM_BANKS; i++ ) { device_tree_get_reg(&cell, address_cells, size_cells, &start, &siz= e); + if ( mem =3D=3D &bootinfo.reserved_mem && + check_reserved_regions_overlap(start, size) ) + return -EINVAL; /* Some DT may describe empty bank, ignore them */ if ( !size ) continue; @@ -482,7 +485,9 @@ static int __init process_shm_node(const void *fdt, int= node, return -EINVAL; } =20 - if ( (end <=3D mem->bank[i].start) || (paddr >=3D bank_end) ) + if ( check_reserved_regions_overlap(paddr, size) ) + return -EINVAL; + else { if ( strcmp(shm_id, mem->bank[i].shm_id) !=3D 0 ) continue; @@ -493,12 +498,6 @@ static int __init process_shm_node(const void *fdt, in= t node, return -EINVAL; } } - else - { - printk("fdt: shared memory region overlap with an existing= entry %#"PRIpaddr" - %#"PRIpaddr"\n", - mem->bank[i].start, bank_end); - return -EINVAL; - } } } =20 diff --git a/xen/arch/arm/include/asm/setup.h b/xen/arch/arm/include/asm/se= tup.h index a926f30a2b..f0592370ea 100644 --- a/xen/arch/arm/include/asm/setup.h +++ b/xen/arch/arm/include/asm/setup.h @@ -143,6 +143,8 @@ void fw_unreserved_regions(paddr_t s, paddr_t e, size_t boot_fdt_info(const void *fdt, paddr_t paddr); const char *boot_fdt_cmdline(const void *fdt); =20 +bool check_reserved_regions_overlap(paddr_t region_start, paddr_t region_s= ize); + struct bootmodule *add_boot_module(bootmodule_kind kind, paddr_t start, paddr_t size, bool domU); struct bootmodule *boot_module_find_by_kind(bootmodule_kind kind); diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c index 1f26f67b90..28c79a413f 100644 --- a/xen/arch/arm/setup.c +++ b/xen/arch/arm/setup.c @@ -261,6 +261,36 @@ static void __init dt_unreserved_regions(paddr_t s, pa= ddr_t e, cb(s, e); } =20 +/* + * TODO: '*_end' could be 0 if the bank/region is at the end of the physic= al + * address space. This is for now not handled as it requires more rework. + */ +static bool __init meminfo_overlap_check(struct meminfo *meminfo, + paddr_t region_start, + paddr_t region_size) +{ + paddr_t bank_start =3D INVALID_PADDR, bank_end =3D 0; + paddr_t region_end =3D region_start + region_size; + unsigned int i, bank_num =3D meminfo->nr_banks; + + for ( i =3D 0; i < bank_num; i++ ) + { + bank_start =3D meminfo->bank[i].start; + bank_end =3D bank_start + meminfo->bank[i].size; + + if ( region_end <=3D bank_start || region_start >=3D bank_end ) + continue; + else + { + printk("Region: [%#"PRIpaddr", %#"PRIpaddr") overlapping with = bank[%u]: [%#"PRIpaddr", %#"PRIpaddr")\n", + region_start, region_end, i, bank_start, bank_end); + return true; + } + } + + return false; +} + void __init fw_unreserved_regions(paddr_t s, paddr_t e, void (*cb)(paddr_t, paddr_t), unsigned int first) @@ -271,7 +301,22 @@ void __init fw_unreserved_regions(paddr_t s, paddr_t e, cb(s, e); } =20 +/* + * Given an input physical address range, check if this range is overlappi= ng + * with the existing reserved memory regions defined in bootinfo. + * Return true if the input physical address range is overlapping with any + * existing reserved memory regions, otherwise false. + */ +bool __init check_reserved_regions_overlap(paddr_t region_start, + paddr_t region_size) +{ + /* Check if input region is overlapping with bootinfo.reserved_mem ban= ks */ + if ( meminfo_overlap_check(&bootinfo.reserved_mem, + region_start, region_size) ) + return true; =20 + return false; +} =20 struct bootmodule __init *add_boot_module(bootmodule_kind kind, paddr_t start, paddr_t size, --=20 2.25.1 From nobody Sun May 12 15:40:25 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 1675217758632802.7029321458145; Tue, 31 Jan 2023 18:15:58 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.488037.755915 (Exim 4.92) (envelope-from ) id 1pN2ex-0001lM-DG; Wed, 01 Feb 2023 02:15:39 +0000 Received: by outflank-mailman (output) from mailman id 488037.755915; Wed, 01 Feb 2023 02:15:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pN2ex-0001lF-A4; Wed, 01 Feb 2023 02:15:39 +0000 Received: by outflank-mailman (input) for mailman id 488037; Wed, 01 Feb 2023 02:15:37 +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 1pN2ev-0001AP-42 for xen-devel@lists.xenproject.org; Wed, 01 Feb 2023 02:15:37 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 4f450631-a1d6-11ed-b63b-5f92e7d2e73a; Wed, 01 Feb 2023 03:15:34 +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 ADFD44B3; Tue, 31 Jan 2023 18:16:16 -0800 (PST) Received: from a015966.shanghai.arm.com (a015966.shanghai.arm.com [10.169.190.24]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 30FBE3F882; Tue, 31 Jan 2023 18:15:31 -0800 (PST) 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: 4f450631-a1d6-11ed-b63b-5f92e7d2e73a From: Henry Wang To: xen-devel@lists.xenproject.org Cc: Henry Wang , Stefano Stabellini , Julien Grall , Wei Chen , Bertrand Marquis , Volodymyr Babchuk Subject: [PATCH v4 2/3] xen/arm: Extend the memory overlap check to include bootmodules Date: Wed, 1 Feb 2023 10:15:12 +0800 Message-Id: <20230201021513.336837-3-Henry.Wang@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230201021513.336837-1-Henry.Wang@arm.com> References: <20230201021513.336837-1-Henry.Wang@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1675217760045100003 Content-Type: text/plain; charset="utf-8" Similarly as the static regions defined in bootinfo.reserved_mem, the bootmodule regions defined in bootinfo.modules should also not be overlapping with memory regions in either bootinfo.reserved_mem or bootinfo.modules. Therefore, this commit introduces a helper `bootmodules_overlap_check()` and uses this helper to extend the check in function `check_reserved_regions_overlap()` so that memory regions in bootinfo.modules are included. Use `check_reserved_regions_overlap()` in `add_boot_module()` to return early if any error occurs. Signed-off-by: Henry Wang Reviewed-by: Stefano Stabellini --- v3 -> v4: 1. Correct printk error message, end should be exclusive. 2. Add comment explaining the unhandled case where '*_end' could be 0 if the module/region is at the end of the physical address space. 3. Add Stefano's reviewed-by tag. v2 -> v3: 1. Use "[start, end]" format in printk error message. 2. Change the return type of helper functions to bool. 3. Use 'start' and 'size' in helper functions to describe a region. v1 -> v2: 1. Split original `overlap_check()` to `bootmodules_overlap_check()`. 2. Rework commit message. --- xen/arch/arm/setup.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c index 28c79a413f..1ea42a0386 100644 --- a/xen/arch/arm/setup.c +++ b/xen/arch/arm/setup.c @@ -291,6 +291,36 @@ static bool __init meminfo_overlap_check(struct meminf= o *meminfo, return false; } =20 +/* + * TODO: '*_end' could be 0 if the module/region is at the end of the phys= ical + * address space. This is for now not handled as it requires more rework. + */ +static bool __init bootmodules_overlap_check(struct bootmodules *bootmodul= es, + paddr_t region_start, + paddr_t region_size) +{ + paddr_t mod_start =3D INVALID_PADDR, mod_end =3D 0; + paddr_t region_end =3D region_start + region_size; + unsigned int i, mod_num =3D bootmodules->nr_mods; + + for ( i =3D 0; i < mod_num; i++ ) + { + mod_start =3D bootmodules->module[i].start; + mod_end =3D mod_start + bootmodules->module[i].size; + + if ( region_end <=3D mod_start || region_start >=3D mod_end ) + continue; + else + { + printk("Region: [%#"PRIpaddr", %#"PRIpaddr") overlapping with = mod[%u]: [%#"PRIpaddr", %#"PRIpaddr")\n", + region_start, region_end, i, mod_start, mod_end); + return true; + } + } + + return false; +} + void __init fw_unreserved_regions(paddr_t s, paddr_t e, void (*cb)(paddr_t, paddr_t), unsigned int first) @@ -315,6 +345,11 @@ bool __init check_reserved_regions_overlap(paddr_t reg= ion_start, region_start, region_size) ) return true; =20 + /* Check if input region is overlapping with bootmodules */ + if ( bootmodules_overlap_check(&bootinfo.modules, + region_start, region_size) ) + return true; + return false; } =20 @@ -332,6 +367,10 @@ struct bootmodule __init *add_boot_module(bootmodule_k= ind kind, boot_module_kind_as_string(kind), start, start + size); return NULL; } + + if ( check_reserved_regions_overlap(start, size) ) + return NULL; + for ( i =3D 0 ; i < mods->nr_mods ; i++ ) { mod =3D &mods->module[i]; --=20 2.25.1 From nobody Sun May 12 15:40:25 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 1675217758313597.429245934238; Tue, 31 Jan 2023 18:15:58 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.488038.755926 (Exim 4.92) (envelope-from ) id 1pN2ez-00023G-Km; Wed, 01 Feb 2023 02:15:41 +0000 Received: by outflank-mailman (output) from mailman id 488038.755926; Wed, 01 Feb 2023 02:15:41 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pN2ez-000239-He; Wed, 01 Feb 2023 02:15:41 +0000 Received: by outflank-mailman (input) for mailman id 488038; Wed, 01 Feb 2023 02:15:40 +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 1pN2ey-0001AP-N7 for xen-devel@lists.xenproject.org; Wed, 01 Feb 2023 02:15:40 +0000 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by se1-gles-flk1.inumbo.com (Halon) with ESMTP id 517cf61f-a1d6-11ed-b63b-5f92e7d2e73a; Wed, 01 Feb 2023 03:15:38 +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 57DF04B3; Tue, 31 Jan 2023 18:16:20 -0800 (PST) Received: from a015966.shanghai.arm.com (a015966.shanghai.arm.com [10.169.190.24]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id CED7E3F882; Tue, 31 Jan 2023 18:15:35 -0800 (PST) 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: 517cf61f-a1d6-11ed-b63b-5f92e7d2e73a From: Henry Wang To: xen-devel@lists.xenproject.org Cc: Henry Wang , Stefano Stabellini , Julien Grall , Wei Chen , Bertrand Marquis , Volodymyr Babchuk Subject: [PATCH v4 3/3] xen/arm: Extend the memory overlap check to include EfiACPIReclaimMemory Date: Wed, 1 Feb 2023 10:15:13 +0800 Message-Id: <20230201021513.336837-4-Henry.Wang@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230201021513.336837-1-Henry.Wang@arm.com> References: <20230201021513.336837-1-Henry.Wang@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1675217759869100001 Content-Type: text/plain; charset="utf-8" Similarly as the static regions and boot modules, memory regions with EfiACPIReclaimMemory type (defined in bootinfo.acpi if CONFIG_ACPI is enabled) should also not be overlapping with memory regions in bootinfo.reserved_mem and bootinfo.modules. Therefore, this commit reuses the `meminfo_overlap_check()` to further extends the check in function `check_reserved_regions_overlap()` so that memory regions in bootinfo.acpi are included. If any error occurs in the extended `check_reserved_regions_overlap()`, the `meminfo_add_bank()` defined in `efi-boot.h` will return early. Signed-off-by: Henry Wang Reviewed-by: Stefano Stabellini --- v3 -> v4: 1. No changes. v2 -> v3: 1. Rebase on top of patch #1 and #2. 2. Add Stefano's Reviewed-by tag. v1 -> v2: 1. Rebase on top of patch #1 and #2. --- xen/arch/arm/efi/efi-boot.h | 10 ++++++++-- xen/arch/arm/setup.c | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/xen/arch/arm/efi/efi-boot.h b/xen/arch/arm/efi/efi-boot.h index 223db0c4da..bb64925d70 100644 --- a/xen/arch/arm/efi/efi-boot.h +++ b/xen/arch/arm/efi/efi-boot.h @@ -161,13 +161,19 @@ static bool __init meminfo_add_bank(struct meminfo *m= em, EFI_MEMORY_DESCRIPTOR *desc) { struct membank *bank; + paddr_t start =3D desc->PhysicalStart; + paddr_t size =3D desc->NumberOfPages * EFI_PAGE_SIZE; =20 if ( mem->nr_banks >=3D NR_MEM_BANKS ) return false; +#ifdef CONFIG_ACPI + if ( check_reserved_regions_overlap(start, size) ) + return false; +#endif =20 bank =3D &mem->bank[mem->nr_banks]; - bank->start =3D desc->PhysicalStart; - bank->size =3D desc->NumberOfPages * EFI_PAGE_SIZE; + bank->start =3D start; + bank->size =3D size; =20 mem->nr_banks++; =20 diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c index 1ea42a0386..6f9f4d8c8a 100644 --- a/xen/arch/arm/setup.c +++ b/xen/arch/arm/setup.c @@ -350,6 +350,12 @@ bool __init check_reserved_regions_overlap(paddr_t reg= ion_start, region_start, region_size) ) return true; =20 +#ifdef CONFIG_ACPI + /* Check if input region is overlapping with ACPI EfiACPIReclaimMemory= */ + if ( meminfo_overlap_check(&bootinfo.acpi, region_start, region_size) ) + return true; +#endif + return false; } =20 --=20 2.25.1