mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Chuck Ebbert <76306.1226@compuserve.com>
Cc: linux-kernel <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@osdl.org>, Linus Torvalds <torvalds@osdl.org>
Subject: Re: [patch] i386: switch_to(): misplaced parentheses
Date: Fri, 28 Jul 2006 21:39:11 -0400	[thread overview]
Message-ID: <1154137151.19722.63.camel@localhost.localdomain> (raw)
In-Reply-To: <200607251616_MC3-1-C618-C015@compuserve.com>

On Tue, 2006-07-25 at 16:15 -0400, Chuck Ebbert wrote:
> Recent changes in i386 __switch_to() have a misplaced closing
> parenthesis causing an unlikely() to terminate early.
> 
> Signed-off-by: Chuck Ebbert <76306.1226@compuserve.com>
> 
> --- 2.6.18-rc2-32.orig/arch/i386/kernel/process.c
> +++ 2.6.18-rc2-32/arch/i386/kernel/process.c
> @@ -690,8 +690,8 @@ struct task_struct fastcall * __switch_t
>  	/*
>  	 * Now maybe handle debug registers and/or IO bitmaps
>  	 */
> -	if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW))
> -	    || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP))
> +	if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW)
> +	    || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)))
>  		__switch_to_xtra(next_p, tss);
>  
>  	disable_tsc(prev_p, next_p);

Unlikely's with or's are kind of ambiguous.  An 'and' makes sense but
or's don't.  Because a branch is going to happen anyway.  Just to test
this out, I made a little function and tried out different types of
parenthesis placements.

Here: (the original placement)

	#define unlikely(x) __builtin_expect(!!(x), 0)

	int switch_to(int x, int y)
	{
		int a = 0;
		if (unlikely(x==24) || (y==83))
			a=10;
		return a;
	}


and now yours:

	#define unlikely(x) __builtin_expect(!!(x), 0)

	int switch_to(int x, int y)
	{
		int a = 0;
		if (unlikely(x==24 || y==83))
			a=10;
		return a;
	}

The original gave me this: gcc -O2 -c switch.c

objdump -Dr switch.o

00000000 <switch_to>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 7d 08 18             cmpl   $0x18,0x8(%ebp)
   7:   74 0a                   je     13 <switch_to+0x13>
   9:   31 c0                   xor    %eax,%eax
   b:   83 7d 0c 53             cmpl   $0x53,0xc(%ebp)
   f:   74 02                   je     13 <switch_to+0x13>
  11:   5d                      pop    %ebp
  12:   c3                      ret
  13:   5d                      pop    %ebp
  14:   b8 0a 00 00 00          mov    $0xa,%eax
  19:   c3                      ret


and then yours:  gcc -O2 -c switch_alt.c

objdump -Dr switch_alt.o

00000000 <switch_to>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 7d 08 18             cmpl   $0x18,0x8(%ebp)
   7:   74 0a                   je     13 <switch_to+0x13>
   9:   31 c0                   xor    %eax,%eax
   b:   83 7d 0c 53             cmpl   $0x53,0xc(%ebp)
   f:   74 02                   je     13 <switch_to+0x13>
  11:   5d                      pop    %ebp
  12:   c3                      ret
  13:   5d                      pop    %ebp
  14:   b8 0a 00 00 00          mov    $0xa,%eax
  19:   c3                      ret


They are identical!!!

This is not surprising since you still need to test the other OR case if
it fails, which you are saying it will.  So saying it is unlikely is
meaningless.  Likely's are good for 'or' and unlikely's are good for
'and'.  But not the other way around.

|| and && short circuit when they can.

So the statement of A || B is:

TEST A
JUMP_IF_TRUE true
TEST B
JUMP_IF_FALSE false
true:
 do true statement
JUMP out:
false:
 do else statement
out:

Saying TEST A is likely to fail really doesn't help the situation.
Saying it will likely pass will. Then we could do this:

TEST A
JUMP_IF_FALSE test2
true:
 do true statement
JUMP out:
false:
  do false statement
jump out:
test2:
TEST B
JUMP_IF_TRUE true
JUMP false:
out:

Where here we can move the true after the first compare and prevent the
conditional branch.

So what I'm saying is that unlikely(a || b) has really no good meaning.

Out of curiosity, I removed the unlikely altogether and here's what I
got.

gcc -O2 -c switch_none.c

00000000 <switch_to>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 7d 08 18             cmpl   $0x18,0x8(%ebp)
   7:   74 0a                   je     13 <switch_to+0x13>
   9:   31 c0                   xor    %eax,%eax
   b:   83 7d 0c 53             cmpl   $0x53,0xc(%ebp)
   f:   74 02                   je     13 <switch_to+0x13>
  11:   5d                      pop    %ebp
  12:   c3                      ret
  13:   5d                      pop    %ebp
  14:   b8 0a 00 00 00          mov    $0xa,%eax
  19:   c3                      ret


Ha! still the same.

Is that unlikely there really do any good?

-- Steve


  reply	other threads:[~2006-07-29  1:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-25 20:15 Chuck Ebbert
2006-07-29  1:39 ` Steven Rostedt [this message]
2006-07-29  2:08   ` Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1154137151.19722.63.camel@localhost.localdomain \
    --to=rostedt@goodmis.org \
    --cc=76306.1226@compuserve.com \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®