Fix: futex wait: handle spurious futex wakeups
[lttng-tools.git] / src / common / futex.c
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include <limits.h>
11 #include <unistd.h>
12 #include <urcu.h>
13 #include <urcu/futex.h>
14
15 #include <common/common.h>
16
17 #include "futex.h"
18
19 /*
20 * This futex wait/wake scheme only works for N wakers / 1 waiters. Hence the
21 * "nto1" added to all function signature.
22 *
23 * Please see wait_gp()/update_counter_and_wait() calls in urcu.c in the urcu
24 * git tree for a detail example of this scheme being used. futex_async() is
25 * the urcu wrapper over the futex() sycall.
26 *
27 * There is also a formal verification available in the git tree.
28 *
29 * branch: formal-model
30 * commit id: 2a8044f3493046fcc8c67016902dc7beec6f026a
31 *
32 * Ref: git://git.lttng.org/userspace-rcu.git
33 */
34
35 /*
36 * Update futex according to active or not. This scheme is used to wake every
37 * libust waiting on the shared memory map futex hence the INT_MAX used in the
38 * futex() call. If active, we set the value and wake everyone else we indicate
39 * that we are gone (cleanup() case).
40 */
41 LTTNG_HIDDEN
42 void futex_wait_update(int32_t *futex, int active)
43 {
44 if (active) {
45 uatomic_set(futex, 1);
46 if (futex_async(futex, FUTEX_WAKE,
47 INT_MAX, NULL, NULL, 0) < 0) {
48 PERROR("futex_async");
49 abort();
50 }
51 } else {
52 uatomic_set(futex, 0);
53 }
54
55 DBG("Futex wait update active %d", active);
56 }
57
58 /*
59 * Prepare futex.
60 */
61 LTTNG_HIDDEN
62 void futex_nto1_prepare(int32_t *futex)
63 {
64 uatomic_set(futex, -1);
65 cmm_smp_mb();
66
67 DBG("Futex n to 1 prepare done");
68 }
69
70 /*
71 * Wait futex.
72 */
73 LTTNG_HIDDEN
74 void futex_nto1_wait(int32_t *futex)
75 {
76 cmm_smp_mb();
77
78 while (uatomic_read(futex) == -1) {
79 if (!futex_async(futex, FUTEX_WAIT, -1, NULL, NULL, 0)) {
80 /*
81 * Prior queued wakeups queued by unrelated code
82 * using the same address can cause futex wait to
83 * return 0 even through the futex value is still
84 * -1 (spurious wakeups). Check the value again
85 * in user-space to validate whether it really
86 * differs from -1.
87 */
88 continue;
89 }
90 switch (errno) {
91 case EAGAIN:
92 /* Value already changed. */
93 goto end;
94 case EINTR:
95 /* Retry if interrupted by signal. */
96 break; /* Get out of switch. Check again. */
97 default:
98 /* Unexpected error. */
99 PERROR("futex_async");
100 abort();
101 }
102 }
103 end:
104 DBG("Futex n to 1 wait done");
105 }
106
107 /*
108 * Wake 1 futex.
109 */
110 LTTNG_HIDDEN
111 void futex_nto1_wake(int32_t *futex)
112 {
113 if (caa_unlikely(uatomic_read(futex) != -1))
114 goto end;
115 uatomic_set(futex, 0);
116 if (futex_async(futex, FUTEX_WAKE, 1, NULL, NULL, 0) < 0) {
117 PERROR("futex_async");
118 abort();
119 }
120 end:
121 DBG("Futex n to 1 wake done");
122 }
This page took 0.031424 seconds and 4 git commands to generate.