linux-um archives
 help / color / mirror / Atom feed
* [uml-devel] [PATCH] host context switch reduction
@ 2004-02-29  4:35 Jeff Dike
  2004-02-29 16:48 ` BlaisorBlade
                   ` (2 more replies)
  0 siblings, 3 replies; 30+ messages in thread
From: Jeff Dike @ 2004-02-29  4:35 UTC (permalink / raw)
  To: user-mode-linux-devel

[-- Attachment #1: Type: text/plain, Size: 436 bytes --]

The patches below are from Laurent Vivier who didn't make them public.  They
add a new feature to ptrace on the host which cuts down on the number of
context switches needed for a UML system call, plus makes UML use it.

There's some interest in this, so I'm putting it out as-is.  I haven't played
with it.

Laurent did some performance testing and found ~40% speedup on a getpid
loop, and a 3.5% speedup on a kernel build.

				Jeff


[-- Attachment #2: x --]
[-- Type: text/plain, Size: 4539 bytes --]

diff -Nrc skas3-linux-2.4.22/Makefile skas4-linux-2.4.22/Makefile
*** skas3-linux-2.4.22/Makefile	Tue Dec  2 21:24:14 2003
--- skas4-linux-2.4.22/Makefile	Tue Dec  2 20:32:41 2003
***************
*** 1,7 ****
  VERSION = 2
  PATCHLEVEL = 4
  SUBLEVEL = 22
! EXTRAVERSION = -skas3
  
  KERNELRELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
  
--- 1,7 ----
  VERSION = 2
  PATCHLEVEL = 4
  SUBLEVEL = 22
! EXTRAVERSION = -sysemu
  
  KERNELRELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
  
diff -Nrc skas3-linux-2.4.22/arch/i386/kernel/entry.S skas4-linux-2.4.22/arch/i386/kernel/entry.S
*** skas3-linux-2.4.22/arch/i386/kernel/entry.S	Fri Jun 13 16:51:29 2003
--- skas4-linux-2.4.22/arch/i386/kernel/entry.S	Tue Dec  2 20:27:41 2003
***************
*** 203,208 ****
--- 203,210 ----
  	pushl %eax			# save orig_eax
  	SAVE_ALL
  	GET_CURRENT(%ebx)
+ 	testb $0x20,tsk_ptrace(%ebx)	# PT_SCEMU
+ 	jne emulatesys
  	testb $0x02,tsk_ptrace(%ebx)	# PT_TRACESYS
  	jne tracesys
  	cmpl $(NR_syscalls),%eax
***************
*** 237,242 ****
--- 239,248 ----
  	jmp restore_all
  
  	ALIGN
+ emulatesys:
+ 	call SYMBOL_NAME(syscall_emulate)
+ 	jmp ret_from_sys_call
+ 	ALIGN
  tracesys:
  	movl $-ENOSYS,EAX(%esp)
  	call SYMBOL_NAME(syscall_trace)
diff -Nrc skas3-linux-2.4.22/arch/i386/kernel/ptrace.c skas4-linux-2.4.22/arch/i386/kernel/ptrace.c
*** skas3-linux-2.4.22/arch/i386/kernel/ptrace.c	Tue Dec  2 21:24:01 2003
--- skas4-linux-2.4.22/arch/i386/kernel/ptrace.c	Tue Dec  2 18:52:10 2003
***************
*** 275,280 ****
--- 275,281 ----
  		  }
  		  break;
  
+ 	case PTRACE_SCEMU: /* continue and replace next syscall */
  	case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  	case PTRACE_CONT: { /* restart after signal. */
  		long tmp;
***************
*** 282,287 ****
--- 283,292 ----
  		ret = -EIO;
  		if ((unsigned long) data > _NSIG)
  			break;
+ 		if (request == PTRACE_SCEMU)
+ 			child->ptrace |= PT_SCEMU;
+ 		else
+ 			child->ptrace &= ~PT_SCEMU;
  		if (request == PTRACE_SYSCALL)
  			child->ptrace |= PT_TRACESYS;
  		else
***************
*** 320,326 ****
  		ret = -EIO;
  		if ((unsigned long) data > _NSIG)
  			break;
! 		child->ptrace &= ~PT_TRACESYS;
  		if ((child->ptrace & PT_DTRACE) == 0) {
  			/* Spurious delayed TF traps may occur */
  			child->ptrace |= PT_DTRACE;
--- 325,331 ----
  		ret = -EIO;
  		if ((unsigned long) data > _NSIG)
  			break;
! 		child->ptrace &= ~(PT_TRACESYS|PT_SCEMU);
  		if ((child->ptrace & PT_DTRACE) == 0) {
  			/* Spurious delayed TF traps may occur */
  			child->ptrace |= PT_DTRACE;
***************
*** 482,487 ****
--- 487,515 ----
  	return ret;
  }
  
+ asmlinkage void syscall_emulate(void)
+ {
+ 	if ((current->ptrace & (PT_PTRACED|PT_SCEMU)) !=
+ 			(PT_PTRACED|PT_SCEMU))
+ 		return;
+ 	/* the 0x80 provides a way for the tracing parent to distinguish
+ 	   between a syscall stop and SIGTRAP delivery */
+ 	current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
+ 					? 0x80 : 0);
+ 	current->state = TASK_STOPPED;
+ 	notify_parent(current, SIGCHLD);
+ 	schedule();
+ 	/*
+ 	 * this isn't the same as continuing with a signal, but it will do
+ 	 * for normal use.  strace only continues with a signal if the
+ 	 * stopping signal is not SIGTRAP.  -brl
+ 	 */
+ 	if (current->exit_code) {
+ 		send_sig(current->exit_code, current, 1);
+ 		current->exit_code = 0;
+ 	}
+ }
+ 
  asmlinkage void syscall_trace(void)
  {
  	if ((current->ptrace & (PT_PTRACED|PT_TRACESYS)) !=
diff -Nrc skas3-linux-2.4.22/include/linux/ptrace.h skas4-linux-2.4.22/include/linux/ptrace.h
*** skas3-linux-2.4.22/include/linux/ptrace.h	Sat Sep 15 00:55:17 2001
--- skas4-linux-2.4.22/include/linux/ptrace.h	Tue Dec  2 18:42:55 2003
***************
*** 20,25 ****
--- 20,26 ----
  #define PTRACE_DETACH		0x11
  
  #define PTRACE_SYSCALL		  24
+ #define PTRACE_SCEMU		  25
  
  #include <asm/ptrace.h>
  
diff -Nrc skas3-linux-2.4.22/include/linux/sched.h skas4-linux-2.4.22/include/linux/sched.h
*** skas3-linux-2.4.22/include/linux/sched.h	Fri Jun 13 16:51:39 2003
--- skas4-linux-2.4.22/include/linux/sched.h	Tue Dec  2 20:28:19 2003
***************
*** 444,449 ****
--- 444,450 ----
  #define PT_DTRACE	0x00000004	/* delayed trace (used on m68k, i386) */
  #define PT_TRACESYSGOOD	0x00000008
  #define PT_PTRACE_CAP	0x00000010	/* ptracer can follow suid-exec */
+ #define PT_SCEMU	0x00000020	/* syscall emulation for UML */
  
  #define is_dumpable(tsk)    ((tsk)->task_dumpable && (tsk)->mm && (tsk)->mm->dumpable)
  


[-- Attachment #3: y --]
[-- Type: text/plain, Size: 4124 bytes --]

diff -Nrc uml-linux-2.4.22/arch/um/kernel/process.c sysemu-uml-linux-2.4.22/arch/um/kernel/process.c
*** uml-linux-2.4.22/arch/um/kernel/process.c	Tue Oct 14 15:28:28 2003
--- sysemu-uml-linux-2.4.22/arch/um/kernel/process.c	Thu Dec  4 20:14:33 2003
***************
*** 222,227 ****
--- 222,243 ----
  	}
  	stop_ptraced_child(pid, stack, 0);
  	printk("OK\n");
+ 
+ #ifdef PTRACE_SYSEMU
+ 	printk("Checking syscall emulation patch for ptrace...");
+ 
+ 	use_sysemu = 0;
+ 	pid = start_ptraced_child(&stack);
+ 	if(ptrace(PTRACE_SYSEMU, pid, 0, 0) >= 0)
+ 	{
+ 		printk("OK\n");
+         	use_sysemu = 1;
+ 	}
+ 	else
+ 		printk("missing\n");
+ 
+ 	stop_ptraced_child(pid, stack, 0);
+ #endif
  }
  
  int run_kernel_thread(int (*fn)(void *), void *arg, void **jmp_ptr)
diff -Nrc uml-linux-2.4.22/arch/um/kernel/skas/include/ptrace-skas.h sysemu-uml-linux-2.4.22/arch/um/kernel/skas/include/ptrace-skas.h
*** uml-linux-2.4.22/arch/um/kernel/skas/include/ptrace-skas.h	Tue Oct 14 15:28:28 2003
--- sysemu-uml-linux-2.4.22/arch/um/kernel/skas/include/ptrace-skas.h	Thu Dec  4 20:15:34 2003
***************
*** 10,15 ****
--- 10,22 ----
  
  #ifdef UML_CONFIG_MODE_SKAS
  
+ /* syscall emulation path in ptrace */
+ 
+ #ifndef PTRACE_SYSEMU
+ #define PTRACE_SYSEMU 25
+ #endif
+ extern int use_sysemu;
+ 
  #include "skas_ptregs.h"
  
  #define HOST_FRAME_SIZE 17
diff -Nrc uml-linux-2.4.22/arch/um/kernel/skas/process.c sysemu-uml-linux-2.4.22/arch/um/kernel/skas/process.c
*** uml-linux-2.4.22/arch/um/kernel/skas/process.c	Wed Dec  3 18:53:54 2003
--- sysemu-uml-linux-2.4.22/arch/um/kernel/skas/process.c	Fri Dec  5 17:57:03 2003
***************
*** 27,32 ****
--- 27,36 ----
  #include "skas_ptrace.h"
  #include "chan_user.h"
  
+ #ifdef PTRACE_SYSEMU
+ int use_sysemu = 0;
+ #endif
+ 
  int is_skas_winch(int pid, int fd, void *data)
  {
  	if(pid != getpid())
***************
*** 65,85 ****
  		return;
  	}
  
! 	err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
! 	if(err < 0)
! 	        panic("handle_trap - nullifying syscall failed errno = %d\n", 
! 		      errno);
  
! 	err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
! 	if(err < 0)
! 	        panic("handle_trap - continuing to end of syscall failed, "
! 		      "errno = %d\n", errno);
! 
! 	err = waitpid(pid, &status, WUNTRACED);
! 	if((err < 0) || !WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
! 		panic("handle_trap - failed to wait at end of syscall, "
! 		      "errno = %d, status = %d\n", errno, status);
  
  	handle_syscall(regs);
  }
  
--- 69,91 ----
  		return;
  	}
  
! 	if (!use_sysemu)
! 	{
! 		err = ptrace(PTRACE_POKEUSER, pid, PT_SYSCALL_NR_OFFSET, __NR_getpid);
! 		if(err < 0)
! 	        	panic("handle_trap - nullifying syscall failed errno = %d\n", 
! 		      	errno);
  
! 		err = ptrace(PTRACE_SYSCALL, pid, 0, 0);
! 		if(err < 0)
! 	        	panic("handle_trap - continuing to end of syscall failed, "
! 		      	"errno = %d\n", errno);
  
+ 		err = waitpid(pid, &status, WUNTRACED);
+ 		if((err < 0) || !WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
+ 			panic("handle_trap - failed to wait at end of syscall, "
+ 		      	"errno = %d, status = %d\n", errno, status);
+ 	}
  	handle_syscall(regs);
  }
  
***************
*** 134,140 ****
--- 140,154 ----
  
  	restore_registers(regs);
  		
+ #ifdef PTRACE_SYSEMU
+ 	if (use_sysemu)
+ 		err = ptrace(PTRACE_SYSEMU, userspace_pid, 0, 0);
+ 	else
+ 		err = ptrace(PTRACE_SYSCALL, userspace_pid, 0, 0);
+ #else
  	err = ptrace(PTRACE_SYSCALL, userspace_pid, 0, 0);
+ #endif
+ 
  	if(err)
  		panic("userspace - PTRACE_SYSCALL failed, errno = %d\n", 
  		       errno);
***************
*** 172,179 ****
--- 186,202 ----
  
  		restore_registers(regs);
  
+ #ifdef PTRACE_SYSEMU
+ 		if (use_sysemu)
+ 			op = singlestepping_skas() ? PTRACE_SINGLESTEP : 
+ 				PTRACE_SYSEMU;
+ 		else
+ 			op = singlestepping_skas() ? PTRACE_SINGLESTEP : 
+ 				PTRACE_SYSCALL;
+ #else
  		op = singlestepping_skas() ? PTRACE_SINGLESTEP : 
  			PTRACE_SYSCALL;
+ #endif
  		err = ptrace(op, userspace_pid, 0, 0);
  		if(err)
  			panic("userspace - PTRACE_SYSCALL failed, "

^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2004-05-28  4:52 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-02-29  4:35 [uml-devel] [PATCH] host context switch reduction Jeff Dike
2004-02-29 16:48 ` BlaisorBlade
2004-03-15 11:42 ` Sven 'Darkman' Michels
2004-03-15 19:13   ` Jeff Dike
2004-05-17 20:08 ` roland
2004-05-17 22:00   ` Laurent Vivier
2004-05-17 22:22     ` roland
2004-05-18 17:59       ` Jeff Dike
2004-05-19 17:27         ` Laurent Vivier
2004-05-19 18:01           ` roland
2004-05-19 18:17             ` Laurent Vivier
2004-05-19 19:39               ` roland
2004-05-19 19:49                 ` Laurent Vivier
2004-05-20  2:30                   ` roland
2004-05-20 12:49                     ` Laurent Vivier
2004-05-23  0:42                       ` [uml-devel] [PATCH] commandline switch for sysemu patch roland
2004-05-23 13:31                       ` [uml-devel] [PATCH] host context switch reduction roland
2004-05-19 23:54               ` Jeff Dike
2004-05-20  1:01               ` roland
2004-05-20 14:12                 ` Henrik Nordstrom
2004-05-20 15:27                   ` roland
2004-05-20 15:35                     ` Henrik Nordstrom
2004-05-19 22:40           ` Nuno Silva
2004-05-20 18:28           ` roland
2004-05-20 18:47             ` Henrik Nordstrom
2004-05-27  6:16           ` Christopher S. Aker
2004-05-27  8:22             ` Laurent Vivier
2004-05-27  9:25               ` Christopher S. Aker
2004-05-27 11:50                 ` Laurent Vivier
2004-05-28  4:53                   ` [uml-devel] [PATCH] host context switch reduction Benchmarks Christopher S. Aker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox