1 /* SPDX-License-Identifier: GPL-2.0-or-later * 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /********************************************* 2 /****************************************************************************** 3 * 3 * 4 * Copyright © International Business Machi 4 * Copyright © International Business Machines Corp., 2009 5 * 5 * 6 * DESCRIPTION 6 * DESCRIPTION 7 * Glibc independent futex library for te 7 * Glibc independent futex library for testing kernel functionality. 8 * 8 * 9 * AUTHOR 9 * AUTHOR 10 * Darren Hart <dvhart@linux.intel.com> 10 * Darren Hart <dvhart@linux.intel.com> 11 * 11 * 12 * HISTORY 12 * HISTORY 13 * 2009-Nov-6: Initial version by Darren 13 * 2009-Nov-6: Initial version by Darren Hart <dvhart@linux.intel.com> 14 * 14 * 15 ********************************************* 15 *****************************************************************************/ 16 16 17 #ifndef _FUTEXTEST_H 17 #ifndef _FUTEXTEST_H 18 #define _FUTEXTEST_H 18 #define _FUTEXTEST_H 19 19 20 #include <unistd.h> 20 #include <unistd.h> 21 #include <sys/syscall.h> 21 #include <sys/syscall.h> 22 #include <sys/types.h> 22 #include <sys/types.h> 23 #include <linux/futex.h> 23 #include <linux/futex.h> 24 24 25 typedef volatile u_int32_t futex_t; 25 typedef volatile u_int32_t futex_t; 26 #define FUTEX_INITIALIZER 0 26 #define FUTEX_INITIALIZER 0 27 27 28 /* Define the newer op codes if the system hea 28 /* Define the newer op codes if the system header file is not up to date. */ 29 #ifndef FUTEX_WAIT_BITSET 29 #ifndef FUTEX_WAIT_BITSET 30 #define FUTEX_WAIT_BITSET 9 30 #define FUTEX_WAIT_BITSET 9 31 #endif 31 #endif 32 #ifndef FUTEX_WAKE_BITSET 32 #ifndef FUTEX_WAKE_BITSET 33 #define FUTEX_WAKE_BITSET 10 33 #define FUTEX_WAKE_BITSET 10 34 #endif 34 #endif 35 #ifndef FUTEX_WAIT_REQUEUE_PI 35 #ifndef FUTEX_WAIT_REQUEUE_PI 36 #define FUTEX_WAIT_REQUEUE_PI 11 36 #define FUTEX_WAIT_REQUEUE_PI 11 37 #endif 37 #endif 38 #ifndef FUTEX_CMP_REQUEUE_PI 38 #ifndef FUTEX_CMP_REQUEUE_PI 39 #define FUTEX_CMP_REQUEUE_PI 12 39 #define FUTEX_CMP_REQUEUE_PI 12 40 #endif 40 #endif 41 #ifndef FUTEX_WAIT_REQUEUE_PI_PRIVATE 41 #ifndef FUTEX_WAIT_REQUEUE_PI_PRIVATE 42 #define FUTEX_WAIT_REQUEUE_PI_PRIVATE (FUTEX 42 #define FUTEX_WAIT_REQUEUE_PI_PRIVATE (FUTEX_WAIT_REQUEUE_PI | \ 43 FUTEX 43 FUTEX_PRIVATE_FLAG) 44 #endif 44 #endif 45 #ifndef FUTEX_REQUEUE_PI_PRIVATE 45 #ifndef FUTEX_REQUEUE_PI_PRIVATE 46 #define FUTEX_CMP_REQUEUE_PI_PRIVATE (FUTEX 46 #define FUTEX_CMP_REQUEUE_PI_PRIVATE (FUTEX_CMP_REQUEUE_PI | \ 47 FUTEX 47 FUTEX_PRIVATE_FLAG) 48 #endif 48 #endif 49 49 50 /** 50 /** 51 * futex() - SYS_futex syscall wrapper 51 * futex() - SYS_futex syscall wrapper 52 * @uaddr: address of first futex 52 * @uaddr: address of first futex 53 * @op: futex op code 53 * @op: futex op code 54 * @val: typically expected value of ua 54 * @val: typically expected value of uaddr, but varies by op 55 * @timeout: typically an absolute struct t 55 * @timeout: typically an absolute struct timespec (except where noted 56 * otherwise). Overloaded by some 56 * otherwise). Overloaded by some ops 57 * @uaddr2: address of second futex for so 57 * @uaddr2: address of second futex for some ops\ 58 * @val3: varies by op 58 * @val3: varies by op 59 * @opflags: flags to be bitwise OR'd with 59 * @opflags: flags to be bitwise OR'd with op, such as FUTEX_PRIVATE_FLAG 60 * 60 * 61 * futex() is used by all the following futex 61 * futex() is used by all the following futex op wrappers. It can also be 62 * used for misuse and abuse testing. Generall 62 * used for misuse and abuse testing. Generally, the specific op wrappers 63 * should be used instead. It is a macro inste 63 * should be used instead. It is a macro instead of an static inline function as 64 * some of the types over overloaded (timeout 64 * some of the types over overloaded (timeout is used for nr_requeue for 65 * example). 65 * example). 66 * 66 * 67 * These argument descriptions are the default 67 * These argument descriptions are the defaults for all 68 * like-named arguments in the following wrapp 68 * like-named arguments in the following wrappers except where noted below. 69 */ 69 */ 70 #define futex(uaddr, op, val, timeout, uaddr2, 70 #define futex(uaddr, op, val, timeout, uaddr2, val3, opflags) \ 71 syscall(SYS_futex, uaddr, op | opflags 71 syscall(SYS_futex, uaddr, op | opflags, val, timeout, uaddr2, val3) 72 72 73 /** 73 /** 74 * futex_wait() - block on uaddr with optional 74 * futex_wait() - block on uaddr with optional timeout 75 * @timeout: relative timeout 75 * @timeout: relative timeout 76 */ 76 */ 77 static inline int 77 static inline int 78 futex_wait(futex_t *uaddr, futex_t val, struct 78 futex_wait(futex_t *uaddr, futex_t val, struct timespec *timeout, int opflags) 79 { 79 { 80 return futex(uaddr, FUTEX_WAIT, val, t 80 return futex(uaddr, FUTEX_WAIT, val, timeout, NULL, 0, opflags); 81 } 81 } 82 82 83 /** 83 /** 84 * futex_wake() - wake one or more tasks block 84 * futex_wake() - wake one or more tasks blocked on uaddr 85 * @nr_wake: wake up to this many tasks 85 * @nr_wake: wake up to this many tasks 86 */ 86 */ 87 static inline int 87 static inline int 88 futex_wake(futex_t *uaddr, int nr_wake, int op 88 futex_wake(futex_t *uaddr, int nr_wake, int opflags) 89 { 89 { 90 return futex(uaddr, FUTEX_WAKE, nr_wak 90 return futex(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags); 91 } 91 } 92 92 93 /** 93 /** 94 * futex_wait_bitset() - block on uaddr with b 94 * futex_wait_bitset() - block on uaddr with bitset 95 * @bitset: bitset to be used with futex_w 95 * @bitset: bitset to be used with futex_wake_bitset 96 */ 96 */ 97 static inline int 97 static inline int 98 futex_wait_bitset(futex_t *uaddr, futex_t val, 98 futex_wait_bitset(futex_t *uaddr, futex_t val, struct timespec *timeout, 99 u_int32_t bitset, int opflag 99 u_int32_t bitset, int opflags) 100 { 100 { 101 return futex(uaddr, FUTEX_WAIT_BITSET, 101 return futex(uaddr, FUTEX_WAIT_BITSET, val, timeout, NULL, bitset, 102 opflags); 102 opflags); 103 } 103 } 104 104 105 /** 105 /** 106 * futex_wake_bitset() - wake one or more task 106 * futex_wake_bitset() - wake one or more tasks blocked on uaddr with bitset 107 * @bitset: bitset to compare with that us 107 * @bitset: bitset to compare with that used in futex_wait_bitset 108 */ 108 */ 109 static inline int 109 static inline int 110 futex_wake_bitset(futex_t *uaddr, int nr_wake, 110 futex_wake_bitset(futex_t *uaddr, int nr_wake, u_int32_t bitset, int opflags) 111 { 111 { 112 return futex(uaddr, FUTEX_WAKE_BITSET, 112 return futex(uaddr, FUTEX_WAKE_BITSET, nr_wake, NULL, NULL, bitset, 113 opflags); 113 opflags); 114 } 114 } 115 115 116 /** 116 /** 117 * futex_lock_pi() - block on uaddr as a PI mu 117 * futex_lock_pi() - block on uaddr as a PI mutex 118 * @detect: whether (1) or not (0) to perf 118 * @detect: whether (1) or not (0) to perform deadlock detection 119 */ 119 */ 120 static inline int 120 static inline int 121 futex_lock_pi(futex_t *uaddr, struct timespec 121 futex_lock_pi(futex_t *uaddr, struct timespec *timeout, int detect, 122 int opflags) 122 int opflags) 123 { 123 { 124 return futex(uaddr, FUTEX_LOCK_PI, det 124 return futex(uaddr, FUTEX_LOCK_PI, detect, timeout, NULL, 0, opflags); 125 } 125 } 126 126 127 /** 127 /** 128 * futex_unlock_pi() - release uaddr as a PI m 128 * futex_unlock_pi() - release uaddr as a PI mutex, waking the top waiter 129 */ 129 */ 130 static inline int 130 static inline int 131 futex_unlock_pi(futex_t *uaddr, int opflags) 131 futex_unlock_pi(futex_t *uaddr, int opflags) 132 { 132 { 133 return futex(uaddr, FUTEX_UNLOCK_PI, 0 133 return futex(uaddr, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0, opflags); 134 } 134 } 135 135 136 /** 136 /** 137 * futex_wake_op() - FIXME: COME UP WITH A GOO 137 * futex_wake_op() - FIXME: COME UP WITH A GOOD ONE LINE DESCRIPTION 138 */ 138 */ 139 static inline int 139 static inline int 140 futex_wake_op(futex_t *uaddr, futex_t *uaddr2, 140 futex_wake_op(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_wake2, 141 int wake_op, int opflags) 141 int wake_op, int opflags) 142 { 142 { 143 return futex(uaddr, FUTEX_WAKE_OP, nr_ 143 return futex(uaddr, FUTEX_WAKE_OP, nr_wake, nr_wake2, uaddr2, wake_op, 144 opflags); 144 opflags); 145 } 145 } 146 146 147 /** 147 /** 148 * futex_requeue() - requeue without expected 148 * futex_requeue() - requeue without expected value comparison, deprecated 149 * @nr_wake: wake up to this many tasks 149 * @nr_wake: wake up to this many tasks 150 * @nr_requeue: requeue up to this many tasks 150 * @nr_requeue: requeue up to this many tasks 151 * 151 * 152 * Due to its inherently racy implementation, 152 * Due to its inherently racy implementation, futex_requeue() is deprecated in 153 * favor of futex_cmp_requeue(). 153 * favor of futex_cmp_requeue(). 154 */ 154 */ 155 static inline int 155 static inline int 156 futex_requeue(futex_t *uaddr, futex_t *uaddr2, 156 futex_requeue(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_requeue, 157 int opflags) 157 int opflags) 158 { 158 { 159 return futex(uaddr, FUTEX_REQUEUE, nr_ 159 return futex(uaddr, FUTEX_REQUEUE, nr_wake, nr_requeue, uaddr2, 0, 160 opflags); 160 opflags); 161 } 161 } 162 162 163 /** 163 /** 164 * futex_cmp_requeue() - requeue tasks from ua 164 * futex_cmp_requeue() - requeue tasks from uaddr to uaddr2 165 * @nr_wake: wake up to this many tasks 165 * @nr_wake: wake up to this many tasks 166 * @nr_requeue: requeue up to this many tasks 166 * @nr_requeue: requeue up to this many tasks 167 */ 167 */ 168 static inline int 168 static inline int 169 futex_cmp_requeue(futex_t *uaddr, futex_t val, 169 futex_cmp_requeue(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake, 170 int nr_requeue, int opflags) 170 int nr_requeue, int opflags) 171 { 171 { 172 return futex(uaddr, FUTEX_CMP_REQUEUE, 172 return futex(uaddr, FUTEX_CMP_REQUEUE, nr_wake, nr_requeue, uaddr2, 173 val, opflags); 173 val, opflags); 174 } 174 } 175 175 176 /** 176 /** 177 * futex_wait_requeue_pi() - block on uaddr an 177 * futex_wait_requeue_pi() - block on uaddr and prepare to requeue to uaddr2 178 * @uaddr: non-PI futex source 178 * @uaddr: non-PI futex source 179 * @uaddr2: PI futex target 179 * @uaddr2: PI futex target 180 * 180 * 181 * This is the first half of the requeue_pi me 181 * This is the first half of the requeue_pi mechanism. It shall always be 182 * paired with futex_cmp_requeue_pi(). 182 * paired with futex_cmp_requeue_pi(). 183 */ 183 */ 184 static inline int 184 static inline int 185 futex_wait_requeue_pi(futex_t *uaddr, futex_t 185 futex_wait_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2, 186 struct timespec *timeout 186 struct timespec *timeout, int opflags) 187 { 187 { 188 return futex(uaddr, FUTEX_WAIT_REQUEUE 188 return futex(uaddr, FUTEX_WAIT_REQUEUE_PI, val, timeout, uaddr2, 0, 189 opflags); 189 opflags); 190 } 190 } 191 191 192 /** 192 /** 193 * futex_cmp_requeue_pi() - requeue tasks from 193 * futex_cmp_requeue_pi() - requeue tasks from uaddr to uaddr2 (PI aware) 194 * @uaddr: non-PI futex source 194 * @uaddr: non-PI futex source 195 * @uaddr2: PI futex target 195 * @uaddr2: PI futex target 196 * @nr_wake: wake up to this many tasks 196 * @nr_wake: wake up to this many tasks 197 * @nr_requeue: requeue up to this many tasks 197 * @nr_requeue: requeue up to this many tasks 198 */ 198 */ 199 static inline int 199 static inline int 200 futex_cmp_requeue_pi(futex_t *uaddr, futex_t v 200 futex_cmp_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake, 201 int nr_requeue, int opfla 201 int nr_requeue, int opflags) 202 { 202 { 203 return futex(uaddr, FUTEX_CMP_REQUEUE_ 203 return futex(uaddr, FUTEX_CMP_REQUEUE_PI, nr_wake, nr_requeue, uaddr2, 204 val, opflags); 204 val, opflags); 205 } 205 } 206 206 207 /** 207 /** 208 * futex_cmpxchg() - atomic compare and exchan 208 * futex_cmpxchg() - atomic compare and exchange 209 * @uaddr: The address of the futex to be 209 * @uaddr: The address of the futex to be modified 210 * @oldval: The expected value of the fute 210 * @oldval: The expected value of the futex 211 * @newval: The new value to try and assig 211 * @newval: The new value to try and assign the futex 212 * 212 * 213 * Implement cmpxchg using gcc atomic builtins 213 * Implement cmpxchg using gcc atomic builtins. 214 * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc 214 * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html 215 * 215 * 216 * Return the old futex value. 216 * Return the old futex value. 217 */ 217 */ 218 static inline u_int32_t 218 static inline u_int32_t 219 futex_cmpxchg(futex_t *uaddr, u_int32_t oldval 219 futex_cmpxchg(futex_t *uaddr, u_int32_t oldval, u_int32_t newval) 220 { 220 { 221 return __sync_val_compare_and_swap(uad 221 return __sync_val_compare_and_swap(uaddr, oldval, newval); 222 } 222 } 223 223 224 /** 224 /** 225 * futex_dec() - atomic decrement of the futex 225 * futex_dec() - atomic decrement of the futex value 226 * @uaddr: The address of the futex to be 226 * @uaddr: The address of the futex to be modified 227 * 227 * 228 * Return the new futex value. 228 * Return the new futex value. 229 */ 229 */ 230 static inline u_int32_t 230 static inline u_int32_t 231 futex_dec(futex_t *uaddr) 231 futex_dec(futex_t *uaddr) 232 { 232 { 233 return __sync_sub_and_fetch(uaddr, 1); 233 return __sync_sub_and_fetch(uaddr, 1); 234 } 234 } 235 235 236 /** 236 /** 237 * futex_inc() - atomic increment of the futex 237 * futex_inc() - atomic increment of the futex value 238 * @uaddr: the address of the futex to be 238 * @uaddr: the address of the futex to be modified 239 * 239 * 240 * Return the new futex value. 240 * Return the new futex value. 241 */ 241 */ 242 static inline u_int32_t 242 static inline u_int32_t 243 futex_inc(futex_t *uaddr) 243 futex_inc(futex_t *uaddr) 244 { 244 { 245 return __sync_add_and_fetch(uaddr, 1); 245 return __sync_add_and_fetch(uaddr, 1); 246 } 246 } 247 247 248 /** 248 /** 249 * futex_set() - atomic decrement of the futex 249 * futex_set() - atomic decrement of the futex value 250 * @uaddr: the address of the futex to be 250 * @uaddr: the address of the futex to be modified 251 * @newval: New value for the atomic_t 251 * @newval: New value for the atomic_t 252 * 252 * 253 * Return the new futex value. 253 * Return the new futex value. 254 */ 254 */ 255 static inline u_int32_t 255 static inline u_int32_t 256 futex_set(futex_t *uaddr, u_int32_t newval) 256 futex_set(futex_t *uaddr, u_int32_t newval) 257 { 257 { 258 *uaddr = newval; 258 *uaddr = newval; 259 return newval; 259 return newval; 260 } 260 } 261 261 262 #endif 262 #endif 263 263
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.