Skip to contentSkip to conversation

x86-64 TIF flags for debug regs and io bitmap in ctxsw

PATCH In review Stephane Eranian 3 participants 8 messages Jul 4, 2006 ptrace support

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 x86-64 TIF flags for debug regs and io bitmap in ctxsw

Hello,

Following my discussion with Andi. Here is a patch that introduces two new TIF flags to simplify the context switch code in __switch_to(). The idea is to minimize the number of cache lines accessed in the common case, i.e., when neither the debug registers nor the I/O bitmap are used.

This patch covers the x86-64 modifications. A patch for i386 follows.

Changelog:
- add TIF_DEBUG to track when debug registers are active
- add TIF_IO_BITMAP to track when I/O bitmap is used
- modify __switch_to() to use the new TIF flags

<signed-off-by>: [email protected]

arch/x86_64/ia32/ptrace32.c +4 -0
diff -urNp linux-2.6.17.2.orig/arch/x86_64/ia32/ptrace32.c linux-2.6.17.2-tif/arch/x86_64/ia32/ptrace32.c
@@ -118,6 +118,10 @@ static int putreg32(struct task_struct *
 			if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1)
 			       return -EIO;
 		child->thread.debugreg7 = val; 
+		if (val)
+			set_tsk_thread_flag(child, TIF_DEBUG);
+		else
+			clear_tsk_thread_flag(child, TIF_DEBUG);
 		break; 
 		    
 	default:
arch/x86_64/kernel/ioport.c +1 -0
diff -urNp linux-2.6.17.2.orig/arch/x86_64/kernel/ioport.c linux-2.6.17.2-tif/arch/x86_64/kernel/ioport.c
@@ -56,6 +56,7 @@ asmlinkage long sys_ioperm(unsigned long
 
 		memset(bitmap, 0xff, IO_BITMAP_BYTES);
 		t->io_bitmap_ptr = bitmap;
+		set_thread_flag(TIF_IO_BITMAP);
 	}
 
 	/*
arch/x86_64/kernel/process.c +42 -32

111 lines not on this page · raw message

arch/x86_64/kernel/ptrace.c +6 -2
diff -urNp linux-2.6.17.2.orig/arch/x86_64/kernel/ptrace.c linux-2.6.17.2-tif/arch/x86_64/kernel/ptrace.c
@@ -420,9 +420,13 @@ long arch_ptrace(struct task_struct *chi
 				if ((0x5554 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
 					break;
 			if (i == 4) {
-				child->thread.debugreg7 = data;
+			  child->thread.debugreg7 = data;
+			  if (data)
+			  	set_tsk_thread_flag(child, TIF_DEBUG);
+			  else
+			  	clear_tsk_thread_flag(child, TIF_DEBUG);
 			  ret = 0;
-		  }
+		  	}
 		  break;
 		}
 		break;
include/asm-x86_64/thread_info.h +7 -0
diff -urNp linux-2.6.17.2.orig/include/asm-x86_64/thread_info.h linux-2.6.17.2-tif/include/asm-x86_64/thread_info.h
@@ -106,6 +106,8 @@ static inline struct thread_info *stack_
 #define TIF_FORK		18	/* ret_from_fork */
 #define TIF_ABI_PENDING		19
 #define TIF_MEMDIE		20
+#define TIF_DEBUG		21	/* uses debug registers */
+#define TIF_IO_BITMAP		22	/* uses I/O bitmap */
 
 #define _TIF_SYSCALL_TRACE	(1<<TIF_SYSCALL_TRACE)
 #define _TIF_NOTIFY_RESUME	(1<<TIF_NOTIFY_RESUME)
@@ -119,6 +121,8 @@ static inline struct thread_info *stack_
 #define _TIF_IA32		(1<<TIF_IA32)
 #define _TIF_FORK		(1<<TIF_FORK)
 #define _TIF_ABI_PENDING	(1<<TIF_ABI_PENDING)
+#define _TIF_DEBUG		(1<<TIF_DEBUG)
+#define _TIF_IO_BITMAP		(1<<TIF_IO_BITMAP)
 
 /* work to do on interrupt/exception return */
 #define _TIF_WORK_MASK \
@@ -126,6 +130,9 @@ static inline struct thread_info *stack_
 /* work to do on any return to user space */
 #define _TIF_ALLWORK_MASK (0x0000FFFF & ~_TIF_SECCOMP)
 
+/* flags to check in __switch_to() */
+#define _TIF_WORK_CTXSW (_TIF_DEBUG|_TIF_IO_BITMAP)
+
 #define PREEMPT_ACTIVE     0x10000000
 
 /*
#1 lore raw

arch/x86_64/kernel/process.c __switch_to(struct task_struct *prev_p, · line 627

Quoted 5 lines
arch/x86_64/kernel/process.c +3 -2
-		}
-	}
+	if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW))
+	    || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP))
+		__switch_to_xtra(prev_p, next_p, tss);

well isn't this replacing an if() (which isn't cheap but also not expensive, due to unlikely()) with an atomic operation (which *is* expensive) ?

That to me doesn't make this sound like an actual win....

#2 lore raw
Quoted 9 lines · Arjan van de Ven

arch/x86_64/kernel/process.c __switch_to(struct task_struct *prev_p, · line 627

Quoted 5 lines
arch/x86_64/kernel/process.c +3 -2
-		}
-	}
+	if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW))
+	    || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP))
+		__switch_to_xtra(prev_p, next_p, tss);

well isn't this replacing an if() (which isn't cheap but also not expensive, due to unlikely()) with an atomic operation (which *is* expensive) ?

Where do you see an atomic operation?

Also on x86-64 unlikely is an no-op.

-Andi

#3 lore raw
Quoted 11 lines · Andi Kleen
Quoted 9 lines · Arjan van de Ven

arch/x86_64/kernel/process.c __switch_to(struct task_struct *prev_p, · line 627

Quoted 5 lines
arch/x86_64/kernel/process.c +3 -2
-		}
-	}
+	if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW))
+	    || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP))
+		__switch_to_xtra(prev_p, next_p, tss);

well isn't this replacing an if() (which isn't cheap but also not expensive, due to unlikely()) with an atomic operation (which *is* expensive) ?

Quoted 1 line

Where do you see an atomic operation?

test_tsk_thread_flag() ends up doing an atomic op afaics

#4 lore raw
In reply to Arjan van de Ven
Quoted 10 lines · Arjan van de Ven

arch/x86_64/kernel/process.c __switch_to(struct task_struct *prev_p, · line 627

Quoted 5 lines
arch/x86_64/kernel/process.c +3 -2
-		}
-	}
+	if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW))
+	    || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP))
+		__switch_to_xtra(prev_p, next_p, tss);

well isn't this replacing an if() (which isn't cheap but also not expensive, due to unlikely()) with an atomic operation (which *is* expensive) ?

Quoted 2 lines

That to me doesn't make this sound like an actual win....

Although the two if were marked unlikely, you had to do the test anyway. So you had to touch next->debugreg[7], next->io_bitmap_ptr, and prev->io_bitmap_ptr. Now the first two are collapsed into one cache line in thread_info->flags.

Yet, I see your point about the test_tsk_thread_flag() and I am wondering if we do need the atomicity in this case and whether we could simplify by using the same expression as for next, i.e, task_thread_info(prev_p)->flags & TIF_IO_BITMAP?

Signature · 1 line
-Stephane
#5 lore raw
In reply to Arjan van de Ven

Arjan,

Quoted 10 lines · Arjan van de Ven

arch/x86_64/kernel/process.c __switch_to(struct task_struct *prev_p, · line 627

Quoted 5 lines
arch/x86_64/kernel/process.c +3 -2
-		}
-	}
+	if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW))
+	    || test_tsk_thread_flag(prev_p, TIF_IO_BITMAP))
+		__switch_to_xtra(prev_p, next_p, tss);

well isn't this replacing an if() (which isn't cheap but also not expensive, due to unlikely()) with an atomic operation (which *is* expensive) ?

Andi is right. I double checked the test_tsk_thread_flag() and it does not use atomic ops.

Signature · 1 line
-Stephane
#6 lore raw
In reply to Stephane Eranian

arch/x86_64/ia32/ptrace32.c

Quoted 4 lines

diff -urNp linux-2.6.17.2.orig/arch/x86_64/ia32/ptrace32.c linux-2.6.17.2-tif/arch/x86_64/ia32/ptrace32.c

arch/x86_64/ia32/ptrace32.c +0 -0

Added thanks. But I had to fix it up by hand because of conflicts. Please submit patches against mainline, not stable.

arch/x86_64/ia32/ptrace32.c

Quoted 3 lines
       if (t->flags & _TIF_ABI_PENDING)
arch/x86_64/ia32/ptrace32.c +1 -1
-               t->flags ^= (_TIF_ABI_PENDING | _TIF_IA32);
+               t->flags ^= (_TIF_ABI_PENDING | _TIF_IA32 | _TIF_DEBUG);

This xor must be obviously outside the if(). I fixed that up too by changing it to an unconditional clear.

-Andi

#7 lore raw

Andi,

Quoted 8 lines · Andi Kleen

arch/x86_64/ia32/ptrace32.c

Quoted 4 lines

diff -urNp linux-2.6.17.2.orig/arch/x86_64/ia32/ptrace32.c linux-2.6.17.2-tif/arch/x86_64/ia32/ptrace32.c

arch/x86_64/ia32/ptrace32.c +0 -0

Added thanks. But I had to fix it up by hand because of conflicts. Please submit patches against mainline, not stable.

Sorry about that.

Quoted 6 lines

arch/x86_64/ia32/ptrace32.c

Quoted 3 lines
       if (t->flags & _TIF_ABI_PENDING)
arch/x86_64/ia32/ptrace32.c +1 -1
-               t->flags ^= (_TIF_ABI_PENDING | _TIF_IA32);
+               t->flags ^= (_TIF_ABI_PENDING | _TIF_IA32 | _TIF_DEBUG);

This xor must be obviously outside the if(). I fixed that up too by changing it to an unconditional clear.

Yes, my mistake.

Signature · 1 line
-Stephane