Skip to contentSkip to conversation

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

PATCH Superseded Gabriel Rondon 2 participants 3 messages Mar 19 – Mar 30

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

BUG_ON() is deprecated as it crashes the entire kernel on assertion failure (see Documentation/process/deprecated.rst). Replace all BUG_ON() calls in dim2.c with proper error handling that returns appropriate error codes instead of panicking.

In try_start_dim_transfer(), replace the BUG_ON for null/uninitialized channel with an early return of -EINVAL, and replace the BUG_ON for zero bus_address with an error return that properly releases the spinlock first.

In service_done_flag(), replace BUG_ON with an early return since the function returns void.

In configure_channel(), enqueue(), and poison_channel(), replace BUG_ON range checks on ch_idx with returns of -EINVAL.

---
 drivers/staging/most/dim2/dim2.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
drivers/staging/most/dim2/dim2.c +14 -8
index 8d649d920..06f4252a8 100644
@@ -168,8 +168,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)) {
@@ -190,7 +190,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);
@@ -271,8 +274,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);
 
@@ -457,7 +460,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;
@@ -569,7 +573,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;
@@ -645,7 +650,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
maintainer
#1 lore raw
Quoted 10 lines · Gabriel Rondon

BUG_ON() is deprecated as it crashes the entire kernel on assertion failure (see Documentation/process/deprecated.rst). Replace all BUG_ON() calls in dim2.c with proper error handling that returns appropriate error codes instead of panicking.

In try_start_dim_transfer(), replace the BUG_ON for null/uninitialized channel with an early return of -EINVAL, and replace the BUG_ON for zero bus_address with an error return that properly releases the spinlock first.

Quoted 5 lines

In service_done_flag(), replace BUG_ON with an early return since the function returns void.

In configure_channel(), enqueue(), and poison_channel(), replace BUG_ON range checks on ch_idx with returns of -EINVAL.

This should be one patch per function, right?

thanks,

greg k-h

#2 lore raw

From: Gabriel Rondon <[email protected]>

Quoted 1 line · Greg KH

This should be one patch per function, right?

Yes, will resend as a series with one patch per function.

Thanks,
Gabriel