io_uring/kbuf: rename struct io_uring_buf_reg 'pad' to'flags'
[linux-block.git] / io_uring / kbuf.h
CommitLineData
3b77495a
JA
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
7struct io_buffer_list {
8 /*
9 * If ->buf_nr_pages is set, then buf_pages/buf_ring are used. If not,
10 * then these are classic provided buffers and ->buf_list is used.
11 */
12 union {
13 struct list_head buf_list;
14 struct {
15 struct page **buf_pages;
16 struct io_uring_buf_ring *buf_ring;
17 };
18 };
19 __u16 bgid;
20
21 /* below is for ring provided buffers */
22 __u16 buf_nr_pages;
23 __u16 nr_entries;
24 __u16 head;
25 __u16 mask;
25a2c188
JA
26
27 /* ring mapped provided buffers */
28 __u8 is_mapped;
3b77495a
JA
29};
30
31struct io_buffer {
32 struct list_head list;
33 __u64 addr;
34 __u32 len;
35 __u16 bid;
36 __u16 bgid;
37};
38
39void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
40 unsigned int issue_flags);
3b77495a
JA
41void io_destroy_buffers(struct io_ring_ctx *ctx);
42
43int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
44int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags);
45
46int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
47int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags);
48
49int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
50int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
51
53ccf69b
PB
52unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags);
53
024b8fde 54void io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags);
795bbbc8
HX
55
56static inline void io_kbuf_recycle_ring(struct io_kiocb *req)
57{
58 /*
59 * We don't need to recycle for REQ_F_BUFFER_RING, we can just clear
60 * the flag and hence ensure that bl->head doesn't get incremented.
61 * If the tail has already been incremented, hang on to it.
62 * The exception is partial io, that case we should increment bl->head
63 * to monopolize the buffer.
64 */
65 if (req->buf_list) {
66 if (req->flags & REQ_F_PARTIAL_IO) {
67 /*
68 * If we end up here, then the io_uring_lock has
69 * been kept held since we retrieved the buffer.
70 * For the io-wq case, we already cleared
71 * req->buf_list when the buffer was retrieved,
72 * hence it cannot be set here for that case.
73 */
74 req->buf_list->head++;
75 req->buf_list = NULL;
76 } else {
77 req->buf_index = req->buf_list->bgid;
78 req->flags &= ~REQ_F_BUFFER_RING;
79 }
80 }
81}
024b8fde 82
3b77495a
JA
83static inline bool io_do_buffer_select(struct io_kiocb *req)
84{
85 if (!(req->flags & REQ_F_BUFFER_SELECT))
86 return false;
87 return !(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING));
88}
89
90static inline void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags)
91{
024b8fde
HX
92 if (req->flags & REQ_F_BUFFER_SELECTED)
93 io_kbuf_recycle_legacy(req, issue_flags);
94 if (req->flags & REQ_F_BUFFER_RING)
95 io_kbuf_recycle_ring(req);
3b77495a
JA
96}
97
53ccf69b
PB
98static inline unsigned int __io_put_kbuf_list(struct io_kiocb *req,
99 struct list_head *list)
3b77495a 100{
32f3c434
DY
101 unsigned int ret = IORING_CQE_F_BUFFER | (req->buf_index << IORING_CQE_BUFFER_SHIFT);
102
3b77495a 103 if (req->flags & REQ_F_BUFFER_RING) {
32f3c434
DY
104 if (req->buf_list) {
105 req->buf_index = req->buf_list->bgid;
3b77495a 106 req->buf_list->head++;
32f3c434 107 }
3b77495a
JA
108 req->flags &= ~REQ_F_BUFFER_RING;
109 } else {
32f3c434 110 req->buf_index = req->kbuf->bgid;
3b77495a
JA
111 list_add(&req->kbuf->list, list);
112 req->flags &= ~REQ_F_BUFFER_SELECTED;
113 }
114
32f3c434 115 return ret;
3b77495a
JA
116}
117
118static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req)
119{
120 lockdep_assert_held(&req->ctx->completion_lock);
121
122 if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
123 return 0;
53ccf69b 124 return __io_put_kbuf_list(req, &req->ctx->io_buffers_comp);
3b77495a
JA
125}
126
127static inline unsigned int io_put_kbuf(struct io_kiocb *req,
128 unsigned issue_flags)
129{
3b77495a
JA
130
131 if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
132 return 0;
53ccf69b 133 return __io_put_kbuf(req, issue_flags);
3b77495a
JA
134}
135#endif