]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/io-wq.h
io_uring/io-wq: don't use static creds/mm assignments
[linux.git] / fs / io-wq.h
1 #ifndef INTERNAL_IO_WQ_H
2 #define INTERNAL_IO_WQ_H
3
4 struct io_wq;
5
6 enum {
7         IO_WQ_WORK_CANCEL       = 1,
8         IO_WQ_WORK_HAS_MM       = 2,
9         IO_WQ_WORK_HASHED       = 4,
10         IO_WQ_WORK_NEEDS_FILES  = 16,
11         IO_WQ_WORK_UNBOUND      = 32,
12         IO_WQ_WORK_INTERNAL     = 64,
13         IO_WQ_WORK_CB           = 128,
14         IO_WQ_WORK_NO_CANCEL    = 256,
15         IO_WQ_WORK_CONCURRENT   = 512,
16
17         IO_WQ_HASH_SHIFT        = 24,   /* upper 8 bits are used for hash key */
18 };
19
20 enum io_wq_cancel {
21         IO_WQ_CANCEL_OK,        /* cancelled before started */
22         IO_WQ_CANCEL_RUNNING,   /* found, running, and attempted cancelled */
23         IO_WQ_CANCEL_NOTFOUND,  /* work not found */
24 };
25
26 struct io_wq_work_node {
27         struct io_wq_work_node *next;
28 };
29
30 struct io_wq_work_list {
31         struct io_wq_work_node *first;
32         struct io_wq_work_node *last;
33 };
34
35 static inline void wq_list_add_tail(struct io_wq_work_node *node,
36                                     struct io_wq_work_list *list)
37 {
38         if (!list->first) {
39                 list->last = node;
40                 WRITE_ONCE(list->first, node);
41         } else {
42                 list->last->next = node;
43                 list->last = node;
44         }
45 }
46
47 static inline void wq_node_del(struct io_wq_work_list *list,
48                                struct io_wq_work_node *node,
49                                struct io_wq_work_node *prev)
50 {
51         if (node == list->first)
52                 WRITE_ONCE(list->first, node->next);
53         if (node == list->last)
54                 list->last = prev;
55         if (prev)
56                 prev->next = node->next;
57         node->next = NULL;
58 }
59
60 #define wq_list_for_each(pos, prv, head)                        \
61         for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
62
63 #define wq_list_empty(list)     (READ_ONCE((list)->first) == NULL)
64 #define INIT_WQ_LIST(list)      do {                            \
65         (list)->first = NULL;                                   \
66         (list)->last = NULL;                                    \
67 } while (0)
68
69 struct io_wq_work {
70         union {
71                 struct io_wq_work_node list;
72                 void *data;
73         };
74         void (*func)(struct io_wq_work **);
75         struct files_struct *files;
76         struct mm_struct *mm;
77         const struct cred *creds;
78         unsigned flags;
79 };
80
81 #define INIT_IO_WORK(work, _func)                       \
82         do {                                            \
83                 (work)->list.next = NULL;               \
84                 (work)->func = _func;                   \
85                 (work)->flags = 0;                      \
86                 (work)->files = NULL;                   \
87                 (work)->mm = NULL;                      \
88                 (work)->creds = NULL;                   \
89         } while (0)                                     \
90
91 typedef void (get_work_fn)(struct io_wq_work *);
92 typedef void (put_work_fn)(struct io_wq_work *);
93
94 struct io_wq_data {
95         struct user_struct *user;
96
97         get_work_fn *get_work;
98         put_work_fn *put_work;
99 };
100
101 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
102 void io_wq_destroy(struct io_wq *wq);
103
104 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
105 void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val);
106 void io_wq_flush(struct io_wq *wq);
107
108 void io_wq_cancel_all(struct io_wq *wq);
109 enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork);
110
111 typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
112
113 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
114                                         void *data);
115
116 #if defined(CONFIG_IO_WQ)
117 extern void io_wq_worker_sleeping(struct task_struct *);
118 extern void io_wq_worker_running(struct task_struct *);
119 #else
120 static inline void io_wq_worker_sleeping(struct task_struct *tsk)
121 {
122 }
123 static inline void io_wq_worker_running(struct task_struct *tsk)
124 {
125 }
126 #endif
127
128 static inline bool io_wq_current_is_worker(void)
129 {
130         return in_task() && (current->flags & PF_IO_WORKER);
131 }
132 #endif