From 9d22f6e14e249713108c8adcc8705c39303f0628 Mon Sep 17 00:00:00 2001 From: Shuah Khan Date: Fri, 12 Feb 2016 17:43:31 -0700 Subject: selftests: add a new test for Media Controller API This test opens user specified Media Device and calls MEDIA_IOC_DEVICE_INFO ioctl in a loop once every 10 seconds. This test is for detecting errors in device removal path. Usage: sudo ./media_device_test -d /dev/mediaX While test is running, remove the device and ensure there are no use after free errors and other Oops in the dmesg. Enable KaSan kernel config option for use-after-free error detection. Signed-off-by: Shuah Khan --- tools/testing/selftests/media_tests/.gitignore | 1 + tools/testing/selftests/media_tests/Makefile | 7 ++ .../selftests/media_tests/media_device_test.c | 94 ++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 tools/testing/selftests/media_tests/.gitignore create mode 100644 tools/testing/selftests/media_tests/Makefile create mode 100644 tools/testing/selftests/media_tests/media_device_test.c diff --git a/tools/testing/selftests/media_tests/.gitignore b/tools/testing/selftests/media_tests/.gitignore new file mode 100644 index 000000000000..1c0711708b98 --- /dev/null +++ b/tools/testing/selftests/media_tests/.gitignore @@ -0,0 +1 @@ +media_device_test diff --git a/tools/testing/selftests/media_tests/Makefile b/tools/testing/selftests/media_tests/Makefile new file mode 100644 index 000000000000..7071bcc1d066 --- /dev/null +++ b/tools/testing/selftests/media_tests/Makefile @@ -0,0 +1,7 @@ +TEST_PROGS := media_device_test +all: $(TEST_PROGS) + +include ../lib.mk + +clean: + rm -fr media_device_test diff --git a/tools/testing/selftests/media_tests/media_device_test.c b/tools/testing/selftests/media_tests/media_device_test.c new file mode 100644 index 000000000000..a47880b9e00a --- /dev/null +++ b/tools/testing/selftests/media_tests/media_device_test.c @@ -0,0 +1,94 @@ +/* + * media_devkref_test.c - Media Controller Device Kref API Test + * + * Copyright (c) 2016 Shuah Khan + * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * + * This file is released under the GPLv2. + */ + +/* + * This file adds a test for Media Controller API. + * This test should be run as root and should not be + * included in the Kselftest run. This test should be + * run when hardware and driver that makes use Media + * Controller API are present in the system. + * + * This test opens user specified Media Device and calls + * MEDIA_IOC_DEVICE_INFO ioctl in a loop once every 10 + * seconds. + * + * Usage: + * sudo ./media_devkref_test -d /dev/mediaX + * + * While test is running, remove the device and + * ensure there are no use after free errors and + * other Oops in the dmesg. Enable KaSan kernel + * config option for use-after-free error detection. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int opt; + char media_device[256]; + int count = 0; + struct media_device_info mdi; + int ret; + int fd; + + if (argc < 2) { + printf("Usage: %s [-d ]\n", argv[0]); + exit(-1); + } + + /* Process arguments */ + while ((opt = getopt(argc, argv, "d:")) != -1) { + switch (opt) { + case 'd': + strncpy(media_device, optarg, sizeof(media_device) - 1); + media_device[sizeof(media_device)-1] = '\0'; + break; + default: + printf("Usage: %s [-d ]\n", argv[0]); + exit(-1); + } + } + + if (getuid() != 0) { + printf("Please run the test as root - Exiting.\n"); + exit(-1); + } + + /* Open Media device and keep it open */ + fd = open(media_device, O_RDWR); + if (fd == -1) { + printf("Media Device open errno %s\n", strerror(errno)); + exit(-1); + } + + printf("\nNote:\n" + "While test is running, remove the device and\n" + "ensure there are no use after free errors and\n" + "other Oops in the dmesg. Enable KaSan kernel\n" + "config option for use-after-free error detection.\n\n"); + + while (count < 100) { + ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi); + if (ret < 0) + printf("Media Device Info errno %s\n", strerror(errno)); + printf("Media device model %s driver %s\n", + mdi.model, mdi.driver); + sleep(10); + count++; + } +} -- cgit v1.2.3 From bfd092b8c272866ba1db3e63ebbe08f8ccf979cc Mon Sep 17 00:00:00 2001 From: Greg Hackmann Date: Mon, 22 Feb 2016 10:10:57 -0800 Subject: selftests: breakpoint: add step_after_suspend_test Commit e56d82a11617 ("arm64: cpu hotplug: ensure we mask out CPU_TASKS_FROZEN in notifiers") fixed a long-standing ARM64 bug that broke single-stepping after a suspend/resume cycle. Add a kernel selftest to make sure this doesn't regress or affect other platforms. Signed-off-by: Greg Hackmann Signed-off-by: Shuah Khan --- tools/testing/selftests/breakpoints/Makefile | 4 +- .../breakpoints/step_after_suspend_test.c | 218 +++++++++++++++++++++ 2 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/breakpoints/step_after_suspend_test.c diff --git a/tools/testing/selftests/breakpoints/Makefile b/tools/testing/selftests/breakpoints/Makefile index c0d957015f52..74e533fd4bc5 100644 --- a/tools/testing/selftests/breakpoints/Makefile +++ b/tools/testing/selftests/breakpoints/Makefile @@ -6,9 +6,11 @@ ifeq ($(ARCH),x86) TEST_PROGS := breakpoint_test endif +TEST_PROGS += step_after_suspend_test + all: $(TEST_PROGS) include ../lib.mk clean: - rm -fr breakpoint_test + rm -fr breakpoint_test step_after_suspend_test diff --git a/tools/testing/selftests/breakpoints/step_after_suspend_test.c b/tools/testing/selftests/breakpoints/step_after_suspend_test.c new file mode 100644 index 000000000000..60b8a95dac26 --- /dev/null +++ b/tools/testing/selftests/breakpoints/step_after_suspend_test.c @@ -0,0 +1,218 @@ +/* + * Copyright (C) 2016 Google, Inc. + * + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" + +void child(int cpu) +{ + cpu_set_t set; + + CPU_ZERO(&set); + CPU_SET(cpu, &set); + if (sched_setaffinity(0, sizeof(set), &set) != 0) { + perror("sched_setaffinity() failed"); + _exit(1); + } + + if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0) { + perror("ptrace(PTRACE_TRACEME) failed"); + _exit(1); + } + + if (raise(SIGSTOP) != 0) { + perror("raise(SIGSTOP) failed"); + _exit(1); + } + + _exit(0); +} + +bool run_test(int cpu) +{ + int status; + pid_t pid = fork(); + pid_t wpid; + + if (pid < 0) { + perror("fork() failed"); + return false; + } + if (pid == 0) + child(cpu); + + wpid = waitpid(pid, &status, __WALL); + if (wpid != pid) { + perror("waitpid() failed"); + return false; + } + if (!WIFSTOPPED(status)) { + printf("child did not stop\n"); + return false; + } + if (WSTOPSIG(status) != SIGSTOP) { + printf("child did not stop with SIGSTOP\n"); + return false; + } + + if (ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL) < 0) { + if (errno == EIO) { + printf("ptrace(PTRACE_SINGLESTEP) not supported on this architecture\n"); + ksft_exit_skip(); + } + perror("ptrace(PTRACE_SINGLESTEP) failed"); + return false; + } + + wpid = waitpid(pid, &status, __WALL); + if (wpid != pid) { + perror("waitpid() failed"); + return false; + } + if (WIFEXITED(status)) { + printf("child did not single-step\n"); + return false; + } + if (!WIFSTOPPED(status)) { + printf("child did not stop\n"); + return false; + } + if (WSTOPSIG(status) != SIGTRAP) { + printf("child did not stop with SIGTRAP\n"); + return false; + } + + if (ptrace(PTRACE_CONT, pid, NULL, NULL) < 0) { + perror("ptrace(PTRACE_CONT) failed"); + return false; + } + + wpid = waitpid(pid, &status, __WALL); + if (wpid != pid) { + perror("waitpid() failed"); + return false; + } + if (!WIFEXITED(status)) { + printf("child did not exit after PTRACE_CONT\n"); + return false; + } + + return true; +} + +void suspend(void) +{ + int power_state_fd; + struct sigevent event = {}; + int timerfd; + int err; + struct itimerspec spec = {}; + + power_state_fd = open("/sys/power/state", O_RDWR); + if (power_state_fd < 0) { + perror("open(\"/sys/power/state\") failed (is this test running as root?)"); + ksft_exit_fail(); + } + + timerfd = timerfd_create(CLOCK_BOOTTIME_ALARM, 0); + if (timerfd < 0) { + perror("timerfd_create() failed"); + ksft_exit_fail(); + } + + spec.it_value.tv_sec = 5; + err = timerfd_settime(timerfd, 0, &spec, NULL); + if (err < 0) { + perror("timerfd_settime() failed"); + ksft_exit_fail(); + } + + if (write(power_state_fd, "mem", strlen("mem")) != strlen("mem")) { + perror("entering suspend failed"); + ksft_exit_fail(); + } + + close(timerfd); + close(power_state_fd); +} + +int main(int argc, char **argv) +{ + int opt; + bool do_suspend = true; + bool succeeded = true; + cpu_set_t available_cpus; + int err; + int cpu; + + while ((opt = getopt(argc, argv, "n")) != -1) { + switch (opt) { + case 'n': + do_suspend = false; + break; + default: + printf("Usage: %s [-n]\n", argv[0]); + printf(" -n: do not trigger a suspend/resume cycle before the test\n"); + return -1; + } + } + + if (do_suspend) + suspend(); + + err = sched_getaffinity(0, sizeof(available_cpus), &available_cpus); + if (err < 0) { + perror("sched_getaffinity() failed"); + ksft_exit_fail(); + } + + for (cpu = 0; cpu < CPU_SETSIZE; cpu++) { + bool test_success; + + if (!CPU_ISSET(cpu, &available_cpus)) + continue; + + test_success = run_test(cpu); + printf("CPU %d: ", cpu); + if (test_success) { + printf("[OK]\n"); + ksft_inc_pass_cnt(); + } else { + printf("[FAILED]\n"); + ksft_inc_fail_cnt(); + succeeded = false; + } + } + + ksft_print_cnts(); + if (succeeded) + ksft_exit_pass(); + else + ksft_exit_fail(); +} -- cgit v1.2.3 From 8c749ce93ee69e789e46b3be98de9e0cbfcf8ed8 Mon Sep 17 00:00:00 2001 From: Bamvor Jian Zhang Date: Fri, 8 Jan 2016 15:27:33 +0800 Subject: selftests: create test-specific kconfig fragments Create the config file in each directory of testcase which need more kernel configuration than the default defconfig. User could use these configs with merge_config.sh script: Enable config for specific testcase: (export ARCH=xxx #for cross compiling) ./scripts/kconfig/merge_config.sh .config \ tools/testing/selftests/xxx/config Enable configs for all testcases: (export ARCH=xxx #for cross compiling) ./scripts/kconfig/merge_config.sh .config \ tools/testing/selftests/*/config Signed-off-by: Bamvor Jian Zhang Reviewed-by: Shuah Khan Signed-off-by: Shuah Khan --- tools/testing/selftests/cpu-hotplug/config | 2 ++ tools/testing/selftests/firmware/config | 1 + tools/testing/selftests/ftrace/config | 1 + tools/testing/selftests/ipc/config | 2 ++ tools/testing/selftests/memory-hotplug/config | 4 ++++ tools/testing/selftests/mount/config | 2 ++ tools/testing/selftests/net/config | 3 +++ tools/testing/selftests/pstore/config | 4 ++++ tools/testing/selftests/seccomp/config | 2 ++ tools/testing/selftests/static_keys/config | 1 + tools/testing/selftests/user/config | 1 + tools/testing/selftests/vm/config | 1 + tools/testing/selftests/zram/config | 2 ++ 13 files changed, 26 insertions(+) create mode 100644 tools/testing/selftests/cpu-hotplug/config create mode 100644 tools/testing/selftests/firmware/config create mode 100644 tools/testing/selftests/ftrace/config create mode 100644 tools/testing/selftests/ipc/config create mode 100644 tools/testing/selftests/memory-hotplug/config create mode 100644 tools/testing/selftests/mount/config create mode 100644 tools/testing/selftests/net/config create mode 100644 tools/testing/selftests/pstore/config create mode 100644 tools/testing/selftests/seccomp/config create mode 100644 tools/testing/selftests/static_keys/config create mode 100644 tools/testing/selftests/user/config create mode 100644 tools/testing/selftests/vm/config create mode 100644 tools/testing/selftests/zram/config diff --git a/tools/testing/selftests/cpu-hotplug/config b/tools/testing/selftests/cpu-hotplug/config new file mode 100644 index 000000000000..e6ab090cfbf3 --- /dev/null +++ b/tools/testing/selftests/cpu-hotplug/config @@ -0,0 +1,2 @@ +CONFIG_NOTIFIER_ERROR_INJECTION=y +CONFIG_CPU_NOTIFIER_ERROR_INJECT=m diff --git a/tools/testing/selftests/firmware/config b/tools/testing/selftests/firmware/config new file mode 100644 index 000000000000..c8137f70e291 --- /dev/null +++ b/tools/testing/selftests/firmware/config @@ -0,0 +1 @@ +CONFIG_TEST_FIRMWARE=y diff --git a/tools/testing/selftests/ftrace/config b/tools/testing/selftests/ftrace/config new file mode 100644 index 000000000000..ef8214661612 --- /dev/null +++ b/tools/testing/selftests/ftrace/config @@ -0,0 +1 @@ +CONFIG_FTRACE=y diff --git a/tools/testing/selftests/ipc/config b/tools/testing/selftests/ipc/config new file mode 100644 index 000000000000..0702447109f5 --- /dev/null +++ b/tools/testing/selftests/ipc/config @@ -0,0 +1,2 @@ +CONFIG_EXPERT=y +CONFIG_CHECKPOINT_RESTORE=y diff --git a/tools/testing/selftests/memory-hotplug/config b/tools/testing/selftests/memory-hotplug/config new file mode 100644 index 000000000000..2fde30191a47 --- /dev/null +++ b/tools/testing/selftests/memory-hotplug/config @@ -0,0 +1,4 @@ +CONFIG_MEMORY_HOTPLUG=y +CONFIG_MEMORY_HOTPLUG_SPARSE=y +CONFIG_NOTIFIER_ERROR_INJECTION=y +CONFIG_MEMORY_NOTIFIER_ERROR_INJECT=m diff --git a/tools/testing/selftests/mount/config b/tools/testing/selftests/mount/config new file mode 100644 index 000000000000..b5d881e48548 --- /dev/null +++ b/tools/testing/selftests/mount/config @@ -0,0 +1,2 @@ +CONFIG_USER_NS=y +CONFIG_DEVPTS_MULTIPLE_INSTANCES=y diff --git a/tools/testing/selftests/net/config b/tools/testing/selftests/net/config new file mode 100644 index 000000000000..e57b4ac40e72 --- /dev/null +++ b/tools/testing/selftests/net/config @@ -0,0 +1,3 @@ +CONFIG_USER_NS=y +CONFIG_BPF_SYSCALL=y +CONFIG_TEST_BPF=m diff --git a/tools/testing/selftests/pstore/config b/tools/testing/selftests/pstore/config new file mode 100644 index 000000000000..6a8e5a9bfc10 --- /dev/null +++ b/tools/testing/selftests/pstore/config @@ -0,0 +1,4 @@ +CONFIG_MISC_FILESYSTEMS=y +CONFIG_PSTORE=y +CONFIG_PSTORE_PMSG=y +CONFIG_PSTORE_CONSOLE=y diff --git a/tools/testing/selftests/seccomp/config b/tools/testing/selftests/seccomp/config new file mode 100644 index 000000000000..db1e11b08c8a --- /dev/null +++ b/tools/testing/selftests/seccomp/config @@ -0,0 +1,2 @@ +CONFIG_SECCOMP=y +CONFIG_SECCOMP_FILTER=y diff --git a/tools/testing/selftests/static_keys/config b/tools/testing/selftests/static_keys/config new file mode 100644 index 000000000000..d538fb774b96 --- /dev/null +++ b/tools/testing/selftests/static_keys/config @@ -0,0 +1 @@ +CONFIG_TEST_STATIC_KEYS=m diff --git a/tools/testing/selftests/user/config b/tools/testing/selftests/user/config new file mode 100644 index 000000000000..784ed8416324 --- /dev/null +++ b/tools/testing/selftests/user/config @@ -0,0 +1 @@ +CONFIG_TEST_USER_COPY=m diff --git a/tools/testing/selftests/vm/config b/tools/testing/selftests/vm/config new file mode 100644 index 000000000000..698c7ed28a26 --- /dev/null +++ b/tools/testing/selftests/vm/config @@ -0,0 +1 @@ +CONFIG_USERFAULTFD=y diff --git a/tools/testing/selftests/zram/config b/tools/testing/selftests/zram/config new file mode 100644 index 000000000000..e0cc47e2c7e2 --- /dev/null +++ b/tools/testing/selftests/zram/config @@ -0,0 +1,2 @@ +CONFIG_ZSMALLOC=y +CONFIG_ZRAM=m -- cgit v1.2.3 From 3d6dee7af7fe11c2cea3d9ae70faedf2987ccf96 Mon Sep 17 00:00:00 2001 From: Bamvor Jian Zhang Date: Fri, 8 Jan 2016 15:27:34 +0800 Subject: Makefile: add kselftest-merge Add kselftest-merge to enable the dependencies of kernel config for all the kselftest testcases. User should call it after create the kernel configuration(.config), eg: $ make kselftest-merge Signed-off-by: Bamvor Jian Zhang Signed-off-by: Shuah Khan --- Makefile | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Makefile b/Makefile index c65fe37c99e5..42e3d1e111bb 100644 --- a/Makefile +++ b/Makefile @@ -1087,6 +1087,14 @@ kselftest: kselftest-clean: $(Q)$(MAKE) -C tools/testing/selftests clean +PHONY += kselftest-merge +kselftest-merge: + $(if $(wildcard $(objtree)/.config),, $(error No .config exists, config your kernel first!)) + $(Q)$(CONFIG_SHELL) $(srctree)/scripts/kconfig/merge_config.sh \ + -m $(objtree)/.config \ + $(srctree)/tools/testing/selftests/*/config + +$(Q)$(MAKE) -f $(srctree)/Makefile olddefconfig + # --------------------------------------------------------------------------- # Modules @@ -1295,6 +1303,8 @@ help: @echo ' Build, install, and boot kernel before' @echo ' running kselftest on it' @echo ' kselftest-clean - Remove all generated kselftest files' + @echo ' kselftest-merge - Merge all the config dependencies of kselftest to existed' + @echo ' .config.' @echo '' @echo 'Kernel packaging:' @$(MAKE) $(build)=$(package-dir) help -- cgit v1.2.3 From 3b48bfc0ab997d43fa89190c769a881db853c569 Mon Sep 17 00:00:00 2001 From: Sudeep Holla Date: Thu, 25 Feb 2016 13:57:07 +0000 Subject: selftests: add missing .gitignore file or entry Only IPC selftest is missing the .gitignore file, so add it. Also step_after_suspend_test is missing in breakpoints selftest .gitignore file Signed-off-by: Sudeep Holla Signed-off-by: Shuah Khan --- tools/testing/selftests/breakpoints/.gitignore | 1 + tools/testing/selftests/ipc/.gitignore | 1 + 2 files changed, 2 insertions(+) create mode 100644 tools/testing/selftests/ipc/.gitignore diff --git a/tools/testing/selftests/breakpoints/.gitignore b/tools/testing/selftests/breakpoints/.gitignore index 9b3193d06608..a23bb4a6f06c 100644 --- a/tools/testing/selftests/breakpoints/.gitignore +++ b/tools/testing/selftests/breakpoints/.gitignore @@ -1 +1,2 @@ breakpoint_test +step_after_suspend_test diff --git a/tools/testing/selftests/ipc/.gitignore b/tools/testing/selftests/ipc/.gitignore new file mode 100644 index 000000000000..84b66a3c1f74 --- /dev/null +++ b/tools/testing/selftests/ipc/.gitignore @@ -0,0 +1 @@ +msgque_test -- cgit v1.2.3 From 36d3f7d8204dabc225febdeca9db8a3114cc9b6d Mon Sep 17 00:00:00 2001 From: Shuah Khan Date: Thu, 25 Feb 2016 10:17:42 -0700 Subject: selftests: media_dcevice_test fix to handle ioctl failure case Fix to print information returned by ioctl only when it returns success. Signed-off-by: Shuah Khan --- tools/testing/selftests/media_tests/media_device_test.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/media_tests/media_device_test.c b/tools/testing/selftests/media_tests/media_device_test.c index a47880b9e00a..7a4d613f4742 100644 --- a/tools/testing/selftests/media_tests/media_device_test.c +++ b/tools/testing/selftests/media_tests/media_device_test.c @@ -86,8 +86,9 @@ int main(int argc, char **argv) ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi); if (ret < 0) printf("Media Device Info errno %s\n", strerror(errno)); - printf("Media device model %s driver %s\n", - mdi.model, mdi.driver); + else + printf("Media device model %s driver %s\n", + mdi.model, mdi.driver); sleep(10); count++; } -- cgit v1.2.3 From 6accd8e9bf22749f621fb7fb57995fa65f4cd873 Mon Sep 17 00:00:00 2001 From: Shuah Khan Date: Thu, 25 Feb 2016 10:35:01 -0700 Subject: selftests: media_dcevice_test fix usage information Fix the incorrect usage information. Signed-off-by: Shuah Khan --- tools/testing/selftests/media_tests/media_device_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/media_tests/media_device_test.c b/tools/testing/selftests/media_tests/media_device_test.c index 7a4d613f4742..cbf53a032ab5 100644 --- a/tools/testing/selftests/media_tests/media_device_test.c +++ b/tools/testing/selftests/media_tests/media_device_test.c @@ -19,7 +19,7 @@ * seconds. * * Usage: - * sudo ./media_devkref_test -d /dev/mediaX + * sudo ./media_device_test -d /dev/mediaX * * While test is running, remove the device and * ensure there are no use after free errors and -- cgit v1.2.3