| 1 | /* |
| 2 | * workqueue.c |
| 3 | * |
| 4 | * Userspace RCU library - Userspace workqeues |
| 5 | * |
| 6 | * Copyright (c) 2010 Paul E. McKenney <paulmck@linux.vnet.ibm.com> |
| 7 | * Copyright (c) 2017 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU Lesser General Public |
| 11 | * License as published by the Free Software Foundation; either |
| 12 | * version 2.1 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public |
| 20 | * License along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 | */ |
| 23 | |
| 24 | #define _LGPL_SOURCE |
| 25 | #include <stdio.h> |
| 26 | #include <pthread.h> |
| 27 | #include <signal.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stdint.h> |
| 30 | #include <string.h> |
| 31 | #include <errno.h> |
| 32 | #include <poll.h> |
| 33 | #include <sys/time.h> |
| 34 | #include <unistd.h> |
| 35 | #include <sched.h> |
| 36 | |
| 37 | #include "compat-getcpu.h" |
| 38 | #include <urcu/assert.h> |
| 39 | #include <urcu/wfcqueue.h> |
| 40 | #include <urcu/pointer.h> |
| 41 | #include <urcu/list.h> |
| 42 | #include <urcu/futex.h> |
| 43 | #include <urcu/tls-compat.h> |
| 44 | #include <urcu/ref.h> |
| 45 | #include "urcu-die.h" |
| 46 | |
| 47 | #include "workqueue.h" |
| 48 | |
| 49 | #define SET_AFFINITY_CHECK_PERIOD (1U << 8) /* 256 */ |
| 50 | #define SET_AFFINITY_CHECK_PERIOD_MASK (SET_AFFINITY_CHECK_PERIOD - 1) |
| 51 | |
| 52 | /* Data structure that identifies a workqueue. */ |
| 53 | |
| 54 | struct urcu_workqueue { |
| 55 | /* |
| 56 | * We do not align head on a different cache-line than tail |
| 57 | * mainly because workqueue threads use batching ("splice") to |
| 58 | * get an entire list of callbacks, which effectively empties |
| 59 | * the queue, and requires to touch the tail anyway. |
| 60 | */ |
| 61 | struct cds_wfcq_tail cbs_tail; |
| 62 | struct cds_wfcq_head cbs_head; |
| 63 | unsigned long flags; |
| 64 | int32_t futex; |
| 65 | unsigned long qlen; /* maintained for debugging. */ |
| 66 | pthread_t tid; |
| 67 | int cpu_affinity; |
| 68 | unsigned long loop_count; |
| 69 | void *priv; |
| 70 | void (*grace_period_fct)(struct urcu_workqueue *workqueue, void *priv); |
| 71 | void (*initialize_worker_fct)(struct urcu_workqueue *workqueue, void *priv); |
| 72 | void (*finalize_worker_fct)(struct urcu_workqueue *workqueue, void *priv); |
| 73 | void (*worker_before_pause_fct)(struct urcu_workqueue *workqueue, void *priv); |
| 74 | void (*worker_after_resume_fct)(struct urcu_workqueue *workqueue, void *priv); |
| 75 | void (*worker_before_wait_fct)(struct urcu_workqueue *workqueue, void *priv); |
| 76 | void (*worker_after_wake_up_fct)(struct urcu_workqueue *workqueue, void *priv); |
| 77 | } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); |
| 78 | |
| 79 | struct urcu_workqueue_completion { |
| 80 | int barrier_count; |
| 81 | int32_t futex; |
| 82 | struct urcu_ref ref; |
| 83 | }; |
| 84 | |
| 85 | struct urcu_workqueue_completion_work { |
| 86 | struct urcu_work work; |
| 87 | struct urcu_workqueue_completion *completion; |
| 88 | }; |
| 89 | |
| 90 | /* |
| 91 | * Periodically retry setting CPU affinity if we migrate. |
| 92 | * Losing affinity can be caused by CPU hotunplug/hotplug, or by |
| 93 | * cpuset(7). |
| 94 | */ |
| 95 | #ifdef HAVE_SCHED_SETAFFINITY |
| 96 | static int set_thread_cpu_affinity(struct urcu_workqueue *workqueue) |
| 97 | { |
| 98 | cpu_set_t mask; |
| 99 | int ret; |
| 100 | |
| 101 | if (workqueue->cpu_affinity < 0) |
| 102 | return 0; |
| 103 | if (++workqueue->loop_count & SET_AFFINITY_CHECK_PERIOD_MASK) |
| 104 | return 0; |
| 105 | if (urcu_sched_getcpu() == workqueue->cpu_affinity) |
| 106 | return 0; |
| 107 | |
| 108 | CPU_ZERO(&mask); |
| 109 | CPU_SET(workqueue->cpu_affinity, &mask); |
| 110 | ret = sched_setaffinity(0, sizeof(mask), &mask); |
| 111 | |
| 112 | /* |
| 113 | * EINVAL is fine: can be caused by hotunplugged CPUs, or by |
| 114 | * cpuset(7). This is why we should always retry if we detect |
| 115 | * migration. |
| 116 | */ |
| 117 | if (ret && errno == EINVAL) { |
| 118 | ret = 0; |
| 119 | errno = 0; |
| 120 | } |
| 121 | return ret; |
| 122 | } |
| 123 | #else |
| 124 | static int set_thread_cpu_affinity(struct urcu_workqueue *workqueue __attribute__((unused))) |
| 125 | { |
| 126 | return 0; |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | static void futex_wait(int32_t *futex) |
| 131 | { |
| 132 | /* Read condition before read futex */ |
| 133 | cmm_smp_mb(); |
| 134 | while (uatomic_read(futex) == -1) { |
| 135 | if (!futex_async(futex, FUTEX_WAIT, -1, NULL, NULL, 0)) { |
| 136 | /* |
| 137 | * Prior queued wakeups queued by unrelated code |
| 138 | * using the same address can cause futex wait to |
| 139 | * return 0 even through the futex value is still |
| 140 | * -1 (spurious wakeups). Check the value again |
| 141 | * in user-space to validate whether it really |
| 142 | * differs from -1. |
| 143 | */ |
| 144 | continue; |
| 145 | } |
| 146 | switch (errno) { |
| 147 | case EAGAIN: |
| 148 | /* Value already changed. */ |
| 149 | return; |
| 150 | case EINTR: |
| 151 | /* Retry if interrupted by signal. */ |
| 152 | break; /* Get out of switch. Check again. */ |
| 153 | default: |
| 154 | /* Unexpected error. */ |
| 155 | urcu_die(errno); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | static void futex_wake_up(int32_t *futex) |
| 161 | { |
| 162 | /* Write to condition before reading/writing futex */ |
| 163 | cmm_smp_mb(); |
| 164 | if (caa_unlikely(uatomic_read(futex) == -1)) { |
| 165 | uatomic_set(futex, 0); |
| 166 | if (futex_async(futex, FUTEX_WAKE, 1, |
| 167 | NULL, NULL, 0) < 0) |
| 168 | urcu_die(errno); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /* This is the code run by each worker thread. */ |
| 173 | |
| 174 | static void *workqueue_thread(void *arg) |
| 175 | { |
| 176 | unsigned long cbcount; |
| 177 | struct urcu_workqueue *workqueue = (struct urcu_workqueue *) arg; |
| 178 | int rt = !!(uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_RT); |
| 179 | |
| 180 | if (set_thread_cpu_affinity(workqueue)) |
| 181 | urcu_die(errno); |
| 182 | |
| 183 | if (workqueue->initialize_worker_fct) |
| 184 | workqueue->initialize_worker_fct(workqueue, workqueue->priv); |
| 185 | |
| 186 | if (!rt) { |
| 187 | uatomic_dec(&workqueue->futex); |
| 188 | /* Decrement futex before reading workqueue */ |
| 189 | cmm_smp_mb(); |
| 190 | } |
| 191 | for (;;) { |
| 192 | struct cds_wfcq_head cbs_tmp_head; |
| 193 | struct cds_wfcq_tail cbs_tmp_tail; |
| 194 | struct cds_wfcq_node *cbs, *cbs_tmp_n; |
| 195 | enum cds_wfcq_ret splice_ret; |
| 196 | |
| 197 | if (set_thread_cpu_affinity(workqueue)) |
| 198 | urcu_die(errno); |
| 199 | |
| 200 | if (uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSE) { |
| 201 | /* |
| 202 | * Pause requested. Become quiescent: remove |
| 203 | * ourself from all global lists, and don't |
| 204 | * process any callback. The callback lists may |
| 205 | * still be non-empty though. |
| 206 | */ |
| 207 | if (workqueue->worker_before_pause_fct) |
| 208 | workqueue->worker_before_pause_fct(workqueue, workqueue->priv); |
| 209 | cmm_smp_mb__before_uatomic_or(); |
| 210 | uatomic_or(&workqueue->flags, URCU_WORKQUEUE_PAUSED); |
| 211 | while ((uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSE) != 0) |
| 212 | (void) poll(NULL, 0, 1); |
| 213 | uatomic_and(&workqueue->flags, ~URCU_WORKQUEUE_PAUSED); |
| 214 | cmm_smp_mb__after_uatomic_and(); |
| 215 | if (workqueue->worker_after_resume_fct) |
| 216 | workqueue->worker_after_resume_fct(workqueue, workqueue->priv); |
| 217 | } |
| 218 | |
| 219 | cds_wfcq_init(&cbs_tmp_head, &cbs_tmp_tail); |
| 220 | splice_ret = __cds_wfcq_splice_blocking(&cbs_tmp_head, |
| 221 | &cbs_tmp_tail, &workqueue->cbs_head, &workqueue->cbs_tail); |
| 222 | urcu_posix_assert(splice_ret != CDS_WFCQ_RET_WOULDBLOCK); |
| 223 | urcu_posix_assert(splice_ret != CDS_WFCQ_RET_DEST_NON_EMPTY); |
| 224 | if (splice_ret != CDS_WFCQ_RET_SRC_EMPTY) { |
| 225 | if (workqueue->grace_period_fct) |
| 226 | workqueue->grace_period_fct(workqueue, workqueue->priv); |
| 227 | cbcount = 0; |
| 228 | __cds_wfcq_for_each_blocking_safe(&cbs_tmp_head, |
| 229 | &cbs_tmp_tail, cbs, cbs_tmp_n) { |
| 230 | struct urcu_work *uwp; |
| 231 | |
| 232 | uwp = caa_container_of(cbs, |
| 233 | struct urcu_work, next); |
| 234 | uwp->func(uwp); |
| 235 | cbcount++; |
| 236 | } |
| 237 | uatomic_sub(&workqueue->qlen, cbcount); |
| 238 | } |
| 239 | if (uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_STOP) |
| 240 | break; |
| 241 | if (workqueue->worker_before_wait_fct) |
| 242 | workqueue->worker_before_wait_fct(workqueue, workqueue->priv); |
| 243 | if (!rt) { |
| 244 | if (cds_wfcq_empty(&workqueue->cbs_head, |
| 245 | &workqueue->cbs_tail)) { |
| 246 | futex_wait(&workqueue->futex); |
| 247 | uatomic_dec(&workqueue->futex); |
| 248 | /* |
| 249 | * Decrement futex before reading |
| 250 | * urcu_work list. |
| 251 | */ |
| 252 | cmm_smp_mb(); |
| 253 | } |
| 254 | } else { |
| 255 | if (cds_wfcq_empty(&workqueue->cbs_head, |
| 256 | &workqueue->cbs_tail)) { |
| 257 | (void) poll(NULL, 0, 10); |
| 258 | } |
| 259 | } |
| 260 | if (workqueue->worker_after_wake_up_fct) |
| 261 | workqueue->worker_after_wake_up_fct(workqueue, workqueue->priv); |
| 262 | } |
| 263 | if (!rt) { |
| 264 | /* |
| 265 | * Read urcu_work list before write futex. |
| 266 | */ |
| 267 | cmm_smp_mb(); |
| 268 | uatomic_set(&workqueue->futex, 0); |
| 269 | } |
| 270 | if (workqueue->finalize_worker_fct) |
| 271 | workqueue->finalize_worker_fct(workqueue, workqueue->priv); |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | struct urcu_workqueue *urcu_workqueue_create(unsigned long flags, |
| 276 | int cpu_affinity, void *priv, |
| 277 | void (*grace_period_fct)(struct urcu_workqueue *workqueue, void *priv), |
| 278 | void (*initialize_worker_fct)(struct urcu_workqueue *workqueue, void *priv), |
| 279 | void (*finalize_worker_fct)(struct urcu_workqueue *workqueue, void *priv), |
| 280 | void (*worker_before_wait_fct)(struct urcu_workqueue *workqueue, void *priv), |
| 281 | void (*worker_after_wake_up_fct)(struct urcu_workqueue *workqueue, void *priv), |
| 282 | void (*worker_before_pause_fct)(struct urcu_workqueue *workqueue, void *priv), |
| 283 | void (*worker_after_resume_fct)(struct urcu_workqueue *workqueue, void *priv)) |
| 284 | { |
| 285 | struct urcu_workqueue *workqueue; |
| 286 | int ret; |
| 287 | |
| 288 | workqueue = malloc(sizeof(*workqueue)); |
| 289 | if (workqueue == NULL) |
| 290 | urcu_die(errno); |
| 291 | memset(workqueue, '\0', sizeof(*workqueue)); |
| 292 | cds_wfcq_init(&workqueue->cbs_head, &workqueue->cbs_tail); |
| 293 | workqueue->qlen = 0; |
| 294 | workqueue->futex = 0; |
| 295 | workqueue->flags = flags; |
| 296 | workqueue->priv = priv; |
| 297 | workqueue->grace_period_fct = grace_period_fct; |
| 298 | workqueue->initialize_worker_fct = initialize_worker_fct; |
| 299 | workqueue->finalize_worker_fct = finalize_worker_fct; |
| 300 | workqueue->worker_before_wait_fct = worker_before_wait_fct; |
| 301 | workqueue->worker_after_wake_up_fct = worker_after_wake_up_fct; |
| 302 | workqueue->worker_before_pause_fct = worker_before_pause_fct; |
| 303 | workqueue->worker_after_resume_fct = worker_after_resume_fct; |
| 304 | workqueue->cpu_affinity = cpu_affinity; |
| 305 | workqueue->loop_count = 0; |
| 306 | cmm_smp_mb(); /* Structure initialized before pointer is planted. */ |
| 307 | ret = pthread_create(&workqueue->tid, NULL, workqueue_thread, workqueue); |
| 308 | if (ret) { |
| 309 | urcu_die(ret); |
| 310 | } |
| 311 | return workqueue; |
| 312 | } |
| 313 | |
| 314 | static void wake_worker_thread(struct urcu_workqueue *workqueue) |
| 315 | { |
| 316 | if (!(_CMM_LOAD_SHARED(workqueue->flags) & URCU_WORKQUEUE_RT)) |
| 317 | futex_wake_up(&workqueue->futex); |
| 318 | } |
| 319 | |
| 320 | static int urcu_workqueue_destroy_worker(struct urcu_workqueue *workqueue) |
| 321 | { |
| 322 | int ret; |
| 323 | void *retval; |
| 324 | |
| 325 | uatomic_or(&workqueue->flags, URCU_WORKQUEUE_STOP); |
| 326 | wake_worker_thread(workqueue); |
| 327 | |
| 328 | ret = pthread_join(workqueue->tid, &retval); |
| 329 | if (ret) { |
| 330 | urcu_die(ret); |
| 331 | } |
| 332 | if (retval != NULL) { |
| 333 | urcu_die(EINVAL); |
| 334 | } |
| 335 | workqueue->flags &= ~URCU_WORKQUEUE_STOP; |
| 336 | workqueue->tid = 0; |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | void urcu_workqueue_destroy(struct urcu_workqueue *workqueue) |
| 341 | { |
| 342 | if (workqueue == NULL) { |
| 343 | return; |
| 344 | } |
| 345 | if (urcu_workqueue_destroy_worker(workqueue)) { |
| 346 | urcu_die(errno); |
| 347 | } |
| 348 | urcu_posix_assert(cds_wfcq_empty(&workqueue->cbs_head, &workqueue->cbs_tail)); |
| 349 | free(workqueue); |
| 350 | } |
| 351 | |
| 352 | void urcu_workqueue_queue_work(struct urcu_workqueue *workqueue, |
| 353 | struct urcu_work *work, |
| 354 | void (*func)(struct urcu_work *work)) |
| 355 | { |
| 356 | cds_wfcq_node_init(&work->next); |
| 357 | work->func = func; |
| 358 | cds_wfcq_enqueue(&workqueue->cbs_head, &workqueue->cbs_tail, &work->next); |
| 359 | uatomic_inc(&workqueue->qlen); |
| 360 | wake_worker_thread(workqueue); |
| 361 | } |
| 362 | |
| 363 | static |
| 364 | void free_completion(struct urcu_ref *ref) |
| 365 | { |
| 366 | struct urcu_workqueue_completion *completion; |
| 367 | |
| 368 | completion = caa_container_of(ref, struct urcu_workqueue_completion, ref); |
| 369 | free(completion); |
| 370 | } |
| 371 | |
| 372 | static |
| 373 | void _urcu_workqueue_wait_complete(struct urcu_work *work) |
| 374 | { |
| 375 | struct urcu_workqueue_completion_work *completion_work; |
| 376 | struct urcu_workqueue_completion *completion; |
| 377 | |
| 378 | completion_work = caa_container_of(work, struct urcu_workqueue_completion_work, work); |
| 379 | completion = completion_work->completion; |
| 380 | if (!uatomic_sub_return(&completion->barrier_count, 1)) |
| 381 | futex_wake_up(&completion->futex); |
| 382 | urcu_ref_put(&completion->ref, free_completion); |
| 383 | free(completion_work); |
| 384 | } |
| 385 | |
| 386 | struct urcu_workqueue_completion *urcu_workqueue_create_completion(void) |
| 387 | { |
| 388 | struct urcu_workqueue_completion *completion; |
| 389 | |
| 390 | completion = calloc(sizeof(*completion), 1); |
| 391 | if (!completion) |
| 392 | urcu_die(errno); |
| 393 | urcu_ref_set(&completion->ref, 1); |
| 394 | completion->barrier_count = 0; |
| 395 | return completion; |
| 396 | } |
| 397 | |
| 398 | void urcu_workqueue_destroy_completion(struct urcu_workqueue_completion *completion) |
| 399 | { |
| 400 | urcu_ref_put(&completion->ref, free_completion); |
| 401 | } |
| 402 | |
| 403 | void urcu_workqueue_wait_completion(struct urcu_workqueue_completion *completion) |
| 404 | { |
| 405 | /* Wait for them */ |
| 406 | for (;;) { |
| 407 | uatomic_dec(&completion->futex); |
| 408 | /* Decrement futex before reading barrier_count */ |
| 409 | cmm_smp_mb(); |
| 410 | if (!uatomic_read(&completion->barrier_count)) |
| 411 | break; |
| 412 | futex_wait(&completion->futex); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | void urcu_workqueue_queue_completion(struct urcu_workqueue *workqueue, |
| 417 | struct urcu_workqueue_completion *completion) |
| 418 | { |
| 419 | struct urcu_workqueue_completion_work *work; |
| 420 | |
| 421 | work = calloc(sizeof(*work), 1); |
| 422 | if (!work) |
| 423 | urcu_die(errno); |
| 424 | work->completion = completion; |
| 425 | urcu_ref_get(&completion->ref); |
| 426 | uatomic_inc(&completion->barrier_count); |
| 427 | urcu_workqueue_queue_work(workqueue, &work->work, _urcu_workqueue_wait_complete); |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Wait for all in-flight work to complete execution. |
| 432 | */ |
| 433 | void urcu_workqueue_flush_queued_work(struct urcu_workqueue *workqueue) |
| 434 | { |
| 435 | struct urcu_workqueue_completion *completion; |
| 436 | |
| 437 | completion = urcu_workqueue_create_completion(); |
| 438 | if (!completion) |
| 439 | urcu_die(ENOMEM); |
| 440 | urcu_workqueue_queue_completion(workqueue, completion); |
| 441 | urcu_workqueue_wait_completion(completion); |
| 442 | urcu_workqueue_destroy_completion(completion); |
| 443 | } |
| 444 | |
| 445 | /* To be used in before fork handler. */ |
| 446 | void urcu_workqueue_pause_worker(struct urcu_workqueue *workqueue) |
| 447 | { |
| 448 | uatomic_or(&workqueue->flags, URCU_WORKQUEUE_PAUSE); |
| 449 | cmm_smp_mb__after_uatomic_or(); |
| 450 | wake_worker_thread(workqueue); |
| 451 | |
| 452 | while ((uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSED) == 0) |
| 453 | (void) poll(NULL, 0, 1); |
| 454 | } |
| 455 | |
| 456 | /* To be used in after fork parent handler. */ |
| 457 | void urcu_workqueue_resume_worker(struct urcu_workqueue *workqueue) |
| 458 | { |
| 459 | uatomic_and(&workqueue->flags, ~URCU_WORKQUEUE_PAUSE); |
| 460 | while ((uatomic_read(&workqueue->flags) & URCU_WORKQUEUE_PAUSED) != 0) |
| 461 | (void) poll(NULL, 0, 1); |
| 462 | } |
| 463 | |
| 464 | void urcu_workqueue_create_worker(struct urcu_workqueue *workqueue) |
| 465 | { |
| 466 | int ret; |
| 467 | |
| 468 | /* Clear workqueue state from parent. */ |
| 469 | workqueue->flags &= ~URCU_WORKQUEUE_PAUSED; |
| 470 | workqueue->flags &= ~URCU_WORKQUEUE_PAUSE; |
| 471 | workqueue->tid = 0; |
| 472 | ret = pthread_create(&workqueue->tid, NULL, workqueue_thread, workqueue); |
| 473 | if (ret) { |
| 474 | urcu_die(ret); |
| 475 | } |
| 476 | } |