All of lore.kernel.org
 help / color / mirror / Atom feed
From: Urja Rannikko <urjaman@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 17/17] spi: Avoid using malloc() in a critical function
Date: Sun, 19 May 2019 17:04:45 +0000	[thread overview]
Message-ID: <CAPCnQJ=utRAsB17xBtt0fw3etxstobbsq8RgM9ekB7UV7gSYdg@mail.gmail.com> (raw)
In-Reply-To: <20190518175954.262021-18-sjg@chromium.org>

Hi,

On Sat, May 18, 2019 at 6:05 PM Simon Glass <sjg@chromium.org> wrote:
>
> In general we should avoid calling malloc() and free() repeatedly in
> U-Boot lest we turn it into tianocore. In SPL this can make SPI flash
> unusable since free() is often a nop and allocation space is limited.
>
> In any case, these seems no need for malloc() since the number of bytes
> is very small, perhaps less than 8.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> Fixes: d13f5b254a (spi: Extend the core to ease integration of SPI
>         memory controllers)
>
> ---
>
>  drivers/spi/spi-mem.c | 15 +++++++++++----
>  1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/spi/spi-mem.c b/drivers/spi/spi-mem.c
> index b86eee75bcb..7aabebeff5f 100644
> --- a/drivers/spi/spi-mem.c
> +++ b/drivers/spi/spi-mem.c
> @@ -201,7 +201,6 @@ int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
>         unsigned int pos = 0;
>         const u8 *tx_buf = NULL;
>         u8 *rx_buf = NULL;
> -       u8 *op_buf;
>         int op_len;
>         u32 flag;
>         int ret;
> @@ -338,7 +337,17 @@ int spi_mem_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
>         }
>
>         op_len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
> -       op_buf = calloc(1, op_len);
> +
> +       /*
> +        * Avoid using malloc() here so that we can use this code in SPL where
> +        * simple malloc may be used. That implementation does not allow free()
> +        * so repeated calls to this code can exhaust the space.
> +        *
> +        * The value of op_len is small, since it does not include the actual
> +        * data being sent, only the op-code and address. In fact, it should be
> +        * possible to just use a small fixed value here instead of op_len.
> +        */
> +       u8 op_buf[op_len];

I'd say just make this a fixed buffer instead of a VLA - less code
space bloat and potential stack problems in case of nonsensical
inputs.

As for the size, 8 bytes would be fine and actually leave some margin:
the most i would expect for op_len is 1 + 4 + 1 = 6 bytes (the lowest
would be 1+3+0 and the usual 1+3+1).

-- 
Urja Rannikko

  reply	other threads:[~2019-05-19 17:04 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-18 17:59 [U-Boot] [PATCH 00/17] sandbox: Various bug fixes Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 01/17] spl: misc: Allow misc drivers in SPL and TPL Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 02/17] Add an empty stdint.h file Simon Glass
2019-05-22 13:21   ` Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 03/17] sandbox: Sync up sandbox64.dts with main DT Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 04/17] sandbox: Create a common sandbox DT Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 05/17] sandbox: Add an alias for SPI Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 06/17] sandbox: Exit when SYSRESET_POWER_OFF is requested Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 07/17] sandbox: Quit when hang() is called Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 08/17] sandbox: spl: Lower priority of standard loader Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 09/17] sandbox: Add a comment to board_init_f() Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 10/17] sandbox: Allo sdl-config to be overridden Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 11/17] sandbox: Add a memory {} node Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 12/17] sandbox: Correct spi flash operation Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 13/17] sandbox: Add documentation on how to run valgrind Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 14/17] x86: Add a forward struct declaration in coreboot_tables.h Simon Glass
2019-05-19  7:39   ` Bin Meng
2019-05-18 17:59 ` [U-Boot] [PATCH 15/17] bootstage: Add support for TPL record count Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 16/17] blk: Allow control of the block cache in TPL Simon Glass
2019-05-18 17:59 ` [U-Boot] [PATCH 17/17] spi: Avoid using malloc() in a critical function Simon Glass
2019-05-19 17:04   ` Urja Rannikko [this message]
2019-05-20 16:09     ` Simon Glass
2019-05-20 16:35       ` Urja Rannikko
2019-05-20 20:51         ` Simon Glass
2019-06-28 13:54 ` [U-Boot] [PATCH 00/17] sandbox: Various bug fixes Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAPCnQJ=utRAsB17xBtt0fw3etxstobbsq8RgM9ekB7UV7gSYdg@mail.gmail.com' \
    --to=urjaman@gmail.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.