Skip to contentSkip to conversation

iio: core: WARN in case sample bits do not fit storage bits

PATCH Superseded Marek Vasut 4 participants 11 messages Mar 20, 2022 – Mar 22, 2022 IIO

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: core: WARN in case sample bits do not fit storage bits

Add runtime check to verify whether storagebits are at least as big as shifted realbits. This should help spot broken drivers which may set realbits + shift above storagebits.

Cc Andy Shevchenko <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Daniel Baluta <[email protected]>
Cc Jonathan Cameron <[email protected]>
---
 drivers/iio/industrialio-buffer.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/iio/industrialio-buffer.c +5 -0
index b078eb2f3c9de..bac1e994e7bef 100644
@@ -1629,6 +1629,11 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
 			if (channels[i].scan_index < 0)
 				continue;
 
+			/* Verify that sample bits fit into storage */
+			WARN_ON(channels[i].scan_type.storagebits <
+				channels[i].scan_type.realbits +
+				channels[i].scan_type.shift);
+
 			ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
 							 &channels[i]);
 			if (ret < 0)
Signature · 1 line
2.35.1
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 20 lines

-----Original Message-----
From: Marek Vasut <[email protected]>
Sent: Sunday, March 20, 2022 7:16 PM
To: [email protected]
Cc: Marek Vasut <[email protected]>; Andy Shevchenko <[email protected]>; Andy Shevchenko <[email protected]>; Daniel Baluta <[email protected]>; Jonathan Cameron <[email protected]>
Subject: [PATCH] iio: core: WARN in case sample bits do not fit storage bits

[External]

Add runtime check to verify whether storagebits are at least as big as shifted realbits. This should help spot broken drivers which may set realbits + shift above storagebits.

Quoted 6 lines
Cc Andy Shevchenko <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Daniel Baluta <[email protected]>
Cc Jonathan Cameron <[email protected]>

---

Quoted 24 lines
 drivers/iio/industrialio-buffer.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/iio/industrialio- renamed from drivers/iio/industrialio-buffer.c +0 -0

buffer.c
index b078eb2f3c9de..bac1e994e7bef 100644

drivers/iio/industrialio-buffer.c +5 -0
@@ -1629,6 +1629,11 @@ static int
 __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
 			if (channels[i].scan_index < 0)
 				continue;
 
+			/* Verify that sample bits fit into storage */
+			WARN_ON(channels[i].scan_type.storagebits <
+				channels[i].scan_type.realbits +
+				channels[i].scan_type.shift);
+
 			ret = iio_buffer_add_channel_sysfs(indio_dev,
 buffer,
drivers/iio/industrialio-buffer.c +0 -1
 							 &channels[i]);
 			if (ret < 0)
--

2.35.1

#2 lore raw
In reply to Marek Vasut
Quoted 3 lines · Marek Vasut

Add runtime check to verify whether storagebits are at least as big as shifted realbits. This should help spot broken drivers which may set realbits + shift above storagebits.

Thanks!

...

drivers/iio/industrialio-buffer.c static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer, · line 1635

Quoted 5 lines
drivers/iio/industrialio-buffer.c +4 -0
+			/* Verify that sample bits fit into storage */
+			WARN_ON(channels[i].scan_type.storagebits <
+				channels[i].scan_type.realbits +
+				channels[i].scan_type.shift);

Not sure WARN is a good level (it might be fatal on some setups and we won't that), besides the fact that we may use dev_WARN(). Perhaps dev_warn() would suffice?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 10 lines · Andy Shevchenko
Quoted 3 lines · Marek Vasut

Add runtime check to verify whether storagebits are at least as big as shifted realbits. This should help spot broken drivers which may set realbits + shift above storagebits.

Thanks!

...

Quoted 1 line
Quoted 7 lines

drivers/iio/industrialio-buffer.c static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer, · line 1635

Quoted 4 lines
drivers/iio/industrialio-buffer.c +4 -0
+			/* Verify that sample bits fit into storage */
+			WARN_ON(channels[i].scan_type.storagebits <
+				channels[i].scan_type.realbits +
+				channels[i].scan_type.shift);

Not sure WARN is a good level (it might be fatal on some setups and we won't that), besides the fact that we may use dev_WARN(). Perhaps dev_warn() would suffice?

I was actually thinking about BUG(), but that might crash existing systems. I think we want a strong indicator that something wrong is going on which must be fixed and the splat produced by WARN_ON() is a good indicator of that. It also does not crash existing systems, so even if existing users get a warning now, they won't get an unbootable system and can report that warning.

#4 lore raw
Quoted 18 lines · Marek Vasut
Quoted 9 lines · Andy Shevchenko
Quoted 3 lines · Marek Vasut

Add runtime check to verify whether storagebits are at least as big as shifted realbits. This should help spot broken drivers which may set realbits + shift above storagebits.

Thanks!

...

Quoted 7 lines

drivers/iio/industrialio-buffer.c static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer, · line 1635

Quoted 4 lines
drivers/iio/industrialio-buffer.c +4 -0
+			/* Verify that sample bits fit into storage */
+			WARN_ON(channels[i].scan_type.storagebits <
+				channels[i].scan_type.realbits +
+				channels[i].scan_type.shift);

Not sure WARN is a good level (it might be fatal on some setups and we won't that), besides the fact that we may use dev_WARN(). Perhaps dev_warn() would suffice?

Quoted 4 lines

I was actually thinking about BUG(), but that might crash existing systems. I think we want a strong indicator that something wrong is going on which must be fixed and the splat produced by WARN_ON() is a good indicator of that. It also does not crash existing systems,

It does crash _some_ of them, unfortunately.

Quoted 3 lines

so even if existing users get
a warning now, they won't get an unbootable system and can report that warning.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 19 lines · Andy Shevchenko
Quoted 18 lines · Marek Vasut
Quoted 16 lines · Andy Shevchenko
Quoted 3 lines · Marek Vasut

Add runtime check to verify whether storagebits are at least as big as shifted realbits. This should help spot broken drivers which may set realbits + shift above storagebits.

Thanks!

...

drivers/iio/industrialio-buffer.c static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer, · line 1635

Quoted 4 lines
drivers/iio/industrialio-buffer.c +4 -0
+			/* Verify that sample bits fit into storage */
+			WARN_ON(channels[i].scan_type.storagebits <
+				channels[i].scan_type.realbits +
+				channels[i].scan_type.shift);

Not sure WARN is a good level (it might be fatal on some setups and we won't that), besides the fact that we may use dev_WARN(). Perhaps dev_warn() would suffice?

Quoted 6 lines
Quoted 4 lines

I was actually thinking about BUG(), but that might crash existing systems. I think we want a strong indicator that something wrong is going on which must be fixed and the splat produced by WARN_ON() is a good indicator of that. It also does not crash existing systems,

It does crash _some_ of them, unfortunately.

Details please ?

WARN_ON() shouldn't cause crash outright, or do I miss something ?

#6 lore raw
Quoted 35 lines · Marek Vasut
Quoted 34 lines · Andy Shevchenko
Quoted 32 lines · Marek Vasut
Quoted 19 lines · Andy Shevchenko
Quoted 5 lines · Marek Vasut

Add runtime check to verify whether storagebits are at least as big
as shifted realbits. This should help spot broken drivers which may
set realbits + shift above storagebits.

Thanks!

...

Quoted 7 lines

+                       /* Verify that sample bits fit into storage */
+                       WARN_ON(channels[i].scan_type.storage bits <
+                               channels[i].scan_type.realbit s +
+                               channels[i].scan_type.shift);

Quoted 4 lines

Not sure WARN is a good level (it might be fatal on some setups and we won't that),
besides the fact that we may use dev_WARN(). Perhaps dev_warn() would suffice?

I was actually thinking about BUG(), but that might crash existing systems.
I think we want a strong indicator that something wrong is going on which
must be fixed and the splat produced by WARN_ON() is a good indicator of
that. It also does not crash existing systems,

Quoted 5 lines
Quoted 1 line

It does crash _some_ of them, unfortunately.

Details please ?

WARN_ON() shouldn't cause crash outright, or do I miss something ?

Arghh, completely forgot about this... Andy is right, maybe there are other cases (in which case, it would be nice to share :D), but this one is definitely one of them:

https://elixir.bootlin.com/linux/latest/source/kernel/panic.c#L579

You can have a cmdline parameter to panic on _WARN() and some systems may have it.

That said, the "nice" stack_dump using WARN is way more explicit about saying that something is seriously wrong and must be fixed. dev_warn() is easier to ignore... But surely it is not nice to brick existing systems.

Not really sure here...

- Nuno Sá

#7 lore raw
Quoted 42 lines · Nuno Sá
Quoted 35 lines · Marek Vasut
Quoted 34 lines · Andy Shevchenko
Quoted 32 lines · Marek Vasut
Quoted 19 lines · Andy Shevchenko
Quoted 5 lines · Marek Vasut

Add runtime check to verify whether storagebits are at least as big
as shifted realbits. This should help spot broken drivers which may
set realbits + shift above storagebits.

Thanks!

...

Quoted 7 lines

+                       /* Verify that sample bits fit into storage */
+                       WARN_ON(channels[i].scan_type.storage bits <
+                               channels[i].scan_type.realbit s +
+                               channels[i].scan_type.shift);

Quoted 4 lines

Not sure WARN is a good level (it might be fatal on some setups and we won't that),
besides the fact that we may use dev_WARN(). Perhaps dev_warn() would suffice?

I was actually thinking about BUG(), but that might crash existing systems.
I think we want a strong indicator that something wrong is going on which
must be fixed and the splat produced by WARN_ON() is a good indicator of
that. It also does not crash existing systems,

Quoted 5 lines
Quoted 1 line

It does crash _some_ of them, unfortunately.

Details please ?

WARN_ON() shouldn't cause crash outright, or do I miss something ?

Quoted 8 lines

Arghh, completely forgot about this... Andy is right, maybe there are other cases (in which case, it would be nice to share :D), but this one is definitely one of them:

https://elixir.bootlin.com/linux/latest/source/kernel/panic.c#L579

You can have a cmdline parameter to panic on _WARN() and some systems may have it.

Yes, I meant panic on warning.

And I can't imagine that this driver can be system critical to the extent that we have to crash the system.

Quoted 6 lines

That said, the "nice" stack_dump using WARN is way more explicit about saying that something is seriously wrong and must be fixed. dev_warn() is easier to ignore... But surely it is not nice to brick existing systems.

Not really sure here...

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
Quoted 49 lines · Andy Shevchenko
Quoted 48 lines · Nuno Sá
Quoted 35 lines · Marek Vasut
Quoted 34 lines · Andy Shevchenko
Quoted 32 lines · Marek Vasut
Quoted 19 lines · Andy Shevchenko
Quoted 5 lines · Marek Vasut

Add runtime check to verify whether storagebits are at least as big
as shifted realbits. This should help spot broken drivers which may
set realbits + shift above storagebits.

Thanks!

...

Quoted 7 lines

+                       /* Verify that sample bits fit into storage */
+                       WARN_ON(channels[i].scan_type.storage bits <
+                               channels[i].scan_type.realbit s +
+                               channels[i].scan_type.shift);

Quoted 4 lines

Not sure WARN is a good level (it might be fatal on some setups and we won't that),
besides the fact that we may use dev_WARN(). Perhaps dev_warn() would suffice?

I was actually thinking about BUG(), but that might crash existing systems.
I think we want a strong indicator that something wrong is going on which
must be fixed and the splat produced by WARN_ON() is a good indicator of
that. It also does not crash existing systems,

Quoted 5 lines
Quoted 1 line

It does crash _some_ of them, unfortunately.

Details please ?

WARN_ON() shouldn't cause crash outright, or do I miss something ?

Arghh, completely forgot about this... Andy is right, maybe there are other cases (in which case, it would be nice to share :D), but this one is definitely one of them:

https://elixir.bootlin.com/linux/latest/source/kernel/panic.c#L579

Quoted 7 lines
Quoted 2 lines

You can have a cmdline parameter to panic on _WARN() and some systems may have it.

Yes, I meant panic on warning.

And I can't imagine that this driver can be system critical to the extent that we have to crash the system.

Is there something which does trigger a backtrace, but without panic()ing the system ?

#9 lore raw
Quoted 67 lines · Marek Vasut
Quoted 66 lines · Andy Shevchenko
Quoted 64 lines · Nuno Sá
Quoted 45 lines · Marek Vasut
Quoted 44 lines · Andy Shevchenko
Quoted 42 lines · Marek Vasut
Quoted 26 lines · Andy Shevchenko
Quoted 7 lines · Marek Vasut

Add runtime check to verify whether storagebits are at least
as big
as shifted realbits. This should help spot broken drivers
which may
set realbits + shift above storagebits.

Thanks!

...

Quoted 11 lines

+                       /* Verify that sample bits fit into
storage */
+                       WARN_ON(channels[i].scan_type.s torage
bits <
+                               channels[i].scan_type.r ealbit
s +
+                               channels[i].scan_type.s hift);

Quoted 6 lines

Not sure WARN is a good level (it might be fatal on some setups
and we won't that),
besides the fact that we may use dev_WARN(). Perhaps dev_warn()
would suffice?

I was actually thinking about BUG(), but that might crash existing systems.
I think we want a strong indicator that something wrong is going
on which
must be fixed and the splat produced by WARN_ON() is a good indicator of
that. It also does not crash existing systems,

Quoted 6 lines
Quoted 1 line

It does crash _some_ of them, unfortunately.

Details please ?

WARN_ON() shouldn't cause crash outright, or do I miss something ?

Arghh, completely forgot about this... Andy is right, maybe there are
other cases (in which case, it would be nice to share :D), but this one
is definitely one of them:

https://elixir.bootlin.com/linux/latest/source/kernel/panic.c#L579

You can have a cmdline parameter to panic on _WARN() and some systems
may have it.

Quoted 8 lines
Quoted 5 lines

Yes, I meant panic on warning.

And I can't imagine that this driver can be system critical to the extent
that we have to crash the system.

Is there something which does trigger a backtrace, but without panic()ing the system ?

Nothing I'm aware... Other than directly call 'dump_stack()'

- Nuno Sá

#10 lore raw

On 3/22/22 11:10, Nuno Sá wrote:

Hi,

[...]

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 11 lines

Arghh, completely forgot about this... Andy is right, maybe there are
other cases (in which case, it would be nice to share :D), but this one
is definitely one of them:

https://elixir.bootlin.com/linux/latest/source/kernel/panic.c#L579

You can have a cmdline parameter to panic on _WARN() and some systems
may have it.

Yes, I meant panic on warning.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

And I can't imagine that this driver can be system critical to the extent
that we have to crash the system.

Is there something which does trigger a backtrace, but without panic()ing the system ?

Nothing I'm aware... Other than directly call 'dump_stack()'

All right, dev_err() it is then. I sent V2.