]>
Commit | Line | Data |
---|---|---|
8f0aff2a | 1 | .\" Page by b.hubert |
1abce893 MK |
2 | .\" and Copyright (C) 2015, Thomas Gleixner <tglx@linutronix.de> |
3 | .\" and Copyright (C) 2015, Michael Kerrisk <mtk.manpages@gmail.com> | |
2297bf0e | 4 | .\" |
2e46a6e7 | 5 | .\" %%%LICENSE_START(FREELY_REDISTRIBUTABLE) |
8f0aff2a | 6 | .\" may be freely modified and distributed |
8ff7380d | 7 | .\" %%%LICENSE_END |
fea681da MK |
8 | .\" |
9 | .\" Niki A. Rahimi (LTC Security Development, narahimi@us.ibm.com) | |
10 | .\" added ERRORS section. | |
11 | .\" | |
12 | .\" Modified 2004-06-17 mtk | |
13 | .\" Modified 2004-10-07 aeb, added FUTEX_REQUEUE, FUTEX_CMP_REQUEUE | |
14 | .\" | |
4f58b197 MK |
15 | .\" 2.6.31 adds FUTEX_WAIT_REQUEUE_PI, FUTEX_CMP_REQUEUE_PI |
16 | .\" commit 52400ba946759af28442dee6265c5c0180ac7122 | |
17 | .\" Author: Darren Hart <dvhltc@us.ibm.com> | |
18 | .\" Date: Fri Apr 3 13:40:49 2009 -0700 | |
19 | .\" | |
20 | .\" commit ba9c22f2c01cf5c88beed5a6b9e07d42e10bd358 | |
21 | .\" Author: Darren Hart <dvhltc@us.ibm.com> | |
22 | .\" Date: Mon Apr 20 22:22:22 2009 -0700 | |
23 | .\" | |
24 | .\" See Documentation/futex-requeue-pi.txt | |
34f7665a | 25 | .\" |
3d155313 | 26 | .TH FUTEX 2 2014-05-21 "Linux" "Linux Programmer's Manual" |
fea681da | 27 | .SH NAME |
ce154705 | 28 | futex \- fast user-space locking |
fea681da | 29 | .SH SYNOPSIS |
9d9dc1e8 | 30 | .nf |
fea681da MK |
31 | .sp |
32 | .B "#include <linux/futex.h>" | |
fea681da MK |
33 | .B "#include <sys/time.h>" |
34 | .sp | |
d33602c4 | 35 | .BI "int futex(int *" uaddr ", int " futex_op ", int " val , |
768d3c23 MK |
36 | .BI " const struct timespec *" timeout , \ |
37 | " \fR /* or: \fBu32 \fIval2\fP */ | |
9d9dc1e8 | 38 | .BI " int *" uaddr2 ", int " val3 ); |
9d9dc1e8 | 39 | .fi |
409f08b0 | 40 | |
b939d6e4 MK |
41 | .IR Note : |
42 | There is no glibc wrapper for this system call; see NOTES. | |
47297adb | 43 | .SH DESCRIPTION |
fea681da MK |
44 | .PP |
45 | The | |
e511ffb6 | 46 | .BR futex () |
fea681da MK |
47 | system call provides a method for |
48 | a program to wait for a value at a given address to change, and a | |
49 | method to wake up anyone waiting on a particular address (while the | |
50 | addresses for the same memory in separate processes may not be | |
51 | equal, the kernel maps them internally so the same memory mapped in | |
52 | different locations will correspond for | |
e511ffb6 | 53 | .BR futex () |
c13182ef | 54 | calls). |
fd3fa7ef | 55 | This system call is typically used to |
fea681da MK |
56 | implement the contended case of a lock in shared memory, as |
57 | described in | |
a8bda636 | 58 | .BR futex (7). |
fea681da | 59 | .PP |
f388ba70 MK |
60 | When a futex operation did not finish uncontended in user space, a |
61 | .BR futex () | |
62 | call needs to be made to the kernel to arbitrate. | |
c13182ef | 63 | Arbitration can either mean putting the calling |
fea681da MK |
64 | process to sleep or, conversely, waking a waiting process. |
65 | .PP | |
f388ba70 MK |
66 | Callers of |
67 | .BR futex () | |
68 | are expected to adhere to the semantics described in | |
a8bda636 | 69 | .BR futex (7). |
fea681da | 70 | As these |
d603cc27 | 71 | semantics involve writing nonportable assembly instructions, this in turn |
fea681da MK |
72 | probably means that most users will in fact be library authors and not |
73 | general application developers. | |
74 | .PP | |
75 | The | |
76 | .I uaddr | |
f388ba70 MK |
77 | argument points to an integer which stores the counter (futex). |
78 | On all platforms, futexes are four-byte integers that | |
79 | must be aligned on a four-byte boundary. | |
80 | The operation to perform on the futex is specified in the | |
81 | .I futex_op | |
82 | argument; | |
83 | .IR val | |
84 | is a value whose meaning and purpose depends on | |
85 | .IR futex_op . | |
36ab2074 MK |
86 | |
87 | The remaining arguments | |
88 | .RI ( timeout , | |
89 | .IR uaddr2 , | |
90 | and | |
91 | .IR val3 ) | |
92 | are required only for certain of the futex operations described below. | |
93 | Where one of these arguments is not required, it is ignored. | |
768d3c23 | 94 | |
36ab2074 MK |
95 | For several blocking operations, the |
96 | .I timeout | |
97 | argument is a pointer to a | |
98 | .IR timespec | |
99 | structure that specifies a timeout for the operation. | |
100 | However, notwithstanding the prototype shown above, for some operations, | |
101 | this argument is instead a four-byte integer whose meaning | |
102 | is determined by the operation. | |
768d3c23 MK |
103 | For these operations, the kernel casts the |
104 | .I timeout | |
105 | value to | |
106 | .IR u32 , | |
107 | and in the remainder of this page, this argument is referred to as | |
108 | .I val2 | |
109 | when interpreted in this fashion. | |
110 | ||
de5a3bb4 | 111 | Where it is required, the |
36ab2074 | 112 | .IR uaddr2 |
de5a3bb4 | 113 | argument is a pointer to a second futex that is employed by the operation. |
36ab2074 MK |
114 | The interpretation of the final integer argument, |
115 | .IR val3 , | |
116 | depends on the operation. | |
117 | ||
6be4bad7 | 118 | The |
d33602c4 | 119 | .I futex_op |
6be4bad7 MK |
120 | argument consists of two parts: |
121 | a command that specifies the operation to be performed, | |
122 | bit-wise ORed with zero or or more options that | |
123 | modify the behaviour of the operation. | |
fc30eb79 | 124 | The options that may be included in |
d33602c4 | 125 | .I futex_op |
fc30eb79 TG |
126 | are as follows: |
127 | .TP | |
128 | .BR FUTEX_PRIVATE_FLAG " (since Linux 2.6.22)" | |
129 | .\" commit 34f01cc1f512fa783302982776895c73714ebbc2 | |
130 | This option bit can be employed with all futex operations. | |
131 | It tells the kernel that the futex is process private and not shared | |
132 | with another process. | |
133 | This allows the kernel to choose the fast path for validating | |
134 | the user-space address and avoids expensive VMA lookups, | |
135 | taking reference counts on file backing store, and so on. | |
ae2c1774 MK |
136 | |
137 | As a convenience, | |
138 | .IR <linux/futex.h> | |
139 | defines a set of constants with the suffix | |
140 | .BR _PRIVATE | |
141 | that are equivalents of all of the operations listed below, | |
dcdfde26 | 142 | .\" except the obsolete FUTEX_FD, for which the "private" flag was |
ae2c1774 MK |
143 | .\" meaningless |
144 | but with the | |
145 | .BR FUTEX_PRIVATE_FLAG | |
146 | ORed into the constant value. | |
147 | Thus, there are | |
148 | .BR FUTEX_WAIT_PRIVATE , | |
149 | .BR FUTEX_WAKE_PRIVATE , | |
150 | and so on. | |
2e98bbc2 TG |
151 | .TP |
152 | .BR FUTEX_CLOCK_REALTIME " (since Linux 2.6.28)" | |
153 | .\" commit 1acdac104668a0834cfa267de9946fac7764d486 | |
4a7e5b05 | 154 | This option bit can be employed only with the |
2e98bbc2 TG |
155 | .BR FUTEX_WAIT_BITSET |
156 | and | |
157 | .BR FUTEX_WAIT_REQUEUE_PI | |
c84cf68c | 158 | operations. |
2e98bbc2 | 159 | |
f2103b26 MK |
160 | If this option is set, the kernel treats |
161 | .I timeout | |
162 | as an absolute time based on | |
2e98bbc2 TG |
163 | .BR CLOCK_REALTIME . |
164 | ||
f2103b26 MK |
165 | If this option is not set, the kernel treats |
166 | .I timeout | |
167 | as relative time, | |
1c952cf5 MK |
168 | .\" FIXME I added CLOCK_MONOTONIC here. Is it correct? |
169 | measured against the | |
170 | .BR CLOCK_MONOTONIC | |
171 | clock. | |
6be4bad7 MK |
172 | .PP |
173 | The operation specified in | |
d33602c4 | 174 | .I futex_op |
6be4bad7 | 175 | is one of the following: |
fea681da | 176 | .TP |
81c9d87e MK |
177 | .BR FUTEX_WAIT " (since Linux 2.6.0)" |
178 | .\" Strictly speaking, since some time in 2.5.x | |
f065673c MK |
179 | This operation tests that the value at the |
180 | location pointed to by the futex address | |
fea681da MK |
181 | .I uaddr |
182 | still contains the value | |
183 | .IR val , | |
f065673c | 184 | and then sleeps awaiting |
682edefb | 185 | .B FUTEX_WAKE |
f065673c MK |
186 | on the futex address. |
187 | The test and sleep steps are performed atomically. | |
188 | If the futex value does not match | |
189 | .IR val , | |
4710334a | 190 | then the call fails immediately with the error |
badbf70c | 191 | .BR EAGAIN . |
f065673c MK |
192 | .\" FIXME I added the following sentence. Please confirm that it is correct. |
193 | The purpose of the test step is to detect races where | |
194 | another process changes that value of the futex between | |
195 | the time it was last checked and the time of the | |
196 | .BR FUTEX_WAIT | |
63d3f911 | 197 | operation. |
1909e523 | 198 | |
c13182ef | 199 | If the |
fea681da | 200 | .I timeout |
1c952cf5 MK |
201 | argument is non-NULL, its contents specify a relative timeout for the wait |
202 | .\" FIXME I added CLOCK_MONOTONIC here. Is it correct? | |
203 | measured according to the | |
204 | .BR CLOCK_MONOTONIC | |
205 | clock. | |
82a6092b MK |
206 | (This interval will be rounded up to the system clock granularity, |
207 | and kernel scheduling delays mean that the | |
208 | blocking interval may overrun by a small amount.) | |
209 | If | |
210 | .I timeout | |
211 | is NULL, the call blocks indefinitely. | |
4798a7f3 | 212 | |
c13182ef | 213 | The arguments |
fea681da MK |
214 | .I uaddr2 |
215 | and | |
216 | .I val3 | |
217 | are ignored. | |
218 | ||
219 | For | |
a8bda636 | 220 | .BR futex (7), |
fea681da MK |
221 | this call is executed if decrementing the count gave a negative value |
222 | (indicating contention), and will sleep until another process releases | |
682edefb MK |
223 | the futex and executes the |
224 | .B FUTEX_WAKE | |
225 | operation. | |
fea681da | 226 | .TP |
81c9d87e MK |
227 | .BR FUTEX_WAKE " (since Linux 2.6.0)" |
228 | .\" Strictly speaking, since Linux 2.5.x | |
f065673c MK |
229 | This operation wakes at most |
230 | .I val | |
231 | processes waiting (i.e., inside | |
232 | .BR FUTEX_WAIT ) | |
233 | on the futex at the address | |
234 | .IR uaddr . | |
235 | Most commonly, | |
236 | .I val | |
237 | is specified as either 1 (wake up a single waiter) or | |
238 | .BR INT_MAX | |
239 | (wake up all waiters). | |
730bfbda MK |
240 | .\" FIXME Please confirm that the following is correct: |
241 | No guarantee is provided about which waiters are awoken | |
242 | (e.g., a waiter with a higher scheduling priority is not guaranteed | |
243 | to be awoken in preference to a waiter with a lower priority). | |
4798a7f3 | 244 | |
fea681da MK |
245 | The arguments |
246 | .IR timeout , | |
c8b921bd | 247 | .IR uaddr2 , |
fea681da MK |
248 | and |
249 | .I val3 | |
250 | are ignored. | |
251 | ||
252 | For | |
a8bda636 | 253 | .BR futex (7), |
fea681da MK |
254 | this is executed if incrementing |
255 | the count showed that there were waiters, once the futex value has been set | |
256 | to 1 (indicating that it is available). | |
a7c2bf45 MK |
257 | .TP |
258 | .BR FUTEX_FD " (from Linux 2.6.0 up to and including Linux 2.6.25)" | |
259 | .\" Strictly speaking, from Linux 2.5.x to 2.6.25 | |
260 | This operation creates a file descriptor that is associated with the futex at | |
261 | .IR uaddr . | |
262 | .\" , suitable for .BR poll (2). | |
263 | The calling process must close the returned file descriptor after use. | |
264 | When another process performs a | |
265 | .BR FUTEX_WAKE | |
266 | on the futex, the file descriptor indicates as being readable with | |
267 | .BR select (2), | |
268 | .BR poll (2), | |
269 | and | |
270 | .BR epoll (7) | |
271 | ||
272 | The file descriptor can be used to obtain asynchronous notifications: | |
273 | if | |
274 | .I val | |
275 | is nonzero, then when another process executes a | |
276 | .BR FUTEX_WAKE , | |
277 | the caller will receive the signal number that was passed in | |
278 | .IR val . | |
279 | ||
280 | The arguments | |
281 | .IR timeout , | |
282 | .I uaddr2 | |
283 | and | |
284 | .I val3 | |
285 | are ignored. | |
286 | ||
287 | To prevent race conditions, the caller should test if the futex has | |
288 | been upped after | |
289 | .B FUTEX_FD | |
290 | returns. | |
291 | ||
292 | Because it was inherently racy, | |
293 | .B FUTEX_FD | |
294 | has been removed | |
295 | .\" commit 82af7aca56c67061420d618cc5a30f0fd4106b80 | |
296 | from Linux 2.6.26 onward. | |
297 | .TP | |
298 | .BR FUTEX_REQUEUE " (since Linux 2.6.0)" | |
299 | .\" Strictly speaking: from Linux 2.5.70 | |
300 | .\" | |
301 | .\" FIXME I added this warning. Okay? | |
302 | .IR "Avoid using this operation" . | |
303 | It is broken (unavoidably racy) for its intended purpose. | |
304 | Use | |
305 | .BR FUTEX_CMP_REQUEUE | |
306 | instead. | |
307 | ||
308 | This operation performs the same task as | |
309 | .BR FUTEX_CMP_REQUEUE , | |
310 | except that no check is made using the value in | |
311 | .IR val3 . | |
312 | (The argument | |
313 | .I val3 | |
314 | is ignored.) | |
315 | .TP | |
316 | .BR FUTEX_CMP_REQUEUE " (since Linux 2.6.7)" | |
317 | This operation was added as a replacement for the earlier | |
318 | .BR FUTEX_REQUEUE , | |
319 | because that operation was racy for its intended use. | |
320 | ||
321 | As with | |
322 | .BR FUTEX_REQUEUE , | |
323 | the | |
324 | .BR FUTEX_CMP_REQUEUE | |
325 | operation is used to avoid a "thundering herd" effect when | |
326 | .B FUTEX_WAKE | |
327 | is used and all processes woken up need to acquire another futex. | |
328 | It differs from | |
329 | .BR FUTEX_REQUEUE | |
330 | in that it first checks whether the location | |
331 | .I uaddr | |
332 | still contains the value | |
333 | .IR val3 . | |
334 | If not, the operation fails with the error | |
335 | .BR EAGAIN . | |
336 | .\" FIXME I added the following sentence on rational for FUTEX_CMP_REQUEUE. | |
337 | .\" Is it correct? SHould it be expanded? | |
338 | This additional feature of | |
339 | .BR FUTEX_CMP_REQUEUE | |
340 | can be used by the caller to (atomically) detect changes | |
341 | in the value of the target futex at | |
342 | .IR uaddr2 . | |
343 | ||
344 | The operation wakes up a maximum of | |
345 | .I val | |
346 | waiters that are waiting on the futex at | |
347 | .IR uaddr . | |
348 | If there are more than | |
349 | .I val | |
350 | waiters, then the remaining waiters are removed | |
351 | from the wait queue of the source futex at | |
352 | .I uaddr | |
353 | and added to the wait queue of the target futex at | |
354 | .IR uaddr2 . | |
936876a9 | 355 | |
a7c2bf45 | 356 | The |
768d3c23 | 357 | .I val2 |
936876a9 | 358 | argument specifies an upper limit on the number of waiters |
a7c2bf45 | 359 | that are requeued to the futex at |
768d3c23 | 360 | .IR uaddr2 . |
a7c2bf45 MK |
361 | |
362 | .\" FIXME Please review the following new paragraph to see if it is | |
363 | .\" accurate. | |
364 | Typical values to specify for | |
365 | .I val | |
366 | are 0 or or 1. | |
367 | (Specifying | |
368 | .BR INT_MAX | |
369 | is not useful, because it would make the | |
370 | .BR FUTEX_CMP_REQUEUE | |
371 | operation equivalent to | |
372 | .BR FUTEX_WAKE .) | |
936876a9 | 373 | The limit value specified via |
768d3c23 MK |
374 | .I val2 |
375 | is typically either 1 or | |
a7c2bf45 MK |
376 | .BR INT_MAX . |
377 | (Specifying the argument as 0 is not useful, because it would make the | |
378 | .BR FUTEX_CMP_REQUEUE | |
379 | operation equivalent to | |
380 | .BR FUTEX_WAIT .) | |
6bac3b85 MK |
381 | .\" |
382 | .\" FIXME I added some FUTEX_WAKE_OP text, and I'd be happy if someone | |
383 | .\" checked it. | |
fea681da | 384 | .TP |
d67e21f5 MK |
385 | .BR FUTEX_WAKE_OP " (since Linux 2.6.14)" |
386 | .\" commit 4732efbeb997189d9f9b04708dc26bf8613ed721 | |
6bac3b85 MK |
387 | .\" Author: Jakub Jelinek <jakub@redhat.com> |
388 | .\" Date: Tue Sep 6 15:16:25 2005 -0700 | |
389 | This operation was added to support some user-space use cases | |
390 | where more than one futex must be handled at the same time. | |
391 | The most notable example is the implementation of | |
392 | .BR pthread_cond_signal (3), | |
393 | which requires operations on two futexes, | |
394 | the one used to implement the mutex and the one used in the implementation | |
395 | of the wait queue associated with the condition variable. | |
396 | .BR FUTEX_WAKE_OP | |
397 | allows such cases to be implemented without leading to | |
398 | high rates of contention and context switching. | |
399 | ||
400 | The | |
401 | .BR FUTEX_WAIT_OP | |
402 | operation is equivalent to atomically executing the following code: | |
403 | ||
404 | .in +4n | |
405 | .nf | |
406 | int oldval = *(int *) uaddr2; | |
407 | *(int *) uaddr2 = oldval \fIop\fP \fIoparg\fP; | |
408 | futex(uaddr, FUTEX_WAKE, val, 0, 0, 0); | |
409 | if (oldval \fIcmp\fP \fIcmparg\fP) | |
768d3c23 | 410 | futex(uaddr2, FUTEX_WAKE, val2, 0, 0, 0); |
6bac3b85 MK |
411 | .fi |
412 | .in | |
413 | ||
414 | In other words, | |
415 | .BR FUTEX_WAIT_OP | |
416 | does the following: | |
417 | .RS | |
418 | .IP * 3 | |
419 | saves the original value of the futex at | |
420 | .IR uaddr2 ; | |
421 | .IP * | |
422 | performs an operation to modify the value of the futex at | |
423 | .IR uaddr2 ; | |
424 | .IP * | |
425 | wakes up a maximum of | |
426 | .I val | |
427 | waiters on the futex | |
428 | .IR uaddr ; | |
429 | and | |
430 | .IP * | |
431 | dependent on the results of a test of the original value of the futex at | |
432 | .IR uaddr2 , | |
433 | wakes up a maximum of | |
768d3c23 | 434 | .I val2 |
6bac3b85 MK |
435 | waiters on the futex |
436 | .IR uaddr2 . | |
437 | .RE | |
438 | .IP | |
6bac3b85 MK |
439 | The operation and comparison that are to be performed are encoded |
440 | in the bits of the argument | |
441 | .IR val3 . | |
442 | Pictorially, the encoding is: | |
443 | ||
f6af90e7 | 444 | .in +8n |
6bac3b85 | 445 | .nf |
f6af90e7 MK |
446 | +---+---+-----------+-----------+ |
447 | |op |cmp| oparg | cmparg | | |
448 | +---+---+-----------+-----------+ | |
449 | 4 4 12 12 <== # of bits | |
6bac3b85 MK |
450 | .fi |
451 | .in | |
452 | ||
453 | Expressed in code, the encoding is: | |
454 | ||
455 | .in +4n | |
456 | .nf | |
457 | #define FUTEX_OP(op, oparg, cmp, cmparg) \\ | |
458 | (((op & 0xf) << 28) | \\ | |
459 | ((cmp & 0xf) << 24) | \\ | |
460 | ((oparg & 0xfff) << 12) | \\ | |
461 | (cmparg & 0xfff)) | |
462 | .fi | |
463 | .in | |
464 | ||
465 | In the above, | |
466 | .I op | |
467 | and | |
468 | .I cmp | |
469 | are each one of the codes listed below. | |
470 | The | |
471 | .I oparg | |
472 | and | |
473 | .I cmparg | |
474 | components are literal numeric values, except as noted below. | |
475 | ||
476 | The | |
477 | .I op | |
478 | component has one of the following values: | |
479 | ||
480 | .in +4n | |
481 | .nf | |
482 | FUTEX_OP_SET 0 /* uaddr2 = oparg; */ | |
483 | FUTEX_OP_ADD 1 /* uaddr2 += oparg; */ | |
484 | FUTEX_OP_OR 2 /* uaddr2 |= oparg; */ | |
485 | FUTEX_OP_ANDN 3 /* uaddr2 &= ~oparg; */ | |
486 | FUTEX_OP_XOR 4 /* uaddr2 ^= oparg; */ | |
487 | .fi | |
488 | .in | |
489 | ||
490 | In addition, bit-wise ORing the following value into | |
491 | .I op | |
492 | causes | |
493 | .IR "(1\ <<\ oparg)" | |
494 | to be used as the operand: | |
495 | ||
496 | .in +4n | |
497 | .nf | |
498 | FUTEX_OP_ARG_SHIFT 8 /* Use (1 << oparg) as operand */ | |
499 | .fi | |
500 | .in | |
501 | ||
502 | The | |
503 | .I cmp | |
504 | field is one of the following: | |
505 | ||
506 | .in +4n | |
507 | .nf | |
508 | FUTEX_OP_CMP_EQ 0 /* if (oldval == cmparg) wake */ | |
509 | FUTEX_OP_CMP_NE 1 /* if (oldval != cmparg) wake */ | |
510 | FUTEX_OP_CMP_LT 2 /* if (oldval < cmparg) wake */ | |
511 | FUTEX_OP_CMP_LE 3 /* if (oldval <= cmparg) wake */ | |
512 | FUTEX_OP_CMP_GT 4 /* if (oldval > cmparg) wake */ | |
513 | FUTEX_OP_CMP_GE 5 /* if (oldval >= cmparg) wake */ | |
514 | .fi | |
515 | .in | |
516 | ||
517 | The return value of | |
518 | .BR FUTEX_WAKE_OP | |
519 | is the sum of the number of waiters woken on the futex | |
520 | .IR uaddr | |
521 | plus the number of waiters woken on the futex | |
522 | .IR uaddr2 . | |
d67e21f5 | 523 | .TP |
79c9b436 TG |
524 | .BR FUTEX_WAIT_BITSET " (since Linux 2.6.25)" |
525 | .\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d | |
fd9e59d4 | 526 | This operation is like |
79c9b436 TG |
527 | .BR FUTEX_WAIT |
528 | except that | |
529 | .I val3 | |
530 | is used to provide a 32-bit bitset to the kernel. | |
531 | This bitset is stored in the kernel-internal state of the waiter. | |
532 | See the description of | |
533 | .BR FUTEX_WAKE_BITSET | |
534 | for further details. | |
535 | ||
fd9e59d4 MK |
536 | The |
537 | .BR FUTEX_WAIT_BITSET | |
538 | also interprets the | |
539 | .I timeout | |
540 | argument differently from | |
541 | .BR FUTEX_WAIT . | |
542 | See the discussion of | |
543 | .BR FUTEX_CLOCK_REALTIME , | |
544 | above. | |
545 | ||
79c9b436 TG |
546 | The |
547 | .I uaddr2 | |
548 | argument is ignored. | |
549 | .TP | |
d67e21f5 MK |
550 | .BR FUTEX_WAKE_BITSET " (since Linux 2.6.25)" |
551 | .\" commit cd689985cf49f6ff5c8eddc48d98b9d581d9475d | |
55cc422d TG |
552 | This operation is the same as |
553 | .BR FUTEX_WAKE | |
554 | except that the | |
555 | .I val3 | |
556 | argument is used to provide a 32-bit bitset to the kernel. | |
98d769c0 MK |
557 | This bitset is used to select which waiters should be woken up. |
558 | The selection is done by a bit-wise AND of the "wake" bitset | |
559 | (i.e., the value in | |
560 | .IR val3 ) | |
561 | and the bitset which is stored in the kernel-internal | |
09cb4ce7 | 562 | state of the waiter (the "wait" bitset that is set using |
98d769c0 MK |
563 | .BR FUTEX_WAIT_BITSET ). |
564 | All of the waiters for which the result of the AND is nonzero are woken up; | |
565 | the remaining waiters are left sleeping. | |
566 | ||
e9d4496b MK |
567 | .\" FIXME please review this paragraph that I added |
568 | The effect of | |
569 | .BR FUTEX_WAIT_BITSET | |
570 | and | |
571 | .BR FUTEX_WAKE_BITSET | |
572 | is to allow selective wake-ups among multiple waiters that are waiting | |
573 | on the same futex; | |
574 | since a futex has a size of 32 bits, | |
575 | these operations provide 32 wakeup "channels". | |
576 | (The | |
577 | .BR FUTEX_WAIT | |
578 | and | |
579 | .BR FUTEX_WAKE | |
580 | operations correspond to | |
581 | .BR FUTEX_WAIT_BITSET | |
582 | and | |
583 | .BR FUTEX_WAKE_BITSET | |
584 | operations where the bitsets are all ones.) | |
09cb4ce7 | 585 | Note, however, that using this bitset multiplexing feature on a |
e9d4496b MK |
586 | futex is less efficient than simply using multiple futexes, |
587 | because employing bitset multiplexing requires the kernel | |
588 | to check all waiters on a futex, | |
589 | including those that are not interested in being woken up | |
590 | (i.e., they do not have the relevant bit set in their "wait" bitset). | |
591 | .\" According to http://locklessinc.com/articles/futex_cheat_sheet/: | |
592 | .\" | |
593 | .\" "The original reason for the addition of these extensions | |
594 | .\" was to improve the performance of pthread read-write locks | |
595 | .\" in glibc. However, the pthreads library no longer uses the | |
596 | .\" same locking algorithm, and these extensions are not used | |
597 | .\" without the bitset parameter being all ones. | |
598 | .\" | |
599 | .\" The page goes on to note that the FUTEX_WAIT_BITSET operation | |
600 | .\" is nevertheless used (with a bitset of all ones) in order to | |
601 | .\" obtain the absolute timeout functionality that is useful | |
602 | .\" for efficiently implementing Pthreads APIs (which use absolute | |
603 | .\" timeouts); FUTEX_WAIT provides only relative timeouts. | |
604 | ||
98d769c0 MK |
605 | The |
606 | .I uaddr2 | |
607 | and | |
608 | .I timeout | |
609 | arguments are ignored. | |
bd90a5f9 MK |
610 | .\" |
611 | .\" | |
612 | .SS Priority-inheritance futexes | |
b52e1cd4 MK |
613 | Linux supports priority-inheritance (PI) futexes in order to handle |
614 | priority-inversion problems that can be encountered with | |
615 | normal futex locks. | |
79d918c7 MK |
616 | .\" |
617 | .\" FIXME ===== Start of adapted Hart/Guniguntala text ===== | |
618 | .\" The following text is drawn from the Hart/Guniguntala paper, | |
619 | .\" but I have reworded some pieces significantly. Please check it. | |
620 | .\" | |
621 | The PI futex operations described below differ from the other | |
622 | futex operations in that they impose policy on the use of the futex value: | |
623 | .IP * 3 | |
7c16fbff | 624 | If the lock is unowned, the futex value shall be 0. |
79d918c7 MK |
625 | .IP * |
626 | If the lock is owned, the futex value shall be the thread ID (TID; see | |
627 | .BR gettid (2)) | |
628 | of the owning thread. | |
629 | .IP * | |
630 | .\" FIXME In the following line, I added "the lock is owned and". Okay? | |
631 | If the lock is owned and there are threads contending for the lock, | |
632 | then the | |
633 | .B FUTEX_WAITERS | |
634 | bit shall be set in the futex value; in other words, the futex value is: | |
635 | ||
636 | FUTEX_WAITERS | TID | |
637 | .PP | |
638 | With this policy in place, | |
639 | a user-space application can acquire an unowned | |
b52e1cd4 | 640 | lock or release an uncontended lock using a atomic |
79d918c7 | 641 | .\" FIXME In the following line, I added "user-space". Okay? |
b52e1cd4 MK |
642 | user-space instructions (e.g., |
643 | .I cmpxchg | |
644 | on the x86 architecture). | |
645 | Locking an unowned lock simply consists of setting | |
646 | the futex value to the caller's TID. | |
647 | Releasing an uncontended lock simply requires setting the futex value to 0. | |
648 | ||
649 | If a futex is currently owned (i.e., has a nonzero value), | |
650 | waiters must employ the | |
79d918c7 MK |
651 | .B FUTEX_LOCK_PI |
652 | operation to acquire the lock. | |
b52e1cd4 | 653 | If a lock is contended (i.e., the |
79d918c7 | 654 | .B FUTEX_WAITERS |
b52e1cd4 | 655 | bit is set in the futex value), the lock owner must employ the |
79d918c7 | 656 | .B FUTEX_UNLOCK_PI |
b52e1cd4 MK |
657 | operation to release the lock. |
658 | ||
79d918c7 MK |
659 | In the cases where callers are forced into the kernel |
660 | (i.e., required to perform a | |
661 | .BR futex () | |
662 | operation), | |
663 | they then deal directly with a so-called RT-mutex, | |
664 | a kernel locking mechanism which implements the required | |
665 | priority-inheritance semantics. | |
666 | After the RT-mutex is acquired, the futex value is updated accordingly, | |
667 | before the calling thread returns to user space. | |
668 | .\" FIXME ===== End of adapted Hart/Guniguntala text ===== | |
669 | ||
670 | It is important | |
671 | .\" FIXME We need some explanation here of why it is important to note this | |
672 | to note that the kernel will update the futex value prior | |
673 | to returning to user space. | |
674 | Unlike the other futex operations described above, | |
675 | the PI futex operations are designed | |
7c16fbff | 676 | for the implementation of very specific IPC mechanisms). |
fc57e6bb MK |
677 | .\" |
678 | .\" FIXME We don't quite have a definition anywhere of what a PI futex | |
679 | .\" is (vs a non-PI futex). Below, we have the information of | |
680 | .\" FUTEX_CMP_REQUEUE_PI requeues from a non-PI futex to a | |
681 | .\" PI futex, but what determines whether the futex is of one | |
682 | .\" kind of the other? We should have such a definition somewhere | |
683 | .\" about here. | |
bd90a5f9 MK |
684 | |
685 | PI futexes are operated on by specifying one of the following values in | |
686 | .IR futex_op : | |
d67e21f5 MK |
687 | .TP |
688 | .BR FUTEX_LOCK_PI " (since Linux 2.6.18)" | |
689 | .\" commit c87e2837be82df479a6bae9f155c43516d2feebc | |
67833bec MK |
690 | .\" |
691 | .\" FIXME I did some significant rewording of tglx's text. | |
692 | .\" Please check, in case I injected errors. | |
693 | .\" | |
694 | This operation is used after after an attempt to acquire | |
695 | the futex lock via an atomic user-space instruction failed | |
696 | because the futex has a nonzero value\(emspecifically, | |
697 | because it contained the namespace-specific TID of the lock owner. | |
67259526 | 698 | .\" FIXME In the preceding line, what does "namespace-specific" mean? |
67833bec | 699 | .\" (I kept those words from tglx.) |
67259526 | 700 | .\" That is, what kind of namespace are we talking about? |
67833bec MK |
701 | .\" (I suppose we are talking PID namespaces here, but I want to |
702 | .\" be sure.) | |
703 | ||
704 | The operation checks the value of the futex at the address | |
705 | .IR uaddr . | |
706 | If the value is 0, then the kernel tries to atomically set the futex value to | |
707 | the caller's TID. | |
708 | If that fails, | |
709 | .\" FIXME What would be the cause of failure? | |
710 | or the futex value is nonzero, | |
711 | the kernel atomically sets the | |
e0547e70 | 712 | .B FUTEX_WAITERS |
67833bec MK |
713 | bit, which signals the futex owner that it cannot unlock the futex in |
714 | user space atomically by setting the futex value to 0. | |
715 | After that, the kernel tries to find the thread which is | |
716 | associated with the owner TID, | |
717 | .\" FIXME Could I get a bit more detail on the next two lines? | |
718 | .\" What is "creates or reuses kernel state" about? | |
719 | creates or reuses kernel state on behalf of the owner | |
720 | and attaches the waiter to it. | |
67259526 MK |
721 | .\" FIXME In the next line, what type of "priority" are we talking about? |
722 | .\" Realtime priorities for SCHED_FIFO and SCHED_RR? | |
723 | .\" Or something else? | |
e0547e70 TG |
724 | The enqueing of the waiter is in descending priority order if more |
725 | than one waiter exists. | |
67259526 | 726 | .\" FIXME What does "bandwidth" refer to in the next line? |
e0547e70 | 727 | The owner inherits either the priority or the bandwidth of the waiter. |
67259526 MK |
728 | .\" FIXME In the preceding line, what determines whether the |
729 | .\" owner inherits the priority versus the bandwidth? | |
67833bec MK |
730 | .\" |
731 | .\" FIXME Could I get some help translating the next sentence into | |
732 | .\" something that user-space developers (and I) can understand? | |
733 | .\" In particular, what are "nexted locks" in this context? | |
e0547e70 TG |
734 | This inheritance follows the lock chain in the case of |
735 | nested locking and performs deadlock detection. | |
736 | ||
9ce19cf1 MK |
737 | .\" FIXME tglx says "The timeout argument is handled as described in |
738 | .\" FUTEX_WAIT." However, it appears to me that this is not right. | |
739 | .\" Is the following formulation correct. | |
e0547e70 TG |
740 | The |
741 | .I timeout | |
9ce19cf1 MK |
742 | argument provides a timeout for the lock attempt. |
743 | It is interpreted as an absolute time, measured against the | |
744 | .BR CLOCK_REALTIME | |
745 | clock. | |
746 | If | |
747 | .I timeout | |
748 | is NULL, the operation will block indefinitely. | |
e0547e70 | 749 | |
a449c634 | 750 | The |
e0547e70 TG |
751 | .IR uaddr2 , |
752 | .IR val , | |
753 | and | |
754 | .IR val3 | |
a449c634 | 755 | arguments are ignored. |
fedaeaf3 | 756 | .\" FIXME |
a9dcb4d1 | 757 | .\" tglx noted the following "ERROR" case for FUTEX_LOCK_PI and |
670b34f8 MK |
758 | .\" FUTEX_TRYLOCK_PI and FUTEX_WAIT_REQUEUE_PI: |
759 | .\" | |
a9dcb4d1 MK |
760 | .\" > [EOWNERDIED] The owner of the futex died and the kernel made the |
761 | .\" > caller the new owner. The kernel sets the FUTEX_OWNER_DIED bit | |
762 | .\" > in the futex userspace value. Caller is responsible for cleanup | |
fedaeaf3 | 763 | .\" |
a9dcb4d1 | 764 | .\" However, there is no such thing as an EOWNERDIED error. I had a look |
fedaeaf3 MK |
765 | .\" through the kernel source for the FUTEX_OWNER_DIED cases and didn't |
766 | .\" see an obvious error associated with them. Can you clarify? (I think | |
767 | .\" the point is that this condition, which is described in | |
768 | .\" Documentation/robust-futexes.txt, is not an error as such. However, | |
769 | .\" I'm not yet sure of how to describe it in the man page.) | |
670b34f8 | 770 | .\" Suggestions please! |
67833bec | 771 | .\" |
d67e21f5 | 772 | .TP |
12fdbe23 | 773 | .BR FUTEX_TRYLOCK_PI " (since Linux 2.6.18)" |
d67e21f5 | 774 | .\" commit c87e2837be82df479a6bae9f155c43516d2feebc |
12fdbe23 MK |
775 | This operation tries to acquire the futex at |
776 | .IR uaddr . | |
0b761826 MK |
777 | .\" FIXME I think it would be helpful here to say a few more words about |
778 | .\" the difference(s) between FUTEX_LOCK_PI and FUTEX_TRYLOCK_PI | |
fa0388c3 | 779 | It deals with the situation where the TID value at |
12fdbe23 MK |
780 | .I uaddr |
781 | is 0, but the | |
b52e1cd4 | 782 | .B FUTEX_WAITERS |
12fdbe23 | 783 | bit is set. |
fa0388c3 MK |
784 | .\" FIXME How does the situation in the previous sentence come about? |
785 | .\" Probably it would be helpful to say something about that in | |
786 | .\" the man page. | |
badbf70c | 787 | .\" FIXME And *how* does FUTEX_TRYLOCK_PI deal with this situation? |
12fdbe23 | 788 | User space cannot handle this race free. |
084744ef MK |
789 | |
790 | The | |
791 | .IR uaddr2 , | |
792 | .IR val , | |
793 | .IR timeout , | |
794 | and | |
795 | .IR val3 | |
796 | arguments are ignored. | |
d67e21f5 | 797 | .TP |
12fdbe23 | 798 | .BR FUTEX_UNLOCK_PI " (since Linux 2.6.18)" |
d67e21f5 | 799 | .\" commit c87e2837be82df479a6bae9f155c43516d2feebc |
ecae2099 TG |
800 | This operation wakes the top priority waiter which is waiting in |
801 | .B FUTEX_LOCK_PI | |
802 | on the futex address provided by the | |
803 | .I uaddr | |
804 | argument. | |
805 | ||
806 | This is called when the user space value at | |
807 | .I uaddr | |
808 | cannot be changed atomically from a TID (of the owner) to 0. | |
809 | ||
810 | The | |
811 | .IR uaddr2 , | |
812 | .IR val , | |
813 | .IR timeout , | |
814 | and | |
815 | .IR val3 | |
11a194bf | 816 | arguments are ignored. |
d67e21f5 | 817 | .TP |
d67e21f5 MK |
818 | .BR FUTEX_CMP_REQUEUE_PI " (since Linux 2.6.31)" |
819 | .\" commit 52400ba946759af28442dee6265c5c0180ac7122 | |
820 | .\" FIXME to complete | |
f812a08b DH |
821 | This operation is a PI-aware variant of |
822 | .BR FUTEX_CMP_REQUEUE . | |
823 | It requeues waiters that are blocked via | |
824 | .B FUTEX_WAIT_REQUEUE_PI | |
825 | on | |
826 | .I uaddr | |
827 | from a non-PI source futex | |
828 | .RI ( uaddr ) | |
829 | to a PI target futex | |
830 | .RI ( uaddr2 ). | |
831 | ||
9e54d26d MK |
832 | As with |
833 | .BR FUTEX_CMP_REQUEUE , | |
834 | this operation wakes up a maximum of | |
835 | .I val | |
836 | waiters that are waiting on the futex at | |
837 | .IR uaddr . | |
838 | However, for | |
839 | .BR FUTEX_CMP_REQUEUE_PI , | |
840 | .I val | |
841 | is required to be 1. | |
842 | The remaining waiters are removed from the wait queue of the source futex at | |
843 | .I uaddr | |
844 | and added to the wait queue of the target futex at | |
845 | .IR uaddr2 . | |
f812a08b | 846 | |
9e54d26d | 847 | The |
768d3c23 | 848 | .I val2 |
c6d8cf21 MK |
849 | .\" val2 is the cap on the number of requeued waiters. |
850 | .\" In the glibc pthread_cond_broadcast() implementation, this argument | |
851 | .\" is specified as INT_MAX, and for pthread_cond_signal() it is 0. | |
9e54d26d | 852 | and |
768d3c23 | 853 | .I val3 |
9e54d26d MK |
854 | arguments serve the same purposes as for |
855 | .BR FUTEX_CMP_REQUEUE . | |
be376673 MK |
856 | .\" FIXME The page at http://locklessinc.com/articles/futex_cheat_sheet/ |
857 | .\" notes that "priority-inheritance Futex to priority-inheritance | |
858 | .\" Futex requeues are currently unsupported". Do we need to say | |
859 | .\" something in the man page about that? | |
d67e21f5 MK |
860 | .TP |
861 | .BR FUTEX_WAIT_REQUEUE_PI " (since Linux 2.6.31)" | |
862 | .\" commit 52400ba946759af28442dee6265c5c0180ac7122 | |
6ff1b4c0 TG |
863 | Wait operation to wait on a non-PI futex at |
864 | .I uaddr | |
865 | and potentially be requeued onto a PI futex at | |
866 | .IR uaddr2 . | |
867 | The wait operation on | |
868 | .I uaddr | |
869 | is the same as | |
870 | .BR FUTEX_WAIT . | |
871 | The waiter can be removed from the wait on | |
872 | .I uaddr | |
873 | via | |
874 | .BR FUTEX_WAKE | |
875 | without requeueing on | |
876 | .IR uaddr2 . | |
47297adb | 877 | .SH RETURN VALUE |
fea681da | 878 | .PP |
6f147f79 | 879 | In the event of an error, all operations return \-1 and set |
e808bba0 | 880 | .I errno |
6f147f79 | 881 | to indicate the cause of the error. |
e808bba0 MK |
882 | The return value on success depends on the operation, |
883 | as described in the following list: | |
fea681da MK |
884 | .TP |
885 | .B FUTEX_WAIT | |
682edefb MK |
886 | Returns 0 if the process was woken by a |
887 | .B FUTEX_WAKE | |
7446a837 MK |
888 | or |
889 | .B FUTEX_WAKE_BITSET | |
682edefb | 890 | call. |
fea681da MK |
891 | .TP |
892 | .B FUTEX_WAKE | |
893 | Returns the number of processes woken up. | |
894 | .TP | |
895 | .B FUTEX_FD | |
896 | Returns the new file descriptor associated with the futex. | |
897 | .TP | |
898 | .B FUTEX_REQUEUE | |
899 | Returns the number of processes woken up. | |
900 | .TP | |
901 | .B FUTEX_CMP_REQUEUE | |
3dfcc11d MK |
902 | Returns the total number of processes woken up or requeued to the futex at |
903 | .IR uaddr2 . | |
904 | If this value is greater than | |
905 | .IR val , | |
906 | then difference is the number of waiters requeued to the futex at | |
907 | .IR uaddr2 . | |
519f2c3d MK |
908 | .\" |
909 | .\" FIXME Add success returns for other operations | |
dcad19c0 MK |
910 | .TP |
911 | .B FUTEX_WAKE_OP | |
a8b5b324 MK |
912 | .\" FIXME Is the following correct? |
913 | Returns the total number of waiters that were woken up. | |
914 | This is the sum of the woken waiters on the two futexes at | |
915 | .I uaddr | |
916 | and | |
917 | .IR uaddr2 . | |
dcad19c0 MK |
918 | .TP |
919 | .B FUTEX_WAIT_BITSET | |
7bcc5351 MK |
920 | .\" FIXME Is the following correct? |
921 | Returns 0 if the process was woken by a | |
922 | .B FUTEX_WAKE | |
923 | or | |
924 | .B FUTEX_WAKE_BITSET | |
925 | call. | |
dcad19c0 MK |
926 | .TP |
927 | .B FUTEX_WAKE_BITSET | |
b884566b MK |
928 | .\" FIXME Is the following correct? |
929 | Returns the number of processes woken up. | |
dcad19c0 MK |
930 | .TP |
931 | .B FUTEX_LOCK_PI | |
bf02a260 MK |
932 | .\" FIXME Is the following correct? |
933 | Returns 0 if the futex was successfully locked. | |
dcad19c0 MK |
934 | .TP |
935 | .B FUTEX_TRYLOCK_PI | |
5c716eef MK |
936 | .\" FIXME Is the following correct? |
937 | Returns 0 if the futex was successfully locked. | |
dcad19c0 MK |
938 | .TP |
939 | .B FUTEX_UNLOCK_PI | |
52bb928f MK |
940 | .\" FIXME Is the following correct? |
941 | Returns 0 if the futex was successfully unlocked. | |
dcad19c0 MK |
942 | .TP |
943 | .B FUTEX_CMP_REQUEUE_PI | |
dddd395a MK |
944 | .\" FIXME Is the following correct? |
945 | Returns the total number of processes woken up or requeued to the futex at | |
946 | .IR uaddr2 . | |
947 | If this value is greater than | |
948 | .IR val , | |
949 | then difference is the number of waiters requeued to the futex at | |
950 | .IR uaddr2 . | |
dcad19c0 MK |
951 | .TP |
952 | .B FUTEX_WAIT_REQUEUE_PI | |
22c15de9 MK |
953 | .\" FIXME Is the following correct? |
954 | Returns 0 if the caller was successfully requeued to the futex at | |
955 | .IR uaddr2 . | |
fea681da MK |
956 | .SH ERRORS |
957 | .TP | |
958 | .B EACCES | |
959 | No read access to futex memory. | |
960 | .TP | |
961 | .B EAGAIN | |
badbf70c MK |
962 | .RB ( FUTEX_WAIT ) |
963 | The value pointed to by | |
964 | .I uaddr | |
965 | was not equal to the expected value | |
966 | .I val | |
967 | at the time of the call. | |
968 | .TP | |
969 | .B EAGAIN | |
8f2068bb MK |
970 | .RB ( FUTEX_CMP_REQUEUE , |
971 | .BR FUTEX_CMP_REQUEUE_PI ) | |
ce5602fd | 972 | The value pointed to by |
9f6c40c0 МК |
973 | .I uaddr |
974 | is not equal to the expected value | |
975 | .IR val3 . | |
fd1dc4c2 | 976 | .\" FIXME: Is the following sentence correct? |
fea681da | 977 | (This probably indicates a race; |
682edefb MK |
978 | use the safe |
979 | .B FUTEX_WAKE | |
980 | now.) | |
c0091dd3 MK |
981 | .\" |
982 | .\" FIXME Should there be an EAGAIN case for FUTEX_TRYLOCK_PI? | |
983 | .\" It seems so, looking at the handling of the rt_mutex_trylock() | |
984 | .\" call in futex_lock_pi() | |
985 | .\" | |
fea681da | 986 | .TP |
5662f56a MK |
987 | .BR EAGAIN |
988 | .RB ( FUTEX_LOCK_PI , | |
aaec9032 MK |
989 | .BR FUTEX_TRYLOCK_PI , |
990 | .BR FUTEX_CMP_REQUEUE_PI ) | |
991 | The futex owner thread ID of | |
992 | .I uaddr | |
993 | (for | |
994 | .BR FUTEX_CMP_REQUEUE_PI : | |
995 | .IR uaddr2 ) | |
996 | is about to exit, | |
5662f56a MK |
997 | but has not yet handled the internal state cleanup. |
998 | Try again. | |
61f8c1d1 MK |
999 | .\" |
1000 | .\" FIXME Is there not also an EAGAIN error case on 'uaddr2' for | |
1001 | .\" FUTEX_REQUEUE and FUTEX_CMP_REQUEUE via | |
1002 | .\" futex_requeue() ==> futex_proxy_trylock_atomic() ==> | |
1003 | .\" futex_lock_pi_atomic() ==> attach_to_pi_owner() ==> EAGAIN? | |
5662f56a | 1004 | .TP |
7a39e745 MK |
1005 | .BR EDEADLK |
1006 | .RB ( FUTEX_LOCK_PI , | |
1007 | .BR FUTEX_TRYLOCK_PI ) | |
1008 | The futex at | |
1009 | .I uaddr | |
1010 | is already locked by the caller. | |
d08ce5dd MK |
1011 | .\" |
1012 | .\" FIXME Is there not also an EDEADLK error case on 'uaddr2' for | |
1013 | .\" FUTEX_REQUEUE and FUTEX_CMP_REQUEUE via | |
1014 | .\" futex_requeue() ==> futex_proxy_trylock_atomic() ==> | |
1015 | .\" futex_lock_pi_atomic() ==> attach_to_pi_owner() ==> EDEADLK? | |
7a39e745 | 1016 | .TP |
662c0da8 MK |
1017 | .BR EDEADLK |
1018 | .\" FIXME I reworded tglx's text somewhat; is the following okay? | |
1019 | .RB ( FUTEX_CMP_REQUEUE_PI ) | |
1020 | While requeueing a waiter to the PI futex at | |
1021 | .IR uaddr2 , | |
1022 | the kernel detected a deadlock. | |
1023 | .TP | |
fea681da | 1024 | .B EFAULT |
1ea901e8 MK |
1025 | A required pointer argument (i.e., |
1026 | .IR uaddr , | |
1027 | .IR uaddr2 , | |
1028 | or | |
1029 | .IR timeout ) | |
496df304 | 1030 | did not point to a valid user-space address. |
fea681da | 1031 | .TP |
9f6c40c0 | 1032 | .B EINTR |
e808bba0 | 1033 | A |
9f6c40c0 | 1034 | .B FUTEX_WAIT |
2674f781 MK |
1035 | or |
1036 | .B FUTEX_WAIT_BITSET | |
e808bba0 MK |
1037 | operation was interrupted by a signal (see |
1038 | .BR signal (7)) | |
1039 | or a spurious wakeup. | |
5eeca856 MK |
1040 | .\" FIXME |
1041 | .\" Regarding the words "spurious wakeup" above, I received this | |
1042 | .\" bug report from Rich Felker: | |
1043 | .\" | |
1044 | .\" I see no code in the kernel whereby a "spurious wakeup", or anything | |
1045 | .\" other than interruption by a signal handler that's not SA_RESTART, | |
1046 | .\" can cause futex to fail with EINTR. In general, overloading of EINTR | |
1047 | .\" and/or spurious EINTRs from a syscall make it impossible to use that | |
1048 | .\" syscall for implementing any function where EINTR is a mandatory | |
1049 | .\" failure on interruption-by-signal, since there is no way for | |
1050 | .\" userspace to distinguish whether the EINTR occurred as a result of | |
1051 | .\" an interrupting signal or some other reason. The kernel folks have | |
1052 | .\" gone to great lengths to fix spurious EINTRs (see signal(7) for | |
1053 | .\" history), especially by non-interrupting signal handlers, including | |
1054 | .\" in futex, and allowing EINTR here would be contrary to that goal. | |
1055 | .\" | |
1056 | .\" It's my belief that the "or a spurious wakeup" text should simply be | |
1057 | .\" removed. | |
1058 | .\" | |
1059 | .\" The reason I'm raising this topic is its relevance to a thread on | |
1060 | .\" libc-alpha: | |
1061 | .\" [RFC] mutex destruction (#13690): problem description and workarounds | |
1062 | .\" | |
1063 | .\" The bug and mailing list discussions to which Rich refers are: | |
1064 | .\" https://sourceware.org/bugzilla/show_bug.cgi?id=13690 | |
1065 | .\" https://sourceware.org/ml/libc-alpha/2014-12/threads.html#0001 | |
1066 | .\" | |
1067 | .\" Can anyone comment on whether the words "spurious wakeup" are correct? | |
1068 | .\" | |
9f6c40c0 | 1069 | .TP |
fea681da | 1070 | .B EINVAL |
180f97b7 MK |
1071 | The operation in |
1072 | .IR futex_op | |
1073 | is one of those that employs a timeout, but the supplied | |
fb2f4c27 MK |
1074 | .I timeout |
1075 | argument was invalid | |
1076 | .RI ( tv_sec | |
1077 | was less than zero, or | |
1078 | .IR tv_nsec | |
1079 | was not less than 1000,000,000). | |
1080 | .TP | |
1081 | .B EINVAL | |
0c74df0b | 1082 | The operation specified in |
025e1374 | 1083 | .IR futex_op |
0c74df0b | 1084 | employs one or both of the pointers |
51ee94be | 1085 | .I uaddr |
a1f47699 | 1086 | and |
0c74df0b MK |
1087 | .IR uaddr2 , |
1088 | but one of these does not point to a valid object\(emthat is, | |
1089 | the address is not four-byte-aligned. | |
51ee94be MK |
1090 | .TP |
1091 | .B EINVAL | |
55cc422d TG |
1092 | .RB ( FUTEX_WAIT_BITSET , |
1093 | .BR FUTEX_WAKE_BITSET ) | |
79c9b436 TG |
1094 | The bitset supplied in |
1095 | .IR val3 | |
1096 | is zero. | |
1097 | .TP | |
1098 | .B EINVAL | |
2043f2c1 MK |
1099 | .RB ( FUTEX_REQUEUE , |
1100 | .\" FIXME tglx suggested adding this, but does this error really occur for | |
1101 | .\" FUTEX_REQUEUE? (The case where it occurs for FUTEX_CMP_REQUEUE_PI | |
1102 | .\" is obvious at the start of futex_requeue().) | |
1103 | .BR FUTEX_CMP_REQUEUE_PI ) | |
add875c0 MK |
1104 | .I uaddr |
1105 | equals | |
1106 | .IR uaddr2 | |
1107 | (i.e., an attempt was made to requeue to the same futex). | |
1108 | .TP | |
ff597681 MK |
1109 | .BR EINVAL |
1110 | .RB ( FUTEX_FD ) | |
1111 | The signal number supplied in | |
1112 | .I val | |
1113 | is invalid. | |
1114 | .TP | |
6bac3b85 | 1115 | .B EINVAL |
476debd7 MK |
1116 | .RB ( FUTEX_WAKE , |
1117 | .BR FUTEX_WAKE_OP , | |
1118 | .BR FUTEX_WAKE_BITSET , | |
1119 | .BR FUTEX_REQUEUE , | |
1120 | .BR FUTEX_CMP_REQUEUE ) | |
1121 | The kernel detected an inconsistency between the user-space state at | |
1122 | .I uaddr | |
1123 | and the kernel state\(emthat is, it detected a waiter which waits in | |
1124 | .BR FUTEX_LOCK_PI | |
1125 | on | |
1126 | .IR uaddr . | |
1127 | .TP | |
1128 | .B EINVAL | |
a218ef20 | 1129 | .RB ( FUTEX_LOCK_PI , |
ce022f18 MK |
1130 | .BR FUTEX_TRYLOCK_PI , |
1131 | .BR FUTEX_UNLOCK_PI ) | |
a218ef20 MK |
1132 | The kernel detected an inconsistency between the user-space state at |
1133 | .I uaddr | |
1134 | and the kernel state. | |
ce022f18 MK |
1135 | This indicates either state corruption |
1136 | .\" FIXME tglx did not mention the "state corruption" for FUTEX_UNLOCK_PI. | |
1137 | .\" Does that case also apply for FUTEX_UNLOCK_PI? | |
1138 | or that the kernel found a waiter on | |
a218ef20 MK |
1139 | .I uaddr |
1140 | which is waiting via | |
1141 | .BR FUTEX_WAIT | |
1142 | or | |
1143 | .BR FUTEX_WAIT_BITSET . | |
1144 | .TP | |
1145 | .B EINVAL | |
f9250b1a MK |
1146 | .RB ( FUTEX_CMP_REQUEUE_PI ) |
1147 | The kernel detected an inconsistency between the user-space state at | |
99c0041d MK |
1148 | .I uaddr2 |
1149 | and the kernel state; | |
1150 | that is, the kernel detected a waiter which waits via | |
1151 | .BR FUTEX_WAIT | |
1152 | .\" FIXME tglx did not mention FUTEX_WAIT_BITSET here, | |
1153 | .\" but should that not also be included here? | |
1154 | on | |
1155 | .IR uaddr2 . | |
1156 | .TP | |
1157 | .B EINVAL | |
1158 | .RB ( FUTEX_CMP_REQUEUE_PI ) | |
1159 | The kernel detected an inconsistency between the user-space state at | |
f9250b1a MK |
1160 | .I uaddr |
1161 | and the kernel state; | |
1162 | that is, the kernel detected a waiter which waits via | |
99c0041d MK |
1163 | .BR FUTEX_LOCK_PI , |
1164 | .BR FUTEX_WAIT , | |
1165 | or | |
1166 | .BR FUTEX_WAIT_BITSET , | |
f9250b1a MK |
1167 | on |
1168 | .IR uaddr . | |
1169 | .TP | |
1170 | .B EINVAL | |
99c0041d MK |
1171 | .RB ( FUTEX_CMP_REQUEUE_PI ) |
1172 | .TP | |
1173 | .B EINVAL | |
4832b48a | 1174 | Invalid argument. |
fea681da | 1175 | .TP |
a449c634 MK |
1176 | .BR ENOMEM |
1177 | .RB ( FUTEX_LOCK_PI , | |
e34a8fb6 MK |
1178 | .BR FUTEX_TRYLOCK_PI , |
1179 | .BR FUTEX_CMP_REQUEUE_PI ) | |
a449c634 MK |
1180 | The kernel could not allocate memory to hold state information. |
1181 | .TP | |
fea681da | 1182 | .B ENFILE |
ff597681 | 1183 | .RB ( FUTEX_FD ) |
fea681da | 1184 | The system limit on the total number of open files has been reached. |
4701fc28 MK |
1185 | .TP |
1186 | .B ENOSYS | |
1187 | Invalid operation specified in | |
d33602c4 | 1188 | .IR futex_op . |
9f6c40c0 | 1189 | .TP |
4a7e5b05 MK |
1190 | .B ENOSYS |
1191 | The | |
1192 | .BR FUTEX_CLOCK_REALTIME | |
1193 | option was specified in | |
1afcee7c | 1194 | .IR futex_op , |
4a7e5b05 MK |
1195 | but the accompanying operation was neither |
1196 | .BR FUTEX_WAIT_BITSET | |
1197 | nor | |
1198 | .BR FUTEX_WAIT_REQUEUE_PI . | |
1199 | .TP | |
a9dcb4d1 MK |
1200 | .BR ENOSYS |
1201 | .RB ( FUTEX_LOCK_PI , | |
f2424fae | 1202 | .BR FUTEX_TRYLOCK_PI , |
4945ff19 | 1203 | .BR FUTEX_UNLOCK_PI , |
794bb106 MK |
1204 | .BR FUTEX_CMP_REQUEUE_PI |
1205 | .BR FUTEX_WAIT_REQUEUE_PI ) | |
a9dcb4d1 | 1206 | A run-time check determined that the operation not available. |
a2ebebcd MK |
1207 | The PI futex operations are not implemented on all architectures and |
1208 | are not supported on some CPU variants. | |
a9dcb4d1 | 1209 | .TP |
c7589177 MK |
1210 | .BR EPERM |
1211 | .RB ( FUTEX_LOCK_PI , | |
dc2742a8 MK |
1212 | .BR FUTEX_TRYLOCK_PI , |
1213 | .BR FUTEX_CMP_REQUEUE_PI ) | |
04331c3f | 1214 | The caller is not allowed to attach itself to the futex at |
dc2742a8 MK |
1215 | .I uaddr |
1216 | (for | |
1217 | .BR FUTEX_CMP_REQUEUE_PI : | |
1218 | the futex at | |
1219 | .IR uaddr2 ). | |
c7589177 | 1220 | (This may be caused by a state corruption in user space.) |
61f8c1d1 MK |
1221 | .\" |
1222 | .\" FIXME Is there not also an EPERM error case on 'uaddr2' for | |
1223 | .\" FUTEX_REQUEUE and FUTEX_CMP_REQUEUE via | |
1224 | .\" futex_requeue() ==> futex_proxy_trylock_atomic() ==> | |
1225 | .\" futex_lock_pi_atomic() ==> attach_to_pi_owner() ==> EPERM? | |
c7589177 | 1226 | .TP |
76f347ba | 1227 | .BR EPERM |
87276709 | 1228 | .RB ( FUTEX_UNLOCK_PI ) |
76f347ba MK |
1229 | The caller does not own the futex. |
1230 | .TP | |
0b0e4934 MK |
1231 | .BR ESRCH |
1232 | .RB ( FUTEX_LOCK_PI , | |
1233 | .BR FUTEX_TRYLOCK_PI ) | |
1234 | .\" FIXME I reworded the following sentence a bit differently from | |
1235 | .\" tglx's formulation. Is it okay? | |
1236 | The thread ID in the futex at | |
1237 | .I uaddr | |
1238 | does not exist. | |
61f8c1d1 MK |
1239 | .\" |
1240 | .\" FIXME Is there not also an ESRCH error case on 'uaddr2' for | |
1241 | .\" FUTEX_REQUEUE and FUTEX_CMP_REQUEUE via | |
1242 | .\" futex_requeue() ==> futex_proxy_trylock_atomic() ==> | |
1243 | .\" futex_lock_pi_atomic() ==> attach_to_pi_owner() ==> ESRCH? | |
0b0e4934 | 1244 | .TP |
360f773c MK |
1245 | .BR ESRCH |
1246 | .RB ( FUTEX_CMP_REQUEUE_PI ) | |
1247 | .\" FIXME I reworded the following sentence a bit differently from | |
1248 | .\" tglx's formulation. Is it okay? | |
1249 | The thread ID in the futex at | |
1250 | .I uaddr2 | |
1251 | does not exist. | |
1252 | .TP | |
9f6c40c0 | 1253 | .B ETIMEDOUT |
4d85047f MK |
1254 | The operation in |
1255 | .IR futex_op | |
1256 | employed the timeout specified in | |
1257 | .IR timeout , | |
1258 | and the timeout expired before the operation completed. | |
47297adb | 1259 | .SH VERSIONS |
a1d5f77c | 1260 | .PP |
81c9d87e MK |
1261 | Futexes were first made available in a stable kernel release |
1262 | with Linux 2.6.0. | |
1263 | ||
a1d5f77c MK |
1264 | Initial futex support was merged in Linux 2.5.7 but with different semantics |
1265 | from what was described above. | |
52dee70e | 1266 | A four-argument system call with the semantics |
fd3fa7ef | 1267 | described in this page was introduced in Linux 2.5.40. |
11b520ed | 1268 | In Linux 2.5.70, one argument |
a1d5f77c | 1269 | was added. |
11b520ed | 1270 | In Linux 2.6.7, a sixth argument was added\(emmessy, especially |
a1d5f77c | 1271 | on the s390 architecture. |
47297adb | 1272 | .SH CONFORMING TO |
8382f16d | 1273 | This system call is Linux-specific. |
47297adb | 1274 | .SH NOTES |
fea681da | 1275 | .PP |
fcdad7d6 | 1276 | To reiterate, bare futexes are not intended as an easy-to-use abstraction |
c13182ef | 1277 | for end-users. |
fcdad7d6 | 1278 | (There is no wrapper function for this system call in glibc.) |
c13182ef | 1279 | Implementors are expected to be assembly literate and to have |
7fac88a9 | 1280 | read the sources of the futex user-space library referenced below. |
d282bb24 | 1281 | .\" .SH AUTHORS |
fea681da MK |
1282 | .\" .PP |
1283 | .\" Futexes were designed and worked on by | |
1284 | .\" Hubertus Franke (IBM Thomas J. Watson Research Center), | |
1285 | .\" Matthew Kirkwood, Ingo Molnar (Red Hat) | |
1286 | .\" and Rusty Russell (IBM Linux Technology Center). | |
1287 | .\" This page written by bert hubert. | |
47297adb | 1288 | .SH SEE ALSO |
9913033c | 1289 | .BR get_robust_list (2), |
d806bc05 | 1290 | .BR restart_syscall (2), |
14d8dd3b | 1291 | .BR futex (7) |
fea681da | 1292 | .PP |
f5ad572f MK |
1293 | The following kernel source files: |
1294 | .IP * 2 | |
1295 | .I Documentation/pi-futex.txt | |
1296 | .IP * | |
1297 | .I Documentation/futex-requeue-pi.txt | |
1298 | .IP * | |
1299 | .I Documentation/locking/rt-mutex.txt | |
1300 | .IP * | |
1301 | .I Documentation/locking/rt-mutex-design.txt | |
43b99089 | 1302 | .PP |
52087dd3 | 1303 | \fIFuss, Futexes and Furwocks: Fast Userlevel Locking in Linux\fP |
9b936e9e MK |
1304 | (proceedings of the Ottawa Linux Symposium 2002), online at |
1305 | .br | |
608bf950 SK |
1306 | .UR http://kernel.org\:/doc\:/ols\:/2002\:/ols2002-pages-479-495.pdf |
1307 | .UE | |
f42eb21b | 1308 | |
2ed26199 MK |
1309 | \fIA futex overview and update\fP, 11 November 2009 |
1310 | .UR http://lwn.net/Articles/360699/ | |
1311 | .UE | |
1312 | ||
0483b6cc MK |
1313 | \fIRequeue-PI: Making Glibc Condvars PI-Aware\fP |
1314 | (2009 Real-Time Linux Workshop) | |
1315 | .UR http://lwn.net/images/conf/rtlws11/papers/proc/p10.pdf | |
1316 | .UE | |
1317 | ||
f42eb21b MK |
1318 | \fIFutexes Are Tricky\fP (updated in 2011), Ulrich Drepper |
1319 | .UR http://www.akkadia.org/drepper/futex.pdf | |
1320 | .UE | |
9b936e9e MK |
1321 | .PP |
1322 | Futex example library, futex-*.tar.bz2 at | |
1323 | .br | |
a605264d | 1324 | .UR ftp://ftp.kernel.org\:/pub\:/linux\:/kernel\:/people\:/rusty/ |
608bf950 | 1325 | .UE |
34f14794 MK |
1326 | .\" |
1327 | .\" FIXME Are there any other resources that should be listed | |
1328 | .\" in the SEE ALSO section? |