blob: 3677b39db015055e827a62e623e0fa6b10677142 [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#include <linux/io_uring.h>
6
Jens Axboe771b53d02019-10-22 10:25:58 -06007struct io_wq;
8
9enum {
10 IO_WQ_WORK_CANCEL = 1,
Pavel Begunkove883a792020-06-25 18:20:53 +030011 IO_WQ_WORK_HASHED = 2,
12 IO_WQ_WORK_UNBOUND = 4,
Pavel Begunkove883a792020-06-25 18:20:53 +030013 IO_WQ_WORK_CONCURRENT = 16,
Jens Axboe771b53d02019-10-22 10:25:58 -060014
15 IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */
16};
17
18enum io_wq_cancel {
19 IO_WQ_CANCEL_OK, /* cancelled before started */
20 IO_WQ_CANCEL_RUNNING, /* found, running, and attempted cancelled */
21 IO_WQ_CANCEL_NOTFOUND, /* work not found */
22};
23
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030024static inline void wq_list_add_after(struct io_wq_work_node *node,
25 struct io_wq_work_node *pos,
26 struct io_wq_work_list *list)
27{
28 struct io_wq_work_node *next = pos->next;
29
30 pos->next = node;
31 node->next = next;
32 if (!next)
33 list->last = node;
34}
35
Jens Axboe6206f0e2019-11-26 11:59:32 -070036static inline void wq_list_add_tail(struct io_wq_work_node *node,
37 struct io_wq_work_list *list)
38{
39 if (!list->first) {
Jens Axboee995d512019-12-07 21:06:46 -070040 list->last = node;
41 WRITE_ONCE(list->first, node);
Jens Axboe6206f0e2019-11-26 11:59:32 -070042 } else {
43 list->last->next = node;
44 list->last = node;
45 }
Xiaoguang Wang0020ef02020-12-18 15:26:48 +080046 node->next = NULL;
Jens Axboe6206f0e2019-11-26 11:59:32 -070047}
48
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030049static inline void wq_list_cut(struct io_wq_work_list *list,
50 struct io_wq_work_node *last,
51 struct io_wq_work_node *prev)
52{
53 /* first in the list, if prev==NULL */
54 if (!prev)
55 WRITE_ONCE(list->first, last->next);
56 else
57 prev->next = last->next;
58
59 if (last == list->last)
60 list->last = prev;
61 last->next = NULL;
62}
63
64static inline void wq_list_del(struct io_wq_work_list *list,
Jens Axboe6206f0e2019-11-26 11:59:32 -070065 struct io_wq_work_node *node,
66 struct io_wq_work_node *prev)
67{
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030068 wq_list_cut(list, node, prev);
Jens Axboe6206f0e2019-11-26 11:59:32 -070069}
70
71#define wq_list_for_each(pos, prv, head) \
72 for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
73
Jens Axboee995d512019-12-07 21:06:46 -070074#define wq_list_empty(list) (READ_ONCE((list)->first) == NULL)
Jens Axboe6206f0e2019-11-26 11:59:32 -070075#define INIT_WQ_LIST(list) do { \
76 (list)->first = NULL; \
77 (list)->last = NULL; \
78} while (0)
79
Jens Axboe771b53d02019-10-22 10:25:58 -060080struct io_wq_work {
Pavel Begunkov18a542f2020-03-23 00:23:29 +030081 struct io_wq_work_node list;
Jens Axboe4379bf82021-02-15 13:40:22 -070082 const struct cred *creds;
Jens Axboe6206f0e2019-11-26 11:59:32 -070083 unsigned flags;
Jens Axboe771b53d02019-10-22 10:25:58 -060084};
85
Pavel Begunkov86f3cd12020-03-23 22:57:22 +030086static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
87{
88 if (!work->list.next)
89 return NULL;
90
91 return container_of(work->list.next, struct io_wq_work, list);
92}
93
Pavel Begunkov5280f7e2021-02-04 13:52:08 +000094typedef struct io_wq_work *(free_work_fn)(struct io_wq_work *);
95typedef void (io_wq_work_fn)(struct io_wq_work *);
Jens Axboe7d723062019-11-12 22:31:31 -070096
Jens Axboee9418942021-02-19 12:33:30 -070097struct io_wq_hash {
98 refcount_t refs;
99 unsigned long map;
100 struct wait_queue_head wait;
101};
102
103static inline void io_wq_put_hash(struct io_wq_hash *hash)
104{
105 if (refcount_dec_and_test(&hash->refs))
106 kfree(hash);
107}
108
Jens Axboe576a3472019-11-25 08:49:20 -0700109struct io_wq_data {
Jens Axboee9418942021-02-19 12:33:30 -0700110 struct io_wq_hash *hash;
Pavel Begunkovf5fa38c2020-06-08 21:08:20 +0300111 io_wq_work_fn *do_work;
Pavel Begunkove9fd9392020-03-04 16:14:12 +0300112 free_work_fn *free_work;
Jens Axboe576a3472019-11-25 08:49:20 -0700113};
114
115struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
Jens Axboe771b53d02019-10-22 10:25:58 -0600116void io_wq_destroy(struct io_wq *wq);
117
118void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300119void io_wq_hash_work(struct io_wq_work *work, void *val);
120
Jens Axboe843bbfd2021-02-17 21:05:41 -0700121pid_t io_wq_fork_thread(int (*fn)(void *), void *arg);
122
Pavel Begunkov8766dd52020-03-14 00:31:04 +0300123static inline bool io_wq_is_hashed(struct io_wq_work *work)
124{
125 return work->flags & IO_WQ_WORK_HASHED;
126}
Jens Axboe771b53d02019-10-22 10:25:58 -0600127
Jens Axboe62755e32019-10-28 21:49:21 -0600128typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
129
130enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
Pavel Begunkov4f26bda2020-06-15 10:24:03 +0300131 void *data, bool cancel_all);
Jens Axboe62755e32019-10-28 21:49:21 -0600132
Jens Axboe771b53d02019-10-22 10:25:58 -0600133#if defined(CONFIG_IO_WQ)
134extern void io_wq_worker_sleeping(struct task_struct *);
135extern void io_wq_worker_running(struct task_struct *);
136#else
137static inline void io_wq_worker_sleeping(struct task_struct *tsk)
138{
139}
140static inline void io_wq_worker_running(struct task_struct *tsk)
141{
142}
Jens Axboe525b3052019-12-17 14:13:37 -0700143#endif
Jens Axboe771b53d02019-10-22 10:25:58 -0600144
Jens Axboe525b3052019-12-17 14:13:37 -0700145static inline bool io_wq_current_is_worker(void)
146{
Jens Axboe3bfe6102021-02-16 14:15:30 -0700147 return in_task() && (current->flags & PF_IO_WORKER) &&
148 current->pf_io_worker;
Jens Axboe525b3052019-12-17 14:13:37 -0700149}
150#endif