futex2.rst 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ======
  3. futex2
  4. ======
  5. :Author: André Almeida <[email protected]>
  6. futex, or fast user mutex, is a set of syscalls to allow userspace to create
  7. performant synchronization mechanisms, such as mutexes, semaphores and
  8. conditional variables in userspace. C standard libraries, like glibc, uses it
  9. as a means to implement more high level interfaces like pthreads.
  10. futex2 is a followup version of the initial futex syscall, designed to overcome
  11. limitations of the original interface.
  12. User API
  13. ========
  14. ``futex_waitv()``
  15. -----------------
  16. Wait on an array of futexes, wake on any::
  17. futex_waitv(struct futex_waitv *waiters, unsigned int nr_futexes,
  18. unsigned int flags, struct timespec *timeout, clockid_t clockid)
  19. struct futex_waitv {
  20. __u64 val;
  21. __u64 uaddr;
  22. __u32 flags;
  23. __u32 __reserved;
  24. };
  25. Userspace sets an array of struct futex_waitv (up to a max of 128 entries),
  26. using ``uaddr`` for the address to wait for, ``val`` for the expected value
  27. and ``flags`` to specify the type (e.g. private) and size of futex.
  28. ``__reserved`` needs to be 0, but it can be used for future extension. The
  29. pointer for the first item of the array is passed as ``waiters``. An invalid
  30. address for ``waiters`` or for any ``uaddr`` returns ``-EFAULT``.
  31. If userspace has 32-bit pointers, it should do a explicit cast to make sure
  32. the upper bits are zeroed. ``uintptr_t`` does the tricky and it works for
  33. both 32/64-bit pointers.
  34. ``nr_futexes`` specifies the size of the array. Numbers out of [1, 128]
  35. interval will make the syscall return ``-EINVAL``.
  36. The ``flags`` argument of the syscall needs to be 0, but it can be used for
  37. future extension.
  38. For each entry in ``waiters`` array, the current value at ``uaddr`` is compared
  39. to ``val``. If it's different, the syscall undo all the work done so far and
  40. return ``-EAGAIN``. If all tests and verifications succeeds, syscall waits until
  41. one of the following happens:
  42. - The timeout expires, returning ``-ETIMEOUT``.
  43. - A signal was sent to the sleeping task, returning ``-ERESTARTSYS``.
  44. - Some futex at the list was woken, returning the index of some waked futex.
  45. An example of how to use the interface can be found at ``tools/testing/selftests/futex/functional/futex_waitv.c``.
  46. Timeout
  47. -------
  48. ``struct timespec *timeout`` argument is an optional argument that points to an
  49. absolute timeout. You need to specify the type of clock being used at
  50. ``clockid`` argument. ``CLOCK_MONOTONIC`` and ``CLOCK_REALTIME`` are supported.
  51. This syscall accepts only 64bit timespec structs.
  52. Types of futex
  53. --------------
  54. A futex can be either private or shared. Private is used for processes that
  55. shares the same memory space and the virtual address of the futex will be the
  56. same for all processes. This allows for optimizations in the kernel. To use
  57. private futexes, it's necessary to specify ``FUTEX_PRIVATE_FLAG`` in the futex
  58. flag. For processes that doesn't share the same memory space and therefore can
  59. have different virtual addresses for the same futex (using, for instance, a
  60. file-backed shared memory) requires different internal mechanisms to be get
  61. properly enqueued. This is the default behavior, and it works with both private
  62. and shared futexes.
  63. Futexes can be of different sizes: 8, 16, 32 or 64 bits. Currently, the only
  64. supported one is 32 bit sized futex, and it need to be specified using
  65. ``FUTEX_32`` flag.