Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp278853lqb; Tue, 28 May 2024 15:37:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVUtxVTqiFoLFJHPjOXSV6rTDLiZhXpN1QVlsebApvn47omlls5WCx4YQLvTLVG9kmxg2e/FHQPDcROzjkA6VInyVtBhW0rJsdXUmUp4g== X-Google-Smtp-Source: AGHT+IFqg75PQxwK11KbT5KFN7m1wB1rs+TN0NJx5m8ov2SXXOwsEV6MiqW5d62/vuHiHld0uZlU X-Received: by 2002:a05:6a20:5b03:b0:1b0:1bae:6a9c with SMTP id adf61e73a8af0-1b212d5f736mr11883876637.20.1716935860821; Tue, 28 May 2024 15:37:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716935860; cv=pass; d=google.com; s=arc-20160816; b=aD2rzX4GHVtkDI5jy69ScIfiZBywjpBuL4V3smx3uYj/etn3DOajL5y/Cej4MkjdKD J7WRsMiB/JXf1Eau6BSpwOxGcBoXM7i7Tmb+WVmnqEdWDK4Y35jgBl9GnDNWfWpN/JwH lbbAMtxfQRSZT+uUPBChbnc/wBt/wzqhUkyQy3HPojx1rLiIUdojIv8yzYxRUlT/OFw5 u/QGoWSGY5i/1fdUzMovbOEGwF1fyplZ350mXAxtkpTe5ykO7xRolzFplm1JkiHXDDRA 6bec7+X/Tg1cwfRMeQub/N6hFhZGfMmQJj860jrwA/StZsmB6d6v6k8zU0r6oZQw23nT kjiQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=S4ptlVoiRxy1U8psoznXLlkgymQEYIzekZdybqiOGko=; fh=5q7Lfq2/iqa6Nge8xgjPtA1A5dj8UKBzT9nsh98vriI=; b=mWfMBN4OOI4DFzA2vpUjqmbh//kELkR8qqhCay6zZB0LbfWK6c7HgJd2CFmgpFlm8+ 0GHl6Tb6x2WcLYPEHpb5mcEVSxPvb9wbjTVNVyRi6W9rXlF2clsCMCxDNUliAkFsPhdF 0XWjLfo3qNB5NL0dC6FhQr5owsW5PoXLN9KNd8ra819xKDv++5tCtr8UEqGaK3v0w7QB BPMaTxpZ+LTEGbNDlKHQw8uU/95eS4+puf+F7AIWwchseBfjQy5YWEMjwF80V9YkpLJw kSD6w+6+H6GuK2x/CH/2rnjvaoFZ9rPkqaHp2XcebeeRI7kx/1beMcUN6GAd3fIq72Uq 1Qew==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=o3X1zt02; arc=pass (i=1 spf=pass spfdomain=quicinc.com dkim=pass dkdomain=quicinc.com dmarc=pass fromdomain=quicinc.com); spf=pass (google.com: domain of linux-kernel+bounces-193188-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-193188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d2e1a72fcca58-702083f85a8si130501b3a.4.2024.05.28.15.37.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 15:37:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-193188-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=o3X1zt02; arc=pass (i=1 spf=pass spfdomain=quicinc.com dkim=pass dkdomain=quicinc.com dmarc=pass fromdomain=quicinc.com); spf=pass (google.com: domain of linux-kernel+bounces-193188-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-193188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 659EE283560 for ; Tue, 28 May 2024 22:37:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4506E13E8A5; Tue, 28 May 2024 22:37:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="o3X1zt02" Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.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 DE2F913DDD2; Tue, 28 May 2024 22:37:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716935844; cv=none; b=FhVKDVv9A51gqNwPBZQSjwYw0kYCtFtuMoHn9yi1oI0MOavE450QJ07XrGmXGpjt8+lSVZ4NXdLQwFRzYGMTTCmpK/ALf20v8XTjEC9Q5b+sPN9mP403A/mU86A0PKuKPvwaKI6nG8mDE/DwpHjaNWeMs8Wc+tXG6O75MPYzPUg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716935844; c=relaxed/simple; bh=UJv1CD8WbiI13h2HLlJ1I59Yi1cp3y58PBrUVCgUOgY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FhuV4+XIioCkajBmTIu9L9jiCwHE1QSD+DzKZ2dpdPC5eqK6tjCpaimG7eV2cVmlcw3tkyY/aX2C7dpIijEQG/ymJN1XuLpgJDKRb3b5wXTJD4fpr2BHKuOhR4HMwpLuer9Oyws19Y8tY1agHJpHqtCHtYRmheUI74bqy1IyAcM= 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=o3X1zt02; arc=none smtp.client-ip=205.220.168.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 Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 44SAw9nS000379; Tue, 28 May 2024 22:37:08 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= S4ptlVoiRxy1U8psoznXLlkgymQEYIzekZdybqiOGko=; b=o3X1zt0255gLcp4s u0oxyAOQVUx1n84btXbWkqgVBUY15vysu0A8+TwKXrChxfgVKs/klKMwQsOfAlZa 0bV3wuuGyPpIFF2CshMN950Sk022bfORj24xIgVEzOACpPkEWzwUz/xn7NIrrUrz PzN4GS1jE/SWbTLv6OHjvW42Smx0pIPDzIgb1O9mK4Qga2sZTF4s3B4dcpZMSZz2 9xV2d28wZO2lEn7sRyxDk2+EKaxdsspGGgixG2EgTJgjG8Rl5yvVzXl9U5lCemzS hVjJgMGUyHn668MINF3QX8Zl+WTsyTBsGvkBmoEDlwOLD8gLU+iNCYSvJnZY31Yr mIP0VQ== Received: from nalasppmta03.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3yba2h7ftr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 28 May 2024 22:37:08 +0000 (GMT) Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA03.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 44SMb7WQ012676 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 28 May 2024 22:37:07 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; Tue, 28 May 2024 15:37:03 -0700 From: Oreoluwa Babatunde To: , , , , , , CC: , , , , Oreoluwa Babatunde Subject: [PATCH v6 2/4] of: reserved_mem: Add code to dynamically allocate reserved_mem array Date: Tue, 28 May 2024 15:36:48 -0700 Message-ID: <20240528223650.619532-3-quic_obabatun@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240528223650.619532-1-quic_obabatun@quicinc.com> References: <20240528223650.619532-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: 8bit Content-Type: text/plain 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: 2OzWrI-2HaUP-mlkaBa04kFh4qryhpOk X-Proofpoint-ORIG-GUID: 2OzWrI-2HaUP-mlkaBa04kFh4qryhpOk X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.12.28.16 definitions=2024-05-28_14,2024-05-28_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 phishscore=0 spamscore=0 malwarescore=0 bulkscore=0 mlxscore=0 impostorscore=0 priorityscore=1501 suspectscore=0 mlxlogscore=999 lowpriorityscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2405280168 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 | 58 +++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c index 78ffc2635f11..113d593ea031 100644 --- a/drivers/of/of_reserved_mem.c +++ b/drivers/of/of_reserved_mem.c @@ -27,7 +27,9 @@ #include "of_private.h" -static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS]; +static struct reserved_mem reserved_mem_array[MAX_RESERVED_REGIONS] __initdata; +static struct reserved_mem *reserved_mem __refdata = reserved_mem_array; +static int total_reserved_mem_cnt = MAX_RESERVED_REGIONS; static int reserved_mem_count; static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, @@ -55,6 +57,45 @@ static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, return err; } +/* + * 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 = array_size(total_reserved_mem_cnt, sizeof(*new_array)); + if (alloc_size == SIZE_MAX) + pr_err("Failed to allocate memory for reserved_mem array with err: %d", -EOVERFLOW); + + new_array = memblock_alloc(alloc_size, SMP_CACHE_BYTES); + if (!new_array) + pr_err("Failed to allocate memory for reserved_mem array with err: %d", -ENOMEM); + + copy_size = array_size(reserved_mem_count, sizeof(*new_array)); + if (copy_size == SIZE_MAX) { + memblock_free(new_array, alloc_size); + total_reserved_mem_cnt = MAX_RESERVED_REGIONS; + pr_err("Failed to allocate memory for reserved_mem array with err: %d", -EOVERFLOW); + } + + memset_size = alloc_size - copy_size; + + memcpy(new_array, reserved_mem, copy_size); + memset(new_array + reserved_mem_count, 0, memset_size); + + reserved_mem = new_array; +} + /* * fdt_reserved_mem_save_node() - save fdt node for second pass initialization */ @@ -63,7 +104,7 @@ static void __init fdt_reserved_mem_save_node(unsigned long node, const char *un { struct reserved_mem *rmem = &reserved_mem[reserved_mem_count]; - if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) { + if (reserved_mem_count == total_reserved_mem_cnt) { pr_err("not enough space for all defined regions.\n"); return; } @@ -220,7 +261,7 @@ static int __init __reserved_mem_alloc_size(unsigned long node, const char *unam int __init fdt_scan_reserved_mem(void) { int node, child; - int dynamic_nodes_cnt = 0; + int dynamic_nodes_cnt = 0, count = 0; int dynamic_nodes[MAX_RESERVED_REGIONS]; const void *fdt = initial_boot_params; @@ -243,6 +284,8 @@ int __init fdt_scan_reserved_mem(void) uname = fdt_get_name(fdt, child, NULL); err = __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 @@ -257,11 +300,16 @@ int __init fdt_scan_reserved_mem(void) } for (int i = 0; i < dynamic_nodes_cnt; i++) { const char *uname; + int err; child = dynamic_nodes[i]; uname = fdt_get_name(fdt, child, NULL); - __reserved_mem_alloc_size(child, uname); + + err = __reserved_mem_alloc_size(child, uname); + if (!err) + count++; } + total_reserved_mem_cnt = count++; return 0; } @@ -494,6 +542,8 @@ void __init fdt_init_reserved_mem(void) { int i; + alloc_reserved_mem_array(); + fdt_scan_reserved_mem_reg_nodes(); /* check for overlapping reserved regions */ -- 2.34.1