Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 1 | #ifndef INTERNAL_IO_WQ_H |
| 2 | #define INTERNAL_IO_WQ_H |
| 3 | |
| 4 | struct io_wq; |
| 5 | |
| 6 | enum { |
| 7 | IO_WQ_WORK_CANCEL = 1, |
| 8 | IO_WQ_WORK_HAS_MM = 2, |
| 9 | IO_WQ_WORK_HASHED = 4, |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 10 | IO_WQ_WORK_UNBOUND = 32, |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 11 | IO_WQ_WORK_INTERNAL = 64, |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 12 | IO_WQ_WORK_CB = 128, |
Jens Axboe | 0c9d5cc | 2019-12-11 19:29:43 -0700 | [diff] [blame] | 13 | IO_WQ_WORK_NO_CANCEL = 256, |
Jens Axboe | 895e2ca | 2019-12-17 08:46:33 -0700 | [diff] [blame] | 14 | IO_WQ_WORK_CONCURRENT = 512, |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 15 | |
| 16 | IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */ |
| 17 | }; |
| 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 | |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 25 | struct io_wq_work_node { |
| 26 | struct io_wq_work_node *next; |
| 27 | }; |
| 28 | |
| 29 | struct io_wq_work_list { |
| 30 | struct io_wq_work_node *first; |
| 31 | struct io_wq_work_node *last; |
| 32 | }; |
| 33 | |
| 34 | static inline void wq_list_add_tail(struct io_wq_work_node *node, |
| 35 | struct io_wq_work_list *list) |
| 36 | { |
| 37 | if (!list->first) { |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 38 | list->last = node; |
| 39 | WRITE_ONCE(list->first, node); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 40 | } else { |
| 41 | list->last->next = node; |
| 42 | list->last = node; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | static inline void wq_node_del(struct io_wq_work_list *list, |
| 47 | struct io_wq_work_node *node, |
| 48 | struct io_wq_work_node *prev) |
| 49 | { |
| 50 | if (node == list->first) |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 51 | WRITE_ONCE(list->first, node->next); |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 52 | if (node == list->last) |
| 53 | list->last = prev; |
| 54 | if (prev) |
| 55 | prev->next = node->next; |
Jens Axboe | 08bdcc3 | 2019-12-04 17:19:44 -0700 | [diff] [blame] | 56 | node->next = NULL; |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | #define wq_list_for_each(pos, prv, head) \ |
| 60 | for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next) |
| 61 | |
Jens Axboe | e995d51 | 2019-12-07 21:06:46 -0700 | [diff] [blame] | 62 | #define wq_list_empty(list) (READ_ONCE((list)->first) == NULL) |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 63 | #define INIT_WQ_LIST(list) do { \ |
| 64 | (list)->first = NULL; \ |
| 65 | (list)->last = NULL; \ |
| 66 | } while (0) |
| 67 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 68 | struct io_wq_work { |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 69 | union { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 70 | struct io_wq_work_node list; |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 71 | void *data; |
| 72 | }; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 73 | void (*func)(struct io_wq_work **); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 74 | struct files_struct *files; |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 75 | struct mm_struct *mm; |
| 76 | const struct cred *creds; |
Jens Axboe | 9392a27 | 2020-02-06 21:42:51 -0700 | [diff] [blame] | 77 | struct fs_struct *fs; |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 78 | unsigned flags; |
Jens Axboe | 3628288 | 2020-02-08 19:16:39 -0700 | [diff] [blame] | 79 | pid_t task_pid; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | #define INIT_IO_WORK(work, _func) \ |
| 83 | do { \ |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame] | 84 | (work)->list.next = NULL; \ |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 85 | (work)->func = _func; \ |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 86 | (work)->files = NULL; \ |
Jens Axboe | cccf0ee | 2020-01-27 16:34:48 -0700 | [diff] [blame] | 87 | (work)->mm = NULL; \ |
| 88 | (work)->creds = NULL; \ |
Jens Axboe | 9392a27 | 2020-02-06 21:42:51 -0700 | [diff] [blame] | 89 | (work)->fs = NULL; \ |
| 90 | (work)->flags = 0; \ |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 91 | } while (0) \ |
| 92 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 93 | typedef void (get_work_fn)(struct io_wq_work *); |
| 94 | typedef void (put_work_fn)(struct io_wq_work *); |
| 95 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 96 | struct io_wq_data { |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 97 | struct user_struct *user; |
| 98 | |
| 99 | get_work_fn *get_work; |
| 100 | put_work_fn *put_work; |
| 101 | }; |
| 102 | |
| 103 | struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data); |
Pavel Begunkov | eba6f5a | 2020-01-28 03:15:47 +0300 | [diff] [blame] | 104 | bool io_wq_get(struct io_wq *wq, struct io_wq_data *data); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 105 | void io_wq_destroy(struct io_wq *wq); |
| 106 | |
| 107 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work); |
| 108 | void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val); |
| 109 | void io_wq_flush(struct io_wq *wq); |
| 110 | |
| 111 | void io_wq_cancel_all(struct io_wq *wq); |
| 112 | enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork); |
Jens Axboe | 3628288 | 2020-02-08 19:16:39 -0700 | [diff] [blame] | 113 | enum io_wq_cancel io_wq_cancel_pid(struct io_wq *wq, pid_t pid); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 114 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 115 | typedef bool (work_cancel_fn)(struct io_wq_work *, void *); |
| 116 | |
| 117 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
| 118 | void *data); |
| 119 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 120 | #if defined(CONFIG_IO_WQ) |
| 121 | extern void io_wq_worker_sleeping(struct task_struct *); |
| 122 | extern void io_wq_worker_running(struct task_struct *); |
| 123 | #else |
| 124 | static inline void io_wq_worker_sleeping(struct task_struct *tsk) |
| 125 | { |
| 126 | } |
| 127 | static inline void io_wq_worker_running(struct task_struct *tsk) |
| 128 | { |
| 129 | } |
Jens Axboe | 525b305 | 2019-12-17 14:13:37 -0700 | [diff] [blame] | 130 | #endif |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 131 | |
Jens Axboe | 525b305 | 2019-12-17 14:13:37 -0700 | [diff] [blame] | 132 | static inline bool io_wq_current_is_worker(void) |
| 133 | { |
| 134 | return in_task() && (current->flags & PF_IO_WORKER); |
| 135 | } |
| 136 | #endif |