Path: blob/master/tools/testing/selftests/futex/include/futextest.h
29270 views
/* SPDX-License-Identifier: GPL-2.0-or-later */1/******************************************************************************2*3* Copyright © International Business Machines Corp., 20094*5* DESCRIPTION6* Glibc independent futex library for testing kernel functionality.7*8* AUTHOR9* Darren Hart <[email protected]>10*11* HISTORY12* 2009-Nov-6: Initial version by Darren Hart <[email protected]>13*14*****************************************************************************/1516#ifndef _FUTEXTEST_H17#define _FUTEXTEST_H1819#include <unistd.h>20#include <sys/syscall.h>21#include <sys/types.h>22#include <linux/futex.h>2324typedef volatile u_int32_t futex_t;25#define FUTEX_INITIALIZER 02627/* Define the newer op codes if the system header file is not up to date. */28#ifndef FUTEX_WAIT_BITSET29#define FUTEX_WAIT_BITSET 930#endif31#ifndef FUTEX_WAKE_BITSET32#define FUTEX_WAKE_BITSET 1033#endif34#ifndef FUTEX_WAIT_REQUEUE_PI35#define FUTEX_WAIT_REQUEUE_PI 1136#endif37#ifndef FUTEX_CMP_REQUEUE_PI38#define FUTEX_CMP_REQUEUE_PI 1239#endif40#ifndef FUTEX_WAIT_REQUEUE_PI_PRIVATE41#define FUTEX_WAIT_REQUEUE_PI_PRIVATE (FUTEX_WAIT_REQUEUE_PI | \42FUTEX_PRIVATE_FLAG)43#endif44#ifndef FUTEX_REQUEUE_PI_PRIVATE45#define FUTEX_CMP_REQUEUE_PI_PRIVATE (FUTEX_CMP_REQUEUE_PI | \46FUTEX_PRIVATE_FLAG)47#endif4849/*50* SYS_futex is expected from system C library, in glibc some 32-bit51* architectures (e.g. RV32) are using 64-bit time_t, therefore it doesn't have52* SYS_futex defined but just SYS_futex_time64. Define SYS_futex as53* SYS_futex_time64 in this situation to ensure the compilation and the54* compatibility.55*/56#if !defined(SYS_futex) && defined(SYS_futex_time64)57#define SYS_futex SYS_futex_time6458#endif5960/*61* On 32bit systems if we use "-D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64" or if62* we are using a newer compiler then the size of the timestamps will be 64bit,63* however, the SYS_futex will still point to the 32bit futex system call.64*/65#if __SIZEOF_POINTER__ == 4 && defined(SYS_futex_time64) && \66defined(_TIME_BITS) && _TIME_BITS == 6467# undef SYS_futex68# define SYS_futex SYS_futex_time6469#endif7071/**72* futex() - SYS_futex syscall wrapper73* @uaddr: address of first futex74* @op: futex op code75* @val: typically expected value of uaddr, but varies by op76* @timeout: typically an absolute struct timespec (except where noted77* otherwise). Overloaded by some ops78* @uaddr2: address of second futex for some ops\79* @val3: varies by op80* @opflags: flags to be bitwise OR'd with op, such as FUTEX_PRIVATE_FLAG81*82* futex() is used by all the following futex op wrappers. It can also be83* used for misuse and abuse testing. Generally, the specific op wrappers84* should be used instead. It is a macro instead of an static inline function as85* some of the types over overloaded (timeout is used for nr_requeue for86* example).87*88* These argument descriptions are the defaults for all89* like-named arguments in the following wrappers except where noted below.90*/91#define futex(uaddr, op, val, timeout, uaddr2, val3, opflags) \92syscall(SYS_futex, uaddr, op | opflags, val, timeout, uaddr2, val3)9394/**95* futex_wait() - block on uaddr with optional timeout96* @timeout: relative timeout97*/98static inline int99futex_wait(futex_t *uaddr, futex_t val, struct timespec *timeout, int opflags)100{101return futex(uaddr, FUTEX_WAIT, val, timeout, NULL, 0, opflags);102}103104/**105* futex_wake() - wake one or more tasks blocked on uaddr106* @nr_wake: wake up to this many tasks107*/108static inline int109futex_wake(futex_t *uaddr, int nr_wake, int opflags)110{111return futex(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags);112}113114/**115* futex_wait_bitset() - block on uaddr with bitset116* @bitset: bitset to be used with futex_wake_bitset117*/118static inline int119futex_wait_bitset(futex_t *uaddr, futex_t val, struct timespec *timeout,120u_int32_t bitset, int opflags)121{122return futex(uaddr, FUTEX_WAIT_BITSET, val, timeout, NULL, bitset,123opflags);124}125126/**127* futex_wake_bitset() - wake one or more tasks blocked on uaddr with bitset128* @bitset: bitset to compare with that used in futex_wait_bitset129*/130static inline int131futex_wake_bitset(futex_t *uaddr, int nr_wake, u_int32_t bitset, int opflags)132{133return futex(uaddr, FUTEX_WAKE_BITSET, nr_wake, NULL, NULL, bitset,134opflags);135}136137/**138* futex_lock_pi() - block on uaddr as a PI mutex139* @detect: whether (1) or not (0) to perform deadlock detection140*/141static inline int142futex_lock_pi(futex_t *uaddr, struct timespec *timeout, int detect,143int opflags)144{145return futex(uaddr, FUTEX_LOCK_PI, detect, timeout, NULL, 0, opflags);146}147148/**149* futex_unlock_pi() - release uaddr as a PI mutex, waking the top waiter150*/151static inline int152futex_unlock_pi(futex_t *uaddr, int opflags)153{154return futex(uaddr, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0, opflags);155}156157/**158* futex_wake_op() - FIXME: COME UP WITH A GOOD ONE LINE DESCRIPTION159*/160static inline int161futex_wake_op(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_wake2,162int wake_op, int opflags)163{164return futex(uaddr, FUTEX_WAKE_OP, nr_wake, nr_wake2, uaddr2, wake_op,165opflags);166}167168/**169* futex_requeue() - requeue without expected value comparison, deprecated170* @nr_wake: wake up to this many tasks171* @nr_requeue: requeue up to this many tasks172*173* Due to its inherently racy implementation, futex_requeue() is deprecated in174* favor of futex_cmp_requeue().175*/176static inline int177futex_requeue(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_requeue,178int opflags)179{180return futex(uaddr, FUTEX_REQUEUE, nr_wake, nr_requeue, uaddr2, 0,181opflags);182}183184/**185* futex_cmp_requeue() - requeue tasks from uaddr to uaddr2186* @nr_wake: wake up to this many tasks187* @nr_requeue: requeue up to this many tasks188*/189static inline int190futex_cmp_requeue(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake,191int nr_requeue, int opflags)192{193return futex(uaddr, FUTEX_CMP_REQUEUE, nr_wake, nr_requeue, uaddr2,194val, opflags);195}196197/**198* futex_wait_requeue_pi() - block on uaddr and prepare to requeue to uaddr2199* @uaddr: non-PI futex source200* @uaddr2: PI futex target201*202* This is the first half of the requeue_pi mechanism. It shall always be203* paired with futex_cmp_requeue_pi().204*/205static inline int206futex_wait_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2,207struct timespec *timeout, int opflags)208{209return futex(uaddr, FUTEX_WAIT_REQUEUE_PI, val, timeout, uaddr2, 0,210opflags);211}212213/**214* futex_cmp_requeue_pi() - requeue tasks from uaddr to uaddr2 (PI aware)215* @uaddr: non-PI futex source216* @uaddr2: PI futex target217* @nr_wake: wake up to this many tasks218* @nr_requeue: requeue up to this many tasks219*/220static inline int221futex_cmp_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake,222int nr_requeue, int opflags)223{224return futex(uaddr, FUTEX_CMP_REQUEUE_PI, nr_wake, nr_requeue, uaddr2,225val, opflags);226}227228/**229* futex_cmpxchg() - atomic compare and exchange230* @uaddr: The address of the futex to be modified231* @oldval: The expected value of the futex232* @newval: The new value to try and assign the futex233*234* Implement cmpxchg using gcc atomic builtins.235* http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html236*237* Return the old futex value.238*/239static inline u_int32_t240futex_cmpxchg(futex_t *uaddr, u_int32_t oldval, u_int32_t newval)241{242return __sync_val_compare_and_swap(uaddr, oldval, newval);243}244245/**246* futex_dec() - atomic decrement of the futex value247* @uaddr: The address of the futex to be modified248*249* Return the new futex value.250*/251static inline u_int32_t252futex_dec(futex_t *uaddr)253{254return __sync_sub_and_fetch(uaddr, 1);255}256257/**258* futex_inc() - atomic increment of the futex value259* @uaddr: the address of the futex to be modified260*261* Return the new futex value.262*/263static inline u_int32_t264futex_inc(futex_t *uaddr)265{266return __sync_add_and_fetch(uaddr, 1);267}268269/**270* futex_set() - atomic decrement of the futex value271* @uaddr: the address of the futex to be modified272* @newval: New value for the atomic_t273*274* Return the new futex value.275*/276static inline u_int32_t277futex_set(futex_t *uaddr, u_int32_t newval)278{279*uaddr = newval;280return newval;281}282283#endif284285286