public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] selftests: New very basic kernel selftests directory
@ 2011-12-02 15:41 Frederic Weisbecker
  2011-12-02 15:41 ` [PATCH 2/2] selftests: New x86 breakpoints selftest Frederic Weisbecker
  2012-01-13 18:54 ` [PATCH 1/2] selftests: New very basic kernel selftests directory Fubo Chen
  0 siblings, 2 replies; 7+ messages in thread
From: Frederic Weisbecker @ 2011-12-02 15:41 UTC (permalink / raw)
  To: Andrew Morton; +Cc: LKML, Frederic Weisbecker

Bring a new kernel selftests directory in tools/testing/selftests.
To add a new selftest, create a subdirectory with the sources and
a makefile that creates a target named "run_test" then add the
subdirectory name to the TARGET var in tools/testing/selftests/Makefile
and tools/testing/selftests/run_tests script.

This can help centralizing and maintaining any useful selftest that
developers usually tend to let rust in peace on some random server.

Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
---
 tools/testing/selftests/Makefile  |   11 +++++++++++
 tools/testing/selftests/run_tests |    8 ++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)
 create mode 100644 tools/testing/selftests/Makefile
 create mode 100755 tools/testing/selftests/run_tests

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
new file mode 100644
index 0000000..298a5c0
--- /dev/null
+++ b/tools/testing/selftests/Makefile
@@ -0,0 +1,11 @@
+TARGETS =
+
+all:
+	for TARGET in $(TARGETS); do \
+		make -C $$TARGET; \
+	done;
+
+clean:
+	for TARGET in $(TARGETS); do \
+		make -C $$TARGET clean; \
+	done;
diff --git a/tools/testing/selftests/run_tests b/tools/testing/selftests/run_tests
new file mode 100755
index 0000000..701960d
--- /dev/null
+++ b/tools/testing/selftests/run_tests
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+TARGETS=
+
+for TARGET in $TARGETS
+do
+	$TARGET/run_test
+done
-- 
1.7.5.4


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

* [PATCH 2/2] selftests: New x86 breakpoints selftest
  2011-12-02 15:41 [PATCH 1/2] selftests: New very basic kernel selftests directory Frederic Weisbecker
@ 2011-12-02 15:41 ` Frederic Weisbecker
  2011-12-07 23:32   ` Andrew Morton
  2011-12-12  6:06   ` K.Prasad
  2012-01-13 18:54 ` [PATCH 1/2] selftests: New very basic kernel selftests directory Fubo Chen
  1 sibling, 2 replies; 7+ messages in thread
From: Frederic Weisbecker @ 2011-12-02 15:41 UTC (permalink / raw)
  To: Andrew Morton
  Cc: LKML, Frederic Weisbecker, Thomas Gleixner, Ingo Molnar,
	H. Peter Anvin, Jason Wessel, Will Deacon

Bring a first selftest in the relevant directory. This tests
several combinations of breakpoints and watchpoints in x86, as
well as icebp traps and int3 traps. Given the amount of breakpoint
regressions we raised after we merged the generic breakpoint
infrastructure, such selftest became necessary and can still serve
today as a basis for new patches that touch the do_debug() path.

Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Jason Wessel <jason.wessel@windriver.com>
Cc: Will Deacon <will.deacon@arm.com>
---
 tools/testing/selftests/Makefile                   |    2 +-
 tools/testing/selftests/breakpoints/Makefile       |   20 +
 .../selftests/breakpoints/breakpoint_test.c        |  394 ++++++++++++++++++++
 tools/testing/selftests/run_tests                  |    2 +-
 4 files changed, 416 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/breakpoints/Makefile
 create mode 100644 tools/testing/selftests/breakpoints/breakpoint_test.c

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 298a5c0..4ec8401 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -1,4 +1,4 @@
-TARGETS =
+TARGETS = breakpoints
 
 all:
 	for TARGET in $(TARGETS); do \
diff --git a/tools/testing/selftests/breakpoints/Makefile b/tools/testing/selftests/breakpoints/Makefile
new file mode 100644
index 0000000..f362722
--- /dev/null
+++ b/tools/testing/selftests/breakpoints/Makefile
@@ -0,0 +1,20 @@
+# Taken from perf makefile
+uname_M := $(shell uname -m 2>/dev/null || echo not)
+ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/i386/)
+ifeq ($(ARCH),i386)
+        ARCH := x86
+endif
+ifeq ($(ARCH),x86_64)
+	ARCH := x86
+endif
+
+
+all:
+ifeq ($(ARCH),x86)
+	gcc breakpoint_test.c -o run_test
+else
+	echo "Not an x86 target, can't build breakpoints selftests"
+endif
+
+clean:
+	rm -fr run_test
diff --git a/tools/testing/selftests/breakpoints/breakpoint_test.c b/tools/testing/selftests/breakpoints/breakpoint_test.c
new file mode 100644
index 0000000..a0743f3
--- /dev/null
+++ b/tools/testing/selftests/breakpoints/breakpoint_test.c
@@ -0,0 +1,394 @@
+/*
+ * Copyright (C) 2011 Red Hat, Inc., Frederic Weisbecker <fweisbec@redhat.com>
+ *
+ * Licensed under the terms of the GNU GPL License version 2
+ *
+ * Selftests for breakpoints (and more generally the do_debug() path) in x86.
+ */
+
+
+#include <sys/ptrace.h>
+#include <unistd.h>
+#include <stddef.h>
+#include <sys/user.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+
+/* Breakpoint access modes */
+enum {
+	BP_X = 1,
+	BP_RW = 2,
+	BP_W = 4,
+};
+
+static pid_t child_pid;
+
+/*
+ * Ensures the child and parent are always "talking" about
+ * the same test sequence. (ie: that we haven't forgotten
+ * to call check_trapped() somewhere).
+ */
+static int nr_tests;
+
+static void set_breakpoint_addr(void *addr, int n)
+{
+	int ret;
+
+	ret = ptrace(PTRACE_POKEUSER, child_pid,
+		     offsetof(struct user, u_debugreg[n]), addr);
+	if (ret) {
+		perror("Can't set breakpoint addr\n");
+		exit(-1);
+	}
+}
+
+static void toggle_breakpoint(int n, int type, int len,
+			      int local, int global, int set)
+{
+	int ret;
+
+	int xtype, xlen;
+	unsigned long vdr7, dr7;
+
+	switch (type) {
+	case BP_X:
+		xtype = 0;
+		break;
+	case BP_W:
+		xtype = 1;
+		break;
+	case BP_RW:
+		xtype = 3;
+		break;
+	}
+
+	switch (len) {
+	case 1:
+		xlen = 0;
+		break;
+	case 2:
+		xlen = 4;
+		break;
+	case 4:
+		xlen = 0xc;
+		break;
+	case 8:
+		xlen = 8;
+		break;
+	}
+
+	dr7 = ptrace(PTRACE_PEEKUSER, child_pid,
+		     offsetof(struct user, u_debugreg[7]), 0);
+
+	vdr7 = (xlen | xtype) << 16;
+	vdr7 <<= 4 * n;
+
+	if (local) {
+		vdr7 |= 1 << (2 * n);
+		vdr7 |= 1 << 8;
+	}
+	if (global) {
+		vdr7 |= 2 << (2 * n);
+		vdr7 |= 1 << 9;
+	}
+
+	if (set)
+		dr7 |= vdr7;
+	else
+		dr7 &= ~vdr7;
+
+	ret = ptrace(PTRACE_POKEUSER, child_pid,
+		     offsetof(struct user, u_debugreg[7]), dr7);
+	if (ret) {
+		perror("Can't set dr7");
+		exit(-1);
+	}
+}
+
+/* Dummy variables to test read/write accesses */
+static unsigned long long dummy_var[4];
+
+/* Dummy functions to test execution accesses */
+static void dummy_func(void) { }
+static void dummy_func1(void) { }
+static void dummy_func2(void) { }
+static void dummy_func3(void) { }
+
+static void (*dummy_funcs[])(void) = {
+	dummy_func,
+	dummy_func1,
+	dummy_func2,
+	dummy_func3,
+};
+
+static int trapped;
+
+static void check_trapped(void)
+{
+	/*
+	 * If we haven't trapped, wake up the parent
+	 * so that it notices the failure.
+	 */
+	if (!trapped)
+		kill(getpid(), SIGUSR1);
+	trapped = 0;
+
+	nr_tests++;
+}
+
+static void write_var(int len)
+{
+	char *pcval; short *psval; int *pival; long long *plval;
+	int i;
+
+	for (i = 0; i < 4; i++) {
+		switch (len) {
+		case 1:
+			pcval = (char *)&dummy_var[i];
+			*pcval = 0xff;
+			break;
+		case 2:
+			psval = (short *)&dummy_var[i];
+			*psval = 0xffff;
+			break;
+		case 4:
+			pival = (int *)&dummy_var[i];
+			*pival = 0xffffffff;
+			break;
+		case 8:
+			plval = (long long *)&dummy_var[i];
+			*plval = 0xffffffffffffffffLL;
+			break;
+		}
+		check_trapped();
+	}
+}
+
+static void read_var(int len)
+{
+	char cval; short sval; int ival; long long lval;
+	int i;
+
+	for (i = 0; i < 4; i++) {
+		switch (len) {
+		case 1:
+			cval = *(char *)&dummy_var[i];
+			break;
+		case 2:
+			sval = *(short *)&dummy_var[i];
+			break;
+		case 4:
+			ival = *(int *)&dummy_var[i];
+			break;
+		case 8:
+			lval = *(long long *)&dummy_var[i];
+			break;
+		}
+		check_trapped();
+	}
+}
+
+/*
+ * Do the r/w/x accesses to trigger the breakpoints. And run
+ * the usual traps.
+ */
+static void trigger_tests(void)
+{
+	int len, local, global, i;
+	char val;
+	int ret;
+
+	ret = ptrace(PTRACE_TRACEME, 0, NULL, 0);
+	if (ret) {
+		perror("Can't be traced?\n");
+		return;
+	}
+
+	/* Wake up father so that it sets up the first test */
+	kill(getpid(), SIGUSR1);
+
+	/* Test instruction breakpoints */
+	for (local = 0; local < 2; local++) {
+		for (global = 0; global < 2; global++) {
+			if (!local && !global)
+				continue;
+
+			for (i = 0; i < 4; i++) {
+				dummy_funcs[i]();
+				check_trapped();
+			}
+		}
+	}
+
+	/* Test write watchpoints */
+	for (len = 1; len <= sizeof(long); len <<= 1) {
+		for (local = 0; local < 2; local++) {
+			for (global = 0; global < 2; global++) {
+				if (!local && !global)
+					continue;
+				write_var(len);
+			}
+		}
+	}
+
+	/* Test read/write watchpoints (on read accesses) */
+	for (len = 1; len <= sizeof(long); len <<= 1) {
+		for (local = 0; local < 2; local++) {
+			for (global = 0; global < 2; global++) {
+				if (!local && !global)
+					continue;
+				read_var(len);
+			}
+		}
+	}
+
+	/* Icebp trap */
+	asm(".byte 0xf1\n");
+	check_trapped();
+
+	/* Int 3 trap */
+	asm("int $3\n");
+	check_trapped();
+
+	kill(getpid(), SIGUSR1);
+}
+
+static void check_success(const char *msg)
+{
+	const char *msg2;
+	int child_nr_tests;
+	int status;
+
+	/* Wait for the child to SIGTRAP */
+	wait(&status);
+
+	msg2 = "Failed";
+
+	if (WSTOPSIG(status) == SIGTRAP) {
+		child_nr_tests = ptrace(PTRACE_PEEKDATA, child_pid,
+					&nr_tests, 0);
+		if (child_nr_tests == nr_tests)
+			msg2 = "Ok";
+		if (ptrace(PTRACE_POKEDATA, child_pid, &trapped, 1)) {
+			perror("Can't poke\n");
+			exit(-1);
+		}
+	}
+
+	nr_tests++;
+
+	printf("%s [%s]\n", msg, msg2);
+}
+
+static void launch_instruction_breakpoints(char *buf, int local, int global)
+{
+	int i;
+
+	for (i = 0; i < 4; i++) {
+		set_breakpoint_addr(dummy_funcs[i], i);
+		toggle_breakpoint(i, BP_X, 1, local, global, 1);
+		ptrace(PTRACE_CONT, child_pid, NULL, 0);
+		sprintf(buf, "Test breakpoint %d with local: %d global: %d",
+			i, local, global);
+		check_success(buf);
+		toggle_breakpoint(i, BP_X, 1, local, global, 0);
+	}
+}
+
+static void launch_watchpoints(char *buf, int mode, int len,
+			       int local, int global)
+{
+	const char *mode_str;
+	int i;
+
+	if (mode == BP_W)
+		mode_str = "write";
+	else
+		mode_str = "read";
+
+	for (i = 0; i < 4; i++) {
+		set_breakpoint_addr(&dummy_var[i], i);
+		toggle_breakpoint(i, mode, len, local, global, 1);
+		ptrace(PTRACE_CONT, child_pid, NULL, 0);
+		sprintf(buf, "Test %s watchpoint %d with len: %d local: "
+			"%d global: %d", mode_str, i, len, local, global);
+		check_success(buf);
+		toggle_breakpoint(i, mode, len, local, global, 0);
+	}
+}
+
+/* Set the breakpoints and check the child successfully trigger them */
+static void launch_tests(void)
+{
+	char buf[1024];
+	int len, local, global, i;
+
+	/* Instruction breakpoints */
+	for (local = 0; local < 2; local++) {
+		for (global = 0; global < 2; global++) {
+			if (!local && !global)
+				continue;
+			launch_instruction_breakpoints(buf, local, global);
+		}
+	}
+
+	/* Write watchpoint */
+	for (len = 1; len <= sizeof(long); len <<= 1) {
+		for (local = 0; local < 2; local++) {
+			for (global = 0; global < 2; global++) {
+				if (!local && !global)
+					continue;
+				launch_watchpoints(buf, BP_W, len,
+						   local, global);
+			}
+		}
+	}
+
+	/* Read-Write watchpoint */
+	for (len = 1; len <= sizeof(long); len <<= 1) {
+		for (local = 0; local < 2; local++) {
+			for (global = 0; global < 2; global++) {
+				if (!local && !global)
+					continue;
+				launch_watchpoints(buf, BP_RW, len,
+						   local, global);
+			}
+		}
+	}
+
+	/* Icebp traps */
+	ptrace(PTRACE_CONT, child_pid, NULL, 0);
+	check_success("Test icebp");
+
+	/* Int 3 traps */
+	ptrace(PTRACE_CONT, child_pid, NULL, 0);
+	check_success("Test int 3 trap");
+
+	ptrace(PTRACE_CONT, child_pid, NULL, 0);
+}
+
+int main(int argc, char **argv)
+{
+	pid_t pid;
+	int ret;
+
+	pid = fork();
+	if (!pid) {
+		trigger_tests();
+		return 0;
+	}
+
+	child_pid = pid;
+
+	wait(NULL);
+
+	launch_tests();
+
+	wait(NULL);
+
+	return 0;
+}
diff --git a/tools/testing/selftests/run_tests b/tools/testing/selftests/run_tests
index 701960d..320718a 100755
--- a/tools/testing/selftests/run_tests
+++ b/tools/testing/selftests/run_tests
@@ -1,6 +1,6 @@
 #!/bin/bash
 
-TARGETS=
+TARGETS=breakpoints
 
 for TARGET in $TARGETS
 do
-- 
1.7.5.4


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

* Re: [PATCH 2/2] selftests: New x86 breakpoints selftest
  2011-12-02 15:41 ` [PATCH 2/2] selftests: New x86 breakpoints selftest Frederic Weisbecker
@ 2011-12-07 23:32   ` Andrew Morton
  2011-12-08  0:11     ` Frederic Weisbecker
  2011-12-12  6:06   ` K.Prasad
  1 sibling, 1 reply; 7+ messages in thread
From: Andrew Morton @ 2011-12-07 23:32 UTC (permalink / raw)
  To: Frederic Weisbecker
  Cc: LKML, Thomas Gleixner, Ingo Molnar, H. Peter Anvin, Jason Wessel,
	Will Deacon

On Fri,  2 Dec 2011 16:41:15 +0100
Frederic Weisbecker <fweisbec@gmail.com> wrote:

> Bring a first selftest in the relevant directory.

That all looks nice and simple, thanks.  Unless I get suitably shouted
at I think I'll send all this Linuswards.  Then I can hassle people to
add their little test snippets as they add userspace-visible features.

I don't think we'd ever want to turn this into some huge kernel
verification suite.  My thinking here is that I frequently see that
people have written little test cases for their new feature, but those
test cases just die after the feature is merged.  It would be better to
maintain and grow these tests as the relevant features are augmented or
bugfixed.

All these features are Linux-specific.  Standard interface features (eg
POSIX) are and should be tested via other externally-maintained test
suites.

If the whole idea ends up not working out, we can just delete it all.

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

* Re: [PATCH 2/2] selftests: New x86 breakpoints selftest
  2011-12-07 23:32   ` Andrew Morton
@ 2011-12-08  0:11     ` Frederic Weisbecker
  0 siblings, 0 replies; 7+ messages in thread
From: Frederic Weisbecker @ 2011-12-08  0:11 UTC (permalink / raw)
  To: Andrew Morton
  Cc: LKML, Thomas Gleixner, Ingo Molnar, H. Peter Anvin, Jason Wessel,
	Will Deacon

On Wed, Dec 07, 2011 at 03:32:23PM -0800, Andrew Morton wrote:
> On Fri,  2 Dec 2011 16:41:15 +0100
> Frederic Weisbecker <fweisbec@gmail.com> wrote:
> 
> > Bring a first selftest in the relevant directory.
> 
> That all looks nice and simple, thanks.  Unless I get suitably shouted
> at I think I'll send all this Linuswards.  Then I can hassle people to
> add their little test snippets as they add userspace-visible features.
> 
> I don't think we'd ever want to turn this into some huge kernel
> verification suite.  My thinking here is that I frequently see that
> people have written little test cases for their new feature, but those
> test cases just die after the feature is merged.  It would be better to
> maintain and grow these tests as the relevant features are augmented or
> bugfixed.

Exactly. And I also think this is no good place for background long running
stress-tests but rather for correctness tests (Unless we find situations
where short stress-tests are enough to trigger correctness problems).
That's really targeted to spot ABI breakages or alike.

My selftest for the cgroup task counter subsystem is also a good candidate for
that (if that subsystem ever get merged but that's a separate debate ;)

> 
> All these features are Linux-specific.  Standard interface features (eg
> POSIX) are and should be tested via other externally-maintained test
> suites.
> 
> If the whole idea ends up not working out, we can just delete it all.

Agreed, let the selftest subsystem selftest itself for a while and we'll figure
out.

Thanks.

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

* Re: [PATCH 2/2] selftests: New x86 breakpoints selftest
  2011-12-02 15:41 ` [PATCH 2/2] selftests: New x86 breakpoints selftest Frederic Weisbecker
  2011-12-07 23:32   ` Andrew Morton
@ 2011-12-12  6:06   ` K.Prasad
  1 sibling, 0 replies; 7+ messages in thread
From: K.Prasad @ 2011-12-12  6:06 UTC (permalink / raw)
  To: Frederic Weisbecker
  Cc: Andrew Morton, LKML, Thomas Gleixner, Ingo Molnar, H. Peter Anvin,
	Jason Wessel, Will Deacon

On Fri, Dec 02, 2011 at 04:41:15PM +0100, Frederic Weisbecker wrote:
> Bring a first selftest in the relevant directory. This tests
> several combinations of breakpoints and watchpoints in x86, as
> well as icebp traps and int3 traps. Given the amount of breakpoint
> regressions we raised after we merged the generic breakpoint
> infrastructure, such selftest became necessary and can still serve
> today as a basis for new patches that touch the do_debug() path.
> 


Hi Frederic,
	Can we also including testing of watchpoints over kernel variables?
We could do this using the perf syscall or (in a convoluted way) use a
kernel module that works in tandem with this user-space testcase.

Thanks,
K.Prasad


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

* Re: [PATCH 1/2] selftests: New very basic kernel selftests directory
  2011-12-02 15:41 [PATCH 1/2] selftests: New very basic kernel selftests directory Frederic Weisbecker
  2011-12-02 15:41 ` [PATCH 2/2] selftests: New x86 breakpoints selftest Frederic Weisbecker
@ 2012-01-13 18:54 ` Fubo Chen
  2012-01-17  1:49   ` Frederic Weisbecker
  1 sibling, 1 reply; 7+ messages in thread
From: Fubo Chen @ 2012-01-13 18:54 UTC (permalink / raw)
  To: Frederic Weisbecker; +Cc: Andrew Morton, LKML

On Fri, Dec 2, 2011 at 3:41 PM, Frederic Weisbecker <fweisbec@gmail.com> wrote:
> Bring a new kernel selftests directory in tools/testing/selftests.

Interesting ... but why to invent a test framework from scratch while
there already exists stuff like googletest or TAP::Harness ?

Fubo.

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

* Re: [PATCH 1/2] selftests: New very basic kernel selftests directory
  2012-01-13 18:54 ` [PATCH 1/2] selftests: New very basic kernel selftests directory Fubo Chen
@ 2012-01-17  1:49   ` Frederic Weisbecker
  0 siblings, 0 replies; 7+ messages in thread
From: Frederic Weisbecker @ 2012-01-17  1:49 UTC (permalink / raw)
  To: Fubo Chen; +Cc: Andrew Morton, LKML

On Fri, Jan 13, 2012 at 06:54:27PM +0000, Fubo Chen wrote:
> On Fri, Dec 2, 2011 at 3:41 PM, Frederic Weisbecker <fweisbec@gmail.com> wrote:
> > Bring a new kernel selftests directory in tools/testing/selftests.
> 
> Interesting ... but why to invent a test framework from scratch while
> there already exists stuff like googletest or TAP::Harness ?

We wanted to start with something very simple that just launch
selftests in batch.

Let's see first where this goes, if this becomes a useful subsystem that
grows up, then if it makes sense to rely on some more elaborated selftest
framework we can think about it.

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

end of thread, other threads:[~2012-01-17  1:49 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-02 15:41 [PATCH 1/2] selftests: New very basic kernel selftests directory Frederic Weisbecker
2011-12-02 15:41 ` [PATCH 2/2] selftests: New x86 breakpoints selftest Frederic Weisbecker
2011-12-07 23:32   ` Andrew Morton
2011-12-08  0:11     ` Frederic Weisbecker
2011-12-12  6:06   ` K.Prasad
2012-01-13 18:54 ` [PATCH 1/2] selftests: New very basic kernel selftests directory Fubo Chen
2012-01-17  1:49   ` Frederic Weisbecker

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