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

selftests/futex: Refactor futex_requeue_pi_signal_restart with kselftest_harness.h

To reduce the boilerplate code, refactor futex_requeue_pi_signal_restart
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
2ef06156 65a12ce2

+38 -93
+37 -92
tools/testing/selftests/futex/functional/futex_requeue_pi_signal_restart.c
··· 24 24 #include <stdio.h> 25 25 #include <stdlib.h> 26 26 #include <string.h> 27 + 27 28 #include "atomic.h" 28 29 #include "futextest.h" 29 - #include "logging.h" 30 + #include "../../kselftest_harness.h" 30 31 31 - #define TEST_NAME "futex-requeue-pi-signal-restart" 32 32 #define DELAY_US 100 33 33 34 34 futex_t f1 = FUTEX_INITIALIZER; ··· 36 36 atomic_t requeued = ATOMIC_INITIALIZER; 37 37 38 38 int waiter_ret = 0; 39 - 40 - void usage(char *prog) 41 - { 42 - printf("Usage: %s\n", prog); 43 - printf(" -c Use color\n"); 44 - printf(" -h Display this help message\n"); 45 - printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n", 46 - VQUIET, VCRITICAL, VINFO); 47 - } 48 39 49 40 int create_rt_thread(pthread_t *pth, void*(*func)(void *), void *arg, 50 41 int policy, int prio) ··· 48 57 memset(&schedp, 0, sizeof(schedp)); 49 58 50 59 ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); 51 - if (ret) { 52 - error("pthread_attr_setinheritsched\n", ret); 53 - return -1; 54 - } 60 + if (ret) 61 + ksft_exit_fail_msg("pthread_attr_setinheritsched\n"); 55 62 56 63 ret = pthread_attr_setschedpolicy(&attr, policy); 57 - if (ret) { 58 - error("pthread_attr_setschedpolicy\n", ret); 59 - return -1; 60 - } 64 + if (ret) 65 + ksft_exit_fail_msg("pthread_attr_setschedpolicy\n"); 61 66 62 67 schedp.sched_priority = prio; 63 68 ret = pthread_attr_setschedparam(&attr, &schedp); 64 - if (ret) { 65 - error("pthread_attr_setschedparam\n", ret); 66 - return -1; 67 - } 69 + if (ret) 70 + ksft_exit_fail_msg("pthread_attr_setschedparam\n"); 68 71 69 72 ret = pthread_create(pth, &attr, func, arg); 70 - if (ret) { 71 - error("pthread_create\n", ret); 72 - return -1; 73 - } 73 + if (ret) 74 + ksft_exit_fail_msg("pthread_create\n"); 75 + 74 76 return 0; 75 77 } 76 78 77 79 void handle_signal(int signo) 78 80 { 79 - info("signal received %s requeue\n", 81 + ksft_print_dbg_msg("signal received %s requeue\n", 80 82 requeued.val ? "after" : "prior to"); 81 83 } 82 84 ··· 78 94 unsigned int old_val; 79 95 int res; 80 96 81 - waiter_ret = RET_PASS; 82 - 83 - info("Waiter running\n"); 84 - info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2); 97 + ksft_print_dbg_msg("Waiter running\n"); 98 + ksft_print_dbg_msg("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2); 85 99 old_val = f1; 86 100 res = futex_wait_requeue_pi(&f1, old_val, &(f2), NULL, 87 101 FUTEX_PRIVATE_FLAG); 88 102 if (!requeued.val || errno != EWOULDBLOCK) { 89 - fail("unexpected return from futex_wait_requeue_pi: %d (%s)\n", 103 + ksft_test_result_fail("unexpected return from futex_wait_requeue_pi: %d (%s)\n", 90 104 res, strerror(errno)); 91 - info("w2:futex: %x\n", f2); 105 + ksft_print_dbg_msg("w2:futex: %x\n", f2); 92 106 if (!res) 93 107 futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG); 94 - waiter_ret = RET_FAIL; 95 108 } 96 109 97 - info("Waiter exiting with %d\n", waiter_ret); 98 110 pthread_exit(NULL); 99 111 } 100 112 101 113 102 - int main(int argc, char *argv[]) 114 + TEST(futex_requeue_pi_signal_restart) 103 115 { 104 116 unsigned int old_val; 105 117 struct sigaction sa; 106 118 pthread_t waiter; 107 - int c, res, ret = RET_PASS; 108 - 109 - while ((c = getopt(argc, argv, "chv:")) != -1) { 110 - switch (c) { 111 - case 'c': 112 - log_color(1); 113 - break; 114 - case 'h': 115 - usage(basename(argv[0])); 116 - exit(0); 117 - case 'v': 118 - log_verbosity(atoi(optarg)); 119 - break; 120 - default: 121 - usage(basename(argv[0])); 122 - exit(1); 123 - } 124 - } 125 - 126 - ksft_print_header(); 127 - ksft_set_plan(1); 128 - ksft_print_msg("%s: Test signal handling during requeue_pi\n", 129 - basename(argv[0])); 130 - ksft_print_msg("\tArguments: <none>\n"); 119 + int res; 131 120 132 121 sa.sa_handler = handle_signal; 133 122 sigemptyset(&sa.sa_mask); 134 123 sa.sa_flags = 0; 135 - if (sigaction(SIGUSR1, &sa, NULL)) { 136 - error("sigaction\n", errno); 137 - exit(1); 138 - } 124 + if (sigaction(SIGUSR1, &sa, NULL)) 125 + ksft_exit_fail_msg("sigaction\n"); 139 126 140 - info("m1:f2: %x\n", f2); 141 - info("Creating waiter\n"); 127 + ksft_print_dbg_msg("m1:f2: %x\n", f2); 128 + ksft_print_dbg_msg("Creating waiter\n"); 142 129 res = create_rt_thread(&waiter, waiterfn, NULL, SCHED_FIFO, 1); 143 - if (res) { 144 - error("Creating waiting thread failed", res); 145 - ret = RET_ERROR; 146 - goto out; 147 - } 130 + if (res) 131 + ksft_exit_fail_msg("Creating waiting thread failed"); 148 132 149 - info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2); 150 - info("m2:f2: %x\n", f2); 133 + ksft_print_dbg_msg("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2); 134 + ksft_print_dbg_msg("m2:f2: %x\n", f2); 151 135 futex_lock_pi(&f2, 0, 0, FUTEX_PRIVATE_FLAG); 152 - info("m3:f2: %x\n", f2); 136 + ksft_print_dbg_msg("m3:f2: %x\n", f2); 153 137 154 138 while (1) { 155 139 /* ··· 125 173 * restart futex_wait_requeue_pi() in the kernel. Wait for the 126 174 * waiter to block on f1 again. 127 175 */ 128 - info("Issuing SIGUSR1 to waiter\n"); 176 + ksft_print_dbg_msg("Issuing SIGUSR1 to waiter\n"); 129 177 pthread_kill(waiter, SIGUSR1); 130 178 usleep(DELAY_US); 131 179 132 - info("Requeueing waiter via FUTEX_CMP_REQUEUE_PI\n"); 180 + ksft_print_dbg_msg("Requeueing waiter via FUTEX_CMP_REQUEUE_PI\n"); 133 181 old_val = f1; 134 182 res = futex_cmp_requeue_pi(&f1, old_val, &(f2), 1, 0, 135 183 FUTEX_PRIVATE_FLAG); ··· 143 191 atomic_set(&requeued, 1); 144 192 break; 145 193 } else if (res < 0) { 146 - error("FUTEX_CMP_REQUEUE_PI failed\n", errno); 147 - ret = RET_ERROR; 148 - break; 194 + ksft_exit_fail_msg("FUTEX_CMP_REQUEUE_PI failed\n"); 149 195 } 150 196 } 151 - info("m4:f2: %x\n", f2); 197 + ksft_print_dbg_msg("m4:f2: %x\n", f2); 152 198 153 199 /* 154 200 * Signal the waiter after requeue, waiter should return from ··· 154 204 * futex_unlock_pi() can't happen before the signal wakeup is detected 155 205 * in the kernel. 156 206 */ 157 - info("Issuing SIGUSR1 to waiter\n"); 207 + ksft_print_dbg_msg("Issuing SIGUSR1 to waiter\n"); 158 208 pthread_kill(waiter, SIGUSR1); 159 - info("Waiting for waiter to return\n"); 209 + ksft_print_dbg_msg("Waiting for waiter to return\n"); 160 210 pthread_join(waiter, NULL); 161 211 162 - info("Calling FUTEX_UNLOCK_PI on mutex=%x @ %p\n", f2, &f2); 212 + ksft_print_dbg_msg("Calling FUTEX_UNLOCK_PI on mutex=%x @ %p\n", f2, &f2); 163 213 futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG); 164 - info("m5:f2: %x\n", f2); 165 - 166 - out: 167 - if (ret == RET_PASS && waiter_ret) 168 - ret = waiter_ret; 169 - 170 - print_result(TEST_NAME, ret); 171 - return ret; 214 + ksft_print_dbg_msg("m5:f2: %x\n", f2); 172 215 } 216 + 217 + TEST_HARNESS_MAIN
+1 -1
tools/testing/selftests/futex/functional/run.sh
··· 38 38 ./futex_requeue_pi_mismatched_ops 39 39 40 40 echo 41 - ./futex_requeue_pi_signal_restart $COLOR 41 + ./futex_requeue_pi_signal_restart 42 42 43 43 echo 44 44 ./futex_wait_timeout $COLOR