Skip to content

Commit b3cf257

Browse files
Stephane EranianLinus Torvalds
authored andcommitted
[PATCH] i386: use thread_info flags for debug regs and IO bitmaps
Use thread info flags to track use of debug registers and IO bitmaps. - add TIF_DEBUG to track when debug registers are active - add TIF_IO_BITMAP to track when I/O bitmap is used - modify __switch_to() to use the new TIF flags Performance tested on Pentium II, ten runs of LMbench context switch benchmark (smaller is better:) before after avg 3.65 3.39 min 3.55 3.33 Signed-off-by: Stephane Eranian <eranian@hpl.hp.com> Signed-off-by: Chuck Ebbert <76306.1226@compuserve.com> Acked-by: Andi Kleen <ak@suse.de> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 09075ef commit b3cf257

4 files changed

Lines changed: 41 additions & 22 deletions

File tree

‎arch/i386/kernel/ioport.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ asmlinkage long sys_ioperm(unsigned long from, unsigned long num, int turn_on)
7979

8080
memset(bitmap, 0xff, IO_BITMAP_BYTES);
8181
t->io_bitmap_ptr = bitmap;
82+
set_thread_flag(TIF_IO_BITMAP);
8283
}
8384

8485
/*

‎arch/i386/kernel/process.c‎

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,16 @@ EXPORT_SYMBOL(kernel_thread);
359359
*/
360360
void exit_thread(void)
361361
{
362-
struct task_struct *tsk = current;
363-
struct thread_struct *t = &tsk->thread;
364-
365362
/* The process may have allocated an io port bitmap... nuke it. */
366-
if (unlikely(NULL != t->io_bitmap_ptr)) {
363+
if (unlikely(test_thread_flag(TIF_IO_BITMAP))) {
364+
struct task_struct *tsk = current;
365+
struct thread_struct *t = &tsk->thread;
367366
int cpu = get_cpu();
368367
struct tss_struct *tss = &per_cpu(init_tss, cpu);
369368

370369
kfree(t->io_bitmap_ptr);
371370
t->io_bitmap_ptr = NULL;
371+
clear_thread_flag(TIF_IO_BITMAP);
372372
/*
373373
* Careful, clear this in the TSS too:
374374
*/
@@ -387,6 +387,7 @@ void flush_thread(void)
387387

388388
memset(tsk->thread.debugreg, 0, sizeof(unsigned long)*8);
389389
memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
390+
clear_tsk_thread_flag(tsk, TIF_DEBUG);
390391
/*
391392
* Forget coprocessor state..
392393
*/
@@ -431,14 +432,15 @@ int copy_thread(int nr, unsigned long clone_flags, unsigned long esp,
431432
savesegment(gs,p->thread.gs);
432433

433434
tsk = current;
434-
if (unlikely(NULL != tsk->thread.io_bitmap_ptr)) {
435+
if (unlikely(test_tsk_thread_flag(tsk, TIF_IO_BITMAP))) {
435436
p->thread.io_bitmap_ptr = kmalloc(IO_BITMAP_BYTES, GFP_KERNEL);
436437
if (!p->thread.io_bitmap_ptr) {
437438
p->thread.io_bitmap_max = 0;
438439
return -ENOMEM;
439440
}
440441
memcpy(p->thread.io_bitmap_ptr, tsk->thread.io_bitmap_ptr,
441442
IO_BITMAP_BYTES);
443+
set_tsk_thread_flag(p, TIF_IO_BITMAP);
442444
}
443445

444446
/*
@@ -533,17 +535,32 @@ int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
533535
return 1;
534536
}
535537

536-
static inline void
537-
handle_io_bitmap(struct thread_struct *next, struct tss_struct *tss)
538+
static noinline void __switch_to_xtra(struct task_struct *next_p,
539+
struct tss_struct *tss)
538540
{
539-
if (!next->io_bitmap_ptr) {
541+
struct thread_struct *next;
542+
543+
next = &next_p->thread;
544+
545+
if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
546+
set_debugreg(next->debugreg[0], 0);
547+
set_debugreg(next->debugreg[1], 1);
548+
set_debugreg(next->debugreg[2], 2);
549+
set_debugreg(next->debugreg[3], 3);
550+
/* no 4 and 5 */
551+
set_debugreg(next->debugreg[6], 6);
552+
set_debugreg(next->debugreg[7], 7);
553+
}
554+
555+
if (!test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
540556
/*
541557
* Disable the bitmap via an invalid offset. We still cache
542558
* the previous bitmap owner and the IO bitmap contents:
543559
*/
544560
tss->io_bitmap_base = INVALID_IO_BITMAP_OFFSET;
545561
return;
546562
}
563+
547564
if (likely(next == tss->io_bitmap_owner)) {
548565
/*
549566
* Previous owner of the bitmap (hence the bitmap content)
@@ -671,20 +688,11 @@ struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct tas
671688
set_iopl_mask(next->iopl);
672689

673690
/*
674-
* Now maybe reload the debug registers
691+
* Now maybe handle debug registers and/or IO bitmaps
675692
*/
676-
if (unlikely(next->debugreg[7])) {
677-
set_debugreg(next->debugreg[0], 0);
678-
set_debugreg(next->debugreg[1], 1);
679-
set_debugreg(next->debugreg[2], 2);
680-
set_debugreg(next->debugreg[3], 3);
681-
/* no 4 and 5 */
682-
set_debugreg(next->debugreg[6], 6);
683-
set_debugreg(next->debugreg[7], 7);
684-
}
685-
686-
if (unlikely(prev->io_bitmap_ptr || next->io_bitmap_ptr))
687-
handle_io_bitmap(next, tss);
693+
if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW))
694+
|| test_tsk_thread_flag(prev_p, TIF_IO_BITMAP))
695+
__switch_to_xtra(next_p, tss);
688696

689697
disable_tsc(prev_p, next_p);
690698

‎arch/i386/kernel/ptrace.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,11 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
468468
for(i=0; i<4; i++)
469469
if ((0x5f54 >> ((data >> (16 + 4*i)) & 0xf)) & 1)
470470
goto out_tsk;
471+
if (data)
472+
set_tsk_thread_flag(child, TIF_DEBUG);
473+
else
474+
clear_tsk_thread_flag(child, TIF_DEBUG);
471475
}
472-
473476
addr -= (long) &dummy->u_debugreg;
474477
addr = addr >> 2;
475478
child->thread.debugreg[addr] = data;

‎include/asm-i386/thread_info.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ static inline struct thread_info *current_thread_info(void)
140140
#define TIF_SECCOMP 8 /* secure computing */
141141
#define TIF_RESTORE_SIGMASK 9 /* restore signal mask in do_signal() */
142142
#define TIF_MEMDIE 16
143+
#define TIF_DEBUG 17 /* uses debug registers */
144+
#define TIF_IO_BITMAP 18 /* uses I/O bitmap */
143145

144146
#define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
145147
#define _TIF_NOTIFY_RESUME (1<<TIF_NOTIFY_RESUME)
@@ -151,6 +153,8 @@ static inline struct thread_info *current_thread_info(void)
151153
#define _TIF_SYSCALL_AUDIT (1<<TIF_SYSCALL_AUDIT)
152154
#define _TIF_SECCOMP (1<<TIF_SECCOMP)
153155
#define _TIF_RESTORE_SIGMASK (1<<TIF_RESTORE_SIGMASK)
156+
#define _TIF_DEBUG (1<<TIF_DEBUG)
157+
#define _TIF_IO_BITMAP (1<<TIF_IO_BITMAP)
154158

155159
/* work to do on interrupt/exception return */
156160
#define _TIF_WORK_MASK \
@@ -159,6 +163,9 @@ static inline struct thread_info *current_thread_info(void)
159163
/* work to do on any return to u-space */
160164
#define _TIF_ALLWORK_MASK (0x0000FFFF & ~_TIF_SECCOMP)
161165

166+
/* flags to check in __switch_to() */
167+
#define _TIF_WORK_CTXSW (_TIF_DEBUG|_TIF_IO_BITMAP)
168+
162169
/*
163170
* Thread-synchronous status.
164171
*

0 commit comments

Comments
 (0)