xref: /linux/io_uring/rsrc.h (revision e814f3fd16acfb7f9966773953de8f740a1e3202)
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef IOU_RSRC_H
3 #define IOU_RSRC_H
4 
5 #define IO_NODE_ALLOC_CACHE_MAX 32
6 
7 #define IO_RSRC_TAG_TABLE_SHIFT	(PAGE_SHIFT - 3)
8 #define IO_RSRC_TAG_TABLE_MAX	(1U << IO_RSRC_TAG_TABLE_SHIFT)
9 #define IO_RSRC_TAG_TABLE_MASK	(IO_RSRC_TAG_TABLE_MAX - 1)
10 
11 enum {
12 	IORING_RSRC_FILE		= 0,
13 	IORING_RSRC_BUFFER		= 1,
14 };
15 
16 struct io_rsrc_node {
17 	unsigned char			type;
18 	int				refs;
19 
20 	u64 tag;
21 	union {
22 		unsigned long file_ptr;
23 		struct io_mapped_ubuf *buf;
24 	};
25 };
26 
27 struct io_mapped_ubuf {
28 	u64		ubuf;
29 	unsigned int	len;
30 	unsigned int	nr_bvecs;
31 	unsigned int    folio_shift;
32 	refcount_t	refs;
33 	unsigned long	acct_pages;
34 	struct bio_vec	bvec[] __counted_by(nr_bvecs);
35 };
36 
37 struct io_imu_folio_data {
38 	/* Head folio can be partially included in the fixed buf */
39 	unsigned int	nr_pages_head;
40 	/* For non-head/tail folios, has to be fully included */
41 	unsigned int	nr_pages_mid;
42 	unsigned int	folio_shift;
43 	unsigned int	nr_folios;
44 };
45 
46 struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx, int type);
47 void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node);
48 void io_rsrc_data_free(struct io_ring_ctx *ctx, struct io_rsrc_data *data);
49 int io_rsrc_data_alloc(struct io_rsrc_data *data, unsigned nr);
50 
51 int io_import_fixed(int ddir, struct iov_iter *iter,
52 			   struct io_mapped_ubuf *imu,
53 			   u64 buf_addr, size_t len);
54 
55 int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg);
56 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
57 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
58 			    unsigned int nr_args, u64 __user *tags);
59 int io_sqe_files_unregister(struct io_ring_ctx *ctx);
60 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
61 			  unsigned nr_args, u64 __user *tags);
62 
63 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
64 			     unsigned nr_args);
65 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
66 			    unsigned size, unsigned type);
67 int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
68 			unsigned int size, unsigned int type);
69 
70 bool io_check_coalesce_buffer(struct page **page_array, int nr_pages,
71 			      struct io_imu_folio_data *data);
72 
73 static inline struct io_rsrc_node *io_rsrc_node_lookup(struct io_rsrc_data *data,
74 						       int index)
75 {
76 	if (index < data->nr)
77 		return data->nodes[array_index_nospec(index, data->nr)];
78 	return NULL;
79 }
80 
81 static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
82 {
83 	if (node && !--node->refs)
84 		io_free_rsrc_node(ctx, node);
85 }
86 
87 static inline bool io_reset_rsrc_node(struct io_ring_ctx *ctx,
88 				      struct io_rsrc_data *data, int index)
89 {
90 	struct io_rsrc_node *node = data->nodes[index];
91 
92 	if (!node)
93 		return false;
94 	io_put_rsrc_node(ctx, node);
95 	data->nodes[index] = NULL;
96 	return true;
97 }
98 
99 static inline void io_req_put_rsrc_nodes(struct io_kiocb *req)
100 {
101 	if (req->file_node) {
102 		io_put_rsrc_node(req->ctx, req->file_node);
103 		req->file_node = NULL;
104 	}
105 	if (req->flags & REQ_F_BUF_NODE) {
106 		io_put_rsrc_node(req->ctx, req->buf_node);
107 		req->buf_node = NULL;
108 	}
109 }
110 
111 static inline void io_req_assign_rsrc_node(struct io_rsrc_node **dst_node,
112 					   struct io_rsrc_node *node)
113 {
114 	node->refs++;
115 	*dst_node = node;
116 }
117 
118 static inline void io_req_assign_buf_node(struct io_kiocb *req,
119 					  struct io_rsrc_node *node)
120 {
121 	io_req_assign_rsrc_node(&req->buf_node, node);
122 	req->flags |= REQ_F_BUF_NODE;
123 }
124 
125 int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
126 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
127 
128 int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
129 
130 static inline void __io_unaccount_mem(struct user_struct *user,
131 				      unsigned long nr_pages)
132 {
133 	atomic_long_sub(nr_pages, &user->locked_vm);
134 }
135 
136 #endif
137