Skip to content

Commit 577d5cd

Browse files
committed
x86/ioperm: Move iobitmap data into a struct
No point in having all the data in thread_struct, especially as upcoming changes add more. Make the bitmap in the new struct accessible as array of longs and as array of characters via a union, so both the bitmap functions and the update logic can avoid type casts. Signed-off-by: Thomas Gleixner <[email protected]>
1 parent f5848e5 commit 577d5cd

File tree

5 files changed

+57
-39
lines changed

5 files changed

+57
-39
lines changed

arch/x86/include/asm/io_bitmap.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _ASM_X86_IOBITMAP_H
3+
#define _ASM_X86_IOBITMAP_H
4+
5+
#include <asm/processor.h>
6+
7+
struct io_bitmap {
8+
/* The maximum number of bytes to copy so all zero bits are covered */
9+
unsigned int max;
10+
unsigned long bitmap[IO_BITMAP_LONGS];
11+
};
12+
13+
#endif

arch/x86/include/asm/processor.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/* Forward declaration, a strange C thing */
88
struct task_struct;
99
struct mm_struct;
10+
struct io_bitmap;
1011
struct vm86;
1112

1213
#include <asm/math_emu.h>
@@ -501,10 +502,8 @@ struct thread_struct {
501502
struct vm86 *vm86;
502503
#endif
503504
/* IO permissions: */
504-
unsigned long *io_bitmap_ptr;
505+
struct io_bitmap *io_bitmap;
505506
unsigned long iopl;
506-
/* Max allowed port in the bitmap, in bytes: */
507-
unsigned io_bitmap_max;
508507

509508
mm_segment_t addr_limit;
510509

@@ -862,7 +861,6 @@ static inline void spin_lock_prefetch(const void *x)
862861
#define INIT_THREAD { \
863862
.sp0 = TOP_OF_INIT_STACK, \
864863
.sysenter_cs = __KERNEL_CS, \
865-
.io_bitmap_ptr = NULL, \
866864
.addr_limit = KERNEL_DS, \
867865
}
868866

arch/x86/kernel/ioport.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/sched.h>
1212
#include <linux/slab.h>
1313

14+
#include <asm/io_bitmap.h>
1415
#include <asm/desc.h>
1516

1617
/*
@@ -21,7 +22,7 @@ long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
2122
unsigned int i, max_long, bytes, bytes_updated;
2223
struct thread_struct *t = &current->thread;
2324
struct tss_struct *tss;
24-
unsigned long *bitmap;
25+
struct io_bitmap *iobm;
2526

2627
if ((from + num <= from) || (from + num > IO_BITMAP_BITS))
2728
return -EINVAL;
@@ -34,16 +35,16 @@ long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
3435
* IO bitmap up. ioperm() is much less timing critical than clone(),
3536
* this is why we delay this operation until now:
3637
*/
37-
bitmap = t->io_bitmap_ptr;
38-
if (!bitmap) {
38+
iobm = t->io_bitmap;
39+
if (!iobm) {
3940
/* No point to allocate a bitmap just to clear permissions */
4041
if (!turn_on)
4142
return 0;
42-
bitmap = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
43-
if (!bitmap)
43+
iobm = kmalloc(sizeof(*iobm), GFP_KERNEL);
44+
if (!iobm)
4445
return -ENOMEM;
4546

46-
memset(bitmap, 0xff, IO_BITMAP_BYTES);
47+
memset(iobm->bitmap, 0xff, sizeof(iobm->bitmap));
4748
}
4849

4950
/*
@@ -52,36 +53,36 @@ long ksys_ioperm(unsigned long from, unsigned long num, int turn_on)
5253
*/
5354
preempt_disable();
5455
if (turn_on)
55-
bitmap_clear(bitmap, from, num);
56+
bitmap_clear(iobm->bitmap, from, num);
5657
else
57-
bitmap_set(bitmap, from, num);
58+
bitmap_set(iobm->bitmap, from, num);
5859

5960
/*
6061
* Search for a (possibly new) maximum. This is simple and stupid,
6162
* to keep it obviously correct:
6263
*/
6364
max_long = 0;
6465
for (i = 0; i < IO_BITMAP_LONGS; i++) {
65-
if (bitmap[i] != ~0UL)
66+
if (iobm->bitmap[i] != ~0UL)
6667
max_long = i;
6768
}
6869

6970
bytes = (max_long + 1) * sizeof(unsigned long);
70-
bytes_updated = max(bytes, t->io_bitmap_max);
71+
bytes_updated = max(bytes, t->io_bitmap->max);
7172

7273
/* Update the thread data */
73-
t->io_bitmap_max = bytes;
74+
iobm->max = bytes;
7475
/*
7576
* Store the bitmap pointer (might be the same if the task already
7677
* head one). Set the TIF flag, just in case this is the first
7778
* invocation.
7879
*/
79-
t->io_bitmap_ptr = bitmap;
80+
t->io_bitmap = iobm;
8081
set_thread_flag(TIF_IO_BITMAP);
8182

8283
/* Update the TSS */
8384
tss = this_cpu_ptr(&cpu_tss_rw);
84-
memcpy(tss->io_bitmap.bitmap, t->io_bitmap_ptr, bytes_updated);
85+
memcpy(tss->io_bitmap.bitmap, iobm->bitmap, bytes_updated);
8586
/* Store the new end of the zero bits */
8687
tss->io_bitmap.prev_max = bytes;
8788
/* Make the bitmap base in the TSS valid */

arch/x86/kernel/process.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <asm/desc.h>
4242
#include <asm/prctl.h>
4343
#include <asm/spec-ctrl.h>
44+
#include <asm/io_bitmap.h>
4445
#include <asm/proto.h>
4546

4647
#include "process.h"
@@ -101,21 +102,20 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
101102
void exit_thread(struct task_struct *tsk)
102103
{
103104
struct thread_struct *t = &tsk->thread;
104-
unsigned long *bp = t->io_bitmap_ptr;
105+
struct io_bitmap *iobm = t->io_bitmap;
105106
struct fpu *fpu = &t->fpu;
106107
struct tss_struct *tss;
107108

108-
if (bp) {
109+
if (iobm) {
109110
preempt_disable();
110111
tss = this_cpu_ptr(&cpu_tss_rw);
111112

112-
t->io_bitmap_ptr = NULL;
113-
t->io_bitmap_max = 0;
113+
t->io_bitmap = NULL;
114114
clear_thread_flag(TIF_IO_BITMAP);
115115
/* Invalidate the io bitmap base in the TSS */
116116
tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_INVALID;
117117
preempt_enable();
118-
kfree(bp);
118+
kfree(iobm);
119119
}
120120

121121
free_vm86(t);
@@ -135,25 +135,25 @@ static int set_new_tls(struct task_struct *p, unsigned long tls)
135135

136136
static inline int copy_io_bitmap(struct task_struct *tsk)
137137
{
138+
struct io_bitmap *iobm = current->thread.io_bitmap;
139+
138140
if (likely(!test_tsk_thread_flag(current, TIF_IO_BITMAP)))
139141
return 0;
140142

141-
tsk->thread.io_bitmap_ptr = kmemdup(current->thread.io_bitmap_ptr,
142-
IO_BITMAP_BYTES, GFP_KERNEL);
143-
if (!tsk->thread.io_bitmap_ptr) {
144-
tsk->thread.io_bitmap_max = 0;
143+
tsk->thread.io_bitmap = kmemdup(iobm, sizeof(*iobm), GFP_KERNEL);
144+
145+
if (!tsk->thread.io_bitmap)
145146
return -ENOMEM;
146-
}
147+
147148
set_tsk_thread_flag(tsk, TIF_IO_BITMAP);
148149
return 0;
149150
}
150151

151152
static inline void free_io_bitmap(struct task_struct *tsk)
152153
{
153-
if (tsk->thread.io_bitmap_ptr) {
154-
kfree(tsk->thread.io_bitmap_ptr);
155-
tsk->thread.io_bitmap_ptr = NULL;
156-
tsk->thread.io_bitmap_max = 0;
154+
if (tsk->thread.io_bitmap) {
155+
kfree(tsk->thread.io_bitmap);
156+
tsk->thread.io_bitmap = NULL;
157157
}
158158
}
159159

@@ -172,7 +172,7 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp,
172172
frame->bp = 0;
173173
frame->ret_addr = (unsigned long) ret_from_fork;
174174
p->thread.sp = (unsigned long) fork_frame;
175-
p->thread.io_bitmap_ptr = NULL;
175+
p->thread.io_bitmap = NULL;
176176
memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
177177

178178
#ifdef CONFIG_X86_64
@@ -366,6 +366,8 @@ static inline void switch_to_bitmap(struct thread_struct *next,
366366
struct tss_struct *tss = this_cpu_ptr(&cpu_tss_rw);
367367

368368
if (tifn & _TIF_IO_BITMAP) {
369+
struct io_bitmap *iobm = next->io_bitmap;
370+
369371
/*
370372
* Copy at least the size of the incoming tasks bitmap
371373
* which covers the last permitted I/O port.
@@ -374,11 +376,11 @@ static inline void switch_to_bitmap(struct thread_struct *next,
374376
* bits permitted, then the copy needs to cover those as
375377
* well so they get turned off.
376378
*/
377-
memcpy(tss->io_bitmap.bitmap, next->io_bitmap_ptr,
378-
max(tss->io_bitmap.prev_max, next->io_bitmap_max));
379+
memcpy(tss->io_bitmap.bitmap, next->io_bitmap->bitmap,
380+
max(tss->io_bitmap.prev_max, next->io_bitmap->max));
379381

380382
/* Store the new max and set io_bitmap_base valid */
381-
tss->io_bitmap.prev_max = next->io_bitmap_max;
383+
tss->io_bitmap.prev_max = next->io_bitmap->max;
382384
tss->x86_tss.io_bitmap_base = IO_BITMAP_OFFSET_VALID;
383385

384386
/*

arch/x86/kernel/ptrace.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <asm/traps.h>
4343
#include <asm/syscall.h>
4444
#include <asm/fsgsbase.h>
45+
#include <asm/io_bitmap.h>
4546

4647
#include "tls.h"
4748

@@ -697,20 +698,23 @@ static int ptrace_set_debugreg(struct task_struct *tsk, int n,
697698
static int ioperm_active(struct task_struct *target,
698699
const struct user_regset *regset)
699700
{
700-
return DIV_ROUND_UP(target->thread.io_bitmap_max, regset->size);
701+
struct io_bitmap *iobm = target->thread.io_bitmap;
702+
703+
return iobm ? DIV_ROUND_UP(iobm->max, regset->size) : 0;
701704
}
702705

703706
static int ioperm_get(struct task_struct *target,
704707
const struct user_regset *regset,
705708
unsigned int pos, unsigned int count,
706709
void *kbuf, void __user *ubuf)
707710
{
708-
if (!target->thread.io_bitmap_ptr)
711+
struct io_bitmap *iobm = target->thread.io_bitmap;
712+
713+
if (!iobm)
709714
return -ENXIO;
710715

711716
return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
712-
target->thread.io_bitmap_ptr,
713-
0, IO_BITMAP_BYTES);
717+
iobm->bitmap, 0, IO_BITMAP_BYTES);
714718
}
715719

716720
/*

0 commit comments

Comments
 (0)