From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932505AbcFCKsz (ORCPT ); Fri, 3 Jun 2016 06:48:55 -0400 Received: from terminus.zytor.com ([198.137.202.10]:55984 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932397AbcFCKsw (ORCPT ); Fri, 3 Jun 2016 06:48:52 -0400 Date: Fri, 3 Jun 2016 03:48:00 -0700 From: "tip-bot for Gaurav Jindal (Gaurav Jindal)" Message-ID: Cc: linux-kernel@vger.kernel.org, hpa@zytor.com, peterz@infradead.org, gaurav.jindal@spreadtrum.com, sanjeev.yadav@spreadtrum.com, torvalds@linux-foundation.org, efault@gmx.de, tglx@linutronix.de, Gaurav.Jindal@spreadtrum.com, mingo@kernel.org Reply-To: linux-kernel@vger.kernel.org, mingo@kernel.org, Gaurav.Jindal@spreadtrum.com, tglx@linutronix.de, efault@gmx.de, gaurav.jindal@spreadtrum.com, sanjeev.yadav@spreadtrum.com, torvalds@linux-foundation.org, peterz@infradead.org, hpa@zytor.com In-Reply-To: <20160512101330.GA488@gauravjindalubtnb.del.spreadtrum.com> References: <20160512101330.GA488@gauravjindalubtnb.del.spreadtrum.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:sched/core] sched/idle: Optimize the generic idle loop Git-Commit-ID: df55f462b905f3b2d40ec3fb865891382a6ebfb1 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: df55f462b905f3b2d40ec3fb865891382a6ebfb1 Gitweb: http://git.kernel.org/tip/df55f462b905f3b2d40ec3fb865891382a6ebfb1 Author: Gaurav Jindal (Gaurav Jindal) AuthorDate: Thu, 12 May 2016 10:13:33 +0000 Committer: Ingo Molnar CommitDate: Fri, 3 Jun 2016 09:18:56 +0200 sched/idle: Optimize the generic idle loop Currently, smp_processor_id() is used to fetch the current CPU in cpu_idle_loop(). Every time the idle thread runs, it fetches the current CPU using smp_processor_id(). Since the idle thread is per CPU, the current CPU is constant, so we can lift the load out of the loop, saving execution cycles/time in the loop. x86-64: Before patch (execution in loop): 148: 0f ae e8 lfence 14b: 65 8b 04 25 00 00 00 00 mov %gs:0x0,%eax 152: 00 153: 89 c0 mov %eax,%eax 155: 49 0f a3 04 24 bt %rax,(%r12) After patch (execution in loop): 150: 0f ae e8 lfence 153: 4d 0f a3 34 24 bt %r14,(%r12) ARM64: Before patch (execution in loop): 168: d5033d9f dsb ld 16c: b9405661 ldr w1,[x19,#84] 170: 1100fc20 add w0,w1,#0x3f 174: 6b1f003f cmp w1,wzr 178: 1a81b000 csel w0,w0,w1,lt 17c: 130c7000 asr w0,w0,#6 180: 937d7c00 sbfiz x0,x0,#3,#32 184: f8606aa0 ldr x0,[x21,x0] 188: 9ac12401 lsr x1,x0,x1 18c: 36000e61 tbz w1,#0,358 After patch (execution in loop): 1a8: d50339df dsb ld 1ac: f8776ac0 ldr x0,[x22,x23] ab0: ea18001f tst x0,x24 1b4: 54000ea0 b.eq 388 Further observance on ARM64 for 4 seconds shows that cpu_idle_loop is called 8672 times. Shifting the code will save instructions executed in loop and eventually time as well. Signed-off-by: Gaurav Jindal Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Sanjeev Yadav Cc: Linus Torvalds Cc: Mike Galbraith Cc: Peter Zijlstra Cc: Thomas Gleixner Link: http://lkml.kernel.org/r/20160512101330.GA488@gauravjindalubtnb.del.spreadtrum.com Signed-off-by: Ingo Molnar --- kernel/sched/idle.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c index bd12c6c..db4ff7c 100644 --- a/kernel/sched/idle.c +++ b/kernel/sched/idle.c @@ -201,6 +201,8 @@ exit_idle: */ static void cpu_idle_loop(void) { + int cpu = smp_processor_id(); + while (1) { /* * If the arch has a polling bit, we maintain an invariant: @@ -219,7 +221,7 @@ static void cpu_idle_loop(void) check_pgt_cache(); rmb(); - if (cpu_is_offline(smp_processor_id())) { + if (cpu_is_offline(cpu)) { cpuhp_report_idle_dead(); arch_cpu_idle_dead(); }