Skip to contentSkip to conversation

iio: adc: make read-only const array config static

PATCH In review Colin Ian King 6 participants 9 messages Jul 14 – Jul 20

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

PATCH 1/1 iio: adc: make read-only const array config static

Don't populate the read-only const array config on the stack at run time, instead make it static.

---
 drivers/iio/adc/88pm886-gpadc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/adc/88pm886-gpadc.c +1 -1
index ff9bc5f06c18..938ba43c2c35 100644
@@ -279,7 +279,7 @@ static int pm886_gpadc_read_raw(struct iio_dev *iio, struct iio_chan_spec const
 
 static int pm886_gpadc_hw_enable(struct regmap *map)
 {
-	const u8 config[] = {
+	static const u8 config[] = {
 		PM886_GPADC_CONFIG1_EN_ALL,
 		PM886_GPADC_CONFIG2_EN_ALL,
 		PM886_GPADC_GND_DET2_EN,
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Colin Ian King

Don't populate the read-only const array config on the stack at run time, instead make it static.

...

In all patches like this it's always a bikeshedding possible of moving static data outside of a function. I have no strong opinion in these cases (when the data solely used by a single function), but in general it might give different readability experience (it's harder to notice static data in the local function definition block). So I leave this exercise to the maintainers of the respective pieces of the code.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 6 lines · On Tue, 14 Jul 2026 20:08:10 +0300 Andy Shevchenko…
Quoted 2 lines · Colin Ian King

Don't populate the read-only const array config on the stack at run time, instead make it static.

Quoted 9 lines

...

In all patches like this it's always a bikeshedding possible of moving static data outside of a function. I have no strong opinion in these cases (when the data solely used by a single function), but in general it might give different readability experience (it's harder to notice static data in the local function definition block). So I leave this exercise to the maintainers of the respective pieces of the code.

If global you start wondering where it is used....

There is also the fact that run-time initialisation of a 3 byte array on stack is very likely to generate faster code than using static data. Mostly because the access to the static data is likely to be a cache miss.

Then there are the architectures where just generating the address of the static data takes multiple instructions.
In that case the on-stack version may even be smaller.

David

#3 lore raw
In reply to Andy Shevchenko
Quoted 6 lines · Andy Shevchenko
Quoted 2 lines · Colin Ian King

Don't populate the read-only const array config on the stack at run time, instead make it static.

Quoted 9 lines

...

In all patches like this it's always a bikeshedding possible of moving static data outside of a function. I have no strong opinion in these cases (when the data solely used by a single function), but in general it might give different readability experience (it's harder to notice static data in the local function definition block). So I leave this exercise to the maintainers of the respective pieces of the code.

It's an interesting point...

At one point Smatch didn't track static variables and it used to generate occasional false positives. And it's like you say, those little "static" qualifiers are hard to spot in a wall of declaration text. I've never considered moving the declarations out of the function scope but it might be a good idea?

But in this case since this data is const, the static vs not-static doesn't affect flow analysis or readability.

regards,
dan carpenter

#4 lore raw
Quoted 2 lines · Dan Carpenter
Quoted 1 line · Andy Shevchenko

On Tue, Jul 14, 2026 at 05:50:12PM +0100, Colin Ian King wrote:

...

Quoted 8 lines
Quoted 7 lines

In all patches like this it's always a bikeshedding possible of moving static data outside of a function. I have no strong opinion in these cases (when the data solely used by a single function), but in general it might give different readability experience (it's harder to notice static data in the local function definition block). So I leave this exercise to the maintainers of the respective pieces of the code.

Quoted 7 lines

It's an interesting point...

At one point Smatch didn't track static variables and it used to generate occasional false positives. And it's like you say, those little "static" qualifiers are hard to spot in a wall of declaration text. I've never considered moving the declarations out of the function scope but it might be a good idea?

Maybe, as I said, I have no strong opinion here. I am all ears to hear what others think.

Quoted 2 lines

But in this case since this data is const, the static vs not-static doesn't affect flow analysis or readability.

It doesn't affect flow analysis, but we also have David's point about amount of data to be static may affect the code generation.

...

The main point I have is that stumbling over the 'static' in the definition block might rise some additional questions and slow down the understanding of the code (by reading).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 16 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter
Quoted 1 line · Andy Shevchenko

On Tue, Jul 14, 2026 at 05:50:12PM +0100, Colin Ian King wrote:

...

Quoted 10 lines
Quoted 7 lines

In all patches like this it's always a bikeshedding possible of moving static data outside of a function. I have no strong opinion in these cases (when the data solely used by a single function), but in general it might give different readability experience (it's harder to notice static data in the local function definition block). So I leave this exercise to the maintainers of the respective pieces of the code.

It's an interesting point...

Quoted 8 lines
Quoted 5 lines

At one point Smatch didn't track static variables and it used to generate occasional false positives. And it's like you say, those little "static" qualifiers are hard to spot in a wall of declaration text. I've never considered moving the declarations out of the function scope but it might be a good idea?

Maybe, as I said, I have no strong opinion here. I am all ears to hear what others think.

For small arrays it's a moot point if this type of change is useful, so I'm OK if it's not accepted. I appreciate the feedback.

Colin

Quoted 13 lines
Quoted 3 lines

But in this case since this data is const, the static vs not-static doesn't affect flow analysis or readability.

It doesn't affect flow analysis, but we also have David's point about amount of data to be static may affect the code generation.

...

The main point I have is that stumbling over the 'static' in the definition block might rise some additional questions and slow down the understanding of the code (by reading).

#6 lore raw
Quoted 4 lines · Colin King (gmail)
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter
Quoted 1 line · Andy Shevchenko

On Tue, Jul 14, 2026 at 05:50:12PM +0100, Colin Ian King wrote:

...

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 6 lines

In all patches like this it's always a bikeshedding possible of moving static data outside of a function. I have no strong opinion in these cases (when the data solely used by a single function), but in general it might give different readability experience (it's harder to notice static data in the local function definition block). So I leave this exercise to the maintainers of the respective pieces of the code.

It's an interesting point...

At one point Smatch didn't track static variables and it used to generate occasional false positives. And it's like you say, those little "static" qualifiers are hard to spot in a wall of declaration text. I've never considered moving the declarations out of the function scope but it might be a good idea?

Quoted 4 lines
Quoted 2 lines

Maybe, as I said, I have no strong opinion here. I am all ears to hear what others think.

For small arrays it's a moot point if this type of change is useful, so

Yep, it actually becomes a trade-off between generated code and existing data. With a small difference that static const may be located in ROM (important for the embedded devices with minimum available memory).

Quoted 1 line

I'm OK if it's not accepted. I appreciate the feedback.

Me too.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

But in this case since this data is const, the static vs not-static doesn't affect flow analysis or readability.

It doesn't affect flow analysis, but we also have David's point about amount of data to be static may affect the code generation.

...

Quoted 3 lines
Quoted 3 lines

The main point I have is that stumbling over the 'static' in the definition block might rise some additional questions and slow down the understanding of the code (by reading).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
Quoted 28 lines · On Wed, 15 Jul 2026 16:54:13 +0300 Andy Shevchenko…
Quoted 4 lines · Colin King (gmail)
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter
Quoted 1 line · Andy Shevchenko

On Tue, Jul 14, 2026 at 05:50:12PM +0100, Colin Ian King wrote:

...

Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
Quoted 6 lines

In all patches like this it's always a bikeshedding possible of moving static data outside of a function. I have no strong opinion in these cases (when the data solely used by a single function), but in general it might give different readability experience (it's harder to notice static data in the local function definition block). So I leave this exercise to the maintainers of the respective pieces of the code.

It's an interesting point...

At one point Smatch didn't track static variables and it used to generate occasional false positives. And it's like you say, those little "static" qualifiers are hard to spot in a wall of declaration text. I've never considered moving the declarations out of the function scope but it might be a good idea?

Quoted 4 lines
Quoted 2 lines

Maybe, as I said, I have no strong opinion here. I am all ears to hear what others think.

For small arrays it's a moot point if this type of change is useful, so

Quoted 7 lines

Yep, it actually becomes a trade-off between generated code and existing data. With a small difference that static const may be located in ROM (important for the embedded devices with minimum available memory).

Quoted 1 line

I'm OK if it's not accepted. I appreciate the feedback.

Me too.

I prefer not to think too much about the trade offs and just assume it is 'mostly' worth doing so add static everywhere like this unless it's a hot path.

Anyhow, I've picked this one up.

Jonathan

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

But in this case since this data is const, the static vs not-static doesn't affect flow analysis or readability.

It doesn't affect flow analysis, but we also have David's point about amount of data to be static may affect the code generation.

...

Quoted 3 lines
Quoted 3 lines

The main point I have is that stumbling over the 'static' in the definition block might rise some additional questions and slow down the understanding of the code (by reading).

reviewer
#8 lore raw
In reply to Colin Ian King
Quoted 3 lines · Colin Ian King

Don't populate the read-only const array config on the stack at run time, instead make it static.

Subject line should have 88pm886-gpadc in it. Can leave out config to keep it shorter.

iio: adc: 88pm886-gpadc: make read-only const array static