1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Berkeley style UIO structures - Alan Cox 1994.
8 #include <linux/kernel.h>
9 #include <linux/thread_info.h>
10 #include <uapi/linux/uio.h>
13 struct pipe_inode_info;
16 void *iov_base; /* and that should *never* hold a userland pointer */
32 * Bit 0 is the read/write bit, set if we're writing.
33 * Bit 1 is the BVEC_FLAG_NO_REF bit, set if type is a bvec and
34 * the caller isn't expecting to drop a page reference when done.
40 const struct iovec *iov;
41 const struct kvec *kvec;
42 const struct bio_vec *bvec;
43 struct xarray *xarray;
44 struct pipe_inode_info *pipe;
47 unsigned long nr_segs;
50 unsigned int start_head;
56 static inline enum iter_type iov_iter_type(const struct iov_iter *i)
58 return i->type & ~(READ | WRITE);
61 static inline bool iter_is_iovec(const struct iov_iter *i)
63 return iov_iter_type(i) == ITER_IOVEC;
66 static inline bool iov_iter_is_kvec(const struct iov_iter *i)
68 return iov_iter_type(i) == ITER_KVEC;
71 static inline bool iov_iter_is_bvec(const struct iov_iter *i)
73 return iov_iter_type(i) == ITER_BVEC;
76 static inline bool iov_iter_is_pipe(const struct iov_iter *i)
78 return iov_iter_type(i) == ITER_PIPE;
81 static inline bool iov_iter_is_discard(const struct iov_iter *i)
83 return iov_iter_type(i) == ITER_DISCARD;
86 static inline bool iov_iter_is_xarray(const struct iov_iter *i)
88 return iov_iter_type(i) == ITER_XARRAY;
91 static inline unsigned char iov_iter_rw(const struct iov_iter *i)
93 return i->type & (READ | WRITE);
97 * Total number of bytes covered by an iovec.
99 * NOTE that it is not safe to use this function until all the iovec's
100 * segment lengths have been validated. Because the individual lengths can
101 * overflow a size_t when added together.
103 static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs)
108 for (seg = 0; seg < nr_segs; seg++)
109 ret += iov[seg].iov_len;
113 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
115 return (struct iovec) {
116 .iov_base = iter->iov->iov_base + iter->iov_offset,
117 .iov_len = min(iter->count,
118 iter->iov->iov_len - iter->iov_offset),
122 size_t iov_iter_copy_from_user_atomic(struct page *page,
123 struct iov_iter *i, unsigned long offset, size_t bytes);
124 void iov_iter_advance(struct iov_iter *i, size_t bytes);
125 void iov_iter_revert(struct iov_iter *i, size_t bytes);
126 int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes);
127 size_t iov_iter_single_seg_count(const struct iov_iter *i);
128 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
130 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
133 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
134 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i);
135 bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i);
136 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i);
137 bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i);
139 static __always_inline __must_check
140 size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
142 if (unlikely(!check_copy_size(addr, bytes, true)))
145 return _copy_to_iter(addr, bytes, i);
148 static __always_inline __must_check
149 size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
151 if (unlikely(!check_copy_size(addr, bytes, false)))
154 return _copy_from_iter(addr, bytes, i);
157 static __always_inline __must_check
158 bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
160 if (unlikely(!check_copy_size(addr, bytes, false)))
163 return _copy_from_iter_full(addr, bytes, i);
166 static __always_inline __must_check
167 size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
169 if (unlikely(!check_copy_size(addr, bytes, false)))
172 return _copy_from_iter_nocache(addr, bytes, i);
175 static __always_inline __must_check
176 bool copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
178 if (unlikely(!check_copy_size(addr, bytes, false)))
181 return _copy_from_iter_full_nocache(addr, bytes, i);
184 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
186 * Note, users like pmem that depend on the stricter semantics of
187 * copy_from_iter_flushcache() than copy_from_iter_nocache() must check for
188 * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the
189 * destination is flushed from the cache on return.
191 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i);
193 #define _copy_from_iter_flushcache _copy_from_iter_nocache
196 #ifdef CONFIG_ARCH_HAS_COPY_MC
197 size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
199 #define _copy_mc_to_iter _copy_to_iter
202 static __always_inline __must_check
203 size_t copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
205 if (unlikely(!check_copy_size(addr, bytes, false)))
208 return _copy_from_iter_flushcache(addr, bytes, i);
211 static __always_inline __must_check
212 size_t copy_mc_to_iter(void *addr, size_t bytes, struct iov_iter *i)
214 if (unlikely(!check_copy_size(addr, bytes, true)))
217 return _copy_mc_to_iter(addr, bytes, i);
220 size_t iov_iter_zero(size_t bytes, struct iov_iter *);
221 unsigned long iov_iter_alignment(const struct iov_iter *i);
222 unsigned long iov_iter_gap_alignment(const struct iov_iter *i);
223 void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov,
224 unsigned long nr_segs, size_t count);
225 void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec,
226 unsigned long nr_segs, size_t count);
227 void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec,
228 unsigned long nr_segs, size_t count);
229 void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe,
231 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count);
232 void iov_iter_xarray(struct iov_iter *i, unsigned int direction, struct xarray *xarray,
233 loff_t start, size_t count);
234 ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
235 size_t maxsize, unsigned maxpages, size_t *start);
236 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages,
237 size_t maxsize, size_t *start);
238 int iov_iter_npages(const struct iov_iter *i, int maxpages);
240 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags);
242 static inline size_t iov_iter_count(const struct iov_iter *i)
248 * Cap the iov_iter by given limit; note that the second argument is
249 * *not* the new size - it's upper limit for such. Passing it a value
250 * greater than the amount of data in iov_iter is fine - it'll just do
251 * nothing in that case.
253 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
256 * count doesn't have to fit in size_t - comparison extends both
257 * operands to u64 here and any value that would be truncated by
258 * conversion in assignement is by definition greater than all
259 * values of size_t, including old i->count.
261 if (i->count > count)
266 * reexpand a previously truncated iterator; count must be no more than how much
269 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
279 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csstate, struct iov_iter *i);
280 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
281 bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
282 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
285 struct iovec *iovec_from_user(const struct iovec __user *uvector,
286 unsigned long nr_segs, unsigned long fast_segs,
287 struct iovec *fast_iov, bool compat);
288 ssize_t import_iovec(int type, const struct iovec __user *uvec,
289 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
291 ssize_t __import_iovec(int type, const struct iovec __user *uvec,
292 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
293 struct iov_iter *i, bool compat);
294 int import_single_range(int type, void __user *buf, size_t len,
295 struct iovec *iov, struct iov_iter *i);
297 int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
298 int (*f)(struct kvec *vec, void *context),