blob: 75113bcd5889f41d9fe891aaa033db1d6154e0f1 [file] [log] [blame]
Jens Axboe771b53d02019-10-22 10:25:58 -06001#ifndef INTERNAL_IO_WQ_H
2#define INTERNAL_IO_WQ_H
3
Jens Axboe98447d62020-10-14 10:48:51 -06004#include <linux/io_uring.h>
5
Jens Axboe771b53d02019-10-22 10:25:58 -06006struct io_wq;
7
8enum {
9 IO_WQ_WORK_CANCEL = 1,
Pavel Begunkove883a792020-06-25 18:20:53 +030010 IO_WQ_WORK_HASHED = 2,
11 IO_WQ_WORK_UNBOUND = 4,
12 IO_WQ_WORK_NO_CANCEL = 8,
13 IO_WQ_WORK_CONCURRENT = 16,
Jens Axboe771b53d02019-10-22 10:25:58 -060014
Jens Axboe0f203762020-10-14 09:23:55 -060015 IO_WQ_WORK_FILES = 32,
16 IO_WQ_WORK_FS = 64,
17 IO_WQ_WORK_MM = 128,
18 IO_WQ_WORK_CREDS = 256,
19 IO_WQ_WORK_BLKCG = 512,
Jens Axboe69228332020-10-20 14:28:41 -060020 IO_WQ_WORK_FSIZE = 1024,
Jens Axboe0f203762020-10-14 09:23:55 -060021
Jens Axboe771b53d02019-10-22 10:25:58 -060022 IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */
23};
24
25enum io_wq_cancel {
26 IO_WQ_CANCEL_OK, /* cancelled before started */
27 IO_WQ_CANCEL_RUNNING, /* found, running, and attempted cancelled */
28 IO_WQ_CANCEL_NOTFOUND, /* work not found */
29};
30
Jens Axboe6206f0e2019-11-26 11:59:32 -070031struct io_wq_work_node {
32 struct io_wq_work_node *next;
33};
34
35struct io_wq_work_list {
36 struct io_wq_work_node *first;
37 struct io_wq_work_node *last;
38};
39
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030040static inline void wq_list_add_after(struct io_wq_work_node *node,
41 struct io_wq_work_node *pos,
42 struct io_wq_work_list *list)
43{
44 struct io_wq_work_node *next = pos->next;
45
46 pos->next = node;
47 node->next = next;
48 if (!next)
49 list->last = node;
50}
51
Jens Axboe6206f0e2019-11-26 11:59:32 -070052static inline void wq_list_add_tail(struct io_wq_work_node *node,
53 struct io_wq_work_list *list)
54{
55 if (!list->first) {
Jens Axboee995d512019-12-07 21:06:46 -070056 list->last = node;
57 WRITE_ONCE(list->first, node);
Jens Axboe6206f0e2019-11-26 11:59:32 -070058 } else {
59 list->last->next = node;
60 list->last = node;
61 }
Xiaoguang Wangb1442ad2020-12-18 15:26:48 +080062 node->next = NULL;
Jens Axboe6206f0e2019-11-26 11:59:32 -070063}
64
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030065static inline void wq_list_cut(struct io_wq_work_list *list,
66 struct io_wq_work_node *last,
67 struct io_wq_work_node *prev)
68{
69 /* first in the list, if prev==NULL */
70 if (!prev)
71 WRITE_ONCE(list->first, last->next);
72 else
73 prev->next = last->next;
74
75 if (last == list->last)
76 list->last = prev;
77 last->next = NULL;
78}
79
80static inline void wq_list_del(struct io_wq_work_list *list,
Jens Axboe6206f0e2019-11-26 11:59:32 -070081 struct io_wq_work_node *node,
82 struct io_wq_work_node *prev)
83{
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030084 wq_list_cut(list, node, prev);
Jens Axboe6206f0e2019-11-26 11:59:32 -070085}
86
87#define wq_list_for_each(pos, prv, head) \
88 for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
89
Jens Axboee995d512019-12-07 21:06:46 -070090#define wq_list_empty(list) (READ_ONCE((list)->first) == NULL)
Jens Axboe6206f0e2019-11-26 11:59:32 -070091#define INIT_WQ_LIST(list) do { \
92 (list)->first = NULL; \
93 (list)->last = NULL; \
94} while (0)
95
Jens Axboe771b53d02019-10-22 10:25:58 -060096struct io_wq_work {
Pavel Begunkov18a542f2020-03-23 00:23:29 +030097 struct io_wq_work_node list;
Jens Axboe98447d62020-10-14 10:48:51 -060098 struct io_identity *identity;
Jens Axboe6206f0e2019-11-26 11:59:32 -070099 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -0600100};
101
Pavel Begunkov86f3cd12020-03-23 22:57:22 +0300102static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
103{
104 if (!work->list.next)
105 return NULL;
106
107 return container_of(work->list.next, struct io_wq_work, list);
108}
109
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300110typedef void (free_work_fn)(struct io_wq_work *);
Pavel Begunkovf4db7182020-06-25 18:20:54 +0300111typedef struct io_wq_work *(io_wq_work_fn)(struct io_wq_work *);
Jens Axboe7d723062019-11-12 22:31:31 -0700112
Jens Axboe576a3472019-11-25 08:49:20 -0700113struct io_wq_data {
Jens Axboe576a3472019-11-25 08:49:20 -0700114 struct user_struct *user;
115
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300116 io_wq_work_fn *do_work;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300117 free_work_fn *free_work;
Jens Axboe576a3472019-11-25 08:49:20 -0700118};
119
120struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +0300121bool io_wq_get(struct io_wq *wq, struct io_wq_data *data);
Jens Axboe771b53d02019-10-22 10:25:58 -0600122void io_wq_destroy(struct io_wq *wq);
123
124void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300125void io_wq_hash_work(struct io_wq_work *work, void *val);
126
127static inline bool io_wq_is_hashed(struct io_wq_work *work)
128{
129 return work->flags & IO_WQ_WORK_HASHED;
130}
Jens Axboe771b53d02019-10-22 10:25:58 -0600131
132void io_wq_cancel_all(struct io_wq *wq);
Jens Axboe771b53d02019-10-22 10:25:58 -0600133
Jens Axboe62755e32019-10-28 21:49:21 -0600134typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
135
136enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300137 void *data, bool cancel_all);
Jens Axboe62755e32019-10-28 21:49:21 -0600138
Jens Axboeaa96bf82020-04-03 11:26:26 -0600139struct task_struct *io_wq_get_task(struct io_wq *wq);
140
Jens Axboe771b53d02019-10-22 10:25:58 -0600141#if defined(CONFIG_IO_WQ)
142extern void io_wq_worker_sleeping(struct task_struct *);
143extern void io_wq_worker_running(struct task_struct *);
144#else
145static inline void io_wq_worker_sleeping(struct task_struct *tsk)
146{
147}
148static inline void io_wq_worker_running(struct task_struct *tsk)
149{
150}
Jens Axboe525b3052019-12-17 14:13:37 -0700151#endif
Jens Axboe771b53d02019-10-22 10:25:58 -0600152
Jens Axboe525b3052019-12-17 14:13:37 -0700153static inline bool io_wq_current_is_worker(void)
154{
155 return in_task() && (current->flags & PF_IO_WORKER);
156}
157#endif