From: "Woodhouse, David" <dwmw@amazon.co.uk>
To: Paul Turner <pjt@google.com>,
Peter Zijlstra <peterz@infradead.org>,
"Van De Ven, Arjan" <arjan.van.de.ven@intel.com>
Cc: Andi Kleen <andi@firstfloor.org>,
LKML <linux-kernel@vger.kernel.org>,
Greg Kroah-Hartman <gregkh@linux-foundation.org>,
Tim Chen <tim.c.chen@linux.intel.com>,
Dave Hansen <dave.hansen@intel.com>,
"Thomas Gleixner" <tglx@linutronix.de>,
Andy Lutomirski <luto@amacapital.net>,
"Andi Kleen" <ak@linux.intel.com>
Subject: Re: [PATCH] x86/retpoline: Avoid return buffer underflows on context switch
Date: Tue, 9 Jan 2018 00:44:33 +0000 [thread overview]
Message-ID: <1515458673.4423.79.camel@amazon.co.uk> (raw)
In-Reply-To: <CAPM31R+bsdYftUPL8cca1J64=EGuV+uPWoRHc7HXZO4cQ8xiCQ@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2747 bytes --]
On Mon, 2018-01-08 at 16:15 -0800, Paul Turner wrote:
> On Mon, Jan 8, 2018 at 2:11 PM, Peter Zijlstra <peterz@infradead.org> wrote:
> > On Mon, Jan 08, 2018 at 12:15:31PM -0800, Andi Kleen wrote:
> >> diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
> >> index b8c8eeacb4be..e84e231248c2 100644
> >> --- a/arch/x86/include/asm/nospec-branch.h
> >> +++ b/arch/x86/include/asm/nospec-branch.h
> >> @@ -53,6 +53,35 @@
> >> #endif
> >> .endm
> >>
> >> +/*
> >> + * We use 32-N: 32 is the max return buffer size,
> >> + * but there should have been at a minimum two
> >> + * controlled calls already: one into the kernel
> >> + * from entry*.S and another into the function
> >> + * containing this macro. So N=2, thus 30.
> >> + */
> >> +#define NUM_BRANCHES_TO_FILL 30
> >> +
> >> +/*
> >> + * Fill the CPU return branch buffer to prevent
> >> + * indirect branch prediction on underflow.
> >> + * Caller should check for X86_FEATURE_SMEP and X86_FEATURE_RETPOLINE
> >> + */
> >> +.macro FILL_RETURN_BUFFER
> >> +#ifdef CONFIG_RETPOLINE
> >> + .rept NUM_BRANCHES_TO_FILL
> >> + call 1221f
> >> + pause /* stop speculation */
> >> +1221:
> >> + .endr
> >> +#ifdef CONFIG_64BIT
> >> + addq $8*NUM_BRANCHES_TO_FILL, %rsp
> >> +#else
> >> + addl $4*NUM_BRANCHES_TO_FILL, %esp
> >> +#endif
> >> +#endif
> >> +.endm
> >
> > So pjt did alignment, a single unroll and per discussion earlier today
> > (CET) or late last night (PST), he only does 16.
> >
> > Why is none of that done here? Also, can we pretty please stop using
> > those retarded number labels, they make this stuff unreadable.
> >
> > Also, pause is unlikely to stop speculation, that comment doesn't make
> > sense. Looking at PJT's version there used to be a speculation trap in
> > there, but I can't see that here.
> >
>
> You definitely want the speculation traps.. these entries are
> potentially consumed.
> Worse: The first entry that will be consumed is the last call in your
> linear chain, meaning that it immediately gets to escape into
> alternative execution.
> (When I was experimenting with icache-minimizing constructions here I
> actually used intentional backwards jumps in linear chains to avoid
> this.)
>
> The sequence I reported is what ended up seeming optimal.
On IRC, Arjan assures me that 'pause' here really is sufficient as a
speculation trap. If we do end up returning back here as a
misprediction, that 'pause' will stop the speculative execution on
affected CPUs even though it isn't *architecturally* documented to do
so.
Arjan, can you confirm that in email please?
[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 5210 bytes --]
next prev parent reply other threads:[~2018-01-09 0:45 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-08 20:15 [PATCH] x86/retpoline: Avoid return buffer underflows on context switch Andi Kleen
2018-01-08 21:38 ` David Woodhouse
2018-01-08 22:54 ` Andi Kleen
2018-01-08 22:11 ` Peter Zijlstra
2018-01-08 22:17 ` Woodhouse, David
2018-01-08 22:26 ` Peter Zijlstra
2018-01-08 23:25 ` Woodhouse, David
2018-01-08 22:25 ` Andi Kleen
2018-01-08 22:58 ` Andi Kleen
2018-01-09 0:15 ` Paul Turner
2018-01-09 0:15 ` Paul Turner
2018-01-09 0:44 ` Woodhouse, David [this message]
2018-01-09 0:48 ` David Woodhouse
2018-01-09 2:48 ` Paul Turner
2018-01-09 3:05 ` David Woodhouse
2018-01-12 11:15 ` David Laight
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=1515458673.4423.79.camel@amazon.co.uk \
--to=dwmw@amazon.co.uk \
--cc=ak@linux.intel.com \
--cc=andi@firstfloor.org \
--cc=arjan.van.de.ven@intel.com \
--cc=dave.hansen@intel.com \
--cc=gregkh@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=peterz@infradead.org \
--cc=pjt@google.com \
--cc=tglx@linutronix.de \
--cc=tim.c.chen@linux.intel.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.