OSDN Git Service

sched/idle: Avoid spurious wakeup IPIs
authorPeter Zijlstra <peterz@infradead.org>
Wed, 9 Apr 2014 13:35:08 +0000 (15:35 +0200)
committerIngo Molnar <mingo@kernel.org>
Thu, 8 May 2014 07:16:58 +0000 (09:16 +0200)
commitfd99f91aa007ba255aac44fe6cf21c1db398243a
treec4ba7f236d88c2ee2d0f7b6d3601e32d0ab25e57
parent69dd0f848879328ae6c6f54c2ec80e49eef042d8
sched/idle: Avoid spurious wakeup IPIs

Because mwait_idle_with_hints() gets called from !idle context it must
call current_clr_polling(). This however means that resched_task() is
very likely to send an IPI even when we were polling:

  CPU0 CPU1

  if (current_set_polling_and_test())
    goto out;

  __monitor(&ti->flags);
  if (!need_resched())
    __mwait(eax, ecx);
set_tsk_need_resched(p);
smp_mb();
out:
  current_clr_polling();
if (!tsk_is_polling(p))
  smp_send_reschedule(cpu);

So while it is correct (extra IPIs aren't a problem, whereas a missed
IPI would be) it is a performance problem (for some).

Avoid this issue by using fetch_or() to atomically set NEED_RESCHED
and test if POLLING_NRFLAG is set.

Since a CPU stuck in mwait is unlikely to modify the flags word,
contention on the cmpxchg is unlikely and thus we should mostly
succeed in a single go.

Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Acked-by: Nicolas Pitre <nico@linaro.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/n/tip-kf5suce6njh5xf5d3od13rr0@git.kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
kernel/sched/core.c