From patchwork Mon Dec 4 04:13:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oreoluwa Babatunde X-Patchwork-Id: 173065 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp2542077vqy; Sun, 3 Dec 2023 20:15:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IFrUdd9LrsuoVr0PE3RW8nv8qrfdKmw/mex1ZS7zoK3VoD3tqhaQRD0ZkxsqLEjLC4H0zjK X-Received: by 2002:a05:6a20:daa5:b0:18f:97c:8a22 with SMTP id iy37-20020a056a20daa500b0018f097c8a22mr5034438pzb.77.1701663344031; Sun, 03 Dec 2023 20:15:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701663344; cv=none; d=google.com; s=arc-20160816; b=N/WKPGcWXsxYZQivsMdV2CGr5NA/PqLmV2c4VTp0scS4eu8u79/R1BUqOyLnx9VEEj /gKdnhCf+LIxAi3bUY6buF9njtTUhvMnhrhuQDLEhKhCWZUyZpFURe+fz1GwN34q7f67 bYqN8+0R4M66CxajSqyd/6CihvAJZC/nI7p0lb1VXLiHz63RCkUOLXJe9xMxnUCy26A1 PGRloVaa7+gZkxVSGPKCoZickFLSgRf1+1CCIyHO3VlWlz0KhSXAPPdD0z34jTjP9EU4 +DTekvIIT3bOTSg6WkGPHH8ygdAVz1BFR1E5BrYVj/iYqJlpmpJwKIJBP2+ppAi0X48p Ezfg== 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=JCb6YaKmbUpp/1fFgtqOrlyOUwhehZd1rxGbEgdh7vM=; b=S2h+3naYqYuMNHP/tUopKrUttJzvzl32tZepEZHiGBFtp2r53T0qUZgJS6/XJieeZe Q+9W0uvXfn+fBtGezhzm67XYtArnjKMQbk2vYJ6wtxSnuoIfNW32pg+yisZZCaxBpbXI Pa7pclI+xiTzSFdXicdGQg9xGeEQNfzs7ikP3WLdI+k/pgZISp4LELD6mJoHimGDdR6o BBo+sdNLo8BQuxDazluafF1FPEP4mF72R2CbJFXw98O4XMSzv0or1pKd6DmxvuknRCw9 TVTuGfl6hlxWxuAMKPH8j4JEKyOFg1hbB5T3KlEJzS89DockoXmlKw4PDyXLKHAfWM5a D8yQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=VZQJgduZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id l13-20020a170903120d00b001cfb316a183si7342328plh.141.2023.12.03.20.15.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Dec 2023 20:15:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=VZQJgduZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id A9C8D807931D; Sun, 3 Dec 2023 20:15:16 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234457AbjLDEO1 (ORCPT + 99 others); Sun, 3 Dec 2023 23:14:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234444AbjLDEOY (ORCPT ); Sun, 3 Dec 2023 23:14:24 -0500 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CAD08D5; Sun, 3 Dec 2023 20:14:30 -0800 (PST) Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3B41lTrQ021618; Mon, 4 Dec 2023 04:14:07 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=VZQJgduZ84HbASKD4Pl1j4WhYG9QKpmmn3OZvqLGxZr9/5yPLwsFlUr3rzdR085ClezV 8wkejFBunVRQJVnooOgLeb6lk89HxlV4DO8d5HaQuC48fbOpVx0SOCNIi3zdsmqiU0px AtxVKjQfo0qTll4d9mcO0OFuLW78vs0q017x6TZWuxofWP/QM/Ubwu7S3bcZkmhU5eKs dWlS6BziwCG1XZhMqy20o2IR2HBt7KhCQkz+pb5PMn4IKK2mLbq2VYrr8IOtpt/LOgrs TeCLH1yA/nRuDheVCqEIKYxQITYtno3sWoZlKwbpxWpfU/Nnu9XpZslIDBNHb+pZULUL iw== Received: from nalasppmta03.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3uqsxw357w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 04 Dec 2023 04:14:06 +0000 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 3B44E55B026344 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 4 Dec 2023 04:14:05 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; Sun, 3 Dec 2023 20:14:02 -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: Sun, 3 Dec 2023 20:13:38 -0800 Message-ID: <20231204041339.9902-6-quic_obabatun@quicinc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231204041339.9902-1-quic_obabatun@quicinc.com> References: <20231204041339.9902-1-quic_obabatun@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) 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: A8SsO6wWn22noIW1LxRR94xcAmw0O5LG X-Proofpoint-GUID: A8SsO6wWn22noIW1LxRR94xcAmw0O5LG 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_01,2023-11-30_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 suspectscore=0 mlxscore=0 bulkscore=0 spamscore=0 clxscore=1015 priorityscore=1501 mlxlogscore=633 malwarescore=0 impostorscore=0 adultscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311060000 definitions=main-2312040029 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 fry.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 (fry.vger.email [0.0.0.0]); Sun, 03 Dec 2023 20:15:16 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784323342442301363 X-GMAIL-MSGID: 1784323342442301363 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();