linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] spi: axi-spi-engine: small cleanups
@ 2024-03-01 20:25 David Lechner
  2024-03-01 20:25 ` [PATCH 1/3] spi: axi-spi-engine: remove p from struct spi_engine_message_state David Lechner
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: David Lechner @ 2024-03-01 20:25 UTC (permalink / raw)
  To: Mark Brown
  Cc: David Lechner, Michael Hennerich, Nuno Sá,
	Kees Cook, Gustavo A. R. Silva, linux-spi, linux-kernel,
	linux-hardening, Christophe JAILLET

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 <dlechner@baylibre.com>


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/3] spi: axi-spi-engine: remove p from struct spi_engine_message_state
  2024-03-01 20:25 [PATCH 0/3] spi: axi-spi-engine: small cleanups David Lechner
@ 2024-03-01 20:25 ` David Lechner
  2024-03-01 20:42   ` Kees Cook
  2024-03-01 20:25 ` [PATCH 2/3] spi: axi-spi-engine: use __counted_by() attribute David Lechner
  2024-03-01 20:25 ` [PATCH 3/3] spi: axi-spi-engine: use struct_size() macro David Lechner
  2 siblings, 1 reply; 7+ messages in thread
From: David Lechner @ 2024-03-01 20:25 UTC (permalink / raw)
  To: Mark Brown
  Cc: David Lechner, Michael Hennerich, Nuno Sá,
	Kees Cook, Gustavo A. R. Silva, linux-spi, linux-kernel,
	linux-hardening

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 <dlechner@baylibre.com>
---
 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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/3] spi: axi-spi-engine: use __counted_by() attribute
  2024-03-01 20:25 [PATCH 0/3] spi: axi-spi-engine: small cleanups David Lechner
  2024-03-01 20:25 ` [PATCH 1/3] spi: axi-spi-engine: remove p from struct spi_engine_message_state David Lechner
@ 2024-03-01 20:25 ` David Lechner
  2024-03-01 20:41   ` Kees Cook
  2024-03-01 20:25 ` [PATCH 3/3] spi: axi-spi-engine: use struct_size() macro David Lechner
  2 siblings, 1 reply; 7+ messages in thread
From: David Lechner @ 2024-03-01 20:25 UTC (permalink / raw)
  To: Mark Brown
  Cc: David Lechner, Michael Hennerich, Nuno Sá,
	Kees Cook, Gustavo A. R. Silva, linux-spi, linux-kernel,
	linux-hardening

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á <nuno.sa@analog.com>
Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/3] spi: axi-spi-engine: use struct_size() macro
  2024-03-01 20:25 [PATCH 0/3] spi: axi-spi-engine: small cleanups David Lechner
  2024-03-01 20:25 ` [PATCH 1/3] spi: axi-spi-engine: remove p from struct spi_engine_message_state David Lechner
  2024-03-01 20:25 ` [PATCH 2/3] spi: axi-spi-engine: use __counted_by() attribute David Lechner
@ 2024-03-01 20:25 ` David Lechner
  2024-03-01 20:45   ` Kees Cook
  2 siblings, 1 reply; 7+ messages in thread
From: David Lechner @ 2024-03-01 20:25 UTC (permalink / raw)
  To: Mark Brown
  Cc: David Lechner, Michael Hennerich, Nuno Sá,
	Kees Cook, Gustavo A. R. Silva, linux-spi, linux-kernel,
	linux-hardening, Christophe JAILLET

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 <christophe.jaillet@wanadoo.fr>
Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/3] spi: axi-spi-engine: use __counted_by() attribute
  2024-03-01 20:25 ` [PATCH 2/3] spi: axi-spi-engine: use __counted_by() attribute David Lechner
@ 2024-03-01 20:41   ` Kees Cook
  0 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2024-03-01 20:41 UTC (permalink / raw)
  To: David Lechner
  Cc: Mark Brown, Michael Hennerich, Nuno Sá,
	Gustavo A. R. Silva, linux-spi, linux-kernel, linux-hardening

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á <nuno.sa@analog.com>
> Signed-off-by: David Lechner <dlechner@baylibre.com>
> ---
>  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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/3] spi: axi-spi-engine: remove p from struct spi_engine_message_state
  2024-03-01 20:25 ` [PATCH 1/3] spi: axi-spi-engine: remove p from struct spi_engine_message_state David Lechner
@ 2024-03-01 20:42   ` Kees Cook
  0 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2024-03-01 20:42 UTC (permalink / raw)
  To: David Lechner
  Cc: Mark Brown, Michael Hennerich, Nuno Sá,
	Gustavo A. R. Silva, linux-spi, linux-kernel, linux-hardening

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 <dlechner@baylibre.com>

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/3] spi: axi-spi-engine: use struct_size() macro
  2024-03-01 20:25 ` [PATCH 3/3] spi: axi-spi-engine: use struct_size() macro David Lechner
@ 2024-03-01 20:45   ` Kees Cook
  0 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2024-03-01 20:45 UTC (permalink / raw)
  To: David Lechner
  Cc: Mark Brown, Michael Hennerich, Nuno Sá,
	Gustavo A. R. Silva, linux-spi, linux-kernel, linux-hardening,
	Christophe JAILLET

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 <christophe.jaillet@wanadoo.fr>
> Signed-off-by: David Lechner <dlechner@baylibre.com>

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-03-01 20:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-01 20:25 [PATCH 0/3] spi: axi-spi-engine: small cleanups David Lechner
2024-03-01 20:25 ` [PATCH 1/3] spi: axi-spi-engine: remove p from struct spi_engine_message_state David Lechner
2024-03-01 20:42   ` Kees Cook
2024-03-01 20:25 ` [PATCH 2/3] spi: axi-spi-engine: use __counted_by() attribute David Lechner
2024-03-01 20:41   ` Kees Cook
2024-03-01 20:25 ` [PATCH 3/3] spi: axi-spi-engine: use struct_size() macro David Lechner
2024-03-01 20:45   ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).