sched/wake_q: Add branch prediction hint to wake_q_add() cmpxchg
authorDavidlohr Bueso <dave@stgolabs.net>
Mon, 3 Dec 2018 05:31:30 +0000 (21:31 -0800)
committerIngo Molnar <mingo@kernel.org>
Mon, 21 Jan 2019 10:18:50 +0000 (11:18 +0100)
The cmpxchg() will fail when the task is already in the process
of waking up, and as such is an extremely rare occurrence.
Micro-optimize the call and put an unlikely() around it.

To no surprise, when using CONFIG_PROFILE_ANNOTATED_BRANCHES
under a number of workloads the incorrect rate was a mere 1-2%.

Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Waiman Long <longman@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Yongji Xie <elohimes@gmail.com>
Cc: andrea.parri@amarulasolutions.com
Cc: lilin24@baidu.com
Cc: liuqi16@baidu.com
Cc: nixun@baidu.com
Cc: xieyongji@baidu.com
Cc: yuanlinsi01@baidu.com
Cc: zhangyu31@baidu.com
Link: https://lkml.kernel.org/r/20181203053130.gwkw6kg72azt2npb@linux-r8p5
Signed-off-by: Ingo Molnar <mingo@kernel.org>
kernel/sched/core.c

index d8d76a6..b05eef7 100644 (file)
@@ -421,7 +421,7 @@ void wake_q_add(struct wake_q_head *head, struct task_struct *task)
         * state, even in the failed case, an explicit smp_mb() must be used.
         */
        smp_mb__before_atomic();
-       if (cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL))
+       if (unlikely(cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL)))
                return;
 
        get_task_struct(task);