Futex
In computing, a futex (short for "fast userspace mutex") is a kernel system call that programmers can use to implement basic locking, or as a building block for higher-level locking abstractions such as semaphores and POSIX mutexes or condition variables.
A futex consists of a kernel-space wait queue that is attached to an atomic integer in userspace. Multiple processes or threads operate on the integer entirely in userspace (using atomic operations to avoid interfering with one another), and only resort to relatively expensive[citation needed] system calls to request operations on the wait queue (for example to wake up waiting processes, or to put the current process on the wait queue). A properly programmed futex-based lock will not use system calls except when the lock has contended; since most operations do not require arbitration between processes, this will not happen in most cases.
History
This section is missing information about FUTEX2 by valve, mainly intended to mimic WaitForMultipleObjects in wine/proton “fsync”.November 2021) ( |
Hubertus Franke (IBM Thomas J. Watson Research Center), Matthew Kirkwood, Ingo Molnár (Red Hat), and Rusty Russell (IBM Linux Technology Center) originated the futex mechanism on Linux in 2002.
[1] In the same year, discussions took place on a proposal to make futexes accessible via the file system by creating a special node in /dev
or /proc
. However, Linus Torvalds strongly opposed this idea and rejected any related patches.[2]
Futexes then appeared for the first time in version 2.5.7 of the Linux kernel development series; the semantics stabilized as of version 2.5.40, and futexes have been part of the Linux kernel mainline since the December 2003 release of 2.6.x stable kernel series.
Futexes have been implemented in Microsoft Windows since Windows 8 or Windows Server 2012 under the name WaitOnAddress.[3]
In 2013, Microsoft patented futexes and the patent was granted in 2014.[4]
In May 2014, the CVE system announced a vulnerability discovered in the Linux kernel's futex subsystem that allowed denial-of-service attacks or local privilege escalation.[5][6]
In May 2015, the Linux kernel introduced a deadlock bug via Commit b0c29f79ecea that caused a hang in user applications. The bug affected many enterprise Linux distributions, including 3.x and 4.x kernels, and Red Hat Enterprise Linux version 5, 6 and 7, SUSE Linux 12 and Amazon Linux.[7]
Futexes have been implemented in OpenBSD since 2016.[8]
The futex mechanism is one of the core concepts of the Zircon kernel[9] in Google's Fuchsia operating system since at least April 2018.[10]
Operations
Futexes have two basic operations, WAIT
and WAKE
.
WAIT(addr, val)
- If the value stored at the address
addr
isval
, puts the current thread to sleep.
WAKE(addr, num)
- Wakes up
num
number of threads waiting on the addressaddr
.
For more advanced uses, there are a number of other operations, the most used being REQUEUE
and WAKE_OP
, which both function as more generic WAKE
operations.[11]
CMP_REQUEUE(old_addr, new_addr, num_wake, num_move, val)
- If the value stored at the address
old_addr
isval
, wakesnum_wake
threads waiting on the addressold_addr
, and enqueuesnum_move
threads waiting on the addressold_addr
to now wait on the addressnew_addr
. This can be used to avoid the thundering herd problem on wake.[12][13]
WAKE_OP(addr1, addr2, num1, num2, op, op_arg, cmp, cmp_arg)
- Will read
addr2
, performop
withop_arg
on it, and storing the result back toaddr2
. Then it will wakenum1
threads waiting onaddr1
, and, if the previously read value fromaddr2
matchescmp_arg
using comparisoncmp
, will wakenum2
threads waiting onaddr2
. This very flexible and generic wake mechanism is useful for implementing many synchronization primitives.
See also
References
- ↑ "Fuss, Futexes and Furwocks: Fast Userlevel Locking in Linux" by Franke, Russell, Kirkwood. Published in 2002 for the Ottawa Linux Symposium.
- ↑ Torvalds, Linus. "Futex Asynchronous Interface". http://yarchive.net/comp/linux/everything_is_file.html.
- ↑ "WaitOnAddress function". https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitonaddress.
- ↑ "US8782674B2 Wait on address synchronization interface". https://patents.google.com/patent/US8782674B2/en.
- ↑ CVE-2014-3153
- ↑ "[SECURITY] [DSA 2949-1] linux security update". Lists.debian.org. 2014-06-05. https://lists.debian.org/debian-security-announce/2014/msg00130.html.
- ↑ "Linux futex_wait() bug...". 2015-05-13. https://groups.google.com/forum/#!topic/mechanical-sympathy/QbmpZxp6C64.
- ↑ Mazurek, Michal. "'Futexes for OpenBSD' - MARC". https://marc.info/?l=openbsd-tech&m=147282346309815&w=2.
- ↑ "Zircon Kernel Concepts". https://fuchsia.dev/fuchsia-src/zircon/concepts#futexes.
- ↑ "zx_futex_wait". https://fuchsia.dev/fuchsia-src/reference/syscalls/futex_wait.
- ↑ Futexes Are Tricky Ulrich Drepper (Red Hat, v1.6, 2011)
- ↑ Linux futex(2) man page, FUTEX_CMP_REQUEUE section
- ↑ Zircon zx_futex_requeue documentation
External links
- - futex() system call
- - futex semantics and usage
- Hubertus Franke, Rusty Russell, Matthew Kirkwood. Fuss, futexes and furwocks: Fast Userlevel Locking in Linux, Ottawa Linux Symposium 2002.
- Drepper, Ulrich (2011). "Futexes are Tricky". Red Hat. https://www.akkadia.org/drepper/futex.pdf.
- Bert Hubert (2004). Unofficial Futex manpages
- Ingo Molnar. "Robust Futexes", Linux Kernel Documentation
- "Priority Inheritance Futexes", Linux Kernel Documentation
Original source: https://en.wikipedia.org/wiki/Futex.
Read more |