tick/nohz: Evaluate the CPU expression after the static key
authorPeter Zijlstra <peterz@infradead.org>
Wed, 12 May 2021 23:29:15 +0000 (01:29 +0200)
committerIngo Molnar <mingo@kernel.org>
Thu, 13 May 2021 12:21:20 +0000 (14:21 +0200)
commitf105dfec0a951cd0d5bfbfe9dc067ea69f71ad5c
treea0924a795d04767e5eaa3d2aa27a4a71ea3e9536
parent6efb943b8616ec53a5e444193dccf1af9ad627b5
tick/nohz: Evaluate the CPU expression after the static key

When tick_nohz_full_cpu() is called with smp_processor_id(), the latter
is unconditionally evaluated whether the static key is on or off. It is
not necessary in the off-case though, so make sure the cpu expression
is executed at the last moment.

Illustrate with the following test function:

int tick_nohz_test(void)
{
return tick_nohz_full_cpu(smp_processor_id());
}

The resulting code before was:

mov    %gs:0x7eea92d1(%rip),%eax   # smp_processor_id() fetch
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
retq
cmpb   $0x0,0x29d393a(%rip)        # <tick_nohz_full_running>
je     tick_nohz_test+0x29         # jump to below eax clear
mov    %eax,%eax
bt     %rax,0x29d3936(%rip)        # <tick_nohz_full_mask>
setb   %al
movzbl %al,%eax
retq
xor    %eax,%eax
retq

Now it becomes:

nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
retq
cmpb   $0x0,0x29d3871(%rip)        # <tick_nohz_full_running>
je     tick_nohz_test+0x29         # jump to below eax clear
mov    %gs:0x7eea91f0(%rip),%eax   # smp_processor_id() fetch, after static key
mov    %eax,%eax
bt     %rax,0x29d3866(%rip)        # <tick_nohz_full_mask>
setb   %al
movzbl %al,%eax
retq
xor    %eax,%eax
retq

Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lore.kernel.org/r/20210512232924.150322-2-frederic@kernel.org
include/linux/tick.h