Commit | Line | Data |
---|---|---|
48363c84 MD |
1 | // SPDX-License-Identifier: MIT |
2 | /* | |
3 | * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | */ | |
5 | ||
6 | #include <sched.h> | |
054b7b5c | 7 | #include <string.h> |
48363c84 MD |
8 | #include <stdint.h> |
9 | #include <pthread.h> | |
10 | #include <stdbool.h> | |
11 | #include <poll.h> | |
054b7b5c | 12 | #include <stdlib.h> |
5a76c31e MD |
13 | #include <unistd.h> |
14 | #include <sys/syscall.h> | |
15 | #include <linux/membarrier.h> | |
48363c84 MD |
16 | |
17 | #include "rcu.h" | |
054b7b5c | 18 | #include "smp.h" |
48363c84 | 19 | |
5a76c31e MD |
20 | static int |
21 | membarrier(int cmd, unsigned int flags, int cpu_id) | |
22 | { | |
23 | return syscall(__NR_membarrier, cmd, flags, cpu_id); | |
24 | } | |
25 | ||
48363c84 MD |
26 | /* active_readers is an input/output parameter. */ |
27 | static | |
28 | void check_active_readers(struct side_rcu_gp_state *gp_state, bool *active_readers) | |
29 | { | |
30 | uintptr_t sum[2] = { 0, 0 }; /* begin - end */ | |
31 | int i; | |
32 | ||
33 | for (i = 0; i < gp_state->nr_cpus; i++) { | |
34 | struct side_rcu_cpu_gp_state *cpu_state = &gp_state->percpu_state[i]; | |
35 | ||
7fb53c62 | 36 | if (active_readers[0]) { |
48363c84 | 37 | sum[0] -= __atomic_load_n(&cpu_state->count[0].end, __ATOMIC_RELAXED); |
7fb53c62 MD |
38 | sum[0] -= __atomic_load_n(&cpu_state->count[0].rseq_end, __ATOMIC_RELAXED); |
39 | } | |
40 | if (active_readers[1]) { | |
48363c84 | 41 | sum[1] -= __atomic_load_n(&cpu_state->count[1].end, __ATOMIC_RELAXED); |
7fb53c62 MD |
42 | sum[1] -= __atomic_load_n(&cpu_state->count[1].rseq_end, __ATOMIC_RELAXED); |
43 | } | |
48363c84 MD |
44 | } |
45 | ||
46 | /* | |
47 | * This memory barrier (C) pairs with either of memory barriers | |
48 | * (A) or (B) (one is sufficient). | |
49 | * | |
50 | * Read end counts before begin counts. Reading "end" before | |
51 | * "begin" counts ensures we never see an "end" without having | |
52 | * seen its associated "begin", because "begin" is always | |
53 | * incremented before "end", as guaranteed by memory barriers | |
54 | * (A) or (B). | |
55 | */ | |
5a76c31e MD |
56 | if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0)) |
57 | abort(); | |
48363c84 MD |
58 | |
59 | for (i = 0; i < gp_state->nr_cpus; i++) { | |
60 | struct side_rcu_cpu_gp_state *cpu_state = &gp_state->percpu_state[i]; | |
61 | ||
7fb53c62 | 62 | if (active_readers[0]) { |
48363c84 | 63 | sum[0] += __atomic_load_n(&cpu_state->count[0].begin, __ATOMIC_RELAXED); |
7fb53c62 MD |
64 | sum[0] += __atomic_load_n(&cpu_state->count[0].rseq_begin, __ATOMIC_RELAXED); |
65 | } | |
66 | if (active_readers[1]) { | |
48363c84 | 67 | sum[1] += __atomic_load_n(&cpu_state->count[1].begin, __ATOMIC_RELAXED); |
7fb53c62 MD |
68 | sum[1] += __atomic_load_n(&cpu_state->count[1].rseq_begin, __ATOMIC_RELAXED); |
69 | } | |
48363c84 MD |
70 | } |
71 | if (active_readers[0]) | |
72 | active_readers[0] = sum[0]; | |
73 | if (active_readers[1]) | |
74 | active_readers[1] = sum[1]; | |
75 | } | |
76 | ||
77 | /* | |
78 | * Wait for previous period to have no active readers. | |
79 | * | |
80 | * active_readers is an input/output parameter. | |
81 | */ | |
82 | static | |
83 | void wait_for_prev_period_readers(struct side_rcu_gp_state *gp_state, bool *active_readers) | |
84 | { | |
85 | unsigned int prev_period = gp_state->period ^ 1; | |
86 | ||
87 | /* | |
88 | * If a prior active readers scan already observed that no | |
89 | * readers are present for the previous period, there is no need | |
90 | * to scan again. | |
91 | */ | |
92 | if (!active_readers[prev_period]) | |
93 | return; | |
94 | /* | |
95 | * Wait for the sum of CPU begin/end counts to match for the | |
96 | * previous period. | |
97 | */ | |
98 | for (;;) { | |
99 | check_active_readers(gp_state, active_readers); | |
100 | if (!active_readers[prev_period]) | |
101 | break; | |
102 | /* Retry after 10ms. */ | |
103 | poll(NULL, 0, 10); | |
104 | } | |
105 | } | |
106 | ||
107 | /* | |
108 | * The grace period completes when it observes that there are no active | |
109 | * readers within each of the periods. | |
110 | * | |
111 | * The active_readers state is initially true for each period, until the | |
112 | * grace period observes that no readers are present for each given | |
113 | * period, at which point the active_readers state becomes false. | |
114 | */ | |
115 | void side_rcu_wait_grace_period(struct side_rcu_gp_state *gp_state) | |
116 | { | |
117 | bool active_readers[2] = { true, true }; | |
118 | ||
119 | /* | |
120 | * This memory barrier (D) pairs with memory barriers (A) and | |
121 | * (B) on the read-side. | |
122 | * | |
123 | * It orders prior loads and stores before the "end"/"begin" | |
124 | * reader state loads. In other words, it orders prior loads and | |
125 | * stores before observation of active readers quiescence, | |
126 | * effectively ensuring that read-side critical sections which | |
127 | * exist after the grace period completes are ordered after | |
128 | * loads and stores performed before the grace period. | |
129 | */ | |
5a76c31e MD |
130 | if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0)) |
131 | abort(); | |
48363c84 MD |
132 | |
133 | /* | |
134 | * First scan through all cpus, for both period. If no readers | |
135 | * are accounted for, we have observed quiescence and can | |
136 | * complete the grace period immediately. | |
137 | */ | |
138 | check_active_readers(gp_state, active_readers); | |
139 | if (!active_readers[0] && !active_readers[1]) | |
140 | goto end; | |
141 | ||
142 | pthread_mutex_lock(&gp_state->gp_lock); | |
143 | ||
144 | wait_for_prev_period_readers(gp_state, active_readers); | |
145 | /* | |
146 | * If the reader scan detected that there are no readers in the | |
147 | * current period as well, we can complete the grace period | |
148 | * immediately. | |
149 | */ | |
150 | if (!active_readers[gp_state->period]) | |
151 | goto unlock; | |
152 | ||
153 | /* Flip period: 0 -> 1, 1 -> 0. */ | |
154 | (void) __atomic_xor_fetch(&gp_state->period, 1, __ATOMIC_RELAXED); | |
155 | ||
156 | wait_for_prev_period_readers(gp_state, active_readers); | |
157 | unlock: | |
158 | pthread_mutex_unlock(&gp_state->gp_lock); | |
159 | end: | |
160 | /* | |
161 | * This memory barrier (E) pairs with memory barriers (A) and | |
162 | * (B) on the read-side. | |
163 | * | |
164 | * It orders the "end"/"begin" reader state loads before | |
165 | * following loads and stores. In other words, it orders | |
166 | * observation of active readers quiescence before following | |
167 | * loads and stores, effectively ensuring that read-side | |
168 | * critical sections which existed prior to the grace period | |
169 | * are ordered before loads and stores performed after the grace | |
170 | * period. | |
171 | */ | |
5a76c31e MD |
172 | if (membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0, 0)) |
173 | abort(); | |
48363c84 | 174 | } |
054b7b5c MD |
175 | |
176 | void side_rcu_gp_init(struct side_rcu_gp_state *rcu_gp) | |
177 | { | |
178 | memset(rcu_gp, 0, sizeof(*rcu_gp)); | |
179 | rcu_gp->nr_cpus = get_possible_cpus_array_len(); | |
180 | if (!rcu_gp->nr_cpus) | |
181 | abort(); | |
182 | pthread_mutex_init(&rcu_gp->gp_lock, NULL); | |
183 | rcu_gp->percpu_state = calloc(rcu_gp->nr_cpus, sizeof(struct side_rcu_cpu_gp_state)); | |
184 | if (!rcu_gp->percpu_state) | |
185 | abort(); | |
5a76c31e MD |
186 | if (membarrier(MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, 0, 0)) |
187 | abort(); | |
054b7b5c | 188 | } |
6e46f5e6 MD |
189 | |
190 | void side_rcu_gp_exit(struct side_rcu_gp_state *rcu_gp) | |
191 | { | |
7fb53c62 | 192 | rseq_prepare_unload(); |
6e46f5e6 MD |
193 | pthread_mutex_destroy(&rcu_gp->gp_lock); |
194 | free(rcu_gp->percpu_state); | |
195 | } |