All of lore.kernel.org
 help / color / mirror / Atom feed
* if(function()) BUG(); still bad?
@ 2007-12-03  0:20 Roel Kluin
  2007-12-03  0:32 ` Matthew Wilcox
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Roel Kluin @ 2007-12-03  0:20 UTC (permalink / raw)
  To: kernel-janitors

On the kernel janitors site it is stated:

make sure BUG() is used correctly (i.e. if(function()) BUG(); is evil)
    * i.e. even when no-op-ing BUG we still have an if (See also: BUG_ON)

Is this still true? A recent patch added such entries, see for instance
http://lkml.org/lkml/2007/11/30/298 There are also several occurances in the
kernel, which can be spotted with:

[begin bash]

s="[[:space:]]*";
V="[[:alpha:]_]\+[[:alnum:]_]*"

# a bit of magic to cope with comments, chars and strings
cendl="$s\(\/[\*\/].*\)\?$"
ccode="\([^\/\"']*\|\/[^\*\/]\|\/\*.*\*\/\|'[^']*'\|\"[^\"]*\"\)*"

q="${s}if$s($ccode$s$V$s($ccode)$s$ccode$s)"

git-grep -n -B1 "^\($q\)\?${s}BUG($s)$s;$cendl" | grep -A1 "$q$cendl" | less

[end bash]

There are a few false positives due to sizeof() and MACROS(), but I can think of
a way to filter those as well.

for instance in a patch I saw this:

> +		if (__blk_end_request(rq, 0, 0))
> +			BUG();
>  		spin_unlock(q->queue_lock);

If it's still bad, what should the fix be? (I'll write a script and post
patches)

Roel



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: if(function()) BUG(); still bad?
  2007-12-03  0:20 if(function()) BUG(); still bad? Roel Kluin
@ 2007-12-03  0:32 ` Matthew Wilcox
  2007-12-03  0:40 ` Roel Kluin
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Matthew Wilcox @ 2007-12-03  0:32 UTC (permalink / raw)
  To: kernel-janitors

On Mon, Dec 03, 2007 at 01:20:20AM +0100, Roel Kluin wrote:
> for instance in a patch I saw this:
> 
> > +		if (__blk_end_request(rq, 0, 0))
> > +			BUG();
> >  		spin_unlock(q->queue_lock);
> 
> If it's still bad, what should the fix be? (I'll write a script and post
> patches)

This one should not be converted to a BUG_ON.  We always want to call
__blk_end_request(), whether we have CONFIG_BUG or not.  This case just
wants to test the return value.

-- 
Intel are signing my paycheques ... these opinions are still mine
"Bill, look, we understand that you're interested in selling us this
operating system, but compare it to ours.  We can't possibly take such
a retrograde step."

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: if(function()) BUG(); still bad?
  2007-12-03  0:20 if(function()) BUG(); still bad? Roel Kluin
  2007-12-03  0:32 ` Matthew Wilcox
@ 2007-12-03  0:40 ` Roel Kluin
  2007-12-03  1:18 ` Adrian Bunk
  2007-12-03  2:48 ` Matthew Wilcox
  3 siblings, 0 replies; 5+ messages in thread
From: Roel Kluin @ 2007-12-03  0:40 UTC (permalink / raw)
  To: kernel-janitors

Matthew Wilcox wrote:
> On Mon, Dec 03, 2007 at 01:20:20AM +0100, Roel Kluin wrote:
>> for instance in a patch I saw this:
>>
>>> +		if (__blk_end_request(rq, 0, 0))
>>> +			BUG();
>>>  		spin_unlock(q->queue_lock);
>> If it's still bad, what should the fix be? (I'll write a script and post
>> patches)
> 
> This one should not be converted to a BUG_ON.  We always want to call
> __blk_end_request(), whether we have CONFIG_BUG or not.  This case just
> wants to test the return value.
> 

Ok, from your reply I can make up it's bad if the function in the if()
assigns a value, or has pointers as arguments, which it modifies.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: if(function()) BUG(); still bad?
  2007-12-03  0:20 if(function()) BUG(); still bad? Roel Kluin
  2007-12-03  0:32 ` Matthew Wilcox
  2007-12-03  0:40 ` Roel Kluin
@ 2007-12-03  1:18 ` Adrian Bunk
  2007-12-03  2:48 ` Matthew Wilcox
  3 siblings, 0 replies; 5+ messages in thread
From: Adrian Bunk @ 2007-12-03  1:18 UTC (permalink / raw)
  To: kernel-janitors

On Sun, Dec 02, 2007 at 05:32:22PM -0700, Matthew Wilcox wrote:
> On Mon, Dec 03, 2007 at 01:20:20AM +0100, Roel Kluin wrote:
> > for instance in a patch I saw this:
> > 
> > > +		if (__blk_end_request(rq, 0, 0))
> > > +			BUG();
> > >  		spin_unlock(q->queue_lock);
> > 
> > If it's still bad, what should the fix be? (I'll write a script and post
> > patches)
> 
> This one should not be converted to a BUG_ON.  We always want to call
> __blk_end_request(), whether we have CONFIG_BUG or not.  This case just
> wants to test the return value.

It can be converted, the empty BUG_ON() in include/asm-generic/bug.h 
handles it with

  #define BUG_ON(condition) do { if (condition) ; } while(0)


cu
Adrian

-- 

       "Is there not promise of rain?" Ling Tan asked suddenly out
        of the darkness. There had been need of rain for many days.
       "Only a promise," Lao Er said.
                                       Pearl S. Buck - Dragon Seed


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: if(function()) BUG(); still bad?
  2007-12-03  0:20 if(function()) BUG(); still bad? Roel Kluin
                   ` (2 preceding siblings ...)
  2007-12-03  1:18 ` Adrian Bunk
@ 2007-12-03  2:48 ` Matthew Wilcox
  3 siblings, 0 replies; 5+ messages in thread
From: Matthew Wilcox @ 2007-12-03  2:48 UTC (permalink / raw)
  To: kernel-janitors

On Mon, Dec 03, 2007 at 02:18:35AM +0100, Adrian Bunk wrote:
> On Sun, Dec 02, 2007 at 05:32:22PM -0700, Matthew Wilcox wrote:
> > On Mon, Dec 03, 2007 at 01:20:20AM +0100, Roel Kluin wrote:
> > > for instance in a patch I saw this:
> > > 
> > > > +		if (__blk_end_request(rq, 0, 0))
> > > > +			BUG();
> > 
> > This one should not be converted to a BUG_ON.  We always want to call
> > __blk_end_request(), whether we have CONFIG_BUG or not.  This case just
> > wants to test the return value.
> 
> It can be converted, the empty BUG_ON() in include/asm-generic/bug.h 
> handles it with
> 
>   #define BUG_ON(condition) do { if (condition) ; } while(0)

Yes, but it gives the wrong impression.

-- 
Intel are signing my paycheques ... these opinions are still mine
"Bill, look, we understand that you're interested in selling us this
operating system, but compare it to ours.  We can't possibly take such
a retrograde step."

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-12-03  2:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-12-03  0:20 if(function()) BUG(); still bad? Roel Kluin
2007-12-03  0:32 ` Matthew Wilcox
2007-12-03  0:40 ` Roel Kluin
2007-12-03  1:18 ` Adrian Bunk
2007-12-03  2:48 ` Matthew Wilcox

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.