162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci======
462306a36Sopenharmony_cifutex2
562306a36Sopenharmony_ci======
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci:Author: André Almeida <andrealmeid@collabora.com>
862306a36Sopenharmony_ci
962306a36Sopenharmony_cifutex, or fast user mutex, is a set of syscalls to allow userspace to create
1062306a36Sopenharmony_ciperformant synchronization mechanisms, such as mutexes, semaphores and
1162306a36Sopenharmony_ciconditional variables in userspace. C standard libraries, like glibc, uses it
1262306a36Sopenharmony_cias a means to implement more high level interfaces like pthreads.
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_cifutex2 is a followup version of the initial futex syscall, designed to overcome
1562306a36Sopenharmony_cilimitations of the original interface.
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciUser API
1862306a36Sopenharmony_ci========
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci``futex_waitv()``
2162306a36Sopenharmony_ci-----------------
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ciWait on an array of futexes, wake on any::
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci  futex_waitv(struct futex_waitv *waiters, unsigned int nr_futexes,
2662306a36Sopenharmony_ci              unsigned int flags, struct timespec *timeout, clockid_t clockid)
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci  struct futex_waitv {
2962306a36Sopenharmony_ci        __u64 val;
3062306a36Sopenharmony_ci        __u64 uaddr;
3162306a36Sopenharmony_ci        __u32 flags;
3262306a36Sopenharmony_ci        __u32 __reserved;
3362306a36Sopenharmony_ci  };
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ciUserspace sets an array of struct futex_waitv (up to a max of 128 entries),
3662306a36Sopenharmony_ciusing ``uaddr`` for the address to wait for, ``val`` for the expected value
3762306a36Sopenharmony_ciand ``flags`` to specify the type (e.g. private) and size of futex.
3862306a36Sopenharmony_ci``__reserved`` needs to be 0, but it can be used for future extension. The
3962306a36Sopenharmony_cipointer for the first item of the array is passed as ``waiters``. An invalid
4062306a36Sopenharmony_ciaddress for ``waiters`` or for any ``uaddr`` returns ``-EFAULT``.
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciIf userspace has 32-bit pointers, it should do a explicit cast to make sure
4362306a36Sopenharmony_cithe upper bits are zeroed. ``uintptr_t`` does the tricky and it works for
4462306a36Sopenharmony_ciboth 32/64-bit pointers.
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci``nr_futexes`` specifies the size of the array. Numbers out of [1, 128]
4762306a36Sopenharmony_ciinterval will make the syscall return ``-EINVAL``.
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciThe ``flags`` argument of the syscall needs to be 0, but it can be used for
5062306a36Sopenharmony_cifuture extension.
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciFor each entry in ``waiters`` array, the current value at ``uaddr`` is compared
5362306a36Sopenharmony_cito ``val``. If it's different, the syscall undo all the work done so far and
5462306a36Sopenharmony_cireturn ``-EAGAIN``. If all tests and verifications succeeds, syscall waits until
5562306a36Sopenharmony_cione of the following happens:
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci- The timeout expires, returning ``-ETIMEOUT``.
5862306a36Sopenharmony_ci- A signal was sent to the sleeping task, returning ``-ERESTARTSYS``.
5962306a36Sopenharmony_ci- Some futex at the list was woken, returning the index of some waked futex.
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ciAn example of how to use the interface can be found at ``tools/testing/selftests/futex/functional/futex_waitv.c``.
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ciTimeout
6462306a36Sopenharmony_ci-------
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci``struct timespec *timeout`` argument is an optional argument that points to an
6762306a36Sopenharmony_ciabsolute timeout. You need to specify the type of clock being used at
6862306a36Sopenharmony_ci``clockid`` argument. ``CLOCK_MONOTONIC`` and ``CLOCK_REALTIME`` are supported.
6962306a36Sopenharmony_ciThis syscall accepts only 64bit timespec structs.
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciTypes of futex
7262306a36Sopenharmony_ci--------------
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ciA futex can be either private or shared. Private is used for processes that
7562306a36Sopenharmony_cishares the same memory space and the virtual address of the futex will be the
7662306a36Sopenharmony_cisame for all processes. This allows for optimizations in the kernel. To use
7762306a36Sopenharmony_ciprivate futexes, it's necessary to specify ``FUTEX_PRIVATE_FLAG`` in the futex
7862306a36Sopenharmony_ciflag. For processes that doesn't share the same memory space and therefore can
7962306a36Sopenharmony_cihave different virtual addresses for the same futex (using, for instance, a
8062306a36Sopenharmony_cifile-backed shared memory) requires different internal mechanisms to be get
8162306a36Sopenharmony_ciproperly enqueued. This is the default behavior, and it works with both private
8262306a36Sopenharmony_ciand shared futexes.
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciFutexes can be of different sizes: 8, 16, 32 or 64 bits. Currently, the only
8562306a36Sopenharmony_cisupported one is 32 bit sized futex, and it need to be specified using
8662306a36Sopenharmony_ci``FUTEX_32`` flag.
87