Skip to contentSkip to conversation

staging: speakup: Replace BUG_ON() with WARN_ON().

PATCH Superseded Varsha Rao 3 participants 11 messages Feb 24, 2017 – Feb 25, 2017 Staging

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Varsha Rao

author
#0 lore raw

PATCH v2 1/1 staging: speakup: Replace BUG_ON() with WARN_ON().

BUG_ON() is replaced with WARN_ON() to fix the following checkpatch issue:

Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON().

---
Changes in v2:
- Added if condition to avoid dereferencing a NULL pointer.

 drivers/staging/speakup/kobjects.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/staging/speakup/kobjects.c +6 -4
index ca075fe..68a4cc8 100644
@@ -833,10 +833,12 @@ static ssize_t message_show(struct kobject *kobj,
 	struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
 	unsigned long flags;
 
-	BUG_ON(!group);
-	spin_lock_irqsave(&speakup_info.spinlock, flags);
-	retval = message_show_helper(buf, group->start, group->end);
-	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
+	if (WARN_ON(!group)) {
+		spin_lock_irqsave(&speakup_info.spinlock, flags);
+		retval = message_show_helper(buf, group->start, group->end);
+		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
+	}
+
 	return retval;
 }
 
Signature · 1 line
2.9.3
#1 lore raw
Quoted 23 lines · Varsha Rao

BUG_ON() is replaced with WARN_ON() to fix the following checkpatch issue:

Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON().

---
Changes in v2:
- Added if condition to avoid dereferencing a NULL pointer.

 drivers/staging/speakup/kobjects.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/staging/speakup/kobjects.c +0 -1
index ca075fe..68a4cc8 100644
@@ -833,10 +833,12 @@ static ssize_t message_show(struct kobject *kobj,
 	struct msg_group_t *group = spk_find_msg_group(attr->attr.name);
 	unsigned long flags;
 
-	BUG_ON(!group);

drivers/staging/speakup/kobjects.c static ssize_t message_show(struct kobject *kobj, · line 842

Quoted 10 lines
drivers/staging/speakup/kobjects.c +6 -3
-	spin_lock_irqsave(&speakup_info.spinlock, flags);
-	retval = message_show_helper(buf, group->start, group->end);
-	spin_unlock_irqrestore(&speakup_info.spinlock, flags);
+	if (WARN_ON(!group)) {
+		spin_lock_irqsave(&speakup_info.spinlock, flags);
+		retval = message_show_helper(buf, group->start, group->end);
+		spin_unlock_irqrestore(&speakup_info.spinlock, flags);
+	}
+
 	return retval;

I think you should go back and think much harder about what this code does. Does WARN_ON return true if the condition is true or if it is false? If WARN_ON returns true, then should the code that you have put in the then branch be executed? What code do you consider to be the recovery code? BUG_ON always crashed the kernel, so there was no recovery code before. What new code have you added to perform the recovery? What is the value of retval if the branch is or is not taken? Is this the meaningful value in each case?

julia

Quoted 12 lines
drivers/staging/speakup/kobjects.c +0 -1
 }
 
--

2.9.3

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/58b067d9.4d8d620a.8af7e.1ae6%40mx.google.com.
For more options, visit https://groups.google.com/d/optout.

author
#2 lore raw
Quoted 20 lines

Review

Quoted 3 lines
(quoted diff) +0 -0

b/drivers/staging/speakup/kobjects.c

Quoted 5 lines

index ca075fe..68a4cc8 100644

drivers/staging/speakup/kobjects.c +0 -0
@@ -833,10 +833,12 @@ static ssize_t message_show(struct kobject *kobj, 
         struct msg_group_t *group = spk_find_msg_group(attr->attr.name); 

drivers/staging/speakup/kobjects.c static ssize_t message_show(struct kobject *kobj, · line 839

Quoted 10 lines
drivers/staging/speakup/kobjects.c +3 -4
 
         unsigned long flags; 
 
-        BUG_ON(!group); 
-        spin_lock_irqsave(&speakup_info.spinlock, flags); 
-        retval = message_show_helper(buf, group->start, group->end); 
-        spin_unlock_irqrestore(&speakup_info.spinlock, flags); 
+        if (WARN_ON(!group)) { 
+                spin_lock_irqsave(&speakup_info.spinlock, flags); 
+                retval = message_show_helper(buf, group->start, 

group->end);

Quoted 8 lines

drivers/staging/speakup/kobjects.c static ssize_t message_show(struct kobject *kobj, · line 842

Quoted 4 lines
drivers/staging/speakup/kobjects.c +3 -0
+                spin_unlock_irqrestore(&speakup_info.spinlock, flags); 
+        } 
+ 
         return retval; 

I think you should go back and think much harder about what this code does. Does WARN_ON return true if the condition is true or if it is false?

if group = NULL
then WARN_ON(!group) would return true

Quoted 2 lines

If WARN_ON returns true, then should the code that you have put in
the then branch be executed?

The if condition has to be (!WARN_ON(!group)) instead of
WARN_ON(!group)
then then the statements inside if block won't excetute and the
function would return
retval with value 0.

What code do you consider to be the recovery

Quoted 2 lines

code? BUG_ON always crashed the kernel, so there was no recovery code before. What new code have you added to perform the recovery?

I didn't understand regarding recovery. Here further changes has to be
made. As message_show is passed as argument in __ATTR().
It has been called multiple times in drivers/staging/speakup/kobjects.c
and in the following way:

__ATTR(announcements, S_IWUSR | S_IRUGO, message_show, message_store);

     Within staging/include/linux/sysfs.h  it has been defined.

      #define __ATTR(_name, _mode, _show, _store) {                \
    .attr = {.name = __stringify(_name),                \
         .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },        \
    .show    = _show,                        \
    .store    = _store,                        \
     }

From the above code it would imply changes has to be made within this
macro or in show macro definition.
Not sure about what would happen if _show value is 0.

What is

Quoted 4 lines

the value of retval if the branch is or is not taken? Is this the meaningful value in each case?

retval is assigned with size of the message to be displayed. Few of the

things are not clear.

Quoted 21 lines

julia

Quoted 7 lines
drivers/staging/speakup/kobjects.c +0 -0
 } 
Signature · 5 lines
-- 
2.9.3 

-- 
You received this message because you are subscribed to the Google 

Groups "outreachy-kernel" group.

Quoted 1 line

To unsubscribe from this group and stop receiving emails from it, send

an email to [email protected] <javascript:>.

Quoted 1 line

To post to this group, send email to [email protected]

<javascript:>.

Quoted 1 line

To view this discussion on the web visit

https://groups.google.com/d/msgid/outreachy-kernel/58b067d9.4d8d620a.8af7e.1ae6%40mx.google.com.

Quoted 2 lines

For more options, visit https://groups.google.com/d/optout.

#3 lore raw
Quoted 45 lines · Varsha Rao
      > diff --git a/drivers/staging/speakup/kobjects.c
      b/drivers/staging/speakup/kobjects.c
      > index ca075fe..68a4cc8 100644
      > --- a/drivers/staging/speakup/kobjects.c
      > +++ b/drivers/staging/speakup/kobjects.c
      > @@ -833,10 +833,12 @@ static ssize_t message_show(struct
      kobject *kobj,
      >          struct msg_group_t *group =
      spk_find_msg_group(attr->attr.name);
      >          unsigned long flags;
      >
      > -        BUG_ON(!group);
      > -        spin_lock_irqsave(&speakup_info.spinlock, flags);
      > -        retval = message_show_helper(buf, group->start,
      group->end);
      > -        spin_unlock_irqrestore(&speakup_info.spinlock,
      flags);
      > +        if (WARN_ON(!group)) {
      > +                spin_lock_irqsave(&speakup_info.spinlock,
      flags);
      > +                retval = message_show_helper(buf,
      group->start, group->end);
      >
      +                spin_unlock_irqrestore(&speakup_info.spinlock,
      flags);
      > +        }
      > +
      >          return retval;

      I think you should go back and think much harder about what this
      code
      does.  Does WARN_ON return true if the condition is true or if
      it is
      false?


    if group = NULL
    then WARN_ON(!group) would return true

 If WARN_ON returns true, then should the code that you have put in
the then branch be executed?

Quoted 2 lines

    The if condition has to be (!WARN_ON(!group))  instead of WARN_ON(!group)

The double negation does not look like a good idea. Normally kernel code looks like this:

if (detect_failure)
handle_failure;
return;
success_code

Quoted 19 lines

     then then the statements inside if block won't excetute and the function would return
     retval with value 0.

 What code do you consider to be the recovery
code?  BUG_ON always crashed the kernel, so there was no recovery code
before.  What new code have you added to perform the recovery?


    I didn't understand regarding recovery. Here further changes has to be made. As message_show is passed as argument in  __ATTR().
    It has been called multiple times in drivers/staging/speakup/kobjects.c and in the following way:

__ATTR(announcements, S_IWUSR | S_IRUGO, message_show, message_store);

     Within staging/include/linux/sysfs.h  it has been defined.

Quoted 9 lines

      #define __ATTR(_name, _mode, _show, _store) {                \
    .attr = {.name = __stringify(_name),                \
         .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },        \
    .show    = _show,                        \
    .store    = _store,                        \
     }

   From the above code it would imply changes has to be made within this macro or in show macro definition.

You cannot change the macro. It is used in many other places in the kernel.

Perhaps an approach would be to look at other show functions in the kernel, ideally not in staging, and see what they do in case of failure.

julia

Quoted 41 lines

   Not sure about what would happen if _show value is 0.

 What is
the value of retval if the branch is or is not taken?  Is this the
meaningful value in each case?

   retval is assigned with size of the message to be displayed. Few of the things are not clear.

      julia


      >  }
      >
      > --
      > 2.9.3
      >
      > --
      > You received this message because you are subscribed to the
      Google Groups "outreachy-kernel" group.
      > To unsubscribe from this group and stop receiving emails from
      it, send an email to [email protected].
      > To post to this group, send email to
      [email protected].
      > To view this discussion on the web visithttps://groups.google.com/d/msgid/outreachy-kernel/58b067d9.4d8d620a.8af7e.
      1ae6%40mx.google.com.
      > For more options, visit https://groups.google.com/d/optout.
      >

--
You received this message because you are subscribed to the Google Groups
"outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visithttps://groups.google.com/d/msgid/outreachy-kernel/3607bae1-f325-4a95-bbe8- d94fcf492009%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

#4 lore raw
Quoted 7 lines · Julia Lawall, on sam. 25 f�vr. 2017 10:39:36 +0100, wrote:

The double negation does not look like a good idea. Normally kernel code looks like this:

if (detect_failure)

   handle_failure;
   return;

success_code

Indeed, you could instead just do

if (WARN_ON(!group))

	return -INVAL;

and leave the rest intact.

Samuel

#5 lore raw
In reply to Varsha Rao

Hello,

Quoted 15 lines · Varsha Rao, on sam. 25 f�vr. 2017 01:32:42 -0800, wrote:
Quoted 7 lines

-��������BUG_ON(!group); -��������spin_lock_irqsave(&speakup_info.spinlock, flags); -��������retval = message_show_helper(buf, group->start, group->end); -��������spin_unlock_irqrestore(&speakup_info.spinlock, flags); +��������if (WARN_ON(!group)) { +����������������spin_lock_irqsave(&speakup_info.spinlock, flags); +����������������retval = message_show_helper(buf, group->start, group->

    end);
    > +����������������spin_unlock_irqrestore(&speakup_info.spinlock, flags);
    > +��������}
    > +

    �What code do you consider to be the recovery
    code? �BUG_ON always crashed the kernel, so there was no recovery code
    before. �What new code have you added to perform the recovery?
Quoted 7 lines

� �
��� I didn't understand regarding recovery. Here further changes has to be made. As message_show is passed as argument in� __ATTR().
� � It has been called multiple times in drivers/staging/speakup/kobjects.c and in the following way:

__ATTR(announcements, S_IWUSR | S_IRUGO, message_show, message_store);

So it's called on writes to a sys file. Just returning an -EINVAL error there is fine.

Quoted 6 lines

�What is
the value of retval if the branch is or is not taken? �Is this the meaningful value in each case?

�� retval is assigned with size of the message to be displayed. Few of the things are not clear.

Julia asked about when the branch is *not* taken. In that case retval is still 0, that's not a useful value. Please make retval -INVAL instead of 0.

And then with all these fixed (including the WARN_ON logic inversion), your patch makes perfect sense to me.

Samuel

#6 lore raw
Quoted 36 lines · Samuel Thibault

Hello,

Quoted 15 lines · Varsha Rao, on sam. 25 févr. 2017 01:32:42 -0800, wrote:
Quoted 7 lines

-        BUG_ON(!group);
-        spin_lock_irqsave(&speakup_info.spinlock, flags);
-        retval = message_show_helper(buf, group->start, group->end); -        spin_unlock_irqrestore(&speakup_info.spinlock, flags); +        if (WARN_ON(!group)) {
+                spin_lock_irqsave(&speakup_info.spinlock, flags); +                retval = message_show_helper(buf, group->start, group->

    end);
    > +                spin_unlock_irqrestore(&speakup_info.spinlock, flags);
    > +        }
    > +

     What code do you consider to be the recovery
    code?  BUG_ON always crashed the kernel, so there was no recovery code
    before.  What new code have you added to perform the recovery?
Quoted 7 lines


    I didn't understand regarding recovery. Here further changes has to be made. As message_show is passed as argument in  __ATTR().
    It has been called multiple times in drivers/staging/speakup/kobjects.c and in the following way:

__ATTR(announcements, S_IWUSR | S_IRUGO, message_show, message_store);

So it's called on writes to a sys file. Just returning an -EINVAL error there is fine.

Quoted 6 lines

 What is
the value of retval if the branch is or is not taken?  Is this the meaningful value in each case?

   retval is assigned with size of the message to be displayed. Few of the things are not clear.

Quoted 6 lines

Julia asked about when the branch is *not* taken. In that case retval is still 0, that's not a useful value. Please make retval -INVAL instead of 0.

And then with all these fixed (including the WARN_ON logic inversion), your patch makes perfect sense to me.

-EINVAL ?

julia

Quoted 10 lines

Samuel

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20170225094123.ssback7e4kmw2ett%40var.youpi.perso.aquilenet.fr.
For more options, visit https://groups.google.com/d/optout.

#7 lore raw
Quoted 8 lines · Julia Lawall, on sam. 25 f�vr. 2017 10:55:58 +0100, wrote:
Quoted 6 lines

Julia asked about when the branch is *not* taken. In that case retval is still 0, that's not a useful value. Please make retval -INVAL instead of 0.

And then with all these fixed (including the WARN_ON logic inversion), your patch makes perfect sense to me.

-EINVAL ?

Sure :)

Samuel

author
#8 lore raw
In reply to Samuel Thibault
Quoted 4 lines
Quoted 3 lines

It has been called multiple times in

drivers/staging/speakup/kobjects.c and

Quoted 8 lines
Quoted 3 lines

in the following way:

__ATTR(announcements, S_IWUSR | S_IRUGO, message_show, message_store);

So it's called on writes to a sys file. Just returning an -EINVAL error there is fine.

Then is the main purpose of __ATTR is to just write to sys file. How

does .show handle this EINVAL value?

    I understand EINVAL is for invalid argument.
    
     #define __ATTR(_name, _mode, _show, _store) {                \
    .attr = {.name = __stringify(_name),                \
    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },        \
    .show    = _show,                        \
    .store    = _store,                        \
     }
#9 lore raw
Quoted 10 lines · Varsha Rao, on sam. 25 f�vr. 2017 03:42:42 -0800, wrote:

� � Then is the main purpose of __ATTR is to just write to sys file. How does .show handle this EINVAL value?
��� I understand EINVAL is for invalid argument.
���
� �� #define __ATTR(_name, _mode, _show, _store) {��� ��� ��� ��� \
��� .attr = {.name = __stringify(_name),��� ��� ��� ��� \
��� .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },��� ��� \
��� .show��� = _show,��� ��� ��� ��� ��� ��� \
��� .store��� = _store,��� ��� ��� ��� ��� ��� \
���� }

You don't necessarily need to look what __ATTR does (it's a long way up to getting something useful), but just check how other users of __ATTR behave, or see its documentation, which can be found by simply grepping for __ATTR in linux/Documentation/

Samuel

author
#10 lore raw
Quoted 9 lines

You don't necessarily need to look what __ATTR does (it's a long way up to getting something useful), but just check how other users of __ATTR behave, or see its documentation, which can be found by simply grepping for __ATTR in linux/Documentation/

Okay, thanks I'll look into it in the documentation.