x86 page fault handler not interrupt safe
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 5 lines · Brian Gerst
Keep in mind that
regs->eflagscould be from user space, and could have some undesirable flags set. That's why I did a test/sti instead of reloading eflags. Plus my patch leaves interrupts disabled for the minimum time possible.
The plain "popf" should be ok: the way intel works, you cannot actually use popf to set any of the strange flags (if vm86 mode etc).
I like the size of this alternate patch.
Linus
Quoted 10 lines · Linus Torvalds
Quoted 5 lines · Brian Gerst
Keep in mind that
regs->eflagscould be from user space, and could have some undesirable flags set. That's why I did a test/sti instead of reloading eflags. Plus my patch leaves interrupts disabled for the minimum time possible.The plain "popf" should be ok: the way intel works, you cannot actually use popf to set any of the strange flags (if vm86 mode etc).
TF (trap flag) would be one that I wouldn't want to get reset. You could use
local_irq_restore(regs->eflags & ~(TF|RF|VM|NT)) which are all the flags cleared by an interrupt/trap gate. Or if (regs->eflags & IF) sti();
Take your pick.
--
Brian Gerst
Quoted 11 lines · Brian Gerst
Quoted 5 lines
Keep in mind that
regs->eflagscould be from user space, and could have some undesirable flags set. That's why I did a test/sti instead of reloading eflags. Plus my patch leaves interrupts disabled for the minimum time possible.The plain "popf" should be ok: the way intel works, you cannot actually use popf to set any of the strange flags (if vm86 mode etc).
I like the size of this alternate patch.
I dont see where the alternative patch ensures the user didnt flip the direction flag for one
Quoted 3 lines · Alan Cox
I dont see where the alternative patch ensures the user didnt flip the direction flag for one
Yeah.
We might as well just make it "eflags & IF", none of the other flags should matter (or we explicitly want them cleared).
Linus
Quoted 6 lines · Linus Torvalds
Quoted 3 lines · Alan Cox
I dont see where the alternative patch ensures the user didnt flip the direction flag for one
Quoted 6 lines
Yeah.
We might as well just make it "eflags & IF", none of the other flags should matter (or we explicitly want them cleared).
Linus
Here is an updated patch. After reading over the Intel docs, and some testing on my Athlon, I found that %cr2 is not set on alignment check faults. I replaced it with the address of the faulting instruction. It may work on an Intel but is undocumented. The eip makes more sense anyways.
Signature · 1 line
Brian Gerst
arch/i386/kernel/traps.c +2 -11
@@ -225,15 +225,6 @@die (str, regs, err); } -static inline unsigned long get_cr2 (void ) -{ -unsigned long address; - -/* get the address */ - __asm__("movl %%cr2,%0" :"=r" (address)); -return address; -} -static void inline do_trap (int trapnr,int signr,char *str,int vm86,struct pt_regs * regs,long error_code,siginfo_t *info) {
@@ -314,7 +305,7 @@ DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS) DO_ERROR(11, SIGBUS, "segment not present", segment_not_present) DO_ERROR(12, SIGBUS, "stack segment", stack_segment) -DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, get_cr2()) +DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, regs->eip) asmlinkage void do_general_protection(struct pt_regs * regs, long error_code) {
@@ -973,7 +964,7 @@set_trap_gate (11 ,&segment_not_present);set_trap_gate (12 ,&stack_segment);set_trap_gate (13 ,&general_protection); -set_trap_gate (14 ,&page_fault); +set_intr_gate (14 ,&page_fault);set_trap_gate (15 ,&spurious_interrupt_bug);set_trap_gate (16 ,&coprocessor_error);set_trap_gate (17 ,&alignment_check);
arch/i386/mm/fault.c +7 -0
@@ -98,6 +98,9 @@* and the problem, and then passes it off to one of the appropriate * routines. * +* This is called with interrupts off, to protect %cr2 from being +* overwritten by an interrupt handler that faults. +* * error_code: * bit 0 == 0 means no page found, 1 means protection fault * bit 1 == 0 means read, 1 means write
@@ -116,6 +119,10 @@/* get the address */ __asm__("movl %%cr2,%0" :"=r" (address)); + +/* Reenable interrupts, but don't trust any other flags */ +if (regs->eflags &X86_EFLAGS_IF ) +sti (); tsk = current;
Quoted 9 lines · Brian Gerst
Quoted 8 lines · Nigel Gamble
Quoted 5 lines · Linus Torvalds
Quoted 2 lines · Brian Gerst
This patch will still cause the user process to seg fault: The error code on the stack will not match the address in %cr2.
You've convinced me. Good thinking. Let's do the irq thing.
Quoted 7 lines
Quoted 4 lines
I've actually seen user processes seg faulting because of this with the fully preemptible kernel patch applied. The fix we used in that patch was to use an interrupt gate for the fault handler, then to simply restore the interrupt state:
Keep in mind that
regs->eflagscould be from user space, and could have some undesirable flags set. That's why I did a test/sti instead of
Good point.
Quoted 2 lines
reloading eflags. Plus my patch leaves interrupts disabled for the minimum time possible.
I'm not sure that it makes much difference, as interrupts are disabled for such a short time anyway. I'd prefer to put the test/sti in do_page_fault(), and reduce the complexity needed in assembler routines as much as possible, for maintainability reasons.
Nigel Gamble [email protected]
Mountain View, CA, USA. http://www.nrg.org/
MontaVista Software [email protected]