From patchwork Sun Mar 13 14:48:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 550926 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:6713:0:0:0:0 with SMTP id u19csp1826505mag; Sun, 13 Mar 2022 07:50:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwYP/nnbF9YB9SVh9+2g+vK6ve5Q77I18IkJPBfIcEcbFZCj9dpnpHPuTNj5gk7uN4U/cEI X-Received: by 2002:a05:6402:3552:b0:416:9386:9e68 with SMTP id f18-20020a056402355200b0041693869e68mr16655293edd.286.1647183016542; Sun, 13 Mar 2022 07:50:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647183016; cv=none; d=google.com; s=arc-20160816; b=PhtuT4txB2qYMzPSAlu01xA38E09A7LFkmp/DzCS1yJ7K/9gZOOQK5buV9vpQL1LEi nad4EmJPO2qPMFgiY1srQF+VPvsT4v39H7Lhy5lcTjqPpO4UjIHNnRvW2hD57+jPEMpH DEP5saU/slCmxBrMSV7JM4yH0xHp3hx2ti55R6pW45+Wcjlyf5hW1X6By7OSylULUdzC 7I+Em2jA1LTO+SA2ryZCbGjryw4UJNwKfNwjYGEo2S8EEGxa+8lMpi4BdawZlrB2sEZJ cJu119C4Lv0p5bsA1RgZ97oA+ErvdgE8bsRMDR9CN9OwidcjMY3D4L8WHhqPUbaeTRgh WgPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=ZwYntnV3Os4b5feSjstGoJu6tIjUiqLh7TRRWQocKm8=; b=Kk+bA10IeYgOu0BRAoEQf+qP86P4x9ZVFEguNmPKeq58tQYuyBwybJD+JrMgGNI7WJ +mYRhsdD9dAD1/D35LnrBJaEKFgOiAD+gV7TNgT37lQIsS13kLWk94TyvRtQm85FMVB3 xLt9PfzHJJJUnCFVL5C5SJQq7a6DGEy1/RL96NwFJWIGlBbT8LsubPSf9o6xUVRPpMpc /Dd+zMp/LGSPE/goawKqmKNL6IskXavw/OmtRpl8fUrklfODB81deHGVvyh4wG/NaKba lZA7kqwD8GXw+4zbdc6ZPSYPRV3PsQGs2q38j/8NKJnIIYotoaC4fNRv67pxrCLMzZas /R/A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id qb38-20020a1709077ea600b006da8e9a40c2si8800070ejc.575.2022.03.13.07.50.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Mar 2022 07:50:16 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 2D32E83260; Sun, 13 Mar 2022 15:49:56 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id ECDC680F47; Sun, 13 Mar 2022 15:49:17 +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=-1.2 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id D560B83AF7 for ; Sun, 13 Mar 2022 15:49:05 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sughosh.ganu@linaro.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 458251042; Sun, 13 Mar 2022 07:49:03 -0700 (PDT) Received: from a076522.blr.arm.com (a076522.blr.arm.com [10.162.16.44]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 799FF3F7D7; Sun, 13 Mar 2022 07:49:01 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Simon Glass , Heinrich Schuchardt , Sughosh Ganu Subject: [PATCH v5 7/9] cmd: rng: Use a statically allocated array for random bytes Date: Sun, 13 Mar 2022 20:18:00 +0530 Message-Id: <20220313144802.65687-8-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220313144802.65687-1-sughosh.ganu@linaro.org> References: <20220313144802.65687-1-sughosh.ganu@linaro.org> 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.5 at phobos.denx.de X-Virus-Status: Clean Use a statically allocated buffer on stack instead of using malloc for reading the random bytes. Using a local array is faster than allocating heap memory on every initiation of the command. Signed-off-by: Sughosh Ganu --- Changes since V4: * New patch based on review comments from Simon to not use the malloc call cmd/rng.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/cmd/rng.c b/cmd/rng.c index 2ddf27545f..81a23964b8 100644 --- a/cmd/rng.c +++ b/cmd/rng.c @@ -14,9 +14,9 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { size_t n; - struct udevice *dev; - void *buf; + u8 buf[64]; int devnum; + struct udevice *dev; int ret = CMD_RET_SUCCESS; switch (argc) { @@ -41,11 +41,10 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) return CMD_RET_FAILURE; } - buf = malloc(n); - if (!buf) { - printf("Out of memory\n"); - return CMD_RET_FAILURE; - } + if (!n) + return 0; + + n = min(n, sizeof(buf)); if (dm_rng_read(dev, buf, n)) { printf("Reading RNG failed\n"); @@ -54,15 +53,13 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n); } - free(buf); - return ret; } #ifdef CONFIG_SYS_LONGHELP static char rng_help_text[] = "[dev [n]]\n" - " - print n random bytes read from dev\n"; + " - print n random bytes(max 64) read from dev\n"; #endif U_BOOT_CMD(