[PATCH 4/6] sched,idle: Clear polling before descheduling the idle thread

From: Andy Lutomirski
Date: Tue Jun 03 2014 - 20:30:17 EST


Currently, the only real guarantee provided by the polling bit is
that, if you hold rq->lock and the polling bit is set, then you can
set need_resched to force a reschedule.

The only reason the lock is needed is that the idle thread might not
be running at all when setting its need_resched bit, and rq->lock
keeps it pinned.

This is easy to fix: just clear the polling bit before scheduling.
Now the polling bit is only ever set when rq->curr == rq->idle.

Signed-off-by: Andy Lutomirski <luto@xxxxxxxxxxxxxx>
---
kernel/sched/idle.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c
index 2ec9f47..4de3a24 100644
--- a/kernel/sched/idle.c
+++ b/kernel/sched/idle.c
@@ -179,17 +179,22 @@ exit_idle:

/*
* Generic idle loop implementation
+ *
+ * Called with polling cleared.
*/
static void cpu_idle_loop(void)
{
- __current_set_polling();
-
while (1) {
/*
- * Invariant: polling is set here (assuming that the arch
- * has a polling bit.
+ * If the arch has a polling bit, we maintain an invariant:
+ *
+ * The polling bit is clear if we're not scheduled (i.e. if
+ * rq->curr != rq->idle). This means that, if rq->idle has
+ * the polling bit set, then setting need_resched is
+ * guaranteed to cause the cpu to reschedule.
*/

+ __current_set_polling();
tick_nohz_idle_enter();

while (!need_resched()) {
@@ -229,6 +234,8 @@ static void cpu_idle_loop(void)
*/
preempt_set_need_resched();
tick_nohz_idle_exit();
+ __current_clr_polling();
+ smp_mb__after_clear_bit();
schedule_preempt_disabled();
}
}
--
1.9.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/