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