1 #ifndef INTERNAL_IO_WQ_H 2 #define INTERNAL_IO_WQ_H 3 4 #include <linux/refcount.h> 5 #include <linux/io_uring_types.h> 6 7 struct io_wq; 8 9 enum { 10 IO_WQ_WORK_CANCEL = 1, 11 IO_WQ_WORK_HASHED = 2, 12 IO_WQ_WORK_UNBOUND = 4, 13 IO_WQ_WORK_CONCURRENT = 16, 14 15 IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */ 16 }; 17 18 enum io_wq_cancel { 19 IO_WQ_CANCEL_OK, /* cancelled before started */ 20 IO_WQ_CANCEL_RUNNING, /* found, running, and attempted cancelled */ 21 IO_WQ_CANCEL_NOTFOUND, /* work not found */ 22 }; 23 24 #define wq_list_for_each(pos, prv, head) \ 25 for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next) 26 27 #define wq_list_for_each_resume(pos, prv) \ 28 for (; pos; prv = pos, pos = (pos)->next) 29 30 #define wq_list_empty(list) (READ_ONCE((list)->first) == NULL) 31 #define INIT_WQ_LIST(list) do { \ 32 (list)->first = NULL; \ 33 } while (0) 34 35 static inline void wq_list_add_after(struct io_wq_work_node *node, 36 struct io_wq_work_node *pos, 37 struct io_wq_work_list *list) 38 { 39 struct io_wq_work_node *next = pos->next; 40 41 pos->next = node; 42 node->next = next; 43 if (!next) 44 list->last = node; 45 } 46 47 /** 48 * wq_list_merge - merge the second list to the first one. 49 * @list0: the first list 50 * @list1: the second list 51 * Return the first node after mergence. 52 */ 53 static inline struct io_wq_work_node *wq_list_merge(struct io_wq_work_list *list0, 54 struct io_wq_work_list *list1) 55 { 56 struct io_wq_work_node *ret; 57 58 if (!list0->first) { 59 ret = list1->first; 60 } else { 61 ret = list0->first; 62 list0->last->next = list1->first; 63 } 64 INIT_WQ_LIST(list0); 65 INIT_WQ_LIST(list1); 66 return ret; 67 } 68 69 static inline void wq_list_add_tail(struct io_wq_work_node *node, 70 struct io_wq_work_list *list) 71 { 72 node->next = NULL; 73 if (!list->first) { 74 list->last = node; 75 WRITE_ONCE(list->first, node); 76 } else { 77 list->last->next = node; 78 list->last = node; 79 } 80 } 81 82 static inline void wq_list_add_head(struct io_wq_work_node *node, 83 struct io_wq_work_list *list) 84 { 85 node->next = list->first; 86 if (!node->next) 87 list->last = node; 88 WRITE_ONCE(list->first, node); 89 } 90 91 static inline void wq_list_cut(struct io_wq_work_list *list, 92 struct io_wq_work_node *last, 93 struct io_wq_work_node *prev) 94 { 95 /* first in the list, if prev==NULL */ 96 if (!prev) 97 WRITE_ONCE(list->first, last->next); 98 else 99 prev->next = last->next; 100 101 if (last == list->last) 102 list->last = prev; 103 last->next = NULL; 104 } 105 106 static inline void __wq_list_splice(struct io_wq_work_list *list, 107 struct io_wq_work_node *to) 108 { 109 list->last->next = to->next; 110 to->next = list->first; 111 INIT_WQ_LIST(list); 112 } 113 114 static inline bool wq_list_splice(struct io_wq_work_list *list, 115 struct io_wq_work_node *to) 116 { 117 if (!wq_list_empty(list)) { 118 __wq_list_splice(list, to); 119 return true; 120 } 121 return false; 122 } 123 124 static inline void wq_stack_add_head(struct io_wq_work_node *node, 125 struct io_wq_work_node *stack) 126 { 127 node->next = stack->next; 128 stack->next = node; 129 } 130 131 static inline void wq_list_del(struct io_wq_work_list *list, 132 struct io_wq_work_node *node, 133 struct io_wq_work_node *prev) 134 { 135 wq_list_cut(list, node, prev); 136 } 137 138 static inline 139 struct io_wq_work_node *wq_stack_extract(struct io_wq_work_node *stack) 140 { 141 struct io_wq_work_node *node = stack->next; 142 143 stack->next = node->next; 144 return node; 145 } 146 147 static inline struct io_wq_work *wq_next_work(struct io_wq_work *work) 148 { 149 if (!work->list.next) 150 return NULL; 151 152 return container_of(work->list.next, struct io_wq_work, list); 153 } 154 155 typedef struct io_wq_work *(free_work_fn)(struct io_wq_work *); 156 typedef void (io_wq_work_fn)(struct io_wq_work *); 157 158 struct io_wq_hash { 159 refcount_t refs; 160 unsigned long map; 161 struct wait_queue_head wait; 162 }; 163 164 static inline void io_wq_put_hash(struct io_wq_hash *hash) 165 { 166 if (refcount_dec_and_test(&hash->refs)) 167 kfree(hash); 168 } 169 170 struct io_wq_data { 171 struct io_wq_hash *hash; 172 struct task_struct *task; 173 io_wq_work_fn *do_work; 174 free_work_fn *free_work; 175 }; 176 177 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data); 178 void io_wq_exit_start(struct io_wq *wq); 179 void io_wq_put_and_exit(struct io_wq *wq); 180 181 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work); 182 void io_wq_hash_work(struct io_wq_work *work, void *val); 183 184 int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask); 185 int io_wq_max_workers(struct io_wq *wq, int *new_count); 186 187 static inline bool io_wq_is_hashed(struct io_wq_work *work) 188 { 189 return work->flags & IO_WQ_WORK_HASHED; 190 } 191 192 typedef bool (work_cancel_fn)(struct io_wq_work *, void *); 193 194 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, 195 void *data, bool cancel_all); 196 197 #if defined(CONFIG_IO_WQ) 198 extern void io_wq_worker_sleeping(struct task_struct *); 199 extern void io_wq_worker_running(struct task_struct *); 200 #else 201 static inline void io_wq_worker_sleeping(struct task_struct *tsk) 202 { 203 } 204 static inline void io_wq_worker_running(struct task_struct *tsk) 205 { 206 } 207 #endif 208 209 static inline bool io_wq_current_is_worker(void) 210 { 211 return in_task() && (current->flags & PF_IO_WORKER) && 212 current->worker_private; 213 } 214 #endif 215