Skip to contentSkip to conversation

media: dvb-frontends: avoid stack overflow warnings with clang

PATCH In review Arnd Bergmann 3 participants 3 messages Feb 16, 2024 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: dvb-frontends: avoid stack overflow warnings with clang

From: Arnd Bergmann <[email protected]>

A previous patch worked around a KASAN issue in stv0367, now a similar problem showed up with clang:

drivers/media/dvb-frontends/stv0367.c:1222:12: error: stack frame size (3624) exceeds limit (2048) in 'stv0367ter_set_frontend' [-Werror,-Wframe-larger-than]
1214 | static int stv0367ter_set_frontend(struct dvb_frontend *fe)

Rework the stv0367_writereg() function to be simpler and mark both register access functions as noinline_for_stack so the temporary i2c_msg structures do not get duplicated on the stack when KASAN_STACK is enabled.

Fixes 3cd890dbe2a4 ("media: dvb-frontends: fix i2c access helpers for KASAN")
---
 drivers/media/dvb-frontends/stv0367.c | 34 +++++++--------------------
 1 file changed, 8 insertions(+), 26 deletions(-)
drivers/media/dvb-frontends/stv0367.c +8 -26
index 48326434488c..72540ef4e5f8 100644
@@ -118,50 +118,32 @@ static const s32 stv0367cab_RF_LookUp2[RF_LOOKUP_TABLE2_SIZE][RF_LOOKUP_TABLE2_S
 	}
 };
 
-static
-int stv0367_writeregs(struct stv0367_state *state, u16 reg, u8 *data, int len)
+static noinline_for_stack
+int stv0367_writereg(struct stv0367_state *state, u16 reg, u8 data)
 {
-	u8 buf[MAX_XFER_SIZE];
+	u8 buf[3] = { MSB(reg), LSB(reg), data };
 	struct i2c_msg msg = {
 		.addr = state->config->demod_address,
 		.flags = 0,
 		.buf = buf,
-		.len = len + 2
+		.len = 3,
 	};
 	int ret;
 
-	if (2 + len > sizeof(buf)) {
-		printk(KERN_WARNING
-		       "%s: i2c wr reg=%04x: len=%d is too big!\n",
-		       KBUILD_MODNAME, reg, len);
-		return -EINVAL;
-	}
-
-
-	buf[0] = MSB(reg);
-	buf[1] = LSB(reg);
-	memcpy(buf + 2, data, len);
-
 	if (i2cdebug)
 		printk(KERN_DEBUG "%s: [%02x] %02x: %02x\n", __func__,
-			state->config->demod_address, reg, buf[2]);
+			state->config->demod_address, reg, data);
 
 	ret = i2c_transfer(state->i2c, &msg, 1);
 	if (ret != 1)
 		printk(KERN_ERR "%s: i2c write error! ([%02x] %02x: %02x)\n",
-			__func__, state->config->demod_address, reg, buf[2]);
+			__func__, state->config->demod_address, reg, data);
 
 	return (ret != 1) ? -EREMOTEIO : 0;
 }
 
-static int stv0367_writereg(struct stv0367_state *state, u16 reg, u8 data)
-{
-	u8 tmp = data; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
-
-	return stv0367_writeregs(state, reg, &tmp, 1);
-}
-
-static u8 stv0367_readreg(struct stv0367_state *state, u16 reg)
+static noinline_for_stack
+u8 stv0367_readreg(struct stv0367_state *state, u16 reg)
 {
 	u8 b0[] = { 0, 0 };
 	u8 b1[] = { 0 };
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 45 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

A previous patch worked around a KASAN issue in stv0367, now a similar problem showed up with clang:

drivers/media/dvb-frontends/stv0367.c:1222:12: error: stack frame size (3624) exceeds limit (2048) in 'stv0367ter_set_frontend' [-Werror,-Wframe-larger-than]
1214 | static int stv0367ter_set_frontend(struct dvb_frontend *fe)

Rework the stv0367_writereg() function to be simpler and mark both register access functions as noinline_for_stack so the temporary i2c_msg structures do not get duplicated on the stack when KASAN_STACK is enabled.

Fixes 3cd890dbe2a4 ("media: dvb-frontends: fix i2c access helpers for KASAN")
---
 drivers/media/dvb-frontends/stv0367.c | 34 +++++++--------------------
 1 file changed, 8 insertions(+), 26 deletions(-)
drivers/media/dvb-frontends/stv0367.c +4 -5
index 48326434488c..72540ef4e5f8 100644
@@ -118,50 +118,32 @@ static const s32 stv0367cab_RF_LookUp2[RF_LOOKUP_TABLE2_SIZE][RF_LOOKUP_TABLE2_S
        }
 };
 
-static
-int stv0367_writeregs(struct stv0367_state *state, u16 reg, u8 *data, int len)
+static noinline_for_stack
+int stv0367_writereg(struct stv0367_state *state, u16 reg, u8 data)
 {
-       u8 buf[MAX_XFER_SIZE];
+       u8 buf[3] = { MSB(reg), LSB(reg), data };
        struct i2c_msg msg = {
                .addr = state->config->demod_address,
                .flags = 0,
                .buf = buf,
-               .len = len + 2
+               .len = 3,
        };
        int ret;
 
-       if (2 + len > sizeof(buf)) {

drivers/media/dvb-frontends/stv0367.c static const s32 stv0367cab_RF_LookUp2[RF_LOOKUP_TABLE2_SIZE][RF_LOOKUP_TABLE2_S · line 143

Quoted 10 lines
drivers/media/dvb-frontends/stv0367.c +0 -10
-               printk(KERN_WARNING
-                      "%s: i2c wr reg=%04x: len=%d is too big!\n",
-                      KBUILD_MODNAME, reg, len);
-               return -EINVAL;
-       }
-
-
-       buf[0] = MSB(reg);
-       buf[1] = LSB(reg);
-       memcpy(buf + 2, data, len);

I'm curious why a copy was made at all.

Quoted 29 lines
drivers/media/dvb-frontends/stv0367.c +4 -12
-
        if (i2cdebug)
                printk(KERN_DEBUG "%s: [%02x] %02x: %02x\n", __func__,
-                       state->config->demod_address, reg, buf[2]);
+                       state->config->demod_address, reg, data);
 
        ret = i2c_transfer(state->i2c, &msg, 1);
        if (ret != 1)
                printk(KERN_ERR "%s: i2c write error! ([%02x] %02x: %02x)\n",
-                       __func__, state->config->demod_address, reg, buf[2]);
+                       __func__, state->config->demod_address, reg, data);
 
        return (ret != 1) ? -EREMOTEIO : 0;
 }
 
-static int stv0367_writereg(struct stv0367_state *state, u16 reg, u8 data)
-{
-       u8 tmp = data; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
-
-       return stv0367_writeregs(state, reg, &tmp, 1);
-}
-
-static u8 stv0367_readreg(struct stv0367_state *state, u16 reg)
+static noinline_for_stack
+u8 stv0367_readreg(struct stv0367_state *state, u16 reg)
 {
        u8 b0[] = { 0, 0 };
        u8 b1[] = { 0 };
--
Quoted 2 lines

2.39.2

Thanks
Justin

#2 lore raw
Quoted 9 lines · Justin Stitt

drivers/media/dvb-frontends/stv0367.c static const s32 stv0367cab_RF_LookUp2[RF_LOOKUP_TABLE2_SIZE][RF_LOOKUP_TABLE2_S · line 143

Quoted 4 lines · Arnd Bergmann
drivers/media/dvb-frontends/stv0367.c +0 -4
-
-       buf[0] = MSB(reg);
-       buf[1] = LSB(reg);
-       memcpy(buf + 2, data, len);

I'm curious why a copy was made at all.

I guess the idea was to allow writing arbitrarily long contents to a single register with a single decriptor.

The current version of the driver only ever writes a single byte value, but maybe it either used to have other writes, or it was meant to support them later but never did.

Arnd