xref: /openbmc/linux/include/linux/io_uring.h (revision 303c0a13)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _LINUX_IO_URING_H
3 #define _LINUX_IO_URING_H
4 
5 #include <linux/sched.h>
6 #include <linux/xarray.h>
7 #include <uapi/linux/io_uring.h>
8 
9 enum io_uring_cmd_flags {
10 	IO_URING_F_COMPLETE_DEFER	= 1,
11 	IO_URING_F_UNLOCKED		= 2,
12 	/* the request is executed from poll, it should not be freed */
13 	IO_URING_F_MULTISHOT		= 4,
14 	/* executed by io-wq */
15 	IO_URING_F_IOWQ			= 8,
16 	/* int's last bit, sign checks are usually faster than a bit test */
17 	IO_URING_F_NONBLOCK		= INT_MIN,
18 
19 	/* ctx state flags, for URING_CMD */
20 	IO_URING_F_SQE128		= (1 << 8),
21 	IO_URING_F_CQE32		= (1 << 9),
22 	IO_URING_F_IOPOLL		= (1 << 10),
23 };
24 
25 struct io_uring_cmd {
26 	struct file	*file;
27 	const struct io_uring_sqe *sqe;
28 	union {
29 		/* callback to defer completions to task context */
30 		void (*task_work_cb)(struct io_uring_cmd *cmd, unsigned);
31 		/* used for polled completion */
32 		void *cookie;
33 	};
34 	u32		cmd_op;
35 	u32		flags;
36 	u8		pdu[32]; /* available inline for free use */
37 };
38 
io_uring_sqe_cmd(const struct io_uring_sqe * sqe)39 static inline const void *io_uring_sqe_cmd(const struct io_uring_sqe *sqe)
40 {
41 	return sqe->cmd;
42 }
43 
44 #if defined(CONFIG_IO_URING)
45 int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
46 			      struct iov_iter *iter, void *ioucmd);
47 void io_uring_cmd_done(struct io_uring_cmd *cmd, ssize_t ret, ssize_t res2,
48 			unsigned issue_flags);
49 void __io_uring_cancel(bool cancel_all);
50 void __io_uring_free(struct task_struct *tsk);
51 void io_uring_unreg_ringfd(void);
52 const char *io_uring_get_opcode(u8 opcode);
53 void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd,
54 			    void (*task_work_cb)(struct io_uring_cmd *, unsigned),
55 			    unsigned flags);
56 /* users should follow semantics of IOU_F_TWQ_LAZY_WAKE */
57 void io_uring_cmd_do_in_task_lazy(struct io_uring_cmd *ioucmd,
58 			void (*task_work_cb)(struct io_uring_cmd *, unsigned));
59 
io_uring_cmd_complete_in_task(struct io_uring_cmd * ioucmd,void (* task_work_cb)(struct io_uring_cmd *,unsigned))60 static inline void io_uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd,
61 			void (*task_work_cb)(struct io_uring_cmd *, unsigned))
62 {
63 	__io_uring_cmd_do_in_task(ioucmd, task_work_cb, 0);
64 }
65 
io_uring_files_cancel(void)66 static inline void io_uring_files_cancel(void)
67 {
68 	if (current->io_uring) {
69 		io_uring_unreg_ringfd();
70 		__io_uring_cancel(false);
71 	}
72 }
io_uring_task_cancel(void)73 static inline void io_uring_task_cancel(void)
74 {
75 	if (current->io_uring)
76 		__io_uring_cancel(true);
77 }
io_uring_free(struct task_struct * tsk)78 static inline void io_uring_free(struct task_struct *tsk)
79 {
80 	if (tsk->io_uring)
81 		__io_uring_free(tsk);
82 }
83 int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags);
84 bool io_is_uring_fops(struct file *file);
85 #else
io_uring_cmd_import_fixed(u64 ubuf,unsigned long len,int rw,struct iov_iter * iter,void * ioucmd)86 static inline int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
87 			      struct iov_iter *iter, void *ioucmd)
88 {
89 	return -EOPNOTSUPP;
90 }
io_uring_cmd_done(struct io_uring_cmd * cmd,ssize_t ret,ssize_t ret2,unsigned issue_flags)91 static inline void io_uring_cmd_done(struct io_uring_cmd *cmd, ssize_t ret,
92 		ssize_t ret2, unsigned issue_flags)
93 {
94 }
io_uring_cmd_complete_in_task(struct io_uring_cmd * ioucmd,void (* task_work_cb)(struct io_uring_cmd *,unsigned))95 static inline void io_uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd,
96 			void (*task_work_cb)(struct io_uring_cmd *, unsigned))
97 {
98 }
io_uring_cmd_do_in_task_lazy(struct io_uring_cmd * ioucmd,void (* task_work_cb)(struct io_uring_cmd *,unsigned))99 static inline void io_uring_cmd_do_in_task_lazy(struct io_uring_cmd *ioucmd,
100 			void (*task_work_cb)(struct io_uring_cmd *, unsigned))
101 {
102 }
io_uring_task_cancel(void)103 static inline void io_uring_task_cancel(void)
104 {
105 }
io_uring_files_cancel(void)106 static inline void io_uring_files_cancel(void)
107 {
108 }
io_uring_free(struct task_struct * tsk)109 static inline void io_uring_free(struct task_struct *tsk)
110 {
111 }
io_uring_get_opcode(u8 opcode)112 static inline const char *io_uring_get_opcode(u8 opcode)
113 {
114 	return "";
115 }
io_uring_cmd_sock(struct io_uring_cmd * cmd,unsigned int issue_flags)116 static inline int io_uring_cmd_sock(struct io_uring_cmd *cmd,
117 				    unsigned int issue_flags)
118 {
119 	return -EOPNOTSUPP;
120 }
io_is_uring_fops(struct file * file)121 static inline bool io_is_uring_fops(struct file *file)
122 {
123 	return false;
124 }
125 #endif
126 
127 #endif
128