Index: arch/x86_64/kernel/ioport.c
===================================================================
RCS file: /usr/src/bkcvs/linux-2.5/arch/x86_64/kernel/ioport.c,v
retrieving revision 1.15
diff -u -r1.15 ioport.c
--- a/arch/x86_64/kernel/ioport.c	24 Aug 2004 18:20:09 -0000	1.15
+++ b/arch/x86_64/kernel/ioport.c	4 Sep 2004 21:17:30 -0000
@@ -62,12 +62,12 @@
 	 */
 	set_bitmap(t->io_bitmap_ptr, from, num, !turn_on);
 	tss = init_tss + get_cpu();
-	if (tss->io_bitmap_base == IO_BITMAP_OFFSET) { /* already active? */
-		set_bitmap(tss->io_bitmap, from, num, !turn_on);
-	} else {
-		memcpy(tss->io_bitmap, t->io_bitmap_ptr, IO_BITMAP_BYTES);
-		tss->io_bitmap_base = IO_BITMAP_OFFSET; /* Activate it in the TSS */
-	}
+
+	 * Sets the lazy trigger so that the next I/O operation will
+	 * reload the correct bitmap.
+	 */
+	tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
+
 	put_cpu();
 	return 0;
 }
Index: arch/x86_64/kernel/process.c
===================================================================
RCS file: /usr/src/bkcvs/linux-2.5/arch/x86_64/kernel/process.c,v
retrieving revision 1.38
diff -u -r1.38 process.c
--- a/arch/x86_64/kernel/process.c	24 Aug 2004 18:27:55 -0000	1.38
+++ b/arch/x86_64/kernel/process.c	4 Sep 2004 21:17:30 -0000
@@ -485,29 +485,16 @@
 		loaddebug(next, 7);
 	}
 
-
-	/* 
-	 * Handle the IO bitmap 
-	 */ 
-	if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr)) {
-		if (next->io_bitmap_ptr) {
-			/*
-			 * 2 cachelines copy ... not good, but not that
-			 * bad either. Anyone got something better?
-			 * This only affects processes which use ioperm().
-			 */
-			memcpy(tss->io_bitmap, next->io_bitmap_ptr, IO_BITMAP_BYTES);
-			tss->io_bitmap_base = IO_BITMAP_OFFSET;
-		} else {
-			/*
-			 * a bitmap offset pointing outside of the TSS limit
-			 * causes a nicely controllable SIGSEGV if a process
-			 * tries to use a port IO instruction. The first
-			 * sys_ioperm() call sets up the bitmap properly.
-			 */
-			tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
-		}
-	}
+	/*
+	 * Lazy TSS's I/O bitmap copy. We set an invalid offset here and
+	 * we let the task to get a GPF in case an I/O instruction is performed.
+	 * The handler of the GPF will verify that the faulting task has a valid
+	 * I/O bitmap and, if true, does the real copy and restart the instruction.
+	 * This will save us for redoundant copies when the currently switched task
+	 * does not perform any I/O during its timeslice.
+	 */
+	tss->io_bitmap_base = next->io_bitmap_ptr ? INVALID_IO_BITMAP_OFFSET_LAZY:
+		INVALID_IO_BITMAP_OFFSET;
 
 	return prev_p;
 }
Index: arch/x86_64/kernel/traps.c
===================================================================
RCS file: /usr/src/bkcvs/linux-2.5/arch/x86_64/kernel/traps.c,v
retrieving revision 1.44
diff -u -r1.44 traps.c
--- a/arch/x86_64/kernel/traps.c	24 Aug 2004 18:20:09 -0000	1.44
+++ b/arch/x86_64/kernel/traps.c	4 Sep 2004 21:22:15 -0000
@@ -485,6 +485,27 @@
 
 asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
 {
+	int cpu = get_cpu();
+	struct tss_struct *tss = &per_cpu(init_tss, cpu);
+	struct thread_struct *tsk_th = &current->thread;
+
+	/*
+	 * Perform the lazy TSS's I/O bitmap copy. If the TSS has an
+	 * invalid offset set (the LAZY one) and the faulting thread has
+	 * a valid I/O bitmap pointer, we copy the I/O bitmap in the TSS
+	 * and we set the offset field correctly. Then we let the CPU to
+	 * restart the faulting instruction.
+	 */
+	if (tss->io_bitmap_base == INVALID_IO_BITMAP_OFFSET_LAZY &&
+	    tsk_th->io_bitmap_ptr) {
+		memcpy(tss->io_bitmap, tsk_th->io_bitmap_ptr,
+		       IO_BITMAP_BYTES);
+		tss->io_bitmap_base = IO_BITMAP_OFFSET;
+		put_cpu();
+		return;
+	}
+	put_cpu();
+
 	conditional_sti(regs);
 
 #ifdef CONFIG_CHECKING
Index: include/asm-x86_64/processor.h
===================================================================
RCS file: /usr/src/bkcvs/linux-2.5/include/asm-x86_64/processor.h,v
retrieving revision 1.38
diff -u -r1.38 processor.h
--- a/include/asm-x86_64/processor.h	24 Aug 2004 18:08:41 -0000	1.38
+++ b/include/asm-x86_64/processor.h	4 Sep 2004 21:24:11 -0000
@@ -185,6 +185,7 @@
 #define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
 #define IO_BITMAP_OFFSET offsetof(struct tss_struct,io_bitmap)
 #define INVALID_IO_BITMAP_OFFSET 0x8000
+#define INVALID_IO_BITMAP_OFFSET_LAZY 0x9000
 
 struct i387_fxsave_struct {
 	u16	cwd;
@@ -215,13 +216,14 @@
 	u32 reserved4;
 	u16 reserved5;
 	u16 io_bitmap_base;
+
 	/*
 	 * The extra 1 is there because the CPU will access an
 	 * additional byte beyond the end of the IO permission
 	 * bitmap. The extra byte must be all 1 bits, and must
 	 * be within the limit. Thus we have:
 	 *
-	 * 128 bytes, the bitmap itself, for ports 0..0x3ff
+	 * 8192 bytes, the bitmap itself, for ports 0..65535
 	 * 8 bytes, for an extra "long" of ~0UL
 	 */
 	unsigned long io_bitmap[IO_BITMAP_LONGS + 1];
