x86/alternative: Patch a single alternative location only once
v4 cover x86/alternative: Patch a single alternative location only once
Instead of patching a single location potentially multiple times in case of nested ALTERNATIVE()s, do the patching only after having evaluated all alt_instr instances for that location.
Changes in V2:
- complete rework (Boris Petkov)
Changes in V3:
- split former V2 patch into 2 by introducing a helper function (Boris Petkov)
- repost the small cleanup patch 1 which was taken before, but has somehow vanished from the tip x86/alternative branch (it is still in the tip master branch, but I couldn't find it in any other tip branch).
Changes in V4:
- use 3 helpers instead of 1 (Boris Petkov)
Juergen Gross (3):
x86/alternative: Drop not needed test after call of alt_replace_call() x86/alternative: Use helper functions for patching alternatives x86/alternative: Patch a single alternative location only once
arch/x86/kernel/alternative.c | 147 ++++++++++++++++++++-------------- 1 file changed, 87 insertions(+), 60 deletions(-)
Signature · 1 line
2.51.0
PATCH v4 1/3 x86/alternative: Drop not needed test after call of alt_replace_call()
alt_replace_call() will never return a negative value, so testing the return value to be less than zero can be dropped.
This makes it possible to switch the return type of alt_replace_call() and the type of insn_buff_sz to unsigned int.
---
V2:
- new patch
--- arch/x86/kernel/alternative.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
arch/x86/kernel/alternative.c +3 -6
@@ -559,7 +559,7 @@ EXPORT_SYMBOL(BUG_func);* Rewrite the "call BUG_func" replacement to point to the target of the * indirect pv_ops call "call *disp(%ip)". */ -static int alt_replace_call (u8 *instr,u8 *insn_buff,struct alt_instr *a) +static unsigned int alt_replace_call (u8 *instr,u8 *insn_buff,struct alt_instr *a) {void *target, *bug = &BUG_func;s32 disp;
@@ -643,7 +643,7 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,* order. */ for (a = start; a < end; a++) { -int insn_buff_sz =0 ; +unsigned int insn_buff_sz =0 ;/* * In case of nested ALTERNATIVE()s the outer alternative might
@@ -683,11 +683,8 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,memcpy (insn_buff, replacement, a->replacementlen); insn_buff_sz = a->replacementlen; -if (a->flags &ALT_FLAG_DIRECT_CALL ) { +if (a->flags &ALT_FLAG_DIRECT_CALL ) insn_buff_sz =alt_replace_call (instr, insn_buff, a); -if (insn_buff_sz <0 ) -continue ; - }for (; insn_buff_sz < a->instrlen; insn_buff_sz++) insn_buff[insn_buff_sz] =0x90 ;
Signature · 1 line
2.51.0
PATCH v4 2/3 x86/alternative: Use helper functions for patching alternatives
In reply to Juergen GrossTidy up apply_alternatives() by moving the main patching action of a single alternative instance into 3 helper functions:
- analyze_patch_site() for selection whether patching should occur or not and to handle nested alternatives.
- prep_patch_site() for applying any needed relocations and issuing debug prints for the site.
- patch_site() doing the real patching action, including optimization of any padding NOPs.
In prep_patch_site() use __apply_relocation() instead of text_poke_apply_relocation(), as the NOP optimization is now done in patch_site() for all cases.
---
V3:
- new patch
V4:
- further split coding in more helpers (Borislav Petkov)
--- arch/x86/kernel/alternative.c | 140 +++++++++++++++++++++------------- 1 file changed, 85 insertions(+), 55 deletions(-)
arch/x86/kernel/alternative.c +85 -55
@@ -604,6 +604,86 @@ static inline u8 * instr_va(struct alt_instr *i)return (u8 *)&i->instr_offset + i->instr_offset; } +struct patch_site { +u8 *instr; +struct alt_instr *alt; +u8 buff[MAX_PATCH_LEN ]; +u8 len; +}; + +static void __init_or_module analyze_patch_site(struct patch_site *ps, +struct alt_instr *p,struct alt_instr *end) +{ +struct alt_instr *r; + +/* +* In case of nested ALTERNATIVE()s the outer alternative might add +* more padding. To ensure consistent patching find the max padding for +* all alt_instr entries for this site (nested alternatives result in +* consecutive entries). +*/ + ps->instr =instr_va (p); + ps->len = p->instrlen; +for (r = p+1 ; r < end &&instr_va (r) == ps->instr; r++) { + ps->len =max (ps->len, r->instrlen); + p->instrlen = r->instrlen = ps->len; + } + +BUG_ON (ps->len >sizeof (ps->buff)); +BUG_ON (p->cpuid >= (NCAPINTS +NBUGINTS ) *32 ); + +/* +* Patch if either: +* - feature is present +* - feature not present but ALT_FLAG_NOT is set to mean, +* patch if feature is *NOT* present. +*/ +if (!boot_cpu_has (p->cpuid) == !(p->flags &ALT_FLAG_NOT )) + ps->alt =NULL ; +else + ps->alt = p; +} + +static void __init_or_module prep_patch_site(struct patch_site *ps) +{ +struct alt_instr *p = ps->alt; +u8 buff_sz; +u8 *repl; + +if (!p) { +/* Nothing to patch, use original instruction. */ +memcpy (ps->buff, ps->instr, ps->len); +return ; + } + + repl = (u8 *)&p->repl_offset + p->repl_offset; +DPRINTK (ALT ,"feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x" , + p->cpuid >>5 , p->cpuid &0x1f , + ps->instr, ps->instr, ps->len, + repl, p->replacementlen, p->flags); + +memcpy (ps->buff, repl, p->replacementlen); + buff_sz = p->replacementlen; + +if (p->flags &ALT_FLAG_DIRECT_CALL ) + buff_sz =alt_replace_call (ps->instr, ps->buff, p); + +for (; buff_sz < ps->len; buff_sz++) + ps->buff[buff_sz] =0x90 ; + +__apply_relocation (ps->buff, ps->instr, ps->len, repl, p->replacementlen); + +DUMP_BYTES (ALT , ps->instr, ps->len,"%px: old_insn: " , ps->instr); +DUMP_BYTES (ALT , repl, p->replacementlen,"%px: rpl_insn: " , repl); +DUMP_BYTES (ALT , ps->buff, ps->len,"%px: final_insn: " , ps->instr); +} + +static void __init_or_module patch_site(struct patch_site *ps) +{ +optimize_nops (ps->instr, ps->buff, ps->len); +text_poke_early (ps->instr, ps->buff, ps->len); +} +/* * Replace instructions with better alternatives for this CPU type. This runs * before SMP is initialized to avoid SMP problems with self modifying code.
@@ -617,9 +697,7 @@ static inline u8 * instr_va(struct alt_instr *i) void __init_or_module noinline apply_alternatives(struct alt_instr *start,struct alt_instr *end) { -u8 insn_buff[MAX_PATCH_LEN ]; -u8 *instr, *replacement; -struct alt_instr *a, *b; +struct alt_instr *a;DPRINTK (ALT ,"alt table %px, -> %px" , start, end);
@@ -643,59 +721,11 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,* order. */ for (a = start; a < end; a++) { -unsigned int insn_buff_sz =0 ; - -/* -* In case of nested ALTERNATIVE()s the outer alternative might -* add more padding. To ensure consistent patching find the max -* padding for all alt_instr entries for this site (nested -* alternatives result in consecutive entries). -*/ -for (b = a+1 ; b < end &&instr_va (b) ==instr_va (a); b++) { -u8 len =max (a->instrlen, b->instrlen); - a->instrlen = b->instrlen = len; - } - - instr =instr_va (a); - replacement = (u8 *)&a->repl_offset + a->repl_offset; -BUG_ON (a->instrlen >sizeof (insn_buff)); -BUG_ON (a->cpuid >= (NCAPINTS +NBUGINTS ) *32 ); - -/* -* Patch if either: -* - feature is present -* - feature not present but ALT_FLAG_NOT is set to mean, -* patch if feature is *NOT* present. -*/ -if (!boot_cpu_has (a->cpuid) == !(a->flags &ALT_FLAG_NOT )) { -memcpy (insn_buff, instr, a->instrlen); -optimize_nops (instr, insn_buff, a->instrlen); -text_poke_early (instr, insn_buff, a->instrlen); -continue ; - } - -DPRINTK (ALT ,"feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x" , - a->cpuid >>5 , - a->cpuid &0x1f , - instr, instr, a->instrlen, - replacement, a->replacementlen, a->flags); - -memcpy (insn_buff, replacement, a->replacementlen); - insn_buff_sz = a->replacementlen; - -if (a->flags &ALT_FLAG_DIRECT_CALL ) - insn_buff_sz =alt_replace_call (instr, insn_buff, a); - -for (; insn_buff_sz < a->instrlen; insn_buff_sz++) - insn_buff[insn_buff_sz] =0x90 ; - -text_poke_apply_relocation (insn_buff, instr, a->instrlen, replacement, a->replacementlen); - -DUMP_BYTES (ALT , instr, a->instrlen,"%px: old_insn: " , instr); -DUMP_BYTES (ALT , replacement, a->replacementlen,"%px: rpl_insn: " , replacement); -DUMP_BYTES (ALT , insn_buff, insn_buff_sz,"%px: final_insn: " , instr); +struct patch_site ps; -text_poke_early (instr, insn_buff, insn_buff_sz); +analyze_patch_site (&ps, a, end); +prep_patch_site (&ps); +patch_site (&ps); }kasan_enable_current ();
Signature · 1 line
2.51.0
Borislav Petkov
maintainerRe: [PATCH v4 2/3] x86/alternative: Use helper functions for patching alternatives
Quoted 18 lines · Juergen Gross
Tidy up
apply_alternatives()by moving the main patching action of a single alternative instance into 3 helper functions:-
analyze_patch_site()for selection whether patching should occur or not and to handle nested alternatives.-
prep_patch_site()for applying any needed relocations and issuing debug prints for the site.-
patch_site()doing the real patching action, including optimization of any padding NOPs.In
prep_patch_site()use__apply_relocation()instead oftext_poke_apply_relocation(), as the NOP optimization is now done inpatch_site()for all cases.Suggested-by Borislav Petkov <[email protected]>Signed-off-by Juergen Gross <[email protected]>
Quoted 8 lines
---
V3:
- new patch
V4:
- further split coding in more helpers (Borislav Petkov)--- arch/x86/kernel/alternative.c | 140 +++++++++++++++++++++------------- 1 file changed, 85 insertions(+), 55 deletions(-)
Better...
Some additional changes ontop:
- put the comment over the loop where it belongs
- name variables into something more descriptive than just a single letter.
With that I think it is starting to look better/more readable.
arch/x86/kernel/alternative.c +21 -19
@@ -594,25 +594,27 @@ struct patch_site { }; static void __init_or_module analyze_patch_site(struct patch_site *ps, -struct alt_instr *p,struct alt_instr *end) +struct alt_instr *start, +struct alt_instr *end) {struct alt_instr *r; + ps->instr =instr_va (start); + ps->len = start->instrlen; +/* * In case of nested ALTERNATIVE()s the outer alternative might add * more padding. To ensure consistent patching find the max padding for * all alt_instr entries for this site (nested alternatives result in * consecutive entries). */ - ps->instr =instr_va (p); - ps->len = p->instrlen; -for (r = p+1 ; r < end &&instr_va (r) == ps->instr; r++) { +for (r = start+1 ; r < end &&instr_va (r) == ps->instr; r++) { ps->len =max (ps->len, r->instrlen); - p->instrlen = r->instrlen = ps->len; + start->instrlen = r->instrlen = ps->len; }BUG_ON (ps->len >sizeof (ps->buff)); -BUG_ON (p->cpuid >= (NCAPINTS +NBUGINTS ) *32 ); +BUG_ON (start->cpuid >= (NCAPINTS +NBUGINTS ) *32 );/* * Patch if either:
@@ -620,43 +622,43 @@ static void __init_or_module analyze_patch_site(struct patch_site *ps,* - feature not present but ALT_FLAG_NOT is set to mean, * patch if feature is *NOT* present. */ -if (!boot_cpu_has (p->cpuid) == !(p->flags &ALT_FLAG_NOT )) +if (!boot_cpu_has (start->cpuid) == !(start->flags &ALT_FLAG_NOT )) ps->alt =NULL ;else - ps->alt = p; + ps->alt = start; } static void __init_or_module prep_patch_site(struct patch_site *ps) { -struct alt_instr *p = ps->alt; +struct alt_instr *alt = ps->alt;u8 buff_sz;u8 *repl; -if (!p) { +if (!alt) {/* Nothing to patch, use original instruction. */ memcpy (ps->buff, ps->instr, ps->len);return ; } - repl = (u8 *)&p->repl_offset + p->repl_offset; + repl = (u8 *)&alt->repl_offset + alt->repl_offset;DPRINTK (ALT ,"feat: %d*32+%d, old: (%pS (%px) len: %d), repl: (%px, len: %d) flags: 0x%x" , - p->cpuid >>5 , p->cpuid &0x1f , + alt->cpuid >>5 , alt->cpuid &0x1f , ps->instr, ps->instr, ps->len, - repl, p->replacementlen, p->flags); + repl, alt->replacementlen, alt->flags); -memcpy (ps->buff, repl, p->replacementlen); - buff_sz = p->replacementlen; +memcpy (ps->buff, repl, alt->replacementlen); + buff_sz = alt->replacementlen; -if (p->flags &ALT_FLAG_DIRECT_CALL ) - buff_sz =alt_replace_call (ps->instr, ps->buff, p); +if (alt->flags &ALT_FLAG_DIRECT_CALL ) + buff_sz =alt_replace_call (ps->instr, ps->buff, alt);for (; buff_sz < ps->len; buff_sz++) ps->buff[buff_sz] =0x90 ; -__apply_relocation (ps->buff, ps->instr, ps->len, repl, p->replacementlen); +__apply_relocation (ps->buff, ps->instr, ps->len, repl, alt->replacementlen);DUMP_BYTES (ALT , ps->instr, ps->len,"%px: old_insn: " , ps->instr); -DUMP_BYTES (ALT , repl, p->replacementlen,"%px: rpl_insn: " , repl); +DUMP_BYTES (ALT , repl, alt->replacementlen,"%px: rpl_insn: " , repl);DUMP_BYTES (ALT , ps->buff, ps->len,"%px: final_insn: " , ps->instr); }
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v4 3/3 x86/alternative: Patch a single alternative location only once
In reply to Juergen GrossInstead of patching a single location potentially multiple times in case of nested ALTERNATIVE()s, do the patching only after having evaluated all alt_instr instances for that location.
This has multiple advantages:
- In case of replacing an indirect with a direct call using the ALT_FLAG_DIRECT_CALL flag, there is no longer the need to have that instance before any other instances at the same location (the original instruction is needed for finding the target of the direct call).
This issue has been hit when trying to do paravirt patching similar to the following:
ALTERNATIVE_2(PARAVIRT_CALL, // indirect call
instr, feature, // native instruction
ALT_CALL_INSTR, X86_FEATURE_XENPV) // Xen function
In case "feature" was true, "instr" replaced the indirect call. Under Xen PV the patching to have a direct call failed, as the original indirect call was no longer there to find the call target.
- In case of nested ALTERNATIVE()s there is no intermediate replacement visible. This avoids any problems in case e.g. an interrupt is happening between the single instances and the patched location is used during handling the interrupt.
---
V2:
- complete rework (Boris Petkov)
V3:
- rebase to added patch 2
--- arch/x86/kernel/alternative.c | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-)
arch/x86/kernel/alternative.c +21 -21
@@ -611,37 +611,36 @@ struct patch_site {u8 len; }; -static void __init_or_module analyze_patch_site(struct patch_site *ps, -struct alt_instr *p,struct alt_instr *end) +static struct alt_instr * __init_or_module analyze_patch_site( +struct patch_site *ps,struct alt_instr *p,struct alt_instr *end) { -struct alt_instr *r; -/* * In case of nested ALTERNATIVE()s the outer alternative might add * more padding. To ensure consistent patching find the max padding for * all alt_instr entries for this site (nested alternatives result in * consecutive entries). +* Find the last alt_instr eligible for patching at the site. */ ps->instr =instr_va (p); - ps->len = p->instrlen; -for (r = p+1 ; r < end &&instr_va (r) == ps->instr; r++) { - ps->len =max (ps->len, r->instrlen); - p->instrlen = r->instrlen = ps->len; + ps->alt =NULL ; + ps->len =0 ; +for (; p < end &&instr_va (p) == ps->instr; p++) { + ps->len =max (ps->len, p->instrlen); + +BUG_ON (p->cpuid >= (NCAPINTS +NBUGINTS ) *32 ); +/* +* Patch if either: +* - feature is present +* - feature not present but ALT_FLAG_NOT is set to mean, +* patch if feature is *NOT* present. +*/ +if (!boot_cpu_has (p->cpuid) != !(p->flags &ALT_FLAG_NOT )) + ps->alt = p; }BUG_ON (ps->len >sizeof (ps->buff)); -BUG_ON (p->cpuid >= (NCAPINTS +NBUGINTS ) *32 ); -/* -* Patch if either: -* - feature is present -* - feature not present but ALT_FLAG_NOT is set to mean, -* patch if feature is *NOT* present. -*/ -if (!boot_cpu_has (p->cpuid) == !(p->flags &ALT_FLAG_NOT )) - ps->alt =NULL ; -else - ps->alt = p; +return p; } static void __init_or_module prep_patch_site(struct patch_site *ps)
@@ -720,10 +719,11 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,* So be careful if you want to change the scan order to any other * order. */ -for (a = start; a < end; a++) { + a = start; +while (a < end) {struct patch_site ps; -analyze_patch_site (&ps, a, end); + a =analyze_patch_site (&ps, a, end);prep_patch_site (&ps);patch_site (&ps); }
Signature · 1 line
2.51.0
Borislav Petkov
maintainerQuoted 26 lines · Juergen Gross
Instead of patching a single location potentially multiple times in case of nested ALTERNATIVE()s, do the patching only after having evaluated all
alt_instrinstances for that location.This has multiple advantages:
- In case of replacing an indirect with a direct call using the
ALT_FLAG_DIRECT_CALLflag, there is no longer the need to have that instance before any other instances at the same location (the original instruction is needed for finding the target of the direct call).
This issue has been hit when trying to do paravirt patching similar to the following:
ALTERNATIVE_2(PARAVIRT_CALL, // indirect call
instr, feature, // native instruction
ALT_CALL_INSTR,X86_FEATURE_XENPV) // Xen function
In case "feature" was true, "instr" replaced the indirect call. Under Xen PV the patching to have a direct call failed, as the original indirect call was no longer there to find the call target.- In case of nested ALTERNATIVE()s there is no intermediate replacement visible. This avoids any problems in case e.g. an interrupt is happening between the single instances and the patched location is used during handling the interrupt.
Signed-off-by Juergen Gross <[email protected]>
Quoted 8 lines
---
V2:
- complete rework (Boris Petkov)
V3:
- rebase to added patch 2--- arch/x86/kernel/alternative.c | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-)
That one would then look like this as I've moved the struct patch_site ps at the beginning of the loop, where it belongs.
Other than that, yap, looks ok, and it seems to work.
Thx.
---
arch/x86/kernel/alternative.c +27 -23
@@ -593,39 +593,39 @@ struct patch_site {u8 len; }; -static void __init_or_module analyze_patch_site(struct patch_site *ps, -struct alt_instr *start, -struct alt_instr *end) +static struct alt_instr * __init_or_module analyze_patch_site(struct patch_site *ps, +struct alt_instr *start, +struct alt_instr *end) { -struct alt_instr *r; +struct alt_instr *p = start; ps->instr =instr_va (start); - ps->len = start->instrlen;/* * In case of nested ALTERNATIVE()s the outer alternative might add * more padding. To ensure consistent patching find the max padding for * all alt_instr entries for this site (nested alternatives result in * consecutive entries). +* Find the last alt_instr eligible for patching at the site. */ -for (r = start+1 ; r < end &&instr_va (r) == ps->instr; r++) { - ps->len =max (ps->len, r->instrlen); - start->instrlen = r->instrlen = ps->len; +for (; p < end &&instr_va (p) == ps->instr; p++) { + ps->len =max (ps->len, p->instrlen); + +BUG_ON (p->cpuid >= (NCAPINTS +NBUGINTS ) *32 ); + +/* +* Patch if either: +* - feature is present +* - feature not present but ALT_FLAG_NOT is set to mean, +* patch if feature is *NOT* present. +*/ +if (!boot_cpu_has (p->cpuid) != !(p->flags &ALT_FLAG_NOT )) + ps->alt = p; }BUG_ON (ps->len >sizeof (ps->buff)); -BUG_ON (start->cpuid >= (NCAPINTS +NBUGINTS ) *32 ); -/* -* Patch if either: -* - feature is present -* - feature not present but ALT_FLAG_NOT is set to mean, -* patch if feature is *NOT* present. -*/ -if (!boot_cpu_has (start->cpuid) == !(start->flags &ALT_FLAG_NOT )) - ps->alt =NULL ; -else - ps->alt = start; +return p; } static void __init_or_module prep_patch_site(struct patch_site *ps)
@@ -704,10 +704,14 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,* So be careful if you want to change the scan order to any other * order. */ -for (a = start; a < end; a++) { -struct patch_site ps; - -analyze_patch_site (&ps, a, end); + a = start; +while (a < end) { +struct patch_site ps = { + .alt =NULL , + .len =0 + }; + + a =analyze_patch_site (&ps, a, end);prep_patch_site (&ps);patch_site (&ps); }
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePing?
Quoted 24 lines · Juergen Gross
Instead of patching a single location potentially multiple times in case of nested ALTERNATIVE()s, do the patching only after having evaluated all
alt_instrinstances for that location.Changes in V2:
- complete rework (Boris Petkov)Changes in V3:
- split former V2 patch into 2 by introducing a helper function (Boris Petkov)
- repost the small cleanup patch 1 which was taken before, but has somehow vanished from the tip x86/alternative branch (it is still in the tip master branch, but I couldn't find it in any other tip branch).Changes in V4:
- use 3 helpers instead of 1 (Boris Petkov)Juergen Gross (3):
x86/alternative: Drop not needed test after call ofalt_replace_call()x86/alternative: Use helper functions for patching alternatives x86/alternative: Patch a single alternative location only oncearch/x86/kernel/alternative.c | 147 ++++++++++++++++++++-------------- 1 file changed, 87 insertions(+), 60 deletions(-)
Borislav Petkov
maintainerQuoted 1 line · Juergen Gross
Ping?
On vacation.
Will take a look when I get back.
Thx.
Small device. Typos and formatting crap