From patchwork Sun Mar 3 14:34:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hanyuan Zhao X-Patchwork-Id: 1907291 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.a=rsa-sha256 header.s=s201512 header.b=zlOmr5h1; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TnkvG1KSdz23qh for ; Mon, 4 Mar 2024 01:37:46 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8BBDC87F06; Sun, 3 Mar 2024 15:37:42 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.b="zlOmr5h1"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 660B787F0A; Sun, 3 Mar 2024 15:37:40 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, HELO_DYNAMIC_IPADDR,RDNS_DYNAMIC,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from out203-205-221-239.mail.qq.com (out203-205-221-239.mail.qq.com [203.205.221.239]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1DA5087DFC for ; Sun, 3 Mar 2024 15:37:33 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=hanyuan-z@qq.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1709476649; bh=xZeySlevRlUCF1ClBBMg3pQnA4Vl9NgCLalXGQs9BcA=; h=From:To:Cc:Subject:Date; b=zlOmr5h19f82adKX0MCpxw7htVF9vgMz0J1lYWmgx9yPnwNUyplRBs1whovLZ9QsE PMshVPvEyel0lpg/3Ghdnc7vS52JqdQLGzH6MVH5hueghnBEGViM4fBO6plfcFDf2b WCH6enIEohQP9DLdyBjcKZctkhbcMbvMkf0hXlao= Received: from localhost.localdomain ([183.172.216.136]) by newxmesmtplogicsvrszc5-0.qq.com (NewEsmtp) with SMTP id 95A9DA66; Sun, 03 Mar 2024 22:37:26 +0800 X-QQ-mid: xmsmtpt1709476646t2gg7qof7 Message-ID: X-QQ-XMAILINFO: OC704IrkdJt6z2Ib1fSBb1KSATyaJ75SUBp0LN8DTV5jlwZzagAXDpuYmxcBJC eS/F0EmhCsdrhE0qMqb/qESXBzAztyvMUGnMVAn08oFNVeeTZGUmDxSwcJqrgIEfAoR1p/2eeRIW t6QpUBxGGDc2Shr79NF31VRByj17ogDSid6lO/kD8Ypa/O7ZsBqH+VlQNkBQZu8yA4GIRPq9oOhD JkcthlAH2WR3tmDK7mTM3U8O1C7NGZP/Od17kKhvf0jVD8p4BMv3H/TZJW3x52tz8m49953GQ20O DVjmPvmu9FsnCq1Tc/XKYFUssW9UytTNxNrNYeBEMuo+l40PTbl0pTC8k6QNLbEJW28itAn+fwjn qS0bVzoqIzusCh3V4NhwG+3OIPgtL+GcQjVgYyrjWValAT0jYjlU7c/KzHxRr5fR1WB4ghlOFAN6 iko1wQuR0/4FmFwaZ0quj0Ofmspm3XeCV1JYxCgOTnMOnQd+VapZfwef9h9DM/FaHR/IFQEsSbYA z8Swp5oWPA/0kJ9V779bHURMQB8U3z9P7X9l3GnFapYMTWtYh7GDaDUOEj+q2bFynwJL76u7BZb6 77beptJ5M7QOXAcrmAhMcm65WWUQXXo2KUxxt2nctvPeaAjMEcmUWhdSScDUYqYsEWCigv77hWiE H04n/uDFEh8crX3zgY/wv+pfwQwOGY52cGxuze/jysXcX5OSDpaR/WagBAjhUHtlDauY+P5aqJIa Vq05Z9JO0a8GAMZ27nMsblPVIkXGoz8CjByXDLxP9CniBwa5QWeMZBm/LaZcxQ/WAWaaGu1KMwX+ TkRB622gpcJkjMVtmmHKBlBkRdckm/DPDGg07GMBOg/nYZYETxgpAsuDzT4Nk9xgXCUIWxPxZlW+ /8quANlHT6s8Js8gOgVWit+2HwHTscJEEe2RYAD02I6YZ9ZLKdzwjpnDJuVMn0TyT/9GsA0GZjys c9KKQeFZA/MHTmIcFEf9fgvQLNbpK7VE165AIaZwoSJK1ICoBQTG+lAEdViyaN X-QQ-XMRINFO: M/715EihBoGSf6IYSX1iLFg= From: Hanyuan Zhao To: u-boot@lists.denx.de Cc: Tom Rini , Simon Glass , Hanyuan Zhao Subject: [PATCH v2 1/2] cli: panic when failed to allocate memory for the history buffer Date: Sun, 3 Mar 2024 22:34:45 +0800 X-OQ-MSGID: <20240303143445.7030-1-hanyuan-z@qq.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean This commit simply modifies the history initialize function, replacing the return value by panic with reasons. The calling chains of hist_init don't have steps explicitly throwing or dealing with the ENOMEM error, and once the init fails, the whole system is died. Using panic here to provide error information instead. Signed-off-by: Hanyuan Zhao --- This is v2 of patch series cli: allow users to disable history if unused at all --- Changes v1 -> v2: - Remove the lines surrounded by macro CONFIG_CMD_HISTORY, since if the user says N to CMD_HISTORY, the whole C file won't be compiled, i.e. the history is disabled already. - Remove default y to CMD_HISTORY. Here we should follow the original one. --- common/cli_readline.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/cli_readline.c b/common/cli_readline.c index 2507be2295..99e7efdfe5 100644 --- a/common/cli_readline.c +++ b/common/cli_readline.c @@ -110,7 +110,7 @@ static int hist_init(void) hist = calloc(HIST_MAX, HIST_SIZE + 1); if (!hist) - return -ENOMEM; + panic("%s: calloc: out of memory!\n", __func__); for (i = 0; i < HIST_MAX; i++) hist_list[i] = hist + (i * (HIST_SIZE + 1)); From patchwork Sun Mar 3 14:34:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hanyuan Zhao X-Patchwork-Id: 1907292 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.a=rsa-sha256 header.s=s201512 header.b=AweK1FTO; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tnkvq6GW5z23qh for ; Mon, 4 Mar 2024 01:38:15 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 0D8A287F1F; Sun, 3 Mar 2024 15:38:12 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.b="AweK1FTO"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E020187F23; Sun, 3 Mar 2024 15:38:10 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, HELO_DYNAMIC_IPADDR,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,RDNS_DYNAMIC, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from out162-62-58-211.mail.qq.com (out162-62-58-211.mail.qq.com [162.62.58.211]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 9E45E87F1C for ; Sun, 3 Mar 2024 15:38:06 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=hanyuan-z@qq.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1709476678; bh=nPUSqXkBhkZEvU9Y6UpChrbZfWltWqpJoez04Ae+ij4=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=AweK1FTOfoS8t1HUd/xbeb7WHhlZQGC4T9HhH+hcHd4TT+FUxFUJegpd5BnzpZFlO u/jVvUNYbQouwi++EaEHpysRG0NqZszfuUg34wlLN8uos8QyKQnbnJRN+tv0mrODG9 6kuVDlm5Kb5Kw3urn5aLgZZBkmb2wFe07eqp8wEE= Received: from localhost.localdomain ([183.172.216.136]) by newxmesmtplogicsvrszc5-0.qq.com (NewEsmtp) with SMTP id 95A9DA66; Sun, 03 Mar 2024 22:37:26 +0800 X-QQ-mid: xmsmtpt1709476648taq8dclhq Message-ID: X-QQ-XMAILINFO: MB5+LsFw85Nom61B1JsF63Fg84UOkmNY2mYNQGHG1ZXy7kUcFPWcPBuqFH/wDz qzIWI1OYnUZELnW5KPFJCOyBzs5+ndxG/TPqHFG/95EpXqpTBqmBngePrfvUtlZ8Bh31Lhp1PgYX 801c2sOwA6YvFN/b/YNdP93eH4bQkaLQMpamu6YnO8uVhdllkyt48UUKiFalSjbTt2odPk13NQpj 9MyOPgssg2IUYuMmgrnkwuJz8MZ/eqannwmrWxUHRXIA/4StRSJoQssZZlvMI1BnNVGuwyHdSkeW hVm+oWfG7M8RabQl2PwU7H/YGHtHgDysqlJWYzHQCxKdS4dptiT28IfwkcWrnAk3W3jXvmOcIR3Z sLfQ4mlLzlfhagF/o444pFhNJHHPmqTunzLBUOIiDv5P82WEa4G/trPYejVfAB5f8XwEZvdpChvM m7RgxsOt/YJwnYfkxO5m4pt/SycvxzNqKAuSad2ZbZ/A8kce4LLrZ1dwW7kKOxUo6k+GY69nXy87 r0kkzyva/3FcfAOgqSRcKJggjPS/hbE/1OvEbnOce9rkzOz03IzcaMT/Fy8kPb9uKTAe0NpsYARd Sz+7J35L1UodQoiCVgUD3XmQTTxg8DHfXCz+yG5nHeJOVv/8BUzPn9C554m7Iv1S8XR2a2jSqPI2 LaMId0LSUV9/VYbLNJGrpCTBMWHTDWocBwAJdFBn3a4SJYmuHU313b41LsODowG/OT/ELN1aoAw9 B9FY4Iz9HvVueQ41jVgQjeHSgpv+PQrh/FCvmaiot92xwL+FYJeVCOjy+DS/Vc08dl1wTpNlz90A 8Uu8UdB2K9CdEhDs44AR1MU5ewbXNHuXmPvnXkJBzj0XA0ezQJJ9GwD7xTE80cC9oXc9KLbswXgE tbmZ5+hQtH8hUDptwN3R2YiRAq19VOn/ZNJ7hzYt0RZ1/E1foP5pYZcpMkZgvnPkrbateKDuroSl 0a5okFvT+QdAhy36eetBOmaAj0MUPJsmGLgaxdIFTy9YmPsOVFZNozNk46Xa8vQOciDZaLoMw= X-QQ-XMRINFO: M/715EihBoGSf6IYSX1iLFg= From: Hanyuan Zhao To: u-boot@lists.denx.de Cc: Tom Rini , Simon Glass , Hanyuan Zhao Subject: [PATCH v2 2/2] cli: allow users to determine history buffer allocation method Date: Sun, 3 Mar 2024 22:34:47 +0800 X-OQ-MSGID: <20240303143445.7030-2-hanyuan-z@qq.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240303143445.7030-1-hanyuan-z@qq.com> References: <20240303143445.7030-1-hanyuan-z@qq.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean This commit allows users to choose the appropriate memory allocation method between static allocated and dynamically calloc. The previous static-array way will not obviously contribute to the final binary size since it is uninitialized, and might have better performance than the dynamical one. Now we provide the users with both the two options. Signed-off-by: Hanyuan Zhao --- Changes v1 -> v2: - Add more detailed information about the CMD_HISTORY_USE_CALLOC option both in the Kconfig and the code. - Update the comments on global history array and flash running problems. --- cmd/Kconfig | 11 +++++++++++ common/cli_readline.c | 36 +++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/cmd/Kconfig b/cmd/Kconfig index a86b570517..7d2c050e08 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -189,6 +189,17 @@ config CMD_HISTORY Show the command-line history, i.e. a list of commands that are in the history buffer. +config CMD_HISTORY_USE_CALLOC + bool "dynamically allocate memory" + default y + depends on CMD_HISTORY + help + Saying Y to this will use calloc to get the space for history + storing. Otherwise the history buffer will be an uninitialized + static array directly, without the memory allocation, and it is + writable after relocation to RAM. If u-boot is running from ROM + all the time or unsure, say Y to this. + config CMD_LICENSE bool "license" select BUILD_BIN2C diff --git a/common/cli_readline.c b/common/cli_readline.c index 99e7efdfe5..cf4339d0e5 100644 --- a/common/cli_readline.c +++ b/common/cli_readline.c @@ -86,6 +86,9 @@ static int hist_add_idx; static int hist_cur = -1; static unsigned hist_num; +#ifndef CONFIG_CMD_HISTORY_USE_CALLOC +static char hist_data[HIST_MAX][HIST_SIZE + 1]; +#endif static char *hist_list[HIST_MAX]; #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) @@ -100,20 +103,26 @@ static void getcmd_putchars(int count, int ch) static int hist_init(void) { - unsigned char *hist; int i; - hist_max = 0; - hist_add_idx = 0; - hist_cur = -1; - hist_num = 0; - - hist = calloc(HIST_MAX, HIST_SIZE + 1); +#ifndef CONFIG_CMD_HISTORY_USE_CALLOC + for (i = 0; i < HIST_MAX; i++) { + hist_list[i] = hist_data[i]; + hist_list[i][0] = '\0'; + } +#else + unsigned char *hist = calloc(HIST_MAX, HIST_SIZE + 1); if (!hist) panic("%s: calloc: out of memory!\n", __func__); for (i = 0; i < HIST_MAX; i++) hist_list[i] = hist + (i * (HIST_SIZE + 1)); +#endif + + hist_max = 0; + hist_add_idx = 0; + hist_cur = -1; + hist_num = 0; return 0; } @@ -643,10 +652,15 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer, static int initted; /* - * History uses a global array which is not - * writable until after relocation to RAM. - * Revert to non-history version if still - * running from flash. + * Say N to CMD_HISTORY_USE_CALLOC will skip runtime + * allocation for the history buffer and directly + * use an uninitialized static array as the buffer. + * Doing this might have better performance and not + * increase the binary file's size, as it only marks + * the size. However, the array is only writable after + * relocation to RAM. If u-boot is running from ROM + * all the time, consider say Y to CMD_HISTORY_USE_CALLOC + * or disable CMD_HISTORY. */ if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) { if (!initted) {