Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 2 | * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz> |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 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 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | #include <limits.h> |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 26 | #include <errno.h> |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 27 | #include <sys/syscall.h> |
| 28 | #include <linux/futex.h> |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 29 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 30 | #include "test.h" |
| 31 | #include "safe_macros.h" |
Stanislav Kholmanskikh | 5571228 | 2015-05-26 12:16:16 +0300 | [diff] [blame] | 32 | #include "lapi/futex.h" |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 33 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 34 | #define DEFAULT_MSEC_TIMEOUT 10000 |
| 35 | |
Stanislav Kholmanskikh | 5702765 | 2015-05-26 13:21:10 +0300 | [diff] [blame] | 36 | futex_t *tst_futexes; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 37 | unsigned int tst_max_futexes; |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 38 | |
| 39 | void tst_checkpoint_init(const char *file, const int lineno, |
| 40 | void (*cleanup_fn)(void)) |
Stanislav Kholmanskikh | cb80f40 | 2013-11-14 10:37:57 +0400 | [diff] [blame] | 41 | { |
| 42 | int fd; |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 43 | unsigned int page_size; |
Stanislav Kholmanskikh | cb80f40 | 2013-11-14 10:37:57 +0400 | [diff] [blame] | 44 | |
Stanislav Kholmanskikh | 5702765 | 2015-05-26 13:21:10 +0300 | [diff] [blame] | 45 | if (tst_futexes) { |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 46 | tst_brkm(TBROK, cleanup_fn, |
Petr Vorel | f182839 | 2017-12-05 10:44:43 +0100 | [diff] [blame] | 47 | "%s: %d checkpoints already initialized", |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 48 | file, lineno); |
Cyril Hrubis | d101cab | 2017-02-14 11:48:46 +0100 | [diff] [blame] | 49 | return; |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 50 | } |
Stanislav Kholmanskikh | cb80f40 | 2013-11-14 10:37:57 +0400 | [diff] [blame] | 51 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 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); |
Cyril Hrubis | d101cab | 2017-02-14 11:48:46 +0100 | [diff] [blame] | 67 | return; |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 68 | } |
Stanislav Kholmanskikh | cb80f40 | 2013-11-14 10:37:57 +0400 | [diff] [blame] | 69 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 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 | |
Stanislav Kholmanskikh | 5702765 | 2015-05-26 13:21:10 +0300 | [diff] [blame] | 77 | tst_futexes = SAFE_MMAP(cleanup_fn, NULL, page_size, |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 78 | PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 79 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 80 | tst_max_futexes = page_size / sizeof(uint32_t); |
| 81 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 82 | SAFE_CLOSE(cleanup_fn, fd); |
| 83 | } |
| 84 | |
| 85 | int tst_checkpoint_wait(unsigned int id, unsigned int msec_timeout) |
| 86 | { |
| 87 | struct timespec timeout; |
Cyril Hrubis | 07debb7 | 2019-03-18 14:42:27 +0100 | [diff] [blame] | 88 | int ret; |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 89 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 90 | if (id >= tst_max_futexes) { |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 91 | errno = EOVERFLOW; |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | timeout.tv_sec = msec_timeout/1000; |
| 96 | timeout.tv_nsec = (msec_timeout%1000) * 1000000; |
| 97 | |
Cyril Hrubis | 07debb7 | 2019-03-18 14:42:27 +0100 | [diff] [blame] | 98 | do { |
| 99 | ret = syscall(SYS_futex, &tst_futexes[id], FUTEX_WAIT, |
| 100 | tst_futexes[id], &timeout); |
| 101 | } while (ret == -1 && errno == EINTR); |
| 102 | |
| 103 | return ret; |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | int tst_checkpoint_wake(unsigned int id, unsigned int nr_wake, |
| 107 | unsigned int msec_timeout) |
| 108 | { |
| 109 | unsigned int msecs = 0, waked = 0; |
| 110 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 111 | if (id >= tst_max_futexes) { |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 112 | errno = EOVERFLOW; |
| 113 | return -1; |
| 114 | } |
| 115 | |
Jan Stancek | 768157b | 2016-05-11 13:36:45 +0200 | [diff] [blame] | 116 | for (;;) { |
Stanislav Kholmanskikh | 5702765 | 2015-05-26 13:21:10 +0300 | [diff] [blame] | 117 | waked += syscall(SYS_futex, &tst_futexes[id], FUTEX_WAKE, |
| 118 | INT_MAX, NULL); |
Jan Stancek | 768157b | 2016-05-11 13:36:45 +0200 | [diff] [blame] | 119 | |
| 120 | if (waked == nr_wake) |
| 121 | break; |
| 122 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 123 | usleep(1000); |
| 124 | msecs++; |
| 125 | |
| 126 | if (msecs >= msec_timeout) { |
| 127 | errno = ETIMEDOUT; |
Stanislav Kholmanskikh | cb80f40 | 2013-11-14 10:37:57 +0400 | [diff] [blame] | 128 | return -1; |
| 129 | } |
Jan Stancek | 768157b | 2016-05-11 13:36:45 +0200 | [diff] [blame] | 130 | } |
Stanislav Kholmanskikh | cb80f40 | 2013-11-14 10:37:57 +0400 | [diff] [blame] | 131 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 132 | return 0; |
Stanislav Kholmanskikh | cb80f40 | 2013-11-14 10:37:57 +0400 | [diff] [blame] | 133 | } |
| 134 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 135 | void tst_safe_checkpoint_wait(const char *file, const int lineno, |
Cyril Hrubis | 554c270 | 2017-02-13 14:51:23 +0100 | [diff] [blame] | 136 | void (*cleanup_fn)(void), unsigned int id, |
| 137 | unsigned int msec_timeout) |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 138 | { |
Cyril Hrubis | 554c270 | 2017-02-13 14:51:23 +0100 | [diff] [blame] | 139 | int ret; |
| 140 | |
| 141 | if (!msec_timeout) |
| 142 | msec_timeout = DEFAULT_MSEC_TIMEOUT; |
| 143 | |
| 144 | ret = tst_checkpoint_wait(id, msec_timeout); |
Matus Marhefka | 5b03150 | 2014-10-02 13:32:33 +0200 | [diff] [blame] | 145 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 146 | if (ret) { |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 147 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 148 | "%s:%d: tst_checkpoint_wait(%u, %i)", |
Cyril Hrubis | 554c270 | 2017-02-13 14:51:23 +0100 | [diff] [blame] | 149 | file, lineno, id, msec_timeout); |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 150 | } |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 153 | void tst_safe_checkpoint_wake(const char *file, const int lineno, |
| 154 | void (*cleanup_fn)(void), unsigned int id, |
| 155 | unsigned int nr_wake) |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 156 | { |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 157 | int ret = tst_checkpoint_wake(id, nr_wake, DEFAULT_MSEC_TIMEOUT); |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 158 | |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 159 | if (ret) { |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 160 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Cyril Hrubis | 9f136a4 | 2015-02-17 17:16:09 +0100 | [diff] [blame] | 161 | "%s:%d: tst_checkpoint_wake(%u, %u, %i)", |
| 162 | file, lineno, id, nr_wake, DEFAULT_MSEC_TIMEOUT); |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 163 | } |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 164 | } |