From nobody Tue Feb 10 11:56:07 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 703AC49630; Fri, 9 Aug 2024 18:49:02 +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=1723229344; cv=none; b=hKKXLwgtLpMFk4r0zfib+dBtME0WBNPOPRRB9NZ0wdfjWmFTF8Ha/GahK+PJRHJ4w44lIOLnmEfWFDugocg/TdBd+25hOrHeeHFpSL6ABopN3ftDkgm8di1TqrR54+L+IwQc7CGf8SyGmM+TwB2mA08fyWmYqZqMeyNkelHalHQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723229344; c=relaxed/simple; bh=G0s1rj14AIRtP/emmXHl8MDxufjExKfWIr6diFaipi8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FlZ+LBWKC3BHC8lLMe7onFzoaXWBg4NboB6WYjve9hx+maOcvRo0E6kRqBBf5Z+mefAEdicbnrYMSZJKRxZKle2V5YcUiIx38fmM8GqTXh/aPDY3YL2gwUuAs+ushuzKKEU557qu/ymuCjdeArm3O/cpXXchC+uFjg83fHrxgUo= 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=VmDCGgss; 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="VmDCGgss" Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 479AI960003292; Fri, 9 Aug 2024 18:48:42 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= FnZiCsOE4iogMiFCRF4MNW0QwIx+k/BFS0PtP3n+TIE=; b=VmDCGgssBD0zenne Hs3jjme7T9YIg290WaHsLr1OhCwqYIVjtQDqqM3waEFpze0Al6/ffIebHl4fUlyX T7q1CKU3t4p0uerp0JK5jTB1V37qP+tK0pz8Oj7nplQraFrgvO+EVcscAio2o+/v huu6EPXI7/yJShatvoFIwHSaufgFT7Iz5cA+4NIN9elHHzZTsRv3KjS7ePUn1Rdo t9Chzc0ndU9VsehgK1jZ15GP2m2nbu0Ea5CCUJfi08KQxCyH9XQd0I0REvHiMAUa URMUjttOke/0c7PeRn3Q/YgNJNFhNcP7M/7UH4hFnG7hfPHYpKAVsNuiZ71WC5F4 UJnm7w== Received: from nalasppmta02.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 40vue3vkf6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 09 Aug 2024 18:48:42 +0000 (GMT) Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA02.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 479Imf6u000644 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 9 Aug 2024 18:48:41 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, 9 Aug 2024 11:48:38 -0700 From: Oreoluwa Babatunde To: , CC: , , , , , , , , , , , Oreoluwa Babatunde Subject: [PATCH v7 2/2] of: reserved_mem: Add code to dynamically allocate reserved_mem array Date: Fri, 9 Aug 2024 11:48:14 -0700 Message-ID: <20240809184814.2703050-3-quic_obabatun@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240809184814.2703050-1-quic_obabatun@quicinc.com> References: <20240809184814.2703050-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: nalasex01a.na.qualcomm.com (10.47.209.196) 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-GUID: oh29NJunGIW4-wDlBPdCXXFH8dAfW87Z X-Proofpoint-ORIG-GUID: oh29NJunGIW4-wDlBPdCXXFH8dAfW87Z X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-09_15,2024-08-07_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 suspectscore=0 lowpriorityscore=0 priorityscore=1501 phishscore=0 mlxlogscore=999 malwarescore=0 impostorscore=0 bulkscore=0 mlxscore=0 spamscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2407110000 definitions=main-2408090135 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 | 68 +++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c index b52690e554f0..d90972cb5949 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; +} + /* * fdt_reserved_mem_save_node() - save fdt node for second pass initializa= tion */ @@ -63,7 +109,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; } @@ -199,6 +245,13 @@ void __init fdt_scan_reserved_mem_reg_nodes(void) return; } =20 + /* + * Allocate the exact size needed for the reserved_mem array and + * copy all the contents from the previous array if allocation + * is successful. + */ + alloc_reserved_mem_array(); + fdt_for_each_subnode(child, fdt, node) { const char *uname; =20 @@ -233,7 +286,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 @@ -256,6 +309,9 @@ 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 @@ -270,11 +326,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