completion.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #include "completion.h"
  6. #include <linux/kernel.h>
  7. #include "logger.h"
  8. #include "permassert.h"
  9. #include "status-codes.h"
  10. #include "types.h"
  11. #include "vio.h"
  12. #include "vdo.h"
  13. /**
  14. * DOC: vdo completions.
  15. *
  16. * Most of vdo's data structures are lock free, each either belonging to a single "zone," or
  17. * divided into a number of zones whose accesses to the structure do not overlap. During normal
  18. * operation, at most one thread will be operating in any given zone. Each zone has a
  19. * vdo_work_queue which holds vdo_completions that are to be run in that zone. A completion may
  20. * only be enqueued on one queue or operating in a single zone at a time.
  21. *
  22. * At each step of a multi-threaded operation, the completion performing the operation is given a
  23. * callback, error handler, and thread id for the next step. A completion is "run" when it is
  24. * operating on the correct thread (as specified by its callback_thread_id). If the value of its
  25. * "result" field is an error (i.e. not VDO_SUCCESS), the function in its "error_handler" will be
  26. * invoked. If the error_handler is NULL, or there is no error, the function set as its "callback"
  27. * will be invoked. Generally, a completion will not be run directly, but rather will be
  28. * "launched." In this case, it will check whether it is operating on the correct thread. If it is,
  29. * it will run immediately. Otherwise, it will be enqueue on the vdo_work_queue associated with the
  30. * completion's "callback_thread_id". When it is dequeued, it will be on the correct thread, and
  31. * will get run. In some cases, the completion should get queued instead of running immediately,
  32. * even if it is being launched from the correct thread. This is usually in cases where there is a
  33. * long chain of callbacks, all on the same thread, which could overflow the stack. In such cases,
  34. * the completion's "requeue" field should be set to true. Doing so will skip the current thread
  35. * check and simply enqueue the completion.
  36. *
  37. * A completion may be "finished," in which case its "complete" field will be set to true before it
  38. * is next run. It is a bug to attempt to set the result or re-finish a finished completion.
  39. * Because a completion's fields are not safe to examine from any thread other than the one on
  40. * which the completion is currently operating, this field is used only to aid in detecting
  41. * programming errors. It can not be used for cross-thread checking on the status of an operation.
  42. * A completion must be "reset" before it can be reused after it has been finished. Resetting will
  43. * also clear any error from the result field.
  44. **/
  45. void vdo_initialize_completion(struct vdo_completion *completion,
  46. struct vdo *vdo,
  47. enum vdo_completion_type type)
  48. {
  49. memset(completion, 0, sizeof(*completion));
  50. completion->vdo = vdo;
  51. completion->type = type;
  52. vdo_reset_completion(completion);
  53. }
  54. static inline void assert_incomplete(struct vdo_completion *completion)
  55. {
  56. VDO_ASSERT_LOG_ONLY(!completion->complete, "completion is not complete");
  57. }
  58. /**
  59. * vdo_set_completion_result() - Set the result of a completion.
  60. *
  61. * Older errors will not be masked.
  62. */
  63. void vdo_set_completion_result(struct vdo_completion *completion, int result)
  64. {
  65. assert_incomplete(completion);
  66. if (completion->result == VDO_SUCCESS)
  67. completion->result = result;
  68. }
  69. /**
  70. * vdo_launch_completion_with_priority() - Run or enqueue a completion.
  71. * @priority: The priority at which to enqueue the completion.
  72. *
  73. * If called on the correct thread (i.e. the one specified in the completion's callback_thread_id
  74. * field) and not marked for requeue, the completion will be run immediately. Otherwise, the
  75. * completion will be enqueued on the specified thread.
  76. */
  77. void vdo_launch_completion_with_priority(struct vdo_completion *completion,
  78. enum vdo_completion_priority priority)
  79. {
  80. thread_id_t callback_thread = completion->callback_thread_id;
  81. if (completion->requeue || (callback_thread != vdo_get_callback_thread_id())) {
  82. vdo_enqueue_completion(completion, priority);
  83. return;
  84. }
  85. vdo_run_completion(completion);
  86. }
  87. /** vdo_finish_completion() - Mark a completion as complete and then launch it. */
  88. void vdo_finish_completion(struct vdo_completion *completion)
  89. {
  90. assert_incomplete(completion);
  91. completion->complete = true;
  92. if (completion->callback != NULL)
  93. vdo_launch_completion(completion);
  94. }
  95. void vdo_enqueue_completion(struct vdo_completion *completion,
  96. enum vdo_completion_priority priority)
  97. {
  98. struct vdo *vdo = completion->vdo;
  99. thread_id_t thread_id = completion->callback_thread_id;
  100. if (VDO_ASSERT(thread_id < vdo->thread_config.thread_count,
  101. "thread_id %u (completion type %d) is less than thread count %u",
  102. thread_id, completion->type,
  103. vdo->thread_config.thread_count) != VDO_SUCCESS)
  104. BUG();
  105. completion->requeue = false;
  106. completion->priority = priority;
  107. completion->my_queue = NULL;
  108. vdo_enqueue_work_queue(vdo->threads[thread_id].queue, completion);
  109. }
  110. /**
  111. * vdo_requeue_completion_if_needed() - Requeue a completion if not called on the specified thread.
  112. *
  113. * Return: True if the completion was requeued; callers may not access the completion in this case.
  114. */
  115. bool vdo_requeue_completion_if_needed(struct vdo_completion *completion,
  116. thread_id_t callback_thread_id)
  117. {
  118. if (vdo_get_callback_thread_id() == callback_thread_id)
  119. return false;
  120. completion->callback_thread_id = callback_thread_id;
  121. vdo_enqueue_completion(completion, VDO_WORK_Q_DEFAULT_PRIORITY);
  122. return true;
  123. }