Skip to content

Commit ecc7e37

Browse files
committed
x86/io: Speedup schedule out of I/O bitmap user
There is no requirement to update the TSS I/O bitmap when a thread using it is scheduled out and the incoming thread does not use it. For the permission check based on the TSS I/O bitmap the CPU calculates the memory location of the I/O bitmap by the address of the TSS and the io_bitmap_base member of the tss_struct. The easiest way to invalidate the I/O bitmap is to switch the offset to an address outside of the TSS limit. If an I/O instruction is issued from user space the TSS limit causes #GP to be raised in the same was as valid I/O bitmap with all bits set to 1 would do. This removes the extra work when an I/O bitmap using task is scheduled out and puts the burden on the rare I/O bitmap users when they are scheduled in. Signed-off-by: Thomas Gleixner <[email protected]>
1 parent 32f3bf6 commit ecc7e37

File tree

5 files changed

+69
-41
lines changed

5 files changed

+69
-41
lines changed

arch/x86/include/asm/processor.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,23 @@ struct x86_hw_tss {
330330
#define IO_BITMAP_BITS 65536
331331
#define IO_BITMAP_BYTES (IO_BITMAP_BITS/8)
332332
#define IO_BITMAP_LONGS (IO_BITMAP_BYTES/sizeof(long))
333-
#define IO_BITMAP_OFFSET (offsetof(struct tss_struct, io_bitmap) - offsetof(struct tss_struct, x86_tss))
334-
#define INVALID_IO_BITMAP_OFFSET 0x8000
333+
334+
#define IO_BITMAP_OFFSET_VALID \
335+
(offsetof(struct tss_struct, io_bitmap) - \
336+
offsetof(struct tss_struct, x86_tss))
337+
338+
/*
339+
* sizeof(unsigned long) coming from an extra "long" at the end
340+
* of the iobitmap.
341+
*
342+
* -1? seg base+limit should be pointing to the address of the
343+
* last valid byte
344+
*/
345+
#define __KERNEL_TSS_LIMIT \
346+
(IO_BITMAP_OFFSET_VALID + IO_BITMAP_BYTES + sizeof(unsigned long) - 1)
347+
348+
/* Base offset outside of TSS_LIMIT so unpriviledged IO causes #GP */
349+
#define IO_BITMAP_OFFSET_INVALID (__KERNEL_TSS_LIMIT + 1)
335350

336351
struct entry_stack {
337352
unsigned long words[64];
@@ -349,6 +364,15 @@ struct tss_struct {
349364
*/
350365
struct x86_hw_tss x86_tss;
351366

367+
/*
368+
* Store the dirty size of the last io bitmap offender. The next
369+
* one will have to do the cleanup as the switch out to a non io
370+
* bitmap user will just set x86_tss.io_bitmap_base to a value
371+
* outside of the TSS limit. So for sane tasks there is no need to
372+
* actually touch the io_bitmap at all.
373+
*/
374+
unsigned int io_bitmap_prev_max;
375+
352376
/*
353377
* The extra 1 is there because the CPU will access an
354378
* additional byte beyond the end of the IO permission
@@ -360,16 +384,6 @@ struct tss_struct {
360384

361385
DECLARE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw);
362386

363-
/*
364-
* sizeof(unsigned long) coming from an extra "long" at the end
365-
* of the iobitmap.
366-
*
367-
* -1? seg base+limit should be pointing to the address of the
368-
* last valid byte
369-
*/
370-
#define __KERNEL_TSS_LIMIT \
371-
(IO_BITMAP_OFFSET + IO_BITMAP_BYTES + sizeof(unsigned long) - 1)
372-
373387
/* Per CPU interrupt stacks */
374388
struct irq_stack {
375389
char stack[IRQ_STACK_SIZE];

arch/x86/kernel/cpu/common.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,8 @@ void cpu_init(void)
18601860

18611861
/* Initialize the TSS. */
18621862
tss_setup_ist(tss);
1863-
tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET;
1863+
tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID;
1864+
tss->io_bitmap_prev_max = 0;
18641865
memset(tss->io_bitmap, 0xff, sizeof(tss->io_bitmap));
18651866
set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
18661867

arch/x86/kernel/doublefault.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct x86_hw_tss doublefault_tss __cacheline_aligned = {
5454
.sp0 = STACK_START,
5555
.ss0 = __KERNEL_DS,
5656
.ldt = 0,
57-
.io_bitmap_base = INVALID_IO_BITMAP_OFFSET,
57+
.io_bitmap_base = IO_BITMAP_OFFSET_INVALID,
5858

5959
.ip = (unsigned long) doublefault_fn,
6060
/* 0x2 bit is always set */

arch/x86/kernel/ioport.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
8282
/* Update the TSS */
8383
tss = this_cpu_ptr(&cpu_tss_rw);
8484
memcpy(tss->io_bitmap, t->io_bitmap_ptr, bytes_updated);
85+
/* Store the new end of the zero bits */
86+
tss->io_bitmap_prev_max = bytes;
87+
/* Make the bitmap base in the TSS valid */
88+
tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_VALID;
8589
/* Make sure the TSS limit covers the I/O bitmap. */
8690
refresh_tss_limit();
8791

arch/x86/kernel/process.c

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,9 @@ __visible DEFINE_PER_CPU_PAGE_ALIGNED(struct tss_struct, cpu_tss_rw) = {
7272
#ifdef CONFIG_X86_32
7373
.ss0 = __KERNEL_DS,
7474
.ss1 = __KERNEL_CS,
75-
.io_bitmap_base = INVALID_IO_BITMAP_OFFSET,
7675
#endif
76+
.io_bitmap_base = IO_BITMAP_OFFSET_INVALID,
7777
},
78-
#ifdef CONFIG_X86_32
79-
/*
80-
* Note that the .io_bitmap member must be extra-big. This is because
81-
* the CPU will access an additional byte beyond the end of the IO
82-
* permission bitmap. The extra byte must be all 1 bits, and must
83-
* be within the limit.
84-
*/
85-
.io_bitmap = { [0 ... IO_BITMAP_LONGS] = ~0 },
86-
#endif
8778
};
8879
EXPORT_PER_CPU_SYMBOL(cpu_tss_rw);
8980

@@ -112,18 +103,18 @@ void exit_thread(struct task_struct *tsk)
112103
struct thread_struct *t = &tsk->thread;
113104
unsigned long *bp = t->io_bitmap_ptr;
114105
struct fpu *fpu = &t->fpu;
106+
struct tss_struct *tss;
115107

116108
if (bp) {
117-
struct tss_struct *tss = &per_cpu(cpu_tss_rw, get_cpu());
109+
preempt_disable();
110+
tss = this_cpu_ptr(&cpu_tss_rw);
118111

119112
t->io_bitmap_ptr = NULL;
120-
clear_thread_flag(TIF_IO_BITMAP);
121-
/*
122-
* Careful, clear this in the TSS too:
123-
*/
124-
memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
125113
t->io_bitmap_max = 0;
126-
put_cpu();
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();
127118
kfree(bp);
128119
}
129120

@@ -369,29 +360,47 @@ void arch_setup_new_exec(void)
369360
}
370361
}
371362

372-
static inline void switch_to_bitmap(struct thread_struct *prev,
373-
struct thread_struct *next,
363+
static inline void switch_to_bitmap(struct thread_struct *next,
374364
unsigned long tifp, unsigned long tifn)
375365
{
376366
struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
377367

378368
if (tifn & _TIF_IO_BITMAP) {
379369
/*
380-
* Copy the relevant range of the IO bitmap.
381-
* Normally this is 128 bytes or less:
370+
* Copy at least the size of the incoming tasks bitmap
371+
* which covers the last permitted I/O port.
372+
*
373+
* If the previous task which used an io bitmap had more
374+
* bits permitted, then the copy needs to cover those as
375+
* well so they get turned off.
382376
*/
383377
memcpy(tss->io_bitmap, next->io_bitmap_ptr,
384-
max(prev->io_bitmap_max, next->io_bitmap_max));
378+
max(tss->io_bitmap_prev_max, next->io_bitmap_max));
379+
380+
/* Store the new max and set io_bitmap_base valid */
381+
tss->io_bitmap_prev_max = next->io_bitmap_max;
382+
tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_VALID;
383+
385384
/*
386-
* Make sure that the TSS limit is correct for the CPU
387-
* to notice the IO bitmap.
385+
* Make sure that the TSS limit is covering the io bitmap.
386+
* It might have been cut down by a VMEXIT to 0x67 which
387+
* would cause a subsequent I/O access from user space to
388+
* trigger a #GP because tbe bitmap is outside the TSS
389+
* limit.
388390
*/
389391
refresh_tss_limit();
390392
} else if (tifp & _TIF_IO_BITMAP) {
391393
/*
392-
* Clear any possible leftover bits:
394+
* Do not touch the bitmap. Let the next bitmap using task
395+
* deal with the mess. Just make the io_bitmap_base invalid
396+
* by moving it outside the TSS limit so any subsequent I/O
397+
* access from user space will trigger a #GP.
398+
*
399+
* This is correct even when VMEXIT rewrites the TSS limit
400+
* to 0x67 as the only requirement is that the base points
401+
* outside the limit.
393402
*/
394-
memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
403+
tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID;
395404
}
396405
}
397406

@@ -605,7 +614,7 @@ void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p)
605614

606615
tifn = READ_ONCE(task_thread_info(next_p)->flags);
607616
tifp = READ_ONCE(task_thread_info(prev_p)->flags);
608-
switch_to_bitmap(prev, next, tifp, tifn);
617+
switch_to_bitmap(next, tifp, tifn);
609618

610619
propagate_user_return_notify(prev_p, next_p);
611620

0 commit comments

Comments
 (0)