Commit 4e6292114c74 ("x86/paravirt: Add new features for paravirt patching") changed the order in which altinstructions and paravirt instructions are patched at boot time. However, no analogous change was made in module_finalize, where we apply altinstructions and parainstructions during module load. As a result, any code that generates "stacked up" altinstructions and parainstructions (i.e. local_irq_save/restore) will produce different results when used in built-in kernel code vs. kernel modules. This also makes it possible to inadvertently replace altinstructions in the booted kernel with their parainstruction counterparts when using livepatch/kpatch. To fix this, re-order the processing in module_finalize, so that we do things in this order: 1. apply_paravirt 2. apply_retpolines 3. apply_alternatives 4. alternatives_smp_module_add This is the same ordering that is used at boot time in alternative_instructions. Fixes: 4e6292114c74 ("x86/paravirt: Add new features for paravirt patching") Signed-off-by: Alex Thorlton <alex.thorlton@xxxxxxxxxx> Reviewed-by: Boris Ostrovsky <boris.ostrovsky@xxxxxxxxxx> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx> Cc: Ingo Molnar <mingo@xxxxxxxxxx> Cc: Borislav Petkov <bp@xxxxxxxxx> Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx> Cc: "H. Peter Anvin" <hpa@xxxxxxxxx> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx> Cc: Josh Poimboeuf <jpoimboe@xxxxxxxxxx> Cc: Kefeng Wang <wangkefeng.wang@xxxxxxxxxx> Cc: x86@xxxxxxxxxx Cc: linux-kernel@xxxxxxxxxxxxxxx Cc: stable@xxxxxxxxxxxxxxx # 5.13+ --- arch/x86/kernel/module.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c index 95fa745e310a5..4edc9c87ad0bc 100644 --- a/arch/x86/kernel/module.c +++ b/arch/x86/kernel/module.c @@ -273,6 +273,10 @@ int module_finalize(const Elf_Ehdr *hdr, retpolines = s; } + if (para) { + void *pseg = (void *)para->sh_addr; + apply_paravirt(pseg, pseg + para->sh_size); + } if (retpolines) { void *rseg = (void *)retpolines->sh_addr; apply_retpolines(rseg, rseg + retpolines->sh_size); @@ -290,11 +294,6 @@ int module_finalize(const Elf_Ehdr *hdr, tseg, tseg + text->sh_size); } - if (para) { - void *pseg = (void *)para->sh_addr; - apply_paravirt(pseg, pseg + para->sh_size); - } - /* make jump label nops */ jump_label_apply_nops(me); -- 2.33.1