From: Nadav Amit <namit@vmware.com>
To: Andy Lutomirski <luto@kernel.org>
Cc: "H. Peter Anvin" <hpa@zytor.com>, Jiri Kosina <jikos@kernel.org>,
Linus Torvalds <torvalds@linux-foundation.org>,
Josh Poimboeuf <jpoimboe@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
the arch/x86 maintainers <x86@kernel.org>,
Linux List Kernel Mailing <linux-kernel@vger.kernel.org>,
Ard Biesheuvel <ard.biesheuvel@linaro.org>,
Steven Rostedt <rostedt@goodmis.org>,
Ingo Molnar <mingo@kernel.org>,
Thomas Gleixner <tglx@linutronix.de>,
Masami Hiramatsu <mhiramat@kernel.org>,
Jason Baron <jbaron@akamai.com>,
David Laight <David.Laight@aculab.com>,
Borislav Petkov <bp@alien8.de>, Julia Cartwright <julia@ni.com>,
Jessica Yu <jeyu@kernel.org>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Edward Cree <ecree@solarflare.com>,
Daniel Bristot de Oliveira <bristot@redhat.com>
Subject: Re: [PATCH v3 0/6] Static calls
Date: Mon, 14 Jan 2019 23:51:37 +0000 [thread overview]
Message-ID: <CB44BB60-8622-438D-A2E1-B706E2CF4A75@vmware.com> (raw)
In-Reply-To: <CALCETrWE-GYr=bUF-RsSNNW5A-=jd2Oy_4yPisKTUiPZTAHm6A@mail.gmail.com>
> On Jan 14, 2019, at 3:27 PM, Andy Lutomirski <luto@kernel.org> wrote:
>
> On Mon, Jan 14, 2019 at 2:01 PM H. Peter Anvin <hpa@zytor.com> wrote:
>> So I was already in the middle of composing this message when Andy posted:
>>
>>> I don't even think this is sufficient. I think we also need everyone
>>> who clears the bit to check if all bits are clear and, if so, remove
>>> the breakpoint. Otherwise we have a situation where, if you are in
>>> text_poke_bp() and you take an NMI (or interrupt or MCE or whatever)
>>> and that interrupt then hits the breakpoint, then you deadlock because
>>> no one removes the breakpoint.
>>>
>>> If we do this, and if we can guarantee that all CPUs make forward
>>> progress, then maybe the problem is solved. Can we guarantee something
>>> like all NMI handlers that might wait in a spinlock or for any other
>>> reason will periodically check if a sync is needed while they're
>>> spinning?
>>
>> So the really, really nasty case is when an asynchronous event on the
>> *patching* processor gets stuck spinning on a resource which is
>> unavailable due to another processor spinning on the #BP. We can disable
>> interrupts, but we can't stop NMIs from coming in (although we could
>> test in the NMI handler if we are in that condition and return
>> immediately; I'm not sure we want to do that, and we still have to deal
>> with #MC and what not.)
>>
>> The fundamental problem here is that we don't see the #BP on the
>> patching processor, in which case we could simply complete the patching
>> from the #BP handler on that processor.
>>
>> On 1/13/19 6:40 PM, H. Peter Anvin wrote:
>>> On 1/13/19 6:31 PM, H. Peter Anvin wrote:
>>>> static cpumask_t text_poke_cpumask;
>>>>
>>>> static void text_poke_sync(void)
>>>> {
>>>> smp_wmb();
>>>> text_poke_cpumask = cpu_online_mask;
>>>> smp_wmb(); /* Should be optional on x86 */
>>>> cpumask_clear_cpu(&text_poke_cpumask, smp_processor_id());
>>>> on_each_cpu_mask(&text_poke_cpumask, text_poke_sync_cpu, NULL, false);
>>>> while (!cpumask_empty(&text_poke_cpumask)) {
>>>> cpu_relax();
>>>> smp_rmb();
>>>> }
>>>> }
>>>>
>>>> static void text_poke_sync_cpu(void *dummy)
>>>> {
>>>> (void)dummy;
>>>>
>>>> smp_rmb();
>>>> cpumask_clear_cpu(&poke_bitmask, smp_processor_id());
>>>> /*
>>>> * We are guaranteed to return with an IRET, either from the
>>>> * IPI or the #BP handler; this provides serialization.
>>>> */
>>>> }
>>>
>>> The invariants here are:
>>>
>>> 1. The patching routine must set each bit in the cpumask after each event
>>> that requires synchronization is complete.
>>> 2. The bit can be (atomically) cleared on the target CPU only, and only in a
>>> place that guarantees a synchronizing event (e.g. IRET) before it may
>>> reaching the poked instruction.
>>> 3. At a minimum the IPI handler and #BP handler needs to clear the bit. It
>>> *is* also possible to clear it in other places, e.g. the NMI handler, if
>>> necessary as long as condition 2 is satisfied.
>>
>> OK, so with interrupts enabled *on the processor doing the patching* we
>> still have a problem if it takes an interrupt which in turn takes a #BP.
>> Disabling interrupts would not help, because but an NMI and #MC could
>> still cause problems unless we can guarantee that no path which may be
>> invoked by NMI/#MC can do text_poke, which seems to be a very aggressive
>> assumption.
>>
>> Note: I am assuming preemption is disabled.
>>
>> The easiest/sanest way to deal with this might be to switch the IDT (or
>> provide a hook in the generic exception entry code) on the patching
>> processor, such that if an asynchronous event comes in, we either roll
>> forward or revert. This is doable because the second sync we currently
>> do is not actually necessary per the hardware guys.
>
> This is IMO insanely complicated. I much prefer the kind of
> complexity that is more or less deterministic and easy to test to the
> kind of complexity (like this) that only happens in corner cases.
>
> I see two solutions here:
>
> 1. Just suck it up and emulate the CALL. And find a way to write a
> test case so we know it works.
>
> 2. Find a non-deadlocky way to make the breakpoint handler wait for
> the breakpoint to get removed, without any mucking at all with the
> entry code. And find a way to write a test case so we know it works.
> (E.g. stick an actual static_call call site *in text_poke_bp()* that
> fires once on boot so that the really awful recursive case gets
> exercised all the time.
>
> But if we're going to do any mucking with the entry code, let's just
> do the simple mucking to make emulating CALL work.
These two approaches still seem more complicated to me than having a
trampoline as backup, which is patched dynamically.
IIUC, the current pushback against this option is that it makes batching is
more complicated. I am not sure it is that bad, but there are other variants
of this solution, for example using an retpoline-like flow:
BP-handler:
/\x10* Sets a per-CPU variable to hold the target */
this_cpu_write(interrupted_static_call_target) =
get_static_call_targets(regs->rip);
/* Choose the function based in IRQ-disable during interrupt */
if (regs->flags & X86_EFLAGS_IF) {
regs->flags &= ~X86_EFLAGS_IF;
regs->rip = user_handler_IRQ_disabled
} else {
regs->rip = user_handler_IRQ_enabled
}
user_handler_IRQ_disabled:
push PER_CPU_VAR(interrupted_static_call_target)
sti # this one is not needed in the the enabled case
ret # sti-blocking prevents preemption before
Obviously, I don’t know how this coexists with CET.
next prev parent reply other threads:[~2019-01-14 23:51 UTC|newest]
Thread overview: 90+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-09 22:59 Josh Poimboeuf
2019-01-09 22:59 ` [PATCH v3 1/6] compiler.h: Make __ADDRESSABLE() symbol truly unique Josh Poimboeuf
2019-01-09 22:59 ` [PATCH v3 2/6] static_call: Add basic static call infrastructure Josh Poimboeuf
2019-01-10 14:03 ` Edward Cree
2019-01-10 18:37 ` Josh Poimboeuf
2019-01-09 22:59 ` [PATCH v3 3/6] x86/static_call: Add out-of-line static call implementation Josh Poimboeuf
2019-01-10 0:16 ` Nadav Amit
2019-01-10 16:28 ` Josh Poimboeuf
2019-01-09 22:59 ` [PATCH v3 4/6] static_call: Add inline static call infrastructure Josh Poimboeuf
2019-01-09 22:59 ` [PATCH v3 5/6] x86/alternative: Use a single access in text_poke() where possible Josh Poimboeuf
2019-01-10 9:32 ` Nadav Amit
2019-01-10 17:20 ` Josh Poimboeuf
2019-01-10 17:29 ` Nadav Amit
2019-01-10 17:32 ` Steven Rostedt
2019-01-10 17:42 ` Sean Christopherson
2019-01-10 17:57 ` Steven Rostedt
2019-01-10 18:04 ` Sean Christopherson
2019-01-10 18:21 ` Josh Poimboeuf
2019-01-10 18:24 ` Steven Rostedt
2019-01-11 12:10 ` Alexandre Chartre
2019-01-11 15:28 ` Josh Poimboeuf
2019-01-11 16:46 ` Alexandre Chartre
2019-01-11 16:57 ` Josh Poimboeuf
2019-01-11 17:41 ` Jason Baron
2019-01-11 17:54 ` Nadav Amit
2019-01-15 11:10 ` Alexandre Chartre
2019-01-15 16:19 ` Steven Rostedt
2019-01-15 16:45 ` Alexandre Chartre
2019-01-11 0:59 ` hpa
2019-01-11 1:34 ` Sean Christopherson
2019-01-11 8:13 ` hpa
2019-01-09 22:59 ` [PATCH v3 6/6] x86/static_call: Add inline static call implementation for x86-64 Josh Poimboeuf
2019-01-10 1:21 ` [PATCH v3 0/6] Static calls Nadav Amit
2019-01-10 16:44 ` Josh Poimboeuf
2019-01-10 17:32 ` Nadav Amit
2019-01-10 18:18 ` Josh Poimboeuf
2019-01-10 19:45 ` Nadav Amit
2019-01-10 20:32 ` Josh Poimboeuf
2019-01-10 20:48 ` Nadav Amit
2019-01-10 20:57 ` Josh Poimboeuf
2019-01-10 21:47 ` Nadav Amit
2019-01-10 17:31 ` Linus Torvalds
2019-01-10 20:51 ` H. Peter Anvin
2019-01-10 20:30 ` Peter Zijlstra
2019-01-10 20:52 ` Josh Poimboeuf
2019-01-10 23:02 ` Linus Torvalds
2019-01-11 0:56 ` Andy Lutomirski
2019-01-11 1:47 ` Nadav Amit
2019-01-11 15:15 ` Josh Poimboeuf
2019-01-11 15:48 ` Nadav Amit
2019-01-11 16:07 ` Josh Poimboeuf
2019-01-11 17:23 ` Nadav Amit
2019-01-11 19:03 ` Linus Torvalds
2019-01-11 19:17 ` Nadav Amit
2019-01-11 19:23 ` hpa
2019-01-11 19:33 ` Nadav Amit
2019-01-11 19:34 ` Linus Torvalds
2019-01-13 0:34 ` hpa
2019-01-13 0:36 ` hpa
2019-01-11 19:39 ` Jiri Kosina
2019-01-14 2:31 ` H. Peter Anvin
2019-01-14 2:40 ` H. Peter Anvin
2019-01-14 20:11 ` Andy Lutomirski
2019-01-14 22:00 ` H. Peter Anvin
2019-01-14 22:54 ` H. Peter Anvin
2019-01-15 3:05 ` Andy Lutomirski
2019-01-15 5:01 ` H. Peter Anvin
2019-01-15 5:37 ` H. Peter Anvin
2019-01-14 23:27 ` Andy Lutomirski
2019-01-14 23:51 ` Nadav Amit [this message]
2019-01-15 2:28 ` hpa
2019-01-11 20:04 ` Josh Poimboeuf
2019-01-11 20:12 ` Linus Torvalds
2019-01-11 20:31 ` Josh Poimboeuf
2019-01-11 20:46 ` Linus Torvalds
2019-01-11 21:05 ` Andy Lutomirski
2019-01-11 21:10 ` Linus Torvalds
2019-01-11 21:32 ` Josh Poimboeuf
2019-01-14 12:28 ` Peter Zijlstra
2019-01-11 21:22 ` Josh Poimboeuf
2019-01-11 21:23 ` Josh Poimboeuf
2019-01-11 21:25 ` Josh Poimboeuf
2019-01-11 21:36 ` Nadav Amit
2019-01-11 21:41 ` Josh Poimboeuf
2019-01-11 21:55 ` Steven Rostedt
2019-01-11 21:59 ` Nadav Amit
2019-01-11 21:56 ` Nadav Amit
2019-01-12 23:54 ` Andy Lutomirski
2020-02-17 21:10 ` Jann Horn
2020-02-17 21:57 ` Steven Rostedt
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=CB44BB60-8622-438D-A2E1-B706E2CF4A75@vmware.com \
--to=namit@vmware.com \
--cc=David.Laight@aculab.com \
--cc=ard.biesheuvel@linaro.org \
--cc=bp@alien8.de \
--cc=bristot@redhat.com \
--cc=ecree@solarflare.com \
--cc=hpa@zytor.com \
--cc=jbaron@akamai.com \
--cc=jeyu@kernel.org \
--cc=jikos@kernel.org \
--cc=jpoimboe@redhat.com \
--cc=julia@ni.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=luto@kernel.org \
--cc=mhiramat@kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
--cc=x86@kernel.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
all inboxes | Powered by JetHome®