1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2018 Arm Limited
12 #include <rte_common.h>
14 #include <rte_memory.h>
15 #include <rte_malloc.h>
17 #include <rte_atomic.h>
18 #include <rte_per_lcore.h>
19 #include <rte_lcore.h>
20 #include <rte_errno.h>
22 #include "rte_rcu_qsbr.h"
24 /* Get the memory size of QSBR variable */
26 rte_rcu_qsbr_get_memsize(uint32_t max_threads)
30 if (max_threads == 0) {
31 rte_log(RTE_LOG_ERR, rte_rcu_log_type,
32 "%s(): Invalid max_threads %u\n",
33 __func__, max_threads);
39 sz = sizeof(struct rte_rcu_qsbr);
41 /* Add the size of quiescent state counter array */
42 sz += sizeof(struct rte_rcu_qsbr_cnt) * max_threads;
44 /* Add the size of the registered thread ID bitmap array */
45 sz += __RTE_QSBR_THRID_ARRAY_SIZE(max_threads);
50 /* Initialize a quiescent state variable */
52 rte_rcu_qsbr_init(struct rte_rcu_qsbr *v, uint32_t max_threads)
57 rte_log(RTE_LOG_ERR, rte_rcu_log_type,
58 "%s(): Invalid input parameter\n", __func__);
64 sz = rte_rcu_qsbr_get_memsize(max_threads);
68 /* Set all the threads to offline */
70 v->max_threads = max_threads;
71 v->num_elems = RTE_ALIGN_MUL_CEIL(max_threads,
72 __RTE_QSBR_THRID_ARRAY_ELM_SIZE) /
73 __RTE_QSBR_THRID_ARRAY_ELM_SIZE;
74 v->token = __RTE_QSBR_CNT_INIT;
79 /* Register a reader thread to report its quiescent state
83 rte_rcu_qsbr_thread_register(struct rte_rcu_qsbr *v, unsigned int thread_id)
85 unsigned int i, id, success;
86 uint64_t old_bmap, new_bmap;
88 if (v == NULL || thread_id >= v->max_threads) {
89 rte_log(RTE_LOG_ERR, rte_rcu_log_type,
90 "%s(): Invalid input parameter\n", __func__);
96 __RTE_RCU_IS_LOCK_CNT_ZERO(v, thread_id, ERR, "Lock counter %u\n",
97 v->qsbr_cnt[thread_id].lock_cnt);
99 id = thread_id & __RTE_QSBR_THRID_MASK;
100 i = thread_id >> __RTE_QSBR_THRID_INDEX_SHIFT;
102 /* Make sure that the counter for registered threads does not
103 * go out of sync. Hence, additional checks are required.
105 /* Check if the thread is already registered */
106 old_bmap = __atomic_load_n(__RTE_QSBR_THRID_ARRAY_ELM(v, i),
108 if (old_bmap & 1UL << id)
112 new_bmap = old_bmap | (1UL << id);
113 success = __atomic_compare_exchange(
114 __RTE_QSBR_THRID_ARRAY_ELM(v, i),
115 &old_bmap, &new_bmap, 0,
116 __ATOMIC_RELEASE, __ATOMIC_RELAXED);
119 __atomic_fetch_add(&v->num_threads,
120 1, __ATOMIC_RELAXED);
121 else if (old_bmap & (1UL << id))
122 /* Someone else registered this thread.
123 * Counter should not be incremented.
126 } while (success == 0);
131 /* Remove a reader thread, from the list of threads reporting their
132 * quiescent state on a QS variable.
135 rte_rcu_qsbr_thread_unregister(struct rte_rcu_qsbr *v, unsigned int thread_id)
137 unsigned int i, id, success;
138 uint64_t old_bmap, new_bmap;
140 if (v == NULL || thread_id >= v->max_threads) {
141 rte_log(RTE_LOG_ERR, rte_rcu_log_type,
142 "%s(): Invalid input parameter\n", __func__);
148 __RTE_RCU_IS_LOCK_CNT_ZERO(v, thread_id, ERR, "Lock counter %u\n",
149 v->qsbr_cnt[thread_id].lock_cnt);
151 id = thread_id & __RTE_QSBR_THRID_MASK;
152 i = thread_id >> __RTE_QSBR_THRID_INDEX_SHIFT;
154 /* Make sure that the counter for registered threads does not
155 * go out of sync. Hence, additional checks are required.
157 /* Check if the thread is already unregistered */
158 old_bmap = __atomic_load_n(__RTE_QSBR_THRID_ARRAY_ELM(v, i),
160 if (old_bmap & ~(1UL << id))
164 new_bmap = old_bmap & ~(1UL << id);
165 /* Make sure any loads of the shared data structure are
166 * completed before removal of the thread from the list of
169 success = __atomic_compare_exchange(
170 __RTE_QSBR_THRID_ARRAY_ELM(v, i),
171 &old_bmap, &new_bmap, 0,
172 __ATOMIC_RELEASE, __ATOMIC_RELAXED);
175 __atomic_fetch_sub(&v->num_threads,
176 1, __ATOMIC_RELAXED);
177 else if (old_bmap & ~(1UL << id))
178 /* Someone else unregistered this thread.
179 * Counter should not be incremented.
182 } while (success == 0);
187 /* Wait till the reader threads have entered quiescent state. */
189 rte_rcu_qsbr_synchronize(struct rte_rcu_qsbr *v, unsigned int thread_id)
193 RTE_ASSERT(v != NULL);
195 t = rte_rcu_qsbr_start(v);
197 /* If the current thread has readside critical section,
198 * update its quiescent state status.
200 if (thread_id != RTE_QSBR_THRID_INVALID)
201 rte_rcu_qsbr_quiescent(v, thread_id);
203 /* Wait for other readers to enter quiescent state */
204 rte_rcu_qsbr_check(v, t, true);
207 /* Dump the details of a single quiescent state variable to a file. */
209 rte_rcu_qsbr_dump(FILE *f, struct rte_rcu_qsbr *v)
214 if (v == NULL || f == NULL) {
215 rte_log(RTE_LOG_ERR, rte_rcu_log_type,
216 "%s(): Invalid input parameter\n", __func__);
222 fprintf(f, "\nQuiescent State Variable @%p\n", v);
224 fprintf(f, " QS variable memory size = %zu\n",
225 rte_rcu_qsbr_get_memsize(v->max_threads));
226 fprintf(f, " Given # max threads = %u\n", v->max_threads);
227 fprintf(f, " Current # threads = %u\n", v->num_threads);
229 fprintf(f, " Registered thread IDs = ");
230 for (i = 0; i < v->num_elems; i++) {
231 bmap = __atomic_load_n(__RTE_QSBR_THRID_ARRAY_ELM(v, i),
233 id = i << __RTE_QSBR_THRID_INDEX_SHIFT;
235 t = __builtin_ctzl(bmap);
236 fprintf(f, "%u ", id + t);
244 fprintf(f, " Token = %"PRIu64"\n",
245 __atomic_load_n(&v->token, __ATOMIC_ACQUIRE));
247 fprintf(f, "Quiescent State Counts for readers:\n");
248 for (i = 0; i < v->num_elems; i++) {
249 bmap = __atomic_load_n(__RTE_QSBR_THRID_ARRAY_ELM(v, i),
251 id = i << __RTE_QSBR_THRID_INDEX_SHIFT;
253 t = __builtin_ctzl(bmap);
254 fprintf(f, "thread ID = %u, count = %"PRIu64", lock count = %u\n",
257 &v->qsbr_cnt[id + t].cnt,
260 &v->qsbr_cnt[id + t].lock_cnt,
269 int rte_rcu_log_type;
271 RTE_INIT(rte_rcu_register)
273 rte_rcu_log_type = rte_log_register("lib.rcu");
274 if (rte_rcu_log_type >= 0)
275 rte_log_set_level(rte_rcu_log_type, RTE_LOG_ERR);