x86/alternatives: Optimize optimize_nops()
authorPeter Zijlstra <peterz@infradead.org>
Fri, 26 Mar 2021 15:12:01 +0000 (16:12 +0100)
committerIngo Molnar <mingo@kernel.org>
Fri, 2 Apr 2021 10:41:17 +0000 (12:41 +0200)
commit23c1ad538f4f371bdb67d8a112314842d5db7e5a
treea057eba5ff709afba56b16586bc990340ec562c6
parentb1f480bc0686e65d5413c035bd13af2ea4888784
x86/alternatives: Optimize optimize_nops()

Currently, optimize_nops() scans to see if the alternative starts with
NOPs. However, the emit pattern is:

  141: \oldinstr
  142: .skip (len-(142b-141b)), 0x90

That is, when 'oldinstr' is short, the tail is padded with NOPs. This case
never gets optimized.

Rewrite optimize_nops() to replace any trailing string of NOPs inside
the alternative to larger NOPs. Also run it irrespective of patching,
replacing NOPs in both the original and replaced code.

A direct consequence is that 'padlen' becomes superfluous, so remove it.

 [ bp:
   - Adjust commit message
   - remove a stale comment about needing to pad
   - add a comment in optimize_nops()
   - exit early if the NOP verif. loop catches a mismatch - function
     should not not add NOPs in that case
   - fix the "optimized NOPs" offsets output ]

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Borislav Petkov <bp@suse.de>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lkml.kernel.org/r/20210326151259.442992235@infradead.org
arch/x86/include/asm/alternative.h
arch/x86/kernel/alternative.c
tools/objtool/arch/x86/include/arch/special.h