tw.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0
  2. #ifndef IOU_TW_H
  3. #define IOU_TW_H
  4. #include <linux/sched.h>
  5. #include <linux/percpu-refcount.h>
  6. #include <linux/io_uring_types.h>
  7. #define IO_LOCAL_TW_DEFAULT_MAX 20
  8. /*
  9. * Terminate the request if either of these conditions are true:
  10. *
  11. * 1) It's being executed by the original task, but that task is marked
  12. * with PF_EXITING as it's exiting.
  13. * 2) PF_KTHREAD is set, in which case the invoker of the task_work is
  14. * our fallback task_work.
  15. * 3) The ring has been closed and is going away.
  16. */
  17. static inline bool io_should_terminate_tw(struct io_ring_ctx *ctx)
  18. {
  19. return (current->flags & (PF_EXITING | PF_KTHREAD)) || percpu_ref_is_dying(&ctx->refs);
  20. }
  21. void io_req_task_work_add_remote(struct io_kiocb *req, unsigned flags);
  22. struct llist_node *io_handle_tw_list(struct llist_node *node, unsigned int *count, unsigned int max_entries);
  23. void tctx_task_work(struct callback_head *cb);
  24. int io_run_local_work(struct io_ring_ctx *ctx, int min_events, int max_events);
  25. int io_run_task_work_sig(struct io_ring_ctx *ctx);
  26. __cold void io_fallback_req_func(struct work_struct *work);
  27. __cold void io_move_task_work_from_local(struct io_ring_ctx *ctx);
  28. int io_run_local_work_locked(struct io_ring_ctx *ctx, int min_events);
  29. void io_req_local_work_add(struct io_kiocb *req, unsigned flags);
  30. void io_req_normal_work_add(struct io_kiocb *req);
  31. struct llist_node *tctx_task_work_run(struct io_uring_task *tctx, unsigned int max_entries, unsigned int *count);
  32. static inline void __io_req_task_work_add(struct io_kiocb *req, unsigned flags)
  33. {
  34. if (req->ctx->flags & IORING_SETUP_DEFER_TASKRUN)
  35. io_req_local_work_add(req, flags);
  36. else
  37. io_req_normal_work_add(req);
  38. }
  39. static inline void io_req_task_work_add(struct io_kiocb *req)
  40. {
  41. __io_req_task_work_add(req, 0);
  42. }
  43. static inline int io_run_task_work(void)
  44. {
  45. bool ret = false;
  46. /*
  47. * Always check-and-clear the task_work notification signal. With how
  48. * signaling works for task_work, we can find it set with nothing to
  49. * run. We need to clear it for that case, like get_signal() does.
  50. */
  51. if (test_thread_flag(TIF_NOTIFY_SIGNAL))
  52. clear_notify_signal();
  53. /*
  54. * PF_IO_WORKER never returns to userspace, so check here if we have
  55. * notify work that needs processing.
  56. */
  57. if (current->flags & PF_IO_WORKER) {
  58. if (test_thread_flag(TIF_NOTIFY_RESUME)) {
  59. __set_current_state(TASK_RUNNING);
  60. resume_user_mode_work(NULL);
  61. }
  62. if (current->io_uring) {
  63. unsigned int count = 0;
  64. __set_current_state(TASK_RUNNING);
  65. tctx_task_work_run(current->io_uring, UINT_MAX, &count);
  66. if (count)
  67. ret = true;
  68. }
  69. }
  70. if (task_work_pending(current)) {
  71. __set_current_state(TASK_RUNNING);
  72. task_work_run();
  73. ret = true;
  74. }
  75. return ret;
  76. }
  77. static inline bool io_local_work_pending(struct io_ring_ctx *ctx)
  78. {
  79. return !llist_empty(&ctx->work_llist) || !llist_empty(&ctx->retry_llist);
  80. }
  81. static inline bool io_task_work_pending(struct io_ring_ctx *ctx)
  82. {
  83. return task_work_pending(current) || io_local_work_pending(ctx);
  84. }
  85. static inline void io_tw_lock(struct io_ring_ctx *ctx, io_tw_token_t tw)
  86. {
  87. lockdep_assert_held(&ctx->uring_lock);
  88. }
  89. static inline bool io_allowed_defer_tw_run(struct io_ring_ctx *ctx)
  90. {
  91. return likely(ctx->submitter_task == current);
  92. }
  93. static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx)
  94. {
  95. return likely(!(ctx->flags & IORING_SETUP_DEFER_TASKRUN) ||
  96. ctx->submitter_task == current);
  97. }
  98. #endif