2006-11-03 06:32:57

by Chuck Ebbert

[permalink] [raw]
Subject: [patch] i386: remove IOPL check on task switch

IOPL is implicitly saved and restored on task switch,
so explicit check is no longer needed.

Signed-off-by: Chuck Ebbert <[email protected]>

--- 2.6.19-rc4-32smp.orig/arch/i386/kernel/process.c
+++ 2.6.19-rc4-32smp/arch/i386/kernel/process.c
@@ -681,12 +681,6 @@ struct task_struct fastcall * __switch_t
loadsegment(gs, next->gs);

/*
- * Restore IOPL if needed.
- */
- if (unlikely(prev->iopl != next->iopl))
- set_iopl_mask(next->iopl);
-
- /*
* Now maybe handle debug registers and/or IO bitmaps
*/
if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW)
--
Chuck
"Even supernovas have their duller moments."


2006-11-03 18:06:08

by Zachary Amsden

[permalink] [raw]
Subject: Re: [patch] i386: remove IOPL check on task switch

Chuck Ebbert wrote:
> IOPL is implicitly saved and restored on task switch,
> so explicit check is no longer needed.
>
> Signed-off-by: Chuck Ebbert <[email protected]>
>
> --- 2.6.19-rc4-32smp.orig/arch/i386/kernel/process.c
> +++ 2.6.19-rc4-32smp/arch/i386/kernel/process.c
> @@ -681,12 +681,6 @@ struct task_struct fastcall * __switch_t
> loadsegment(gs, next->gs);
>
> /*
> - * Restore IOPL if needed.
> - */
> - if (unlikely(prev->iopl != next->iopl))
> - set_iopl_mask(next->iopl);
> -
> - /*
> * Now maybe handle debug registers and/or IO bitmaps
> */
> if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW)
>

Nack. This is used for paravirt-ops kernels that use IOPL'd userspace.
Fixing it would require a fairly heavy penalty on the iret path, since
every single instruction there contributes to a critical region which
must have custom fixup code, or some other technique to provide
protection against interrupt re-entrancy.

At least, let's discuss other potential solutions first - for now it is
harmless.

Zach

2006-11-03 18:41:36

by Andi Kleen

[permalink] [raw]
Subject: Re: [patch] i386: remove IOPL check on task switch


> Nack. This is used for paravirt-ops kernels that use IOPL'd userspace.
> Fixing it would require a fairly heavy penalty on the iret path, since
> every single instruction there contributes to a critical region which
> must have custom fixup code, or some other technique to provide
> protection against interrupt re-entrancy.
>
> At least, let's discuss other potential solutions first - for now it is
> harmless.

Ok I will drop the patch again

-Andi

2006-11-04 00:03:18

by Chuck Ebbert

[permalink] [raw]
Subject: Re: [patch] i386: remove IOPL check on task switch

In-Reply-To: <[email protected]>

On Fri, 03 Nov 2006 10:06:04 -0800, Zachary Amsden wrote:

> Chuck Ebbert wrote:
> > IOPL is implicitly saved and restored on task switch,
> > so explicit check is no longer needed.
>
> Nack. This is used for paravirt-ops kernels that use IOPL'd userspace.

How does that work? In the stock kernel, anything done by
the call to set_iopl_mask() (that was removed by the patch)
will be nullified by the 'popfl' at the end of the switch_to()
macro.

--
Chuck
"Even supernovas have their duller moments."

2006-11-04 00:03:46

by Chuck Ebbert

[permalink] [raw]
Subject: Re: [patch] i386: remove IOPL check on task switch

In-Reply-To: <[email protected]>

On Fri, 03 Nov 2006 10:06:04 -0800, Zachary Amsden wrote:

> Chuck Ebbert wrote:
> > IOPL is implicitly saved and restored on task switch,
> > so explicit check is no longer needed.
>
> Nack. This is used for paravirt-ops kernels that use IOPL'd userspace.

How does that work? In the stock kernel, anything done by
the call to set_iopl_mask() (that was removed by the patch)
will be nullified by the 'popfl' at the end of the switch_to()
macro.

--
Chuck
"Even supernovas have their duller moments."

2006-11-04 19:19:54

by Zachary Amsden

[permalink] [raw]
Subject: Re: [patch] i386: remove IOPL check on task switch

Chuck Ebbert wrote:
> In-Reply-To: <[email protected]>
>
> On Fri, 03 Nov 2006 10:06:04 -0800, Zachary Amsden wrote:
>
>
>> Chuck Ebbert wrote:
>>
>>> IOPL is implicitly saved and restored on task switch,
>>> so explicit check is no longer needed.
>>>
>> Nack. This is used for paravirt-ops kernels that use IOPL'd userspace.
>>
>
> How does that work? In the stock kernel, anything done by
> the call to set_iopl_mask() (that was removed by the patch)
> will be nullified by the 'popfl' at the end of the switch_to()
> macro.
>

Who put a popfl back in switch_to? I took it out some time ago. It
should not be there. The only reason for it was to stop IOPL leaking
from one process to another from a sleep during a sysenter based system
call.

Zach

2006-11-04 19:39:10

by Zachary Amsden

[permalink] [raw]
Subject: Re: [patch] i386: remove IOPL check on task switch

Zachary Amsden wrote:
> Chuck Ebbert wrote:
>> In-Reply-To: <[email protected]>
>>
>> On Fri, 03 Nov 2006 10:06:04 -0800, Zachary Amsden wrote:
>>
>>
>>> Chuck Ebbert wrote:
>>>
>>>> IOPL is implicitly saved and restored on task switch,
>>>> so explicit check is no longer needed.
>>>>
>>> Nack. This is used for paravirt-ops kernels that use IOPL'd
>>> userspace.
>>
>> How does that work? In the stock kernel, anything done by
>> the call to set_iopl_mask() (that was removed by the patch)
>> will be nullified by the 'popfl' at the end of the switch_to()
>> macro.
>>
>
> Who put a popfl back in switch_to? I took it out some time ago. It
> should not be there. The only reason for it was to stop IOPL leaking
> from one process to another from a sleep during a sysenter based
> system call.

Ok, checking shows Linus put it back to stop NT leakage. This is
correct, but unlikely. Would be nice to avoid it unless absolutely
necessary. Perhaps xor eflags old and new and only set_system_eflags()
if non-ALU bits have changed.

Zach

2006-11-04 19:59:14

by Linus Torvalds

[permalink] [raw]
Subject: Re: [patch] i386: remove IOPL check on task switch



On Sat, 4 Nov 2006, Zachary Amsden wrote:
>
> Ok, checking shows Linus put it back to stop NT leakage. This is correct, but
> unlikely. Would be nice to avoid it unless absolutely necessary. Perhaps xor
> eflags old and new and only set_system_eflags() if non-ALU bits have changed.

Not just NT. AC also leaked, and caused crashes in other programs (Wine)
that didn't expect AC to be set and did unaligned accesses.

Linus

2006-11-04 20:02:25

by Andi Kleen

[permalink] [raw]
Subject: Re: [patch] i386: remove IOPL check on task switch


> Ok, checking shows Linus put it back to stop NT leakage.

It is also needed to stop AC leakage

> This is
> correct, but unlikely. Would be nice to avoid it unless absolutely
> necessary. Perhaps xor eflags old and new and only set_system_eflags()
> if non-ALU bits have changed.

If it's worth it...

If it's worth for the context switch i think it would make
more sense to do it for the normal restore_flags() etc. which are much
more common (and could be actually handled in most cases with test + jump + sti)

-Andi

2006-11-04 20:09:45

by Zachary Amsden

[permalink] [raw]
Subject: Re: [patch] i386: remove IOPL check on task switch

Linus Torvalds wrote:
> On Sat, 4 Nov 2006, Zachary Amsden wrote:
>
>> Ok, checking shows Linus put it back to stop NT leakage. This is correct, but
>> unlikely. Would be nice to avoid it unless absolutely necessary. Perhaps xor
>> eflags old and new and only set_system_eflags() if non-ALU bits have changed.
>>
>
> Not just NT. AC also leaked, and caused crashes in other programs (Wine)
> that didn't expect AC to be set and did unaligned accesses.

Yes, AC, NT, IOPL, ID are bad to leak. DF / TF / RF are impossible to
leak by privilege contract. SF, ZF, PF, OF, CF can be clobbered.

VM / VIF / VIP are dealt with in separate switch paths (although I have
witnessed a VIF leak once from a userspace process that managed to get
VIF set). These can't even be set with popf, and require iret to fix.

But 99% of the time, only SF / ZF / PF / OF / CF will be different, so
you can avoid the popf.

Zach