[PATCH 1/3] staging: most: i2c: replace BUG_ON() with WARN_ON_ONCE() and return error

Olle Lukowski posted 3 patches 3 months, 2 weeks ago
[PATCH 1/3] staging: most: i2c: replace BUG_ON() with WARN_ON_ONCE() and return error
Posted by Olle Lukowski 3 months, 2 weeks ago
Replace BUG_ON() checks for invalid channel indices with WARN_ON_ONCE()
and return -EINVAL to avoid crashing the kernel unnecessarily.

Signed-off-by: Olle Lukowski <olle@lukowski.dev>
---
 drivers/staging/most/i2c/i2c.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/staging/most/i2c/i2c.c b/drivers/staging/most/i2c/i2c.c
index 184b2dd11..b9267c3fc 100644
--- a/drivers/staging/most/i2c/i2c.c
+++ b/drivers/staging/most/i2c/i2c.c
@@ -71,7 +71,8 @@ static int configure_channel(struct most_interface *most_iface,
 	struct hdm_i2c *dev = to_hdm(most_iface);
 	unsigned int delay, pr;
 
-	BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
+	if (WARN_ON_ONCE(ch_idx < 0 || ch_idx >= NUM_CHANNELS))
+		return -EINVAL;
 
 	if (channel_config->data_type != MOST_CH_CONTROL) {
 		pr_err("bad data type for channel %d\n", ch_idx);
@@ -125,7 +126,8 @@ static int enqueue(struct most_interface *most_iface,
 	struct hdm_i2c *dev = to_hdm(most_iface);
 	int ret;
 
-	BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
+	if (WARN_ON_ONCE(ch_idx < 0 || ch_idx >= NUM_CHANNELS))
+		return -EINVAL;
 
 	if (ch_idx == CH_RX) {
 		/* RX */
@@ -170,7 +172,8 @@ static int poison_channel(struct most_interface *most_iface,
 	struct hdm_i2c *dev = to_hdm(most_iface);
 	struct mbo *mbo;
 
-	BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
+	if (WARN_ON_ONCE(ch_idx < 0 || ch_idx >= NUM_CHANNELS))
+		return -EINVAL;
 
 	if (ch_idx == CH_RX) {
 		if (!polling_rate)

-- 
2.51.1
Re: [PATCH 1/3] staging: most: i2c: replace BUG_ON() with WARN_ON_ONCE() and return error
Posted by Greg Kroah-Hartman 3 months, 2 weeks ago
On Tue, Oct 21, 2025 at 03:16:27PM +0300, Olle Lukowski wrote:
> Replace BUG_ON() checks for invalid channel indices with WARN_ON_ONCE()
> and return -EINVAL to avoid crashing the kernel unnecessarily.
> 
> Signed-off-by: Olle Lukowski <olle@lukowski.dev>
> ---
>  drivers/staging/most/i2c/i2c.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/staging/most/i2c/i2c.c b/drivers/staging/most/i2c/i2c.c
> index 184b2dd11..b9267c3fc 100644
> --- a/drivers/staging/most/i2c/i2c.c
> +++ b/drivers/staging/most/i2c/i2c.c
> @@ -71,7 +71,8 @@ static int configure_channel(struct most_interface *most_iface,
>  	struct hdm_i2c *dev = to_hdm(most_iface);
>  	unsigned int delay, pr;
>  
> -	BUG_ON(ch_idx < 0 || ch_idx >= NUM_CHANNELS);
> +	if (WARN_ON_ONCE(ch_idx < 0 || ch_idx >= NUM_CHANNELS))
> +		return -EINVAL;

If this is something that can actually ever happen, this change really
doesn't do anything well.  Many systems (i.e. the HUGE majority of Linux
instances in the world, billions) are running with panic-on-warn set, so
that means this will still reboot the box.

So please, just properly handle the issue and return an error and do not
panic the system.

Same for the other patches in this series.

thanks,

greg k-h