Skip to contentSkip to conversation

i386: don't save eflags on task switch

RFC PATCH In review Chuck Ebbert 3 participants 3 messages Nov 4, 2006

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 i386: don't save eflags on task switch

There is no real need to save eflags in switch_to(). Instead, we can keep a constant value in the thread_struct and always restore that.

This will cause a behavior change, though. If a user sets NT in eflags and then enters the kernel via sysenter, and another task runs before the syscall returns:

(1) If we return via the iret path then no fault will occur (currently the user gets a fault.)

(2) If we return via sysexit, NT will be cleared (currently it remains set.)

Users shouldn't be setting NT anyway, so this shouldn't be a problem.

On a K8 CPU, this patch didn't lower the minimum task-switch time, but it did lower the average and removed most of the variability. It really needs testing on a P4, where it should have much more effect.

---

 arch/i386/kernel/ioport.c    |    7 ++++---
 include/asm-i386/processor.h |    5 +++--
 include/asm-i386/system.h    |    4 ++--
 3 files changed, 9 insertions(+), 7 deletions(-)
arch/i386/kernel/ioport.c +4 -3
@@ -146,8 +146,9 @@ asmlinkage long sys_iopl(unsigned long u
 		if (!capable(CAP_SYS_RAWIO))
 			return -EPERM;
 	}
-	t->iopl = level << 12;
-	regs->eflags = (regs->eflags & ~X86_EFLAGS_IOPL) | t->iopl;
-	set_iopl_mask(t->iopl);
+	t->eflags = level << 12 | X86_EFLAGS_IF | 2;
+	regs->eflags = (regs->eflags & ~X86_EFLAGS_IOPL) |
+		       (t->eflags & X86_EFLAGS_IOPL);
+	set_iopl_mask(t->eflags);
 	return 0;
 }
include/asm-i386/processor.h +3 -2
@@ -449,6 +449,7 @@ struct thread_struct {
 	unsigned long	sysenter_cs;
 	unsigned long	eip;
 	unsigned long	esp;
+ 	unsigned long	eflags;
 	unsigned long	fs;
 	unsigned long	gs;
 /* Hardware debugging registers */
@@ -464,7 +465,6 @@ struct thread_struct {
 	unsigned int		saved_fs, saved_gs;
 /* IO permissions */
 	unsigned long	*io_bitmap_ptr;
- 	unsigned long	iopl;
 /* max allowed port in the bitmap, in bytes: */
 	unsigned long	io_bitmap_max;
 };
@@ -534,7 +534,8 @@ static inline void set_iopl_mask(unsigne
 			      "pushl %0;"
 			      "popfl"
 				: "=&r" (reg)
-				: "i" (~X86_EFLAGS_IOPL), "r" (mask));
+				: "i" (~X86_EFLAGS_IOPL),
+				  "r" (mask & X86_EFLAGS_IOPL));
 }
 
 /* Forward declaration, a strange C thing */
include/asm-i386/system.h +2 -2
@@ -17,7 +17,7 @@ extern struct task_struct * FASTCALL(__s
  */
 #define switch_to(prev,next,last) do {					\
 	unsigned long esi,edi;						\
-	asm volatile("pushfl\n\t"		/* Save flags */	\
+	asm volatile("pushl %9\n\t"		/* Save flags */	\
 		     "pushl %%ebp\n\t"					\
 		     "movl %%esp,%0\n\t"	/* save ESP */		\
 		     "movl %5,%%esp\n\t"	/* restore ESP */	\
@@ -30,7 +30,7 @@ extern struct task_struct * FASTCALL(__s
 		     :"=m" (prev->thread.esp),"=m" (prev->thread.eip),	\
 		      "=a" (last),"=S" (esi),"=D" (edi)			\
 		     :"m" (next->thread.esp),"m" (next->thread.eip),	\
-		      "2" (prev), "d" (next));				\
+		      "2" (prev), "d" (next), "m" (prev->thread.eflags));				\
 } while (0)
 
 #define _set_base(addr,base) do { unsigned long __pr; \
Signature · 1 line
Chuck
#1 lore raw
Quoted 4 lines · Chuck Ebbert

There is no real need to save eflags in switch_to(). Instead, we can keep a constant value in the thread_struct and always restore that.

I don't really see the point. The "pushfl" isn't the expensive part, and it gives sane and expected semantics.

The "popfl" is the expensive part, and that's the thing that can't really even be removed.

Linus

#2 lore raw
Quoted 7 lines · Linus Torvalds
Quoted 4 lines · Chuck Ebbert

There is no real need to save eflags in switch_to(). Instead, we can keep a constant value in the and always restore that.

Quoted 5 lines

I don't really see the point. The "pushfl" isn't the expensive part, and it gives sane and expected semantics.

The "popfl" is the expensive part, and that's the thing that can't really even be removed.

Well it could -- i made an attempt at it recently. But it would add a little code to the SYSENTER entry code (basically a test / conditional jump after the pushfl there) to clear any rogue flags on entry. Not sure it is worth it. That is why i didn't put this patch in.

-Andi