From nobody Tue Feb 10 03:50:37 2026 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 986D682499; Fri, 6 Sep 2024 18:55:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725648953; cv=none; b=dzPD9s7U/x/Fx2o/Gjh+4RZadoH2H3kCwP0JEjB2nXGpAIyC/sttWieKWaG3jBPrp326npyRs9GLHdz//u6QM1t+4slNYoOg8jtW2eIjAdtoqOLxP6toMoVqIOWLGldJx9Jom6mWvzv2ngO5+S+3EgMGYI4fJ/3KCuqX74YTPkU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725648953; c=relaxed/simple; bh=bCpE09tqXxkFB12Gs/CrXFadNitg/OwzKyRBLS7eiV4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=sgIEyaJMZTn2vBQaIwzwppaRhPevf8LAhv4NcaUEog1FnB92EhAmpSWmIgiysOdfbqIOwKnyA1uC7gz+luVvfxhHrZAmQcwKbE92pk9jqL1jks5E3k07NFEqBsUYY7gmDgV30hRA8qJLAanh7WrshQQNnoDlvSBAxF2/a/SKsa4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com; spf=pass smtp.mailfrom=quicinc.com; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b=Srqk6R93; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=quicinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="Srqk6R93" Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4868jeTx027941; Fri, 6 Sep 2024 18:55:32 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= 7Ys3cjUEHo3Gzrw+2RIeljyNoEuoxDhDs3PBcxTctYE=; b=Srqk6R93Ztl80oSJ 46mUJP4APtd/ebiXS68VZPcNtL8d8p1HYwcY+v+qKwxTaUHFN5G6CaYzOHu/wFz9 gx5ZnUsx36X1RtsQQYZfMjivwDS5Xp/ljMKaFCl4xFmx3EK2r2mlwSw8rs7rCMFg xPtsX/ywS2tcxmF8TjHe4GZrK1ZzeLbSQyYV+NSp3HW1apYETDCoU36JDxwUW1+0 I+Qt8ttuolQnpexDTTC3Rr/K0b/ULMcdNKT9IxtOx4NnS8XwgOOLpLysmgoilAvY hbJusGHeCLzlxLzzMb3rsvsS6vEB8PG+Kyv7XrBCVnmhDwOFfT189806Ulum7KAC +aLCrA== Received: from nalasppmta01.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 41fhx1u7fn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 06 Sep 2024 18:55:32 +0000 (GMT) Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA01.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 486ItUhc026365 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 6 Sep 2024 18:55:30 GMT Received: from hu-obabatun-lv.qualcomm.com (10.49.16.6) by nalasex01b.na.qualcomm.com (10.47.209.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Fri, 6 Sep 2024 11:55:27 -0700 From: Oreoluwa Babatunde To: CC: , , , , , , , , , , , , , , Subject: [PATCH v9 1/2] of: reserved_mem: Restruture how the reserved memory regions are processed Date: Fri, 6 Sep 2024 11:53:59 -0700 Message-ID: <20240906185400.3244416-2-quic_obabatun@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240906185400.3244416-1-quic_obabatun@quicinc.com> References: <20240906185400.3244416-1-quic_obabatun@quicinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: nalasex01c.na.qualcomm.com (10.47.97.35) To nalasex01b.na.qualcomm.com (10.47.209.197) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: CrVDY-YHza_u4P1tHiYcS0w3uGQqQdm5 X-Proofpoint-GUID: CrVDY-YHza_u4P1tHiYcS0w3uGQqQdm5 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-06_04,2024-09-06_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 bulkscore=0 clxscore=1015 impostorscore=0 suspectscore=0 mlxlogscore=999 phishscore=0 lowpriorityscore=0 spamscore=0 priorityscore=1501 adultscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2408220000 definitions=main-2409060137 Content-Type: text/plain; charset="utf-8" Reserved memory regions defined in the devicetree can be broken up into two groups: i) Statically-placed reserved memory regions i.e. regions defined with a static start address and size using the "reg" property. ii) Dynamically-placed reserved memory regions. i.e. regions defined by specifying an address range where they can be placed in memory using the "alloc_ranges" and "size" properties. These regions are processed and set aside at boot time. This is done in two stages as seen below: Stage 1: At this stage, fdt_scan_reserved_mem() scans through the child nodes of the reserved_memory node using the flattened devicetree and does the following: 1) If the node represents a statically-placed reserved memory region, i.e. if it is defined using the "reg" property: - Call memblock_reserve() or memblock_mark_nomap() as needed. - Add the information for that region into the reserved_mem array using fdt_reserved_mem_save_node(). i.e. fdt_reserved_mem_save_node(node, name, base, size). 2) If the node represents a dynamically-placed reserved memory region, i.e. if it is defined using "alloc-ranges" and "size" properties: - Add the information for that region to the reserved_mem array with the starting address and size set to 0. i.e. fdt_reserved_mem_save_node(node, name, 0, 0). Note: This region is saved to the array with a starting address of 0 because a starting address is not yet allocated for it. Stage 2: After iterating through all the reserved memory nodes and storing their relevant information in the reserved_mem array,fdt_init_reserved_mem() is called and does the following: 1) For statically-placed reserved memory regions: - Call the region specific init function using __reserved_mem_init_node(). 2) For dynamically-placed reserved memory regions: - Call __reserved_mem_alloc_size() which is used to allocate memory for each of these regions, and mark them as nomap if they have the nomap property specified in the DT. - Call the region specific init function. The current size of the resvered_mem array is 64 as is defined by MAX_RESERVED_REGIONS. This means that there is a limitation of 64 for how many reserved memory regions can be specified on a system. As systems continue to grow more and more complex, the number of reserved memory regions needed are also growing and are starting to hit this 64 count limit, hence the need to make the reserved_mem array dynamically sized (i.e. dynamically allocating memory for the reserved_mem array using membock_alloc_*). On architectures such as arm64, memory allocated using memblock is writable only after the page tables have been setup. This means that if the reserved_mem array is going to be dynamically allocated, it needs to happen after the page tables have been setup, not before. Since the reserved memory regions are currently being processed and added to the array before the page tables are setup, there is a need to change the order in which some of the processing is done to allow for the reserved_mem array to be dynamically sized. It is possible to process the statically-placed reserved memory regions without needing to store them in the reserved_mem array until after the page tables have been setup because all the information stored in the array is readily available in the devicetree and can be referenced at any time. Dynamically-placed reserved memory regions on the other hand get assigned a start address only at runtime, and hence need a place to be stored once they are allocated since there is no other referrence to the start address for these regions. Hence this patch changes the processing order of the reserved memory regions in the following ways: Step 1: fdt_scan_reserved_mem() scans through the child nodes of the reserved_memory node using the flattened devicetree and does the following: 1) If the node represents a statically-placed reserved memory region, i.e. if it is defined using the "reg" property: - Call memblock_reserve() or memblock_mark_nomap() as needed. 2) If the node represents a dynamically-placed reserved memory region, i.e. if it is defined using "alloc-ranges" and "size" properties: - Call __reserved_mem_alloc_size() which will: i) Allocate memory for the reserved region and call memblock_mark_nomap() as needed. ii) Call the region specific initialization function using fdt_init_reserved_mem_node(). iii) Save the region information in the reserved_mem array using fdt_reserved_mem_save_node(). Step 2: 1) This stage of the reserved memory processing is now only used to add the statically-placed reserved memory regions into the reserved_mem array using fdt_scan_reserved_mem_reg_nodes(), as well as call their region specific initialization functions. 2) This step has also been moved to be after the page tables are setup. Moving this will allow us to replace the reserved_mem array with a dynamically sized array before storing the rest of these regions. Signed-off-by: Oreoluwa Babatunde --- drivers/of/fdt.c | 5 +- drivers/of/of_private.h | 3 +- drivers/of/of_reserved_mem.c | 168 ++++++++++++++++++++++++----------- 3 files changed, 122 insertions(+), 54 deletions(-) diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index 68103ad230ee..276d189e808b 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -511,8 +511,6 @@ void __init early_init_fdt_scan_reserved_mem(void) break; memblock_reserve(base, size); } - - fdt_init_reserved_mem(); } =20 /** @@ -1212,6 +1210,9 @@ void __init unflatten_device_tree(void) { void *fdt =3D initial_boot_params; =20 + /* Save the statically-placed regions in the reserved_mem array */ + fdt_scan_reserved_mem_reg_nodes(); + /* Don't use the bootloader provided DTB if ACPI is enabled */ if (!acpi_disabled) fdt =3D NULL; diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h index 04aa2a91f851..29525c0b9939 100644 --- a/drivers/of/of_private.h +++ b/drivers/of/of_private.h @@ -9,6 +9,7 @@ */ =20 #define FDT_ALIGN_SIZE 8 +#define MAX_RESERVED_REGIONS 64 =20 /** * struct alias_prop - Alias property in 'aliases' node @@ -180,7 +181,7 @@ static inline struct device_node *__of_get_dma_parent(c= onst struct device_node * #endif =20 int fdt_scan_reserved_mem(void); -void fdt_init_reserved_mem(void); +void __init fdt_scan_reserved_mem_reg_nodes(void); =20 bool of_fdt_device_is_available(const void *blob, unsigned long node); =20 diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c index 46e1c3fbc769..2011174211f9 100644 --- a/drivers/of/of_reserved_mem.c +++ b/drivers/of/of_reserved_mem.c @@ -27,7 +27,6 @@ =20 #include "of_private.h" =20 -#define MAX_RESERVED_REGIONS 64 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS]; static int reserved_mem_count; =20 @@ -56,6 +55,7 @@ static int __init early_init_dt_alloc_reserved_memory_arc= h(phys_addr_t size, return err; } =20 +static void __init fdt_init_reserved_mem_node(struct reserved_mem *rmem); /* * fdt_reserved_mem_save_node() - save fdt node for second pass initializa= tion */ @@ -74,6 +74,9 @@ static void __init fdt_reserved_mem_save_node(unsigned lo= ng node, const char *un rmem->base =3D base; rmem->size =3D size; =20 + /* Call the region specific initialization function */ + fdt_init_reserved_mem_node(rmem); + reserved_mem_count++; return; } @@ -106,7 +109,6 @@ static int __init __reserved_mem_reserve_reg(unsigned l= ong node, phys_addr_t base, size; int len; const __be32 *prop; - int first =3D 1; bool nomap; =20 prop =3D of_get_flat_dt_prop(node, "reg", &len); @@ -134,10 +136,6 @@ static int __init __reserved_mem_reserve_reg(unsigned = long node, uname, &base, (unsigned long)(size / SZ_1M)); =20 len -=3D t_len; - if (first) { - fdt_reserved_mem_save_node(node, uname, base, size); - first =3D 0; - } } return 0; } @@ -165,12 +163,77 @@ static int __init __reserved_mem_check_root(unsigned = long node) return 0; } =20 +static void __init __rmem_check_for_overlap(void); + +/** + * fdt_scan_reserved_mem_reg_nodes() - Store info for the "reg" defined + * reserved memory regions. + * + * This function is used to scan through the DT and store the + * information for the reserved memory regions that are defined using + * the "reg" property. The region node number, name, base address, and + * size are all stored in the reserved_mem array by calling the + * fdt_reserved_mem_save_node() function. + */ +void __init fdt_scan_reserved_mem_reg_nodes(void) +{ + int t_len =3D (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32); + const void *fdt =3D initial_boot_params; + phys_addr_t base, size; + const __be32 *prop; + int node, child; + int len; + + if (!fdt) + return; + + node =3D fdt_path_offset(fdt, "/reserved-memory"); + if (node < 0) { + pr_info("Reserved memory: No reserved-memory node in the DT\n"); + return; + } + + if (__reserved_mem_check_root(node)) { + pr_err("Reserved memory: unsupported node format, ignoring\n"); + return; + } + + fdt_for_each_subnode(child, fdt, node) { + const char *uname; + + prop =3D of_get_flat_dt_prop(child, "reg", &len); + if (!prop) + continue; + if (!of_fdt_device_is_available(fdt, child)) + continue; + + uname =3D fdt_get_name(fdt, child, NULL); + if (len && len % t_len !=3D 0) { + pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n= ", + uname); + continue; + } + base =3D dt_mem_next_cell(dt_root_addr_cells, &prop); + size =3D dt_mem_next_cell(dt_root_size_cells, &prop); + + if (size) + fdt_reserved_mem_save_node(child, uname, base, size); + } + + /* check for overlapping reserved regions */ + __rmem_check_for_overlap(); +} + +static int __init __reserved_mem_alloc_size(unsigned long node, const char= *uname); + /* * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory */ int __init fdt_scan_reserved_mem(void) { int node, child; + int dynamic_nodes_cnt =3D 0; + int dynamic_nodes[MAX_RESERVED_REGIONS]; const void *fdt =3D initial_boot_params; =20 node =3D fdt_path_offset(fdt, "/reserved-memory"); @@ -192,8 +255,24 @@ int __init fdt_scan_reserved_mem(void) uname =3D fdt_get_name(fdt, child, NULL); =20 err =3D __reserved_mem_reserve_reg(child, uname); - if (err =3D=3D -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) - fdt_reserved_mem_save_node(child, uname, 0, 0); + /* + * Save the nodes for the dynamically-placed regions + * into an array which will be used for allocation right + * after all the statically-placed regions are reserved + * or marked as no-map. This is done to avoid dynamically + * allocating from one of the statically-placed regions. + */ + if (err =3D=3D -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) { + dynamic_nodes[dynamic_nodes_cnt] =3D child; + dynamic_nodes_cnt++; + } + } + for (int i =3D 0; i < dynamic_nodes_cnt; i++) { + const char *uname; + + child =3D dynamic_nodes[i]; + uname =3D fdt_get_name(fdt, child, NULL); + __reserved_mem_alloc_size(child, uname); } return 0; } @@ -253,8 +332,7 @@ static int __init __reserved_mem_alloc_in_range(phys_ad= dr_t size, * __reserved_mem_alloc_size() - allocate reserved memory described by * 'size', 'alignment' and 'alloc-ranges' properties. */ -static int __init __reserved_mem_alloc_size(unsigned long node, - const char *uname, phys_addr_t *res_base, phys_addr_t *res_size) +static int __init __reserved_mem_alloc_size(unsigned long node, const char= *uname) { int t_len =3D (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32); phys_addr_t start =3D 0, end =3D 0; @@ -334,9 +412,8 @@ static int __init __reserved_mem_alloc_size(unsigned lo= ng node, return -ENOMEM; } =20 - *res_base =3D base; - *res_size =3D size; - + /* Save region in the reserved_mem array */ + fdt_reserved_mem_save_node(node, uname, base, size); return 0; } =20 @@ -425,48 +502,37 @@ static void __init __rmem_check_for_overlap(void) } =20 /** - * fdt_init_reserved_mem() - allocate and init all saved reserved memory r= egions + * fdt_init_reserved_mem_node() - Initialize a reserved memory region + * @rmem: reserved_mem struct of the memory region to be initialized. + * + * This function is used to call the region specific initialization + * function for a reserved memory region. */ -void __init fdt_init_reserved_mem(void) +static void __init fdt_init_reserved_mem_node(struct reserved_mem *rmem) { - int i; - - /* check for overlapping reserved regions */ - __rmem_check_for_overlap(); - - for (i =3D 0; i < reserved_mem_count; i++) { - struct reserved_mem *rmem =3D &reserved_mem[i]; - unsigned long node =3D rmem->fdt_node; - int err =3D 0; - bool nomap; + unsigned long node =3D rmem->fdt_node; + int err =3D 0; + bool nomap; =20 - nomap =3D of_get_flat_dt_prop(node, "no-map", NULL) !=3D NULL; + nomap =3D of_get_flat_dt_prop(node, "no-map", NULL) !=3D NULL; =20 - if (rmem->size =3D=3D 0) - err =3D __reserved_mem_alloc_size(node, rmem->name, - &rmem->base, &rmem->size); - if (err =3D=3D 0) { - err =3D __reserved_mem_init_node(rmem); - if (err !=3D 0 && err !=3D -ENOENT) { - pr_info("node %s compatible matching fail\n", - rmem->name); - if (nomap) - memblock_clear_nomap(rmem->base, rmem->size); - else - memblock_phys_free(rmem->base, - rmem->size); - } else { - phys_addr_t end =3D rmem->base + rmem->size - 1; - bool reusable =3D - (of_get_flat_dt_prop(node, "reusable", NULL)) !=3D NULL; - - pr_info("%pa..%pa (%lu KiB) %s %s %s\n", - &rmem->base, &end, (unsigned long)(rmem->size / SZ_1K), - nomap ? "nomap" : "map", - reusable ? "reusable" : "non-reusable", - rmem->name ? rmem->name : "unknown"); - } - } + err =3D __reserved_mem_init_node(rmem); + if (err !=3D 0 && err !=3D -ENOENT) { + pr_info("node %s compatible matching fail\n", rmem->name); + if (nomap) + memblock_clear_nomap(rmem->base, rmem->size); + else + memblock_phys_free(rmem->base, rmem->size); + } else { + phys_addr_t end =3D rmem->base + rmem->size - 1; + bool reusable =3D + (of_get_flat_dt_prop(node, "reusable", NULL)) !=3D NULL; + + pr_info("%pa..%pa (%lu KiB) %s %s %s\n", + &rmem->base, &end, (unsigned long)(rmem->size / SZ_1K), + nomap ? "nomap" : "map", + reusable ? "reusable" : "non-reusable", + rmem->name ? rmem->name : "unknown"); } } =20 --=20 2.34.1 From nobody Tue Feb 10 03:50:37 2026 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 663833FB8B; Fri, 6 Sep 2024 18:55:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725648953; cv=none; b=GQOJsGKdgMwFp2VsqarUDyiBW2y7jtqmmiZW+lhMJYpQCTLHLBaQ1MSUwcwNAeHddZodiMzEEZbY5Qc6HEltm2a1KDs7zjD0zWpTbJ8fyxaLOsEjDXDday/EK437T0UCqkyld+6D7Ub3hAL8bKJIDk/c200VNCjfd1OX/bgmow4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725648953; c=relaxed/simple; bh=GK61cQ3mFSEWeMf+t1FMxrAGCvM8O5dxwMZJAPMq+tc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=j5W8Y5kqgY7fF1UgnJDNp7OpuOieEmcxzv4tzT4Hp5bGSm7w4wFhD+3bmOpeysP5plQZwOJe84d3OzGBro/RYLByWjQahLxYoZuNhoZJPq0+2WLdcVcXeFH0Ekh/fhzGdQI8+rwCCf5RjxK0njEUJcOJdrLStwfmn0fmSy+JJpM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com; spf=pass smtp.mailfrom=quicinc.com; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b=I/00Tyhd; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=quicinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="I/00Tyhd" Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48690soJ027910; Fri, 6 Sep 2024 18:55:32 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= 61KLYYbFXr/uwE+fC9yma3isXx1jdUM/pnhUeWSDiWs=; b=I/00TyhdZyOpXUBU ohkdmOApk0Q5/A/6ErKZ2D9cpgEccC6wHjwjC2yyWM7GCZDqQpiJjqCCdmQcIqt7 WuFxsyL7IxWpWhQh1zfjpf2pjuOsORuY+B9BK0kjH+qsV2v9ANDS0+Sy4Urfzj9c Wwp7bT/l6yXkOuiuXe7Ulkx6I4I0MVeyuyp3JuktzHlw/vN0MeW/TLPB8lJrB2Yk FBpdwxEg1JrUoqhO4fnTfM0u4anzgiWGGc4maRrlHeFV2loFQOND4j6k562OkrcL itnYZ7NkB9bl4SKEHLhNrhAipHdBHmKumN9ZXxiR+1UL+UewDwPz+O+/C1Dk4JsY 4IidZg== Received: from nalasppmta04.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 41fhx1u7fp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 06 Sep 2024 18:55:32 +0000 (GMT) Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA04.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 486ItVjh032175 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 6 Sep 2024 18:55:31 GMT Received: from hu-obabatun-lv.qualcomm.com (10.49.16.6) by nalasex01b.na.qualcomm.com (10.47.209.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Fri, 6 Sep 2024 11:55:27 -0700 From: Oreoluwa Babatunde To: CC: , , , , , , , , , , , , , , Subject: [PATCH v9 2/2] of: reserved_mem: Add code to dynamically allocate reserved_mem array Date: Fri, 6 Sep 2024 11:54:00 -0700 Message-ID: <20240906185400.3244416-3-quic_obabatun@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240906185400.3244416-1-quic_obabatun@quicinc.com> References: <20240906185400.3244416-1-quic_obabatun@quicinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: nalasex01c.na.qualcomm.com (10.47.97.35) To nalasex01b.na.qualcomm.com (10.47.209.197) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: hEHbp_akeNHfnCY7zMRoPk6JiPct4g2d X-Proofpoint-GUID: hEHbp_akeNHfnCY7zMRoPk6JiPct4g2d X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-06_04,2024-09-06_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 bulkscore=0 clxscore=1015 impostorscore=0 suspectscore=0 mlxlogscore=999 phishscore=0 lowpriorityscore=0 spamscore=0 priorityscore=1501 adultscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2408220000 definitions=main-2409060137 Content-Type: text/plain; charset="utf-8" The reserved_mem array is statically allocated with a size of MAX_RESERVED_REGIONS(64). Therefore, if the number of reserved_mem regions exceeds this size, there will not be enough space to store all the data. Hence, extend the use of the static array by introducing a dynamically allocated array based on the number of reserved memory regions specified in the DT. On architectures such as arm64, memblock allocated memory is not writable until after the page tables have been setup. Hence, the dynamic allocation of the reserved_mem array will need to be done only after the page tables have been setup. As a result, a temporary static array is still needed in the initial stages to store the information of the dynamically-placed reserved memory regions because the start address is selected only at run-time and is not stored anywhere else. It is not possible to wait until the reserved_mem array is allocated because this is done after the page tables are setup and the reserved memory regions need to be initialized before then. After the reserved_mem array is allocated, all entries from the static array is copied over to the new array, and the rest of the information for the statically-placed reserved memory regions are read in from the DT and stored in the new array as well. Once the init process is completed, the temporary static array is released back to the system because it is no longer needed. This is achieved by marking it as __initdata. Signed-off-by: Oreoluwa Babatunde --- drivers/of/of_reserved_mem.c | 63 +++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c index 2011174211f9..45517b9e57b1 100644 --- a/drivers/of/of_reserved_mem.c +++ b/drivers/of/of_reserved_mem.c @@ -27,7 +27,9 @@ =20 #include "of_private.h" =20 -static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS]; +static struct reserved_mem reserved_mem_array[MAX_RESERVED_REGIONS] __init= data; +static struct reserved_mem *reserved_mem __refdata =3D reserved_mem_array; +static int total_reserved_mem_cnt =3D MAX_RESERVED_REGIONS; static int reserved_mem_count; =20 static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t siz= e, @@ -55,6 +57,50 @@ static int __init early_init_dt_alloc_reserved_memory_ar= ch(phys_addr_t size, return err; } =20 +/* + * alloc_reserved_mem_array() - allocate memory for the reserved_mem + * array using memblock + * + * This function is used to allocate memory for the reserved_mem + * array according to the total number of reserved memory regions + * defined in the DT. + * After the new array is allocated, the information stored in + * the initial static array is copied over to this new array and + * the new array is used from this point on. + */ +static void __init alloc_reserved_mem_array(void) +{ + struct reserved_mem *new_array; + size_t alloc_size, copy_size, memset_size; + + alloc_size =3D array_size(total_reserved_mem_cnt, sizeof(*new_array)); + if (alloc_size =3D=3D SIZE_MAX) { + pr_err("Failed to allocate memory for reserved_mem array with err: %d", = -EOVERFLOW); + return; + } + + new_array =3D memblock_alloc(alloc_size, SMP_CACHE_BYTES); + if (!new_array) { + pr_err("Failed to allocate memory for reserved_mem array with err: %d", = -ENOMEM); + return; + } + + copy_size =3D array_size(reserved_mem_count, sizeof(*new_array)); + if (copy_size =3D=3D SIZE_MAX) { + memblock_free(new_array, alloc_size); + total_reserved_mem_cnt =3D MAX_RESERVED_REGIONS; + pr_err("Failed to allocate memory for reserved_mem array with err: %d", = -EOVERFLOW); + return; + } + + memset_size =3D alloc_size - copy_size; + + memcpy(new_array, reserved_mem, copy_size); + memset(new_array + reserved_mem_count, 0, memset_size); + + reserved_mem =3D new_array; +} + static void __init fdt_init_reserved_mem_node(struct reserved_mem *rmem); /* * fdt_reserved_mem_save_node() - save fdt node for second pass initializa= tion @@ -64,7 +110,7 @@ static void __init fdt_reserved_mem_save_node(unsigned l= ong node, const char *un { struct reserved_mem *rmem =3D &reserved_mem[reserved_mem_count]; =20 - if (reserved_mem_count =3D=3D ARRAY_SIZE(reserved_mem)) { + if (reserved_mem_count =3D=3D total_reserved_mem_cnt) { pr_err("not enough space for all defined regions.\n"); return; } @@ -193,6 +239,9 @@ void __init fdt_scan_reserved_mem_reg_nodes(void) return; } =20 + /* Attempt dynamic allocation of a new reserved_mem array */ + alloc_reserved_mem_array(); + if (__reserved_mem_check_root(node)) { pr_err("Reserved memory: unsupported node format, ignoring\n"); return; @@ -232,7 +281,7 @@ static int __init __reserved_mem_alloc_size(unsigned lo= ng node, const char *unam int __init fdt_scan_reserved_mem(void) { int node, child; - int dynamic_nodes_cnt =3D 0; + int dynamic_nodes_cnt =3D 0, count =3D 0; int dynamic_nodes[MAX_RESERVED_REGIONS]; const void *fdt =3D initial_boot_params; =20 @@ -255,6 +304,8 @@ int __init fdt_scan_reserved_mem(void) uname =3D fdt_get_name(fdt, child, NULL); =20 err =3D __reserved_mem_reserve_reg(child, uname); + if (!err) + count++; /* * Save the nodes for the dynamically-placed regions * into an array which will be used for allocation right @@ -269,11 +320,15 @@ int __init fdt_scan_reserved_mem(void) } for (int i =3D 0; i < dynamic_nodes_cnt; i++) { const char *uname; + int err; =20 child =3D dynamic_nodes[i]; uname =3D fdt_get_name(fdt, child, NULL); - __reserved_mem_alloc_size(child, uname); + err =3D __reserved_mem_alloc_size(child, uname); + if (!err) + count++; } + total_reserved_mem_cnt =3D count; return 0; } =20 --=20 2.34.1