Fix: futex can be free'd while used by waker thread
[lttng-tools.git] / src / common / futex.c
CommitLineData
099e26bd
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
d14d33bf
AM
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.
099e26bd
DG
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
d14d33bf 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
099e26bd
DG
12 * GNU General Public License for more details.
13 *
d14d33bf
AM
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.
099e26bd
DG
17 */
18
6c1c0768 19#define _LGPL_SOURCE
0fdd1e2c 20#include <limits.h>
099e26bd
DG
21#include <unistd.h>
22#include <urcu.h>
23#include <urcu/futex.h>
24
90e535ef 25#include <common/common.h>
099e26bd
DG
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 *
dce89628
JG
33 * The code is adapted from the adaptative busy wait/wake_up scheme used in
34 * liburcu.
099e26bd
DG
35 */
36
dce89628
JG
37/* Number of busy-loop attempts before waiting on futex. */
38#define FUTEX_WAIT_ATTEMPTS 1000
39
40enum 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
0fdd1e2c
DG
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 */
90e535ef 55LTTNG_HIDDEN
0fdd1e2c
DG
56void futex_wait_update(int32_t *futex, int active)
57{
58 if (active) {
59 uatomic_set(futex, 1);
549731b7
MD
60 if (futex_async(futex, FUTEX_WAKE,
61 INT_MAX, NULL, NULL, 0) < 0) {
62 PERROR("futex_async");
63 abort();
64 }
0fdd1e2c
DG
65 } else {
66 uatomic_set(futex, 0);
67 }
68
69 DBG("Futex wait update active %d", active);
70}
71
099e26bd
DG
72/*
73 * Prepare futex.
74 */
90e535ef 75LTTNG_HIDDEN
099e26bd
DG
76void futex_nto1_prepare(int32_t *futex)
77{
dce89628 78 uatomic_set(futex, FUTEX_WAIT_WAITING);
099e26bd
DG
79 cmm_smp_mb();
80
81 DBG("Futex n to 1 prepare done");
82}
83
84/*
85 * Wait futex.
86 */
90e535ef 87LTTNG_HIDDEN
099e26bd
DG
88void futex_nto1_wait(int32_t *futex)
89{
dce89628 90 unsigned int i;
099e26bd 91
dce89628
JG
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)) {
549731b7
MD
101 switch (errno) {
102 case EWOULDBLOCK:
103 /* Value already changed. */
dce89628 104 goto skip_futex_wait;
549731b7
MD
105 case EINTR:
106 /* Retry if interrupted by signal. */
107 break; /* Get out of switch. */
108 default:
109 /* Unexpected error. */
dce89628 110 PERROR("futex");
549731b7
MD
111 abort();
112 }
099e26bd 113 }
dce89628
JG
114skip_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);
099e26bd
DG
131 DBG("Futex n to 1 wait done");
132}
133
134/*
135 * Wake 1 futex.
136 */
90e535ef 137LTTNG_HIDDEN
099e26bd
DG
138void futex_nto1_wake(int32_t *futex)
139{
dce89628
JG
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 }
099e26bd 148 }
dce89628
JG
149 /* Allow teardown of futex. */
150 uatomic_or(futex, FUTEX_WAIT_TEARDOWN);
099e26bd
DG
151 DBG("Futex n to 1 wake done");
152}
This page took 0.047696 seconds and 4 git commands to generate.