/* SPDX-License-Identifier: GPL-2.0 */1/*2* Glibc independent futex library for testing kernel functionality.3* Shamelessly stolen from Darren Hart <[email protected]>4* http://git.kernel.org/cgit/linux/kernel/git/dvhart/futextest.git/5*/67#ifndef _FUTEX_H8#define _FUTEX_H910#include <stdbool.h>11#include <unistd.h>12#include <sys/syscall.h>13#include <sys/types.h>14#include <linux/futex.h>1516struct bench_futex_parameters {17bool silent;18bool fshared;19bool mlockall;20bool multi; /* lock-pi */21bool pi; /* requeue-pi */22bool broadcast; /* requeue */23unsigned int runtime; /* seconds*/24unsigned int nthreads;25unsigned int nfutexes;26unsigned int nwakes;27unsigned int nrequeue;28int nbuckets;29};3031/**32* futex_syscall() - SYS_futex syscall wrapper33* @uaddr: address of first futex34* @op: futex op code35* @val: typically expected value of uaddr, but varies by op36* @timeout: typically an absolute struct timespec (except where noted37* otherwise). Overloaded by some ops38* @uaddr2: address of second futex for some ops39* @val3: varies by op40* @opflags: flags to be bitwise OR'd with op, such as FUTEX_PRIVATE_FLAG41*42* futex_syscall() is used by all the following futex op wrappers. It can also be43* used for misuse and abuse testing. Generally, the specific op wrappers44* should be used instead.45*46* These argument descriptions are the defaults for all47* like-named arguments in the following wrappers except where noted below.48*/49static inline int50futex_syscall(volatile u_int32_t *uaddr, int op, u_int32_t val, struct timespec *timeout,51volatile u_int32_t *uaddr2, int val3, int opflags)52{53return syscall(SYS_futex, uaddr, op | opflags, val, timeout, uaddr2, val3);54}5556static inline int57futex_syscall_nr_requeue(volatile u_int32_t *uaddr, int op, u_int32_t val, int nr_requeue,58volatile u_int32_t *uaddr2, int val3, int opflags)59{60return syscall(SYS_futex, uaddr, op | opflags, val, nr_requeue, uaddr2, val3);61}6263/**64* futex_wait() - block on uaddr with optional timeout65* @timeout: relative timeout66*/67static inline int68futex_wait(u_int32_t *uaddr, u_int32_t val, struct timespec *timeout, int opflags)69{70return futex_syscall(uaddr, FUTEX_WAIT, val, timeout, NULL, 0, opflags);71}7273/**74* futex_wake() - wake one or more tasks blocked on uaddr75* @nr_wake: wake up to this many tasks76*/77static inline int78futex_wake(u_int32_t *uaddr, int nr_wake, int opflags)79{80return futex_syscall(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags);81}8283/**84* futex_lock_pi() - block on uaddr as a PI mutex85*/86static inline int87futex_lock_pi(u_int32_t *uaddr, struct timespec *timeout, int opflags)88{89return futex_syscall(uaddr, FUTEX_LOCK_PI, 0, timeout, NULL, 0, opflags);90}9192/**93* futex_unlock_pi() - release uaddr as a PI mutex, waking the top waiter94*/95static inline int96futex_unlock_pi(u_int32_t *uaddr, int opflags)97{98return futex_syscall(uaddr, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0, opflags);99}100101/**102* futex_cmp_requeue() - requeue tasks from uaddr to uaddr2103* @nr_wake: wake up to this many tasks104* @nr_requeue: requeue up to this many tasks105*/106static inline int107futex_cmp_requeue(u_int32_t *uaddr, u_int32_t val, u_int32_t *uaddr2, int nr_wake,108int nr_requeue, int opflags)109{110return futex_syscall_nr_requeue(uaddr, FUTEX_CMP_REQUEUE, nr_wake, nr_requeue, uaddr2,111val, opflags);112}113114/**115* futex_wait_requeue_pi() - block on uaddr and prepare to requeue to uaddr2116* @uaddr: non-PI futex source117* @uaddr2: PI futex target118*119* This is the first half of the requeue_pi mechanism. It shall always be120* paired with futex_cmp_requeue_pi().121*/122static inline int123futex_wait_requeue_pi(u_int32_t *uaddr, u_int32_t val, u_int32_t *uaddr2,124struct timespec *timeout, int opflags)125{126return futex_syscall(uaddr, FUTEX_WAIT_REQUEUE_PI, val, timeout, uaddr2, 0,127opflags);128}129130/**131* futex_cmp_requeue_pi() - requeue tasks from uaddr to uaddr2132* @uaddr: non-PI futex source133* @uaddr2: PI futex target134* @nr_requeue: requeue up to this many tasks135*136* This is the second half of the requeue_pi mechanism. It shall always be137* paired with futex_wait_requeue_pi(). The first waker is always awoken.138*/139static inline int140futex_cmp_requeue_pi(u_int32_t *uaddr, u_int32_t val, u_int32_t *uaddr2,141int nr_requeue, int opflags)142{143return futex_syscall_nr_requeue(uaddr, FUTEX_CMP_REQUEUE_PI, 1, nr_requeue, uaddr2,144val, opflags);145}146147void futex_set_nbuckets_param(struct bench_futex_parameters *params);148void futex_print_nbuckets(struct bench_futex_parameters *params);149150#endif /* _FUTEX_H */151152153