xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
To: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: Wei Liu <wei.liu2@citrix.com>, Jan Beulich <JBeulich@suse.com>,
	Xen-devel <xen-devel@lists.xen.org>
Subject: Re: [PATCH for-next 1/2] xen/x86/alternatives: Do not use sync_core() to serialize I$
Date: Fri, 19 May 2017 20:29:18 -0400	[thread overview]
Message-ID: <20170520002918.GD21850@char.us.oracle.com> (raw)
In-Reply-To: <1495219747-26305-1-git-send-email-andrew.cooper3@citrix.com>

On Fri, May 19, 2017 at 07:49:06PM +0100, Andrew Cooper wrote:
> From: Borislav Petkov <bp@suse.de>
> 
> 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.
> 
> Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
> Signed-off-by: Borislav Petkov <bp@suse.de>
> Reviewed-by: Andy Lutomirski <luto@kernel.org>
> [Linux commit 34bfab0eaf0fb5c6fb14c6b4013b06cdc7984466]
> 
> Ported to Xen.
> 
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
> ---
> CC: Jan Beulich <JBeulich@suse.com>
> ---
>  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.

>  2 files changed, 21 insertions(+), 10 deletions(-)
> 
> diff --git a/xen/arch/x86/alternative.c b/xen/arch/x86/alternative.c
> index 6eaa10f..65062c2 100644
> --- a/xen/arch/x86/alternative.c
> +++ b/xen/arch/x86/alternative.c
> @@ -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);
>  }
>  
>  /*
> diff --git a/xen/arch/x86/livepatch.c b/xen/arch/x86/livepatch.c
> index 9663ef6..dd50dd1 100644
> --- a/xen/arch/x86/livepatch.c
> +++ b/xen/arch/x86/livepatch.c
> @@ -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;
> -- 
> 2.1.4
> 
> 
> _______________________________________________
> Xen-devel mailing list
> Xen-devel@lists.xen.org
> https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

  parent reply	other threads:[~2017-05-20  0:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-19 18:49 [PATCH for-next 1/2] xen/x86/alternatives: Do not use sync_core() to serialize I$ Andrew Cooper
2017-05-19 18:49 ` [PATCH for-next 2/2] xen/x86: Drop sync_core() Andrew Cooper
2017-05-22 13:39   ` Jan Beulich
2017-05-20  0:29 ` Konrad Rzeszutek Wilk [this message]
2017-05-22 13:38 ` [PATCH for-next 1/2] xen/x86/alternatives: Do not use sync_core() to serialize I$ Jan Beulich
2017-05-22 13:51   ` Andrew Cooper
2017-05-22 14:17     ` Jan Beulich
2017-05-22 14:26       ` Juergen Gross
2017-05-22 15:29         ` Jan Beulich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170520002918.GD21850@char.us.oracle.com \
    --to=konrad.wilk@oracle.com \
    --cc=JBeulich@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).