2024-03-01 20:26:03

by David Lechner

[permalink] [raw]
Subject: [PATCH 0/3] spi: axi-spi-engine: small cleanups

This series contains a few small cleanups to the axi-spi-engine driver,
mostly suggested from previous reviews.

---
David Lechner (3):
spi: axi-spi-engine: remove p from struct spi_engine_message_state
spi: axi-spi-engine: use __counted_by() attribute
spi: axi-spi-engine: use struct_size() macro

drivers/spi/spi-axi-spi-engine.c | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)
---
base-commit: bf790d87088a04d5f3a4659e04ff2a5a16eca294
change-id: 20240301-mainline-axi-spi-engine-small-cleanups-cd08b51cb6d4

Best regards,
--
David Lechner <[email protected]>



2024-03-01 20:26:06

by David Lechner

[permalink] [raw]
Subject: [PATCH 1/3] spi: axi-spi-engine: remove p from struct spi_engine_message_state

The program pointer p in struct spi_engine_message_state in the AXI SPI
Engine controller driver was assigned but never read so it can be
removed.

Signed-off-by: David Lechner <[email protected]>
---
drivers/spi/spi-axi-spi-engine.c | 3 ---
1 file changed, 3 deletions(-)

diff --git a/drivers/spi/spi-axi-spi-engine.c b/drivers/spi/spi-axi-spi-engine.c
index 6177c1a8d56e..d89f75170c9e 100644
--- a/drivers/spi/spi-axi-spi-engine.c
+++ b/drivers/spi/spi-axi-spi-engine.c
@@ -82,8 +82,6 @@ struct spi_engine_program {
* struct spi_engine_message_state - SPI engine per-message state
*/
struct spi_engine_message_state {
- /** @p: Instructions for executing this message. */
- struct spi_engine_program *p;
/** @cmd_length: Number of elements in cmd_buf array. */
unsigned cmd_length;
/** @cmd_buf: Array of commands not yet written to CMD FIFO. */
@@ -543,7 +541,6 @@ static int spi_engine_transfer_one_message(struct spi_controller *host,

/* reinitialize message state for this transfer */
memset(st, 0, sizeof(*st));
- st->p = p;
st->cmd_buf = p->instructions;
st->cmd_length = p->length;
msg->state = st;

--
2.43.2


2024-03-01 20:26:12

by David Lechner

[permalink] [raw]
Subject: [PATCH 2/3] spi: axi-spi-engine: use __counted_by() attribute

This adds the __counted_by() attribute to the flex array at the end of
struct spi_engine_program in the AXI SPI Engine controller driver.

Suggested-by: Nuno Sá <[email protected]>
Signed-off-by: David Lechner <[email protected]>
---
drivers/spi/spi-axi-spi-engine.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/spi/spi-axi-spi-engine.c b/drivers/spi/spi-axi-spi-engine.c
index d89f75170c9e..e801eed820df 100644
--- a/drivers/spi/spi-axi-spi-engine.c
+++ b/drivers/spi/spi-axi-spi-engine.c
@@ -75,7 +75,7 @@

struct spi_engine_program {
unsigned int length;
- uint16_t instructions[];
+ uint16_t instructions[] __counted_by(length);
};

/**

--
2.43.2


2024-03-01 20:26:27

by David Lechner

[permalink] [raw]
Subject: [PATCH 3/3] spi: axi-spi-engine: use struct_size() macro

This makes use of the struct_size() macro to calculate the size of the
struct axi_spi_engine when allocating it.

Suggested-by: Christophe JAILLET <[email protected]>
Signed-off-by: David Lechner <[email protected]>
---
drivers/spi/spi-axi-spi-engine.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/spi/spi-axi-spi-engine.c b/drivers/spi/spi-axi-spi-engine.c
index e801eed820df..9646764b0042 100644
--- a/drivers/spi/spi-axi-spi-engine.c
+++ b/drivers/spi/spi-axi-spi-engine.c
@@ -12,6 +12,7 @@
#include <linux/io.h>
#include <linux/of.h>
#include <linux/module.h>
+#include <linux/overflow.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>

@@ -501,15 +502,13 @@ static irqreturn_t spi_engine_irq(int irq, void *devid)
static int spi_engine_optimize_message(struct spi_message *msg)
{
struct spi_engine_program p_dry, *p;
- size_t size;

spi_engine_precompile_message(msg);

p_dry.length = 0;
spi_engine_compile_message(msg, true, &p_dry);

- size = sizeof(*p->instructions) * (p_dry.length + 1);
- p = kzalloc(sizeof(*p) + size, GFP_KERNEL);
+ p = kzalloc(struct_size(p, instructions, p_dry.length + 1), GFP_KERNEL);
if (!p)
return -ENOMEM;


--
2.43.2


2024-03-01 20:42:10

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH 2/3] spi: axi-spi-engine: use __counted_by() attribute

On Fri, Mar 01, 2024 at 02:25:19PM -0600, David Lechner wrote:
> This adds the __counted_by() attribute to the flex array at the end of
> struct spi_engine_program in the AXI SPI Engine controller driver.
>
> Suggested-by: Nuno S? <[email protected]>
> Signed-off-by: David Lechner <[email protected]>
> ---
> drivers/spi/spi-axi-spi-engine.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/spi/spi-axi-spi-engine.c b/drivers/spi/spi-axi-spi-engine.c
> index d89f75170c9e..e801eed820df 100644
> --- a/drivers/spi/spi-axi-spi-engine.c
> +++ b/drivers/spi/spi-axi-spi-engine.c
> @@ -75,7 +75,7 @@
>
> struct spi_engine_program {
> unsigned int length;
> - uint16_t instructions[];
> + uint16_t instructions[] __counted_by(length);
> };

You'll also need to change places where you deal with changes to
"length", as now accesses to "instructions" will be bounds-checked
by the compiler. For example, this change:

static void spi_engine_program_add_cmd(struct spi_engine_program *p,
bool dry, uint16_t cmd)
{
p->length++;
if (!dry)
p->instructions[p->length - 1] = cmd;
}

--
Kees Cook

2024-03-01 20:42:23

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH 1/3] spi: axi-spi-engine: remove p from struct spi_engine_message_state

On Fri, Mar 01, 2024 at 02:25:18PM -0600, David Lechner wrote:
> The program pointer p in struct spi_engine_message_state in the AXI SPI
> Engine controller driver was assigned but never read so it can be
> removed.
>
> Signed-off-by: David Lechner <[email protected]>

Reviewed-by: Kees Cook <[email protected]>

--
Kees Cook

2024-03-01 20:46:02

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH 3/3] spi: axi-spi-engine: use struct_size() macro

On Fri, Mar 01, 2024 at 02:25:20PM -0600, David Lechner wrote:
> This makes use of the struct_size() macro to calculate the size of the
> struct axi_spi_engine when allocating it.
>
> Suggested-by: Christophe JAILLET <[email protected]>
> Signed-off-by: David Lechner <[email protected]>

Reviewed-by: Kees Cook <[email protected]>

--
Kees Cook