xref: /linux/io_uring/rsrc.h (revision 2f0f6b0773be0a1ec475097ae54848eea42adc7d)
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef IOU_RSRC_H
3 #define IOU_RSRC_H
4 
5 #include <linux/bvec.h>
6 #include <linux/io_uring_types.h>
7 #include <linux/lockdep.h>
8 #include <linux/uio.h>
9 
10 #define IO_VEC_CACHE_SOFT_CAP		256
11 
12 enum {
13 	IORING_RSRC_FILE		= 0,
14 	IORING_RSRC_BUFFER		= 1,
15 };
16 
17 struct io_rsrc_node {
18 	unsigned char			type;
19 	int				refs;
20 
21 	u64 tag;
22 	union {
23 		unsigned long file_ptr;
24 		struct io_mapped_ubuf *buf;
25 	};
26 };
27 
28 enum {
29 	IO_IMU_DEST	= 1 << ITER_DEST,
30 	IO_IMU_SOURCE	= 1 << ITER_SOURCE,
31 };
32 
33 enum {
34 	IO_REGBUF_F_KBUF		= 1,
35 };
36 
37 struct io_mapped_ubuf {
38 	u64		ubuf;
39 	size_t		len;
40 	unsigned int	nr_bvecs;
41 	unsigned int    folio_shift;
42 	refcount_t	refs;
43 	u8		flags;
44 	u8		dir;
45 	void		(*release)(void *);
46 	void		*priv;
47 	struct bio_vec	bvec[] __counted_by(nr_bvecs);
48 };
49 
50 struct io_imu_folio_data {
51 	/* Head folio can be partially included in the fixed buf */
52 	unsigned int	nr_pages_head;
53 	/* For non-head/tail folios, has to be fully included */
54 	unsigned int	nr_pages_mid;
55 	unsigned int	folio_shift;
56 	unsigned int	nr_folios;
57 	unsigned long	first_folio_page_idx;
58 };
59 
60 bool io_rsrc_cache_init(struct io_ring_ctx *ctx);
61 void io_rsrc_cache_free(struct io_ring_ctx *ctx);
62 struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx, int type);
63 void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node);
64 void io_rsrc_data_free(struct io_ring_ctx *ctx, struct io_rsrc_data *data);
65 int io_rsrc_data_alloc(struct io_rsrc_data *data, unsigned nr);
66 
67 struct io_rsrc_node *io_find_buf_node(struct io_kiocb *req,
68 				      unsigned issue_flags);
69 int io_import_reg_buf(struct io_kiocb *req, struct iov_iter *iter,
70 			u64 buf_addr, size_t len, int ddir,
71 			unsigned issue_flags);
72 int io_import_reg_vec(int ddir, struct iov_iter *iter,
73 			struct io_kiocb *req, struct iou_vec *vec,
74 			unsigned nr_iovs, unsigned issue_flags);
75 int io_prep_reg_iovec(struct io_kiocb *req, struct iou_vec *iv,
76 			const struct iovec __user *uvec, size_t uvec_segs);
77 
78 int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg);
79 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
80 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
81 			    unsigned int nr_args, u64 __user *tags);
82 int io_sqe_files_unregister(struct io_ring_ctx *ctx);
83 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
84 			  unsigned nr_args, u64 __user *tags);
85 
86 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
87 			     unsigned nr_args);
88 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
89 			    unsigned size, unsigned type);
90 int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
91 			unsigned int size, unsigned int type);
92 int io_validate_user_buf_range(u64 uaddr, u64 ulen);
93 
94 bool io_check_coalesce_buffer(struct page **page_array, int nr_pages,
95 			      struct io_imu_folio_data *data);
96 
97 static inline struct io_rsrc_node *io_rsrc_node_lookup(struct io_rsrc_data *data,
98 						       unsigned int index)
99 {
100 	if (index < data->nr)
101 		return data->nodes[array_index_nospec(index, data->nr)];
102 	return NULL;
103 }
104 
105 static inline void io_put_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
106 {
107 	lockdep_assert_held(&ctx->uring_lock);
108 	if (!--node->refs)
109 		io_free_rsrc_node(ctx, node);
110 }
111 
112 static inline bool io_reset_rsrc_node(struct io_ring_ctx *ctx,
113 				      struct io_rsrc_data *data,
114 				      unsigned int index)
115 {
116 	struct io_rsrc_node *node;
117 
118 	if (index >= data->nr)
119 		return false;
120 	index = array_index_nospec(index, data->nr);
121 	node = data->nodes[index];
122 	if (!node)
123 		return false;
124 	io_put_rsrc_node(ctx, node);
125 	data->nodes[index] = NULL;
126 	return true;
127 }
128 
129 int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
130 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
131 
132 int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
133 int io_account_mem(struct user_struct *user, struct mm_struct *mm_account,
134 		   unsigned long nr_pages);
135 void io_unaccount_mem(struct user_struct *user, struct mm_struct *mm_account,
136 		      unsigned long nr_pages);
137 
138 static inline void __io_unaccount_mem(struct user_struct *user,
139 				      unsigned long nr_pages)
140 {
141 	atomic_long_sub(nr_pages, &user->locked_vm);
142 }
143 
144 void io_vec_free(struct iou_vec *iv);
145 int io_vec_realloc(struct iou_vec *iv, unsigned nr_entries);
146 
147 static inline void io_vec_reset_iovec(struct iou_vec *iv,
148 				      struct iovec *iovec, unsigned nr)
149 {
150 	io_vec_free(iv);
151 	iv->iovec = iovec;
152 	iv->nr = nr;
153 }
154 
155 static inline void io_alloc_cache_vec_kasan(struct iou_vec *iv)
156 {
157 	if (IS_ENABLED(CONFIG_KASAN))
158 		io_vec_free(iv);
159 }
160 
161 #endif
162