Re: [tip: x86/core] x86/alternative: Patch a single alternative location only once

From: Jürgen Groß

Date: Tue Oct 14 2025 - 10:13:13 EST


On 14.10.25 15:25, Borislav Petkov wrote:
On Tue, Oct 14, 2025 at 02:59:09PM +0200, Borislav Petkov wrote:
And frankly, the justification for this patch is also meh: an interrupt might
use the location?!? If this is a real issue then we better disable IRQs around
it. But not make the code yucky.

And this patch is not doing what it is claiming is doing:

Here's an incarnation of XSTATE_XSAVE which is a 3-way alternative:

At location ffffffff81283cd3, it replaces the default XSAVE with XSAVEOPT.

[ 2.456696] SMP alternatives: feat: 10*32+0, old: (save_fpregs_to_fpstate+0x43/0xa0 (ffffffff81283cd3) len: 6), repl: (ffffffff89c1e6d9, len: 6) flags: 0x0
[ 2.459317] SMP alternatives: ffffffff81283cd3: old_insn: 49 0f ae 64 24 40 xsave64 0x40(%r12)
[ 2.460806] SMP alternatives: ffffffff89c1e6d9: rpl_insn: 49 0f ae 74 24 40 xsaveopt64 0x40(%r12)
[ 2.463316] SMP alternatives: ffffffff81283cd3: final_insn: 49 0f ae 74 24 40

and then that exact same location:

[ 2.464757] SMP alternatives: feat: 10*32+1, old: (save_fpregs_to_fpstate+0x43/0xa0 (ffffffff81283cd3) len: 6), repl: (ffffffff89c1e6df, len: 6) flags: 0x0
[ 2.467317] SMP alternatives: ffffffff81283cd3: old_insn: 49 0f ae 64 24 40
[ 2.468746] SMP alternatives: ffffffff89c1e6df: rpl_insn: 49 0f c7 64 24 40 xsaves64 0x40(%r12)
[ 2.470167] SMP alternatives: ffffffff81283cd3: final_insn: 49 0f c7 64 24 40

gets XSAVES.

Oh, indeed, I should have adapted the printing. The patching is fine,
it is just the debug output which claims to have written 2 different
instructions to old_insn, while it didn't.


So, long story short, this needs more thought:

1. check whether patching is needed

2. a helper function evaluates all instances and figures out the final insn
bytes which need to be patched along with the proper padding

3. the proper bytes are copied into the target location and all good

But not like this - the idea is somewhat ok but it needs to be executed in
a cleaner manner.

I'd say.

I can have a try with this approach.

Could take some time, vacation is coming up...


Juergen

Attachment: OpenPGP_0xB0DE9DD628BF132F.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature