xref: /openbmc/linux/tools/testing/selftests/futex/functional/futex_wait_wouldblock.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22aa8470fSDarren Hart /******************************************************************************
32aa8470fSDarren Hart  *
42aa8470fSDarren Hart  *   Copyright © International Business Machines  Corp., 2009
52aa8470fSDarren Hart  *
62aa8470fSDarren Hart  * DESCRIPTION
72aa8470fSDarren Hart  *      Test if FUTEX_WAIT op returns -EWOULDBLOCK if the futex value differs
82aa8470fSDarren Hart  *      from the expected one.
92aa8470fSDarren Hart  *
102aa8470fSDarren Hart  * AUTHOR
112aa8470fSDarren Hart  *      Gowrishankar <gowrishankar.m@in.ibm.com>
122aa8470fSDarren Hart  *
132aa8470fSDarren Hart  * HISTORY
142aa8470fSDarren Hart  *      2009-Nov-14: Initial version by Gowrishankar <gowrishankar.m@in.ibm.com>
152aa8470fSDarren Hart  *
162aa8470fSDarren Hart  *****************************************************************************/
172aa8470fSDarren Hart 
182aa8470fSDarren Hart #include <errno.h>
192aa8470fSDarren Hart #include <getopt.h>
202aa8470fSDarren Hart #include <stdio.h>
212aa8470fSDarren Hart #include <stdlib.h>
222aa8470fSDarren Hart #include <string.h>
232aa8470fSDarren Hart #include <time.h>
242aa8470fSDarren Hart #include "futextest.h"
25*9d57f7c7SAndré Almeida #include "futex2test.h"
262aa8470fSDarren Hart #include "logging.h"
272aa8470fSDarren Hart 
281f666e52SNaresh Kamboju #define TEST_NAME "futex-wait-wouldblock"
292aa8470fSDarren Hart #define timeout_ns 100000
302aa8470fSDarren Hart 
usage(char * prog)312aa8470fSDarren Hart void usage(char *prog)
322aa8470fSDarren Hart {
332aa8470fSDarren Hart 	printf("Usage: %s\n", prog);
342aa8470fSDarren Hart 	printf("  -c	Use color\n");
352aa8470fSDarren Hart 	printf("  -h	Display this help message\n");
362aa8470fSDarren Hart 	printf("  -v L	Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
372aa8470fSDarren Hart 	       VQUIET, VCRITICAL, VINFO);
382aa8470fSDarren Hart }
392aa8470fSDarren Hart 
main(int argc,char * argv[])402aa8470fSDarren Hart int main(int argc, char *argv[])
412aa8470fSDarren Hart {
422aa8470fSDarren Hart 	struct timespec to = {.tv_sec = 0, .tv_nsec = timeout_ns};
432aa8470fSDarren Hart 	futex_t f1 = FUTEX_INITIALIZER;
442aa8470fSDarren Hart 	int res, ret = RET_PASS;
452aa8470fSDarren Hart 	int c;
46*9d57f7c7SAndré Almeida 	struct futex_waitv waitv = {
47*9d57f7c7SAndré Almeida 			.uaddr = (uintptr_t)&f1,
48*9d57f7c7SAndré Almeida 			.val = f1+1,
49*9d57f7c7SAndré Almeida 			.flags = FUTEX_32,
50*9d57f7c7SAndré Almeida 			.__reserved = 0
51*9d57f7c7SAndré Almeida 		};
522aa8470fSDarren Hart 
532aa8470fSDarren Hart 	while ((c = getopt(argc, argv, "cht:v:")) != -1) {
542aa8470fSDarren Hart 		switch (c) {
552aa8470fSDarren Hart 		case 'c':
562aa8470fSDarren Hart 			log_color(1);
572aa8470fSDarren Hart 			break;
582aa8470fSDarren Hart 		case 'h':
592aa8470fSDarren Hart 			usage(basename(argv[0]));
602aa8470fSDarren Hart 			exit(0);
612aa8470fSDarren Hart 		case 'v':
622aa8470fSDarren Hart 			log_verbosity(atoi(optarg));
632aa8470fSDarren Hart 			break;
642aa8470fSDarren Hart 		default:
652aa8470fSDarren Hart 			usage(basename(argv[0]));
662aa8470fSDarren Hart 			exit(1);
672aa8470fSDarren Hart 		}
682aa8470fSDarren Hart 	}
692aa8470fSDarren Hart 
70b274e75cSShuah Khan 	ksft_print_header();
71*9d57f7c7SAndré Almeida 	ksft_set_plan(2);
72b274e75cSShuah Khan 	ksft_print_msg("%s: Test the unexpected futex value in FUTEX_WAIT\n",
732aa8470fSDarren Hart 	       basename(argv[0]));
742aa8470fSDarren Hart 
752aa8470fSDarren Hart 	info("Calling futex_wait on f1: %u @ %p with val=%u\n", f1, &f1, f1+1);
762aa8470fSDarren Hart 	res = futex_wait(&f1, f1+1, &to, FUTEX_PRIVATE_FLAG);
772aa8470fSDarren Hart 	if (!res || errno != EWOULDBLOCK) {
78*9d57f7c7SAndré Almeida 		ksft_test_result_fail("futex_wait returned: %d %s\n",
79*9d57f7c7SAndré Almeida 				      res ? errno : res,
80*9d57f7c7SAndré Almeida 				      res ? strerror(errno) : "");
812aa8470fSDarren Hart 		ret = RET_FAIL;
82*9d57f7c7SAndré Almeida 	} else {
83*9d57f7c7SAndré Almeida 		ksft_test_result_pass("futex_wait\n");
842aa8470fSDarren Hart 	}
852aa8470fSDarren Hart 
86*9d57f7c7SAndré Almeida 	if (clock_gettime(CLOCK_MONOTONIC, &to)) {
87*9d57f7c7SAndré Almeida 		error("clock_gettime failed\n", errno);
88*9d57f7c7SAndré Almeida 		return errno;
89*9d57f7c7SAndré Almeida 	}
90*9d57f7c7SAndré Almeida 
91*9d57f7c7SAndré Almeida 	to.tv_nsec += timeout_ns;
92*9d57f7c7SAndré Almeida 
93*9d57f7c7SAndré Almeida 	if (to.tv_nsec >= 1000000000) {
94*9d57f7c7SAndré Almeida 		to.tv_sec++;
95*9d57f7c7SAndré Almeida 		to.tv_nsec -= 1000000000;
96*9d57f7c7SAndré Almeida 	}
97*9d57f7c7SAndré Almeida 
98*9d57f7c7SAndré Almeida 	info("Calling futex_waitv on f1: %u @ %p with val=%u\n", f1, &f1, f1+1);
99*9d57f7c7SAndré Almeida 	res = futex_waitv(&waitv, 1, 0, &to, CLOCK_MONOTONIC);
100*9d57f7c7SAndré Almeida 	if (!res || errno != EWOULDBLOCK) {
101*9d57f7c7SAndré Almeida 		ksft_test_result_pass("futex_waitv returned: %d %s\n",
102*9d57f7c7SAndré Almeida 				      res ? errno : res,
103*9d57f7c7SAndré Almeida 				      res ? strerror(errno) : "");
104*9d57f7c7SAndré Almeida 		ret = RET_FAIL;
105*9d57f7c7SAndré Almeida 	} else {
106*9d57f7c7SAndré Almeida 		ksft_test_result_pass("futex_waitv\n");
107*9d57f7c7SAndré Almeida 	}
108*9d57f7c7SAndré Almeida 
109*9d57f7c7SAndré Almeida 	ksft_print_cnts();
1102aa8470fSDarren Hart 	return ret;
1112aa8470fSDarren Hart }
112