Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/futex: Refactor futex_wait with kselftest_harness.h

To reduce the boilerplate code, refactor futex_wait test to use
kselftest_harness header instead of futex's logging header.

Signed-off-by: André Almeida <andrealmeid@igalia.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

authored by

André Almeida and committed by
Thomas Gleixner
e5c04d0f 14d016bd

+40 -67
+39 -66
tools/testing/selftests/futex/functional/futex_wait.c
··· 9 9 #include <sys/shm.h> 10 10 #include <sys/mman.h> 11 11 #include <fcntl.h> 12 - #include "logging.h" 13 - #include "futextest.h" 14 12 15 - #define TEST_NAME "futex-wait" 13 + #include "futextest.h" 14 + #include "../../kselftest_harness.h" 15 + 16 16 #define timeout_ns 30000000 17 17 #define WAKE_WAIT_US 10000 18 18 #define SHM_PATH "futex_shm_file" 19 19 20 20 void *futex; 21 - 22 - void usage(char *prog) 23 - { 24 - printf("Usage: %s\n", prog); 25 - printf(" -c Use color\n"); 26 - printf(" -h Display this help message\n"); 27 - printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n", 28 - VQUIET, VCRITICAL, VINFO); 29 - } 30 21 31 22 static void *waiterfn(void *arg) 32 23 { ··· 36 45 return NULL; 37 46 } 38 47 39 - int main(int argc, char *argv[]) 48 + TEST(private_futex) 40 49 { 41 - int res, ret = RET_PASS, fd, c, shm_id; 42 - u_int32_t f_private = 0, *shared_data; 43 50 unsigned int flags = FUTEX_PRIVATE_FLAG; 51 + u_int32_t f_private = 0; 44 52 pthread_t waiter; 45 - void *shm; 53 + int res; 46 54 47 55 futex = &f_private; 48 56 49 - while ((c = getopt(argc, argv, "cht:v:")) != -1) { 50 - switch (c) { 51 - case 'c': 52 - log_color(1); 53 - break; 54 - case 'h': 55 - usage(basename(argv[0])); 56 - exit(0); 57 - case 'v': 58 - log_verbosity(atoi(optarg)); 59 - break; 60 - default: 61 - usage(basename(argv[0])); 62 - exit(1); 63 - } 64 - } 65 - 66 - ksft_print_header(); 67 - ksft_set_plan(3); 68 - ksft_print_msg("%s: Test futex_wait\n", basename(argv[0])); 69 - 70 57 /* Testing a private futex */ 71 - info("Calling private futex_wait on futex: %p\n", futex); 58 + ksft_print_dbg_msg("Calling private futex_wait on futex: %p\n", futex); 72 59 if (pthread_create(&waiter, NULL, waiterfn, (void *) &flags)) 73 - error("pthread_create failed\n", errno); 60 + ksft_exit_fail_msg("pthread_create failed\n"); 74 61 75 62 usleep(WAKE_WAIT_US); 76 63 77 - info("Calling private futex_wake on futex: %p\n", futex); 64 + ksft_print_dbg_msg("Calling private futex_wake on futex: %p\n", futex); 78 65 res = futex_wake(futex, 1, FUTEX_PRIVATE_FLAG); 79 66 if (res != 1) { 80 67 ksft_test_result_fail("futex_wake private returned: %d %s\n", 81 68 errno, strerror(errno)); 82 - ret = RET_FAIL; 83 69 } else { 84 70 ksft_test_result_pass("futex_wake private succeeds\n"); 85 71 } 72 + } 73 + 74 + TEST(anon_page) 75 + { 76 + u_int32_t *shared_data; 77 + pthread_t waiter; 78 + int res, shm_id; 86 79 87 80 /* Testing an anon page shared memory */ 88 81 shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666); ··· 80 105 *shared_data = 0; 81 106 futex = shared_data; 82 107 83 - info("Calling shared (page anon) futex_wait on futex: %p\n", futex); 108 + ksft_print_dbg_msg("Calling shared (page anon) futex_wait on futex: %p\n", futex); 84 109 if (pthread_create(&waiter, NULL, waiterfn, NULL)) 85 - error("pthread_create failed\n", errno); 110 + ksft_exit_fail_msg("pthread_create failed\n"); 86 111 87 112 usleep(WAKE_WAIT_US); 88 113 89 - info("Calling shared (page anon) futex_wake on futex: %p\n", futex); 114 + ksft_print_dbg_msg("Calling shared (page anon) futex_wake on futex: %p\n", futex); 90 115 res = futex_wake(futex, 1, 0); 91 116 if (res != 1) { 92 117 ksft_test_result_fail("futex_wake shared (page anon) returned: %d %s\n", 93 118 errno, strerror(errno)); 94 - ret = RET_FAIL; 95 119 } else { 96 120 ksft_test_result_pass("futex_wake shared (page anon) succeeds\n"); 97 121 } 98 122 123 + shmdt(shared_data); 124 + } 125 + 126 + TEST(file_backed) 127 + { 128 + u_int32_t f_private = 0; 129 + pthread_t waiter; 130 + int res, fd; 131 + void *shm; 99 132 100 133 /* Testing a file backed shared memory */ 101 134 fd = open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); 102 - if (fd < 0) { 103 - perror("open"); 104 - exit(1); 105 - } 135 + if (fd < 0) 136 + ksft_exit_fail_msg("open"); 106 137 107 - if (ftruncate(fd, sizeof(f_private))) { 108 - perror("ftruncate"); 109 - exit(1); 110 - } 138 + if (ftruncate(fd, sizeof(f_private))) 139 + ksft_exit_fail_msg("ftruncate"); 111 140 112 141 shm = mmap(NULL, sizeof(f_private), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 113 - if (shm == MAP_FAILED) { 114 - perror("mmap"); 115 - exit(1); 116 - } 142 + if (shm == MAP_FAILED) 143 + ksft_exit_fail_msg("mmap"); 117 144 118 145 memcpy(shm, &f_private, sizeof(f_private)); 119 146 120 147 futex = shm; 121 148 122 - info("Calling shared (file backed) futex_wait on futex: %p\n", futex); 149 + ksft_print_dbg_msg("Calling shared (file backed) futex_wait on futex: %p\n", futex); 123 150 if (pthread_create(&waiter, NULL, waiterfn, NULL)) 124 - error("pthread_create failed\n", errno); 151 + ksft_exit_fail_msg("pthread_create failed\n"); 125 152 126 153 usleep(WAKE_WAIT_US); 127 154 128 - info("Calling shared (file backed) futex_wake on futex: %p\n", futex); 155 + ksft_print_dbg_msg("Calling shared (file backed) futex_wake on futex: %p\n", futex); 129 156 res = futex_wake(shm, 1, 0); 130 157 if (res != 1) { 131 158 ksft_test_result_fail("futex_wake shared (file backed) returned: %d %s\n", 132 159 errno, strerror(errno)); 133 - ret = RET_FAIL; 134 160 } else { 135 161 ksft_test_result_pass("futex_wake shared (file backed) succeeds\n"); 136 162 } 137 163 138 - /* Freeing resources */ 139 - shmdt(shared_data); 140 164 munmap(shm, sizeof(f_private)); 141 165 remove(SHM_PATH); 142 166 close(fd); 143 - 144 - ksft_print_cnts(); 145 - return ret; 146 167 } 168 + 169 + TEST_HARNESS_MAIN
+1 -1
tools/testing/selftests/futex/functional/run.sh
··· 51 51 ./futex_wait_private_mapped_file 52 52 53 53 echo 54 - ./futex_wait $COLOR 54 + ./futex_wait 55 55 56 56 echo 57 57 ./futex_requeue $COLOR