Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | // SPDX-License-Identifier: GPL-2.0-only /* * Copyright 2023 Red Hat */ #include "funnel-workqueue.h" #include <linux/atomic.h> #include <linux/cache.h> #include <linux/completion.h> #include <linux/err.h> #include <linux/kthread.h> #include <linux/percpu.h> #include "funnel-queue.h" #include "logger.h" #include "memory-alloc.h" #include "numeric.h" #include "permassert.h" #include "string-utils.h" #include "completion.h" #include "status-codes.h" static DEFINE_PER_CPU(unsigned int, service_queue_rotor); /** * DOC: Work queue definition. * * There are two types of work queues: simple, with one worker thread, and round-robin, which uses * a group of the former to do the work, and assigns work to them in round-robin fashion (roughly). * Externally, both are represented via the same common sub-structure, though there's actually not * a great deal of overlap between the two types internally. */ struct vdo_work_queue { /* Name of just the work queue (e.g., "cpuQ12") */ char *name; bool round_robin_mode; struct vdo_thread *owner; /* Life cycle functions, etc */ const struct vdo_work_queue_type *type; }; struct simple_work_queue { struct vdo_work_queue common; struct funnel_queue *priority_lists[VDO_WORK_Q_MAX_PRIORITY + 1]; void *private; /* * The fields above are unchanged after setup but often read, and are good candidates for * caching -- and if the max priority is 2, just fit in one x86-64 cache line if aligned. * The fields below are often modified as we sleep and wake, so we want a separate cache * line for performance. */ /* Any (0 or 1) worker threads waiting for new work to do */ wait_queue_head_t waiting_worker_threads ____cacheline_aligned; /* Hack to reduce wakeup calls if the worker thread is running */ atomic_t idle; /* These are infrequently used so in terms of performance we don't care where they land. */ struct task_struct *thread; /* Notify creator once worker has initialized */ struct completion *started; }; struct round_robin_work_queue { struct vdo_work_queue common; struct simple_work_queue **service_queues; unsigned int num_service_queues; }; static inline struct simple_work_queue *as_simple_work_queue(struct vdo_work_queue *queue) { return ((queue == NULL) ? NULL : container_of(queue, struct simple_work_queue, common)); } static inline struct round_robin_work_queue *as_round_robin_work_queue(struct vdo_work_queue *queue) { return ((queue == NULL) ? NULL : container_of(queue, struct round_robin_work_queue, common)); } /* Processing normal completions. */ /* * Dequeue and return the next waiting completion, if any. * * We scan the funnel queues from highest priority to lowest, once; there is therefore a race * condition where a high-priority completion can be enqueued followed by a lower-priority one, and * we'll grab the latter (but we'll catch the high-priority item on the next call). If strict * enforcement of priorities becomes necessary, this function will need fixing. */ static struct vdo_completion *poll_for_completion(struct simple_work_queue *queue) { int i; for (i = queue->common.type->max_priority; i >= 0; i--) { struct funnel_queue_entry *link = vdo_funnel_queue_poll(queue->priority_lists[i]); if (link != NULL) return container_of(link, struct vdo_completion, work_queue_entry_link); } return NULL; } static void enqueue_work_queue_completion(struct simple_work_queue *queue, struct vdo_completion *completion) { VDO_ASSERT_LOG_ONLY(completion->my_queue == NULL, "completion %px (fn %px) to enqueue (%px) is not already queued (%px)", completion, completion->callback, queue, completion->my_queue); if (completion->priority == VDO_WORK_Q_DEFAULT_PRIORITY) completion->priority = queue->common.type->default_priority; if (VDO_ASSERT(completion->priority <= queue->common.type->max_priority, "priority is in range for queue") != VDO_SUCCESS) completion->priority = 0; completion->my_queue = &queue->common; /* Funnel queue handles the synchronization for the put. */ vdo_funnel_queue_put(queue->priority_lists[completion->priority], &completion->work_queue_entry_link); /* * Due to how funnel queue synchronization is handled (just atomic operations), the * simplest safe implementation here would be to wake-up any waiting threads after * enqueueing each item. Even if the funnel queue is not empty at the time of adding an * item to the queue, the consumer thread may not see this since it is not guaranteed to * have the same view of the queue as a producer thread. * * However, the above is wasteful so instead we attempt to minimize the number of thread * wakeups. Using an idle flag, and careful ordering using memory barriers, we should be * able to determine when the worker thread might be asleep or going to sleep. We use * cmpxchg to try to take ownership (vs other producer threads) of the responsibility for * waking the worker thread, so multiple wakeups aren't tried at once. * * This was tuned for some x86 boxes that were handy; it's untested whether doing the read * first is any better or worse for other platforms, even other x86 configurations. */ smp_mb(); if ((atomic_read(&queue->idle) != 1) || (atomic_cmpxchg(&queue->idle, 1, 0) != 1)) return; /* There's a maximum of one thread in this list. */ wake_up(&queue->waiting_worker_threads); } static void run_start_hook(struct simple_work_queue *queue) { if (queue->common.type->start != NULL) queue->common.type->start(queue->private); } static void run_finish_hook(struct simple_work_queue *queue) { if (queue->common.type->finish != NULL) queue->common.type->finish(queue->private); } /* * Wait for the next completion to process, or until kthread_should_stop indicates that it's time * for us to shut down. * * If kthread_should_stop says it's time to stop but we have pending completions return a * completion. * * Also update statistics relating to scheduler interactions. */ static struct vdo_completion *wait_for_next_completion(struct simple_work_queue *queue) { struct vdo_completion *completion; DEFINE_WAIT(wait); while (true) { prepare_to_wait(&queue->waiting_worker_threads, &wait, TASK_INTERRUPTIBLE); /* * Don't set the idle flag until a wakeup will not be lost. * * Force synchronization between setting the idle flag and checking the funnel * queue; the producer side will do them in the reverse order. (There's still a * race condition we've chosen to allow, because we've got a timeout below that * unwedges us if we hit it, but this may narrow the window a little.) */ atomic_set(&queue->idle, 1); smp_mb(); /* store-load barrier between "idle" and funnel queue */ completion = poll_for_completion(queue); if (completion != NULL) break; /* * We need to check for thread-stop after setting TASK_INTERRUPTIBLE state up * above. Otherwise, schedule() will put the thread to sleep and might miss a * wakeup from kthread_stop() call in vdo_finish_work_queue(). */ if (kthread_should_stop()) break; schedule(); /* * Most of the time when we wake, it should be because there's work to do. If it * was a spurious wakeup, continue looping. */ completion = poll_for_completion(queue); if (completion != NULL) break; } finish_wait(&queue->waiting_worker_threads, &wait); atomic_set(&queue->idle, 0); return completion; } static void process_completion(struct simple_work_queue *queue, struct vdo_completion *completion) { if (VDO_ASSERT(completion->my_queue == &queue->common, "completion %px from queue %px marked as being in this queue (%px)", completion, queue, completion->my_queue) == VDO_SUCCESS) completion->my_queue = NULL; vdo_run_completion(completion); } static void service_work_queue(struct simple_work_queue *queue) { run_start_hook(queue); while (true) { struct vdo_completion *completion = poll_for_completion(queue); if (completion == NULL) completion = wait_for_next_completion(queue); if (completion == NULL) { /* No completions but kthread_should_stop() was triggered. */ break; } process_completion(queue, completion); /* * Be friendly to a CPU that has other work to do, if the kernel has told us to. * This speeds up some performance tests; that "other work" might include other VDO * threads. */ if (need_resched()) cond_resched(); } run_finish_hook(queue); } static int work_queue_runner(void *ptr) { struct simple_work_queue *queue = ptr; complete(queue->started); service_work_queue(queue); return 0; } /* Creation & teardown */ static void free_simple_work_queue(struct simple_work_queue *queue) { unsigned int i; for (i = 0; i <= VDO_WORK_Q_MAX_PRIORITY; i++) vdo_free_funnel_queue(queue->priority_lists[i]); vdo_free(queue->common.name); vdo_free(queue); } static void free_round_robin_work_queue(struct round_robin_work_queue *queue) { struct simple_work_queue **queue_table = queue->service_queues; unsigned int count = queue->num_service_queues; unsigned int i; queue->service_queues = NULL; for (i = 0; i < count; i++) free_simple_work_queue(queue_table[i]); vdo_free(queue_table); vdo_free(queue->common.name); vdo_free(queue); } void vdo_free_work_queue(struct vdo_work_queue *queue) { if (queue == NULL) return; vdo_finish_work_queue(queue); if (queue->round_robin_mode) free_round_robin_work_queue(as_round_robin_work_queue(queue)); else free_simple_work_queue(as_simple_work_queue(queue)); } static int make_simple_work_queue(const char *thread_name_prefix, const char *name, struct vdo_thread *owner, void *private, const struct vdo_work_queue_type *type, struct simple_work_queue **queue_ptr) { DECLARE_COMPLETION_ONSTACK(started); struct simple_work_queue *queue; int i; struct task_struct *thread = NULL; int result; VDO_ASSERT_LOG_ONLY((type->max_priority <= VDO_WORK_Q_MAX_PRIORITY), "queue priority count %u within limit %u", type->max_priority, VDO_WORK_Q_MAX_PRIORITY); result = vdo_allocate(1, struct simple_work_queue, "simple work queue", &queue); if (result != VDO_SUCCESS) return result; queue->private = private; queue->started = &started; queue->common.type = type; queue->common.owner = owner; init_waitqueue_head(&queue->waiting_worker_threads); result = vdo_duplicate_string(name, "queue name", &queue->common.name); if (result != VDO_SUCCESS) { vdo_free(queue); return -ENOMEM; } for (i = 0; i <= type->max_priority; i++) { result = vdo_make_funnel_queue(&queue->priority_lists[i]); if (result != VDO_SUCCESS) { free_simple_work_queue(queue); return result; } } thread = kthread_run(work_queue_runner, queue, "%s:%s", thread_name_prefix, queue->common.name); if (IS_ERR(thread)) { free_simple_work_queue(queue); return (int) PTR_ERR(thread); } queue->thread = thread; /* * If we don't wait to ensure the thread is running VDO code, a quick kthread_stop (due to * errors elsewhere) could cause it to never get as far as running VDO, skipping the * cleanup code. * * Eventually we should just make that path safe too, and then we won't need this * synchronization. */ wait_for_completion(&started); *queue_ptr = queue; return VDO_SUCCESS; } /** * vdo_make_work_queue() - Create a work queue; if multiple threads are requested, completions will * be distributed to them in round-robin fashion. * * Each queue is associated with a struct vdo_thread which has a single vdo thread id. Regardless * of the actual number of queues and threads allocated here, code outside of the queue * implementation will treat this as a single zone. */ int vdo_make_work_queue(const char *thread_name_prefix, const char *name, struct vdo_thread *owner, const struct vdo_work_queue_type *type, unsigned int thread_count, void *thread_privates[], struct vdo_work_queue **queue_ptr) { struct round_robin_work_queue *queue; int result; char thread_name[TASK_COMM_LEN]; unsigned int i; if (thread_count == 1) { struct simple_work_queue *simple_queue; void *context = ((thread_privates != NULL) ? thread_privates[0] : NULL); result = make_simple_work_queue(thread_name_prefix, name, owner, context, type, &simple_queue); if (result == VDO_SUCCESS) *queue_ptr = &simple_queue->common; return result; } result = vdo_allocate(1, struct round_robin_work_queue, "round-robin work queue", &queue); if (result != VDO_SUCCESS) return result; result = vdo_allocate(thread_count, struct simple_work_queue *, "subordinate work queues", &queue->service_queues); if (result != VDO_SUCCESS) { vdo_free(queue); return result; } queue->num_service_queues = thread_count; queue->common.round_robin_mode = true; queue->common.owner = owner; result = vdo_duplicate_string(name, "queue name", &queue->common.name); if (result != VDO_SUCCESS) { vdo_free(queue->service_queues); vdo_free(queue); return -ENOMEM; } *queue_ptr = &queue->common; for (i = 0; i < thread_count; i++) { void *context = ((thread_privates != NULL) ? thread_privates[i] : NULL); snprintf(thread_name, sizeof(thread_name), "%s%u", name, i); result = make_simple_work_queue(thread_name_prefix, thread_name, owner, context, type, &queue->service_queues[i]); if (result != VDO_SUCCESS) { queue->num_service_queues = i; /* Destroy previously created subordinates. */ vdo_free_work_queue(vdo_forget(*queue_ptr)); return result; } } return VDO_SUCCESS; } static void finish_simple_work_queue(struct simple_work_queue *queue) { if (queue->thread == NULL) return; /* Tells the worker thread to shut down and waits for it to exit. */ kthread_stop(queue->thread); queue->thread = NULL; } static void finish_round_robin_work_queue(struct round_robin_work_queue *queue) { struct simple_work_queue **queue_table = queue->service_queues; unsigned int count = queue->num_service_queues; unsigned int i; for (i = 0; i < count; i++) finish_simple_work_queue(queue_table[i]); } /* No enqueueing of completions should be done once this function is called. */ void vdo_finish_work_queue(struct vdo_work_queue *queue) { if (queue == NULL) return; if (queue->round_robin_mode) finish_round_robin_work_queue(as_round_robin_work_queue(queue)); else finish_simple_work_queue(as_simple_work_queue(queue)); } /* Debugging dumps */ static void dump_simple_work_queue(struct simple_work_queue *queue) { const char *thread_status = "no threads"; char task_state_report = '-'; if (queue->thread != NULL) { task_state_report = task_state_to_char(queue->thread); thread_status = atomic_read(&queue->idle) ? "idle" : "running"; } vdo_log_info("workQ %px (%s) %s (%c)", &queue->common, queue->common.name, thread_status, task_state_report); /* ->waiting_worker_threads wait queue status? anyone waiting? */ } /* * Write to the buffer some info about the completion, for logging. Since the common use case is * dumping info about a lot of completions to syslog all at once, the format favors brevity over * readability. */ void vdo_dump_work_queue(struct vdo_work_queue *queue) { if (queue->round_robin_mode) { struct round_robin_work_queue *round_robin = as_round_robin_work_queue(queue); unsigned int i; for (i = 0; i < round_robin->num_service_queues; i++) dump_simple_work_queue(round_robin->service_queues[i]); } else { dump_simple_work_queue(as_simple_work_queue(queue)); } } static void get_function_name(void *pointer, char *buffer, size_t buffer_length) { if (pointer == NULL) { /* * Format "%ps" logs a null pointer as "(null)" with a bunch of leading spaces. We * sometimes use this when logging lots of data; don't be so verbose. */ strscpy(buffer, "-", buffer_length); } else { /* * Use a pragma to defeat gcc's format checking, which doesn't understand that * "%ps" actually does support a precision spec in Linux kernel code. */ char *space; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat" snprintf(buffer, buffer_length, "%.*ps", buffer_length - 1, pointer); #pragma GCC diagnostic pop space = strchr(buffer, ' '); if (space != NULL) *space = '\0'; } } void vdo_dump_completion_to_buffer(struct vdo_completion *completion, char *buffer, size_t length) { size_t current_length = scnprintf(buffer, length, "%.*s/", TASK_COMM_LEN, (completion->my_queue == NULL ? "-" : completion->my_queue->name)); if (current_length < length - 1) { get_function_name((void *) completion->callback, buffer + current_length, length - current_length); } } /* Completion submission */ /* * If the completion has a timeout that has already passed, the timeout handler function may be * invoked by this function. */ void vdo_enqueue_work_queue(struct vdo_work_queue *queue, struct vdo_completion *completion) { /* * Convert the provided generic vdo_work_queue to the simple_work_queue to actually queue * on. */ struct simple_work_queue *simple_queue = NULL; if (!queue->round_robin_mode) { simple_queue = as_simple_work_queue(queue); } else { struct round_robin_work_queue *round_robin = as_round_robin_work_queue(queue); /* * It shouldn't be a big deal if the same rotor gets used for multiple work queues. * Any patterns that might develop are likely to be disrupted by random ordering of * multiple completions and migration between cores, unless the load is so light as * to be regular in ordering of tasks and the threads are confined to individual * cores; with a load that light we won't care. */ unsigned int rotor = this_cpu_inc_return(service_queue_rotor); unsigned int index = rotor % round_robin->num_service_queues; simple_queue = round_robin->service_queues[index]; } enqueue_work_queue_completion(simple_queue, completion); } /* Misc */ /* * Return the work queue pointer recorded at initialization time in the work-queue stack handle * initialized on the stack of the current thread, if any. */ static struct simple_work_queue *get_current_thread_work_queue(void) { /* * In interrupt context, if a vdo thread is what got interrupted, the calls below will find * the queue for the thread which was interrupted. However, the interrupted thread may have * been processing a completion, in which case starting to process another would violate * our concurrency assumptions. */ if (in_interrupt()) return NULL; if (kthread_func(current) != work_queue_runner) /* Not a VDO work queue thread. */ return NULL; return kthread_data(current); } struct vdo_work_queue *vdo_get_current_work_queue(void) { struct simple_work_queue *queue = get_current_thread_work_queue(); return (queue == NULL) ? NULL : &queue->common; } struct vdo_thread *vdo_get_work_queue_owner(struct vdo_work_queue *queue) { return queue->owner; } /** * vdo_get_work_queue_private_data() - Returns the private data for the current thread's work * queue, or NULL if none or if the current thread is not a * work queue thread. */ void *vdo_get_work_queue_private_data(void) { struct simple_work_queue *queue = get_current_thread_work_queue(); return (queue != NULL) ? queue->private : NULL; } bool vdo_work_queue_type_is(struct vdo_work_queue *queue, const struct vdo_work_queue_type *type) { return (queue->type == type); } |