Fix: futex can be free'd while used by waker thread
[lttng-tools.git] / src / common / futex.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _LGPL_SOURCE
20 #include <limits.h>
21 #include <unistd.h>
22 #include <urcu.h>
23 #include <urcu/futex.h>
24
25 #include <common/common.h>
26
27 #include "futex.h"
28
29 /*
30 * This futex wait/wake scheme only works for N wakers / 1 waiters. Hence the
31 * "nto1" added to all function signature.
32 *
33 * The code is adapted from the adaptative busy wait/wake_up scheme used in
34 * liburcu.
35 */
36
37 /* Number of busy-loop attempts before waiting on futex. */
38 #define FUTEX_WAIT_ATTEMPTS 1000
39
40 enum futex_wait_state {
41 /* FUTEX_WAIT_WAITING is compared directly (futex() compares it). */
42 FUTEX_WAIT_WAITING = 0,
43 /* non-zero are used as masks. */
44 FUTEX_WAIT_WAKEUP = (1 << 0),
45 FUTEX_WAIT_RUNNING = (1 << 1),
46 FUTEX_WAIT_TEARDOWN = (1 << 2),
47 };
48
49 /*
50 * Update futex according to active or not. This scheme is used to wake every
51 * libust waiting on the shared memory map futex hence the INT_MAX used in the
52 * futex() call. If active, we set the value and wake everyone else we indicate
53 * that we are gone (cleanup() case).
54 */
55 LTTNG_HIDDEN
56 void futex_wait_update(int32_t *futex, int active)
57 {
58 if (active) {
59 uatomic_set(futex, 1);
60 if (futex_async(futex, FUTEX_WAKE,
61 INT_MAX, NULL, NULL, 0) < 0) {
62 PERROR("futex_async");
63 abort();
64 }
65 } else {
66 uatomic_set(futex, 0);
67 }
68
69 DBG("Futex wait update active %d", active);
70 }
71
72 /*
73 * Prepare futex.
74 */
75 LTTNG_HIDDEN
76 void futex_nto1_prepare(int32_t *futex)
77 {
78 uatomic_set(futex, FUTEX_WAIT_WAITING);
79 cmm_smp_mb();
80
81 DBG("Futex n to 1 prepare done");
82 }
83
84 /*
85 * Wait futex.
86 */
87 LTTNG_HIDDEN
88 void futex_nto1_wait(int32_t *futex)
89 {
90 unsigned int i;
91
92 /* Load and test condition before read state */
93 cmm_smp_rmb();
94 for (i = 0; i < FUTEX_WAIT_ATTEMPTS; i++) {
95 if (uatomic_read(futex) != FUTEX_WAIT_WAITING)
96 goto skip_futex_wait;
97 caa_cpu_relax();
98 }
99 while (futex_noasync(futex, FUTEX_WAIT, FUTEX_WAIT_WAITING,
100 NULL, NULL, 0)) {
101 switch (errno) {
102 case EWOULDBLOCK:
103 /* Value already changed. */
104 goto skip_futex_wait;
105 case EINTR:
106 /* Retry if interrupted by signal. */
107 break; /* Get out of switch. */
108 default:
109 /* Unexpected error. */
110 PERROR("futex");
111 abort();
112 }
113 }
114 skip_futex_wait:
115
116 /* Tell waker thread than we are running. */
117 uatomic_or(futex, FUTEX_WAIT_RUNNING);
118
119 /*
120 * Wait until waker thread lets us know it's ok to tear down
121 * memory allocated for the futex.
122 */
123 for (i = 0; i < FUTEX_WAIT_ATTEMPTS; i++) {
124 if (uatomic_read(futex) & FUTEX_WAIT_TEARDOWN)
125 break;
126 caa_cpu_relax();
127 }
128 while (!(uatomic_read(futex) & FUTEX_WAIT_TEARDOWN))
129 poll(NULL, 0, 10);
130 assert(uatomic_read(futex) & FUTEX_WAIT_TEARDOWN);
131 DBG("Futex n to 1 wait done");
132 }
133
134 /*
135 * Wake 1 futex.
136 */
137 LTTNG_HIDDEN
138 void futex_nto1_wake(int32_t *futex)
139 {
140 cmm_smp_mb();
141 uatomic_set(futex, FUTEX_WAIT_WAKEUP);
142 if (!(uatomic_read(futex) & FUTEX_WAIT_RUNNING)) {
143 if (futex_noasync(futex, FUTEX_WAKE, 1,
144 NULL, NULL, 0) < 0) {
145 PERROR("futex_noasync");
146 abort();
147 }
148 }
149 /* Allow teardown of futex. */
150 uatomic_or(futex, FUTEX_WAIT_TEARDOWN);
151 DBG("Futex n to 1 wake done");
152 }
This page took 0.031146 seconds and 4 git commands to generate.