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, |
| 10 | IO_WQ_WORK_NEEDS_USER = 8, |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 11 | IO_WQ_WORK_NEEDS_FILES = 16, |
Jens Axboe | c5def4a | 2019-11-07 11:41:16 -0700 | [diff] [blame] | 12 | IO_WQ_WORK_UNBOUND = 32, |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 13 | IO_WQ_WORK_INTERNAL = 64, |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 14 | IO_WQ_WORK_CB = 128, |
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) { |
| 38 | list->first = list->last = node; |
| 39 | } else { |
| 40 | list->last->next = node; |
| 41 | list->last = node; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | static inline void wq_node_del(struct io_wq_work_list *list, |
| 46 | struct io_wq_work_node *node, |
| 47 | struct io_wq_work_node *prev) |
| 48 | { |
| 49 | if (node == list->first) |
| 50 | list->first = node->next; |
| 51 | if (node == list->last) |
| 52 | list->last = prev; |
| 53 | if (prev) |
| 54 | prev->next = node->next; |
| 55 | } |
| 56 | |
| 57 | #define wq_list_for_each(pos, prv, head) \ |
| 58 | for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next) |
| 59 | |
| 60 | #define wq_list_empty(list) ((list)->first == NULL) |
| 61 | #define INIT_WQ_LIST(list) do { \ |
| 62 | (list)->first = NULL; \ |
| 63 | (list)->last = NULL; \ |
| 64 | } while (0) |
| 65 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 66 | struct io_wq_work { |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 67 | union { |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame^] | 68 | struct io_wq_work_node list; |
Jens Axboe | b76da70 | 2019-11-20 13:05:32 -0700 | [diff] [blame] | 69 | void *data; |
| 70 | }; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 71 | void (*func)(struct io_wq_work **); |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 72 | struct files_struct *files; |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame^] | 73 | unsigned flags; |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | #define INIT_IO_WORK(work, _func) \ |
| 77 | do { \ |
Jens Axboe | 6206f0e | 2019-11-26 11:59:32 -0700 | [diff] [blame^] | 78 | (work)->list.next = NULL; \ |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 79 | (work)->func = _func; \ |
| 80 | (work)->flags = 0; \ |
Jens Axboe | fcb323c | 2019-10-24 12:39:47 -0600 | [diff] [blame] | 81 | (work)->files = NULL; \ |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 82 | } while (0) \ |
| 83 | |
Jens Axboe | 7d72306 | 2019-11-12 22:31:31 -0700 | [diff] [blame] | 84 | typedef void (get_work_fn)(struct io_wq_work *); |
| 85 | typedef void (put_work_fn)(struct io_wq_work *); |
| 86 | |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 87 | struct io_wq_data { |
| 88 | struct mm_struct *mm; |
| 89 | struct user_struct *user; |
Jens Axboe | 181e448 | 2019-11-25 08:52:30 -0700 | [diff] [blame] | 90 | struct cred *creds; |
Jens Axboe | 576a347 | 2019-11-25 08:49:20 -0700 | [diff] [blame] | 91 | |
| 92 | get_work_fn *get_work; |
| 93 | put_work_fn *put_work; |
| 94 | }; |
| 95 | |
| 96 | struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data); |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 97 | void io_wq_destroy(struct io_wq *wq); |
| 98 | |
| 99 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work); |
| 100 | void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val); |
| 101 | void io_wq_flush(struct io_wq *wq); |
| 102 | |
| 103 | void io_wq_cancel_all(struct io_wq *wq); |
| 104 | enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork); |
| 105 | |
Jens Axboe | 62755e3 | 2019-10-28 21:49:21 -0600 | [diff] [blame] | 106 | typedef bool (work_cancel_fn)(struct io_wq_work *, void *); |
| 107 | |
| 108 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
| 109 | void *data); |
| 110 | |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 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 | |
Jens Axboe | 960e432 | 2019-11-12 07:56:39 -0700 | [diff] [blame] | 123 | static inline bool io_wq_current_is_worker(void) |
| 124 | { |
| 125 | return in_task() && (current->flags & PF_IO_WORKER); |
| 126 | } |
Jens Axboe | 771b53d0 | 2019-10-22 10:25:58 -0600 | [diff] [blame] | 127 | #endif |