xref: /linux/io_uring/kbuf.h (revision fb7399cf2d0b33825b8039f95c45395c7deba25c)
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef IOU_KBUF_H
3 #define IOU_KBUF_H
4 
5 #include <uapi/linux/io_uring.h>
6 #include <linux/io_uring_types.h>
7 
8 enum {
9 	/* ring mapped provided buffers */
10 	IOBL_BUF_RING	= 1,
11 	/* buffers are consumed incrementally rather than always fully */
12 	IOBL_INC	= 2,
13 };
14 
15 struct io_buffer_list {
16 	/*
17 	 * If ->buf_nr_pages is set, then buf_pages/buf_ring are used. If not,
18 	 * then these are classic provided buffers and ->buf_list is used.
19 	 */
20 	union {
21 		struct list_head buf_list;
22 		struct io_uring_buf_ring *buf_ring;
23 	};
24 	/* count of classic/legacy buffers in buffer list */
25 	int nbufs;
26 
27 	__u16 bgid;
28 
29 	/* below is for ring provided buffers */
30 	__u16 buf_nr_pages;
31 	__u16 nr_entries;
32 	__u16 head;
33 	__u16 mask;
34 
35 	__u16 flags;
36 
37 	struct io_mapped_region region;
38 };
39 
40 struct io_buffer {
41 	struct list_head list;
42 	__u64 addr;
43 	__u32 len;
44 	__u16 bid;
45 	__u16 bgid;
46 };
47 
48 enum {
49 	/* can alloc a bigger vec */
50 	KBUF_MODE_EXPAND	= 1,
51 	/* if bigger vec allocated, free old one */
52 	KBUF_MODE_FREE		= 2,
53 };
54 
55 struct buf_sel_arg {
56 	struct iovec *iovs;
57 	size_t out_len;
58 	size_t max_len;
59 	unsigned short nr_iovs;
60 	unsigned short mode;
61 	unsigned buf_group;
62 };
63 
64 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
65 			      unsigned buf_group, unsigned int issue_flags);
66 int io_buffers_select(struct io_kiocb *req, struct buf_sel_arg *arg,
67 		      unsigned int issue_flags);
68 int io_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg);
69 void io_destroy_buffers(struct io_ring_ctx *ctx);
70 
71 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
72 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
73 int io_manage_buffers_legacy(struct io_kiocb *req, unsigned int issue_flags);
74 
75 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
76 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
77 int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg);
78 
79 bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags);
80 void io_kbuf_drop_legacy(struct io_kiocb *req);
81 
82 unsigned int __io_put_kbufs(struct io_kiocb *req, int len, int nbufs);
83 bool io_kbuf_commit(struct io_kiocb *req,
84 		    struct io_buffer_list *bl, int len, int nr);
85 
86 struct io_mapped_region *io_pbuf_get_region(struct io_ring_ctx *ctx,
87 					    unsigned int bgid);
88 
89 static inline bool io_kbuf_recycle_ring(struct io_kiocb *req)
90 {
91 	/*
92 	 * We don't need to recycle for REQ_F_BUFFER_RING, we can just clear
93 	 * the flag and hence ensure that bl->head doesn't get incremented.
94 	 * If the tail has already been incremented, hang on to it.
95 	 * The exception is partial io, that case we should increment bl->head
96 	 * to monopolize the buffer.
97 	 */
98 	if (req->buf_list) {
99 		req->flags &= ~(REQ_F_BUFFER_RING|REQ_F_BUFFERS_COMMIT);
100 		return true;
101 	}
102 	return false;
103 }
104 
105 static inline bool io_do_buffer_select(struct io_kiocb *req)
106 {
107 	if (!(req->flags & REQ_F_BUFFER_SELECT))
108 		return false;
109 	return !(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING));
110 }
111 
112 static inline bool io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags)
113 {
114 	if (req->flags & REQ_F_BL_NO_RECYCLE)
115 		return false;
116 	if (req->flags & REQ_F_BUFFER_SELECTED)
117 		return io_kbuf_recycle_legacy(req, issue_flags);
118 	if (req->flags & REQ_F_BUFFER_RING)
119 		return io_kbuf_recycle_ring(req);
120 	return false;
121 }
122 
123 static inline unsigned int io_put_kbuf(struct io_kiocb *req, int len,
124 				       unsigned issue_flags)
125 {
126 	if (!(req->flags & (REQ_F_BUFFER_RING | REQ_F_BUFFER_SELECTED)))
127 		return 0;
128 	return __io_put_kbufs(req, len, 1);
129 }
130 
131 static inline unsigned int io_put_kbufs(struct io_kiocb *req, int len,
132 					int nbufs, unsigned issue_flags)
133 {
134 	if (!(req->flags & (REQ_F_BUFFER_RING | REQ_F_BUFFER_SELECTED)))
135 		return 0;
136 	return __io_put_kbufs(req, len, nbufs);
137 }
138 #endif
139