blob: 298b21f4a4d2902cc7fa26770b89899f93e4962a [file] [log] [blame]
Jens Axboe771b53d02019-10-22 10:25:58 -06001#ifndef INTERNAL_IO_WQ_H
2#define INTERNAL_IO_WQ_H
3
4struct io_wq;
5
6enum {
7 IO_WQ_WORK_CANCEL = 1,
Jens Axboe771b53d02019-10-22 10:25:58 -06008 IO_WQ_WORK_HASHED = 4,
Jens Axboec5def4a2019-11-07 11:41:16 -07009 IO_WQ_WORK_UNBOUND = 32,
Jens Axboe0c9d5cc2019-12-11 19:29:43 -070010 IO_WQ_WORK_NO_CANCEL = 256,
Jens Axboe895e2ca2019-12-17 08:46:33 -070011 IO_WQ_WORK_CONCURRENT = 512,
Jens Axboe771b53d02019-10-22 10:25:58 -060012
13 IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */
14};
15
16enum io_wq_cancel {
17 IO_WQ_CANCEL_OK, /* cancelled before started */
18 IO_WQ_CANCEL_RUNNING, /* found, running, and attempted cancelled */
19 IO_WQ_CANCEL_NOTFOUND, /* work not found */
20};
21
Jens Axboe6206f0e2019-11-26 11:59:32 -070022struct io_wq_work_node {
23 struct io_wq_work_node *next;
24};
25
26struct io_wq_work_list {
27 struct io_wq_work_node *first;
28 struct io_wq_work_node *last;
29};
30
31static inline void wq_list_add_tail(struct io_wq_work_node *node,
32 struct io_wq_work_list *list)
33{
34 if (!list->first) {
Jens Axboee995d512019-12-07 21:06:46 -070035 list->last = node;
36 WRITE_ONCE(list->first, node);
Jens Axboe6206f0e2019-11-26 11:59:32 -070037 } else {
38 list->last->next = node;
39 list->last = node;
40 }
41}
42
43static inline void wq_node_del(struct io_wq_work_list *list,
44 struct io_wq_work_node *node,
45 struct io_wq_work_node *prev)
46{
47 if (node == list->first)
Jens Axboee995d512019-12-07 21:06:46 -070048 WRITE_ONCE(list->first, node->next);
Jens Axboe6206f0e2019-11-26 11:59:32 -070049 if (node == list->last)
50 list->last = prev;
51 if (prev)
52 prev->next = node->next;
Jens Axboe08bdcc32019-12-04 17:19:44 -070053 node->next = NULL;
Jens Axboe6206f0e2019-11-26 11:59:32 -070054}
55
56#define wq_list_for_each(pos, prv, head) \
57 for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
58
Jens Axboee995d512019-12-07 21:06:46 -070059#define wq_list_empty(list) (READ_ONCE((list)->first) == NULL)
Jens Axboe6206f0e2019-11-26 11:59:32 -070060#define INIT_WQ_LIST(list) do { \
61 (list)->first = NULL; \
62 (list)->last = NULL; \
63} while (0)
64
Jens Axboe771b53d02019-10-22 10:25:58 -060065struct io_wq_work {
Jens Axboeb76da702019-11-20 13:05:32 -070066 union {
Jens Axboe6206f0e2019-11-26 11:59:32 -070067 struct io_wq_work_node list;
Jens Axboeb76da702019-11-20 13:05:32 -070068 void *data;
69 };
Jens Axboe771b53d02019-10-22 10:25:58 -060070 void (*func)(struct io_wq_work **);
Jens Axboefcb323c2019-10-24 12:39:47 -060071 struct files_struct *files;
Jens Axboecccf0ee2020-01-27 16:34:48 -070072 struct mm_struct *mm;
73 const struct cred *creds;
Jens Axboe9392a272020-02-06 21:42:51 -070074 struct fs_struct *fs;
Jens Axboe6206f0e2019-11-26 11:59:32 -070075 unsigned flags;
Jens Axboe36282882020-02-08 19:16:39 -070076 pid_t task_pid;
Jens Axboe771b53d02019-10-22 10:25:58 -060077};
78
Jens Axboe2d141dd2020-02-25 11:52:56 -070079#define INIT_IO_WORK(work, _func) \
80 do { \
81 *(work) = (struct io_wq_work){ .func = _func }; \
82 } while (0) \
Jens Axboe771b53d02019-10-22 10:25:58 -060083
Pavel Begunkove9fd9392020-03-04 16:14:12 +030084typedef void (free_work_fn)(struct io_wq_work *);
Jens Axboe7d723062019-11-12 22:31:31 -070085
Jens Axboe576a3472019-11-25 08:49:20 -070086struct io_wq_data {
Jens Axboe576a3472019-11-25 08:49:20 -070087 struct user_struct *user;
88
Pavel Begunkove9fd9392020-03-04 16:14:12 +030089 free_work_fn *free_work;
Jens Axboe576a3472019-11-25 08:49:20 -070090};
91
92struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
Pavel Begunkoveba6f5a2020-01-28 03:15:47 +030093bool io_wq_get(struct io_wq *wq, struct io_wq_data *data);
Jens Axboe771b53d02019-10-22 10:25:58 -060094void io_wq_destroy(struct io_wq *wq);
95
96void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
Pavel Begunkov8766dd52020-03-14 00:31:04 +030097void io_wq_hash_work(struct io_wq_work *work, void *val);
98
99static inline bool io_wq_is_hashed(struct io_wq_work *work)
100{
101 return work->flags & IO_WQ_WORK_HASHED;
102}
Jens Axboe771b53d02019-10-22 10:25:58 -0600103
104void io_wq_cancel_all(struct io_wq *wq);
105enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork);
Jens Axboe36282882020-02-08 19:16:39 -0700106enum io_wq_cancel io_wq_cancel_pid(struct io_wq *wq, pid_t pid);
Jens Axboe771b53d02019-10-22 10:25:58 -0600107
Jens Axboe62755e32019-10-28 21:49:21 -0600108typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
109
110enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
111 void *data);
112
Jens Axboe771b53d02019-10-22 10:25:58 -0600113#if defined(CONFIG_IO_WQ)
114extern void io_wq_worker_sleeping(struct task_struct *);
115extern void io_wq_worker_running(struct task_struct *);
116#else
117static inline void io_wq_worker_sleeping(struct task_struct *tsk)
118{
119}
120static inline void io_wq_worker_running(struct task_struct *tsk)
121{
122}
Jens Axboe525b3052019-12-17 14:13:37 -0700123#endif
Jens Axboe771b53d02019-10-22 10:25:58 -0600124
Jens Axboe525b3052019-12-17 14:13:37 -0700125static inline bool io_wq_current_is_worker(void)
126{
127 return in_task() && (current->flags & PF_IO_WORKER);
128}
129#endif