1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
3 *
4 * Copyright FUJITSU LIMITED 2010
5 * Copyright KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
6 *
7 * DESCRIPTION
8 * Internally, Futex has two handling mode, anon and file. The private file
9 * mapping is special. At first it behave as file, but after write anything
10 * it behave as anon. This test is intent to test such case.
11 *
12 * AUTHOR
13 * KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
14 *
15 * HISTORY
16 * 2010-Jan-6: Initial version by KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
17 *
18 *****************************************************************************/
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <syscall.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <linux/futex.h>
26 #include <pthread.h>
27 #include <libgen.h>
28 #include <signal.h>
29
30 #include "futextest.h"
31 #include "kselftest_harness.h"
32
33 #define PAGE_SZ 4096
34
35 char pad[PAGE_SZ] = {1};
36 futex_t val = 1;
37 char pad2[PAGE_SZ] = {1};
38
39 #define WAKE_WAIT_US 3000000
40 struct timespec wait_timeout = { .tv_sec = 5, .tv_nsec = 0};
41
thr_futex_wait(void * arg)42 void *thr_futex_wait(void *arg)
43 {
44 int ret;
45
46 ksft_print_dbg_msg("futex wait\n");
47 ret = futex_wait(&val, 1, &wait_timeout, 0);
48 if (ret && errno != EWOULDBLOCK && errno != ETIMEDOUT)
49 ksft_exit_fail_msg("futex error.\n");
50
51 if (ret && errno == ETIMEDOUT)
52 ksft_exit_fail_msg("waiter timedout\n");
53
54 ksft_print_dbg_msg("futex_wait: ret = %d, errno = %d\n", ret, errno);
55
56 return NULL;
57 }
58
TEST(wait_private_mapped_file)59 TEST(wait_private_mapped_file)
60 {
61 pthread_t thr;
62 int res;
63
64 res = pthread_create(&thr, NULL, thr_futex_wait, NULL);
65 if (res < 0)
66 ksft_exit_fail_msg("pthread_create error\n");
67
68 ksft_print_dbg_msg("wait a while\n");
69 usleep(WAKE_WAIT_US);
70 val = 2;
71 res = futex_wake(&val, 1, 0);
72 ksft_print_dbg_msg("futex_wake %d\n", res);
73 if (res != 1)
74 ksft_exit_fail_msg("FUTEX_WAKE didn't find the waiting thread.\n");
75
76 ksft_print_dbg_msg("join\n");
77 pthread_join(thr, NULL);
78
79 ksft_test_result_pass("wait_private_mapped_file");
80 }
81
82 TEST_HARNESS_MAIN
83