1 // SPDX-License-Identifier: MIT
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 #define SIDE_CACHE_LINE_SIZE 256
14 struct side_rcu_percpu_count
{
17 } __attribute__((__aligned__(SIDE_CACHE_LINE_SIZE
)));
19 struct side_rcu_cpu_gp_state
{
20 struct side_rcu_percpu_count count
[2];
23 struct side_rcu_gp_state
{
24 struct side_rcu_cpu_gp_state
*percpu_state
;
27 pthread_mutex_t gp_lock
;
30 //TODO: replace atomics by rseq (when available)
31 //TODO: replace acquire/release by membarrier+compiler barrier (when available)
32 //TODO: implement wait/wakeup for grace period using sys_futex
34 unsigned int side_rcu_read_begin(struct side_rcu_gp_state
*gp_state
)
36 int cpu
= sched_getcpu();
37 unsigned int period
= __atomic_load_n(&gp_state
->period
, __ATOMIC_RELAXED
);
42 * This memory barrier (A) ensures that the contents of the
43 * read-side critical section does not leak before the "begin"
44 * counter increment. It pairs with memory barriers (D) and (E).
46 * This memory barrier (A) also ensures that the "begin"
47 * increment is before the "end" increment. It pairs with memory
48 * barrier (C). It is redundant with memory barrier (B) for that
51 (void) __atomic_add_fetch(&gp_state
->percpu_state
[cpu
].count
[period
].begin
, 1, __ATOMIC_SEQ_CST
);
56 void side_rcu_read_end(struct side_rcu_gp_state
*gp_state
, unsigned int period
)
58 int cpu
= sched_getcpu();
63 * This memory barrier (B) ensures that the contents of the
64 * read-side critical section does not leak after the "end"
65 * counter increment. It pairs with memory barriers (D) and (E).
67 * This memory barrier (B) also ensures that the "begin"
68 * increment is before the "end" increment. It pairs with memory
69 * barrier (C). It is redundant with memory barrier (A) for that
72 (void) __atomic_add_fetch(&gp_state
->percpu_state
[cpu
].count
[period
].end
, 1, __ATOMIC_SEQ_CST
);
75 #define side_rcu_dereference(p) \
78 (__typeof__(p) _____side_v = __atomic_load_n(&(p), __ATOMIC_CONSUME); \
82 #define side_rcu_assign_pointer(p, v) __atomic_store_n(&(p), v, __ATOMIC_RELEASE); \
84 /* active_readers is an input/output parameter. */
86 void check_active_readers(struct side_rcu_gp_state
*gp_state
, bool *active_readers
)
88 uintptr_t sum
[2] = { 0, 0 }; /* begin - end */
91 for (i
= 0; i
< gp_state
->nr_cpus
; i
++) {
92 struct side_rcu_cpu_gp_state
*cpu_state
= &gp_state
->percpu_state
[i
];
94 sum
[0] -= __atomic_load_n(&cpu_state
->count
[0].end
, __ATOMIC_RELAXED
);
95 sum
[1] -= __atomic_load_n(&cpu_state
->count
[1].end
, __ATOMIC_RELAXED
);
99 * This memory barrier (C) pairs with either of memory barriers
100 * (A) or (B) (one is sufficient).
102 * Read end counts before begin counts. Reading "end" before
103 * "begin" counts ensures we never see an "end" without having
104 * seen its associated "begin", because "begin" is always
105 * incremented before "end", as guaranteed by memory barriers
108 __atomic_thread_fence(__ATOMIC_SEQ_CST
);
110 for (i
= 0; i
< gp_state
->nr_cpus
; i
++) {
111 struct side_rcu_cpu_gp_state
*cpu_state
= &gp_state
->percpu_state
[i
];
113 sum
[0] += __atomic_load_n(&cpu_state
->count
[0].begin
, __ATOMIC_RELAXED
);
114 sum
[1] += __atomic_load_n(&cpu_state
->count
[1].begin
, __ATOMIC_RELAXED
);
116 if (active_readers
[0])
117 active_readers
[0] = sum
[0];
118 if (active_readers
[1])
119 active_readers
[1] = sum
[1];
123 * Wait for previous period to have no active readers.
125 * active_readers is an input/output parameter.
128 void wait_for_prev_period_readers(struct side_rcu_gp_state
*gp_state
, bool *active_readers
)
130 unsigned int prev_period
= gp_state
->period
^ 1;
133 * If a prior active readers scan already observed that no
134 * readers are present for the previous period, there is no need
137 if (!active_readers
[prev_period
])
140 * Wait for the sum of CPU begin/end counts to match for the
144 check_active_readers(gp_state
, active_readers
);
145 if (!active_readers
[prev_period
])
147 /* Retry after 10ms. */
153 * The grace period completes when it observes that there are no active
154 * readers within each of the periods.
156 * The active_readers state is initially true for each period, until the
157 * grace period observes that no readers are present for each given
158 * period, at which point the active_readers state becomes false.
161 void side_rcu_wait_grace_period(struct side_rcu_gp_state
*gp_state
)
163 bool active_readers
[2] = { true, true };
166 * This memory barrier (D) pairs with memory barriers (A) and
167 * (B) on the read-side.
169 __atomic_thread_fence(__ATOMIC_SEQ_CST
);
172 * First scan through all cpus, for both period. If no readers
173 * are accounted for, we have observed quiescence and can
174 * complete the grace period immediately.
176 check_active_readers(gp_state
, active_readers
);
177 if (!active_readers
[0] && !active_readers
[1])
180 pthread_mutex_lock(&gp_state
->gp_lock
);
182 wait_for_prev_period_readers(gp_state
, active_readers
);
184 * If the reader scan detected that there are no readers in the
185 * current period as well, we can complete the grace period
188 if (!active_readers
[gp_state
->period
])
191 /* Flip period: 0 -> 1, 1 -> 0. */
192 (void) __atomic_xor_fetch(&gp_state
->period
, 1, __ATOMIC_RELAXED
);
194 wait_for_prev_period_readers(gp_state
, active_readers
);
196 pthread_mutex_unlock(&gp_state
->gp_lock
);
199 * This memory barrier (E) pairs with memory barriers (A) and
200 * (B) on the read-side.
202 __atomic_thread_fence(__ATOMIC_SEQ_CST
);