• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *	Berkeley style UIO structures	-	Alan Cox 1994.
4  */
5 #ifndef __LINUX_UIO_H
6 #define __LINUX_UIO_H
7 
8 #include <linux/kernel.h>
9 #include <linux/thread_info.h>
10 #include <uapi/linux/uio.h>
11 
12 struct page;
13 struct pipe_inode_info;
14 
15 struct kvec {
16 	void *iov_base; /* and that should *never* hold a userland pointer */
17 	size_t iov_len;
18 };
19 
20 enum iter_type {
21 	/* iter types */
22 	ITER_IOVEC = 4,
23 	ITER_KVEC = 8,
24 	ITER_BVEC = 16,
25 	ITER_PIPE = 32,
26 	ITER_DISCARD = 64,
27 };
28 
29 struct iov_iter_state {
30 	size_t iov_offset;
31 	size_t count;
32 	unsigned long nr_segs;
33 };
34 
35 struct iov_iter {
36 	/*
37 	 * Bit 0 is the read/write bit, set if we're writing.
38 	 * Bit 1 is the BVEC_FLAG_NO_REF bit, set if type is a bvec and
39 	 * the caller isn't expecting to drop a page reference when done.
40 	 */
41 	unsigned int type;
42 	size_t iov_offset;
43 	size_t count;
44 	union {
45 		const struct iovec *iov;
46 		const struct kvec *kvec;
47 		const struct bio_vec *bvec;
48 		struct pipe_inode_info *pipe;
49 	};
50 	union {
51 		unsigned long nr_segs;
52 		struct {
53 			unsigned int head;
54 			unsigned int start_head;
55 		};
56 	};
57 };
58 
iov_iter_type(const struct iov_iter * i)59 static inline enum iter_type iov_iter_type(const struct iov_iter *i)
60 {
61 	return i->type & ~(READ | WRITE);
62 }
63 
iov_iter_save_state(struct iov_iter * iter,struct iov_iter_state * state)64 static inline void iov_iter_save_state(struct iov_iter *iter,
65 				       struct iov_iter_state *state)
66 {
67 	state->iov_offset = iter->iov_offset;
68 	state->count = iter->count;
69 	state->nr_segs = iter->nr_segs;
70 }
71 
iter_is_iovec(const struct iov_iter * i)72 static inline bool iter_is_iovec(const struct iov_iter *i)
73 {
74 	return iov_iter_type(i) == ITER_IOVEC;
75 }
76 
iov_iter_is_kvec(const struct iov_iter * i)77 static inline bool iov_iter_is_kvec(const struct iov_iter *i)
78 {
79 	return iov_iter_type(i) == ITER_KVEC;
80 }
81 
iov_iter_is_bvec(const struct iov_iter * i)82 static inline bool iov_iter_is_bvec(const struct iov_iter *i)
83 {
84 	return iov_iter_type(i) == ITER_BVEC;
85 }
86 
iov_iter_is_pipe(const struct iov_iter * i)87 static inline bool iov_iter_is_pipe(const struct iov_iter *i)
88 {
89 	return iov_iter_type(i) == ITER_PIPE;
90 }
91 
iov_iter_is_discard(const struct iov_iter * i)92 static inline bool iov_iter_is_discard(const struct iov_iter *i)
93 {
94 	return iov_iter_type(i) == ITER_DISCARD;
95 }
96 
iov_iter_rw(const struct iov_iter * i)97 static inline unsigned char iov_iter_rw(const struct iov_iter *i)
98 {
99 	return i->type & (READ | WRITE);
100 }
101 
102 /*
103  * Total number of bytes covered by an iovec.
104  *
105  * NOTE that it is not safe to use this function until all the iovec's
106  * segment lengths have been validated.  Because the individual lengths can
107  * overflow a size_t when added together.
108  */
iov_length(const struct iovec * iov,unsigned long nr_segs)109 static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs)
110 {
111 	unsigned long seg;
112 	size_t ret = 0;
113 
114 	for (seg = 0; seg < nr_segs; seg++)
115 		ret += iov[seg].iov_len;
116 	return ret;
117 }
118 
iov_iter_iovec(const struct iov_iter * iter)119 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
120 {
121 	return (struct iovec) {
122 		.iov_base = iter->iov->iov_base + iter->iov_offset,
123 		.iov_len = min(iter->count,
124 			       iter->iov->iov_len - iter->iov_offset),
125 	};
126 }
127 
128 size_t iov_iter_copy_from_user_atomic(struct page *page,
129 		struct iov_iter *i, unsigned long offset, size_t bytes);
130 void iov_iter_advance(struct iov_iter *i, size_t bytes);
131 void iov_iter_revert(struct iov_iter *i, size_t bytes);
132 int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes);
133 size_t iov_iter_single_seg_count(const struct iov_iter *i);
134 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
135 			 struct iov_iter *i);
136 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
137 			 struct iov_iter *i);
138 
139 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
140 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i);
141 bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i);
142 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i);
143 bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i);
144 
145 static __always_inline __must_check
copy_to_iter(const void * addr,size_t bytes,struct iov_iter * i)146 size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
147 {
148 	if (unlikely(!check_copy_size(addr, bytes, true)))
149 		return 0;
150 	else
151 		return _copy_to_iter(addr, bytes, i);
152 }
153 
154 static __always_inline __must_check
copy_from_iter(void * addr,size_t bytes,struct iov_iter * i)155 size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
156 {
157 	if (unlikely(!check_copy_size(addr, bytes, false)))
158 		return 0;
159 	else
160 		return _copy_from_iter(addr, bytes, i);
161 }
162 
163 static __always_inline __must_check
copy_from_iter_full(void * addr,size_t bytes,struct iov_iter * i)164 bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
165 {
166 	if (unlikely(!check_copy_size(addr, bytes, false)))
167 		return false;
168 	else
169 		return _copy_from_iter_full(addr, bytes, i);
170 }
171 
172 static __always_inline __must_check
copy_from_iter_nocache(void * addr,size_t bytes,struct iov_iter * i)173 size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
174 {
175 	if (unlikely(!check_copy_size(addr, bytes, false)))
176 		return 0;
177 	else
178 		return _copy_from_iter_nocache(addr, bytes, i);
179 }
180 
181 static __always_inline __must_check
copy_from_iter_full_nocache(void * addr,size_t bytes,struct iov_iter * i)182 bool copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
183 {
184 	if (unlikely(!check_copy_size(addr, bytes, false)))
185 		return false;
186 	else
187 		return _copy_from_iter_full_nocache(addr, bytes, i);
188 }
189 
190 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
191 /*
192  * Note, users like pmem that depend on the stricter semantics of
193  * copy_from_iter_flushcache() than copy_from_iter_nocache() must check for
194  * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the
195  * destination is flushed from the cache on return.
196  */
197 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i);
198 #else
199 #define _copy_from_iter_flushcache _copy_from_iter_nocache
200 #endif
201 
202 #ifdef CONFIG_ARCH_HAS_COPY_MC
203 size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
204 #else
205 #define _copy_mc_to_iter _copy_to_iter
206 #endif
207 
208 static __always_inline __must_check
copy_from_iter_flushcache(void * addr,size_t bytes,struct iov_iter * i)209 size_t copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
210 {
211 	if (unlikely(!check_copy_size(addr, bytes, false)))
212 		return 0;
213 	else
214 		return _copy_from_iter_flushcache(addr, bytes, i);
215 }
216 
217 static __always_inline __must_check
copy_mc_to_iter(void * addr,size_t bytes,struct iov_iter * i)218 size_t copy_mc_to_iter(void *addr, size_t bytes, struct iov_iter *i)
219 {
220 	if (unlikely(!check_copy_size(addr, bytes, true)))
221 		return 0;
222 	else
223 		return _copy_mc_to_iter(addr, bytes, i);
224 }
225 
226 size_t iov_iter_zero(size_t bytes, struct iov_iter *);
227 unsigned long iov_iter_alignment(const struct iov_iter *i);
228 unsigned long iov_iter_gap_alignment(const struct iov_iter *i);
229 void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov,
230 			unsigned long nr_segs, size_t count);
231 void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec,
232 			unsigned long nr_segs, size_t count);
233 void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec,
234 			unsigned long nr_segs, size_t count);
235 void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe,
236 			size_t count);
237 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count);
238 ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
239 			size_t maxsize, unsigned maxpages, size_t *start);
240 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages,
241 			size_t maxsize, size_t *start);
242 int iov_iter_npages(const struct iov_iter *i, int maxpages);
243 void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state);
244 
245 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags);
246 
iov_iter_count(const struct iov_iter * i)247 static inline size_t iov_iter_count(const struct iov_iter *i)
248 {
249 	return i->count;
250 }
251 
252 /*
253  * Cap the iov_iter by given limit; note that the second argument is
254  * *not* the new size - it's upper limit for such.  Passing it a value
255  * greater than the amount of data in iov_iter is fine - it'll just do
256  * nothing in that case.
257  */
iov_iter_truncate(struct iov_iter * i,u64 count)258 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
259 {
260 	/*
261 	 * count doesn't have to fit in size_t - comparison extends both
262 	 * operands to u64 here and any value that would be truncated by
263 	 * conversion in assignement is by definition greater than all
264 	 * values of size_t, including old i->count.
265 	 */
266 	if (i->count > count)
267 		i->count = count;
268 }
269 
270 /*
271  * reexpand a previously truncated iterator; count must be no more than how much
272  * we had shrunk it.
273  */
iov_iter_reexpand(struct iov_iter * i,size_t count)274 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
275 {
276 	i->count = count;
277 }
278 
279 struct csum_state {
280 	__wsum csum;
281 	size_t off;
282 };
283 
284 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csstate, struct iov_iter *i);
285 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
286 bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
287 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
288 		struct iov_iter *i);
289 
290 struct iovec *iovec_from_user(const struct iovec __user *uvector,
291 		unsigned long nr_segs, unsigned long fast_segs,
292 		struct iovec *fast_iov, bool compat);
293 ssize_t import_iovec(int type, const struct iovec __user *uvec,
294 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
295 		 struct iov_iter *i);
296 ssize_t __import_iovec(int type, const struct iovec __user *uvec,
297 		 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
298 		 struct iov_iter *i, bool compat);
299 int import_single_range(int type, void __user *buf, size_t len,
300 		 struct iovec *iov, struct iov_iter *i);
301 
302 int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
303 			    int (*f)(struct kvec *vec, void *context),
304 			    void *context);
305 
306 #endif
307