Fix: waiter: futex wait: handle spurious futex wakeups
[lttng-tools.git] / src / common / waiter.c
CommitLineData
287a512f 1/*
ab5be9fa
MJ
2 * Copyright (C) 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
287a512f 4 *
ab5be9fa 5 * SPDX-License-Identifier: LGPL-2.1-only
287a512f 6 *
287a512f
JG
7 */
8
9#include "waiter.h"
10#include <urcu/uatomic.h>
11#include <urcu/futex.h>
12#include <assert.h>
13#include "error.h"
14#include <poll.h>
15
16/*
17 * Number of busy-loop attempts before waiting on futex.
18 */
19#define WAIT_ATTEMPTS 1000
20
21enum waiter_state {
22 /* WAITER_WAITING is compared directly (futex compares it). */
23 WAITER_WAITING = 0,
24 /* non-zero are used as masks. */
25 WAITER_WOKEN_UP = (1 << 0),
26 WAITER_RUNNING = (1 << 1),
27 WAITER_TEARDOWN = (1 << 2),
28};
29
30LTTNG_HIDDEN
31void lttng_waiter_init(struct lttng_waiter *waiter)
32{
33 cds_wfs_node_init(&waiter->wait_queue_node);
34 uatomic_set(&waiter->state, WAITER_WAITING);
35 cmm_smp_mb();
36}
37
38/*
39 * User must init "waiter" before passing its memory to waker thread.
40 */
41LTTNG_HIDDEN
42void lttng_waiter_wait(struct lttng_waiter *waiter)
43{
44 unsigned int i;
45
46 DBG("Beginning of waiter wait period");
47 /* Load and test condition before read state */
48 cmm_smp_rmb();
49 for (i = 0; i < WAIT_ATTEMPTS; i++) {
50 if (uatomic_read(&waiter->state) != WAITER_WAITING) {
51 goto skip_futex_wait;
52 }
53 caa_cpu_relax();
54 }
1824a395
MD
55 while (uatomic_read(&waiter->state) == WAITER_WAITING) {
56 if (!futex_noasync(&waiter->state, FUTEX_WAIT, WAITER_WAITING, NULL, NULL, 0)) {
57 /*
58 * Prior queued wakeups queued by unrelated code
59 * using the same address can cause futex wait to
60 * return 0 even through the futex value is still
61 * WAITER_WAITING (spurious wakeups). Check
62 * the value again in user-space to validate
63 * whether it really differs from WAITER_WAITING.
64 */
65 continue;
66 }
287a512f 67 switch (errno) {
1824a395 68 case EAGAIN:
287a512f
JG
69 /* Value already changed. */
70 goto skip_futex_wait;
71 case EINTR:
72 /* Retry if interrupted by signal. */
1824a395 73 break; /* Get out of switch. Check again. */
287a512f
JG
74 default:
75 /* Unexpected error. */
76 PERROR("futex_noasync");
77 abort();
78 }
79 }
80skip_futex_wait:
81
82 /* Tell waker thread than we are running. */
83 uatomic_or(&waiter->state, WAITER_RUNNING);
84
85 /*
86 * Wait until waker thread lets us know it's ok to tear down
87 * memory allocated for struct lttng_waiter.
88 */
89 for (i = 0; i < WAIT_ATTEMPTS; i++) {
90 if (uatomic_read(&waiter->state) & WAITER_TEARDOWN) {
91 break;
92 }
93 caa_cpu_relax();
94 }
95 while (!(uatomic_read(&waiter->state) & WAITER_TEARDOWN)) {
96 poll(NULL, 0, 10);
97 }
98 assert(uatomic_read(&waiter->state) & WAITER_TEARDOWN);
99 DBG("End of waiter wait period");
100}
101
102/*
103 * Note: lttng_waiter_wake needs waiter to stay allocated throughout its
104 * execution. In this scheme, the waiter owns the node memory, and we only allow
105 * it to free this memory when it sees the WAITER_TEARDOWN flag.
106 */
107LTTNG_HIDDEN
108void lttng_waiter_wake_up(struct lttng_waiter *waiter)
109{
110 cmm_smp_mb();
111 assert(uatomic_read(&waiter->state) == WAITER_WAITING);
112 uatomic_set(&waiter->state, WAITER_WOKEN_UP);
113 if (!(uatomic_read(&waiter->state) & WAITER_RUNNING)) {
114 if (futex_noasync(&waiter->state, FUTEX_WAKE, 1,
115 NULL, NULL, 0) < 0) {
116 PERROR("futex_noasync");
117 abort();
118 }
119 }
120 /* Allow teardown of struct urcu_wait memory. */
121 uatomic_or(&waiter->state, WAITER_TEARDOWN);
122}
This page took 0.05072 seconds and 4 git commands to generate.