Skip to contentSkip to conversation

staging: most: dim2: replace BUG_ON() with graceful error handling

PATCH In review [email protected] 2 participants 10 messages Mar 30 – Apr 1

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

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

From: Gabriel Rondon <[email protected]>

Replace all BUG_ON() calls in dim2.c with proper error handling that returns appropriate error codes instead of panicking the kernel.

BUG_ON() is deprecated per Documentation/process/deprecated.rst.

Changes in v2:
- Split into one patch per function as requested by Greg KH

Gabriel Rondon (5):
staging: most: dim2: replace BUG_ON() in try_start_dim_transfer()
staging: most: dim2: replace BUG_ON() in service_done_flag()
staging: most: dim2: replace BUG_ON() in configure_channel()
staging: most: dim2: replace BUG_ON() in enqueue()
staging: most: dim2: replace BUG_ON() in poison_channel()

 drivers/staging/most/dim2/dim2.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
Signature · 1 line
2.33.0
#1 lore raw

PATCH v2 1/5 staging: most: dim2: replace BUG_ON() in try_start_dim_transfer()

From: Gabriel Rondon <[email protected]>

Replace BUG_ON() calls with graceful error handling.

For the null/uninitialized channel checks, return -EINVAL instead of crashing the kernel. For the zero bus_address check, release the spinlock and return -EFAULT.

BUG_ON() is deprecated as it crashes the entire kernel on assertion failure (see Documentation/process/deprecated.rst).

---
 drivers/staging/most/dim2/dim2.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/staging/most/dim2/dim2.c +6 -3
index 66617e89e..48315f8cd 100644
@@ -165,8 +165,8 @@ 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 || !hdm_ch->is_initialized)
+		return -EINVAL;
 
 	spin_lock_irqsave(&dim_lock, flags);
 	if (list_empty(head)) {
@@ -187,7 +187,10 @@ 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);
Signature · 1 line
2.33.0
#2 lore raw

PATCH v2 2/5 staging: most: dim2: replace BUG_ON() in service_done_flag()

In reply to [email protected]

From: Gabriel Rondon <[email protected]>

Replace BUG_ON() calls with an early return since the function returns void.

BUG_ON() is deprecated as it crashes the entire kernel on assertion failure (see Documentation/process/deprecated.rst).

---
 drivers/staging/most/dim2/dim2.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/most/dim2/dim2.c +2 -2
index 48315f8cd..b820c3647 100644
@@ -271,8 +271,8 @@ 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 || !hdm_ch->is_initialized)
+		return;
 
 	spin_lock_irqsave(&dim_lock, flags);
 
Signature · 1 line
2.33.0
#3 lore raw

Re: [PATCH v2 2/5] staging: most: dim2: replace BUG_ON() in service_done_flag()

Quoted 18 lines · On Mon, Mar 30, 2026 at 07:22:52PM +0100, [email protected] wrote:

From: Gabriel Rondon <[email protected]>

Replace BUG_ON() calls with an early return since the function returns void.

BUG_ON() is deprecated as it crashes the entire kernel on assertion failure (see Documentation/process/deprecated.rst).

---
 drivers/staging/most/dim2/dim2.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/most/dim2/dim2.c +0 -0
index 48315f8cd..b820c3647 100644

drivers/staging/most/dim2/dim2.c static void service_done_flag(struct dim2_hdm *dev, int ch_idx) · line 274

Quoted 6 lines
drivers/staging/most/dim2/dim2.c +1 -2
@@ -271,8 +271,8 @@ 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 || !hdm_ch->is_initialized)

hdm_ch can't be NULL. It's a pointer to the middle of a struct plus an offset.

regards,
dan carpenter

Quoted 7 lines
drivers/staging/most/dim2/dim2.c +1 -0
+		return;
 
 	spin_lock_irqsave(&dim_lock, flags);
 
Signature · 1 line
2.33.0
#4 lore raw

Re: [PATCH v2 2/5] staging: most: dim2: replace BUG_ON() in service_done_flag()

From: Gabriel Rondon <[email protected]>

Quoted 2 lines · Dan Carpenter

hdm_ch can't be NULL. It's a pointer to the middle of a struct plus an offset.

You're right, the NULL check is unnecessary here. Will send a fix removing the !hdm_ch check and keeping only !hdm_ch->is_initialized.

Thanks,
Gabriel

#5 lore raw

Re: [PATCH v2 2/5] staging: most: dim2: replace BUG_ON() in service_done_flag()

Quoted 8 lines · On Tue, Mar 31, 2026 at 05:44:29PM +0100, [email protected] wrote:

From: Gabriel Rondon <[email protected]>

Quoted 2 lines · Dan Carpenter

hdm_ch can't be NULL. It's a pointer to the middle of a struct plus an offset.

You're right, the NULL check is unnecessary here. Will send a fix removing the !hdm_ch check and keeping only !hdm_ch->is_initialized.

Check them all, right? Maybe the others are similar?

regards,
dan carpenter

#6 lore raw

Re: [PATCH v2 2/5] staging: most: dim2: replace BUG_ON() in service_done_flag()

From: Gabriel Rondon <[email protected]>

Quoted 1 line · Dan Carpenter

Check them all, right? Maybe the others are similar?

Checked. Patches 3/5 through 5/5 (configure_channel, enqueue, poison_channel) only check ch_idx range, no hdm_ch NULL check — those are fine.

Patch 1/5 (try_start_dim_transfer) does have the same unnecessary !hdm_ch check. hdm_ch is a parameter there, but it's already dereferenced on the line above (head = &hdm_ch->pending_list), so a NULL check after that is dead code. Will send a fix for that one too.

Thanks,
Gabriel

#7 lore raw

PATCH v2 3/5 staging: most: dim2: replace BUG_ON() in configure_channel()

In reply to [email protected]

From: Gabriel Rondon <[email protected]>

Replace BUG_ON() range check on ch_idx with a return of -EINVAL.

BUG_ON() is deprecated as it crashes the entire kernel on assertion failure (see Documentation/process/deprecated.rst).

---
 drivers/staging/most/dim2/dim2.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/most/dim2/dim2.c +2 -1
index b820c3647..0b80f313a 100644
@@ -457,7 +457,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;
Signature · 1 line
2.33.0
#8 lore raw

PATCH v2 4/5 staging: most: dim2: replace BUG_ON() in enqueue()

In reply to [email protected]

From: Gabriel Rondon <[email protected]>

Replace BUG_ON() range check on ch_idx with a return of -EINVAL.

BUG_ON() is deprecated as it crashes the entire kernel on assertion failure (see Documentation/process/deprecated.rst).

---
 drivers/staging/most/dim2/dim2.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/most/dim2/dim2.c +2 -1
index 0b80f313a..23230a32c 100644
@@ -570,7 +570,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;
Signature · 1 line
2.33.0
#9 lore raw

PATCH v2 5/5 staging: most: dim2: replace BUG_ON() in poison_channel()

In reply to [email protected]

From: Gabriel Rondon <[email protected]>

Replace BUG_ON() range check on ch_idx with a return of -EINVAL.

BUG_ON() is deprecated as it crashes the entire kernel on assertion failure (see Documentation/process/deprecated.rst).

---
 drivers/staging/most/dim2/dim2.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/most/dim2/dim2.c +2 -1
index 23230a32c..7953d4626 100644
@@ -647,7 +647,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.33.0