iio: core: Avoid BUG() on invalid clock types
PATCH 1/1 iio: core: Avoid BUG() on invalid clock types
Replace deprecated use of BUG() in default switch cases of iio_get_time_ns() and current_timestamp_clock_show() with WARN_ONCE() and return correct fallback or error codes.
--- drivers/iio/industrialio-core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/iio/industrialio-core.c +4 -2
@@ -341,7 +341,8 @@ s64 iio_get_time_ns(const struct iio_dev *indio_dev)case CLOCK_TAI :return ktime_get_clocktai_ns ();default : -BUG (); +WARN_ONCE (1 ,"Invalid clock type selected for IIO device\n" ); +return 0 ; } }EXPORT_SYMBOL (iio_get_time_ns);
@@ -1518,7 +1519,8 @@ static ssize_t current_timestamp_clock_show(struct device *dev,case CLOCK_TAI :break ;default : -BUG (); +WARN_ONCE (1 ,"Invalid clock type selected for IIO device\n" ); +return -EINVAL ; }return sysfs_emit (buf,"%s\n" , clock_names[clk]);
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerQuoted 3 lines · On Sun, 23 Aug 2026 22:25:02 +0000 Rishab Madhugiri…
Replace deprecated use of
BUG()in default switch cases ofiio_get_time_ns()andcurrent_timestamp_clock_show()withWARN_ONCE()and return correct fallback or error codes.
This is there just to stop compilers moaning that we didn't handle all the possible values in the switch statement. We can't actually hit the BUG() unless we have a very unexpected bug or random memory corruption.
So sure, lets move to the new way of handling these paths.
However, they aren't supposed to be possible. SoWARN_ON_ONCE() and no message is fine for these.
Jonathan
Quoted 30 lines
Signed-off-by Rishab Madhugiri <[email protected]>--- drivers/iio/industrialio-core.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)drivers/iio/industrialio-core.c +4
-2@@ -341,7 +341,8 @@ s64 iio_get_time_ns(const struct iio_dev *indio_dev)case CLOCK_TAI :return ktime_get_clocktai_ns ();default : -BUG (); +WARN_ONCE (1 ,"Invalid clock type selected for IIO device\n" ); +return 0 ; } }EXPORT_SYMBOL (iio_get_time_ns);@@ -1518,7 +1519,8 @@ static ssize_t current_timestamp_clock_show(struct device *dev,case CLOCK_TAI :break ;default : -BUG (); +WARN_ONCE (1 ,"Invalid clock type selected for IIO device\n" ); +return -EINVAL ; }return sysfs_emit (buf,"%s\n" , clock_names[clk]);
Quoted 3 lines · Rishab Madhugiri
Replace deprecated use of
BUG()in default switch cases ofiio_get_time_ns()andcurrent_timestamp_clock_show()withWARN_ONCE()and return correct fallback or error codes.
Why?!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 7 lines · On Mon, 24 Aug 2026 12:55:47 +0300 Andy Shevchenko…
Quoted 3 lines · Rishab Madhugiri
Replace deprecated use of
BUG()in default switch cases ofiio_get_time_ns()andcurrent_timestamp_clock_show()withWARN_ONCE()and return correct fallback or error codes.Why?!
Because BUG() and BUG_ON() shouldn't be used anymore, no? Linus himself said that he hates the macros.
Signature · 2 lines
Kind regards, Joshua Crofts
Quoted 4 lines · Joshua Crofts
Quoted 1 line · On Mon, 24 Aug 2026 12:55:47 +0300 Andy Shevchenko…
On Sun, Aug 23, 2026 at 10:25:02PM +0000, Rishab Madhugiri wrote:
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Replace deprecated use of
BUG()in default switch cases ofiio_get_time_ns()andcurrent_timestamp_clock_show()withWARN_ONCE()and return correct fallback or error codes.Why?!
Because
BUG()andBUG_ON()shouldn't be used anymore, no? Linus himself said that he hates the macros.
Why? Please, study the case. As for the above change, there are two questions (at least):
- Why BUG() is deprecated? Where is it stated?
- What will WARN*() gain us here? (Note some specific kernel command line parameter that may convert WARN to BUG)
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Joshua Crofts
Quoted 5 lines · On Mon, 24 Aug 2026 12:55:47 +0300 Andy Shevchenko…
Quoted 3 lines · Rishab Madhugiri
Replace deprecated use of
BUG()in default switch cases ofiio_get_time_ns()andcurrent_timestamp_clock_show()withWARN_ONCE()and return correct fallback or error codes.
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Why?!
Because
BUG()andBUG_ON()shouldn't be used anymore, no? Linus himself said that he hates the macros.Why? Please, study the case. As for the above change, there are two questions (at least):
- WhyBUG()is deprecated? Where is it stated?
https://docs.kernel.org/process/deprecated.html?hl=en-GB#bug-and-bug-on
Quoted 2 lines
- What will WARN*() gain us here? (Note some specific kernel command line parameter that may convert WARN to BUG)
As Jonathan mentioned earlier, the original BUG() was there to prevent the compiler from complaining that all possible values weren't handled. So BUG() or WARN*() here is a placeholder. Just removing the BUG() call should be valid since it's impossible to hit(?)
--
Kind regards,
Joshua Crofts
Quoted 12 lines · Joshua Crofts
Quoted 10 lines · Andy Shevchenko
Quoted 9 lines · Joshua Crofts
Quoted 6 lines · On Mon, 24 Aug 2026 12:55:47 +0300 Andy Shevchenko…
Quoted 3 lines · Rishab Madhugiri
Replace deprecated use of
BUG()in default switch cases ofiio_get_time_ns()andcurrent_timestamp_clock_show()withWARN_ONCE()and return correct fallback or error codes.Why?!
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Because
BUG()andBUG_ON()shouldn't be used anymore, no? Linus himself said that he hates the macros.Why? Please, study the case. As for the above change, there are two questions (at least):
- WhyBUG()is deprecated? Where is it stated?https://docs.kernel.org/process/deprecated.html?hl=en-GB#bug-and-bug-on
Thanks for sharing! So, this is a missing point in the commit message.
Quoted 7 lines
Quoted 2 lines
- What will WARN*() gain us here? (Note some specific kernel command line parameter that may convert WARN to BUG)
As Jonathan mentioned earlier, the original
BUG()was there to prevent the compiler from complaining that all possible values weren't handled. SoBUG()or WARN*() here is a placeholder. Just removing theBUG()call should be valid since it's impossible to hit(?)
Yeah, I was participated in several cases where the compiler made something strange when BUG() was absent (basically no reachable point which BUG() represents). Do we have no issues (anymore?) with the compiler with WARN*()?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hi Jonathan, Andy, and Joshua,
Thank you for the feedback and for sharing the documentation reference. I will incorporate the reference to the documentation in the v2 commit message and update the default cases to use WARN_ON_ONCE(1) as Jonathan suggested.
I will send the v2 patch shortly.
Best regards,
Rishab
Quoted 42 lines · Andy Shevchenko
Quoted 12 lines · Joshua Crofts
Quoted 10 lines · Andy Shevchenko
Quoted 9 lines · Joshua Crofts
Quoted 6 lines · On Mon, 24 Aug 2026 12:55:47 +0300 Andy Shevchenko…
Quoted 3 lines · Rishab Madhugiri
Replace deprecated use of
BUG()in default switch cases ofiio_get_time_ns()andcurrent_timestamp_clock_show()withWARN_ONCE()and return correct fallback or error codes.Why?!
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Because
BUG()andBUG_ON()shouldn't be used anymore, no? Linus himself said that he hates the macros.Why? Please, study the case. As for the above change, there are two questions (at least):
- WhyBUG()is deprecated? Where is it stated?https://docs.kernel.org/process/deprecated.html?hl=en-GB#bug-and-bug-on
Thanks for sharing! So, this is a missing point in the commit message.
Quoted 7 lines
Quoted 2 lines
- What will WARN*() gain us here? (Note some specific kernel command line parameter that may convert WARN to BUG)
As Jonathan mentioned earlier, the original
BUG()was there to prevent the compiler from complaining that all possible values weren't handled. SoBUG()or WARN*() here is a placeholder. Just removing theBUG()call should be valid since it's impossible to hit(?)Yeah, I was participated in several cases where the compiler made something strange when
BUG()was absent (basically no reachable point whichBUG()represents). Do we have no issues (anymore?) with the compiler with WARN*()?--
With Best Regards,
Andy Shevchenko