Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/ioperm: Remove bitmap if all permissions dropped

If ioperm() results in a bitmap with all bits set (no permissions to any
I/O port), then handling that bitmap on context switch and exit to user
mode is pointless. Drop it.

Move the bitmap exit handling to the ioport code and reuse it for both the
thread exit path and dropping it. This allows to reuse this code for the
upcoming iopl() emulation.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Andy Lutomirski <luto@kernel.org>

+22 -14
+2
arch/x86/include/asm/io_bitmap.h
··· 11 11 unsigned long bitmap[IO_BITMAP_LONGS]; 12 12 }; 13 13 14 + void io_bitmap_exit(void); 15 + 14 16 void tss_update_io_bitmap(void); 15 17 16 18 #endif
+18 -1
arch/x86/kernel/ioport.c
··· 16 16 17 17 static atomic64_t io_bitmap_sequence; 18 18 19 + void io_bitmap_exit(void) 20 + { 21 + struct io_bitmap *iobm = current->thread.io_bitmap; 22 + 23 + current->thread.io_bitmap = NULL; 24 + clear_thread_flag(TIF_IO_BITMAP); 25 + preempt_disable(); 26 + tss_update_io_bitmap(); 27 + preempt_enable(); 28 + kfree(iobm); 29 + } 30 + 19 31 /* 20 32 * this changes the io permissions bitmap in the current task. 21 33 */ ··· 73 61 * Search for a (possibly new) maximum. This is simple and stupid, 74 62 * to keep it obviously correct: 75 63 */ 76 - max_long = 0; 64 + max_long = UINT_MAX; 77 65 for (i = 0; i < IO_BITMAP_LONGS; i++) { 78 66 if (iobm->bitmap[i] != ~0UL) 79 67 max_long = i; 68 + } 69 + /* All permissions dropped? */ 70 + if (max_long == UINT_MAX) { 71 + io_bitmap_exit(); 72 + return 0; 80 73 } 81 74 82 75 iobm->max = (max_long + 1) * sizeof(unsigned long);
+2 -13
arch/x86/kernel/process.c
··· 102 102 void exit_thread(struct task_struct *tsk) 103 103 { 104 104 struct thread_struct *t = &tsk->thread; 105 - struct io_bitmap *iobm = t->io_bitmap; 106 105 struct fpu *fpu = &t->fpu; 107 - struct tss_struct *tss; 108 106 109 - if (iobm) { 110 - preempt_disable(); 111 - tss = this_cpu_ptr(&cpu_tss_rw); 112 - 113 - t->io_bitmap = NULL; 114 - clear_thread_flag(TIF_IO_BITMAP); 115 - /* Invalidate the io bitmap base in the TSS */ 116 - tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID; 117 - preempt_enable(); 118 - kfree(iobm); 119 - } 107 + if (test_thread_flag(TIF_IO_BITMAP)) 108 + io_bitmap_exit(); 120 109 121 110 free_vm86(t); 122 111