drm/sitronix: st7920: fix SPI DMA buffer, unplug guard, and row address
cover drm/sitronix: st7920: fix SPI DMA buffer, unplug guard, and row address
I am trying out new Clang static analysis checks for kernel bug classes, and they flagged this code as possibly buggy. The findings were triaged/fixed and commit descriptions were drafted with help of AI, then checked against linux-next (not runtime-tested on hardware).
Thanks,
Elkin
Elkin Cruz (3):
drm/sitronix: st7920: Use DMA-safe serialized TX buffer for SPI writes drm/sitronix: st7920: Check drm_dev_enter() in crtc atomic disable drm/sitronix: st7920: Pass u8 row address when setting GDRAM address
drivers/gpu/drm/sitronix/st7920.c | 48 ++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 14 deletions(-)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 1/3 drm/sitronix: st7920: Use DMA-safe serialized TX buffer for SPI writes
In st7920_spi_write(), u8 reg[CMD_SIZE] is allocated on the stack and passed directly to spi_write(), violating SPI DMA safety requirements when CONFIG_VMAP_STACK is enabled or an IOMMU is in use.
Embed an ARCH_DMA_MINALIGN-aligned TX buffer at the end of struct st7920_device protected by a DRM-managed mutex, and pass st7920 to st7920_spi_write().
--- drivers/gpu/drm/sitronix/st7920.c | 43 +++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 13 deletions(-)
drivers/gpu/drm/sitronix/st7920.c +30 -13
@@ -7,9 +7,12 @@*/ #include <linux/bitrev.h> +#include <linux/cache.h> +#include <linux/cleanup.h> #include <linux/delay.h> #include <linux/gpio/consumer.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/regmap.h> #include <linux/spi/spi.h>
@@ -24,6 +27,7 @@#include <drm/drm_gem_atomic_helper.h> #include <drm/drm_gem_framebuffer_helper.h> #include <drm/drm_gem_shmem_helper.h> +#include <drm/drm_managed.h> #include <drm/drm_plane.h> #include <drm/drm_print.h> #include <drm/drm_probe_helper.h>
@@ -86,6 +90,10 @@ struct st7920_device {u32 height;u32 width; + +/* Serializes SPI command and GDRAM transfers using tx_buf */ +struct mutex tx_lock; +u8 tx_buf[CMD_SIZE ]__aligned(ARCH_DMA_MINALIGN) ; };struct st7920_plane_state {
@@ -165,16 +173,21 @@ static int st7920_store_others(int cmd, const void *data, u8 *reg)return i; } -static void st7920_spi_write (struct spi_device *spi,int cmd,const void *data, -int delay_us,struct spi7920_error *err) +static void st7920_spi_write (struct st7920_device *st7920,int cmd, +const void *data,int delay_us, +struct spi7920_error *err) { -u8 reg[CMD_SIZE ] = {0 }; +u8 *reg = st7920->tx_buf;int size =0 ;int ret;if (err->errno)return ; +guard (mutex)(&st7920->tx_lock); + +memset (reg,0 ,CMD_SIZE ); +/* * First the sync bits are sent: 11111WS0. * Where W is the read/write (RW) bit and S is the register/data (RS) bit.
@@ -190,7 +203,7 @@ static void st7920_spi_write(struct spi_device *spi, int cmd, const void *data,else size =st7920_store_others (cmd, data, reg); - ret =spi_write (spi, reg, size); + ret =spi_write (st7920->spi, reg, size);if (ret) { err->errno = ret;return ;
@@ -232,14 +245,14 @@ MODULE_DEVICE_TABLE(spi, st7920_spi_id);static void st7920_power_on (struct st7920_device *st7920,struct spi7920_error *err) { -st7920_spi_write (st7920->spi,SET_DISPLAY_ON ,NULL ,72 , err); +st7920_spi_write (st7920,SET_DISPLAY_ON ,NULL ,72 , err); }static void st7920_power_off (struct st7920_device *st7920,struct spi7920_error *err) { -st7920_spi_write (st7920->spi,SET_DISPLAY_CLEAR ,NULL ,1600 , err); -st7920_spi_write (st7920->spi,SET_DISPLAY_OFF ,NULL ,72 , err); +st7920_spi_write (st7920,SET_DISPLAY_CLEAR ,NULL ,1600 , err); +st7920_spi_write (st7920,SET_DISPLAY_OFF ,NULL ,72 , err); }static void st7920_hw_reset (struct st7920_device *st7920)
@@ -257,10 +270,10 @@ static int st7920_init(struct st7920_device *st7920) {struct spi7920_error err = {0 }; -st7920_spi_write (st7920->spi,SET_BASIC_INSTRUCTION_SET ,NULL ,72 , &err); +st7920_spi_write (st7920,SET_BASIC_INSTRUCTION_SET ,NULL ,72 , &err);st7920_power_on (st7920, &err); -st7920_spi_write (st7920->spi,SET_GRAPHICS_DISPLAY ,NULL ,72 , &err); -st7920_spi_write (st7920->spi,SET_DISPLAY_CLEAR ,NULL ,1600 , &err); +st7920_spi_write (st7920,SET_GRAPHICS_DISPLAY ,NULL ,72 , &err); +st7920_spi_write (st7920,SET_DISPLAY_CLEAR ,NULL ,1600 , &err);return err.errno; }
@@ -296,8 +309,8 @@ static int st7920_update_rect(struct st7920_device *st7920, data_array[array_idx++] = line_buffer[j]; } -st7920_spi_write (st7920->spi,SET_GDRAM_ADDRESS , &i,72 , &err); -st7920_spi_write (st7920->spi,SET_GDRAM_DATA , line_buffer,72 , &err); +st7920_spi_write (st7920,SET_GDRAM_ADDRESS , &i,72 , &err); +st7920_spi_write (st7920,SET_GDRAM_DATA , line_buffer,72 , &err); }return err.errno;
@@ -309,7 +322,7 @@ static void st7920_clear_screen(struct st7920_device *st7920, u8 *data_array)memset (data_array,0 ,BYTES_IN_DISPLAY ); -st7920_spi_write (st7920->spi,SET_DISPLAY_CLEAR ,NULL ,1600 , &err); +st7920_spi_write (st7920,SET_DISPLAY_CLEAR ,NULL ,1600 , &err); } static int st7920_fb_blit_rect(struct drm_framebuffer *fb,
@@ -812,6 +825,10 @@ static int st7920_probe(struct spi_device *spi) st7920->width =ST7920_DEFAULT_WIDTH ; st7920->height =ST7920_DEFAULT_HEIGHT ; + ret =drmm_mutex_init (drm, &st7920->tx_lock); +if (ret) +return ret; + st7920->reset_gpio =devm_gpiod_get_optional (dev,"reset" ,GPIOD_OUT_LOW );if (IS_ERR (st7920->reset_gpio)) { ret =PTR_ERR (st7920->reset_gpio);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 2/3 drm/sitronix: st7920: Check drm_dev_enter() in crtc atomic disable
In reply to Elkin Cruzst7920_crtc_atomic_disable() ignores the return value of drm_dev_enter(), allowing SPI power-off writes to proceed after the DRM device has been unplugged and unconditionally calling drm_dev_exit().
Return early when drm_dev_enter() returns false.
--- drivers/gpu/drm/sitronix/st7920.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/sitronix/st7920.c +2 -1
@@ -580,7 +580,8 @@ static void st7920_crtc_atomic_disable(struct drm_crtc *crtc,struct st7920_device *st7920 =drm_to_st7920 (drm);int idx; -drm_dev_enter (drm, &idx); +if (!drm_dev_enter (drm, &idx)) +return ;st7920_power_off (st7920, &err);
Signature · 1 line
2.56.1.839.g22a8290db9-goog
PATCH 3/3 drm/sitronix: st7920: Pass u8 row address when setting GDRAM address
In reply to Elkin Cruzst7920_update_rect() passes &i (int *) as the data pointer for SET_GDRAM_ADDRESS, whereas st7920_store_gdram_address() reads a single u8 byte. On big-endian systems this reads the most significant byte (0) instead of the row index.
Store the row index in a u8 variable and pass its address to st7920_spi_write().
--- drivers/gpu/drm/sitronix/st7920.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/sitronix/st7920.c +3 -1
@@ -309,7 +309,9 @@ static int st7920_update_rect(struct st7920_device *st7920, data_array[array_idx++] = line_buffer[j]; } -st7920_spi_write (st7920,SET_GDRAM_ADDRESS , &i,72 , &err); +u8 y = i; + +st7920_spi_write (st7920,SET_GDRAM_ADDRESS , &y,72 , &err);st7920_spi_write (st7920,SET_GDRAM_DATA , line_buffer,72 , &err); }
Signature · 1 line
2.56.1.839.g22a8290db9-goog