Skip to contentSkip to conversation

livepatch: apply_alternatives() is only used for livepatch

PATCH Superseded Roger Pau Monne 4 participants 7 messages Jun 6, 2023 – Jun 7, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 livepatch: apply_alternatives() is only used for livepatch

Guard it with CONFIG_LIVEPATCH. Note alternatives are applied at boot using _apply_alternatives().

---
 xen/arch/arm/alternative.c             | 2 ++
 xen/arch/arm/include/asm/alternative.h | 2 ++
 xen/arch/x86/alternative.c             | 5 +++--
 xen/arch/x86/include/asm/alternative.h | 2 ++
 4 files changed, 9 insertions(+), 2 deletions(-)
xen/arch/arm/alternative.c +2 -0
index 7366af4ea646..016e66978b6d 100644
@@ -223,6 +223,7 @@ void __init apply_alternatives_all(void)
     vunmap(xenmap);
 }
 
+#ifdef CONFIG_LIVEPATCH
 int apply_alternatives(const struct alt_instr *start, const struct alt_instr *end)
 {
     const struct alt_region region = {
@@ -232,6 +233,7 @@ int apply_alternatives(const struct alt_instr *start, const struct alt_instr *en
 
     return __apply_alternatives(&region, 0);
 }
+#endif
 
 /*
  * Local variables:
xen/arch/arm/include/asm/alternative.h +2 -0
index d3210e82f9e5..ce82dc1ca0d2 100644
@@ -29,7 +29,9 @@ typedef void (*alternative_cb_t)(const struct alt_instr *alt,
 				 int nr_inst);
 
 void apply_alternatives_all(void);
+#ifdef CONFIG_LIVEPATCH
 int apply_alternatives(const struct alt_instr *start, const struct alt_instr *end);
+#endif
 
 #define ALTINSTR_ENTRY(feature, cb)					      \
 	" .word 661b - .\n"				/* label           */ \
xen/arch/x86/alternative.c +3 -2
index 99482766b51f..21af0e825822 100644
@@ -358,11 +358,12 @@ static void init_or_livepatch _apply_alternatives(struct alt_instr *start,
     }
 }
 
-void init_or_livepatch apply_alternatives(struct alt_instr *start,
-                                          struct alt_instr *end)
+#ifdef CONFIG_LIVEPATCH
+void apply_alternatives(struct alt_instr *start, struct alt_instr *end)
 {
     _apply_alternatives(start, end, true);
 }
+#endif
 
 static unsigned int __initdata alt_todo;
 static unsigned int __initdata alt_done;
xen/arch/x86/include/asm/alternative.h +2 -0
index a1cd6a9fe5b8..688b554099b3 100644
@@ -24,7 +24,9 @@ struct __packed alt_instr {
 
 extern void add_nops(void *insns, unsigned int len);
 /* Similar to alternative_instructions except it can be run with IRQs enabled. */
+#ifdef CONFIG_LIVEPATCH
 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
+#endif
 extern void alternative_instructions(void);
 extern void alternative_branches(void);
 
Signature · 1 line
2.40.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Roger,

Quoted 4 lines · Roger Pau Monne

Guard it with CONFIG_LIVEPATCH. Note alternatives are applied at boot using _apply_alternatives().

Quoted 46 lines
---
  xen/arch/arm/alternative.c             | 2 ++
  xen/arch/arm/include/asm/alternative.h | 2 ++
  xen/arch/x86/alternative.c             | 5 +++--
  xen/arch/x86/include/asm/alternative.h | 2 ++
  4 files changed, 9 insertions(+), 2 deletions(-)
xen/arch/arm/alternative.c +2 -0
index 7366af4ea646..016e66978b6d 100644
@@ -223,6 +223,7 @@ void __init apply_alternatives_all(void)
      vunmap(xenmap);
  }
 
+#ifdef CONFIG_LIVEPATCH
  int apply_alternatives(const struct alt_instr *start, const struct alt_instr *end)
  {
      const struct alt_region region = {
@@ -232,6 +233,7 @@ int apply_alternatives(const struct alt_instr *start, const struct alt_instr *en
 
      return __apply_alternatives(&region, 0);
  }
+#endif
 
  /*
   * Local variables:
xen/arch/arm/include/asm/alternative.h +2 -0
index d3210e82f9e5..ce82dc1ca0d2 100644
@@ -29,7 +29,9 @@ typedef void (*alternative_cb_t)(const struct alt_instr *alt,
  				 int nr_inst);
 
  void apply_alternatives_all(void);
+#ifdef CONFIG_LIVEPATCH
  int apply_alternatives(const struct alt_instr *start, const struct alt_instr *end);
+#endif
 
  #define ALTINSTR_ENTRY(feature, cb)					      \
  	" .word 661b - .\n"				/* label           */ \
xen/arch/x86/alternative.c +0 -0
index 99482766b51f..21af0e825822 100644

xen/arch/x86/alternative.c static void init_or_livepatch _apply_alternatives(struct alt_instr *start, · line 362

Quoted 5 lines
xen/arch/x86/alternative.c +0 -2
@@ -358,11 +358,12 @@ static void init_or_livepatch _apply_alternatives(struct alt_instr *start,
      }
  }
 
-void init_or_livepatch apply_alternatives(struct alt_instr *start,
-                                          struct alt_instr *end)

NIT: It sounds like the init_* was a left-over after commit 67d01cdb5518 ("x86: infrastructure to allow converting certain indirect calls to direct ones").

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw
Quoted 7 lines · Julien Grall
Quoted 6 lines · Roger Pau Monne
xen/arch/x86/alternative.c +0 -0
@@ -358,11 +358,12 @@ static void init_or_livepatch _apply_alternatives(struct alt_instr *start,
      }
  }
 
Quoted 6 lines

xen/arch/x86/alternative.c static void init_or_livepatch _apply_alternatives(struct alt_instr *start, · line 362

Quoted 2 lines
xen/arch/x86/alternative.c +0 -2
-void init_or_livepatch apply_alternatives(struct alt_instr *start,
-                                          struct alt_instr *end)

NIT: It sounds like the init_* was a left-over after commit 67d01cdb5518 ("x86: infrastructure to allow converting certain indirect calls to direct ones").

Iirc this was intentional, to avoid the need for an #ifdef.

Jan

#3 lore raw
In reply to Julien Grall
Quoted 9 lines · Julien Grall

Hi Roger,

Quoted 4 lines · Roger Pau Monne

Guard it with CONFIG_LIVEPATCH. Note alternatives are applied at boot using _apply_alternatives().

Thanks!

Quoted 41 lines
Quoted 41 lines
---
  xen/arch/arm/alternative.c             | 2 ++
  xen/arch/arm/include/asm/alternative.h | 2 ++
  xen/arch/x86/alternative.c             | 5 +++--
  xen/arch/x86/include/asm/alternative.h | 2 ++
  4 files changed, 9 insertions(+), 2 deletions(-)
xen/arch/arm/alternative.c +2 -0
index 7366af4ea646..016e66978b6d 100644
@@ -223,6 +223,7 @@ void __init apply_alternatives_all(void)
      vunmap(xenmap);
  }
+#ifdef CONFIG_LIVEPATCH
  int apply_alternatives(const struct alt_instr *start, const struct alt_instr *end)
  {
      const struct alt_region region = {
@@ -232,6 +233,7 @@ int apply_alternatives(const struct alt_instr *start, const struct alt_instr *en
      return __apply_alternatives(&region, 0);
  }
+#endif
  /*
   * Local variables:
xen/arch/arm/include/asm/alternative.h +2 -0
index d3210e82f9e5..ce82dc1ca0d2 100644
@@ -29,7 +29,9 @@ typedef void (*alternative_cb_t)(const struct alt_instr *alt,
  				 int nr_inst);
  void apply_alternatives_all(void);
+#ifdef CONFIG_LIVEPATCH
  int apply_alternatives(const struct alt_instr *start, const struct alt_instr *end);
+#endif
  #define ALTINSTR_ENTRY(feature, cb)					      \
  	" .word 661b - .\n"				/* label           */ \
xen/arch/x86/alternative.c +0 -0
index 99482766b51f..21af0e825822 100644
@@ -358,11 +358,12 @@ static void init_or_livepatch _apply_alternatives(struct alt_instr *start,
Quoted 8 lines

xen/arch/x86/alternative.c static void init_or_livepatch _apply_alternatives(struct alt_instr *start, · line 362

Quoted 4 lines
xen/arch/x86/alternative.c +0 -2
      }
  }
-void init_or_livepatch apply_alternatives(struct alt_instr *start,
-                                          struct alt_instr *end)

NIT: It sounds like the init_* was a left-over after commit 67d01cdb5518 ("x86: infrastructure to allow converting certain indirect calls to direct ones").

I think it doesn't warrant a fixes tag in this case. The commit you point out should also have added the livepatch guards in x86 at least, since that's the only caller of apply_alternatives() after the patch.

Thanks, Roger.

#4 lore raw
In reply to Roger Pau Monne

xen/arch/x86/include/asm/alternative.h struct __packed alt_instr { · line 29

Quoted 11 lines · Roger Pau Monne
xen/arch/x86/include/asm/alternative.h +2 -0
index a1cd6a9fe5b8..688b554099b3 100644
@@ -24,7 +24,9 @@ struct __packed alt_instr {
 
 extern void add_nops(void *insns, unsigned int len);
 /* Similar to alternative_instructions except it can be run with IRQs enabled. */
+#ifdef CONFIG_LIVEPATCH
 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
+#endif

Given that this is called by common code, it shouldn't live in an arch-specific header, and it absolutely shouldn't live identically in 2 different arch's header files.

As this is a cleanup patch, we should gain a xen/alternative.h which depending on CONFIG_ALTERNATIVE includes asm/alternative.h

This will help RISC-V too (a little).

~Andrew

#5 lore raw
In reply to Roger Pau Monne

xen/arch/x86/include/asm/alternative.h struct __packed alt_instr { · line 29

Quoted 9 lines · Roger Pau Monne
xen/arch/x86/include/asm/alternative.h +2 -0
@@ -24,7 +24,9 @@ struct __packed alt_instr {
 
 extern void add_nops(void *insns, unsigned int len);
 /* Similar to alternative_instructions except it can be run with IRQs enabled. */
+#ifdef CONFIG_LIVEPATCH
 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
+#endif

I don't see the need for an #ifdef on the declaration. We avoid such in a fair number of other cases, keeping the code better readable.

Jan

#6 lore raw
Quoted 5 lines · Jan Beulich
Quoted 4 lines · Roger Pau Monne
xen/arch/x86/include/asm/alternative.h +0 -0
@@ -24,7 +24,9 @@ struct __packed alt_instr {
 
Quoted 8 lines

xen/arch/x86/include/asm/alternative.h struct __packed alt_instr { · line 29

Quoted 5 lines
xen/arch/x86/include/asm/alternative.h +2 -0
 extern void add_nops(void *insns, unsigned int len);
 /* Similar to alternative_instructions except it can be run with IRQs enabled. */
+#ifdef CONFIG_LIVEPATCH
 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
+#endif

I don't see the need for an #ifdef on the declaration. We avoid such in a fair number of other cases, keeping the code better readable.

Hm, yes, we will get a linker error anyway if attempted to use without livepatch enabled.

Thanks, Roger.