[79d74fe] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Adam Hraska
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup sync
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * @file
|
---|
[181a746] | 36 | * @brief Preemptible read-copy update. Usable from interrupt handlers.
|
---|
[79d74fe] | 37 | */
|
---|
| 38 |
|
---|
| 39 | #include <synch/rcu.h>
|
---|
[181a746] | 40 | #include <synch/condvar.h>
|
---|
| 41 | #include <synch/semaphore.h>
|
---|
| 42 | #include <synch/spinlock.h>
|
---|
[4ec9ea41] | 43 | #include <synch/mutex.h>
|
---|
[181a746] | 44 | #include <proc/thread.h>
|
---|
| 45 | #include <cpu/cpu_mask.h>
|
---|
| 46 | #include <cpu.h>
|
---|
| 47 | #include <smp/smp_call.h>
|
---|
| 48 | #include <compiler/barrier.h>
|
---|
| 49 | #include <atomic.h>
|
---|
| 50 | #include <arch.h>
|
---|
| 51 | #include <macros.h>
|
---|
[79d74fe] | 52 |
|
---|
[181a746] | 53 | /*
|
---|
| 54 | * Number of milliseconds to give to preexisting readers to finish
|
---|
| 55 | * when non-expedited grace period detection is in progress.
|
---|
| 56 | */
|
---|
[0cf813d] | 57 | #define DETECT_SLEEP_MS 10
|
---|
[181a746] | 58 | /*
|
---|
| 59 | * Max number of pending callbacks in the local cpu's queue before
|
---|
| 60 | * aggressively expediting the current grace period
|
---|
| 61 | */
|
---|
[0cf813d] | 62 | #define EXPEDITE_THRESHOLD 2000
|
---|
| 63 | /*
|
---|
| 64 | * Max number of callbacks to execute in one go with preemption
|
---|
| 65 | * enabled. If there are more callbacks to be executed they will
|
---|
| 66 | * be run with preemption disabled in order to prolong reclaimer's
|
---|
| 67 | * time slice and give it a chance to catch up with callback producers.
|
---|
| 68 | */
|
---|
| 69 | #define CRITICAL_THRESHOLD 30000
|
---|
[181a746] | 70 | /* Half the number of values a uint32 can hold. */
|
---|
| 71 | #define UINT32_MAX_HALF 2147483648U
|
---|
[79d74fe] | 72 |
|
---|
[181a746] | 73 |
|
---|
[0cf813d] | 74 | /** Global RCU data. */
|
---|
[181a746] | 75 | typedef struct rcu_data {
|
---|
| 76 | /** Detector uses so signal reclaimers that a grace period ended. */
|
---|
| 77 | condvar_t gp_ended;
|
---|
| 78 | /** Reclaimers notify the detector when they request more grace periods.*/
|
---|
| 79 | condvar_t req_gp_changed;
|
---|
| 80 | /** Reclaimers use to notify the detector to accelerate GP detection. */
|
---|
| 81 | condvar_t expedite_now;
|
---|
| 82 | /**
|
---|
| 83 | * The detector waits on this semaphore for any readers delaying the GP.
|
---|
| 84 | *
|
---|
| 85 | * Each of the cpus with readers that are delaying the current GP
|
---|
| 86 | * must up() this sema once they reach a quiescent state. If there
|
---|
| 87 | * are any readers in cur_preempted (ie preempted preexisting) and
|
---|
| 88 | * they are already delaying GP detection, the last to unlock its
|
---|
| 89 | * reader section must up() this sema once.
|
---|
| 90 | */
|
---|
| 91 | semaphore_t remaining_readers;
|
---|
| 92 |
|
---|
| 93 | /** Protects the 4 fields below. */
|
---|
| 94 | SPINLOCK_DECLARE(gp_lock);
|
---|
| 95 | /** Number of grace period ends the detector was requested to announce. */
|
---|
| 96 | size_t req_gp_end_cnt;
|
---|
| 97 | /** Number of consecutive grace periods to detect quickly and aggressively.*/
|
---|
| 98 | size_t req_expedited_cnt;
|
---|
| 99 | /**
|
---|
| 100 | * The current grace period number. Increases monotonically.
|
---|
| 101 | * Lock gp_lock or preempt_lock to get a current value.
|
---|
| 102 | */
|
---|
| 103 | rcu_gp_t cur_gp;
|
---|
| 104 | /**
|
---|
| 105 | * The number of the most recently completed grace period.
|
---|
| 106 | * At most one behind cur_gp. If equal to cur_gp, a grace
|
---|
| 107 | * period detection is not in progress and the detector
|
---|
| 108 | * is idle.
|
---|
| 109 | */
|
---|
| 110 | rcu_gp_t completed_gp;
|
---|
| 111 |
|
---|
[0cf813d] | 112 | /** Protects the following 3 fields. */
|
---|
[181a746] | 113 | IRQ_SPINLOCK_DECLARE(preempt_lock);
|
---|
| 114 | /** Preexisting readers that have been preempted. */
|
---|
| 115 | list_t cur_preempted;
|
---|
| 116 | /** Reader that have been preempted and might delay the next grace period.*/
|
---|
| 117 | list_t next_preempted;
|
---|
| 118 | /**
|
---|
| 119 | * The detector is waiting for the last preempted reader
|
---|
| 120 | * in cur_preempted to announce that it exited its reader
|
---|
| 121 | * section by up()ing remaining_readers.
|
---|
| 122 | */
|
---|
| 123 | bool preempt_blocking_det;
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * Number of cpus with readers that are delaying the current GP.
|
---|
| 127 | * They will up() remaining_readers.
|
---|
| 128 | */
|
---|
| 129 | atomic_t delaying_cpu_cnt;
|
---|
| 130 |
|
---|
[4ec9ea41] | 131 | /** Excludes simultaneous rcu_barrier() calls. */
|
---|
| 132 | mutex_t barrier_mtx;
|
---|
| 133 | /** Number of cpus that we are waiting for to complete rcu_barrier(). */
|
---|
| 134 | atomic_t barrier_wait_cnt;
|
---|
| 135 | /** rcu_barrier() waits for the completion of barrier callbacks on this wq.*/
|
---|
| 136 | waitq_t barrier_wq;
|
---|
| 137 |
|
---|
[0cf813d] | 138 | /** Interruptible attached detector thread pointer. */
|
---|
[181a746] | 139 | thread_t *detector_thr;
|
---|
| 140 |
|
---|
| 141 | /* Some statistics. */
|
---|
| 142 | size_t stat_expedited_cnt;
|
---|
| 143 | size_t stat_delayed_cnt;
|
---|
| 144 | size_t stat_preempt_blocking_cnt;
|
---|
| 145 | /* Does not contain self/local calls. */
|
---|
| 146 | size_t stat_smp_call_cnt;
|
---|
| 147 | } rcu_data_t;
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | static rcu_data_t rcu;
|
---|
| 151 |
|
---|
| 152 | static void start_detector(void);
|
---|
| 153 | static void start_reclaimers(void);
|
---|
| 154 | static void rcu_read_unlock_impl(size_t *pnesting_cnt);
|
---|
| 155 | static void synch_complete(rcu_item_t *rcu_item);
|
---|
[4ec9ea41] | 156 | static void add_barrier_cb(void *arg);
|
---|
| 157 | static void barrier_complete(rcu_item_t *barrier_item);
|
---|
[181a746] | 158 | static void check_qs(void);
|
---|
| 159 | static void record_qs(void);
|
---|
| 160 | static void signal_read_unlock(void);
|
---|
| 161 | static bool arriving_cbs_empty(void);
|
---|
| 162 | static bool next_cbs_empty(void);
|
---|
| 163 | static bool cur_cbs_empty(void);
|
---|
| 164 | static bool all_cbs_empty(void);
|
---|
| 165 | static void reclaimer(void *arg);
|
---|
| 166 | static bool wait_for_pending_cbs(void);
|
---|
| 167 | static bool advance_cbs(void);
|
---|
| 168 | static void exec_completed_cbs(rcu_gp_t last_completed_gp);
|
---|
| 169 | static void exec_cbs(rcu_item_t **phead);
|
---|
| 170 | static void req_detection(size_t req_cnt);
|
---|
| 171 | static bool wait_for_cur_cbs_gp_end(bool expedite, rcu_gp_t *last_completed_gp);
|
---|
[0cf813d] | 172 | static void upd_missed_gp_in_wait(rcu_gp_t completed_gp);
|
---|
[181a746] | 173 | static bool cv_wait_for_gp(rcu_gp_t wait_on_gp);
|
---|
| 174 | static void detector(void *);
|
---|
| 175 | static bool wait_for_detect_req(void);
|
---|
| 176 | static void start_new_gp(void);
|
---|
| 177 | static void end_cur_gp(void);
|
---|
| 178 | static bool wait_for_readers(void);
|
---|
| 179 | static void rm_quiescent_cpus(cpu_mask_t *cpu_mask);
|
---|
| 180 | static bool gp_sleep(void);
|
---|
| 181 | static void interrupt_delaying_cpus(cpu_mask_t *cpu_mask);
|
---|
| 182 | static void sample_local_cpu(void *);
|
---|
| 183 | static bool wait_for_delaying_cpus(void);
|
---|
| 184 | static bool wait_for_preempt_reader(void);
|
---|
[0cf813d] | 185 | static void upd_max_cbs_in_slice(void);
|
---|
[181a746] | 186 |
|
---|
| 187 |
|
---|
| 188 |
|
---|
| 189 | /** Initializes global RCU structures. */
|
---|
| 190 | void rcu_init(void)
|
---|
| 191 | {
|
---|
| 192 | condvar_initialize(&rcu.gp_ended);
|
---|
| 193 | condvar_initialize(&rcu.req_gp_changed);
|
---|
| 194 | condvar_initialize(&rcu.expedite_now);
|
---|
| 195 | semaphore_initialize(&rcu.remaining_readers, 0);
|
---|
| 196 |
|
---|
| 197 | spinlock_initialize(&rcu.gp_lock, "rcu.gp_lock");
|
---|
| 198 | rcu.req_gp_end_cnt = 0;
|
---|
| 199 | rcu.req_expedited_cnt = 0;
|
---|
| 200 | rcu.cur_gp = 0;
|
---|
| 201 | rcu.completed_gp = 0;
|
---|
| 202 |
|
---|
| 203 | irq_spinlock_initialize(&rcu.preempt_lock, "rcu.preempt_lock");
|
---|
| 204 | list_initialize(&rcu.cur_preempted);
|
---|
| 205 | list_initialize(&rcu.next_preempted);
|
---|
| 206 | rcu.preempt_blocking_det = false;
|
---|
| 207 |
|
---|
[4ec9ea41] | 208 | mutex_initialize(&rcu.barrier_mtx, MUTEX_PASSIVE);
|
---|
| 209 | atomic_set(&rcu.barrier_wait_cnt, 0);
|
---|
| 210 | waitq_initialize(&rcu.barrier_wq);
|
---|
| 211 |
|
---|
[181a746] | 212 | atomic_set(&rcu.delaying_cpu_cnt, 0);
|
---|
| 213 |
|
---|
| 214 | rcu.detector_thr = 0;
|
---|
| 215 |
|
---|
| 216 | rcu.stat_expedited_cnt = 0;
|
---|
| 217 | rcu.stat_delayed_cnt = 0;
|
---|
| 218 | rcu.stat_preempt_blocking_cnt = 0;
|
---|
| 219 | rcu.stat_smp_call_cnt = 0;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | /** Initializes per-CPU RCU data. If on the boot cpu inits global data too.*/
|
---|
| 223 | void rcu_cpu_init(void)
|
---|
| 224 | {
|
---|
| 225 | if (config.cpu_active == 1) {
|
---|
| 226 | rcu_init();
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | CPU->rcu.last_seen_gp = 0;
|
---|
| 230 |
|
---|
| 231 | CPU->rcu.pnesting_cnt = &CPU->rcu.tmp_nesting_cnt;
|
---|
| 232 | CPU->rcu.tmp_nesting_cnt = 0;
|
---|
| 233 |
|
---|
| 234 | CPU->rcu.cur_cbs = 0;
|
---|
[0cf813d] | 235 | CPU->rcu.cur_cbs_cnt = 0;
|
---|
[181a746] | 236 | CPU->rcu.next_cbs = 0;
|
---|
[0cf813d] | 237 | CPU->rcu.next_cbs_cnt = 0;
|
---|
[181a746] | 238 | CPU->rcu.arriving_cbs = 0;
|
---|
| 239 | CPU->rcu.parriving_cbs_tail = &CPU->rcu.arriving_cbs;
|
---|
| 240 | CPU->rcu.arriving_cbs_cnt = 0;
|
---|
| 241 |
|
---|
| 242 | CPU->rcu.cur_cbs_gp = 0;
|
---|
| 243 | CPU->rcu.next_cbs_gp = 0;
|
---|
| 244 |
|
---|
| 245 | CPU->rcu.is_delaying_gp = false;
|
---|
| 246 |
|
---|
| 247 | semaphore_initialize(&CPU->rcu.arrived_flag, 0);
|
---|
[0cf813d] | 248 |
|
---|
| 249 | /* BSP creates reclaimer threads before AP's rcu_cpu_init() runs. */
|
---|
| 250 | if (config.cpu_active == 1)
|
---|
| 251 | CPU->rcu.reclaimer_thr = 0;
|
---|
[181a746] | 252 |
|
---|
| 253 | CPU->rcu.stat_max_cbs = 0;
|
---|
| 254 | CPU->rcu.stat_avg_cbs = 0;
|
---|
| 255 | CPU->rcu.stat_missed_gps = 0;
|
---|
[0cf813d] | 256 | CPU->rcu.stat_missed_gp_in_wait = 0;
|
---|
| 257 | CPU->rcu.stat_max_slice_cbs = 0;
|
---|
| 258 | CPU->rcu.last_arriving_cnt = 0;
|
---|
[181a746] | 259 | }
|
---|
| 260 |
|
---|
| 261 | /** Completes RCU init. Creates and runs the detector and reclaimer threads.*/
|
---|
| 262 | void rcu_kinit_init(void)
|
---|
| 263 | {
|
---|
| 264 | start_detector();
|
---|
| 265 | start_reclaimers();
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | /** Initializes any per-thread RCU structures. */
|
---|
| 269 | void rcu_thread_init(thread_t *thread)
|
---|
| 270 | {
|
---|
| 271 | thread->rcu.nesting_cnt = 0;
|
---|
| 272 | thread->rcu.was_preempted = false;
|
---|
| 273 | link_initialize(&thread->rcu.preempt_link);
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | /** Called from scheduler() when exiting the current thread.
|
---|
| 277 | *
|
---|
| 278 | * Preemption or interrupts are disabled and the scheduler() already
|
---|
| 279 | * switched away from the current thread, calling rcu_after_thread_ran().
|
---|
[79d74fe] | 280 | */
|
---|
[181a746] | 281 | void rcu_thread_exiting(void)
|
---|
[79d74fe] | 282 | {
|
---|
[181a746] | 283 | ASSERT(THREAD != 0);
|
---|
| 284 | ASSERT(THREAD->state == Exiting);
|
---|
| 285 | ASSERT(PREEMPTION_DISABLED || interrupts_disabled());
|
---|
| 286 | /*
|
---|
| 287 | * The scheduler() must have already switched to a temporary
|
---|
| 288 | * nesting counter for interrupt handlers (we could be idle)
|
---|
| 289 | * so that interrupt handlers do not modify the exiting thread's
|
---|
| 290 | * reader section nesting count while we examine/process it.
|
---|
| 291 | */
|
---|
| 292 | ASSERT(&CPU->rcu.tmp_nesting_cnt == CPU->rcu.pnesting_cnt);
|
---|
| 293 |
|
---|
| 294 | /*
|
---|
| 295 | * The thread forgot to exit its reader critical secion.
|
---|
| 296 | * It is a bug, but rather than letting the entire system lock up
|
---|
| 297 | * forcefully leave the reader section. The thread is not holding
|
---|
| 298 | * any references anyway since it is exiting so it is safe.
|
---|
| 299 | */
|
---|
| 300 | if (0 < THREAD->rcu.nesting_cnt) {
|
---|
| 301 | THREAD->rcu.nesting_cnt = 1;
|
---|
| 302 | rcu_read_unlock_impl(&THREAD->rcu.nesting_cnt);
|
---|
| 303 | }
|
---|
[79d74fe] | 304 | }
|
---|
| 305 |
|
---|
[181a746] | 306 | /** Cleans up global RCU resources and stops dispatching callbacks.
|
---|
| 307 | *
|
---|
| 308 | * Call when shutting down the kernel. Outstanding callbacks will
|
---|
| 309 | * not be processed. Instead they will linger forever.
|
---|
| 310 | */
|
---|
| 311 | void rcu_stop(void)
|
---|
| 312 | {
|
---|
| 313 | /* Stop and wait for reclaimers. */
|
---|
| 314 | for (unsigned int cpu_id = 0; cpu_id < config.cpu_active; ++cpu_id) {
|
---|
| 315 | ASSERT(cpus[cpu_id].rcu.reclaimer_thr != 0);
|
---|
| 316 |
|
---|
| 317 | if (cpus[cpu_id].rcu.reclaimer_thr) {
|
---|
| 318 | thread_interrupt(cpus[cpu_id].rcu.reclaimer_thr);
|
---|
| 319 | thread_join(cpus[cpu_id].rcu.reclaimer_thr);
|
---|
| 320 | thread_detach(cpus[cpu_id].rcu.reclaimer_thr);
|
---|
| 321 | cpus[cpu_id].rcu.reclaimer_thr = 0;
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | /* Stop the detector and wait. */
|
---|
| 326 | if (rcu.detector_thr) {
|
---|
| 327 | thread_interrupt(rcu.detector_thr);
|
---|
| 328 | thread_join(rcu.detector_thr);
|
---|
| 329 | thread_detach(rcu.detector_thr);
|
---|
| 330 | rcu.detector_thr = 0;
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
[79d74fe] | 333 |
|
---|
[181a746] | 334 | /** Starts the detector thread. */
|
---|
| 335 | static void start_detector(void)
|
---|
| 336 | {
|
---|
| 337 | rcu.detector_thr =
|
---|
| 338 | thread_create(detector, 0, TASK, THREAD_FLAG_NONE, "rcu-det");
|
---|
| 339 |
|
---|
| 340 | if (!rcu.detector_thr)
|
---|
| 341 | panic("Failed to create RCU detector thread.");
|
---|
| 342 |
|
---|
| 343 | thread_ready(rcu.detector_thr);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | /** Creates and runs cpu-bound reclaimer threads. */
|
---|
| 347 | static void start_reclaimers(void)
|
---|
| 348 | {
|
---|
| 349 | for (unsigned int cpu_id = 0; cpu_id < config.cpu_count; ++cpu_id) {
|
---|
| 350 | char name[THREAD_NAME_BUFLEN] = {0};
|
---|
| 351 |
|
---|
| 352 | snprintf(name, THREAD_NAME_BUFLEN - 1, "rcu-rec/%u", cpu_id);
|
---|
| 353 |
|
---|
| 354 | cpus[cpu_id].rcu.reclaimer_thr =
|
---|
| 355 | thread_create(reclaimer, 0, TASK, THREAD_FLAG_NONE, name);
|
---|
| 356 |
|
---|
| 357 | if (!cpus[cpu_id].rcu.reclaimer_thr)
|
---|
| 358 | panic("Failed to create RCU reclaimer thread on cpu%u.", cpu_id);
|
---|
| 359 |
|
---|
| 360 | thread_wire(cpus[cpu_id].rcu.reclaimer_thr, &cpus[cpu_id]);
|
---|
| 361 | thread_ready(cpus[cpu_id].rcu.reclaimer_thr);
|
---|
| 362 | }
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | /** Returns the number of elapsed grace periods since boot. */
|
---|
| 366 | uint64_t rcu_completed_gps(void)
|
---|
| 367 | {
|
---|
| 368 | spinlock_lock(&rcu.gp_lock);
|
---|
| 369 | uint64_t completed = rcu.completed_gp;
|
---|
| 370 | spinlock_unlock(&rcu.gp_lock);
|
---|
| 371 |
|
---|
| 372 | return completed;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | /** Delimits the start of an RCU reader critical section.
|
---|
| 376 | *
|
---|
| 377 | * Reader sections may be nested and are preemptable. You must not
|
---|
| 378 | * however block/sleep within reader sections.
|
---|
[79d74fe] | 379 | */
|
---|
[181a746] | 380 | void rcu_read_lock(void)
|
---|
[79d74fe] | 381 | {
|
---|
[181a746] | 382 | ASSERT(CPU);
|
---|
| 383 | preemption_disable();
|
---|
| 384 |
|
---|
| 385 | check_qs();
|
---|
| 386 | ++(*CPU->rcu.pnesting_cnt);
|
---|
| 387 |
|
---|
| 388 | preemption_enable();
|
---|
[79d74fe] | 389 | }
|
---|
| 390 |
|
---|
[181a746] | 391 | /** Delimits the end of an RCU reader critical section. */
|
---|
| 392 | void rcu_read_unlock(void)
|
---|
| 393 | {
|
---|
| 394 | ASSERT(CPU);
|
---|
| 395 | preemption_disable();
|
---|
| 396 |
|
---|
| 397 | rcu_read_unlock_impl(CPU->rcu.pnesting_cnt);
|
---|
| 398 |
|
---|
| 399 | preemption_enable();
|
---|
| 400 | }
|
---|
[79d74fe] | 401 |
|
---|
[4a6da62] | 402 | /** Returns true if in an rcu reader section. */
|
---|
| 403 | bool rcu_read_locked(void)
|
---|
| 404 | {
|
---|
| 405 | preemption_disable();
|
---|
| 406 | bool locked = 0 < *CPU->rcu.pnesting_cnt;
|
---|
| 407 | preemption_enable();
|
---|
| 408 |
|
---|
| 409 | return locked;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[181a746] | 412 | /** Unlocks the local reader section using the given nesting count.
|
---|
| 413 | *
|
---|
| 414 | * Preemption or interrupts must be disabled.
|
---|
| 415 | *
|
---|
| 416 | * @param pnesting_cnt Either &CPU->rcu.tmp_nesting_cnt or
|
---|
| 417 | * THREAD->rcu.nesting_cnt.
|
---|
[79d74fe] | 418 | */
|
---|
[181a746] | 419 | static void rcu_read_unlock_impl(size_t *pnesting_cnt)
|
---|
| 420 | {
|
---|
| 421 | ASSERT(PREEMPTION_DISABLED || interrupts_disabled());
|
---|
| 422 |
|
---|
| 423 | if (0 == --(*pnesting_cnt)) {
|
---|
| 424 | record_qs();
|
---|
| 425 |
|
---|
| 426 | /*
|
---|
| 427 | * The thread was preempted while in a critical section or
|
---|
| 428 | * the detector is eagerly waiting for this cpu's reader
|
---|
| 429 | * to finish.
|
---|
| 430 | *
|
---|
| 431 | * Note that THREAD may be 0 in scheduler() and not just during boot.
|
---|
| 432 | */
|
---|
| 433 | if ((THREAD && THREAD->rcu.was_preempted) || CPU->rcu.is_delaying_gp) {
|
---|
| 434 | /* Rechecks with disabled interrupts. */
|
---|
| 435 | signal_read_unlock();
|
---|
| 436 | }
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | /** Records a QS if not in a reader critical section. */
|
---|
| 441 | static void check_qs(void)
|
---|
| 442 | {
|
---|
| 443 | ASSERT(PREEMPTION_DISABLED || interrupts_disabled());
|
---|
| 444 |
|
---|
| 445 | if (0 == *CPU->rcu.pnesting_cnt)
|
---|
| 446 | record_qs();
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | /** Unconditionally records a quiescent state for the local cpu. */
|
---|
| 450 | static void record_qs(void)
|
---|
| 451 | {
|
---|
| 452 | ASSERT(PREEMPTION_DISABLED || interrupts_disabled());
|
---|
| 453 |
|
---|
| 454 | /*
|
---|
| 455 | * A new GP was started since the last time we passed a QS.
|
---|
| 456 | * Notify the detector we have reached a new QS.
|
---|
| 457 | */
|
---|
| 458 | if (CPU->rcu.last_seen_gp != rcu.cur_gp) {
|
---|
| 459 | rcu_gp_t cur_gp = ACCESS_ONCE(rcu.cur_gp);
|
---|
| 460 | /*
|
---|
| 461 | * Contain memory accesses within a reader critical section.
|
---|
| 462 | * If we are in rcu_lock() it also makes changes prior to the
|
---|
| 463 | * start of the GP visible in the reader section.
|
---|
| 464 | */
|
---|
| 465 | memory_barrier();
|
---|
| 466 | /*
|
---|
| 467 | * Acknowledge we passed a QS since the beginning of rcu.cur_gp.
|
---|
| 468 | * Cache coherency will lazily transport the value to the
|
---|
| 469 | * detector while it sleeps in gp_sleep().
|
---|
| 470 | *
|
---|
| 471 | * Note that there is a theoretical possibility that we
|
---|
| 472 | * overwrite a more recent/greater last_seen_gp here with
|
---|
| 473 | * an older/smaller value. If this cpu is interrupted here
|
---|
| 474 | * while in rcu_lock() reader sections in the interrupt handler
|
---|
| 475 | * will update last_seen_gp to the same value as is currently
|
---|
| 476 | * in local cur_gp. However, if the cpu continues processing
|
---|
| 477 | * interrupts and the detector starts a new GP immediately,
|
---|
| 478 | * local interrupt handlers may update last_seen_gp again (ie
|
---|
| 479 | * properly ack the new GP) with a value greater than local cur_gp.
|
---|
| 480 | * Resetting last_seen_gp to a previous value here is however
|
---|
| 481 | * benign and we only have to remember that this reader may end up
|
---|
| 482 | * in cur_preempted even after the GP ends. That is why we
|
---|
| 483 | * append next_preempted to cur_preempted rather than overwriting
|
---|
| 484 | * it as if cur_preempted were empty.
|
---|
| 485 | */
|
---|
| 486 | CPU->rcu.last_seen_gp = cur_gp;
|
---|
| 487 | }
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | /** If necessary, signals the detector that we exited a reader section. */
|
---|
| 491 | static void signal_read_unlock(void)
|
---|
| 492 | {
|
---|
| 493 | ASSERT(PREEMPTION_DISABLED || interrupts_disabled());
|
---|
| 494 |
|
---|
| 495 | /*
|
---|
| 496 | * We have to disable interrupts in order to make checking
|
---|
| 497 | * and resetting was_preempted and is_delaying_gp atomic
|
---|
| 498 | * with respect to local interrupt handlers. Otherwise
|
---|
| 499 | * an interrupt could beat us to calling semaphore_up()
|
---|
| 500 | * before we reset the appropriate flag.
|
---|
| 501 | */
|
---|
| 502 | ipl_t ipl = interrupts_disable();
|
---|
| 503 |
|
---|
| 504 | /*
|
---|
| 505 | * If the detector is eagerly waiting for this cpu's reader to unlock,
|
---|
| 506 | * notify it that the reader did so.
|
---|
| 507 | */
|
---|
| 508 | if (CPU->rcu.is_delaying_gp) {
|
---|
| 509 | CPU->rcu.is_delaying_gp = false;
|
---|
| 510 | semaphore_up(&rcu.remaining_readers);
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | /*
|
---|
| 514 | * This reader was preempted while in a reader section.
|
---|
| 515 | * We might be holding up the current GP. Notify the
|
---|
| 516 | * detector if so.
|
---|
| 517 | */
|
---|
| 518 | if (THREAD && THREAD->rcu.was_preempted) {
|
---|
| 519 | ASSERT(link_used(&THREAD->rcu.preempt_link));
|
---|
| 520 | THREAD->rcu.was_preempted = false;
|
---|
| 521 |
|
---|
| 522 | irq_spinlock_lock(&rcu.preempt_lock, false);
|
---|
| 523 |
|
---|
| 524 | bool prev_empty = list_empty(&rcu.cur_preempted);
|
---|
| 525 | list_remove(&THREAD->rcu.preempt_link);
|
---|
| 526 | bool now_empty = list_empty(&rcu.cur_preempted);
|
---|
| 527 |
|
---|
| 528 | /* This was the last reader in cur_preempted. */
|
---|
| 529 | bool last_removed = now_empty && !prev_empty;
|
---|
| 530 |
|
---|
| 531 | /*
|
---|
| 532 | * Preempted readers are blocking the detector and
|
---|
| 533 | * this was the last reader blocking the current GP.
|
---|
| 534 | */
|
---|
| 535 | if (last_removed && rcu.preempt_blocking_det) {
|
---|
| 536 | rcu.preempt_blocking_det = false;
|
---|
| 537 | semaphore_up(&rcu.remaining_readers);
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | irq_spinlock_unlock(&rcu.preempt_lock, false);
|
---|
| 541 | }
|
---|
| 542 | interrupts_restore(ipl);
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | typedef struct synch_item {
|
---|
| 546 | waitq_t wq;
|
---|
| 547 | rcu_item_t rcu_item;
|
---|
| 548 | } synch_item_t;
|
---|
| 549 |
|
---|
| 550 | /** Blocks until all preexisting readers exit their critical sections. */
|
---|
[79d74fe] | 551 | void rcu_synchronize(void)
|
---|
[4ec9ea41] | 552 | {
|
---|
| 553 | _rcu_synchronize(false);
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | /** Blocks until all preexisting readers exit their critical sections. */
|
---|
| 557 | void rcu_synchronize_expedite(void)
|
---|
| 558 | {
|
---|
| 559 | _rcu_synchronize(true);
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | /** Blocks until all preexisting readers exit their critical sections. */
|
---|
| 563 | void _rcu_synchronize(bool expedite)
|
---|
[79d74fe] | 564 | {
|
---|
[181a746] | 565 | /* Calling from a reader section will deadlock. */
|
---|
| 566 | ASSERT(THREAD == 0 || 0 == THREAD->rcu.nesting_cnt);
|
---|
| 567 |
|
---|
| 568 | synch_item_t completion;
|
---|
| 569 |
|
---|
| 570 | waitq_initialize(&completion.wq);
|
---|
[4ec9ea41] | 571 | _rcu_call(expedite, &completion.rcu_item, synch_complete);
|
---|
[181a746] | 572 | waitq_sleep(&completion.wq);
|
---|
| 573 | waitq_complete_wakeup(&completion.wq);
|
---|
[79d74fe] | 574 | }
|
---|
| 575 |
|
---|
[181a746] | 576 | /** rcu_synchronize's callback. */
|
---|
| 577 | static void synch_complete(rcu_item_t *rcu_item)
|
---|
| 578 | {
|
---|
| 579 | synch_item_t *completion = member_to_inst(rcu_item, synch_item_t, rcu_item);
|
---|
| 580 | ASSERT(completion);
|
---|
| 581 | waitq_wakeup(&completion->wq, WAKEUP_FIRST);
|
---|
| 582 | }
|
---|
[79d74fe] | 583 |
|
---|
[4ec9ea41] | 584 | /** Waits for all outstanding rcu calls to complete. */
|
---|
| 585 | void rcu_barrier(void)
|
---|
| 586 | {
|
---|
| 587 | /*
|
---|
| 588 | * Serialize rcu_barrier() calls so we don't overwrite cpu.barrier_item
|
---|
| 589 | * currently in use by rcu_barrier().
|
---|
| 590 | */
|
---|
| 591 | mutex_lock(&rcu.barrier_mtx);
|
---|
| 592 |
|
---|
| 593 | /*
|
---|
| 594 | * Ensure we queue a barrier callback on all cpus before the already
|
---|
| 595 | * enqueued barrier callbacks start signaling completion.
|
---|
| 596 | */
|
---|
| 597 | atomic_set(&rcu.barrier_wait_cnt, 1);
|
---|
| 598 |
|
---|
| 599 | DEFINE_CPU_MASK(cpu_mask);
|
---|
| 600 | cpu_mask_active(cpu_mask);
|
---|
| 601 |
|
---|
| 602 | cpu_mask_for_each(*cpu_mask, cpu_id) {
|
---|
| 603 | smp_call(cpu_id, add_barrier_cb, 0);
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | if (0 < atomic_predec(&rcu.barrier_wait_cnt)) {
|
---|
| 607 | waitq_sleep(&rcu.barrier_wq);
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | mutex_unlock(&rcu.barrier_mtx);
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | /** Issues a rcu_barrier() callback on the local cpu.
|
---|
| 614 | *
|
---|
| 615 | * Executed with interrupts disabled.
|
---|
| 616 | */
|
---|
| 617 | static void add_barrier_cb(void *arg)
|
---|
| 618 | {
|
---|
| 619 | ASSERT(interrupts_disabled() || PREEMPTION_DISABLED);
|
---|
| 620 | atomic_inc(&rcu.barrier_wait_cnt);
|
---|
| 621 | rcu_call(&CPU->rcu.barrier_item, barrier_complete);
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | /** Local cpu's rcu_barrier() completion callback. */
|
---|
| 625 | static void barrier_complete(rcu_item_t *barrier_item)
|
---|
| 626 | {
|
---|
| 627 | /* Is this the last barrier callback completed? */
|
---|
| 628 | if (0 == atomic_predec(&rcu.barrier_wait_cnt)) {
|
---|
| 629 | /* Notify rcu_barrier() that we're done. */
|
---|
| 630 | waitq_wakeup(&rcu.barrier_wq, WAKEUP_FIRST);
|
---|
| 631 | }
|
---|
| 632 | }
|
---|
| 633 |
|
---|
[181a746] | 634 | /** Adds a callback to invoke after all preexisting readers finish.
|
---|
| 635 | *
|
---|
| 636 | * May be called from within interrupt handlers or RCU reader sections.
|
---|
| 637 | *
|
---|
| 638 | * @param rcu_item Used by RCU to track the call. Must remain
|
---|
| 639 | * until the user callback function is entered.
|
---|
| 640 | * @param func User callback function that will be invoked once a full
|
---|
| 641 | * grace period elapsed, ie at a time when all preexisting
|
---|
| 642 | * readers have finished. The callback should be short and must
|
---|
| 643 | * not block. If you must sleep, enqueue your work in the system
|
---|
| 644 | * work queue from the callback (ie workq_global_enqueue()).
|
---|
[79d74fe] | 645 | */
|
---|
[181a746] | 646 | void rcu_call(rcu_item_t *rcu_item, rcu_func_t func)
|
---|
[79d74fe] | 647 | {
|
---|
[181a746] | 648 | _rcu_call(false, rcu_item, func);
|
---|
[79d74fe] | 649 | }
|
---|
| 650 |
|
---|
[181a746] | 651 | /** rcu_call() implementation. See rcu_call() for comments. */
|
---|
| 652 | void _rcu_call(bool expedite, rcu_item_t *rcu_item, rcu_func_t func)
|
---|
| 653 | {
|
---|
| 654 | ASSERT(rcu_item);
|
---|
| 655 |
|
---|
| 656 | rcu_item->func = func;
|
---|
| 657 | rcu_item->next = 0;
|
---|
| 658 |
|
---|
| 659 | preemption_disable();
|
---|
| 660 |
|
---|
| 661 | ipl_t ipl = interrupts_disable();
|
---|
[79d74fe] | 662 |
|
---|
[181a746] | 663 | *CPU->rcu.parriving_cbs_tail = rcu_item;
|
---|
| 664 | CPU->rcu.parriving_cbs_tail = &rcu_item->next;
|
---|
| 665 |
|
---|
| 666 | size_t cnt = ++CPU->rcu.arriving_cbs_cnt;
|
---|
| 667 | interrupts_restore(ipl);
|
---|
| 668 |
|
---|
| 669 | if (expedite) {
|
---|
| 670 | CPU->rcu.expedite_arriving = true;
|
---|
| 671 | }
|
---|
| 672 |
|
---|
| 673 | /* Added first callback - notify the reclaimer. */
|
---|
| 674 | if (cnt == 1 && !semaphore_count_get(&CPU->rcu.arrived_flag)) {
|
---|
| 675 | semaphore_up(&CPU->rcu.arrived_flag);
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | preemption_enable();
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | static bool cur_cbs_empty(void)
|
---|
| 682 | {
|
---|
| 683 | ASSERT(THREAD && THREAD->wired);
|
---|
| 684 | return 0 == CPU->rcu.cur_cbs;
|
---|
| 685 | }
|
---|
| 686 |
|
---|
| 687 | static bool next_cbs_empty(void)
|
---|
| 688 | {
|
---|
| 689 | ASSERT(THREAD && THREAD->wired);
|
---|
| 690 | return 0 == CPU->rcu.next_cbs;
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | /** Disable interrupts to get an up-to-date result. */
|
---|
| 694 | static bool arriving_cbs_empty(void)
|
---|
| 695 | {
|
---|
| 696 | ASSERT(THREAD && THREAD->wired);
|
---|
| 697 | /*
|
---|
| 698 | * Accessing with interrupts enabled may at worst lead to
|
---|
| 699 | * a false negative if we race with a local interrupt handler.
|
---|
| 700 | */
|
---|
| 701 | return 0 == CPU->rcu.arriving_cbs;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | static bool all_cbs_empty(void)
|
---|
| 705 | {
|
---|
| 706 | return cur_cbs_empty() && next_cbs_empty() && arriving_cbs_empty();
|
---|
| 707 | }
|
---|
| 708 |
|
---|
| 709 | /** Reclaimer thread dispatches locally queued callbacks once a GP ends. */
|
---|
| 710 | static void reclaimer(void *arg)
|
---|
| 711 | {
|
---|
| 712 | ASSERT(THREAD && THREAD->wired);
|
---|
[0cf813d] | 713 | ASSERT(THREAD == CPU->rcu.reclaimer_thr);
|
---|
[181a746] | 714 |
|
---|
| 715 | rcu_gp_t last_compl_gp = 0;
|
---|
| 716 | bool ok = true;
|
---|
| 717 |
|
---|
| 718 | while (ok && wait_for_pending_cbs()) {
|
---|
[0cf813d] | 719 | ASSERT(CPU->rcu.reclaimer_thr == THREAD);
|
---|
[181a746] | 720 |
|
---|
[0cf813d] | 721 | exec_completed_cbs(last_compl_gp);
|
---|
| 722 |
|
---|
[181a746] | 723 | bool expedite = advance_cbs();
|
---|
| 724 |
|
---|
| 725 | ok = wait_for_cur_cbs_gp_end(expedite, &last_compl_gp);
|
---|
| 726 | }
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | /** Waits until there are callbacks waiting to be dispatched. */
|
---|
| 730 | static bool wait_for_pending_cbs(void)
|
---|
| 731 | {
|
---|
| 732 | if (!all_cbs_empty())
|
---|
| 733 | return true;
|
---|
| 734 |
|
---|
| 735 | bool ok = true;
|
---|
| 736 |
|
---|
| 737 | while (arriving_cbs_empty() && ok) {
|
---|
| 738 | ok = semaphore_down_interruptable(&CPU->rcu.arrived_flag);
|
---|
| 739 | }
|
---|
| 740 |
|
---|
| 741 | return ok;
|
---|
| 742 | }
|
---|
| 743 |
|
---|
[b68ae24] | 744 | static void upd_stat_missed_gp(rcu_gp_t compl)
|
---|
[181a746] | 745 | {
|
---|
[b68ae24] | 746 | if (CPU->rcu.cur_cbs_gp < compl) {
|
---|
| 747 | CPU->rcu.stat_missed_gps += (size_t)(compl - CPU->rcu.cur_cbs_gp);
|
---|
[181a746] | 748 | }
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | /** Executes all callbacks for the given completed grace period. */
|
---|
| 752 | static void exec_completed_cbs(rcu_gp_t last_completed_gp)
|
---|
| 753 | {
|
---|
[b68ae24] | 754 | upd_stat_missed_gp(last_completed_gp);
|
---|
| 755 |
|
---|
[0cf813d] | 756 | /* Both next_cbs and cur_cbs GP elapsed. */
|
---|
[181a746] | 757 | if (CPU->rcu.next_cbs_gp <= last_completed_gp) {
|
---|
[0cf813d] | 758 | ASSERT(CPU->rcu.cur_cbs_gp <= CPU->rcu.next_cbs_gp);
|
---|
| 759 |
|
---|
| 760 | size_t exec_cnt = CPU->rcu.cur_cbs_cnt + CPU->rcu.next_cbs_cnt;
|
---|
| 761 |
|
---|
| 762 | if (exec_cnt < CRITICAL_THRESHOLD) {
|
---|
| 763 | exec_cbs(&CPU->rcu.cur_cbs);
|
---|
| 764 | exec_cbs(&CPU->rcu.next_cbs);
|
---|
| 765 | } else {
|
---|
| 766 | /*
|
---|
| 767 | * Getting overwhelmed with too many callbacks to run.
|
---|
| 768 | * Disable preemption in order to prolong our time slice
|
---|
| 769 | * and catch up with updaters posting new callbacks.
|
---|
| 770 | */
|
---|
| 771 | preemption_disable();
|
---|
| 772 | exec_cbs(&CPU->rcu.cur_cbs);
|
---|
| 773 | exec_cbs(&CPU->rcu.next_cbs);
|
---|
| 774 | preemption_enable();
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 | CPU->rcu.cur_cbs_cnt = 0;
|
---|
| 778 | CPU->rcu.next_cbs_cnt = 0;
|
---|
| 779 | } else if (CPU->rcu.cur_cbs_gp <= last_completed_gp) {
|
---|
| 780 |
|
---|
| 781 | if (CPU->rcu.cur_cbs_cnt < CRITICAL_THRESHOLD) {
|
---|
| 782 | exec_cbs(&CPU->rcu.cur_cbs);
|
---|
| 783 | } else {
|
---|
| 784 | /*
|
---|
| 785 | * Getting overwhelmed with too many callbacks to run.
|
---|
| 786 | * Disable preemption in order to prolong our time slice
|
---|
| 787 | * and catch up with updaters posting new callbacks.
|
---|
| 788 | */
|
---|
| 789 | preemption_disable();
|
---|
| 790 | exec_cbs(&CPU->rcu.cur_cbs);
|
---|
| 791 | preemption_enable();
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | CPU->rcu.cur_cbs_cnt = 0;
|
---|
[181a746] | 795 | }
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | /** Executes callbacks in the single-linked list. The list is left empty. */
|
---|
| 799 | static void exec_cbs(rcu_item_t **phead)
|
---|
| 800 | {
|
---|
| 801 | rcu_item_t *rcu_item = *phead;
|
---|
| 802 |
|
---|
| 803 | while (rcu_item) {
|
---|
| 804 | /* func() may free rcu_item. Get a local copy. */
|
---|
| 805 | rcu_item_t *next = rcu_item->next;
|
---|
| 806 | rcu_func_t func = rcu_item->func;
|
---|
| 807 |
|
---|
| 808 | func(rcu_item);
|
---|
| 809 |
|
---|
| 810 | rcu_item = next;
|
---|
| 811 | }
|
---|
| 812 |
|
---|
| 813 | *phead = 0;
|
---|
| 814 | }
|
---|
| 815 |
|
---|
| 816 | static void upd_stat_cb_cnts(size_t arriving_cnt)
|
---|
| 817 | {
|
---|
| 818 | CPU->rcu.stat_max_cbs = max(arriving_cnt, CPU->rcu.stat_max_cbs);
|
---|
| 819 | if (0 < arriving_cnt) {
|
---|
| 820 | CPU->rcu.stat_avg_cbs =
|
---|
| 821 | (99 * CPU->rcu.stat_avg_cbs + 1 * arriving_cnt) / 100;
|
---|
| 822 | }
|
---|
| 823 | }
|
---|
| 824 |
|
---|
| 825 |
|
---|
| 826 | /** Prepares another batch of callbacks to dispatch at the nest grace period.
|
---|
| 827 | *
|
---|
| 828 | * @return True if the next batch of callbacks must be expedited quickly.
|
---|
[79d74fe] | 829 | */
|
---|
[181a746] | 830 | static bool advance_cbs(void)
|
---|
| 831 | {
|
---|
| 832 | /* Move next_cbs to cur_cbs. */
|
---|
| 833 | CPU->rcu.cur_cbs = CPU->rcu.next_cbs;
|
---|
[0cf813d] | 834 | CPU->rcu.cur_cbs_cnt = CPU->rcu.next_cbs_cnt;
|
---|
[181a746] | 835 | CPU->rcu.cur_cbs_gp = CPU->rcu.next_cbs_gp;
|
---|
| 836 |
|
---|
| 837 | /* Move arriving_cbs to next_cbs. Empties arriving_cbs. */
|
---|
| 838 | ipl_t ipl = interrupts_disable();
|
---|
| 839 |
|
---|
| 840 | /*
|
---|
| 841 | * Too many callbacks queued. Better speed up the detection
|
---|
| 842 | * or risk exhausting all system memory.
|
---|
| 843 | */
|
---|
| 844 | bool expedite = (EXPEDITE_THRESHOLD < CPU->rcu.arriving_cbs_cnt)
|
---|
| 845 | || CPU->rcu.expedite_arriving;
|
---|
| 846 |
|
---|
| 847 | CPU->rcu.expedite_arriving = false;
|
---|
[0cf813d] | 848 |
|
---|
[181a746] | 849 | CPU->rcu.next_cbs = CPU->rcu.arriving_cbs;
|
---|
[0cf813d] | 850 | CPU->rcu.next_cbs_cnt = CPU->rcu.arriving_cbs_cnt;
|
---|
| 851 |
|
---|
[181a746] | 852 | CPU->rcu.arriving_cbs = 0;
|
---|
| 853 | CPU->rcu.parriving_cbs_tail = &CPU->rcu.arriving_cbs;
|
---|
| 854 | CPU->rcu.arriving_cbs_cnt = 0;
|
---|
| 855 |
|
---|
| 856 | interrupts_restore(ipl);
|
---|
[0cf813d] | 857 |
|
---|
| 858 | /* Update statistics of arrived callbacks. */
|
---|
| 859 | upd_stat_cb_cnts(CPU->rcu.next_cbs_cnt);
|
---|
[181a746] | 860 |
|
---|
| 861 | /*
|
---|
| 862 | * Make changes prior to queuing next_cbs visible to readers.
|
---|
| 863 | * See comment in wait_for_readers().
|
---|
| 864 | */
|
---|
| 865 | memory_barrier(); /* MB A, B */
|
---|
| 866 |
|
---|
| 867 | /* At the end of next_cbs_gp, exec next_cbs. Determine what GP that is. */
|
---|
| 868 |
|
---|
| 869 | if (!next_cbs_empty()) {
|
---|
| 870 | spinlock_lock(&rcu.gp_lock);
|
---|
| 871 |
|
---|
| 872 | /* Exec next_cbs at the end of the next GP. */
|
---|
| 873 | CPU->rcu.next_cbs_gp = rcu.cur_gp + 1;
|
---|
| 874 |
|
---|
| 875 | /*
|
---|
| 876 | * There are no callbacks to invoke before next_cbs. Instruct
|
---|
| 877 | * wait_for_cur_cbs_gp() to notify us of the nearest GP end.
|
---|
| 878 | * That could be sooner than next_cbs_gp (if the current GP
|
---|
| 879 | * had not yet completed), so we'll create a shorter batch
|
---|
| 880 | * of callbacks next time around.
|
---|
| 881 | */
|
---|
| 882 | if (cur_cbs_empty()) {
|
---|
| 883 | CPU->rcu.cur_cbs_gp = rcu.completed_gp + 1;
|
---|
| 884 | }
|
---|
| 885 |
|
---|
| 886 | spinlock_unlock(&rcu.gp_lock);
|
---|
| 887 | } else {
|
---|
| 888 | CPU->rcu.next_cbs_gp = CPU->rcu.cur_cbs_gp;
|
---|
| 889 | }
|
---|
| 890 |
|
---|
| 891 | ASSERT(CPU->rcu.cur_cbs_gp <= CPU->rcu.next_cbs_gp);
|
---|
| 892 |
|
---|
| 893 | return expedite;
|
---|
| 894 | }
|
---|
| 895 |
|
---|
| 896 | /** Waits for the grace period associated with callbacks cub_cbs to elapse.
|
---|
| 897 | *
|
---|
| 898 | * @param expedite Instructs the detector to aggressively speed up grace
|
---|
| 899 | * period detection without any delay.
|
---|
| 900 | * @param completed_gp Returns the most recent completed grace period
|
---|
| 901 | * number.
|
---|
| 902 | * @return false if the thread was interrupted and should stop.
|
---|
| 903 | */
|
---|
| 904 | static bool wait_for_cur_cbs_gp_end(bool expedite, rcu_gp_t *completed_gp)
|
---|
| 905 | {
|
---|
| 906 | /*
|
---|
| 907 | * Use a possibly outdated version of completed_gp to bypass checking
|
---|
| 908 | * with the lock.
|
---|
| 909 | *
|
---|
| 910 | * Note that loading and storing rcu.completed_gp is not atomic
|
---|
| 911 | * (it is 64bit wide). Reading a clobbered value that is less than
|
---|
| 912 | * rcu.completed_gp is harmless - we'll recheck with a lock. The
|
---|
| 913 | * only way to read a clobbered value that is greater than the actual
|
---|
| 914 | * value is if the detector increases the higher-order word first and
|
---|
| 915 | * then decreases the lower-order word (or we see stores in that order),
|
---|
| 916 | * eg when incrementing from 2^32 - 1 to 2^32. The loaded value
|
---|
| 917 | * suddenly jumps by 2^32. It would take hours for such an increase
|
---|
| 918 | * to occur so it is safe to discard the value. We allow increases
|
---|
| 919 | * of up to half the maximum to generously accommodate for loading an
|
---|
| 920 | * outdated lower word.
|
---|
| 921 | */
|
---|
| 922 | rcu_gp_t compl_gp = ACCESS_ONCE(rcu.completed_gp);
|
---|
| 923 | if (CPU->rcu.cur_cbs_gp <= compl_gp
|
---|
| 924 | && compl_gp <= CPU->rcu.cur_cbs_gp + UINT32_MAX_HALF) {
|
---|
| 925 | *completed_gp = compl_gp;
|
---|
| 926 | return true;
|
---|
| 927 | }
|
---|
| 928 |
|
---|
| 929 | spinlock_lock(&rcu.gp_lock);
|
---|
| 930 |
|
---|
| 931 | if (CPU->rcu.cur_cbs_gp <= rcu.completed_gp) {
|
---|
| 932 | *completed_gp = rcu.completed_gp;
|
---|
| 933 | spinlock_unlock(&rcu.gp_lock);
|
---|
| 934 | return true;
|
---|
| 935 | }
|
---|
| 936 |
|
---|
| 937 | ASSERT(CPU->rcu.cur_cbs_gp <= CPU->rcu.next_cbs_gp);
|
---|
| 938 | ASSERT(rcu.cur_gp <= CPU->rcu.cur_cbs_gp);
|
---|
| 939 |
|
---|
| 940 | /*
|
---|
| 941 | * Notify the detector of how many GP ends we intend to wait for, so
|
---|
| 942 | * it can avoid going to sleep unnecessarily. Optimistically assume
|
---|
| 943 | * new callbacks will arrive while we're waiting; hence +1.
|
---|
| 944 | */
|
---|
| 945 | size_t remaining_gp_ends = (size_t) (CPU->rcu.next_cbs_gp - rcu.cur_gp);
|
---|
| 946 | req_detection(remaining_gp_ends + (arriving_cbs_empty() ? 0 : 1));
|
---|
| 947 |
|
---|
| 948 | /*
|
---|
| 949 | * Ask the detector to speed up GP detection if there are too many
|
---|
| 950 | * pending callbacks and other reclaimers have not already done so.
|
---|
| 951 | */
|
---|
| 952 | if (expedite) {
|
---|
| 953 | if(0 == rcu.req_expedited_cnt)
|
---|
| 954 | condvar_signal(&rcu.expedite_now);
|
---|
| 955 |
|
---|
| 956 | /*
|
---|
| 957 | * Expedite only cub_cbs. If there really is a surge of callbacks
|
---|
| 958 | * the arriving batch will expedite the GP for the huge number
|
---|
| 959 | * of callbacks currently in next_cbs
|
---|
| 960 | */
|
---|
| 961 | rcu.req_expedited_cnt = 1;
|
---|
| 962 | }
|
---|
| 963 |
|
---|
| 964 | /* Wait for cur_cbs_gp to end. */
|
---|
| 965 | bool interrupted = cv_wait_for_gp(CPU->rcu.cur_cbs_gp);
|
---|
[0cf813d] | 966 |
|
---|
[181a746] | 967 | *completed_gp = rcu.completed_gp;
|
---|
| 968 | spinlock_unlock(&rcu.gp_lock);
|
---|
| 969 |
|
---|
[0cf813d] | 970 | upd_missed_gp_in_wait(*completed_gp);
|
---|
| 971 |
|
---|
[181a746] | 972 | return !interrupted;
|
---|
| 973 | }
|
---|
| 974 |
|
---|
[0cf813d] | 975 | static void upd_missed_gp_in_wait(rcu_gp_t completed_gp)
|
---|
| 976 | {
|
---|
| 977 | ASSERT(CPU->rcu.cur_cbs_gp <= completed_gp);
|
---|
| 978 |
|
---|
| 979 | size_t delta = (size_t)(completed_gp - CPU->rcu.cur_cbs_gp);
|
---|
| 980 | CPU->rcu.stat_missed_gp_in_wait += delta;
|
---|
| 981 | }
|
---|
| 982 |
|
---|
| 983 |
|
---|
[181a746] | 984 | /** Requests the detector to detect at least req_cnt consecutive grace periods.*/
|
---|
| 985 | static void req_detection(size_t req_cnt)
|
---|
| 986 | {
|
---|
| 987 | if (rcu.req_gp_end_cnt < req_cnt) {
|
---|
| 988 | bool detector_idle = (0 == rcu.req_gp_end_cnt);
|
---|
| 989 | rcu.req_gp_end_cnt = req_cnt;
|
---|
| 990 |
|
---|
| 991 | if (detector_idle) {
|
---|
| 992 | ASSERT(rcu.cur_gp == rcu.completed_gp);
|
---|
| 993 | condvar_signal(&rcu.req_gp_changed);
|
---|
| 994 | }
|
---|
| 995 | }
|
---|
| 996 | }
|
---|
| 997 |
|
---|
| 998 | /** Waits for an announcement of the end of the grace period wait_on_gp. */
|
---|
| 999 | static bool cv_wait_for_gp(rcu_gp_t wait_on_gp)
|
---|
| 1000 | {
|
---|
| 1001 | ASSERT(spinlock_locked(&rcu.gp_lock));
|
---|
| 1002 |
|
---|
| 1003 | bool interrupted = false;
|
---|
| 1004 |
|
---|
| 1005 | /* Wait until wait_on_gp ends. */
|
---|
| 1006 | while (rcu.completed_gp < wait_on_gp && !interrupted) {
|
---|
| 1007 | int ret = _condvar_wait_timeout_spinlock(&rcu.gp_ended, &rcu.gp_lock,
|
---|
| 1008 | SYNCH_NO_TIMEOUT, SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
| 1009 | interrupted = (ret == ESYNCH_INTERRUPTED);
|
---|
| 1010 | }
|
---|
| 1011 |
|
---|
| 1012 | ASSERT(wait_on_gp <= rcu.completed_gp);
|
---|
| 1013 |
|
---|
| 1014 | return interrupted;
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
| 1017 | /** The detector thread detects and notifies reclaimers of grace period ends. */
|
---|
| 1018 | static void detector(void *arg)
|
---|
| 1019 | {
|
---|
| 1020 | spinlock_lock(&rcu.gp_lock);
|
---|
| 1021 |
|
---|
| 1022 | while (wait_for_detect_req()) {
|
---|
| 1023 | /*
|
---|
| 1024 | * Announce new GP started. Readers start lazily acknowledging that
|
---|
| 1025 | * they passed a QS.
|
---|
| 1026 | */
|
---|
| 1027 | start_new_gp();
|
---|
| 1028 |
|
---|
| 1029 | spinlock_unlock(&rcu.gp_lock);
|
---|
| 1030 |
|
---|
| 1031 | if (!wait_for_readers())
|
---|
| 1032 | goto unlocked_out;
|
---|
| 1033 |
|
---|
| 1034 | spinlock_lock(&rcu.gp_lock);
|
---|
| 1035 |
|
---|
| 1036 | /* Notify reclaimers that they may now invoke queued callbacks. */
|
---|
| 1037 | end_cur_gp();
|
---|
| 1038 | }
|
---|
| 1039 |
|
---|
| 1040 | spinlock_unlock(&rcu.gp_lock);
|
---|
| 1041 |
|
---|
| 1042 | unlocked_out:
|
---|
| 1043 | return;
|
---|
| 1044 | }
|
---|
| 1045 |
|
---|
| 1046 | /** Waits for a request from a reclaimer thread to detect a grace period. */
|
---|
| 1047 | static bool wait_for_detect_req(void)
|
---|
| 1048 | {
|
---|
| 1049 | ASSERT(spinlock_locked(&rcu.gp_lock));
|
---|
| 1050 |
|
---|
| 1051 | bool interrupted = false;
|
---|
| 1052 |
|
---|
| 1053 | while (0 == rcu.req_gp_end_cnt && !interrupted) {
|
---|
| 1054 | int ret = _condvar_wait_timeout_spinlock(&rcu.req_gp_changed,
|
---|
| 1055 | &rcu.gp_lock, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
| 1056 |
|
---|
| 1057 | interrupted = (ret == ESYNCH_INTERRUPTED);
|
---|
| 1058 | }
|
---|
| 1059 |
|
---|
| 1060 | return !interrupted;
|
---|
| 1061 | }
|
---|
| 1062 |
|
---|
| 1063 | /** Announces the start of a new grace period for preexisting readers to ack. */
|
---|
| 1064 | static void start_new_gp(void)
|
---|
| 1065 | {
|
---|
| 1066 | ASSERT(spinlock_locked(&rcu.gp_lock));
|
---|
| 1067 |
|
---|
| 1068 | irq_spinlock_lock(&rcu.preempt_lock, true);
|
---|
| 1069 |
|
---|
| 1070 | /* Start a new GP. Announce to readers that a quiescent state is needed. */
|
---|
| 1071 | ++rcu.cur_gp;
|
---|
| 1072 |
|
---|
| 1073 | /*
|
---|
| 1074 | * Readers preempted before the start of this GP (next_preempted)
|
---|
| 1075 | * are preexisting readers now that a GP started and will hold up
|
---|
| 1076 | * the current GP until they exit their reader sections.
|
---|
| 1077 | *
|
---|
| 1078 | * Preempted readers from the previous GP have finished so
|
---|
| 1079 | * cur_preempted is empty, but see comment in record_qs().
|
---|
| 1080 | */
|
---|
[c14762e] | 1081 | list_concat(&rcu.cur_preempted, &rcu.next_preempted);
|
---|
[181a746] | 1082 |
|
---|
| 1083 | irq_spinlock_unlock(&rcu.preempt_lock, true);
|
---|
| 1084 | }
|
---|
| 1085 |
|
---|
| 1086 | static void end_cur_gp(void)
|
---|
| 1087 | {
|
---|
| 1088 | ASSERT(spinlock_locked(&rcu.gp_lock));
|
---|
| 1089 |
|
---|
| 1090 | rcu.completed_gp = rcu.cur_gp;
|
---|
| 1091 | --rcu.req_gp_end_cnt;
|
---|
| 1092 |
|
---|
| 1093 | condvar_broadcast(&rcu.gp_ended);
|
---|
| 1094 | }
|
---|
| 1095 |
|
---|
| 1096 | /** Waits for readers that started before the current GP started to finish. */
|
---|
| 1097 | static bool wait_for_readers(void)
|
---|
| 1098 | {
|
---|
| 1099 | DEFINE_CPU_MASK(reading_cpus);
|
---|
| 1100 |
|
---|
| 1101 | /* All running cpus have potential readers. */
|
---|
| 1102 | cpu_mask_active(reading_cpus);
|
---|
| 1103 |
|
---|
| 1104 | /*
|
---|
| 1105 | * Ensure the announcement of the start of a new GP (ie up-to-date
|
---|
| 1106 | * cur_gp) propagates to cpus that are just coming out of idle
|
---|
| 1107 | * mode before we sample their idle state flag.
|
---|
| 1108 | *
|
---|
| 1109 | * Cpus guarantee that after they set CPU->idle = true they will not
|
---|
| 1110 | * execute any RCU reader sections without first setting idle to
|
---|
| 1111 | * false and issuing a memory barrier. Therefore, if rm_quiescent_cpus()
|
---|
| 1112 | * later on sees an idle cpu, but the cpu is just exiting its idle mode,
|
---|
| 1113 | * the cpu must not have yet executed its memory barrier (otherwise
|
---|
| 1114 | * it would pair up with this mem barrier and we would see idle == false).
|
---|
| 1115 | * That memory barrier will pair up with the one below and ensure
|
---|
| 1116 | * that a reader on the now-non-idle cpu will see the most current
|
---|
| 1117 | * cur_gp. As a result, such a reader will never attempt to semaphore_up(
|
---|
| 1118 | * pending_readers) during this GP, which allows the detector to
|
---|
| 1119 | * ignore that cpu (the detector thinks it is idle). Moreover, any
|
---|
| 1120 | * changes made by RCU updaters will have propagated to readers
|
---|
| 1121 | * on the previously idle cpu -- again thanks to issuing a memory
|
---|
| 1122 | * barrier after returning from idle mode.
|
---|
| 1123 | *
|
---|
| 1124 | * idle -> non-idle cpu | detector | reclaimer
|
---|
| 1125 | * ------------------------------------------------------
|
---|
| 1126 | * rcu reader 1 | | rcu_call()
|
---|
| 1127 | * MB X | |
|
---|
| 1128 | * idle = true | | rcu_call()
|
---|
| 1129 | * (no rcu readers allowed ) | | MB A in advance_cbs()
|
---|
| 1130 | * MB Y | (...) | (...)
|
---|
| 1131 | * (no rcu readers allowed) | | MB B in advance_cbs()
|
---|
| 1132 | * idle = false | ++cur_gp |
|
---|
| 1133 | * (no rcu readers allowed) | MB C |
|
---|
| 1134 | * MB Z | signal gp_end |
|
---|
| 1135 | * rcu reader 2 | | exec_cur_cbs()
|
---|
| 1136 | *
|
---|
| 1137 | *
|
---|
| 1138 | * MB Y orders visibility of changes to idle for detector's sake.
|
---|
| 1139 | *
|
---|
| 1140 | * MB Z pairs up with MB C. The cpu making a transition from idle
|
---|
| 1141 | * will see the most current value of cur_gp and will not attempt
|
---|
| 1142 | * to notify the detector even if preempted during this GP.
|
---|
| 1143 | *
|
---|
| 1144 | * MB Z pairs up with MB A from the previous batch. Updaters' changes
|
---|
| 1145 | * are visible to reader 2 even when the detector thinks the cpu is idle
|
---|
| 1146 | * but it is not anymore.
|
---|
| 1147 | *
|
---|
| 1148 | * MB X pairs up with MB B. Late mem accesses of reader 1 are contained
|
---|
| 1149 | * and visible before idling and before any callbacks are executed
|
---|
| 1150 | * by reclaimers.
|
---|
| 1151 | *
|
---|
| 1152 | * In summary, the detector does not know of or wait for reader 2, but
|
---|
| 1153 | * it does not have to since it is a new reader that will not access
|
---|
| 1154 | * data from previous GPs and will see any changes.
|
---|
| 1155 | */
|
---|
| 1156 | memory_barrier(); /* MB C */
|
---|
| 1157 |
|
---|
| 1158 | /*
|
---|
| 1159 | * Give readers time to pass through a QS. Also, batch arriving
|
---|
| 1160 | * callbacks in order to amortize detection overhead.
|
---|
| 1161 | */
|
---|
| 1162 | if (!gp_sleep())
|
---|
| 1163 | return false;
|
---|
| 1164 |
|
---|
| 1165 | /* Non-intrusively determine which cpus have yet to pass a QS. */
|
---|
| 1166 | rm_quiescent_cpus(reading_cpus);
|
---|
| 1167 |
|
---|
| 1168 | /* Actively interrupt cpus delaying the current GP and demand a QS. */
|
---|
| 1169 | interrupt_delaying_cpus(reading_cpus);
|
---|
| 1170 |
|
---|
| 1171 | /* Wait for the interrupted cpus to notify us that they reached a QS. */
|
---|
| 1172 | if (!wait_for_delaying_cpus())
|
---|
| 1173 | return false;
|
---|
| 1174 | /*
|
---|
| 1175 | * All cpus recorded a QS or are still idle. Any new readers will be added
|
---|
| 1176 | * to next_preempt if preempted, ie the number of readers in cur_preempted
|
---|
| 1177 | * monotonically descreases.
|
---|
| 1178 | */
|
---|
| 1179 |
|
---|
| 1180 | /* Wait for the last reader in cur_preempted to notify us it is done. */
|
---|
| 1181 | if (!wait_for_preempt_reader())
|
---|
| 1182 | return false;
|
---|
| 1183 |
|
---|
| 1184 | return true;
|
---|
| 1185 | }
|
---|
| 1186 |
|
---|
| 1187 | /** Remove those cpus from the mask that have already passed a quiescent
|
---|
| 1188 | * state since the start of the current grace period.
|
---|
| 1189 | */
|
---|
| 1190 | static void rm_quiescent_cpus(cpu_mask_t *cpu_mask)
|
---|
| 1191 | {
|
---|
| 1192 | cpu_mask_for_each(*cpu_mask, cpu_id) {
|
---|
| 1193 | /*
|
---|
| 1194 | * The cpu already checked for and passed through a quiescent
|
---|
| 1195 | * state since the beginning of this GP.
|
---|
| 1196 | *
|
---|
| 1197 | * rcu.cur_gp is modified by local detector thread only.
|
---|
| 1198 | * Therefore, it is up-to-date even without a lock.
|
---|
| 1199 | */
|
---|
| 1200 | bool cpu_acked_gp = (cpus[cpu_id].rcu.last_seen_gp == rcu.cur_gp);
|
---|
| 1201 |
|
---|
| 1202 | /*
|
---|
| 1203 | * Either the cpu is idle or it is exiting away from idle mode
|
---|
| 1204 | * and already sees the most current rcu.cur_gp. See comment
|
---|
| 1205 | * in wait_for_readers().
|
---|
| 1206 | */
|
---|
| 1207 | bool cpu_idle = cpus[cpu_id].idle;
|
---|
| 1208 |
|
---|
| 1209 | if (cpu_acked_gp || cpu_idle) {
|
---|
| 1210 | cpu_mask_reset(cpu_mask, cpu_id);
|
---|
| 1211 | }
|
---|
| 1212 | }
|
---|
| 1213 | }
|
---|
| 1214 |
|
---|
| 1215 | /** Sleeps a while if the current grace period is not to be expedited. */
|
---|
| 1216 | static bool gp_sleep(void)
|
---|
| 1217 | {
|
---|
| 1218 | spinlock_lock(&rcu.gp_lock);
|
---|
| 1219 |
|
---|
| 1220 | int ret = 0;
|
---|
| 1221 | while (0 == rcu.req_expedited_cnt && 0 == ret) {
|
---|
| 1222 | /* minor bug: sleeps for the same duration if woken up spuriously. */
|
---|
| 1223 | ret = _condvar_wait_timeout_spinlock(&rcu.expedite_now, &rcu.gp_lock,
|
---|
| 1224 | DETECT_SLEEP_MS * 1000, SYNCH_FLAGS_INTERRUPTIBLE);
|
---|
| 1225 | }
|
---|
| 1226 |
|
---|
| 1227 | if (0 < rcu.req_expedited_cnt) {
|
---|
| 1228 | --rcu.req_expedited_cnt;
|
---|
| 1229 | /* Update statistic. */
|
---|
| 1230 | ++rcu.stat_expedited_cnt;
|
---|
| 1231 | }
|
---|
| 1232 |
|
---|
| 1233 | spinlock_unlock(&rcu.gp_lock);
|
---|
| 1234 |
|
---|
| 1235 | return (ret != ESYNCH_INTERRUPTED);
|
---|
| 1236 | }
|
---|
| 1237 |
|
---|
| 1238 | /** Actively interrupts and checks the offending cpus for quiescent states. */
|
---|
| 1239 | static void interrupt_delaying_cpus(cpu_mask_t *cpu_mask)
|
---|
| 1240 | {
|
---|
| 1241 | const size_t max_conconcurrent_calls = 16;
|
---|
| 1242 | smp_call_t call[max_conconcurrent_calls];
|
---|
| 1243 | size_t outstanding_calls = 0;
|
---|
| 1244 |
|
---|
| 1245 | atomic_set(&rcu.delaying_cpu_cnt, 0);
|
---|
| 1246 |
|
---|
| 1247 | cpu_mask_for_each(*cpu_mask, cpu_id) {
|
---|
| 1248 | smp_call_async(cpu_id, sample_local_cpu, 0, &call[outstanding_calls]);
|
---|
| 1249 | ++outstanding_calls;
|
---|
| 1250 |
|
---|
| 1251 | /* Update statistic. */
|
---|
| 1252 | if (CPU->id != cpu_id)
|
---|
| 1253 | ++rcu.stat_smp_call_cnt;
|
---|
| 1254 |
|
---|
| 1255 | if (outstanding_calls == max_conconcurrent_calls) {
|
---|
| 1256 | for (size_t k = 0; k < outstanding_calls; ++k) {
|
---|
| 1257 | smp_call_wait(&call[k]);
|
---|
| 1258 | }
|
---|
| 1259 |
|
---|
| 1260 | outstanding_calls = 0;
|
---|
| 1261 | }
|
---|
| 1262 | }
|
---|
| 1263 |
|
---|
| 1264 | for (size_t k = 0; k < outstanding_calls; ++k) {
|
---|
| 1265 | smp_call_wait(&call[k]);
|
---|
| 1266 | }
|
---|
| 1267 | }
|
---|
| 1268 |
|
---|
| 1269 | /** Invoked on a cpu delaying grace period detection.
|
---|
| 1270 | *
|
---|
| 1271 | * Induces a quiescent state for the cpu or it instructs remaining
|
---|
| 1272 | * readers to notify the detector once they finish.
|
---|
| 1273 | */
|
---|
| 1274 | static void sample_local_cpu(void *arg)
|
---|
| 1275 | {
|
---|
| 1276 | ASSERT(interrupts_disabled());
|
---|
| 1277 | ASSERT(!CPU->rcu.is_delaying_gp);
|
---|
| 1278 |
|
---|
| 1279 | /* Cpu did not pass a quiescent state yet. */
|
---|
| 1280 | if (CPU->rcu.last_seen_gp != rcu.cur_gp) {
|
---|
| 1281 | /* Interrupted a reader in a reader critical section. */
|
---|
| 1282 | if (0 < (*CPU->rcu.pnesting_cnt)) {
|
---|
| 1283 | ASSERT(!CPU->idle);
|
---|
| 1284 | /* Note to notify the detector from rcu_read_unlock(). */
|
---|
| 1285 | CPU->rcu.is_delaying_gp = true;
|
---|
| 1286 | atomic_inc(&rcu.delaying_cpu_cnt);
|
---|
| 1287 | } else {
|
---|
| 1288 | /*
|
---|
| 1289 | * The cpu did not enter any rcu reader sections since
|
---|
| 1290 | * the start of the current GP. Record a quiescent state.
|
---|
| 1291 | *
|
---|
| 1292 | * Or, we interrupted rcu_read_unlock_impl() right before
|
---|
| 1293 | * it recorded a QS. Record a QS for it. The memory barrier
|
---|
| 1294 | * contains the reader section's mem accesses before
|
---|
| 1295 | * updating last_seen_gp.
|
---|
| 1296 | *
|
---|
| 1297 | * Or, we interrupted rcu_read_lock() right after it recorded
|
---|
| 1298 | * a QS for the previous GP but before it got a chance to
|
---|
| 1299 | * increment its nesting count. The memory barrier again
|
---|
| 1300 | * stops the CS code from spilling out of the CS.
|
---|
| 1301 | */
|
---|
| 1302 | memory_barrier();
|
---|
| 1303 | CPU->rcu.last_seen_gp = rcu.cur_gp;
|
---|
| 1304 | }
|
---|
| 1305 | } else {
|
---|
| 1306 | /*
|
---|
| 1307 | * This cpu already acknowledged that it had passed through
|
---|
| 1308 | * a quiescent state since the start of cur_gp.
|
---|
| 1309 | */
|
---|
| 1310 | }
|
---|
| 1311 |
|
---|
| 1312 | /*
|
---|
| 1313 | * smp_call() makes sure any changes propagate back to the caller.
|
---|
| 1314 | * In particular, it makes the most current last_seen_gp visible
|
---|
| 1315 | * to the detector.
|
---|
| 1316 | */
|
---|
| 1317 | }
|
---|
| 1318 |
|
---|
| 1319 | /** Waits for cpus delaying the current grace period if there are any. */
|
---|
| 1320 | static bool wait_for_delaying_cpus(void)
|
---|
| 1321 | {
|
---|
| 1322 | int delaying_cpu_cnt = atomic_get(&rcu.delaying_cpu_cnt);
|
---|
[79d74fe] | 1323 |
|
---|
[181a746] | 1324 | for (int i = 0; i < delaying_cpu_cnt; ++i){
|
---|
| 1325 | if (!semaphore_down_interruptable(&rcu.remaining_readers))
|
---|
| 1326 | return false;
|
---|
| 1327 | }
|
---|
| 1328 |
|
---|
| 1329 | /* Update statistic. */
|
---|
| 1330 | rcu.stat_delayed_cnt += delaying_cpu_cnt;
|
---|
| 1331 |
|
---|
| 1332 | return true;
|
---|
| 1333 | }
|
---|
| 1334 |
|
---|
| 1335 | /** Waits for any preempted readers blocking this grace period to finish.*/
|
---|
| 1336 | static bool wait_for_preempt_reader(void)
|
---|
| 1337 | {
|
---|
| 1338 | irq_spinlock_lock(&rcu.preempt_lock, true);
|
---|
| 1339 |
|
---|
| 1340 | bool reader_exists = !list_empty(&rcu.cur_preempted);
|
---|
| 1341 | rcu.preempt_blocking_det = reader_exists;
|
---|
| 1342 |
|
---|
| 1343 | irq_spinlock_unlock(&rcu.preempt_lock, true);
|
---|
| 1344 |
|
---|
| 1345 | if (reader_exists) {
|
---|
| 1346 | /* Update statistic. */
|
---|
| 1347 | ++rcu.stat_preempt_blocking_cnt;
|
---|
| 1348 |
|
---|
| 1349 | return semaphore_down_interruptable(&rcu.remaining_readers);
|
---|
| 1350 | }
|
---|
| 1351 |
|
---|
| 1352 | return true;
|
---|
| 1353 | }
|
---|
| 1354 |
|
---|
| 1355 | /** Called by the scheduler() when switching away from the current thread. */
|
---|
| 1356 | void rcu_after_thread_ran(void)
|
---|
| 1357 | {
|
---|
| 1358 | ASSERT(interrupts_disabled());
|
---|
| 1359 | ASSERT(CPU->rcu.pnesting_cnt == &THREAD->rcu.nesting_cnt);
|
---|
| 1360 |
|
---|
| 1361 | /* Preempted a reader critical section for the first time. */
|
---|
| 1362 | if (0 < THREAD->rcu.nesting_cnt && !THREAD->rcu.was_preempted) {
|
---|
| 1363 | THREAD->rcu.was_preempted = true;
|
---|
| 1364 |
|
---|
| 1365 | irq_spinlock_lock(&rcu.preempt_lock, false);
|
---|
| 1366 |
|
---|
| 1367 | if (CPU->rcu.last_seen_gp != rcu.cur_gp) {
|
---|
| 1368 | /* The reader started before the GP started - we must wait for it.*/
|
---|
| 1369 | list_append(&THREAD->rcu.preempt_link, &rcu.cur_preempted);
|
---|
| 1370 | } else {
|
---|
| 1371 | /*
|
---|
| 1372 | * The reader started after the GP started and this cpu
|
---|
| 1373 | * already noted a quiescent state. We might block the next GP.
|
---|
| 1374 | */
|
---|
| 1375 | list_append(&THREAD->rcu.preempt_link, &rcu.next_preempted);
|
---|
| 1376 | }
|
---|
| 1377 |
|
---|
| 1378 | irq_spinlock_unlock(&rcu.preempt_lock, false);
|
---|
| 1379 | }
|
---|
| 1380 |
|
---|
| 1381 | /*
|
---|
| 1382 | * The preempted reader has been noted globally. There are therefore
|
---|
| 1383 | * no readers running on this cpu so this is a quiescent state.
|
---|
| 1384 | */
|
---|
| 1385 | record_qs();
|
---|
| 1386 |
|
---|
| 1387 | /*
|
---|
| 1388 | * This cpu is holding up the current GP. Let the detector know
|
---|
| 1389 | * it has just passed a quiescent state.
|
---|
| 1390 | *
|
---|
| 1391 | * The detector waits separately for preempted readers, so we have
|
---|
| 1392 | * to notify the detector even if we have just preempted a reader.
|
---|
| 1393 | */
|
---|
| 1394 | if (CPU->rcu.is_delaying_gp) {
|
---|
| 1395 | CPU->rcu.is_delaying_gp = false;
|
---|
| 1396 | semaphore_up(&rcu.remaining_readers);
|
---|
| 1397 | }
|
---|
| 1398 |
|
---|
| 1399 | /*
|
---|
| 1400 | * After this point THREAD is 0 and stays 0 until the scheduler()
|
---|
| 1401 | * switches to a new thread. Use a temporary nesting counter for readers
|
---|
| 1402 | * in handlers of interrupts that are raised while idle in the scheduler.
|
---|
| 1403 | */
|
---|
| 1404 | CPU->rcu.pnesting_cnt = &CPU->rcu.tmp_nesting_cnt;
|
---|
| 1405 |
|
---|
| 1406 | /*
|
---|
| 1407 | * Forcefully associate the detector with the highest priority
|
---|
| 1408 | * even if preempted due to its time slice running out.
|
---|
| 1409 | *
|
---|
| 1410 | * todo: Replace with strict scheduler priority classes.
|
---|
| 1411 | */
|
---|
| 1412 | if (THREAD == rcu.detector_thr) {
|
---|
| 1413 | THREAD->priority = -1;
|
---|
| 1414 | }
|
---|
[0cf813d] | 1415 | else if (THREAD == CPU->rcu.reclaimer_thr) {
|
---|
| 1416 | THREAD->priority = -1;
|
---|
| 1417 | }
|
---|
| 1418 |
|
---|
| 1419 | upd_max_cbs_in_slice();
|
---|
| 1420 | }
|
---|
| 1421 |
|
---|
| 1422 | static void upd_max_cbs_in_slice(void)
|
---|
| 1423 | {
|
---|
| 1424 | rcu_cpu_data_t *cr = &CPU->rcu;
|
---|
| 1425 |
|
---|
| 1426 | if (cr->arriving_cbs_cnt > cr->last_arriving_cnt) {
|
---|
| 1427 | size_t arrived_cnt = cr->arriving_cbs_cnt - cr->last_arriving_cnt;
|
---|
| 1428 | cr->stat_max_slice_cbs = max(arrived_cnt, cr->stat_max_slice_cbs);
|
---|
| 1429 | }
|
---|
| 1430 |
|
---|
| 1431 | cr->last_arriving_cnt = cr->arriving_cbs_cnt;
|
---|
[181a746] | 1432 | }
|
---|
| 1433 |
|
---|
| 1434 | /** Called by the scheduler() when switching to a newly scheduled thread. */
|
---|
| 1435 | void rcu_before_thread_runs(void)
|
---|
| 1436 | {
|
---|
| 1437 | ASSERT(PREEMPTION_DISABLED || interrupts_disabled());
|
---|
| 1438 | ASSERT(&CPU->rcu.tmp_nesting_cnt == CPU->rcu.pnesting_cnt);
|
---|
| 1439 |
|
---|
| 1440 | CPU->rcu.pnesting_cnt = &THREAD->rcu.nesting_cnt;
|
---|
| 1441 | }
|
---|
| 1442 |
|
---|
| 1443 |
|
---|
| 1444 | /** Prints RCU run-time statistics. */
|
---|
| 1445 | void rcu_print_stat(void)
|
---|
| 1446 | {
|
---|
[0cf813d] | 1447 | /*
|
---|
| 1448 | * Don't take locks. Worst case is we get out-dated values.
|
---|
| 1449 | * CPU local values are updated without any locks, so there
|
---|
| 1450 | * are no locks to lock in order to get up-to-date values.
|
---|
| 1451 | */
|
---|
| 1452 |
|
---|
| 1453 | printf("Configuration: expedite_threshold=%d, critical_threshold=%d,"
|
---|
| 1454 | " detect_sleep=%dms\n",
|
---|
| 1455 | EXPEDITE_THRESHOLD, CRITICAL_THRESHOLD, DETECT_SLEEP_MS);
|
---|
[181a746] | 1456 | printf("Completed GPs: %" PRIu64 "\n", rcu.completed_gp);
|
---|
| 1457 | printf("Expedited GPs: %zu\n", rcu.stat_expedited_cnt);
|
---|
| 1458 | printf("Delayed GPs: %zu (cpus w/ still running readers after gp sleep)\n",
|
---|
| 1459 | rcu.stat_delayed_cnt);
|
---|
| 1460 | printf("Preempt blocked GPs: %zu (waited for preempted readers; "
|
---|
[b68ae24] | 1461 | "running or not)\n", rcu.stat_preempt_blocking_cnt);
|
---|
| 1462 | printf("Smp calls: %zu\n", rcu.stat_smp_call_cnt);
|
---|
[181a746] | 1463 |
|
---|
[0cf813d] | 1464 | printf("Max arrived callbacks per GP and CPU:\n");
|
---|
| 1465 | for (unsigned int i = 0; i < config.cpu_count; ++i) {
|
---|
[181a746] | 1466 | printf(" %zu", cpus[i].rcu.stat_max_cbs);
|
---|
| 1467 | }
|
---|
| 1468 |
|
---|
[0cf813d] | 1469 | printf("\nAvg arrived callbacks per GP and CPU (nonempty batches only):\n");
|
---|
| 1470 | for (unsigned int i = 0; i < config.cpu_count; ++i) {
|
---|
[181a746] | 1471 | printf(" %zu", cpus[i].rcu.stat_avg_cbs);
|
---|
| 1472 | }
|
---|
| 1473 |
|
---|
[0cf813d] | 1474 | printf("\nMax arrived callbacks per time slice and CPU:\n");
|
---|
| 1475 | for (unsigned int i = 0; i < config.cpu_count; ++i) {
|
---|
| 1476 | printf(" %zu", cpus[i].rcu.stat_max_slice_cbs);
|
---|
| 1477 | }
|
---|
| 1478 |
|
---|
| 1479 | printf("\nMissed GP notifications per CPU:\n");
|
---|
| 1480 | for (unsigned int i = 0; i < config.cpu_count; ++i) {
|
---|
[181a746] | 1481 | printf(" %zu", cpus[i].rcu.stat_missed_gps);
|
---|
| 1482 | }
|
---|
[0cf813d] | 1483 |
|
---|
| 1484 | printf("\nMissed GP notifications per CPU while waking up:\n");
|
---|
| 1485 | for (unsigned int i = 0; i < config.cpu_count; ++i) {
|
---|
| 1486 | printf(" %zu", cpus[i].rcu.stat_missed_gp_in_wait);
|
---|
| 1487 | }
|
---|
[181a746] | 1488 | printf("\n");
|
---|
| 1489 | }
|
---|
| 1490 |
|
---|
| 1491 | /** @}
|
---|
| 1492 | */
|
---|