io_uring: fix race condition in task_work add and clear
[linux-2.6-microblaze.git] / fs / io-wq.h
1 #ifndef INTERNAL_IO_WQ_H
2 #define INTERNAL_IO_WQ_H
3
4 #include <linux/refcount.h>
5 #include <linux/io_uring.h>
6
7 struct io_wq;
8
9 enum {
10         IO_WQ_WORK_CANCEL       = 1,
11         IO_WQ_WORK_HASHED       = 2,
12         IO_WQ_WORK_UNBOUND      = 4,
13         IO_WQ_WORK_CONCURRENT   = 16,
14
15         IO_WQ_HASH_SHIFT        = 24,   /* upper 8 bits are used for hash key */
16 };
17
18 enum 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
24 static 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
36 static inline void wq_list_add_tail(struct io_wq_work_node *node,
37                                     struct io_wq_work_list *list)
38 {
39         if (!list->first) {
40                 list->last = node;
41                 WRITE_ONCE(list->first, node);
42         } else {
43                 list->last->next = node;
44                 list->last = node;
45         }
46         node->next = NULL;
47 }
48
49 static 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
64 static inline void wq_list_del(struct io_wq_work_list *list,
65                                struct io_wq_work_node *node,
66                                struct io_wq_work_node *prev)
67 {
68         wq_list_cut(list, node, prev);
69 }
70
71 #define wq_list_for_each(pos, prv, head)                        \
72         for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
73
74 #define wq_list_empty(list)     (READ_ONCE((list)->first) == NULL)
75 #define INIT_WQ_LIST(list)      do {                            \
76         (list)->first = NULL;                                   \
77         (list)->last = NULL;                                    \
78 } while (0)
79
80 struct io_wq_work {
81         struct io_wq_work_node list;
82         const struct cred *creds;
83         unsigned flags;
84 };
85
86 static 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
94 typedef struct io_wq_work *(free_work_fn)(struct io_wq_work *);
95 typedef void (io_wq_work_fn)(struct io_wq_work *);
96
97 struct io_wq_hash {
98         refcount_t refs;
99         unsigned long map;
100         struct wait_queue_head wait;
101 };
102
103 static 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
109 struct io_wq_data {
110         struct io_wq_hash *hash;
111         io_wq_work_fn *do_work;
112         free_work_fn *free_work;
113 };
114
115 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
116 void io_wq_put(struct io_wq *wq);
117 void io_wq_put_and_exit(struct io_wq *wq);
118
119 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
120 void io_wq_hash_work(struct io_wq_work *work, void *val);
121
122 pid_t io_wq_fork_thread(int (*fn)(void *), void *arg);
123
124 static inline bool io_wq_is_hashed(struct io_wq_work *work)
125 {
126         return work->flags & IO_WQ_WORK_HASHED;
127 }
128
129 typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
130
131 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
132                                         void *data, bool cancel_all);
133
134 #if defined(CONFIG_IO_WQ)
135 extern void io_wq_worker_sleeping(struct task_struct *);
136 extern void io_wq_worker_running(struct task_struct *);
137 #else
138 static inline void io_wq_worker_sleeping(struct task_struct *tsk)
139 {
140 }
141 static inline void io_wq_worker_running(struct task_struct *tsk)
142 {
143 }
144 #endif
145
146 static inline bool io_wq_current_is_worker(void)
147 {
148         return in_task() && (current->flags & PF_IO_WORKER) &&
149                 current->pf_io_worker;
150 }
151 #endif