Skip to content

Commit 060aa16

Browse files
committed
x86/ioperm: Add bitmap sequence number
Add a globally unique sequence number which is incremented when ioperm() is changing the I/O bitmap of a task. Store the new sequence number in the io_bitmap structure and compare it with the sequence number of the I/O bitmap which was last loaded on a CPU. Only update the bitmap if the sequence is different. That should further reduce the overhead of I/O bitmap scheduling when there are only a few I/O bitmap users on the system. The 64bit sequence counter is sufficient. A wraparound of the sequence counter assuming an ioperm() call every nanosecond would require about 584 years of uptime. Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]>
1 parent 577d5cd commit 060aa16

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

arch/x86/include/asm/io_bitmap.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <asm/processor.h>
66

77
struct io_bitmap {
8+
u64 sequence;
89
/* The maximum number of bytes to copy so all zero bits are covered */
910
unsigned int max;
1011
unsigned long bitmap[IO_BITMAP_LONGS];

arch/x86/include/asm/processor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ struct entry_stack_page {
361361
* All IO bitmap related data stored in the TSS:
362362
*/
363363
struct x86_io_bitmap {
364+
/* The sequence number of the last active bitmap. */
365+
u64 prev_sequence;
366+
364367
/*
365368
* Store the dirty size of the last io bitmap offender. The next
366369
* one will have to do the cleanup as the switch out to a non io

arch/x86/kernel/cpu/common.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,7 @@ void cpu_init(void)
18621862
tss_setup_ist(tss);
18631863
tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID;
18641864
tss->io_bitmap.prev_max = 0;
1865+
tss->io_bitmap.prev_sequence = 0;
18651866
memset(tss->io_bitmap.bitmap, 0xff, sizeof(tss->io_bitmap.bitmap));
18661867
set_tss_desc(cpu, &get_cpu_entry_area(cpu)->tss.x86_tss);
18671868

arch/x86/kernel/ioport.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <asm/io_bitmap.h>
1515
#include <asm/desc.h>
1616

17+
static atomic64_t io_bitmap_sequence;
18+
1719
/*
1820
* this changes the io permissions bitmap in the current task.
1921
*/
@@ -72,6 +74,9 @@ long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
7274

7375
/* Update the thread data */
7476
iobm->max = bytes;
77+
/* Update the sequence number to force an update in switch_to() */
78+
iobm->sequence = atomic64_add_return(1, &io_bitmap_sequence);
79+
7580
/*
7681
* Store the bitmap pointer (might be the same if the task already
7782
* head one). Set the TIF flag, just in case this is the first

arch/x86/kernel/process.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,28 @@ void arch_setup_new_exec(void)
360360
}
361361
}
362362

363+
static void switch_to_update_io_bitmap(struct tss_struct *tss,
364+
struct io_bitmap *iobm)
365+
{
366+
/*
367+
* Copy at least the byte range of the incoming tasks bitmap which
368+
* covers the permitted I/O ports.
369+
*
370+
* If the previous task which used an I/O bitmap had more bits
371+
* permitted, then the copy needs to cover those as well so they
372+
* get turned off.
373+
*/
374+
memcpy(tss->io_bitmap.bitmap, iobm->bitmap,
375+
max(tss->io_bitmap.prev_max, iobm->max));
376+
377+
/*
378+
* Store the new max and the sequence number of this bitmap
379+
* and a pointer to the bitmap itself.
380+
*/
381+
tss->io_bitmap.prev_max = iobm->max;
382+
tss->io_bitmap.prev_sequence = iobm->sequence;
383+
}
384+
363385
static inline void switch_to_bitmap(struct thread_struct *next,
364386
unsigned long tifp, unsigned long tifn)
365387
{
@@ -369,18 +391,14 @@ static inline void switch_to_bitmap(struct thread_struct *next,
369391
struct io_bitmap *iobm = next->io_bitmap;
370392

371393
/*
372-
* Copy at least the size of the incoming tasks bitmap
373-
* which covers the last permitted I/O port.
374-
*
375-
* If the previous task which used an io bitmap had more
376-
* bits permitted, then the copy needs to cover those as
377-
* well so they get turned off.
394+
* Only copy bitmap data when the sequence number
395+
* differs. The update time is accounted to the incoming
396+
* task.
378397
*/
379-
memcpy(tss->io_bitmap.bitmap, next->io_bitmap->bitmap,
380-
max(tss->io_bitmap.prev_max, next->io_bitmap->max));
398+
if (tss->io_bitmap.prev_sequence != iobm->sequence)
399+
switch_to_update_io_bitmap(tss, iobm);
381400

382-
/* Store the new max and set io_bitmap_base valid */
383-
tss->io_bitmap.prev_max = next->io_bitmap->max;
401+
/* Enable the bitmap */
384402
tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_VALID;
385403

386404
/*

0 commit comments

Comments
 (0)