As some additional enticing Linux kernel patches posted this week for review, an updated patch series is working to optimize code generation during context switching.
Xie Yuanbin pposted the set of three patches working to optimize the compiler’s code generation during context switching, which given its frequency can be an expensive task especially with the ever-increasing number of CPU security mitigations often making context switching even more costly.
Xie explained on the latest patch series:
“The purpose of this series of patches is to optimize the performance of context switching. It does not change the code logic, but only modifies the inline attributes of some functions.
It is found that finish_task_switch() is not inlined even in the O2 level optimization. This may affect performance for the following reasons:
1. It is in the context switching code, which is a hot code path.
2. Because of the modern CPU vulnerabilities, kernel is likely to perform some mitigations inside switch_mm_irq_off(), such as branch prediction hardening or l1d flushing, the instruction pipeline and cache may be flushed. finish_task_switch() is right after that, so the performance here is greatly affected by function calls and branch jumps.
3. The __schedule function has __sched attribute, which makes it be placed in the “.sched.text” section, while finish_task_switch() does not, which causes their distance to be very far in binary, aggravating the above performance degradation.This series of patches primarily make some functions called in context switching as always inline to optimize performance.”
In ensuring more optimal code generation, time spent performing a context switch dropped by 11% on an Intel CPU or decreased by 44% when having Spectre V2 mitigations applied. Similarly, for an LLVM Clang built kernel the context switching time decreased by 8% or 35% when having Spectre V2 mitigations active.
Promising data and those wanting to learn more or try out these patches can do so via this patch series.
