diff mbox series

[v7,5/7] cmd: rng: Use a statically allocated array for random bytes

Message ID 20220720122926.415659-6-sughosh.ganu@linaro.org
State Superseded
Headers show
Series tpm: rng: Move TPM RNG functionality to driver model | expand

Commit Message

Sughosh Ganu July 20, 2022, 12:29 p.m. UTC
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 <sughosh.ganu@linaro.org>
---
Changes since V6: None

 cmd/rng.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

Comments

Ilias Apalodimas July 21, 2022, 8:05 a.m. UTC | #1
Hi Sughosh,

This is missing the r-b tags from Simon and myself.  Once I finish up
the review, can you re-send the series with the missing tags?

Thanks
/Ilias

On Wed, 20 Jul 2022 at 15:30, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
>
> 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 <sughosh.ganu@linaro.org>
> ---
> Changes since V6: None
>
>  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(
> --
> 2.34.1
>
Sughosh Ganu July 21, 2022, 8:15 a.m. UTC | #2
hi Ilias,

On Thu, 21 Jul 2022 at 13:36, Ilias Apalodimas
<ilias.apalodimas@linaro.org> wrote:
>
> Hi Sughosh,
>
> This is missing the r-b tags from Simon and myself.  Once I finish up
> the review, can you re-send the series with the missing tags?

Sure, will add the r-b tags in the next version. Thanks.

-sughosh

>
> Thanks
> /Ilias
>
> On Wed, 20 Jul 2022 at 15:30, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
> >
> > 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 <sughosh.ganu@linaro.org>
> > ---
> > Changes since V6: None
> >
> >  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(
> > --
> > 2.34.1
> >
diff mbox series

Patch

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(