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