^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #ifndef INTERNAL_IO_WQ_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #define INTERNAL_IO_WQ_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/io_uring.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) struct io_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) IO_WQ_WORK_CANCEL = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) IO_WQ_WORK_HASHED = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) IO_WQ_WORK_UNBOUND = 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) IO_WQ_WORK_NO_CANCEL = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) IO_WQ_WORK_CONCURRENT = 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) IO_WQ_WORK_FILES = 32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) IO_WQ_WORK_FS = 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) IO_WQ_WORK_MM = 128,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) IO_WQ_WORK_CREDS = 256,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) IO_WQ_WORK_BLKCG = 512,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) IO_WQ_WORK_FSIZE = 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) IO_WQ_HASH_SHIFT = 24, /* upper 8 bits are used for hash key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) enum io_wq_cancel {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) IO_WQ_CANCEL_OK, /* cancelled before started */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) IO_WQ_CANCEL_RUNNING, /* found, running, and attempted cancelled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) IO_WQ_CANCEL_NOTFOUND, /* work not found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct io_wq_work_node {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct io_wq_work_node *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct io_wq_work_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct io_wq_work_node *first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct io_wq_work_node *last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static inline void wq_list_add_after(struct io_wq_work_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct io_wq_work_node *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct io_wq_work_list *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct io_wq_work_node *next = pos->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) pos->next = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) node->next = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (!next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) list->last = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static inline void wq_list_add_tail(struct io_wq_work_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct io_wq_work_list *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (!list->first) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) list->last = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) WRITE_ONCE(list->first, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) list->last->next = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) list->last = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) node->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static inline void wq_list_cut(struct io_wq_work_list *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct io_wq_work_node *last,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct io_wq_work_node *prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* first in the list, if prev==NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) WRITE_ONCE(list->first, last->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) prev->next = last->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (last == list->last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) list->last = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) last->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static inline void wq_list_del(struct io_wq_work_list *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct io_wq_work_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct io_wq_work_node *prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) wq_list_cut(list, node, prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define wq_list_for_each(pos, prv, head) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) for (pos = (head)->first, prv = NULL; pos; prv = pos, pos = (pos)->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #define wq_list_empty(list) (READ_ONCE((list)->first) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #define INIT_WQ_LIST(list) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) (list)->first = NULL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) (list)->last = NULL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct io_wq_work {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct io_wq_work_node list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct io_identity *identity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unsigned flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static inline struct io_wq_work *wq_next_work(struct io_wq_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!work->list.next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return container_of(work->list.next, struct io_wq_work, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) typedef void (free_work_fn)(struct io_wq_work *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) typedef struct io_wq_work *(io_wq_work_fn)(struct io_wq_work *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct io_wq_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct user_struct *user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) io_wq_work_fn *do_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) free_work_fn *free_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) bool io_wq_get(struct io_wq *wq, struct io_wq_data *data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) void io_wq_destroy(struct io_wq *wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) void io_wq_hash_work(struct io_wq_work *work, void *val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static inline bool io_wq_is_hashed(struct io_wq_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return work->flags & IO_WQ_WORK_HASHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) void io_wq_cancel_all(struct io_wq *wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) typedef bool (work_cancel_fn)(struct io_wq_work *, void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) void *data, bool cancel_all);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct task_struct *io_wq_get_task(struct io_wq *wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #if defined(CONFIG_IO_WQ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) extern void io_wq_worker_sleeping(struct task_struct *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) extern void io_wq_worker_running(struct task_struct *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static inline void io_wq_worker_sleeping(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static inline void io_wq_worker_running(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static inline bool io_wq_current_is_worker(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return in_task() && (current->flags & PF_IO_WORKER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #endif