Re: [PATCH 1/1] x86/fpu: math_state_restore() should not blindly disable irqs

From: Linus Torvalds
Date: Sat Mar 07 2015 - 15:11:58 EST


On Sat, Mar 7, 2015 at 2:36 AM, Ingo Molnar <mingo@xxxxxxxxxx> wrote:
>
> We could save the same 10 cycles from page fault overhead as well,
> AFAICS.

Are trap gates actually noticeably faster? Or is it just he
"conditional_sti()" you're worried about?

Anyway, for page faulting, we traditionally actually wanted an
interrupt gate, because of how we wanted to avoid interrupts coming in
and possibly messing up %cr2 due to vmalloc faults, but more
importantly for preemption. vmalloc faults are "harmless" because
we'll notice that it's already done, return, and then re-take the real
fault. But a preemption event before we read %cr2 can cause bad things
to happen:

- page fault pushes error code on stack, address in %cr2

- we don't have interrupts disabled, and some interrupt comes in and
causes preemption

- some other process runs, take another page fault. %cr2 now is the
wrong address

- we go back to the original thread (perhaps on another cpu), which
now reads %cr2 for the wrong address

- we send the process a SIGSEGV because we think it's accessing
memory that it has no place touching

So the page fault code actually *needs* interrupts disabled until we
read %cr2. Stupid x86 trap semantics where the error code is on the
thread-safe stack, but %cr2 is not.

Maybe there is some trick I'm missing, but on the whole I think
"interrupt gate + conditional_sti()" does have things going for it.
Yes, it still leaves NMI as being special, but NMI really *is*
special.

Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/