staging: most: dim2: replace BUG_ON() with error handling
Subsystems (2)
PATCH 1/1 staging: most: dim2: replace BUG_ON() with error handling
Replace BUG_ON() calls with error handling to avoid unnecessary kernel crashes.
--- drivers/staging/most/dim2/dim2.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-)
drivers/staging/most/dim2/dim2.c +17 -8
@@ -168,8 +168,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)) {
@@ -190,7 +192,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 -EINVAL ; + } +if (!dim_enqueue_buffer (&hdm_ch->ch, mbo->bus_address, buf_size)) {list_del (head->next);spin_unlock_irqrestore (&dim_lock, flags);
@@ -271,8 +277,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 +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 ;
@@ -569,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 ;
@@ -645,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.53.0
Greg KH
maintainerQuoted 13 lines · Mark Adamenko
Replace
BUG_ON()calls with error handling to avoid unnecessary kernel crashes.Signed-off-by Mark Adamenko <[email protected]>--- drivers/staging/most/dim2/dim2.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-)drivers/staging/most/dim2/dim2.c +0
-0
drivers/staging/most/dim2/dim2.c static int try_start_dim_transfer(struct hdm_channel *hdm_ch) · line 172
Quoted 7 lines
drivers/staging/most/dim2/dim2.c +2
-2@@ -168,8 +168,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 ;
Can this ever happen? If not, why is this even checked? (hint, I do not think it ever can)
drivers/staging/most/dim2/dim2.c static int try_start_dim_transfer(struct hdm_channel *hdm_ch) · line 174
Quoted 2 lines
drivers/staging/most/dim2/dim2.c +2
-0+if (!hdm_ch->is_initialized) +return -EINVAL ;
Same here, are you sure this can happen? And can the caller handle an error? At first glance, I do not think it can. So be careful with this.
You can't just blindly replace BUG_ON() with tests, please verify if they are needed, and if so, how to handle the error correctly.
thanks,
greg k-h