io_uring: optimise submission side poll_refs
[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;
26};
27
28struct io_buffer {
29 struct list_head list;
30 __u64 addr;
31 __u32 len;
32 __u16 bid;
33 __u16 bgid;
34};
35
36void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
37 unsigned int issue_flags);
3b77495a
JA
38void io_destroy_buffers(struct io_ring_ctx *ctx);
39
40int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
41int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags);
42
43int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
44int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags);
45
46int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
47int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg);
48
53ccf69b
PB
49unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags);
50
024b8fde
HX
51void io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags);
52void io_kbuf_recycle_ring(struct io_kiocb *req);
53
3b77495a
JA
54static inline bool io_do_buffer_select(struct io_kiocb *req)
55{
56 if (!(req->flags & REQ_F_BUFFER_SELECT))
57 return false;
58 return !(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING));
59}
60
61static inline void io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags)
62{
3b77495a
JA
63 /*
64 * READV uses fields in `struct io_rw` (len/addr) to stash the selected
65 * buffer data. However if that buffer is recycled the original request
66 * data stored in addr is lost. Therefore forbid recycling for now.
67 */
68 if (req->opcode == IORING_OP_READV)
69 return;
70
024b8fde
HX
71 if (req->flags & REQ_F_BUFFER_SELECTED)
72 io_kbuf_recycle_legacy(req, issue_flags);
73 if (req->flags & REQ_F_BUFFER_RING)
74 io_kbuf_recycle_ring(req);
3b77495a
JA
75}
76
53ccf69b
PB
77static inline unsigned int __io_put_kbuf_list(struct io_kiocb *req,
78 struct list_head *list)
3b77495a
JA
79{
80 if (req->flags & REQ_F_BUFFER_RING) {
81 if (req->buf_list)
82 req->buf_list->head++;
83 req->flags &= ~REQ_F_BUFFER_RING;
84 } else {
85 list_add(&req->kbuf->list, list);
86 req->flags &= ~REQ_F_BUFFER_SELECTED;
87 }
88
89 return IORING_CQE_F_BUFFER | (req->buf_index << IORING_CQE_BUFFER_SHIFT);
90}
91
92static inline unsigned int io_put_kbuf_comp(struct io_kiocb *req)
93{
94 lockdep_assert_held(&req->ctx->completion_lock);
95
96 if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
97 return 0;
53ccf69b 98 return __io_put_kbuf_list(req, &req->ctx->io_buffers_comp);
3b77495a
JA
99}
100
101static inline unsigned int io_put_kbuf(struct io_kiocb *req,
102 unsigned issue_flags)
103{
3b77495a
JA
104
105 if (!(req->flags & (REQ_F_BUFFER_SELECTED|REQ_F_BUFFER_RING)))
106 return 0;
53ccf69b 107 return __io_put_kbuf(req, issue_flags);
3b77495a
JA
108}
109#endif