• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #include <stdint.h>
25 #include <limits.h>
26 #include <errno.h>
27 #include <sys/syscall.h>
28 #include <linux/futex.h>
29 
30 #include "test.h"
31 #include "safe_macros.h"
32 #include "lapi/futex.h"
33 
34 #define DEFAULT_MSEC_TIMEOUT 10000
35 
36 futex_t *tst_futexes;
37 unsigned int tst_max_futexes;
38 
tst_checkpoint_init(const char * file,const int lineno,void (* cleanup_fn)(void))39 void tst_checkpoint_init(const char *file, const int lineno,
40                          void (*cleanup_fn)(void))
41 {
42 	int fd;
43 	unsigned int page_size;
44 
45 	if (tst_futexes) {
46 		tst_brkm(TBROK, cleanup_fn,
47 		         "%s: %d checkpoints already initialized",
48 		         file, lineno);
49 		return;
50 	}
51 
52 	/*
53 	 * The parent test process is responsible for creating the temporary
54 	 * directory and therefore must pass non-zero cleanup (to remove the
55 	 * directory if something went wrong).
56 	 *
57 	 * We cannot do this check unconditionally because if we need to init
58 	 * the checkpoint from a binary that was started by exec() the
59 	 * tst_tmpdir_created() will return false because the tmpdir was
60 	 * created by parent. In this case we expect the subprogram can call
61 	 * the init as a first function with NULL as cleanup function.
62 	 */
63 	if (cleanup_fn && !tst_tmpdir_created()) {
64 		tst_brkm(TBROK, cleanup_fn,
65 		         "%s:%d You have to create test temporary directory "
66 		         "first (call tst_tmpdir())", file, lineno);
67 		return;
68 	}
69 
70 	page_size = getpagesize();
71 
72 	fd = SAFE_OPEN(cleanup_fn, "checkpoint_futex_base_file",
73 	               O_RDWR | O_CREAT, 0666);
74 
75 	SAFE_FTRUNCATE(cleanup_fn, fd, page_size);
76 
77 	tst_futexes = SAFE_MMAP(cleanup_fn, NULL, page_size,
78 	                    PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
79 
80 	tst_max_futexes = page_size / sizeof(uint32_t);
81 
82 	SAFE_CLOSE(cleanup_fn, fd);
83 }
84 
tst_checkpoint_wait(unsigned int id,unsigned int msec_timeout)85 int tst_checkpoint_wait(unsigned int id, unsigned int msec_timeout)
86 {
87 	struct timespec timeout;
88 
89 	if (id >= tst_max_futexes) {
90 		errno = EOVERFLOW;
91 		return -1;
92 	}
93 
94 	timeout.tv_sec = msec_timeout/1000;
95 	timeout.tv_nsec = (msec_timeout%1000) * 1000000;
96 
97 	return syscall(SYS_futex, &tst_futexes[id], FUTEX_WAIT,
98 		       tst_futexes[id], &timeout);
99 }
100 
tst_checkpoint_wake(unsigned int id,unsigned int nr_wake,unsigned int msec_timeout)101 int tst_checkpoint_wake(unsigned int id, unsigned int nr_wake,
102                         unsigned int msec_timeout)
103 {
104 	unsigned int msecs = 0, waked = 0;
105 
106 	if (id >= tst_max_futexes) {
107 		errno = EOVERFLOW;
108 		return -1;
109 	}
110 
111 	for (;;) {
112 		waked += syscall(SYS_futex, &tst_futexes[id], FUTEX_WAKE,
113 				 INT_MAX, NULL);
114 
115 		if (waked == nr_wake)
116 			break;
117 
118 		usleep(1000);
119 		msecs++;
120 
121 		if (msecs >= msec_timeout) {
122 			errno = ETIMEDOUT;
123 			return -1;
124 		}
125 	}
126 
127 	return 0;
128 }
129 
tst_safe_checkpoint_wait(const char * file,const int lineno,void (* cleanup_fn)(void),unsigned int id,unsigned int msec_timeout)130 void tst_safe_checkpoint_wait(const char *file, const int lineno,
131                               void (*cleanup_fn)(void), unsigned int id,
132 			      unsigned int msec_timeout)
133 {
134 	int ret;
135 
136 	if (!msec_timeout)
137 		msec_timeout = DEFAULT_MSEC_TIMEOUT;
138 
139 	ret = tst_checkpoint_wait(id, msec_timeout);
140 
141 	if (ret) {
142 		tst_brkm(TBROK | TERRNO, cleanup_fn,
143 		         "%s:%d: tst_checkpoint_wait(%u, %i)",
144 		         file, lineno, id, msec_timeout);
145 	}
146 }
147 
tst_safe_checkpoint_wake(const char * file,const int lineno,void (* cleanup_fn)(void),unsigned int id,unsigned int nr_wake)148 void tst_safe_checkpoint_wake(const char *file, const int lineno,
149                               void (*cleanup_fn)(void), unsigned int id,
150                               unsigned int nr_wake)
151 {
152 	int ret = tst_checkpoint_wake(id, nr_wake, DEFAULT_MSEC_TIMEOUT);
153 
154 	if (ret) {
155 		tst_brkm(TBROK | TERRNO, cleanup_fn,
156 		         "%s:%d: tst_checkpoint_wake(%u, %u, %i)",
157 		         file, lineno, id, nr_wake, DEFAULT_MSEC_TIMEOUT);
158 	}
159 }
160