Skip to contentSkip to conversation

Questions regarding BUG macro.

DISCUSSION Varsha Rao 3 participants 4 messages Feb 24, 2017

lore
Share & tools
lore

Varsha Rao

author
#0 lore raw

What is the functionality of unreachable() ?
Isn't the do while loop unnecessary in the given definition ?

#define BUG()                                
    do {                                
        _BUG_OR_WARN(0);                    
        unreachable();                        
    } while (0)

Found the above definition at staging/arch/avr32/include/asm/bug.h Also what is the difference between unreachable() and unlikely()?

#1 lore raw
Quoted 2 lines · Varsha Rao

What is the functionality of unreachable() ?

It tells the compiler that it will never execute this.

Quoted 1 line

Isn't the do while loop unnecessary in the given definition ?

Not at all, why do you think it would be?

Quoted 8 lines

#define BUG()��� ��� ��� ��� ��� ��� ��� ��� ��� do {��� ��� ��� ��� ��� ��� ��� ��� ��� ��� _BUG_OR_WARN(0);��� ��� ��� ��� ��� ��� ��� unreachable();��� ��� ��� ��� ��� ��� ��� } while (0)

Found the above definition at staging/arch/avr32/include/asm/bug.h Also what is the difference between unreachable() and unlikely()?

unlikely is a hint to the compiler that the statement vare rarely happens. unreachable tells the compiler that this code will never be run, due to something stopping the CPU before it can happen.

Never use either of these in your code unless you really know what you are doing. Never use unlikely() unless you can measure the difference of it being used and not being used, because almost always humans get it wrong, CPUs are very good at guessing what should happen.

hope this helps,

greg k-h

author
#2 lore raw
Quoted 11 lines · gregkh
Quoted 2 lines · Varsha Rao

What is the functionality of unreachable() ?

It tells the compiler that it will never execute this.

Quoted 1 line

Isn't the do while loop unnecessary in the given definition ?

Not at all, why do you think it would be?

Since there are only two macro calls and no local variables are used,
can't it be like this:

   #define BUG() (_BUG_OR_WARN(0), unreachable())

I understand that do while is used, when there are multiple statements.

Quoted 25 lines
Quoted 8 lines
#define BUG()                                
    do {                                
        _BUG_OR_WARN(0);                    
        unreachable();                        
    } while (0) 

Found the above definition at staging/arch/avr32/include/asm/bug.h Also what is the difference between unreachable() and unlikely()?

unlikely is a hint to the compiler that the statement vare rarely happens. unreachable tells the compiler that this code will never be run, due to something stopping the CPU before it can happen.

Never use either of these in your code unless you really know what you are doing. Never use unlikely() unless you can measure the difference of it being used and not being used, because almost always humans get it wrong, CPUs are very good at guessing what should happen.

Thank you for the explanation.

hope this helps,

greg k-h

#3 lore raw
Quoted 11 lines · Varsha Rao

On Friday, February 24, 2017 at 8:18:17 PM UTC+5:30, gregkh wrote:
On Fri, Feb 24, 2017 at 04:10:30AM -0800, Varsha Rao wrote:
>
> What is the functionality of unreachable() ?

It tells the compiler that it will never execute this.

> Isn't the do while loop unnecessary in the given definition ?

Quoted 7 lines

Not at all, why do you think it would be?

   Since there are only two macro calls and no local variables are used, can't it be like this:

   #define BUG() (_BUG_OR_WARN(0), unreachable())

I think that this form of code is not common (or not used at all) in the kernel. It does not make the purpose very clear - it looks like it is probably intended to be an argument list.

julia

Quoted 45 lines

  I understand that  do while is used, when there are multiple statements.
> #define BUG()
>     do {
>         _BUG_OR_WARN(0);
>         unreachable();
>     } while (0)
>
> Found the above definition at staging/arch/avr32/include/asm/bug.h
> Also what is the difference between unreachable() and unlikely()?

unlikely is a hint to the compiler that the statement vare rarely
happens.  unreachable tells the compiler that this code will never be
run, due to something stopping the CPU before it can happen.

Never use either of these in your code unless you really know what you
are doing.  Never use unlikely() unless you can measure the difference
of it being used and not being used, because almost always humans get it
wrong, CPUs are very good at guessing what should happen.

    Thank you for the explanation.

hope this helps,

greg k-h

--
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/32dfda0b-6ce2-40f2-bf35- 789c0c31b4cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.