blob: 0e6d310999e899a9d416459138be22a23f02bf56 [file] [log] [blame]
Jens Axboe771b53d02019-10-22 10:25:58 -06001#ifndef INTERNAL_IO_WQ_H
2#define INTERNAL_IO_WQ_H
3
Jens Axboee9418942021-02-19 12:33:30 -07004#include <linux/refcount.h>
Jens Axboe98447d62020-10-14 10:48:51 -06005
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,
Pavel Begunkove883a792020-06-25 18:20:53 +030012 IO_WQ_WORK_CONCURRENT = 16,
Jens Axboe771b53d02019-10-22 10:25:58 -060013
14 IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */
15};
16
17enum io_wq_cancel {
18 IO_WQ_CANCEL_OK, /* cancelled before started */
19 IO_WQ_CANCEL_RUNNING, /* found, running, and attempted cancelled */
20 IO_WQ_CANCEL_NOTFOUND, /* work not found */
21};
22
Stefan Metzmacher53e043b2021-03-15 12:56:56 +010023struct io_wq_work_node {
24 struct io_wq_work_node *next;
25};
26
27struct io_wq_work_list {
28 struct io_wq_work_node *first;
29 struct io_wq_work_node *last;
30};
31
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030032static inline void wq_list_add_after(struct io_wq_work_node *node,
33 struct io_wq_work_node *pos,
34 struct io_wq_work_list *list)
35{
36 struct io_wq_work_node *next = pos->next;
37
38 pos->next = node;
39 node->next = next;
40 if (!next)
41 list->last = node;
42}
43
Jens Axboe6206f0e2019-11-26 11:59:32 -070044static inline void wq_list_add_tail(struct io_wq_work_node *node,
45 struct io_wq_work_list *list)
46{
47 if (!list->first) {
Jens Axboee995d512019-12-07 21:06:46 -070048 list->last = node;
49 WRITE_ONCE(list->first, node);
Jens Axboe6206f0e2019-11-26 11:59:32 -070050 } else {
51 list->last->next = node;
52 list->last = node;
53 }
Xiaoguang Wang0020ef02020-12-18 15:26:48 +080054 node->next = NULL;
Jens Axboe6206f0e2019-11-26 11:59:32 -070055}
56
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030057static inline void wq_list_cut(struct io_wq_work_list *list,
58 struct io_wq_work_node *last,
59 struct io_wq_work_node *prev)
60{
61 /* first in the list, if prev==NULL */
62 if (!prev)
63 WRITE_ONCE(list->first, last->next);
64 else
65 prev->next = last->next;
66
67 if (last == list->last)
68 list->last = prev;
69 last->next = NULL;
70}
71
72static inline void wq_list_del(struct io_wq_work_list *list,
Jens Axboe6206f0e2019-11-26 11:59:32 -070073 struct io_wq_work_node *node,
74 struct io_wq_work_node *prev)
75{
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030076 wq_list_cut(list, node, prev);
Jens Axboe6206f0e2019-11-26 11:59:32 -070077}
78
79#define wq_list_for_each(pos, prv, head) \
80 for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
81
Jens Axboee995d512019-12-07 21:06:46 -070082#define wq_list_empty(list) (READ_ONCE((list)->first) == NULL)
Jens Axboe6206f0e2019-11-26 11:59:32 -070083#define INIT_WQ_LIST(list) do { \
84 (list)->first = NULL; \
85 (list)->last = NULL; \
86} while (0)
87
Jens Axboe771b53d02019-10-22 10:25:58 -060088struct io_wq_work {
Pavel Begunkov18a542f2020-03-23 00:23:29 +030089 struct io_wq_work_node list;
Jens Axboe003e8dc2021-03-06 09:22:27 -070090 const struct cred *creds;
Jens Axboe6206f0e2019-11-26 11:59:32 -070091 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -060092};
93
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030094static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
95{
96 if (!work->list.next)
97 return NULL;
98
99 return container_of(work->list.next, struct io_wq_work, list);
100}
101
Pavel Begunkov5280f7e2021-02-04 13:52:08 +0000102typedef struct io_wq_work *(free_work_fn)(struct io_wq_work *);
103typedef void (io_wq_work_fn)(struct io_wq_work *);
Jens Axboe7d723062019-11-12 22:31:31 -0700104
Jens Axboee9418942021-02-19 12:33:30 -0700105struct io_wq_hash {
106 refcount_t refs;
107 unsigned long map;
108 struct wait_queue_head wait;
109};
110
111static inline void io_wq_put_hash(struct io_wq_hash *hash)
112{
113 if (refcount_dec_and_test(&hash->refs))
114 kfree(hash);
115}
116
Jens Axboe576a3472019-11-25 08:49:20 -0700117struct io_wq_data {
Jens Axboee9418942021-02-19 12:33:30 -0700118 struct io_wq_hash *hash;
Jens Axboe685fe7f2021-03-08 09:37:51 -0700119 struct task_struct *task;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300120 io_wq_work_fn *do_work;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300121 free_work_fn *free_work;
Jens Axboe576a3472019-11-25 08:49:20 -0700122};
123
124struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
Jens Axboe4fb6ac32021-02-25 10:17:09 -0700125void io_wq_put(struct io_wq *wq);
Jens Axboeafcc4012021-02-26 13:48:19 -0700126void io_wq_put_and_exit(struct io_wq *wq);
Jens Axboe771b53d02019-10-22 10:25:58 -0600127
128void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300129void io_wq_hash_work(struct io_wq_work *work, void *val);
130
131static inline bool io_wq_is_hashed(struct io_wq_work *work)
132{
133 return work->flags & IO_WQ_WORK_HASHED;
134}
Jens Axboe771b53d02019-10-22 10:25:58 -0600135
Jens Axboe62755e32019-10-28 21:49:21 -0600136typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
137
138enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300139 void *data, bool cancel_all);
Jens Axboe62755e32019-10-28 21:49:21 -0600140
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{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700155 return in_task() && (current->flags & PF_IO_WORKER) &&
156 current->pf_io_worker;
Jens Axboe525b3052019-12-17 14:13:37 -0700157}
158#endif