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 <crypto/hash.h>
11 #include <uapi/linux/uio.h>
14 struct pipe_inode_info;
17 void *iov_base; /* and that should *never* hold a userland pointer */
22 /* set if ITER_BVEC doesn't hold a bv_page ref */
23 ITER_BVEC_FLAG_NO_REF = 2,
35 * Bit 0 is the read/write bit, set if we're writing.
36 * Bit 1 is the BVEC_FLAG_NO_REF bit, set if type is a bvec and
37 * the caller isn't expecting to drop a page reference when done.
43 const struct iovec *iov;
44 const struct kvec *kvec;
45 const struct bio_vec *bvec;
46 struct pipe_inode_info *pipe;
49 unsigned long nr_segs;
57 static inline enum iter_type iov_iter_type(const struct iov_iter *i)
59 return i->type & ~(READ | WRITE | ITER_BVEC_FLAG_NO_REF);
62 static inline bool iter_is_iovec(const struct iov_iter *i)
64 return iov_iter_type(i) == ITER_IOVEC;
67 static inline bool iov_iter_is_kvec(const struct iov_iter *i)
69 return iov_iter_type(i) == ITER_KVEC;
72 static inline bool iov_iter_is_bvec(const struct iov_iter *i)
74 return iov_iter_type(i) == ITER_BVEC;
77 static inline bool iov_iter_is_pipe(const struct iov_iter *i)
79 return iov_iter_type(i) == ITER_PIPE;
82 static inline bool iov_iter_is_discard(const struct iov_iter *i)
84 return iov_iter_type(i) == ITER_DISCARD;
87 static inline unsigned char iov_iter_rw(const struct iov_iter *i)
89 return i->type & (READ | WRITE);
92 static inline bool iov_iter_bvec_no_ref(const struct iov_iter *i)
94 return (i->type & ITER_BVEC_FLAG_NO_REF) != 0;
98 * Total number of bytes covered by an iovec.
100 * NOTE that it is not safe to use this function until all the iovec's
101 * segment lengths have been validated. Because the individual lengths can
102 * overflow a size_t when added together.
104 static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs)
109 for (seg = 0; seg < nr_segs; seg++)
110 ret += iov[seg].iov_len;
114 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
116 return (struct iovec) {
117 .iov_base = iter->iov->iov_base + iter->iov_offset,
118 .iov_len = min(iter->count,
119 iter->iov->iov_len - iter->iov_offset),
123 size_t iov_iter_copy_from_user_atomic(struct page *page,
124 struct iov_iter *i, unsigned long offset, size_t bytes);
125 void iov_iter_advance(struct iov_iter *i, size_t bytes);
126 void iov_iter_revert(struct iov_iter *i, size_t bytes);
127 int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes);
128 size_t iov_iter_single_seg_count(const struct iov_iter *i);
129 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
131 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
134 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
135 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i);
136 bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i);
137 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i);
138 bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i);
140 static __always_inline __must_check
141 size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
143 if (unlikely(!check_copy_size(addr, bytes, true)))
146 return _copy_to_iter(addr, bytes, i);
149 static __always_inline __must_check
150 size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
152 if (unlikely(!check_copy_size(addr, bytes, false)))
155 return _copy_from_iter(addr, bytes, i);
158 static __always_inline __must_check
159 bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
161 if (unlikely(!check_copy_size(addr, bytes, false)))
164 return _copy_from_iter_full(addr, bytes, i);
167 static __always_inline __must_check
168 size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
170 if (unlikely(!check_copy_size(addr, bytes, false)))
173 return _copy_from_iter_nocache(addr, bytes, i);
176 static __always_inline __must_check
177 bool copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
179 if (unlikely(!check_copy_size(addr, bytes, false)))
182 return _copy_from_iter_full_nocache(addr, bytes, i);
185 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
187 * Note, users like pmem that depend on the stricter semantics of
188 * copy_from_iter_flushcache() than copy_from_iter_nocache() must check for
189 * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the
190 * destination is flushed from the cache on return.
192 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i);
194 #define _copy_from_iter_flushcache _copy_from_iter_nocache
197 #ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
198 size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i);
200 #define _copy_to_iter_mcsafe _copy_to_iter
203 static __always_inline __must_check
204 size_t copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
206 if (unlikely(!check_copy_size(addr, bytes, false)))
209 return _copy_from_iter_flushcache(addr, bytes, i);
212 static __always_inline __must_check
213 size_t copy_to_iter_mcsafe(void *addr, size_t bytes, struct iov_iter *i)
215 if (unlikely(!check_copy_size(addr, bytes, true)))
218 return _copy_to_iter_mcsafe(addr, bytes, i);
221 size_t iov_iter_zero(size_t bytes, struct iov_iter *);
222 unsigned long iov_iter_alignment(const struct iov_iter *i);
223 unsigned long iov_iter_gap_alignment(const struct iov_iter *i);
224 void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov,
225 unsigned long nr_segs, size_t count);
226 void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec,
227 unsigned long nr_segs, size_t count);
228 void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec,
229 unsigned long nr_segs, size_t count);
230 void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe,
232 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count);
233 ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
234 size_t maxsize, unsigned maxpages, size_t *start);
235 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages,
236 size_t maxsize, size_t *start);
237 int iov_iter_npages(const struct iov_iter *i, int maxpages);
239 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags);
241 static inline size_t iov_iter_count(const struct iov_iter *i)
247 * Cap the iov_iter by given limit; note that the second argument is
248 * *not* the new size - it's upper limit for such. Passing it a value
249 * greater than the amount of data in iov_iter is fine - it'll just do
250 * nothing in that case.
252 static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
255 * count doesn't have to fit in size_t - comparison extends both
256 * operands to u64 here and any value that would be truncated by
257 * conversion in assignement is by definition greater than all
258 * values of size_t, including old i->count.
260 if (i->count > count)
265 * reexpand a previously truncated iterator; count must be no more than how much
268 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
272 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csump, struct iov_iter *i);
273 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
274 bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
275 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
278 int import_iovec(int type, const struct iovec __user * uvector,
279 unsigned nr_segs, unsigned fast_segs,
280 struct iovec **iov, struct iov_iter *i);
284 int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
285 unsigned nr_segs, unsigned fast_segs,
286 struct iovec **iov, struct iov_iter *i);
289 int import_single_range(int type, void __user *buf, size_t len,
290 struct iovec *iov, struct iov_iter *i);
292 int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
293 int (*f)(struct kvec *vec, void *context),