Skip to contentSkip to conversation

staging: most: replace BUG_ON() with proper error handling

PATCH In review Olle Lukowski 2 participants 6 messages Oct 21, 2025

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

v2 cover staging: most: replace BUG_ON() with proper error handling

This small series replaces BUG_ON() calls in three staging MOST drivers with appropriate error handling.

The affected drivers are:

- drivers/staging/most/i2c/i2c.c
- drivers/staging/most/dim2/dim2.c
- drivers/staging/most/video/video.c

All changes are mechanical and do not alter runtime behavior, except that the kernel won't panic if a driver invariant is violated.

Tested with checkpatch.pl and compile-tested (make M=drivers/staging/most).

---
Changes in v2:
- Drop WARN_ON_ONCE() per review (Greg KH). Many systems enable
panic_on_warn; use plain checks + error returns instead.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Olle Lukowski (3):
staging: most: i2c: replace BUG_ON() with proper checks and error returns
staging: most: dim2: replace BUG_ON() with proper checks and error returns
staging: most: video: replace BUG_ON() with proper check

 drivers/staging/most/dim2/dim2.c   | 27 +++++++++++++++++++--------
 drivers/staging/most/i2c/i2c.c     |  9 ++++++---
 drivers/staging/most/video/video.c |  3 ++-
 3 files changed, 27 insertions(+), 12 deletions(-)

---
base-commit: 211ddde0823f1442e4ad052a2f30f050145ccada
change-id: 20251021-staging-most-warn-51deb2e454aa

Best regards,

Signature · 1 line
Olle Lukowski <[email protected]>
#1 lore raw

PATCH v2 1/3 staging: most: i2c: replace BUG_ON() with proper checks and error returns

Replace BUG_ON() checks for invalid channel indices with proper checks and return -EINVAL to avoid crashing the kernel unnecessarily.

---
 drivers/staging/most/i2c/i2c.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/staging/most/i2c/i2c.c +6 -3
index 184b2dd11..db806b00d 100644
@@ -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 (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 (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 (ch_idx < 0 || ch_idx >= NUM_CHANNELS)
+		return -EINVAL;
 
 	if (ch_idx == CH_RX) {
 		if (!polling_rate)
Signature · 1 line
2.51.1
#2 lore raw

PATCH v2 2/3 staging: most: dim2: replace BUG_ON() with proper checks and error returns

In reply to Olle Lukowski

Replace BUG_ON() calls with proper checks to prevent unnecessary kernel panics. Return appropriate error codes (-EINVAL or -EFAULT) instead of crashing the system.

---
 drivers/staging/most/dim2/dim2.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
drivers/staging/most/dim2/dim2.c +19 -8
index dad2abe6c..d0832704b 100644
@@ -166,8 +166,10 @@ static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
 	unsigned long flags;
 	struct dim_ch_state st;
 
-	BUG_ON(!hdm_ch);
-	BUG_ON(!hdm_ch->is_initialized);
+	if (!hdm_ch)
+		return -EINVAL;
+	if (!hdm_ch->is_initialized)
+		return -EINVAL;
 
 	spin_lock_irqsave(&dim_lock, flags);
 	if (list_empty(head)) {
@@ -188,7 +190,11 @@ static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
 		return -EAGAIN;
 	}
 
-	BUG_ON(mbo->bus_address == 0);
+	if (mbo->bus_address == 0) {
+		spin_unlock_irqrestore(&dim_lock, flags);
+		return -EFAULT;
+	}
+
 	if (!dim_enqueue_buffer(&hdm_ch->ch, mbo->bus_address, buf_size)) {
 		list_del(head->next);
 		spin_unlock_irqrestore(&dim_lock, flags);
@@ -269,8 +275,10 @@ static void service_done_flag(struct dim2_hdm *dev, int ch_idx)
 	unsigned long flags;
 	u8 *data;
 
-	BUG_ON(!hdm_ch);
-	BUG_ON(!hdm_ch->is_initialized);
+	if (!hdm_ch)
+		return;
+	if (!hdm_ch->is_initialized)
+		return;
 
 	spin_lock_irqsave(&dim_lock, flags);
 
@@ -455,7 +463,8 @@ static int configure_channel(struct most_interface *most_iface, int ch_idx,
 	int const ch_addr = ch_idx * 2 + 2;
 	struct hdm_channel *const hdm_ch = dev->hch + ch_idx;
 
-	BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
+	if (ch_idx < 0 || ch_idx >= DMA_CHANNELS)
+		return -EINVAL;
 
 	if (hdm_ch->is_initialized)
 		return -EPERM;
@@ -567,7 +576,8 @@ static int enqueue(struct most_interface *most_iface, int ch_idx,
 	struct hdm_channel *hdm_ch = dev->hch + ch_idx;
 	unsigned long flags;
 
-	BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
+	if (ch_idx < 0 || ch_idx >= DMA_CHANNELS)
+		return -EINVAL;
 
 	if (!hdm_ch->is_initialized)
 		return -EPERM;
@@ -643,7 +653,8 @@ static int poison_channel(struct most_interface *most_iface, int ch_idx)
 	u8 hal_ret;
 	int ret = 0;
 
-	BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
+	if (ch_idx < 0 || ch_idx >= DMA_CHANNELS)
+		return -EINVAL;
 
 	if (!hdm_ch->is_initialized)
 		return -EPERM;
Signature · 1 line
2.51.1
#3 lore raw

Re: [PATCH v2 2/3] staging: most: dim2: replace BUG_ON() with proper checks and error returns

Quoted 14 lines · Olle Lukowski

Replace BUG_ON() calls with proper checks to prevent unnecessary kernel panics. Return appropriate error codes (-EINVAL or -EFAULT) instead of crashing the system.

---
 drivers/staging/most/dim2/dim2.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
drivers/staging/most/dim2/dim2.c +0 -0
index dad2abe6c..d0832704b 100644

drivers/staging/most/dim2/dim2.c static int try_start_dim_transfer(struct hdm_channel *hdm_ch) · line 172

Quoted 9 lines
drivers/staging/most/dim2/dim2.c +4 -2
@@ -166,8 +166,10 @@ static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
 	unsigned long flags;
 	struct dim_ch_state st;
 
-	BUG_ON(!hdm_ch);
-	BUG_ON(!hdm_ch->is_initialized);
+	if (!hdm_ch)
+		return -EINVAL;
+	if (!hdm_ch->is_initialized)
+		return -EINVAL;

Can these things ever actually happen? In looking at the code, I don't see how that could be, do you?

Let's not check for things that are impossible to ever hit.

Same with the other changes in this series, please verify that these are actually possible to happen. If not, then just remove the check.

drivers/staging/most/dim2/dim2.c static int try_start_dim_transfer(struct hdm_channel *hdm_ch) · line 195

Quoted 10 lines
drivers/staging/most/dim2/dim2.c +3 -1
 	spin_lock_irqsave(&dim_lock, flags);
 	if (list_empty(head)) {
@@ -188,7 +190,11 @@ static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
 		return -EAGAIN;
 	}
 
-	BUG_ON(mbo->bus_address == 0);
+	if (mbo->bus_address == 0) {
+		spin_unlock_irqrestore(&dim_lock, flags);
+		return -EFAULT;

You need to do more than just that here :(

Please be very careful on error paths to properly clean up everything.

thanks,

greg k-h

#4 lore raw

PATCH v2 3/3 staging: most: video: replace BUG_ON() with proper check

In reply to Olle Lukowski

Replace a BUG_ON() call with a proper check to prevent an unnecessary kernel panic.

---
 drivers/staging/most/video/video.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/most/video/video.c +2 -1
index 32f71d9a9..3a7ee2791 100644
@@ -575,7 +575,8 @@ static void __exit comp_exit(void)
 
 	most_deregister_configfs_subsys(&comp);
 	most_deregister_component(&comp);
-	BUG_ON(!list_empty(&video_devices));
+	if (!list_empty(&video_devices))
+		pr_err("video_devices list not empty during exit\n");
 }
 
 module_init(comp_init);
Signature · 1 line
2.51.1
#5 lore raw

Re: [PATCH v2 3/3] staging: most: video: replace BUG_ON() with proper check

Quoted 13 lines · Olle Lukowski

Replace a BUG_ON() call with a proper check to prevent an unnecessary kernel panic.

---
 drivers/staging/most/video/video.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/most/video/video.c +0 -0
index 32f71d9a9..3a7ee2791 100644

drivers/staging/most/video/video.c static void __exit comp_exit(void) · line 579

Quoted 6 lines
drivers/staging/most/video/video.c +2 -1
@@ -575,7 +575,8 @@ static void __exit comp_exit(void)
 
 	most_deregister_configfs_subsys(&comp);
 	most_deregister_component(&comp);
-	BUG_ON(!list_empty(&video_devices));
+	if (!list_empty(&video_devices))
+		pr_err("video_devices list not empty during exit\n");

Is this possible to ever have happen?

thanks,

greg k-h