Skip to contentSkip to conversation

ptrace_multi: speedup for virtual machines (and debuggers) running on ptrace

PATCH In review Renzo Davoli 1 participant 1 message Jun 16, 2008 User-mode Linux (UML)

lore
Share & tools
lore
#0 lore raw

PATCH 2/2 ptrace_multi: speedup for virtual machines (and debuggers) running on ptrace

Warning:
This is a proof-of-concept. It shows the effectiveness of PTRACE_MULTI (PATCH 1/2). It is incomplete, not to be included at this stage.

This patch adds some PTRACE_MULTI optimizations to User-Mode Linux client code. With these optimization UML decreases the overall number of syscalls (around 15% in my preliminary tests) and shows some speedup (5%).

I expect better figures when all the UML client code will take advantage of PTRACE_MULTI and on other architectures (e.g. on powerpc, still unsupported, where tens of ptrace in a row are needed to get/set registers).

This patch is against 2.6.26-rc6 (or git2) + ptrace_multi + ptrace_vm

renzo

Signed-off-by Renzo Davoli

---

arch/um/include/ptrace_user.h +15 -0
diff -Naur linux-2.6.26-rc6-ptrace_multi-ptrace_vm/arch/um/include/ptrace_user.h linux-2.6.26-rc6-ptrace_multi-ptrace_vm-umlmulti/arch/um/include/ptrace_user.h
@@ -20,6 +20,18 @@
 #define PTRACE_SYSEMU_SINGLESTEP 32
 #endif
 
+#define PTRACE_MULTI            0x4300
+#define PTRACE_PEEKCHARDATA     0x4301
+#define PTRACE_POKECHARDATA     0x4302
+#define PTRACE_PEEKSTRINGDATA   0x4303
+
+struct ptrace_multi {
+	long request;
+	long addr;
+	void *localaddr;
+	long length;
+};
+
 /* On architectures, that started to support PTRACE_O_TRACESYSGOOD
  * in linux 2.4, there are two different definitions of
  * PTRACE_SETOPTIONS: linux 2.4 uses 21 while linux 2.6 uses 0x4200.
@@ -54,6 +66,9 @@
 void set_using_sysptvm(int value);
 int get_using_sysptvm(void);
 extern int sysptvm_supported;
+void set_using_sysptmulti(int value);
+int get_using_sysptmulti(void);
+extern int sysptmulti_supported;
 
 #define SELECT_PTRACE_OPERATION(sysemu_mode, singlestep_mode) \
 	(((int[3][3] ) { \
arch/um/kernel/process.c +46 -0

72 lines not on this page · raw message

arch/um/os-Linux/skas/mem.c +25 -12
diff -Naur linux-2.6.26-rc6-ptrace_multi-ptrace_vm/arch/um/os-Linux/skas/mem.c linux-2.6.26-rc6-ptrace_multi-ptrace_vm-umlmulti/arch/um/os-Linux/skas/mem.c
@@ -69,19 +69,32 @@
 
 	multi_count++;
 
-	n = ptrace_setregs(pid, syscall_regs);
-	if (n < 0) {
-		printk(UM_KERN_ERR "Registers - \n");
-		for (i = 0; i < MAX_REG_NR; i++)
-			printk(UM_KERN_ERR "\t%d\t0x%lx\n", i, syscall_regs[i]);
-		panic("do_syscall_stub : PTRACE_SETREGS failed, errno = %d\n",
-		      -n);
-	}
+	if (get_using_sysptmulti()) {
+		struct ptrace_multi req[] = {
+			{PTRACE_SETREGS, 0, syscall_regs, 0},
+			{PTRACE_CONT, 0, 0, 0}};
+		err=ptrace(PTRACE_MULTI,pid,req,2);
+		if (err<0) {
+			err = -errno;
+			printk(UM_KERN_ERR "do_syscall_stub: failed ptrace_multi "
+					"failed, pid = %d, errno = %d\n", pid, -err);
+			return err;
+		}
+	} else {
+		n = ptrace_setregs(pid, syscall_regs);
+		if (n < 0) {
+			printk(UM_KERN_ERR "Registers - \n");
+			for (i = 0; i < MAX_REG_NR; i++)
+				printk(UM_KERN_ERR "\t%d\t0x%lx\n", i, syscall_regs[i]);
+			panic("do_syscall_stub : PTRACE_SETREGS failed, errno = %d\n",
+					-n);
+		}
 
-	err = ptrace(PTRACE_CONT, pid, 0, 0);
-	if (err)
-		panic("Failed to continue stub, pid = %d, errno = %d\n", pid,
-		      errno);
+		err = ptrace(PTRACE_CONT, pid, 0, 0);
+		if (err)
+			panic("Failed to continue stub, pid = %d, errno = %d\n", pid,
+					errno);
+	}
 
 	wait_stub_done(pid);
 
arch/um/os-Linux/skas/process.c +62 -36

136 lines not on this page · raw message

arch/um/os-Linux/start_up.c +35 -5
diff -Naur linux-2.6.26-rc6-ptrace_multi-ptrace_vm/arch/um/os-Linux/start_up.c linux-2.6.26-rc6-ptrace_multi-ptrace_vm-umlmulti/arch/um/os-Linux/start_up.c
@@ -225,6 +225,21 @@
 "    Use sysemu instead of sysptvm even when the kernel supports it.\n\n"
 );
 
+static int force_sysptmulti_disabled = 0;
+
+static int __init nosysptmulti_cmd_param(char *str, int* add)
+{
+	  force_sysptmulti_disabled = 1;
+		  return 0;
+}
+
+__uml_setup("nosysptmulti", nosysptmulti_cmd_param,
+		"nosysptmulti\n"
+		"    Turns off syscall multi for ptrace (ptrace_vm) on.\n"
+		"    Ptrace_vm is a feature introduced by Renzo Davoli: several\n"
+		"    ptrace() requests can be evaluated using one system call.\n"
+		"\n");
+
 static void __init check_sysemu(void)
 {
 	unsigned long regs[MAX_REG_NR];
@@ -344,6 +359,7 @@
 	return 0;
 }
 
+#define PTRACE_SUPPORTS_PTMULTI 0x80000000
 /* kernel feature test: 
  * it returns:
  *   -1 error 
@@ -351,7 +367,7 @@
  *   PTRACE_SYSCALL_SKIPEXIT: just skip_exit is provided
  *   PTRACE_SYSCALL_SKIPCALL: the entire syntax is implemented
  *   by the running kernel */
-static int __init test_ptrace_sysptvm(void) {
+static int __init test_ptrace_sysptvm_and_ptmulti(void) {
 	int pid, status, rv, feature;
 	static char stack[1024];
 	feature=0;
@@ -383,6 +399,8 @@
 	if(waitpid(pid, &status, WUNTRACED) < 0)
 		return 0;
 out:
+	if (ptrace(PTRACE_MULTI, pid, stack, 0) >= 0)
+		feature |= PTRACE_SUPPORTS_PTMULTI;
 	ptrace(PTRACE_KILL,pid,0,0);
 	/* eliminate zombie */
 	if(waitpid(pid, &status, WUNTRACED) < 0)
@@ -392,10 +410,11 @@
 
 static int  __init check_sysptvm(void)
 {
-	int feature=test_ptrace_sysptvm();
+	int feature=test_ptrace_sysptvm_and_ptmulti();
+	int rv=0;
   
 	non_fatal("Checking ptrace new tags for syscall emulation...");
-	if (feature==PTRACE_SYSCALL_SKIPCALL) {
+	if ((feature & ~PTRACE_SUPPORTS_PTMULTI)==PTRACE_SYSCALL_SKIPCALL) {
 		sysptvm_supported=1;
 		non_fatal("OK");
 		if (!force_sysptvm_disabled) 
@@ -403,10 +422,21 @@
 		else
 			non_fatal(" (disabled)");
 		non_fatal("\n");
-		return 1;
+		rv=1;
 	} else
 		non_fatal("unsupported\n");
-	return 0;
+	non_fatal("Checking ptrace multi speedup...");
+	if (feature & PTRACE_SUPPORTS_PTMULTI) {
+		sysptmulti_supported=1;
+		non_fatal("OK");
+		if (force_sysptmulti_disabled)
+			non_fatal(" (disabled)");
+		else
+			set_using_sysptmulti(1);
+		non_fatal("\n");
+	} else
+		non_fatal("unsupported\n");
+	return rv;
 }
 
 static void __init check_ptrace(void)