From patchwork Mon Dec 4 18:54:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oreoluwa Babatunde X-Patchwork-Id: 173527 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp2968726vqy; Mon, 4 Dec 2023 10:57:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IECUc6WCy47g3JhuyWD5Xe+PbquINm6d8XEeAr5yWtOrjTsH0kj8LcbrRsmoE3uC1AZGbpQ X-Received: by 2002:a05:6a00:1c8b:b0:6cb:4d36:6f9c with SMTP id y11-20020a056a001c8b00b006cb4d366f9cmr43550pfw.11.1701716248924; Mon, 04 Dec 2023 10:57:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701716248; cv=none; d=google.com; s=arc-20160816; b=boXt3W7CPVOxA2DcdsU8HA83lMp3kJBnlnaCBBRBAA2M6kfdnW1Xs6V+NzbvfAkpQ7 jILo+8TuPhXhsgqb9OUs16SO5y0F5nW1hnbmZVKQ1XlmPRjBVLAlJ8w1CoXdL7B3AkW4 +kO6fqtzFgryNVH/ye6mV1ncYNNxui2ngcV3KUfm+hf08n5ethLdzpBkJcOsWAYQGrVR 1dW6wJqWydYNTyhtUVwsfkkj+HM0W20vJP3aYLj+o9GXcHBi/mZNgVPNUBFd95Ad8aT4 TreRf6WGQsYndoWXkOMKpIrnBCPtoF8XP8xmo6Grqg7ankz/nWNWyKogi6bASnteIVp7 Cp6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=tS6apyrTUM2UpfFkK9FWe2t5HNwAaHbTSnhZYJs1TBE=; fh=AfACOzYsAw2FP5vLurF8iL1ue9vq0GHYQcZwZUotW7Q=; b=WxSng+h0KfI5vF8T5g86o5DKO794d/AAW3f4yQjXgxxgoG+8X7MYijCTv1Mk7AY17c ADi7veyDrAJj022zCRSEd2OkrCXs5FSif20SX0uXCCialZPfivLa0RQTY0xVToOcXy+K b74YIaKeJbzjRydpkZUEigR4nLYIyCNFCHxQcWFFO3ZxMuSwKf7XW6owGdQyNDJ8+JG6 B1QMdYAlYGz+CN17qz4+KTfkef8Mz6Mo4OJ1Lzk5KVlI0f3ejY6MTk8uIZDNO80oTa6O jF5ok2jaLTeMyVg9l564vRCl7MIWtpAmu4erdE99mavey9GvCPta97aAJNoqBa4Cbd7e PA7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=SZs6VYqu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id j7-20020a635947000000b005bddb7249e0si2806104pgm.313.2023.12.04.10.57.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 10:57:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=SZs6VYqu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 3841B8051AAF; Mon, 4 Dec 2023 10:57:22 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235523AbjLDS5F (ORCPT + 99 others); Mon, 4 Dec 2023 13:57:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231516AbjLDS4k (ORCPT ); Mon, 4 Dec 2023 13:56:40 -0500 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EA18107; Mon, 4 Dec 2023 10:56:46 -0800 (PST) Received: from pps.filterd (m0279864.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3B4FjgwX024184; Mon, 4 Dec 2023 18:54:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=qcppdkim1; bh=tS6apyrTUM2UpfFkK9FWe2t5HNwAaHbTSnhZYJs1TBE=; b=SZs6VYquS+RgATEpjCVdCyV1F1jdhnjNt7iOWnOUmlKp8IF7G8eW5bWL6q159USkOzvr RiOOhbh7Hc8zg1yTc5ZSDpX+u8mFewir0492Q1ZkVByw5W57ZnRwYlj/3FB98b7d0Chf 2L3Xxn6hC6YqKeB5Fz9B05ppZywHG2nYeTJEBkOwWy/6iqsRrn+/c/Yu9ho5MEagyIH5 0geCpyNo7UE4DQd1UpcC7++NrYrYHOP5KwgqDqOX8IPA0p5QBHtb4Dqr37nYkqWwvOTw gR319Efc3JtUkawMR5uvDlj8nfkFpCxiucqMyeRA9jjtHzHIGRnWQu1CdQGjKAGJKCo1 /A== Received: from nalasppmta02.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3usgfmrpnc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 04 Dec 2023 18:54:49 +0000 Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA02.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 3B4Isme4013393 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 4 Dec 2023 18:54:48 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.1118.40; Mon, 4 Dec 2023 10:54:45 -0800 From: Oreoluwa Babatunde To: , , , , , , , , , , , , , , , , , , CC: , , , , , Oreoluwa Babatunde Subject: [RFC PATCH v2 5/6] of: reserved_mem: Add code to dynamically allocate reserved_mem array Date: Mon, 4 Dec 2023 10:54:08 -0800 Message-ID: <20231204185409.19615-6-quic_obabatun@quicinc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231204185409.19615-1-quic_obabatun@quicinc.com> References: <20231204185409.19615-1-quic_obabatun@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] 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: 8xbm48r6qn8thC0re3s_UqANDMySnxAE X-Proofpoint-ORIG-GUID: 8xbm48r6qn8thC0re3s_UqANDMySnxAE X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-12-04_18,2023-12-04_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 lowpriorityscore=0 priorityscore=1501 bulkscore=0 mlxscore=0 phishscore=0 impostorscore=0 clxscore=1015 spamscore=0 mlxlogscore=684 malwarescore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311060000 definitions=main-2312040146 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 04 Dec 2023 10:57:22 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784378817454285965 X-GMAIL-MSGID: 1784378817454285965 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. Therefore, 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. Before paging_init() runs, the static array is used to store the dynamically-placed regions. After paging_init(), memory is dynamically allocated for the reserved_mem array, and all entries from the static array is copied over to the new array, and all other statically-placed regions are added in as well. Signed-off-by: Oreoluwa Babatunde --- drivers/of/fdt.c | 13 +++++++--- drivers/of/of_private.h | 1 + drivers/of/of_reserved_mem.c | 48 +++++++++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index 12769dd53c34..2f1eabbd6869 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -563,8 +563,8 @@ static int __init __reserved_mem_check_root(unsigned long node) */ static int __init fdt_scan_reserved_mem(void) { - int node, child; - int dynamic_nodes_cnt = 0; + int node, child, err = 0; + int dynamic_nodes_cnt = 0, count = 0; int dynamic_nodes[MAX_RESERVED_REGIONS]; const void *fdt = initial_boot_params; @@ -579,7 +579,6 @@ static int __init fdt_scan_reserved_mem(void) fdt_for_each_subnode(child, fdt, node) { const char *uname; - int err; if (!of_fdt_device_is_available(fdt, child)) continue; @@ -587,6 +586,8 @@ static int __init fdt_scan_reserved_mem(void) uname = fdt_get_name(fdt, child, NULL); err = __reserved_mem_reserve_reg(child, uname); + if (!err) + count++; if (err == -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) { dynamic_nodes[dynamic_nodes_cnt] = child; @@ -600,8 +601,12 @@ static int __init fdt_scan_reserved_mem(void) 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++; } + update_reserved_mem_max_cnt(count); + return 0; } diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h index 575e2b4119e0..ef56b2ea185c 100644 --- a/drivers/of/of_private.h +++ b/drivers/of/of_private.h @@ -179,5 +179,6 @@ static inline struct device_node *__of_get_dma_parent(const struct device_node * void init_reserved_mem(void); void dt_reserved_mem_save_node(struct device_node *node, const char *uname, phys_addr_t base, phys_addr_t size); +void update_reserved_mem_max_cnt(int max_count); #endif /* _LINUX_OF_PRIVATE_H */ diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c index 2ef9edcb8c93..01cd6a571dc2 100644 --- a/drivers/of/of_reserved_mem.c +++ b/drivers/of/of_reserved_mem.c @@ -26,7 +26,9 @@ #include "of_private.h" -static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS]; +static struct reserved_mem reserved_mem_array[MAX_RESERVED_REGIONS]; +static struct reserved_mem *reserved_mem = 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, @@ -54,6 +56,42 @@ static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, return err; } +void __init update_reserved_mem_max_cnt(int max_count) +{ + total_reserved_mem_cnt = max_count; +} + +static int 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) + return -1; + + new_array = memblock_alloc(alloc_size, SMP_CACHE_BYTES); + if (!new_array) + return -ENOMEM; + + copy_size = array_size(reserved_mem_count, sizeof(*new_array)); + if (copy_size == SIZE_MAX) + goto overlow_err; + + 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; + return 0; + +overlow_err: + memblock_free(new_array, alloc_size); + total_reserved_mem_cnt = MAX_RESERVED_REGIONS; + return -1; +} + /* * dt_reserved_mem_save_node() - save dt node for second pass initialization */ @@ -62,7 +100,7 @@ void __init dt_reserved_mem_save_node(struct device_node *node, const char *unam { 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; } @@ -346,7 +384,11 @@ static void __init __rmem_check_for_overlap(void) */ void __init init_reserved_mem(void) { - int i; + int i, ret; + + ret = alloc_reserved_mem_array(); + if (ret) + pr_err("Failed to allocate memory for reserved_mem array with err: %d", ret); scan_reserved_mem_reg_nodes();