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 <linux/mm_types.h>
11 #include <uapi/linux/uio.h>
16 typedef unsigned int __bitwise iov_iter_extraction_t
;
19 void *iov_base
; /* and that should *never* hold a userland pointer */
34 #define ITER_SOURCE 1 // == WRITE
35 #define ITER_DEST 0 // == READ
37 struct iov_iter_state
{
40 unsigned long nr_segs
;
49 * Hack alert: overlay ubuf_iovec with iovec + count, so
50 * that the members resolve correctly regardless of the type
51 * of iterator used. This means that you can use:
53 * &iter->__ubuf_iovec or iter->__iov
55 * interchangably for the user_backed cases, hence simplifying
56 * some of the cases that need to deal with both.
60 * This really should be a const, but we cannot do that without
61 * also modifying any of the zero-filling iter init functions.
62 * Leave it non-const for now, but it should be treated as such.
64 struct iovec __ubuf_iovec
;
67 /* use iter_iov() to get the current vec */
68 const struct iovec
*__iov
;
69 const struct kvec
*kvec
;
70 const struct bio_vec
*bvec
;
71 const struct folio_queue
*folioq
;
72 struct xarray
*xarray
;
79 unsigned long nr_segs
;
85 static inline const struct iovec
*iter_iov(const struct iov_iter
*iter
)
87 if (iter
->iter_type
== ITER_UBUF
)
88 return (const struct iovec
*) &iter
->__ubuf_iovec
;
92 #define iter_iov_addr(iter) (iter_iov(iter)->iov_base + (iter)->iov_offset)
93 #define iter_iov_len(iter) (iter_iov(iter)->iov_len - (iter)->iov_offset)
95 static inline enum iter_type
iov_iter_type(const struct iov_iter
*i
)
100 static inline void iov_iter_save_state(struct iov_iter
*iter
,
101 struct iov_iter_state
*state
)
103 state
->iov_offset
= iter
->iov_offset
;
104 state
->count
= iter
->count
;
105 state
->nr_segs
= iter
->nr_segs
;
108 static inline bool iter_is_ubuf(const struct iov_iter
*i
)
110 return iov_iter_type(i
) == ITER_UBUF
;
113 static inline bool iter_is_iovec(const struct iov_iter
*i
)
115 return iov_iter_type(i
) == ITER_IOVEC
;
118 static inline bool iov_iter_is_kvec(const struct iov_iter
*i
)
120 return iov_iter_type(i
) == ITER_KVEC
;
123 static inline bool iov_iter_is_bvec(const struct iov_iter
*i
)
125 return iov_iter_type(i
) == ITER_BVEC
;
128 static inline bool iov_iter_is_discard(const struct iov_iter
*i
)
130 return iov_iter_type(i
) == ITER_DISCARD
;
133 static inline bool iov_iter_is_folioq(const struct iov_iter
*i
)
135 return iov_iter_type(i
) == ITER_FOLIOQ
;
138 static inline bool iov_iter_is_xarray(const struct iov_iter
*i
)
140 return iov_iter_type(i
) == ITER_XARRAY
;
143 static inline unsigned char iov_iter_rw(const struct iov_iter
*i
)
145 return i
->data_source
? WRITE
: READ
;
148 static inline bool user_backed_iter(const struct iov_iter
*i
)
150 return iter_is_ubuf(i
) || iter_is_iovec(i
);
154 * Total number of bytes covered by an iovec.
156 * NOTE that it is not safe to use this function until all the iovec's
157 * segment lengths have been validated. Because the individual lengths can
158 * overflow a size_t when added together.
160 static inline size_t iov_length(const struct iovec
*iov
, unsigned long nr_segs
)
165 for (seg
= 0; seg
< nr_segs
; seg
++)
166 ret
+= iov
[seg
].iov_len
;
170 size_t copy_page_from_iter_atomic(struct page
*page
, size_t offset
,
171 size_t bytes
, struct iov_iter
*i
);
172 void iov_iter_advance(struct iov_iter
*i
, size_t bytes
);
173 void iov_iter_revert(struct iov_iter
*i
, size_t bytes
);
174 size_t fault_in_iov_iter_readable(const struct iov_iter
*i
, size_t bytes
);
175 size_t fault_in_iov_iter_writeable(const struct iov_iter
*i
, size_t bytes
);
176 size_t iov_iter_single_seg_count(const struct iov_iter
*i
);
177 size_t copy_page_to_iter(struct page
*page
, size_t offset
, size_t bytes
,
179 size_t copy_page_from_iter(struct page
*page
, size_t offset
, size_t bytes
,
182 size_t _copy_to_iter(const void *addr
, size_t bytes
, struct iov_iter
*i
);
183 size_t _copy_from_iter(void *addr
, size_t bytes
, struct iov_iter
*i
);
184 size_t _copy_from_iter_nocache(void *addr
, size_t bytes
, struct iov_iter
*i
);
186 static inline size_t copy_folio_to_iter(struct folio
*folio
, size_t offset
,
187 size_t bytes
, struct iov_iter
*i
)
189 return copy_page_to_iter(&folio
->page
, offset
, bytes
, i
);
192 static inline size_t copy_folio_from_iter(struct folio
*folio
, size_t offset
,
193 size_t bytes
, struct iov_iter
*i
)
195 return copy_page_from_iter(&folio
->page
, offset
, bytes
, i
);
198 static inline size_t copy_folio_from_iter_atomic(struct folio
*folio
,
199 size_t offset
, size_t bytes
, struct iov_iter
*i
)
201 return copy_page_from_iter_atomic(&folio
->page
, offset
, bytes
, i
);
204 size_t copy_page_to_iter_nofault(struct page
*page
, unsigned offset
,
205 size_t bytes
, struct iov_iter
*i
);
207 static __always_inline __must_check
208 size_t copy_to_iter(const void *addr
, size_t bytes
, struct iov_iter
*i
)
210 if (check_copy_size(addr
, bytes
, true))
211 return _copy_to_iter(addr
, bytes
, i
);
215 static __always_inline __must_check
216 size_t copy_from_iter(void *addr
, size_t bytes
, struct iov_iter
*i
)
218 if (check_copy_size(addr
, bytes
, false))
219 return _copy_from_iter(addr
, bytes
, i
);
223 static __always_inline __must_check
224 bool copy_to_iter_full(const void *addr
, size_t bytes
, struct iov_iter
*i
)
226 size_t copied
= copy_to_iter(addr
, bytes
, i
);
227 if (likely(copied
== bytes
))
229 iov_iter_revert(i
, copied
);
233 static __always_inline __must_check
234 bool copy_from_iter_full(void *addr
, size_t bytes
, struct iov_iter
*i
)
236 size_t copied
= copy_from_iter(addr
, bytes
, i
);
237 if (likely(copied
== bytes
))
239 iov_iter_revert(i
, copied
);
243 static __always_inline __must_check
244 size_t copy_from_iter_nocache(void *addr
, size_t bytes
, struct iov_iter
*i
)
246 if (check_copy_size(addr
, bytes
, false))
247 return _copy_from_iter_nocache(addr
, bytes
, i
);
251 static __always_inline __must_check
252 bool copy_from_iter_full_nocache(void *addr
, size_t bytes
, struct iov_iter
*i
)
254 size_t copied
= copy_from_iter_nocache(addr
, bytes
, i
);
255 if (likely(copied
== bytes
))
257 iov_iter_revert(i
, copied
);
261 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
263 * Note, users like pmem that depend on the stricter semantics of
264 * _copy_from_iter_flushcache() than _copy_from_iter_nocache() must check for
265 * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the
266 * destination is flushed from the cache on return.
268 size_t _copy_from_iter_flushcache(void *addr
, size_t bytes
, struct iov_iter
*i
);
270 #define _copy_from_iter_flushcache _copy_from_iter_nocache
273 #ifdef CONFIG_ARCH_HAS_COPY_MC
274 size_t _copy_mc_to_iter(const void *addr
, size_t bytes
, struct iov_iter
*i
);
276 #define _copy_mc_to_iter _copy_to_iter
279 size_t iov_iter_zero(size_t bytes
, struct iov_iter
*);
280 bool iov_iter_is_aligned(const struct iov_iter
*i
, unsigned addr_mask
,
282 unsigned long iov_iter_alignment(const struct iov_iter
*i
);
283 unsigned long iov_iter_gap_alignment(const struct iov_iter
*i
);
284 void iov_iter_init(struct iov_iter
*i
, unsigned int direction
, const struct iovec
*iov
,
285 unsigned long nr_segs
, size_t count
);
286 void iov_iter_kvec(struct iov_iter
*i
, unsigned int direction
, const struct kvec
*kvec
,
287 unsigned long nr_segs
, size_t count
);
288 void iov_iter_bvec(struct iov_iter
*i
, unsigned int direction
, const struct bio_vec
*bvec
,
289 unsigned long nr_segs
, size_t count
);
290 void iov_iter_discard(struct iov_iter
*i
, unsigned int direction
, size_t count
);
291 void iov_iter_folio_queue(struct iov_iter
*i
, unsigned int direction
,
292 const struct folio_queue
*folioq
,
293 unsigned int first_slot
, unsigned int offset
, size_t count
);
294 void iov_iter_xarray(struct iov_iter
*i
, unsigned int direction
, struct xarray
*xarray
,
295 loff_t start
, size_t count
);
296 ssize_t
iov_iter_get_pages2(struct iov_iter
*i
, struct page
**pages
,
297 size_t maxsize
, unsigned maxpages
, size_t *start
);
298 ssize_t
iov_iter_get_pages_alloc2(struct iov_iter
*i
, struct page
***pages
,
299 size_t maxsize
, size_t *start
);
300 int iov_iter_npages(const struct iov_iter
*i
, int maxpages
);
301 void iov_iter_restore(struct iov_iter
*i
, struct iov_iter_state
*state
);
303 const void *dup_iter(struct iov_iter
*new, struct iov_iter
*old
, gfp_t flags
);
305 static inline size_t iov_iter_count(const struct iov_iter
*i
)
311 * Cap the iov_iter by given limit; note that the second argument is
312 * *not* the new size - it's upper limit for such. Passing it a value
313 * greater than the amount of data in iov_iter is fine - it'll just do
314 * nothing in that case.
316 static inline void iov_iter_truncate(struct iov_iter
*i
, u64 count
)
319 * count doesn't have to fit in size_t - comparison extends both
320 * operands to u64 here and any value that would be truncated by
321 * conversion in assignement is by definition greater than all
322 * values of size_t, including old i->count.
324 if (i
->count
> count
)
329 * reexpand a previously truncated iterator; count must be no more than how much
332 static inline void iov_iter_reexpand(struct iov_iter
*i
, size_t count
)
338 iov_iter_npages_cap(struct iov_iter
*i
, int maxpages
, size_t max_bytes
)
343 if (iov_iter_count(i
) > max_bytes
) {
344 shorted
= iov_iter_count(i
) - max_bytes
;
345 iov_iter_truncate(i
, max_bytes
);
347 npages
= iov_iter_npages(i
, maxpages
);
349 iov_iter_reexpand(i
, iov_iter_count(i
) + shorted
);
354 struct iovec
*iovec_from_user(const struct iovec __user
*uvector
,
355 unsigned long nr_segs
, unsigned long fast_segs
,
356 struct iovec
*fast_iov
, bool compat
);
357 ssize_t
import_iovec(int type
, const struct iovec __user
*uvec
,
358 unsigned nr_segs
, unsigned fast_segs
, struct iovec
**iovp
,
360 ssize_t
__import_iovec(int type
, const struct iovec __user
*uvec
,
361 unsigned nr_segs
, unsigned fast_segs
, struct iovec
**iovp
,
362 struct iov_iter
*i
, bool compat
);
363 int import_ubuf(int type
, void __user
*buf
, size_t len
, struct iov_iter
*i
);
365 static inline void iov_iter_ubuf(struct iov_iter
*i
, unsigned int direction
,
366 void __user
*buf
, size_t count
)
368 WARN_ON(direction
& ~(READ
| WRITE
));
369 *i
= (struct iov_iter
) {
370 .iter_type
= ITER_UBUF
,
371 .data_source
= direction
,
377 /* Flags for iov_iter_get/extract_pages*() */
378 /* Allow P2PDMA on the extracted pages */
379 #define ITER_ALLOW_P2PDMA ((__force iov_iter_extraction_t)0x01)
381 ssize_t
iov_iter_extract_pages(struct iov_iter
*i
, struct page
***pages
,
382 size_t maxsize
, unsigned int maxpages
,
383 iov_iter_extraction_t extraction_flags
,
387 * iov_iter_extract_will_pin - Indicate how pages from the iterator will be retained
388 * @iter: The iterator
390 * Examine the iterator and indicate by returning true or false as to how, if
391 * at all, pages extracted from the iterator will be retained by the extraction
394 * %true indicates that the pages will have a pin placed in them that the
395 * caller must unpin. This is must be done for DMA/async DIO to force fork()
396 * to forcibly copy a page for the child (the parent must retain the original
399 * %false indicates that no measures are taken and that it's up to the caller
400 * to retain the pages.
402 static inline bool iov_iter_extract_will_pin(const struct iov_iter
*iter
)
404 return user_backed_iter(iter
);
408 ssize_t
extract_iter_to_sg(struct iov_iter
*iter
, size_t len
,
409 struct sg_table
*sgtable
, unsigned int sg_max
,
410 iov_iter_extraction_t extraction_flags
);