Skip to contentSkip to conversation

tuners: tda8290: reduce stack usage with kasan

PATCH In review Arnd Bergmann 4 participants 9 messages Dec 11, 2017 – Dec 12, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tuners: tda8290: reduce stack usage with kasan

With CONFIG_KASAN enabled, we get a relatively large stack frame in one function

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: warning: the frame size of 1520 bytes is larger than 1024 bytes [-Wframe-larger-than=]

With CONFIG_KASAN_EXTRA this goes up to

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: error: the frame size of 3200 bytes is larger than 3072 bytes [-Werror=frame-larger-than=]

We can significantly reduce this by marking local arrays as 'static const', and this should result in better compiled code for everyone.

I have another patch for the same symptom to patch tuner_i2c_xfer_*, and we actually want both of them.

---
 drivers/media/tuners/tda8290.c | 76 ++++++++++++++++++++++--------------------
 1 file changed, 39 insertions(+), 37 deletions(-)
drivers/media/tuners/tda8290.c +39 -37
index a59c567c55d6..19854221b72d 100644
@@ -63,8 +63,8 @@ static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
 {
 	struct tda8290_priv *priv = fe->analog_demod_priv;
 
-	unsigned char  enable[2] = { 0x21, 0xC0 };
-	unsigned char disable[2] = { 0x21, 0x00 };
+	static unsigned char  enable[2] = { 0x21, 0xC0 };
+	static unsigned char disable[2] = { 0x21, 0x00 };
 	unsigned char *msg;
 
 	if (close) {
@@ -84,9 +84,9 @@ static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
 {
 	struct tda8290_priv *priv = fe->analog_demod_priv;
 
-	unsigned char  enable[2] = { 0x45, 0xc1 };
-	unsigned char disable[2] = { 0x46, 0x00 };
-	unsigned char buf[3] = { 0x45, 0x01, 0x00 };
+	static unsigned char  enable[2] = { 0x45, 0xc1 };
+	static unsigned char disable[2] = { 0x46, 0x00 };
+	static unsigned char buf[3] = { 0x45, 0x01, 0x00 };
 	unsigned char *msg;
 
 	if (close) {
@@ -178,24 +178,24 @@ static void tda8290_set_params(struct dvb_frontend *fe,
 {
 	struct tda8290_priv *priv = fe->analog_demod_priv;
 
-	unsigned char soft_reset[]  = { 0x00, 0x00 };
+	static unsigned char soft_reset[]  = { 0x00, 0x00 };
 	unsigned char easy_mode[]   = { 0x01, priv->tda8290_easy_mode };
-	unsigned char expert_mode[] = { 0x01, 0x80 };
-	unsigned char agc_out_on[]  = { 0x02, 0x00 };
-	unsigned char gainset_off[] = { 0x28, 0x14 };
-	unsigned char if_agc_spd[]  = { 0x0f, 0x88 };
-	unsigned char adc_head_6[]  = { 0x05, 0x04 };
-	unsigned char adc_head_9[]  = { 0x05, 0x02 };
-	unsigned char adc_head_12[] = { 0x05, 0x01 };
-	unsigned char pll_bw_nom[]  = { 0x0d, 0x47 };
-	unsigned char pll_bw_low[]  = { 0x0d, 0x27 };
-	unsigned char gainset_2[]   = { 0x28, 0x64 };
-	unsigned char agc_rst_on[]  = { 0x0e, 0x0b };
-	unsigned char agc_rst_off[] = { 0x0e, 0x09 };
-	unsigned char if_agc_set[]  = { 0x0f, 0x81 };
-	unsigned char addr_adc_sat  = 0x1a;
-	unsigned char addr_agc_stat = 0x1d;
-	unsigned char addr_pll_stat = 0x1b;
+	static unsigned char expert_mode[] = { 0x01, 0x80 };
+	static unsigned char agc_out_on[]  = { 0x02, 0x00 };
+	static unsigned char gainset_off[] = { 0x28, 0x14 };
+	static unsigned char if_agc_spd[]  = { 0x0f, 0x88 };
+	static unsigned char adc_head_6[]  = { 0x05, 0x04 };
+	static unsigned char adc_head_9[]  = { 0x05, 0x02 };
+	static unsigned char adc_head_12[] = { 0x05, 0x01 };
+	static unsigned char pll_bw_nom[]  = { 0x0d, 0x47 };
+	static unsigned char pll_bw_low[]  = { 0x0d, 0x27 };
+	static unsigned char gainset_2[]   = { 0x28, 0x64 };
+	static unsigned char agc_rst_on[]  = { 0x0e, 0x0b };
+	static unsigned char agc_rst_off[] = { 0x0e, 0x09 };
+	static unsigned char if_agc_set[]  = { 0x0f, 0x81 };
+	static unsigned char addr_adc_sat  = 0x1a;
+	static unsigned char addr_agc_stat = 0x1d;
+	static unsigned char addr_pll_stat = 0x1b;
 	unsigned char adc_sat, agc_stat,
 		      pll_stat;
 	int i;
@@ -468,9 +468,9 @@ static void tda8290_standby(struct dvb_frontend *fe)
 {
 	struct tda8290_priv *priv = fe->analog_demod_priv;
 
-	unsigned char cb1[] = { 0x30, 0xD0 };
-	unsigned char tda8290_standby[] = { 0x00, 0x02 };
-	unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
+	static unsigned char cb1[] = { 0x30, 0xD0 };
+	static unsigned char tda8290_standby[] = { 0x00, 0x02 };
+	static unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
 	struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
 
 	if (fe->ops.analog_ops.i2c_gate_ctrl)
@@ -495,9 +495,9 @@ static void tda8290_init_if(struct dvb_frontend *fe)
 {
 	struct tda8290_priv *priv = fe->analog_demod_priv;
 
-	unsigned char set_VS[] = { 0x30, 0x6F };
-	unsigned char set_GP00_CF[] = { 0x20, 0x01 };
-	unsigned char set_GP01_CF[] = { 0x20, 0x0B };
+	static unsigned char set_VS[] = { 0x30, 0x6F };
+	static unsigned char set_GP00_CF[] = { 0x20, 0x01 };
+	static unsigned char set_GP01_CF[] = { 0x20, 0x0B };
 
 	if ((priv->cfg.config == TDA8290_LNA_GP0_HIGH_ON) ||
 	    (priv->cfg.config == TDA8290_LNA_GP0_HIGH_OFF))
@@ -539,10 +539,12 @@ static void tda8295_init_if(struct dvb_frontend *fe)
 static void tda8290_init_tuner(struct dvb_frontend *fe)
 {
 	struct tda8290_priv *priv = fe->analog_demod_priv;
-	unsigned char tda8275_init[]  = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
-					  0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
-	unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
-					  0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
+	static unsigned char tda8275_init[]  =
+		{ 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
+		  0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
+	static unsigned char tda8275a_init[] =
+		 { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
+		   0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
 	struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
 			      .buf=tda8275_init, .len = 14};
 	if (priv->ver & TDA8275A)
@@ -834,11 +836,11 @@ int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
 		.addr = i2c_addr,
 	};
 
-	unsigned char soft_reset[]   = { 0x00, 0x00 };
-	unsigned char easy_mode_b[]  = { 0x01, 0x02 };
-	unsigned char easy_mode_g[]  = { 0x01, 0x04 };
-	unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
-	unsigned char addr_dto_lsb = 0x07;
+	static unsigned char soft_reset[]   = { 0x00, 0x00 };
+	static unsigned char easy_mode_b[]  = { 0x01, 0x02 };
+	static unsigned char easy_mode_g[]  = { 0x01, 0x04 };
+	static unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
+	static unsigned char addr_dto_lsb = 0x07;
 	unsigned char data;
 #define PROBE_BUFFER_SIZE 8
 	unsigned char buf[PROBE_BUFFER_SIZE];
Signature · 1 line
2.9.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Arnd Bergmann

With CONFIG_KASAN enabled, we get a relatively large stack frame in one function

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: warning: the frame size of 1520 bytes is larger than 1024 bytes [-Wframe-larger-than=]

With CONFIG_KASAN_EXTRA this goes up to

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: error: the frame size of 3200 bytes is larger than 3072 bytes [-Werror=frame-larger-than=]

Quoted 7 lines

We can significantly reduce this by marking local arrays as 'static const', and this should result in better compiled code for everyone.

I have another patch for the same symptom to patch tuner_i2c_xfer_*, and we actually want both of them.

Thank you, Arnd.

Quoted 137 lines
---
 drivers/media/tuners/tda8290.c | 76 ++++++++++++++++++++++--------------------
 1 file changed, 39 insertions(+), 37 deletions(-)
drivers/media/tuners/tda8290.c +39 -38
index a59c567c55d6..19854221b72d 100644
@@ -63,8 +63,8 @@ static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
 {
        struct tda8290_priv *priv = fe->analog_demod_priv;
 
-       unsigned char  enable[2] = { 0x21, 0xC0 };
-       unsigned char disable[2] = { 0x21, 0x00 };
+       static unsigned char  enable[2] = { 0x21, 0xC0 };
+       static unsigned char disable[2] = { 0x21, 0x00 };
        unsigned char *msg;
 
        if (close) {
@@ -84,9 +84,9 @@ static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
 {
        struct tda8290_priv *priv = fe->analog_demod_priv;
 
-       unsigned char  enable[2] = { 0x45, 0xc1 };
-       unsigned char disable[2] = { 0x46, 0x00 };
-       unsigned char buf[3] = { 0x45, 0x01, 0x00 };
+       static unsigned char  enable[2] = { 0x45, 0xc1 };
+       static unsigned char disable[2] = { 0x46, 0x00 };
+       static unsigned char buf[3] = { 0x45, 0x01, 0x00 };
        unsigned char *msg;
 
        if (close) {
@@ -178,24 +178,24 @@ static void tda8290_set_params(struct dvb_frontend *fe,
 {
        struct tda8290_priv *priv = fe->analog_demod_priv;
 
-       unsigned char soft_reset[]  = { 0x00, 0x00 };
+       static unsigned char soft_reset[]  = { 0x00, 0x00 };
        unsigned char easy_mode[]   = { 0x01, priv->tda8290_easy_mode };
-       unsigned char expert_mode[] = { 0x01, 0x80 };
-       unsigned char agc_out_on[]  = { 0x02, 0x00 };
-       unsigned char gainset_off[] = { 0x28, 0x14 };
-       unsigned char if_agc_spd[]  = { 0x0f, 0x88 };
-       unsigned char adc_head_6[]  = { 0x05, 0x04 };
-       unsigned char adc_head_9[]  = { 0x05, 0x02 };
-       unsigned char adc_head_12[] = { 0x05, 0x01 };
-       unsigned char pll_bw_nom[]  = { 0x0d, 0x47 };
-       unsigned char pll_bw_low[]  = { 0x0d, 0x27 };
-       unsigned char gainset_2[]   = { 0x28, 0x64 };
-       unsigned char agc_rst_on[]  = { 0x0e, 0x0b };
-       unsigned char agc_rst_off[] = { 0x0e, 0x09 };
-       unsigned char if_agc_set[]  = { 0x0f, 0x81 };
-       unsigned char addr_adc_sat  = 0x1a;
-       unsigned char addr_agc_stat = 0x1d;
-       unsigned char addr_pll_stat = 0x1b;
+       static unsigned char expert_mode[] = { 0x01, 0x80 };
+       static unsigned char agc_out_on[]  = { 0x02, 0x00 };
+       static unsigned char gainset_off[] = { 0x28, 0x14 };
+       static unsigned char if_agc_spd[]  = { 0x0f, 0x88 };
+       static unsigned char adc_head_6[]  = { 0x05, 0x04 };
+       static unsigned char adc_head_9[]  = { 0x05, 0x02 };
+       static unsigned char adc_head_12[] = { 0x05, 0x01 };
+       static unsigned char pll_bw_nom[]  = { 0x0d, 0x47 };
+       static unsigned char pll_bw_low[]  = { 0x0d, 0x27 };
+       static unsigned char gainset_2[]   = { 0x28, 0x64 };
+       static unsigned char agc_rst_on[]  = { 0x0e, 0x0b };
+       static unsigned char agc_rst_off[] = { 0x0e, 0x09 };
+       static unsigned char if_agc_set[]  = { 0x0f, 0x81 };
+       static unsigned char addr_adc_sat  = 0x1a;
+       static unsigned char addr_agc_stat = 0x1d;
+       static unsigned char addr_pll_stat = 0x1b;
        unsigned char adc_sat, agc_stat,
                      pll_stat;
        int i;
@@ -468,9 +468,9 @@ static void tda8290_standby(struct dvb_frontend *fe)
 {
        struct tda8290_priv *priv = fe->analog_demod_priv;
 
-       unsigned char cb1[] = { 0x30, 0xD0 };
-       unsigned char tda8290_standby[] = { 0x00, 0x02 };
-       unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
+       static unsigned char cb1[] = { 0x30, 0xD0 };
+       static unsigned char tda8290_standby[] = { 0x00, 0x02 };
+       static unsigned char tda8290_agc_tri[] = { 0x02, 0x20 };
        struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0, .buf=cb1, .len = 2};
 
        if (fe->ops.analog_ops.i2c_gate_ctrl)
@@ -495,9 +495,9 @@ static void tda8290_init_if(struct dvb_frontend *fe)
 {
        struct tda8290_priv *priv = fe->analog_demod_priv;
 
-       unsigned char set_VS[] = { 0x30, 0x6F };
-       unsigned char set_GP00_CF[] = { 0x20, 0x01 };
-       unsigned char set_GP01_CF[] = { 0x20, 0x0B };
+       static unsigned char set_VS[] = { 0x30, 0x6F };
+       static unsigned char set_GP00_CF[] = { 0x20, 0x01 };
+       static unsigned char set_GP01_CF[] = { 0x20, 0x0B };
 
        if ((priv->cfg.config == TDA8290_LNA_GP0_HIGH_ON) ||
            (priv->cfg.config == TDA8290_LNA_GP0_HIGH_OFF))
@@ -539,10 +539,12 @@ static void tda8295_init_if(struct dvb_frontend *fe)
 static void tda8290_init_tuner(struct dvb_frontend *fe)
 {
        struct tda8290_priv *priv = fe->analog_demod_priv;
-       unsigned char tda8275_init[]  = { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
-                                         0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
-       unsigned char tda8275a_init[] = { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
-                                         0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
+       static unsigned char tda8275_init[]  =
+               { 0x00, 0x00, 0x00, 0x40, 0xdC, 0x04, 0xAf,
+                 0x3F, 0x2A, 0x04, 0xFF, 0x00, 0x00, 0x40 };
+       static unsigned char tda8275a_init[] =
+                { 0x00, 0x00, 0x00, 0x00, 0xdC, 0x05, 0x8b,
+                  0x0c, 0x04, 0x20, 0xFF, 0x00, 0x00, 0x4b };
        struct i2c_msg msg = {.addr = priv->tda827x_addr, .flags=0,
                              .buf=tda8275_init, .len = 14};
        if (priv->ver & TDA8275A)
@@ -834,11 +836,11 @@ int tda829x_probe(struct i2c_adapter *i2c_adap, u8 i2c_addr)
                .addr = i2c_addr,
        };
 
-       unsigned char soft_reset[]   = { 0x00, 0x00 };
-       unsigned char easy_mode_b[]  = { 0x01, 0x02 };
-       unsigned char easy_mode_g[]  = { 0x01, 0x04 };
-       unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
-       unsigned char addr_dto_lsb = 0x07;
+       static unsigned char soft_reset[]   = { 0x00, 0x00 };
+       static unsigned char easy_mode_b[]  = { 0x01, 0x02 };
+       static unsigned char easy_mode_g[]  = { 0x01, 0x04 };
+       static unsigned char restore_9886[] = { 0x00, 0xd6, 0x30 };
+       static unsigned char addr_dto_lsb = 0x07;
        unsigned char data;
 #define PROBE_BUFFER_SIZE 8
        unsigned char buf[PROBE_BUFFER_SIZE];
--

2.9.0

#2 lore raw
In reply to Arnd Bergmann
Quoted 5 lines · Arnd Bergmann

With CONFIG_KASAN enabled, we get a relatively large stack frame in one function

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: warning: the frame size of 1520 bytes is larger than 1024 bytes [-Wframe-larger-than=]

Quoted 7 lines

With CONFIG_KASAN_EXTRA this goes up to

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: error: the frame size of 3200 bytes is larger than 3072 bytes [-Werror=frame-larger-than=]

We can significantly reduce this by marking local arrays as 'static const', and this should result in better compiled code for everyone.

[]

drivers/media/tuners/tda8290.c

Quoted 1 line
drivers/media/tuners/tda8290.c +0 -0

[]

drivers/media/tuners/tda8290.c static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close) · line 67

Quoted 8 lines
drivers/media/tuners/tda8290.c +2 -2
@@ -63,8 +63,8 @@ static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
 {
 	struct tda8290_priv *priv = fe->analog_demod_priv;
 
-	unsigned char  enable[2] = { 0x21, 0xC0 };
-	unsigned char disable[2] = { 0x21, 0x00 };
+	static unsigned char  enable[2] = { 0x21, 0xC0 };
+	static unsigned char disable[2] = { 0x21, 0x00 };

Doesn't match commit message.

static const or just static?

drivers/media/tuners/tda8290.c static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close) · line 88

Quoted 9 lines
drivers/media/tuners/tda8290.c +2 -3
@@ -84,9 +84,9 @@ static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
 {
 	struct tda8290_priv *priv = fe->analog_demod_priv;
 
-	unsigned char  enable[2] = { 0x45, 0xc1 };
-	unsigned char disable[2] = { 0x46, 0x00 };
-	unsigned char buf[3] = { 0x45, 0x01, 0x00 };
+	static unsigned char  enable[2] = { 0x45, 0xc1 };
+	static unsigned char disable[2] = { 0x46, 0x00 };

etc.

#3 lore raw
Quoted 33 lines · Joe Perches
Quoted 5 lines · Arnd Bergmann

With CONFIG_KASAN enabled, we get a relatively large stack frame in one function

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: warning: the frame size of 1520 bytes is larger than 1024 bytes [-Wframe-larger-than=]

Quoted 7 lines

With CONFIG_KASAN_EXTRA this goes up to

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: error: the frame size of 3200 bytes is larger than 3072 bytes [-Werror=frame-larger-than=]

We can significantly reduce this by marking local arrays as 'static const', and this should result in better compiled code for everyone.

[]

drivers/media/tuners/tda8290.c

Quoted 1 line
drivers/media/tuners/tda8290.c +0 -0

[]

drivers/media/tuners/tda8290.c static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close) · line 67

Quoted 8 lines
drivers/media/tuners/tda8290.c +2 -2
@@ -63,8 +63,8 @@ static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
 {
      struct tda8290_priv *priv = fe->analog_demod_priv;
 
-     unsigned char  enable[2] = { 0x21, 0xC0 };
-     unsigned char disable[2] = { 0x21, 0x00 };
+     static unsigned char  enable[2] = { 0x21, 0xC0 };
+     static unsigned char disable[2] = { 0x21, 0x00 };

Doesn't match commit message.

static const or just static?

Quoted 4 lines
drivers/media/tuners/tda8290.c +0 -0
@@ -84,9 +84,9 @@ static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
 {
      struct tda8290_priv *priv = fe->analog_demod_priv;
 
Quoted 9 lines

drivers/media/tuners/tda8290.c static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close) · line 88

Quoted 5 lines
drivers/media/tuners/tda8290.c +2 -3
-     unsigned char  enable[2] = { 0x45, 0xc1 };
-     unsigned char disable[2] = { 0x46, 0x00 };
-     unsigned char buf[3] = { 0x45, 0x01, 0x00 };
+     static unsigned char  enable[2] = { 0x45, 0xc1 };
+     static unsigned char disable[2] = { 0x46, 0x00 };

etc.

Joe is correct - they can be CONSTified. My bad -- a lot of the code I wrote many years ago has this problem -- I wasn't so stack-conscious back then.

The bytes in enable / disable don't get changed, but they may be copied to another byte array that does get changed. If would be best to make these static const

Best regards,

Michael Ira Krufky

#4 lore raw
Quoted 45 lines · Michael Ira Krufky
Quoted 33 lines · Joe Perches
Quoted 5 lines · Arnd Bergmann

With CONFIG_KASAN enabled, we get a relatively large stack frame in one function

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: warning: the frame size of 1520 bytes is larger than 1024 bytes [-Wframe-larger-than=]

Quoted 7 lines

With CONFIG_KASAN_EXTRA this goes up to

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: error: the frame size of 3200 bytes is larger than 3072 bytes [-Werror=frame-larger-than=]

We can significantly reduce this by marking local arrays as 'static const', and this should result in better compiled code for everyone.

[]

drivers/media/tuners/tda8290.c

Quoted 1 line
drivers/media/tuners/tda8290.c +0 -0

[]

drivers/media/tuners/tda8290.c static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close) · line 67

Quoted 8 lines
drivers/media/tuners/tda8290.c +2 -2
@@ -63,8 +63,8 @@ static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
 {
      struct tda8290_priv *priv = fe->analog_demod_priv;
 
-     unsigned char  enable[2] = { 0x21, 0xC0 };
-     unsigned char disable[2] = { 0x21, 0x00 };
+     static unsigned char  enable[2] = { 0x21, 0xC0 };
+     static unsigned char disable[2] = { 0x21, 0x00 };

Doesn't match commit message.

static const or just static?

Quoted 4 lines
drivers/media/tuners/tda8290.c +0 -0
@@ -84,9 +84,9 @@ static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
 {
      struct tda8290_priv *priv = fe->analog_demod_priv;
 
Quoted 9 lines

drivers/media/tuners/tda8290.c static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close) · line 88

Quoted 5 lines
drivers/media/tuners/tda8290.c +2 -3
-     unsigned char  enable[2] = { 0x45, 0xc1 };
-     unsigned char disable[2] = { 0x46, 0x00 };
-     unsigned char buf[3] = { 0x45, 0x01, 0x00 };
+     static unsigned char  enable[2] = { 0x45, 0xc1 };
+     static unsigned char disable[2] = { 0x46, 0x00 };

etc.

Quoted 7 lines

Joe is correct - they can be CONSTified. My bad -- a lot of the code I wrote many years ago has this problem -- I wasn't so stack-conscious back then.

The bytes in enable / disable don't get changed, but they may be copied to another byte array that does get changed. If would be best to make these static const

Right. This was an older patch of mine that I picked up again after running into a warning that I had been ignoring for a while, and I didn't double-check the message.

I actually thought about marking them 'const' here before sending (without noticing the changelog text) and then ran into what must have led me to drop the 'const' originally: tuner_i2c_xfer_send() takes a non-const pointer. This can be fixed but it requires an ugly cast:

drivers/media/tuners/tuner-i2c.h +2 -2
index bda67a5a76f2..809466eec780 100644
@@ -34,10 +34,10 @@ struct tuner_i2c_props {
 };
 
 static inline int tuner_i2c_xfer_send(struct tuner_i2c_props *props,
-                                     unsigned char *buf, int len)
+                                     const unsigned char *buf, int len)
 {
        struct i2c_msg msg = { .addr = props->addr, .flags = 0,
-                              .buf = buf, .len = len };
+                              .buf = (unsigned char *)buf, .len = len };
        int ret = i2c_transfer(props->adap, &msg, 1);
 
        return (ret == 1) ? len : ret;
@@ -54,11 +54,11 @@ static inline int tuner_i2c_xfer_recv(struct

tuner_i2c_props *props,
}

static inline int tuner_i2c_xfer_send_recv(struct tuner_i2c_props *props,
- unsigned char *obuf, int olen,
+ const unsigned char *obuf, int olen,
unsigned char *ibuf, int ilen)

 {
        struct i2c_msg msg[2] = { { .addr = props->addr, .flags = 0,
-                                   .buf = obuf, .len = olen },
+                                   .buf = (unsigned char *)obuf, .len = olen },
                                  { .addr = props->addr, .flags = I2C_M_RD,
                                    .buf = ibuf, .len = ilen } };
        int ret = i2c_transfer(props->adap, msg, 2);

Should I submit it as a two-patch series with that added in, or update the changelog to not mention 'const' instead?

Arnd

#5 lore raw
Quoted 61 lines · Arnd Bergmann
Quoted 47 lines · Michael Ira Krufky
Quoted 35 lines · Joe Perches
Quoted 5 lines · Arnd Bergmann

With CONFIG_KASAN enabled, we get a relatively large stack frame in one function

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: warning: the frame size of 1520 bytes is larger than 1024 bytes [-Wframe-larger-than=]

Quoted 7 lines

With CONFIG_KASAN_EXTRA this goes up to

drivers/media/tuners/tda8290.c: In function 'tda8290_set_params':
drivers/media/tuners/tda8290.c:310:1: error: the frame size of 3200 bytes is larger than 3072 bytes [-Werror=frame-larger-than=]

We can significantly reduce this by marking local arrays as 'static const', and this should result in better compiled code for everyone.

[]

drivers/media/tuners/tda8290.c

Quoted 1 line
drivers/media/tuners/tda8290.c +0 -0

[]

drivers/media/tuners/tda8290.c static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close) · line 67

Quoted 8 lines
drivers/media/tuners/tda8290.c +2 -2
@@ -63,8 +63,8 @@ static int tda8290_i2c_bridge(struct dvb_frontend *fe, int close)
 {
      struct tda8290_priv *priv = fe->analog_demod_priv;
 
-     unsigned char  enable[2] = { 0x21, 0xC0 };
-     unsigned char disable[2] = { 0x21, 0x00 };
+     static unsigned char  enable[2] = { 0x21, 0xC0 };
+     static unsigned char disable[2] = { 0x21, 0x00 };

Doesn't match commit message.

static const or just static?

Quoted 4 lines
drivers/media/tuners/tda8290.c +0 -0
@@ -84,9 +84,9 @@ static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close)
 {
      struct tda8290_priv *priv = fe->analog_demod_priv;
 
Quoted 9 lines

drivers/media/tuners/tda8290.c static int tda8295_i2c_bridge(struct dvb_frontend *fe, int close) · line 88

Quoted 5 lines
drivers/media/tuners/tda8290.c +2 -3
-     unsigned char  enable[2] = { 0x45, 0xc1 };
-     unsigned char disable[2] = { 0x46, 0x00 };
-     unsigned char buf[3] = { 0x45, 0x01, 0x00 };
+     static unsigned char  enable[2] = { 0x45, 0xc1 };
+     static unsigned char disable[2] = { 0x46, 0x00 };

etc.

Quoted 7 lines

Joe is correct - they can be CONSTified. My bad -- a lot of the code I wrote many years ago has this problem -- I wasn't so stack-conscious back then.

The bytes in enable / disable don't get changed, but they may be copied to another byte array that does get changed. If would be best to make these static const

Right. This was an older patch of mine that I picked up again after running into a warning that I had been ignoring for a while, and I didn't double-check the message.

Quoted 5 lines

I actually thought about marking them 'const' here before sending (without noticing the changelog text) and then ran into what must have led me to drop the 'const' originally: tuner_i2c_xfer_send() takes a non-const pointer. This can be fixed but it requires an ugly cast:

Casting away const is always a horrible hack.

Until it could be changed, my preference would
be to update the changelog and perhaps add to
the changelog the reason why it can not be const
as detailed below.

ie: xfer_send and xfer_xend_recv both take a
non-const unsigned char *

Quoted 37 lines
drivers/media/tuners/tuner-i2c.h +4 -4
index bda67a5a76f2..809466eec780 100644
@@ -34,10 +34,10 @@ struct tuner_i2c_props {
 };
 
 static inline int tuner_i2c_xfer_send(struct tuner_i2c_props *props,
-                                     unsigned char *buf, int len)
+                                     const unsigned char *buf, int len)
 {
        struct i2c_msg msg = { .addr = props->addr, .flags = 0,
-                              .buf = buf, .len = len };
+                              .buf = (unsigned char *)buf, .len = len };
        int ret = i2c_transfer(props->adap, &msg, 1);
 
        return (ret == 1) ? len : ret;
@@ -54,11 +54,11 @@ static inline int tuner_i2c_xfer_recv(struct
 tuner_i2c_props *props,
 }
 
 static inline int tuner_i2c_xfer_send_recv(struct tuner_i2c_props *props,
-                                          unsigned char *obuf, int olen,
+                                          const unsigned char *obuf, int olen,
                                           unsigned char *ibuf, int ilen)
 {
        struct i2c_msg msg[2] = { { .addr = props->addr, .flags = 0,
-                                   .buf = obuf, .len = olen },
+                                   .buf = (unsigned char *)obuf, .len = olen },
                                  { .addr = props->addr, .flags = I2C_M_RD,
                                    .buf = ibuf, .len = ilen } };
        int ret = i2c_transfer(props->adap, msg, 2);

Should I submit it as a two-patch series with that added in, or update the changelog to not mention 'const' instead?

Arnd

#6 lore raw

Em Tue, 12 Dec 2017 03:42:32 -0800
Joe Perches <[email protected]> escreveu:

Quoted 8 lines
Quoted 5 lines

I actually thought about marking them 'const' here before sending (without noticing the changelog text) and then ran into what must have led me to drop the 'const' originally: tuner_i2c_xfer_send() takes a non-const pointer. This can be fixed but it requires an ugly cast:

Casting away const is always a horrible hack.

Quoted 7 lines

Until it could be changed, my preference would
be to update the changelog and perhaps add to
the changelog the reason why it can not be const
as detailed below.

ie: xfer_send and xfer_xend_recv both take a
non-const unsigned char *

Perhaps, on a separate changeset, we could change I2C routines to accept const unsigned char pointers. This is unrelated to tda8290 KASAN fixes. So, it should go via I2C tree, and, once accepted there, we can change V4L2 drivers (and other drivers) accordingly.

Thanks,
Mauro

#7 lore raw
Quoted 18 lines · Mauro Carvalho Chehab

Em Tue, 12 Dec 2017 03:42:32 -0800
Joe Perches <[email protected]> escreveu:

Quoted 15 lines
Quoted 5 lines

I actually thought about marking them 'const' here before sending (without noticing the changelog text) and then ran into what must have led me to drop the 'const' originally: tuner_i2c_xfer_send() takes a non-const pointer. This can be fixed but it requires an ugly cast:

Casting away const is always a horrible hack.

Until it could be changed, my preference would
be to update the changelog and perhaps add to
the changelog the reason why it can not be const
as detailed below.

ie: xfer_send and xfer_xend_recv both take a
non-const unsigned char *

Ok.

Quoted 4 lines

Perhaps, on a separate changeset, we could change I2C routines to accept const unsigned char pointers. This is unrelated to tda8290 KASAN fixes. So, it should go via I2C tree, and, once accepted there, we can change V4L2 drivers (and other drivers) accordingly.

I don't see how that would work unfortunately. i2c_msg contains a pointer to the data, and that is used for both input and output, including arrays like

        struct i2c_msg msgs[] = {
                {
                        .addr = dvo->slave_addr,
                        .flags = 0,
                        .len = 1,
                        .buf = &addr,
                },
                {
                        .addr = dvo->slave_addr,
                        .flags = I2C_M_RD,
                        .len = 1,
                        .buf = val,
                }
        };

that have one constant output pointer and one non-constant input pointer. We could add an anonymous union for 'buf' to make that two separate pointers, but that's barely any better than the cast, and it would break the named initializers in the example above, at least on older compilers. Adding a second pointer to i2c_msg would add a bit of bloat and also require tree-wide changes or ugly hacks.

Arnd

#8 lore raw
Quoted 47 lines · Arnd Bergmann
Quoted 18 lines · Mauro Carvalho Chehab

Em Tue, 12 Dec 2017 03:42:32 -0800
Joe Perches <[email protected]> escreveu:

Quoted 15 lines
Quoted 5 lines

I actually thought about marking them 'const' here before sending (without noticing the changelog text) and then ran into what must have led me to drop the 'const' originally: tuner_i2c_xfer_send() takes a non-const pointer. This can be fixed but it requires an ugly cast:

Casting away const is always a horrible hack.

Until it could be changed, my preference would
be to update the changelog and perhaps add to
the changelog the reason why it can not be const
as detailed below.

ie: xfer_send and xfer_xend_recv both take a
non-const unsigned char *

Ok.

Quoted 4 lines

Perhaps, on a separate changeset, we could change I2C routines to accept const unsigned char pointers. This is unrelated to tda8290 KASAN fixes. So, it should go via I2C tree, and, once accepted there, we can change V4L2 drivers (and other drivers) accordingly.

I don't see how that would work unfortunately. i2c_msg contains a pointer to the data, and that is used for both input and output, including arrays like

        struct i2c_msg msgs[] = {
                {
                        .addr = dvo->slave_addr,
                        .flags = 0,
                        .len = 1,
                        .buf = &addr,
                },
                {
                        .addr = dvo->slave_addr,
                        .flags = I2C_M_RD,
                        .len = 1,
                        .buf = val,
                }
        };
Quoted 7 lines

that have one constant output pointer and one non-constant input pointer. We could add an anonymous union for 'buf' to make that two separate pointers, but that's barely any better than the cast, and it would break the named initializers in the example above, at least on older compilers. Adding a second pointer to i2c_msg would add a bit of bloat and also require tree-wide changes or ugly hacks.

Perhaps add something like

struct i2c_msg_set {
	__u16 addr;		/* slave address			*/
	__u16 flags;
	__u16 len;		/* msg length				*/
	const __u8 *buf;	/* pointer to read-only msg data	*/
};
struct i2c_msg_get {
	__u16 addr;		/* slave address			*/
	__u16 flags;
	__u16 len;		/* msg length				*/
	__u8 *buf;		/* pointer to writeable msg data	*/
};

to the uapi include and use that where appropriate but where a write then read is done via a single i2c_msg array, it's not really feasible either.

Probably better to avoid any churn and just mark all these as static rather than static const.