Skip to contentSkip to conversation

xen/x86/alternatives: Do not use sync_core() to serialize I$

PATCH In review Andrew Cooper 4 participants 9 messages May 19, 2017 – May 22, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 xen/x86/alternatives: Do not use sync_core() to serialize I$

From: Borislav Petkov <[email protected]>

We use sync_core() in the alternatives code to stop speculative execution of prefetched instructions because we are potentially changing them and don't want to execute stale bytes.

What it does on most machines is call CPUID which is a serializing instruction. And that's expensive.

However, the instruction cache is serialized when we're on the local CPU and are changing the data through the same virtual address. So then, we don't need the serializing CPUID but a simple control flow change. Last being accomplished with a CALL/RET which the noinline causes.

[Linux commit 34bfab0eaf0fb5c6fb14c6b4013b06cdc7984466]

Ported to Xen.

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/alternative.c | 11 ++++++-----
 xen/arch/x86/livepatch.c   | 20 +++++++++++++++-----
 2 files changed, 21 insertions(+), 10 deletions(-)
xen/arch/x86/alternative.c +6 -5
index 6eaa10f..65062c2 100644
@@ -128,13 +128,14 @@ void init_or_livepatch add_nops(void *insns, unsigned int len)
  *
  * You should run this with interrupts disabled or on code that is not
  * executing.
+ *
+ * "noinline" to cause control flow change and thus invalidate I$ and
+ * cause refetch after modification.
  */
-static void *init_or_livepatch text_poke(void *addr, const void *opcode, size_t len)
+static void *init_or_livepatch noinline
+text_poke(void *addr, const void *opcode, size_t len)
 {
-    memcpy(addr, opcode, len);
-    sync_core();
-
-    return addr;
+    return memcpy(addr, opcode, len);
 }
 
 /*
xen/arch/x86/livepatch.c +15 -5
index 9663ef6..dd50dd1 100644
@@ -46,7 +46,11 @@ int arch_livepatch_verify_func(const struct livepatch_func *func)
     return 0;
 }
 
-void arch_livepatch_apply(struct livepatch_func *func)
+/*
+ * "noinline" to cause control flow change and thus invalidate I$ and
+ * cause refetch after modification.
+ */
+void noinline arch_livepatch_apply(struct livepatch_func *func)
 {
     uint8_t *old_ptr;
     uint8_t insn[sizeof(func->opaque)];
@@ -75,15 +79,21 @@ void arch_livepatch_apply(struct livepatch_func *func)
     memcpy(old_ptr, insn, len);
 }
 
-void arch_livepatch_revert(const struct livepatch_func *func)
+/*
+ * "noinline" to cause control flow change and thus invalidate I$ and
+ * cause refetch after modification.
+ */
+void noinline arch_livepatch_revert(const struct livepatch_func *func)
 {
     memcpy(func->old_addr, func->opaque, livepatch_insn_len(func));
 }
 
-/* Serialise the CPU pipeline. */
-void arch_livepatch_post_action(void)
+/*
+ * "noinline" to cause control flow change and thus invalidate I$ and
+ * cause refetch after modification.
+ */
+void noinline arch_livepatch_post_action(void)
 {
-    cpuid_eax(0);
 }
 
 static nmi_callback_t *saved_nmi_callback;
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw

PATCH 2/2 xen/x86: Drop sync_core()

As identified in Linux c/s c198b121b1a1d "x86/asm: Rewrite sync_core() to use IRET-to-self", sync_core() is only appropriate for two very specific usecases.

Xen doesn't have need of either of these usecases, so drop sync_core() to avoid any misuse.

In the unlikely event that we do gain a legitimate use for sync_core(), it should be reintroduced as a mov to %cr2 rather than cpuid, which has a lower overhead.

---
CC: Jan Beulich <[email protected]>

---
 xen/include/asm-x86/processor.h | 11 -----------
 1 file changed, 11 deletions(-)
xen/include/asm-x86/processor.h +0 -11
index 1d1a4ff..6a335d3 100644
@@ -396,17 +396,6 @@ static inline bool_t read_pkru_wd(uint32_t pkru, unsigned int pkey)
     outb((data), 0x23); \
 } while (0)
 
-/* Stop speculative execution */
-static inline void sync_core(void)
-{
-    int tmp;
-    asm volatile (
-        "cpuid"
-        : "=a" (tmp)
-        : "0" (1)
-        : "ebx","ecx","edx","memory" );
-}
-
 static always_inline void __monitor(const void *eax, unsigned long ecx,
                                     unsigned long edx)
 {
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH for-next 2/2] xen/x86: Drop sync_core()

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 19.05.17 at 20:49, <[email protected]> wrote:

As identified in Linux c/s c198b121b1a1d "x86/asm: Rewrite sync_core() to use IRET-to-self", sync_core() is only appropriate for two very specific usecases.

Quoted 8 lines

Xen doesn't have need of either of these usecases, so drop sync_core() to avoid any misuse.

In the unlikely event that we do gain a legitimate use for sync_core(), it should be reintroduced as a mov to %cr2 rather than cpuid, which has a lower overhead.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
In reply to Andrew Cooper
Quoted 19 lines · Andrew Cooper

From: Borislav Petkov <[email protected]>

We use sync_core() in the alternatives code to stop speculative execution of prefetched instructions because we are potentially changing them and don't want to execute stale bytes.

What it does on most machines is call CPUID which is a serializing instruction. And that's expensive.

However, the instruction cache is serialized when we're on the local CPU and are changing the data through the same virtual address. So then, we don't need the serializing CPUID but a simple control flow change. Last being accomplished with a CALL/RET which the noinline causes.

[Linux commit 34bfab0eaf0fb5c6fb14c6b4013b06cdc7984466]
Quoted 8 lines

Ported to Xen.

---
CC: Jan Beulich <[email protected]>

---
 xen/arch/x86/alternative.c | 11 ++++++-----
 xen/arch/x86/livepatch.c   | 20 +++++++++++++++-----

Weird that the CC list didn't pick me up.

But Acked-by on the livepatch part.

Quoted 77 lines
 2 files changed, 21 insertions(+), 10 deletions(-)
xen/arch/x86/alternative.c +6 -5
index 6eaa10f..65062c2 100644
@@ -128,13 +128,14 @@ void init_or_livepatch add_nops(void *insns, unsigned int len)
  *
  * You should run this with interrupts disabled or on code that is not
  * executing.
+ *
+ * "noinline" to cause control flow change and thus invalidate I$ and
+ * cause refetch after modification.
  */
-static void *init_or_livepatch text_poke(void *addr, const void *opcode, size_t len)
+static void *init_or_livepatch noinline
+text_poke(void *addr, const void *opcode, size_t len)
 {
-    memcpy(addr, opcode, len);
-    sync_core();
-
-    return addr;
+    return memcpy(addr, opcode, len);
 }
 
 /*
xen/arch/x86/livepatch.c +15 -5
index 9663ef6..dd50dd1 100644
@@ -46,7 +46,11 @@ int arch_livepatch_verify_func(const struct livepatch_func *func)
     return 0;
 }
 
-void arch_livepatch_apply(struct livepatch_func *func)
+/*
+ * "noinline" to cause control flow change and thus invalidate I$ and
+ * cause refetch after modification.
+ */
+void noinline arch_livepatch_apply(struct livepatch_func *func)
 {
     uint8_t *old_ptr;
     uint8_t insn[sizeof(func->opaque)];
@@ -75,15 +79,21 @@ void arch_livepatch_apply(struct livepatch_func *func)
     memcpy(old_ptr, insn, len);
 }
 
-void arch_livepatch_revert(const struct livepatch_func *func)
+/*
+ * "noinline" to cause control flow change and thus invalidate I$ and
+ * cause refetch after modification.
+ */
+void noinline arch_livepatch_revert(const struct livepatch_func *func)
 {
     memcpy(func->old_addr, func->opaque, livepatch_insn_len(func));
 }
 
-/* Serialise the CPU pipeline. */
-void arch_livepatch_post_action(void)
+/*
+ * "noinline" to cause control flow change and thus invalidate I$ and
+ * cause refetch after modification.
+ */
+void noinline arch_livepatch_post_action(void)
 {
-    cpuid_eax(0);
 }
 
 static nmi_callback_t *saved_nmi_callback;
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

Acked-by on v1 1/2
#4 lore raw
In reply to Andrew Cooper
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 19.05.17 at 20:49, <[email protected]> wrote:

We use sync_core() in the alternatives code to stop speculative execution of prefetched instructions because we are potentially changing them and don't want to execute stale bytes.

What it does on most machines is call CPUID which is a serializing instruction. And that's expensive.

However, the instruction cache is serialized when we're on the local CPU and are changing the data through the same virtual address.

Do you have the background of this "same virtual address" constraint? Caches are physically indexed, so I don't see the connection. Yet if there is one, our stub generation in the emulator may have an issue.

Quoted 4 lines

So then, we
don't need the serializing CPUID but a simple control flow change. Last being accomplished with a CALL/RET which the noinline causes.

Quoted 8 lines
[Linux commit 34bfab0eaf0fb5c6fb14c6b4013b06cdc7984466]

Ported to Xen.

with a question:

Quoted 11 lines
xen/arch/x86/alternative.c +3 -1
@@ -128,13 +128,14 @@ void init_or_livepatch add_nops(void *insns, unsigned int len)
  *
  * You should run this with interrupts disabled or on code that is not
  * executing.
+ *
+ * "noinline" to cause control flow change and thus invalidate I$ and
+ * cause refetch after modification.
  */
-static void *init_or_livepatch text_poke(void *addr, const void *opcode, 

xen/arch/x86/alternative.c void init_or_livepatch add_nops(void *insns, unsigned int len) · line 140

Quoted 10 lines
xen/arch/x86/alternative.c +3 -4
 size_t len)
+static void *init_or_livepatch noinline
+text_poke(void *addr, const void *opcode, size_t len)
 {
-    memcpy(addr, opcode, len);
-    sync_core();
-
-    return addr;
+    return memcpy(addr, opcode, len);
 }

What if this is patching memcpy() itself?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 19.05.17 at 20:49, <[email protected]> wrote:

We use sync_core() in the alternatives code to stop speculative execution of prefetched instructions because we are potentially changing them and don't want to execute stale bytes.

Quoted 7 lines
Quoted 5 lines

What it does on most machines is call CPUID which is a serializing instruction. And that's expensive.

However, the instruction cache is serialized when we're on the local CPU and are changing the data through the same virtual address.

Do you have the background of this "same virtual address" constraint?

There was a long LKML thread on the subject.
https://lkml.org/lkml/2016/12/3/108

Quoted 3 lines

Caches are physically indexed, so I don't see the connection. Yet if there is one, our stub generation in the emulator may have an issue.

I think https://lkml.org/lkml/2016/12/2/454 is probably the relevant statement.

Quoted 30 lines
Quoted 5 lines

So then, we
don't need the serializing CPUID but a simple control flow change. Last being accomplished with a CALL/RET which the noinline causes.

Quoted 8 lines
[Linux commit 34bfab0eaf0fb5c6fb14c6b4013b06cdc7984466]

Ported to Xen.

with a question:

Quoted 14 lines
xen/arch/x86/alternative.c +5 -1
@@ -128,13 +128,14 @@ void init_or_livepatch add_nops(void *insns, unsigned int len)
  *
  * You should run this with interrupts disabled or on code that is not
  * executing.
+ *
+ * "noinline" to cause control flow change and thus invalidate I$ and
+ * cause refetch after modification.
  */
-static void *init_or_livepatch text_poke(void *addr, const void *opcode, 
 size_t len)
+static void *init_or_livepatch noinline
+text_poke(void *addr, const void *opcode, size_t len)
Quoted 8 lines

xen/arch/x86/alternative.c void init_or_livepatch add_nops(void *insns, unsigned int len) · line 139

Quoted 7 lines
xen/arch/x86/alternative.c +1 -4
 {
-    memcpy(addr, opcode, len);
-    sync_core();
-
-    return addr;
+    return memcpy(addr, opcode, len);
 }

What if this is patching memcpy() itself?

That would be fine, because its still the same linear addresses, and would still be synchronised.

In reality for livepatching, we only modify the 5 byte prefix at the head of the functions, so this memcpy() wouldn't actually hit itself.

Of course, you could construct a patch which would cause memcpy() to actually hit itself, but that isn't going to work at all, irrespective of serialising instructions.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#6 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.05.17 at 15:51, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.05.17 at 20:49, <[email protected]> wrote:

We use sync_core() in the alternatives code to stop speculative execution of prefetched instructions because we are potentially changing them and don't want to execute stale bytes.

What it does on most machines is call CPUID which is a serializing instruction. And that's expensive.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

However, the instruction cache is serialized when we're on the local CPU and are changing the data through the same virtual address.

Do you have the background of this "same virtual address" constraint?

There was a long LKML thread on the subject.
https://lkml.org/lkml/2016/12/3/108

Well, interesting reading (and at least part of it was Cc-ed to xen-devel iirc), but none of it nor ...

Quoted 6 lines
Quoted 3 lines

Caches are physically indexed, so I don't see the connection. Yet if there is one, our stub generation in the emulator may have an issue.

I think https://lkml.org/lkml/2016/12/2/454 is probably the relevant statement.

... this one doesn't give any background at all of why the virtual address would matter here. Searching the SDM I also can't find any statement as to virtual or physical indexing being used for any of the caches.

Quoted 26 lines
Quoted 14 lines
Quoted 14 lines
xen/arch/x86/alternative.c +5 -1
@@ -128,13 +128,14 @@ void init_or_livepatch add_nops(void *insns, unsigned int len)
  *
  * You should run this with interrupts disabled or on code that is not
  * executing.
+ *
+ * "noinline" to cause control flow change and thus invalidate I$ and
+ * cause refetch after modification.
  */
-static void *init_or_livepatch text_poke(void *addr, const void *opcode, 
 size_t len)
+static void *init_or_livepatch noinline
+text_poke(void *addr, const void *opcode, size_t len)
Quoted 8 lines

xen/arch/x86/alternative.c void init_or_livepatch add_nops(void *insns, unsigned int len) · line 139

Quoted 7 lines
xen/arch/x86/alternative.c +1 -4
 {
-    memcpy(addr, opcode, len);
-    sync_core();
-
-    return addr;
+    return memcpy(addr, opcode, len);
 }

What if this is patching memcpy() itself?

That would be fine, because its still the same linear addresses, and would still be synchronised.

Quoted 6 lines

In reality for livepatching, we only modify the 5 byte prefix at the head of the functions, so this memcpy() wouldn't actually hit itself.

Of course, you could construct a patch which would cause memcpy() to actually hit itself, but that isn't going to work at all, irrespective of serialising instructions.

Hmm, my question was more from an alternative instruction patching viewpoint.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#7 lore raw
Quoted 25 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.05.17 at 15:51, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.05.17 at 20:49, <[email protected]> wrote:

We use sync_core() in the alternatives code to stop speculative execution of prefetched instructions because we are potentially changing them and don't want to execute stale bytes.

What it does on most machines is call CPUID which is a serializing instruction. And that's expensive.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

However, the instruction cache is serialized when we're on the local CPU and are changing the data through the same virtual address.

Do you have the background of this "same virtual address" constraint?

There was a long LKML thread on the subject.
https://lkml.org/lkml/2016/12/3/108

Well, interesting reading (and at least part of it was Cc-ed to xen-devel iirc), but none of it nor ...

Quoted 4 lines
Quoted 3 lines

Caches are physically indexed, so I don't see the connection. Yet if there is one, our stub generation in the emulator may have an issue.

Quoted 7 lines
Quoted 2 lines

I think https://lkml.org/lkml/2016/12/2/454 is probably the relevant statement.

... this one doesn't give any background at all of why the virtual address would matter here. Searching the SDM I also can't find any statement as to virtual or physical indexing being used for any of the caches.

SDM Vol. 3 chapter 11.6 (self modifying code):

A write to a memory location in a code segment that is currently cached in the processor causes the associated
cache line (or lines) to be invalidated. This check is based on the physical address of the instruction. In addition,
the P6 family and Pentium processors check whether a write to a code segment may modify an instruction that has
been prefetched for execution. If the write affects a prefetched instruction, the prefetch queue is invalidated. This
latter check is based on the linear address of the instruction.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#8 lore raw
Quoted 37 lines
Quoted 1 line
Quoted 1 line · On 22.05.17 at 16:26, <[email protected]> wrote:
Quoted 25 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 22.05.17 at 15:51, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.05.17 at 20:49, <[email protected]> wrote:

We use sync_core() in the alternatives code to stop speculative execution of prefetched instructions because we are potentially changing them and don't want to execute stale bytes.

What it does on most machines is call CPUID which is a serializing instruction. And that's expensive.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

However, the instruction cache is serialized when we're on the local CPU and are changing the data through the same virtual address.

Do you have the background of this "same virtual address" constraint?

There was a long LKML thread on the subject.
https://lkml.org/lkml/2016/12/3/108

Well, interesting reading (and at least part of it was Cc-ed to xen-devel iirc), but none of it nor ...

Quoted 4 lines
Quoted 3 lines

Caches are physically indexed, so I don't see the connection. Yet if there is one, our stub generation in the emulator may have an issue.

Quoted 7 lines
Quoted 2 lines

I think https://lkml.org/lkml/2016/12/2/454 is probably the relevant statement.

... this one doesn't give any background at all of why the virtual address would matter here. Searching the SDM I also can't find any statement as to virtual or physical indexing being used for any of the caches.

SDM Vol. 3 chapter 11.6 (self modifying code):

Quoted 9 lines

A write to a memory location in a code segment that is currently cached in the processor causes the associated
cache line (or lines) to be invalidated. This check is based on the physical address of the instruction. In addition,
the P6 family and Pentium processors check whether a write to a code segment may modify an instruction that has
been prefetched for execution. If the write affects a prefetched instruction, the prefetch queue is invalidated. This
latter check is based on the linear address of the instruction.

Right, but 64-bit processors aren't P6 family (at least per the usual grouping, where Pentium 4, Xeon, Core, and Atom processors are all sibling groups to "P6 family", even if the family numbers of all half way recent processors have been 6, leaving Xeon Phi aside). (That aside I'd of course expect physical vs virtual indexing information in e.g. the table titled "Characteristics of the Caches, TLBs, Store Buffer, and ...")

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel