[PATCH] staging: most: dim2: replace BUG_ON with WARN_ON_ONCE and error handling

Yuvraj Singh Chauhan posted 1 patch 16 hours ago
drivers/staging/most/dim2/dim2.c | 26 ++++++++++++++++++--------
1 file changed, 18 insertions(+), 8 deletions(-)
[PATCH] staging: most: dim2: replace BUG_ON with WARN_ON_ONCE and error handling
Posted by Yuvraj Singh Chauhan 16 hours ago
Replace BUG_ON() with WARN_ON_ONCE() and proper error handling in the
DIM2 driver. BUG_ON() crashes the entire kernel, which is excessive for
conditions that can be handled by returning error codes or exiting the
function early.

Changes made:

- try_start_dim_transfer(): Check hdm_ch validity and initialization
  status, returning -EINVAL on failure. Check mbo->bus_address validity,
  releasing the spinlock and returning -EFAULT if zero.

- service_done_flag(): Check hdm_ch validity and initialization status,
  returning early if checks fail.

- configure_channel(), enqueue(), poison_channel(): Validate ch_idx
  bounds and return -EINVAL if out of range.

WARN_ON_ONCE() is used instead of WARN_ON() to prevent log flooding if
the condition triggers repeatedly.

Signed-off-by: Yuvraj Singh Chauhan <ysinghcin@gmail.com>
---
 drivers/staging/most/dim2/dim2.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/drivers/staging/most/dim2/dim2.c b/drivers/staging/most/dim2/dim2.c
index dad2abe6c0c9..58b09fa581e0 100644
--- a/drivers/staging/most/dim2/dim2.c
+++ b/drivers/staging/most/dim2/dim2.c
@@ -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 (WARN_ON_ONCE(!hdm_ch))
+		return -EINVAL;
+	if (WARN_ON_ONCE(!hdm_ch->is_initialized))
+		return -EINVAL;
 
 	spin_lock_irqsave(&dim_lock, flags);
 	if (list_empty(head)) {
@@ -188,7 +190,10 @@ static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
 		return -EAGAIN;
 	}
 
-	BUG_ON(mbo->bus_address == 0);
+	if (WARN_ON_ONCE(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 +274,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 (WARN_ON_ONCE(!hdm_ch))
+		return;
+	if (WARN_ON_ONCE(!hdm_ch->is_initialized))
+		return;
 
 	spin_lock_irqsave(&dim_lock, flags);
 
@@ -455,7 +462,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 (WARN_ON_ONCE(ch_idx < 0 || ch_idx >= DMA_CHANNELS))
+		return -EINVAL;
 
 	if (hdm_ch->is_initialized)
 		return -EPERM;
@@ -567,7 +575,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 (WARN_ON_ONCE(ch_idx < 0 || ch_idx >= DMA_CHANNELS))
+		return -EINVAL;
 
 	if (!hdm_ch->is_initialized)
 		return -EPERM;
@@ -643,7 +652,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 (WARN_ON_ONCE(ch_idx < 0 || ch_idx >= DMA_CHANNELS))
+		return -EINVAL;
 
 	if (!hdm_ch->is_initialized)
 		return -EPERM;

base-commit: 05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
-- 
2.43.0
Re: [PATCH] staging: most: dim2: replace BUG_ON with WARN_ON_ONCE and error handling
Posted by Greg KH 16 hours ago
On Mon, Feb 09, 2026 at 03:48:29PM +0530, Yuvraj Singh Chauhan wrote:
> Replace BUG_ON() with WARN_ON_ONCE() and proper error handling in the
> DIM2 driver. BUG_ON() crashes the entire kernel, which is excessive for
> conditions that can be handled by returning error codes or exiting the
> function early.

WARN_ON() also crashes the kernel if you have panic-on-warn enabled,
which a few billion Linux systems in the world currently have.

So this isn't a valid change at all, sorry.  If these things can happen,
properly handle the error and recover.  If no recovery is possible, and
by hitting these cases the system is so broken that total loss of all
data and memory is the only valid solution, then BUG_ON() should remain.

hope this helps,

greg k-h