From 3aefd1febd882a76e21ca8f61ade03b9c1a2c719 Mon Sep 17 00:00:00 2001 From: SeongJae Park Date: Sat, 18 Jun 2016 18:28:39 +0900 Subject: selftests/lib: set printf.sh executable Test for test_printf module fails always because the test program, printf.sh, has no execution permission. This commit adds execution permission to it. Signed-off-by: SeongJae Park Acked-by: Kees Cook Signed-off-by: Shuah Khan diff --git a/tools/testing/selftests/lib/printf.sh b/tools/testing/selftests/lib/printf.sh old mode 100644 new mode 100755 -- cgit v0.10.2 From ee65735dd5acecbf335f9aa32e3b129a1de9e184 Mon Sep 17 00:00:00 2001 From: Yannick Brosseau Date: Mon, 20 Jun 2016 08:07:21 -0700 Subject: selftests/vm: write strlen length instead of sizeof to nr_hugepages When setting back the initial value to nr_hugepages, the test was writing a length sizeof of the string and checking that strlen was writen. Since those values are not the same, use strlen in both place instead. Also make the error messages more explicit to help in future debugging. Signed-off-by: Yannick Brosseau Signed-off-by: Shuah Khan diff --git a/tools/testing/selftests/vm/compaction_test.c b/tools/testing/selftests/vm/compaction_test.c index 932ff57..6d1437f 100644 --- a/tools/testing/selftests/vm/compaction_test.c +++ b/tools/testing/selftests/vm/compaction_test.c @@ -101,7 +101,7 @@ int check_compaction(unsigned long mem_free, unsigned int hugepage_size) /* Start with the initial condition of 0 huge pages*/ if (write(fd, "0", sizeof(char)) != sizeof(char)) { - perror("Failed to write to /proc/sys/vm/nr_hugepages\n"); + perror("Failed to write 0 to /proc/sys/vm/nr_hugepages\n"); goto close_fd; } @@ -110,14 +110,14 @@ int check_compaction(unsigned long mem_free, unsigned int hugepage_size) /* Request a large number of huge pages. The Kernel will allocate as much as it can */ if (write(fd, "100000", (6*sizeof(char))) != (6*sizeof(char))) { - perror("Failed to write to /proc/sys/vm/nr_hugepages\n"); + perror("Failed to write 100000 to /proc/sys/vm/nr_hugepages\n"); goto close_fd; } lseek(fd, 0, SEEK_SET); if (read(fd, nr_hugepages, sizeof(nr_hugepages)) <= 0) { - perror("Failed to read from /proc/sys/vm/nr_hugepages\n"); + perror("Failed to re-read from /proc/sys/vm/nr_hugepages\n"); goto close_fd; } @@ -136,9 +136,9 @@ int check_compaction(unsigned long mem_free, unsigned int hugepage_size) printf("No of huge pages allocated = %d\n", (atoi(nr_hugepages))); - if (write(fd, initial_nr_hugepages, sizeof(initial_nr_hugepages)) + if (write(fd, initial_nr_hugepages, strlen(initial_nr_hugepages)) != strlen(initial_nr_hugepages)) { - perror("Failed to write to /proc/sys/vm/nr_hugepages\n"); + perror("Failed to write value to /proc/sys/vm/nr_hugepages\n"); goto close_fd; } -- cgit v0.10.2 From bff124682eaa784596672bdd344b9b0f646035a2 Mon Sep 17 00:00:00 2001 From: Yannick Brosseau Date: Mon, 20 Jun 2016 14:27:10 -0700 Subject: selftests/vm: Don't mlockall MCL_CURRENT in on-fault-limit test The default MEMLOCK limit is not big enough to accomodate all the current pages of the test program process, so the test fails at this step. By removing the MCL_CURRENT flag, we allow the mlockall call to succeed. The mmap is twice the size of the current limit, so it will still fail as expected. Signed-off-by: Yannick Brosseau Signed-off-by: Shuah Khan diff --git a/tools/testing/selftests/vm/on-fault-limit.c b/tools/testing/selftests/vm/on-fault-limit.c index 245accc..0ae458f 100644 --- a/tools/testing/selftests/vm/on-fault-limit.c +++ b/tools/testing/selftests/vm/on-fault-limit.c @@ -20,7 +20,7 @@ static int test_limit(void) return ret; } - if (mlockall(MCL_CURRENT | MCL_ONFAULT | MCL_FUTURE)) { + if (mlockall(MCL_ONFAULT | MCL_FUTURE)) { perror("mlockall"); return ret; } -- cgit v0.10.2 From e9c0d44f5328ba844f7e037791b9cc72ee3ef964 Mon Sep 17 00:00:00 2001 From: Shuah Khan Date: Fri, 17 Jun 2016 18:05:05 -0600 Subject: selftests: media_device_test change it to randomize loop count Change it to randomize the loop count instead of hardcoded number of times ioctl is called. Signed-off-by: Shuah Khan diff --git a/tools/testing/selftests/media_tests/media_device_test.c b/tools/testing/selftests/media_tests/media_device_test.c index cbf53a0..5d49943 100644 --- a/tools/testing/selftests/media_tests/media_device_test.c +++ b/tools/testing/selftests/media_tests/media_device_test.c @@ -1,5 +1,5 @@ /* - * media_devkref_test.c - Media Controller Device Kref API Test + * media_device_test.c - Media Controller Device ioctl loop Test * * Copyright (c) 2016 Shuah Khan * Copyright (c) 2016 Samsung Electronics Co., Ltd. @@ -35,13 +35,14 @@ #include #include #include +#include #include int main(int argc, char **argv) { int opt; char media_device[256]; - int count = 0; + int count; struct media_device_info mdi; int ret; int fd; @@ -69,6 +70,10 @@ int main(int argc, char **argv) exit(-1); } + /* Generate random number of interations */ + srand((unsigned int) time(NULL)); + count = rand(); + /* Open Media device and keep it open */ fd = open(media_device, O_RDWR); if (fd == -1) { @@ -82,14 +87,16 @@ int main(int argc, char **argv) "other Oops in the dmesg. Enable KaSan kernel\n" "config option for use-after-free error detection.\n\n"); - while (count < 100) { + printf("Running test for %d iternations\n", count); + + while (count > 0) { ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi); if (ret < 0) printf("Media Device Info errno %s\n", strerror(errno)); else - printf("Media device model %s driver %s\n", - mdi.model, mdi.driver); + printf("Media device model %s driver %s - count %d\n", + mdi.model, mdi.driver, count); sleep(10); - count++; + count--; } } -- cgit v0.10.2 From b96da0fc54ffeca53dbab231ae845ad093785cc7 Mon Sep 17 00:00:00 2001 From: Shuah Khan Date: Fri, 17 Jun 2016 18:06:36 -0600 Subject: selftests: add media_device_open test Add a new media test to open, run ioctl, and close the media device file. Signed-off-by: Shuah Khan diff --git a/tools/testing/selftests/media_tests/Makefile b/tools/testing/selftests/media_tests/Makefile index 7071bcc..177256a 100644 --- a/tools/testing/selftests/media_tests/Makefile +++ b/tools/testing/selftests/media_tests/Makefile @@ -1,7 +1,7 @@ -TEST_PROGS := media_device_test +TEST_PROGS := media_device_test media_device_open all: $(TEST_PROGS) include ../lib.mk clean: - rm -fr media_device_test + rm -fr media_device_test media_device_open diff --git a/tools/testing/selftests/media_tests/media_device_open.c b/tools/testing/selftests/media_tests/media_device_open.c new file mode 100644 index 0000000..44343c0 --- /dev/null +++ b/tools/testing/selftests/media_tests/media_device_open.c @@ -0,0 +1,81 @@ +/* + * media_device_open.c - Media Controller Device Open 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, closes the file, and exits. + * + * Usage: + * sudo ./media_device_open -d /dev/mediaX + * + * Run this test is a loop and run bind/unbind on the driver. +*/ + +#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); + } + + ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi); + if (ret < 0) + printf("Media Device Info errno %s\n", strerror(errno)); + else + printf("Media device model %s driver %s\n", + mdi.model, mdi.driver); +} -- cgit v0.10.2 From fe8777a8a0a144dee2eb59d00664184f1e49db4a Mon Sep 17 00:00:00 2001 From: Shuah Khan Date: Fri, 17 Jun 2016 18:12:04 -0600 Subject: selftests: add media controller regression test scripts and document Add regression test scripts open_loop_test.sh, and bind_unbind_sample.sh. Also add regression_test.txt that describes the regression test procedure. Signed-off-by: Shuah Khan diff --git a/tools/testing/selftests/media_tests/bind_unbind_sample.sh b/tools/testing/selftests/media_tests/bind_unbind_sample.sh new file mode 100755 index 0000000..9f362f1 --- /dev/null +++ b/tools/testing/selftests/media_tests/bind_unbind_sample.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# Find device number in /sys/bus/usb/drivers/drivername +# Edit this file to update the driver numer and name +# Example test for uvcvideo driver +#i=0 +# while :; do +# i=$((i+1)) +# echo 1-5:1.0 > /sys/bus/usb/drivers/uvcvideo/unbind; +# echo 1-5:1.0 > /sys/bus/usb/drivers/uvcvideo/bind; +# clear +# echo $i +#done diff --git a/tools/testing/selftests/media_tests/open_loop_test.sh b/tools/testing/selftests/media_tests/open_loop_test.sh new file mode 100755 index 0000000..dcd3c17 --- /dev/null +++ b/tools/testing/selftests/media_tests/open_loop_test.sh @@ -0,0 +1,10 @@ +#!/bin/bash + i=0 +file=/dev/media$1 + while :; do + echo $file + i=$((i+1)) + R=$(./media_device_open -d $file); + # clear + echo -e "Loop $i\n$R" + done diff --git a/tools/testing/selftests/media_tests/regression_test.txt b/tools/testing/selftests/media_tests/regression_test.txt new file mode 100644 index 0000000..2627367 --- /dev/null +++ b/tools/testing/selftests/media_tests/regression_test.txt @@ -0,0 +1,43 @@ +Testing for regressions in Media Controller API register, ioctl, syscall, +and unregister paths. There have a few problems that result in user-after +free on media_device, media_devnode, and cdev pointers when the driver is +unbound while ioctl is in progress. + +Test Procedure: + +Run bin/unbind loop while ioctls are in progress. +Run rmmod and modprobe. +Disconnect the device. + +Setup: + +Build media_device_test +cd tools/testing/selftests/media_tests +make + +Regressions test for cdev user-after free error on /dev/mediaX when driver +is unbound: + +Start media_device_test to regression test media devnode dynamic alloc +and cdev user-after-free fixes. This opens media dev files and sits in +a loop running media ioctl MEDIA_IOC_DEVICE_INFO command once every 10 +seconds. The idea is when device file goes away, media devnode and cdev +should stick around until this test exits. + +The test for a random number of iterations or until user kills it with a +sleep 10 in between the ioctl calls. + +sudo ./media_device_test -d /dev/mediaX + +Regression test for media_devnode unregister race with ioctl_syscall: + +Start 6 open_loop_test.sh tests with different /dev/mediaX files. When +device file goes away after unbind, device file name changes. Start the +test with possible device names. If we start with /dev/media0 for example, +after unbind, /dev/media1 or /dev/media2 could get created. The idea is +keep ioctls going while bind/unbind runs. + +Copy bind_unbind_sample.txt and make changes to specify the driver name +and number to run bind and unbind. Start the bind_unbind.sh + +Run dmesg looking for any user-after free errors or mutex lock errors. -- cgit v0.10.2 From cde07f453bedd529c9ab7d57535e45f7e90a5c83 Mon Sep 17 00:00:00 2001 From: Shuah Khan Date: Fri, 17 Jun 2016 18:17:15 -0600 Subject: selftests: media_tests - Add media_device_open to .gitignore Add media_device_open to .gitignore Signed-off-by: Shuah Khan diff --git a/tools/testing/selftests/media_tests/.gitignore b/tools/testing/selftests/media_tests/.gitignore index 1c07117..faf5891 100644 --- a/tools/testing/selftests/media_tests/.gitignore +++ b/tools/testing/selftests/media_tests/.gitignore @@ -1 +1,2 @@ media_device_test +media_device_open -- cgit v0.10.2 From d78388dbec256b179f1c42e714cf4acf630067c6 Mon Sep 17 00:00:00 2001 From: Shuah Khan Date: Fri, 22 Jul 2016 15:29:50 -0600 Subject: selftests: media_tests add a new video device test Add a new video device test that opens user specified Video Device and calls video ioctls in a loop once every 10 seconds. This test is intended for testing device removal and driver unbind while an ioctl is active. Clean device removal and driver unbind is expected without any use-after-free and panics. Signed-off-by: Shuah Khan diff --git a/tools/testing/selftests/media_tests/.gitignore b/tools/testing/selftests/media_tests/.gitignore index faf5891..8745eba 100644 --- a/tools/testing/selftests/media_tests/.gitignore +++ b/tools/testing/selftests/media_tests/.gitignore @@ -1,2 +1,3 @@ media_device_test media_device_open +video_device_test diff --git a/tools/testing/selftests/media_tests/Makefile b/tools/testing/selftests/media_tests/Makefile index 177256a..6b34a01 100644 --- a/tools/testing/selftests/media_tests/Makefile +++ b/tools/testing/selftests/media_tests/Makefile @@ -1,7 +1,7 @@ -TEST_PROGS := media_device_test media_device_open +TEST_PROGS := media_device_test media_device_open video_device_test all: $(TEST_PROGS) include ../lib.mk clean: - rm -fr media_device_test media_device_open + rm -fr media_device_test media_device_open video_device_test diff --git a/tools/testing/selftests/media_tests/video_device_test.c b/tools/testing/selftests/media_tests/video_device_test.c new file mode 100644 index 0000000..66d419c --- /dev/null +++ b/tools/testing/selftests/media_tests/video_device_test.c @@ -0,0 +1,100 @@ +/* + * video_device_test - Video Device 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 Video Device. This test should not be included + * in the Kselftest run. This test should be run when hardware and driver + * that makes use of V4L2 API is present. + * + * This test opens user specified Video Device and calls video ioctls in a + * loop once every 10 seconds. + * + * Usage: + * sudo ./video_device_test -d /dev/videoX + * + * While test is running, remove the device or unbind the driver and + * ensure there are no use after free errors and other Oops in the + * dmesg. + * When possible, enable KaSan kernel config option for use-after-free + * error detection. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int opt; + char video_dev[256]; + int count; + struct v4l2_tuner vtuner; + struct v4l2_capability vcap; + 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(video_dev, optarg, sizeof(video_dev) - 1); + video_dev[sizeof(video_dev)-1] = '\0'; + break; + default: + printf("Usage: %s [-d ]\n", argv[0]); + exit(-1); + } + } + + /* Generate random number of interations */ + srand((unsigned int) time(NULL)); + count = rand(); + + /* Open Video device and keep it open */ + fd = open(video_dev, O_RDWR); + if (fd == -1) { + printf("Video Device open errno %s\n", strerror(errno)); + exit(-1); + } + + printf("\nNote:\n" + "While test is running, remove the device or unbind\n" + "driver and ensure there are no use after free errors\n" + "and other Oops in the dmesg. When possible, enable KaSan\n" + "kernel config option for use-after-free error detection.\n\n"); + + while (count > 0) { + ret = ioctl(fd, VIDIOC_QUERYCAP, &vcap); + if (ret < 0) + printf("VIDIOC_QUERYCAP errno %s\n", strerror(errno)); + else + printf("Video device driver %s\n", vcap.driver); + + ret = ioctl(fd, VIDIOC_G_TUNER, &vtuner); + if (ret < 0) + printf("VIDIOC_G_TUNER, errno %s\n", strerror(errno)); + else + printf("type %d rangelow %d rangehigh %d\n", + vtuner.type, vtuner.rangelow, vtuner.rangehigh); + sleep(10); + count--; + } +} -- cgit v0.10.2