io-wq.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef INTERNAL_IO_WQ_H
  2. #define INTERNAL_IO_WQ_H
  3. #include <linux/io_uring.h>
  4. struct io_wq;
  5. enum {
  6. IO_WQ_WORK_CANCEL = 1,
  7. IO_WQ_WORK_HASHED = 2,
  8. IO_WQ_WORK_UNBOUND = 4,
  9. IO_WQ_WORK_NO_CANCEL = 8,
  10. IO_WQ_WORK_CONCURRENT = 16,
  11. IO_WQ_WORK_FILES = 32,
  12. IO_WQ_WORK_FS = 64,
  13. IO_WQ_WORK_MM = 128,
  14. IO_WQ_WORK_CREDS = 256,
  15. IO_WQ_WORK_BLKCG = 512,
  16. IO_WQ_WORK_FSIZE = 1024,
  17. IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */
  18. };
  19. enum io_wq_cancel {
  20. IO_WQ_CANCEL_OK, /* cancelled before started */
  21. IO_WQ_CANCEL_RUNNING, /* found, running, and attempted cancelled */
  22. IO_WQ_CANCEL_NOTFOUND, /* work not found */
  23. };
  24. struct io_wq_work_node {
  25. struct io_wq_work_node *next;
  26. };
  27. struct io_wq_work_list {
  28. struct io_wq_work_node *first;
  29. struct io_wq_work_node *last;
  30. };
  31. static inline void wq_list_add_after(struct io_wq_work_node *node,
  32. struct io_wq_work_node *pos,
  33. struct io_wq_work_list *list)
  34. {
  35. struct io_wq_work_node *next = pos->next;
  36. pos->next = node;
  37. node->next = next;
  38. if (!next)
  39. list->last = node;
  40. }
  41. static inline void wq_list_add_tail(struct io_wq_work_node *node,
  42. struct io_wq_work_list *list)
  43. {
  44. if (!list->first) {
  45. list->last = node;
  46. WRITE_ONCE(list->first, node);
  47. } else {
  48. list->last->next = node;
  49. list->last = node;
  50. }
  51. node->next = NULL;
  52. }
  53. static inline void wq_list_cut(struct io_wq_work_list *list,
  54. struct io_wq_work_node *last,
  55. struct io_wq_work_node *prev)
  56. {
  57. /* first in the list, if prev==NULL */
  58. if (!prev)
  59. WRITE_ONCE(list->first, last->next);
  60. else
  61. prev->next = last->next;
  62. if (last == list->last)
  63. list->last = prev;
  64. last->next = NULL;
  65. }
  66. static inline void wq_list_del(struct io_wq_work_list *list,
  67. struct io_wq_work_node *node,
  68. struct io_wq_work_node *prev)
  69. {
  70. wq_list_cut(list, node, prev);
  71. }
  72. #define wq_list_for_each(pos, prv, head) \
  73. for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
  74. #define wq_list_empty(list) (READ_ONCE((list)->first) == NULL)
  75. #define INIT_WQ_LIST(list) do { \
  76. (list)->first = NULL; \
  77. (list)->last = NULL; \
  78. } while (0)
  79. struct io_wq_work {
  80. struct io_wq_work_node list;
  81. struct io_identity *identity;
  82. unsigned flags;
  83. };
  84. static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
  85. {
  86. if (!work->list.next)
  87. return NULL;
  88. return container_of(work->list.next, struct io_wq_work, list);
  89. }
  90. typedef void (free_work_fn)(struct io_wq_work *);
  91. typedef struct io_wq_work *(io_wq_work_fn)(struct io_wq_work *);
  92. struct io_wq_data {
  93. struct user_struct *user;
  94. io_wq_work_fn *do_work;
  95. free_work_fn *free_work;
  96. };
  97. struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
  98. bool io_wq_get(struct io_wq *wq, struct io_wq_data *data);
  99. void io_wq_destroy(struct io_wq *wq);
  100. void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
  101. void io_wq_hash_work(struct io_wq_work *work, void *val);
  102. static inline bool io_wq_is_hashed(struct io_wq_work *work)
  103. {
  104. return work->flags & IO_WQ_WORK_HASHED;
  105. }
  106. void io_wq_cancel_all(struct io_wq *wq);
  107. typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
  108. enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
  109. void *data, bool cancel_all);
  110. struct task_struct *io_wq_get_task(struct io_wq *wq);
  111. #if defined(CONFIG_IO_WQ)
  112. extern void io_wq_worker_sleeping(struct task_struct *);
  113. extern void io_wq_worker_running(struct task_struct *);
  114. #else
  115. static inline void io_wq_worker_sleeping(struct task_struct *tsk)
  116. {
  117. }
  118. static inline void io_wq_worker_running(struct task_struct *tsk)
  119. {
  120. }
  121. #endif
  122. static inline bool io_wq_current_is_worker(void)
  123. {
  124. return in_task() && (current->flags & PF_IO_WORKER);
  125. }
  126. #endif