1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 27999096fSHerbert Xu #include <crypto/hash.h> 3d879cb83SAl Viro #include <linux/export.h> 42f8b5444SChristoph Hellwig #include <linux/bvec.h> 54d0e9df5SAlbert van der Linde #include <linux/fault-inject-usercopy.h> 6d879cb83SAl Viro #include <linux/uio.h> 7d879cb83SAl Viro #include <linux/pagemap.h> 828961998SIra Weiny #include <linux/highmem.h> 9d879cb83SAl Viro #include <linux/slab.h> 10d879cb83SAl Viro #include <linux/vmalloc.h> 11241699cdSAl Viro #include <linux/splice.h> 12bfdc5970SChristoph Hellwig #include <linux/compat.h> 13d879cb83SAl Viro #include <net/checksum.h> 14d05f4435SSagi Grimberg #include <linux/scatterlist.h> 15d0ef4c36SMarco Elver #include <linux/instrumented.h> 16d879cb83SAl Viro 17241699cdSAl Viro #define PIPE_PARANOIA /* for now */ 18241699cdSAl Viro 19fcb14cb1SAl Viro /* covers ubuf and kbuf alike */ 20fcb14cb1SAl Viro #define iterate_buf(i, n, base, len, off, __p, STEP) { \ 21fcb14cb1SAl Viro size_t __maybe_unused off = 0; \ 22fcb14cb1SAl Viro len = n; \ 23fcb14cb1SAl Viro base = __p + i->iov_offset; \ 24fcb14cb1SAl Viro len -= (STEP); \ 25fcb14cb1SAl Viro i->iov_offset += len; \ 26fcb14cb1SAl Viro n = len; \ 27fcb14cb1SAl Viro } 28fcb14cb1SAl Viro 295c67aa90SAl Viro /* covers iovec and kvec alike */ 30a6e4ec7bSAl Viro #define iterate_iovec(i, n, base, len, off, __p, STEP) { \ 317baa5099SAl Viro size_t off = 0; \ 32a6e4ec7bSAl Viro size_t skip = i->iov_offset; \ 337a1bcb5dSAl Viro do { \ 347baa5099SAl Viro len = min(n, __p->iov_len - skip); \ 357baa5099SAl Viro if (likely(len)) { \ 367baa5099SAl Viro base = __p->iov_base + skip; \ 377baa5099SAl Viro len -= (STEP); \ 387baa5099SAl Viro off += len; \ 397baa5099SAl Viro skip += len; \ 407baa5099SAl Viro n -= len; \ 417a1bcb5dSAl Viro if (skip < __p->iov_len) \ 427a1bcb5dSAl Viro break; \ 43d879cb83SAl Viro } \ 44d879cb83SAl Viro __p++; \ 457a1bcb5dSAl Viro skip = 0; \ 467a1bcb5dSAl Viro } while (n); \ 47a6e4ec7bSAl Viro i->iov_offset = skip; \ 487baa5099SAl Viro n = off; \ 49d879cb83SAl Viro } 50d879cb83SAl Viro 51a6e4ec7bSAl Viro #define iterate_bvec(i, n, base, len, off, p, STEP) { \ 527baa5099SAl Viro size_t off = 0; \ 53a6e4ec7bSAl Viro unsigned skip = i->iov_offset; \ 547491a2bfSAl Viro while (n) { \ 557491a2bfSAl Viro unsigned offset = p->bv_offset + skip; \ 561b4fb5ffSAl Viro unsigned left; \ 5721b56c84SAl Viro void *kaddr = kmap_local_page(p->bv_page + \ 5821b56c84SAl Viro offset / PAGE_SIZE); \ 597baa5099SAl Viro base = kaddr + offset % PAGE_SIZE; \ 60a6e4ec7bSAl Viro len = min(min(n, (size_t)(p->bv_len - skip)), \ 617491a2bfSAl Viro (size_t)(PAGE_SIZE - offset % PAGE_SIZE)); \ 621b4fb5ffSAl Viro left = (STEP); \ 6321b56c84SAl Viro kunmap_local(kaddr); \ 647baa5099SAl Viro len -= left; \ 657baa5099SAl Viro off += len; \ 667baa5099SAl Viro skip += len; \ 677491a2bfSAl Viro if (skip == p->bv_len) { \ 687491a2bfSAl Viro skip = 0; \ 697491a2bfSAl Viro p++; \ 70d879cb83SAl Viro } \ 717baa5099SAl Viro n -= len; \ 721b4fb5ffSAl Viro if (left) \ 731b4fb5ffSAl Viro break; \ 747491a2bfSAl Viro } \ 75a6e4ec7bSAl Viro i->iov_offset = skip; \ 767baa5099SAl Viro n = off; \ 77d879cb83SAl Viro } 78d879cb83SAl Viro 79a6e4ec7bSAl Viro #define iterate_xarray(i, n, base, len, __off, STEP) { \ 801b4fb5ffSAl Viro __label__ __out; \ 81622838f3SAl Viro size_t __off = 0; \ 82821979f5SMatthew Wilcox (Oracle) struct folio *folio; \ 83a6e4ec7bSAl Viro loff_t start = i->xarray_start + i->iov_offset; \ 844b179e9aSAl Viro pgoff_t index = start / PAGE_SIZE; \ 857ff50620SDavid Howells XA_STATE(xas, i->xarray, index); \ 867ff50620SDavid Howells \ 87821979f5SMatthew Wilcox (Oracle) len = PAGE_SIZE - offset_in_page(start); \ 887ff50620SDavid Howells rcu_read_lock(); \ 89821979f5SMatthew Wilcox (Oracle) xas_for_each(&xas, folio, ULONG_MAX) { \ 901b4fb5ffSAl Viro unsigned left; \ 91821979f5SMatthew Wilcox (Oracle) size_t offset; \ 92821979f5SMatthew Wilcox (Oracle) if (xas_retry(&xas, folio)) \ 937ff50620SDavid Howells continue; \ 94821979f5SMatthew Wilcox (Oracle) if (WARN_ON(xa_is_value(folio))) \ 957ff50620SDavid Howells break; \ 96821979f5SMatthew Wilcox (Oracle) if (WARN_ON(folio_test_hugetlb(folio))) \ 977ff50620SDavid Howells break; \ 98821979f5SMatthew Wilcox (Oracle) offset = offset_in_folio(folio, start + __off); \ 99821979f5SMatthew Wilcox (Oracle) while (offset < folio_size(folio)) { \ 100821979f5SMatthew Wilcox (Oracle) base = kmap_local_folio(folio, offset); \ 1017baa5099SAl Viro len = min(n, len); \ 1021b4fb5ffSAl Viro left = (STEP); \ 103821979f5SMatthew Wilcox (Oracle) kunmap_local(base); \ 1047baa5099SAl Viro len -= left; \ 1057baa5099SAl Viro __off += len; \ 1067baa5099SAl Viro n -= len; \ 1071b4fb5ffSAl Viro if (left || n == 0) \ 1081b4fb5ffSAl Viro goto __out; \ 109821979f5SMatthew Wilcox (Oracle) offset += len; \ 110821979f5SMatthew Wilcox (Oracle) len = PAGE_SIZE; \ 1117ff50620SDavid Howells } \ 1127ff50620SDavid Howells } \ 1131b4fb5ffSAl Viro __out: \ 1147ff50620SDavid Howells rcu_read_unlock(); \ 115a6e4ec7bSAl Viro i->iov_offset += __off; \ 116622838f3SAl Viro n = __off; \ 1177ff50620SDavid Howells } 1187ff50620SDavid Howells 1197baa5099SAl Viro #define __iterate_and_advance(i, n, base, len, off, I, K) { \ 120dd254f5aSAl Viro if (unlikely(i->count < n)) \ 121dd254f5aSAl Viro n = i->count; \ 122f5da8354SAl Viro if (likely(n)) { \ 123fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { \ 124fcb14cb1SAl Viro void __user *base; \ 125fcb14cb1SAl Viro size_t len; \ 126fcb14cb1SAl Viro iterate_buf(i, n, base, len, off, \ 127fcb14cb1SAl Viro i->ubuf, (I)) \ 128fcb14cb1SAl Viro } else if (likely(iter_is_iovec(i))) { \ 1295c67aa90SAl Viro const struct iovec *iov = i->iov; \ 1307baa5099SAl Viro void __user *base; \ 1317baa5099SAl Viro size_t len; \ 1327baa5099SAl Viro iterate_iovec(i, n, base, len, off, \ 133a6e4ec7bSAl Viro iov, (I)) \ 134d879cb83SAl Viro i->nr_segs -= iov - i->iov; \ 135d879cb83SAl Viro i->iov = iov; \ 13628f38db7SAl Viro } else if (iov_iter_is_bvec(i)) { \ 13728f38db7SAl Viro const struct bio_vec *bvec = i->bvec; \ 1387baa5099SAl Viro void *base; \ 1397baa5099SAl Viro size_t len; \ 1407baa5099SAl Viro iterate_bvec(i, n, base, len, off, \ 141a6e4ec7bSAl Viro bvec, (K)) \ 1427491a2bfSAl Viro i->nr_segs -= bvec - i->bvec; \ 1437491a2bfSAl Viro i->bvec = bvec; \ 14428f38db7SAl Viro } else if (iov_iter_is_kvec(i)) { \ 1455c67aa90SAl Viro const struct kvec *kvec = i->kvec; \ 1467baa5099SAl Viro void *base; \ 1477baa5099SAl Viro size_t len; \ 1487baa5099SAl Viro iterate_iovec(i, n, base, len, off, \ 149a6e4ec7bSAl Viro kvec, (K)) \ 15028f38db7SAl Viro i->nr_segs -= kvec - i->kvec; \ 15128f38db7SAl Viro i->kvec = kvec; \ 15228f38db7SAl Viro } else if (iov_iter_is_xarray(i)) { \ 1537baa5099SAl Viro void *base; \ 1547baa5099SAl Viro size_t len; \ 1557baa5099SAl Viro iterate_xarray(i, n, base, len, off, \ 156a6e4ec7bSAl Viro (K)) \ 157d879cb83SAl Viro } \ 158d879cb83SAl Viro i->count -= n; \ 159dd254f5aSAl Viro } \ 160d879cb83SAl Viro } 1617baa5099SAl Viro #define iterate_and_advance(i, n, base, len, off, I, K) \ 1627baa5099SAl Viro __iterate_and_advance(i, n, base, len, off, I, ((void)(K),0)) 163d879cb83SAl Viro 16409fc68dcSAl Viro static int copyout(void __user *to, const void *from, size_t n) 16509fc68dcSAl Viro { 1664d0e9df5SAlbert van der Linde if (should_fail_usercopy()) 1674d0e9df5SAlbert van der Linde return n; 16896d4f267SLinus Torvalds if (access_ok(to, n)) { 169d0ef4c36SMarco Elver instrument_copy_to_user(to, from, n); 17009fc68dcSAl Viro n = raw_copy_to_user(to, from, n); 17109fc68dcSAl Viro } 17209fc68dcSAl Viro return n; 17309fc68dcSAl Viro } 17409fc68dcSAl Viro 17509fc68dcSAl Viro static int copyin(void *to, const void __user *from, size_t n) 17609fc68dcSAl Viro { 1774d0e9df5SAlbert van der Linde if (should_fail_usercopy()) 1784d0e9df5SAlbert van der Linde return n; 17996d4f267SLinus Torvalds if (access_ok(from, n)) { 180d0ef4c36SMarco Elver instrument_copy_from_user(to, from, n); 18109fc68dcSAl Viro n = raw_copy_from_user(to, from, n); 18209fc68dcSAl Viro } 18309fc68dcSAl Viro return n; 18409fc68dcSAl Viro } 18509fc68dcSAl Viro 1862dcedb2aSAl Viro static inline struct pipe_buffer *pipe_buf(const struct pipe_inode_info *pipe, 1872dcedb2aSAl Viro unsigned int slot) 1882dcedb2aSAl Viro { 1892dcedb2aSAl Viro return &pipe->bufs[slot & (pipe->ring_size - 1)]; 1902dcedb2aSAl Viro } 1912dcedb2aSAl Viro 192241699cdSAl Viro #ifdef PIPE_PARANOIA 193241699cdSAl Viro static bool sanity(const struct iov_iter *i) 194241699cdSAl Viro { 195241699cdSAl Viro struct pipe_inode_info *pipe = i->pipe; 1968cefc107SDavid Howells unsigned int p_head = pipe->head; 1978cefc107SDavid Howells unsigned int p_tail = pipe->tail; 1988cefc107SDavid Howells unsigned int p_occupancy = pipe_occupancy(p_head, p_tail); 1998cefc107SDavid Howells unsigned int i_head = i->head; 2008cefc107SDavid Howells unsigned int idx; 2018cefc107SDavid Howells 202241699cdSAl Viro if (i->iov_offset) { 203241699cdSAl Viro struct pipe_buffer *p; 2048cefc107SDavid Howells if (unlikely(p_occupancy == 0)) 205241699cdSAl Viro goto Bad; // pipe must be non-empty 2068cefc107SDavid Howells if (unlikely(i_head != p_head - 1)) 207241699cdSAl Viro goto Bad; // must be at the last buffer... 208241699cdSAl Viro 2092dcedb2aSAl Viro p = pipe_buf(pipe, i_head); 210241699cdSAl Viro if (unlikely(p->offset + p->len != i->iov_offset)) 211241699cdSAl Viro goto Bad; // ... at the end of segment 212241699cdSAl Viro } else { 2138cefc107SDavid Howells if (i_head != p_head) 214241699cdSAl Viro goto Bad; // must be right after the last buffer 215241699cdSAl Viro } 216241699cdSAl Viro return true; 217241699cdSAl Viro Bad: 2188cefc107SDavid Howells printk(KERN_ERR "idx = %d, offset = %zd\n", i_head, i->iov_offset); 2198cefc107SDavid Howells printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n", 2208cefc107SDavid Howells p_head, p_tail, pipe->ring_size); 2218cefc107SDavid Howells for (idx = 0; idx < pipe->ring_size; idx++) 222241699cdSAl Viro printk(KERN_ERR "[%p %p %d %d]\n", 223241699cdSAl Viro pipe->bufs[idx].ops, 224241699cdSAl Viro pipe->bufs[idx].page, 225241699cdSAl Viro pipe->bufs[idx].offset, 226241699cdSAl Viro pipe->bufs[idx].len); 227241699cdSAl Viro WARN_ON(1); 228241699cdSAl Viro return false; 229241699cdSAl Viro } 230241699cdSAl Viro #else 231241699cdSAl Viro #define sanity(i) true 232241699cdSAl Viro #endif 233241699cdSAl Viro 23447b7fcaeSAl Viro static struct page *push_anon(struct pipe_inode_info *pipe, unsigned size) 23547b7fcaeSAl Viro { 23647b7fcaeSAl Viro struct page *page = alloc_page(GFP_USER); 23747b7fcaeSAl Viro if (page) { 23847b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++); 23947b7fcaeSAl Viro *buf = (struct pipe_buffer) { 24047b7fcaeSAl Viro .ops = &default_pipe_buf_ops, 24147b7fcaeSAl Viro .page = page, 24247b7fcaeSAl Viro .offset = 0, 24347b7fcaeSAl Viro .len = size 24447b7fcaeSAl Viro }; 24547b7fcaeSAl Viro } 24647b7fcaeSAl Viro return page; 24747b7fcaeSAl Viro } 24847b7fcaeSAl Viro 24947b7fcaeSAl Viro static void push_page(struct pipe_inode_info *pipe, struct page *page, 25047b7fcaeSAl Viro unsigned int offset, unsigned int size) 25147b7fcaeSAl Viro { 25247b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++); 25347b7fcaeSAl Viro *buf = (struct pipe_buffer) { 25447b7fcaeSAl Viro .ops = &page_cache_pipe_buf_ops, 25547b7fcaeSAl Viro .page = page, 25647b7fcaeSAl Viro .offset = offset, 25747b7fcaeSAl Viro .len = size 25847b7fcaeSAl Viro }; 25947b7fcaeSAl Viro get_page(page); 26047b7fcaeSAl Viro } 26147b7fcaeSAl Viro 2628fad7767SAl Viro static inline bool allocated(struct pipe_buffer *buf) 2638fad7767SAl Viro { 2648fad7767SAl Viro return buf->ops == &default_pipe_buf_ops; 2658fad7767SAl Viro } 2668fad7767SAl Viro 2678fad7767SAl Viro static struct page *append_pipe(struct iov_iter *i, size_t size, 2688fad7767SAl Viro unsigned int *off) 2698fad7767SAl Viro { 2708fad7767SAl Viro struct pipe_inode_info *pipe = i->pipe; 2718fad7767SAl Viro size_t offset = i->iov_offset; 2728fad7767SAl Viro struct pipe_buffer *buf; 2738fad7767SAl Viro struct page *page; 2748fad7767SAl Viro 2758fad7767SAl Viro if (offset && offset < PAGE_SIZE) { 2768fad7767SAl Viro // some space in the last buffer; can we add to it? 2778fad7767SAl Viro buf = pipe_buf(pipe, pipe->head - 1); 2788fad7767SAl Viro if (allocated(buf)) { 2798fad7767SAl Viro size = min_t(size_t, size, PAGE_SIZE - offset); 2808fad7767SAl Viro buf->len += size; 2818fad7767SAl Viro i->iov_offset += size; 2828fad7767SAl Viro i->count -= size; 2838fad7767SAl Viro *off = offset; 2848fad7767SAl Viro return buf->page; 2858fad7767SAl Viro } 2868fad7767SAl Viro } 2878fad7767SAl Viro // OK, we need a new buffer 2888fad7767SAl Viro *off = 0; 2898fad7767SAl Viro size = min_t(size_t, size, PAGE_SIZE); 2908fad7767SAl Viro if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 2918fad7767SAl Viro return NULL; 2928fad7767SAl Viro page = push_anon(pipe, size); 2938fad7767SAl Viro if (!page) 2948fad7767SAl Viro return NULL; 2958fad7767SAl Viro i->head = pipe->head - 1; 2968fad7767SAl Viro i->iov_offset = size; 2978fad7767SAl Viro i->count -= size; 2988fad7767SAl Viro return page; 2998fad7767SAl Viro } 3008fad7767SAl Viro 301241699cdSAl Viro static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes, 302241699cdSAl Viro struct iov_iter *i) 303241699cdSAl Viro { 304241699cdSAl Viro struct pipe_inode_info *pipe = i->pipe; 30547b7fcaeSAl Viro unsigned int head = pipe->head; 306241699cdSAl Viro 307241699cdSAl Viro if (unlikely(bytes > i->count)) 308241699cdSAl Viro bytes = i->count; 309241699cdSAl Viro 310241699cdSAl Viro if (unlikely(!bytes)) 311241699cdSAl Viro return 0; 312241699cdSAl Viro 313241699cdSAl Viro if (!sanity(i)) 314241699cdSAl Viro return 0; 315241699cdSAl Viro 31647b7fcaeSAl Viro if (offset && i->iov_offset == offset) { // could we merge it? 31747b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, head - 1); 31847b7fcaeSAl Viro if (buf->page == page) { 319241699cdSAl Viro buf->len += bytes; 320241699cdSAl Viro i->iov_offset += bytes; 32147b7fcaeSAl Viro i->count -= bytes; 32247b7fcaeSAl Viro return bytes; 323241699cdSAl Viro } 324241699cdSAl Viro } 32547b7fcaeSAl Viro if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 326241699cdSAl Viro return 0; 3278cefc107SDavid Howells 32847b7fcaeSAl Viro push_page(pipe, page, offset, bytes); 329241699cdSAl Viro i->iov_offset = offset + bytes; 33047b7fcaeSAl Viro i->head = head; 331241699cdSAl Viro i->count -= bytes; 332241699cdSAl Viro return bytes; 333241699cdSAl Viro } 334241699cdSAl Viro 335d879cb83SAl Viro /* 336a6294593SAndreas Gruenbacher * fault_in_iov_iter_readable - fault in iov iterator for reading 337a6294593SAndreas Gruenbacher * @i: iterator 338a6294593SAndreas Gruenbacher * @size: maximum length 339171a0203SAnton Altaparmakov * 340a6294593SAndreas Gruenbacher * Fault in one or more iovecs of the given iov_iter, to a maximum length of 341a6294593SAndreas Gruenbacher * @size. For each iovec, fault in each page that constitutes the iovec. 342a6294593SAndreas Gruenbacher * 343a6294593SAndreas Gruenbacher * Returns the number of bytes not faulted in (like copy_to_user() and 344a6294593SAndreas Gruenbacher * copy_from_user()). 345a6294593SAndreas Gruenbacher * 346a6294593SAndreas Gruenbacher * Always returns 0 for non-userspace iterators. 347171a0203SAnton Altaparmakov */ 348a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size) 349171a0203SAnton Altaparmakov { 350fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 351fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 352fcb14cb1SAl Viro n -= fault_in_readable(i->ubuf + i->iov_offset, n); 353fcb14cb1SAl Viro return size - n; 354fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 355a6294593SAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 3568409a0d2SAl Viro const struct iovec *p; 3578409a0d2SAl Viro size_t skip; 3588409a0d2SAl Viro 359a6294593SAndreas Gruenbacher size -= count; 360a6294593SAndreas Gruenbacher for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) { 361a6294593SAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 362a6294593SAndreas Gruenbacher size_t ret; 3638409a0d2SAl Viro 3648409a0d2SAl Viro if (unlikely(!len)) 3658409a0d2SAl Viro continue; 366a6294593SAndreas Gruenbacher ret = fault_in_readable(p->iov_base + skip, len); 367a6294593SAndreas Gruenbacher count -= len - ret; 368a6294593SAndreas Gruenbacher if (ret) 369a6294593SAndreas Gruenbacher break; 3708409a0d2SAl Viro } 371a6294593SAndreas Gruenbacher return count + size; 372171a0203SAnton Altaparmakov } 373171a0203SAnton Altaparmakov return 0; 374171a0203SAnton Altaparmakov } 375a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable); 376171a0203SAnton Altaparmakov 377cdd591fcSAndreas Gruenbacher /* 378cdd591fcSAndreas Gruenbacher * fault_in_iov_iter_writeable - fault in iov iterator for writing 379cdd591fcSAndreas Gruenbacher * @i: iterator 380cdd591fcSAndreas Gruenbacher * @size: maximum length 381cdd591fcSAndreas Gruenbacher * 382cdd591fcSAndreas Gruenbacher * Faults in the iterator using get_user_pages(), i.e., without triggering 383cdd591fcSAndreas Gruenbacher * hardware page faults. This is primarily useful when we already know that 384cdd591fcSAndreas Gruenbacher * some or all of the pages in @i aren't in memory. 385cdd591fcSAndreas Gruenbacher * 386cdd591fcSAndreas Gruenbacher * Returns the number of bytes not faulted in, like copy_to_user() and 387cdd591fcSAndreas Gruenbacher * copy_from_user(). 388cdd591fcSAndreas Gruenbacher * 389cdd591fcSAndreas Gruenbacher * Always returns 0 for non-user-space iterators. 390cdd591fcSAndreas Gruenbacher */ 391cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size) 392cdd591fcSAndreas Gruenbacher { 393fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 394fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 395fcb14cb1SAl Viro n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n); 396fcb14cb1SAl Viro return size - n; 397fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 398cdd591fcSAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 399cdd591fcSAndreas Gruenbacher const struct iovec *p; 400cdd591fcSAndreas Gruenbacher size_t skip; 401cdd591fcSAndreas Gruenbacher 402cdd591fcSAndreas Gruenbacher size -= count; 403cdd591fcSAndreas Gruenbacher for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) { 404cdd591fcSAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 405cdd591fcSAndreas Gruenbacher size_t ret; 406cdd591fcSAndreas Gruenbacher 407cdd591fcSAndreas Gruenbacher if (unlikely(!len)) 408cdd591fcSAndreas Gruenbacher continue; 409cdd591fcSAndreas Gruenbacher ret = fault_in_safe_writeable(p->iov_base + skip, len); 410cdd591fcSAndreas Gruenbacher count -= len - ret; 411cdd591fcSAndreas Gruenbacher if (ret) 412cdd591fcSAndreas Gruenbacher break; 413cdd591fcSAndreas Gruenbacher } 414cdd591fcSAndreas Gruenbacher return count + size; 415cdd591fcSAndreas Gruenbacher } 416cdd591fcSAndreas Gruenbacher return 0; 417cdd591fcSAndreas Gruenbacher } 418cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable); 419cdd591fcSAndreas Gruenbacher 420aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction, 421d879cb83SAl Viro const struct iovec *iov, unsigned long nr_segs, 422d879cb83SAl Viro size_t count) 423d879cb83SAl Viro { 424aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 4258cd54c1cSAl Viro *i = (struct iov_iter) { 4268cd54c1cSAl Viro .iter_type = ITER_IOVEC, 4273337ab08SAndreas Gruenbacher .nofault = false, 428fcb14cb1SAl Viro .user_backed = true, 4298cd54c1cSAl Viro .data_source = direction, 4308cd54c1cSAl Viro .iov = iov, 4318cd54c1cSAl Viro .nr_segs = nr_segs, 4328cd54c1cSAl Viro .iov_offset = 0, 4338cd54c1cSAl Viro .count = count 4348cd54c1cSAl Viro }; 435d879cb83SAl Viro } 436d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init); 437d879cb83SAl Viro 4388cefc107SDavid Howells static inline void data_start(const struct iov_iter *i, 4398cefc107SDavid Howells unsigned int *iter_headp, size_t *offp) 440241699cdSAl Viro { 4418cefc107SDavid Howells unsigned int iter_head = i->head; 442241699cdSAl Viro size_t off = i->iov_offset; 4438cefc107SDavid Howells 4442dcedb2aSAl Viro if (off && (!allocated(pipe_buf(i->pipe, iter_head)) || 4458cefc107SDavid Howells off == PAGE_SIZE)) { 4468cefc107SDavid Howells iter_head++; 447241699cdSAl Viro off = 0; 448241699cdSAl Viro } 4498cefc107SDavid Howells *iter_headp = iter_head; 450241699cdSAl Viro *offp = off; 451241699cdSAl Viro } 452241699cdSAl Viro 453241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes, 454241699cdSAl Viro struct iov_iter *i) 455241699cdSAl Viro { 4568fad7767SAl Viro unsigned int off, chunk; 4578fad7767SAl Viro 4588fad7767SAl Viro if (unlikely(bytes > i->count)) 4598fad7767SAl Viro bytes = i->count; 4608fad7767SAl Viro if (unlikely(!bytes)) 4618fad7767SAl Viro return 0; 462241699cdSAl Viro 463241699cdSAl Viro if (!sanity(i)) 464241699cdSAl Viro return 0; 465241699cdSAl Viro 4668fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 4678fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 4688fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 4698fad7767SAl Viro if (!page) 4708fad7767SAl Viro return bytes - n; 4718fad7767SAl Viro memcpy_to_page(page, off, addr, chunk); 472241699cdSAl Viro addr += chunk; 4738fad7767SAl Viro } 474241699cdSAl Viro return bytes; 475241699cdSAl Viro } 476241699cdSAl Viro 477f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len, 478f9152895SAl Viro __wsum sum, size_t off) 479f9152895SAl Viro { 480cc44c17bSAl Viro __wsum next = csum_partial_copy_nocheck(from, to, len); 481f9152895SAl Viro return csum_block_add(sum, next, off); 482f9152895SAl Viro } 483f9152895SAl Viro 48478e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes, 4856852df12SAl Viro struct iov_iter *i, __wsum *sump) 48678e1f386SAl Viro { 4876852df12SAl Viro __wsum sum = *sump; 4886852df12SAl Viro size_t off = 0; 4898fad7767SAl Viro unsigned int chunk, r; 4908fad7767SAl Viro 4918fad7767SAl Viro if (unlikely(bytes > i->count)) 4928fad7767SAl Viro bytes = i->count; 4938fad7767SAl Viro if (unlikely(!bytes)) 4948fad7767SAl Viro return 0; 49578e1f386SAl Viro 49678e1f386SAl Viro if (!sanity(i)) 49778e1f386SAl Viro return 0; 49878e1f386SAl Viro 4996852df12SAl Viro while (bytes) { 5008fad7767SAl Viro struct page *page = append_pipe(i, bytes, &r); 5018fad7767SAl Viro char *p; 5028fad7767SAl Viro 5038fad7767SAl Viro if (!page) 5048fad7767SAl Viro break; 5058fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - r); 5068fad7767SAl Viro p = kmap_local_page(page); 5076852df12SAl Viro sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off); 5082495bdccSAl Viro kunmap_local(p); 50978e1f386SAl Viro off += chunk; 5108fad7767SAl Viro bytes -= chunk; 5116852df12SAl Viro } 5126852df12SAl Viro *sump = sum; 5136852df12SAl Viro return off; 51478e1f386SAl Viro } 51578e1f386SAl Viro 516aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 517d879cb83SAl Viro { 51800e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 519241699cdSAl Viro return copy_pipe_to_iter(addr, bytes, i); 520fcb14cb1SAl Viro if (user_backed_iter(i)) 52109fc68dcSAl Viro might_fault(); 5227baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 5237baa5099SAl Viro copyout(base, addr + off, len), 5247baa5099SAl Viro memcpy(base, addr + off, len) 525d879cb83SAl Viro ) 526d879cb83SAl Viro 527d879cb83SAl Viro return bytes; 528d879cb83SAl Viro } 529aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter); 530d879cb83SAl Viro 531ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC 532ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n) 5338780356eSDan Williams { 53496d4f267SLinus Torvalds if (access_ok(to, n)) { 535d0ef4c36SMarco Elver instrument_copy_to_user(to, from, n); 536ec6347bbSDan Williams n = copy_mc_to_user((__force void *) to, from, n); 5378780356eSDan Williams } 5388780356eSDan Williams return n; 5398780356eSDan Williams } 5408780356eSDan Williams 541ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes, 542ca146f6fSDan Williams struct iov_iter *i) 543ca146f6fSDan Williams { 5448fad7767SAl Viro size_t xfer = 0; 5458fad7767SAl Viro unsigned int off, chunk; 5468fad7767SAl Viro 5478fad7767SAl Viro if (unlikely(bytes > i->count)) 5488fad7767SAl Viro bytes = i->count; 5498fad7767SAl Viro if (unlikely(!bytes)) 5508fad7767SAl Viro return 0; 551ca146f6fSDan Williams 552ca146f6fSDan Williams if (!sanity(i)) 553ca146f6fSDan Williams return 0; 554ca146f6fSDan Williams 5558fad7767SAl Viro while (bytes) { 5568fad7767SAl Viro struct page *page = append_pipe(i, bytes, &off); 557ca146f6fSDan Williams unsigned long rem; 5588fad7767SAl Viro char *p; 5598fad7767SAl Viro 5608fad7767SAl Viro if (!page) 5618fad7767SAl Viro break; 5628fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - off); 5638fad7767SAl Viro p = kmap_local_page(page); 5642a510a74SAl Viro rem = copy_mc_to_kernel(p + off, addr + xfer, chunk); 5652a510a74SAl Viro chunk -= rem; 5662a510a74SAl Viro kunmap_local(p); 5672a510a74SAl Viro xfer += chunk; 5688fad7767SAl Viro bytes -= chunk; 569c3497fd0SAl Viro if (rem) { 5708fad7767SAl Viro iov_iter_revert(i, rem); 571ca146f6fSDan Williams break; 572c3497fd0SAl Viro } 5732a510a74SAl Viro } 574ca146f6fSDan Williams return xfer; 575ca146f6fSDan Williams } 576ca146f6fSDan Williams 577bf3eeb9bSDan Williams /** 578ec6347bbSDan Williams * _copy_mc_to_iter - copy to iter with source memory error exception handling 579bf3eeb9bSDan Williams * @addr: source kernel address 580bf3eeb9bSDan Williams * @bytes: total transfer length 58144e55997SRandy Dunlap * @i: destination iterator 582bf3eeb9bSDan Williams * 583ec6347bbSDan Williams * The pmem driver deploys this for the dax operation 584ec6347bbSDan Williams * (dax_copy_to_iter()) for dax reads (bypass page-cache and the 585ec6347bbSDan Williams * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes 586ec6347bbSDan Williams * successfully copied. 587bf3eeb9bSDan Williams * 588ec6347bbSDan Williams * The main differences between this and typical _copy_to_iter(). 589bf3eeb9bSDan Williams * 590bf3eeb9bSDan Williams * * Typical tail/residue handling after a fault retries the copy 591bf3eeb9bSDan Williams * byte-by-byte until the fault happens again. Re-triggering machine 592bf3eeb9bSDan Williams * checks is potentially fatal so the implementation uses source 593bf3eeb9bSDan Williams * alignment and poison alignment assumptions to avoid re-triggering 594bf3eeb9bSDan Williams * hardware exceptions. 595bf3eeb9bSDan Williams * 596bf3eeb9bSDan Williams * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies. 597bf3eeb9bSDan Williams * Compare to copy_to_iter() where only ITER_IOVEC attempts might return 598bf3eeb9bSDan Williams * a short copy. 59944e55997SRandy Dunlap * 60044e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 601bf3eeb9bSDan Williams */ 602ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 6038780356eSDan Williams { 60400e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 605ec6347bbSDan Williams return copy_mc_pipe_to_iter(addr, bytes, i); 606fcb14cb1SAl Viro if (user_backed_iter(i)) 6078780356eSDan Williams might_fault(); 6087baa5099SAl Viro __iterate_and_advance(i, bytes, base, len, off, 6097baa5099SAl Viro copyout_mc(base, addr + off, len), 6107baa5099SAl Viro copy_mc_to_kernel(base, addr + off, len) 6118780356eSDan Williams ) 6128780356eSDan Williams 6138780356eSDan Williams return bytes; 6148780356eSDan Williams } 615ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter); 616ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */ 6178780356eSDan Williams 618aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) 619d879cb83SAl Viro { 62000e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 621241699cdSAl Viro WARN_ON(1); 622241699cdSAl Viro return 0; 623241699cdSAl Viro } 624fcb14cb1SAl Viro if (user_backed_iter(i)) 62509fc68dcSAl Viro might_fault(); 6267baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6277baa5099SAl Viro copyin(addr + off, base, len), 6287baa5099SAl Viro memcpy(addr + off, base, len) 629d879cb83SAl Viro ) 630d879cb83SAl Viro 631d879cb83SAl Viro return bytes; 632d879cb83SAl Viro } 633aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter); 634d879cb83SAl Viro 635aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) 636d879cb83SAl Viro { 63700e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 638241699cdSAl Viro WARN_ON(1); 639241699cdSAl Viro return 0; 640241699cdSAl Viro } 6417baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6427baa5099SAl Viro __copy_from_user_inatomic_nocache(addr + off, base, len), 6437baa5099SAl Viro memcpy(addr + off, base, len) 644d879cb83SAl Viro ) 645d879cb83SAl Viro 646d879cb83SAl Viro return bytes; 647d879cb83SAl Viro } 648aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache); 649d879cb83SAl Viro 6500aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE 651abd08d7dSDan Williams /** 652abd08d7dSDan Williams * _copy_from_iter_flushcache - write destination through cpu cache 653abd08d7dSDan Williams * @addr: destination kernel address 654abd08d7dSDan Williams * @bytes: total transfer length 65544e55997SRandy Dunlap * @i: source iterator 656abd08d7dSDan Williams * 657abd08d7dSDan Williams * The pmem driver arranges for filesystem-dax to use this facility via 658abd08d7dSDan Williams * dax_copy_from_iter() for ensuring that writes to persistent memory 659abd08d7dSDan Williams * are flushed through the CPU cache. It is differentiated from 660abd08d7dSDan Williams * _copy_from_iter_nocache() in that guarantees all data is flushed for 661abd08d7dSDan Williams * all iterator types. The _copy_from_iter_nocache() only attempts to 662abd08d7dSDan Williams * bypass the cache for the ITER_IOVEC case, and on some archs may use 663abd08d7dSDan Williams * instructions that strand dirty-data in the cache. 66444e55997SRandy Dunlap * 66544e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 666abd08d7dSDan Williams */ 6676a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i) 6680aed55afSDan Williams { 66900e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 6700aed55afSDan Williams WARN_ON(1); 6710aed55afSDan Williams return 0; 6720aed55afSDan Williams } 6737baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6747baa5099SAl Viro __copy_from_user_flushcache(addr + off, base, len), 6757baa5099SAl Viro memcpy_flushcache(addr + off, base, len) 6760aed55afSDan Williams ) 6770aed55afSDan Williams 6780aed55afSDan Williams return bytes; 6790aed55afSDan Williams } 6806a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache); 6810aed55afSDan Williams #endif 6820aed55afSDan Williams 68372e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n) 68472e809edSAl Viro { 6856daef95bSEric Dumazet struct page *head; 6866daef95bSEric Dumazet size_t v = n + offset; 6876daef95bSEric Dumazet 6886daef95bSEric Dumazet /* 6896daef95bSEric Dumazet * The general case needs to access the page order in order 6906daef95bSEric Dumazet * to compute the page size. 6916daef95bSEric Dumazet * However, we mostly deal with order-0 pages and thus can 6926daef95bSEric Dumazet * avoid a possible cache line miss for requests that fit all 6936daef95bSEric Dumazet * page orders. 6946daef95bSEric Dumazet */ 6956daef95bSEric Dumazet if (n <= v && v <= PAGE_SIZE) 6966daef95bSEric Dumazet return true; 6976daef95bSEric Dumazet 6986daef95bSEric Dumazet head = compound_head(page); 6996daef95bSEric Dumazet v += (page - head) << PAGE_SHIFT; 700a90bcb86SPetar Penkov 701a50b854eSMatthew Wilcox (Oracle) if (likely(n <= v && v <= (page_size(head)))) 70272e809edSAl Viro return true; 70372e809edSAl Viro WARN_ON(1); 70472e809edSAl Viro return false; 70572e809edSAl Viro } 706cbbd26b8SAl Viro 70708aa6479SAl Viro static size_t __copy_page_to_iter(struct page *page, size_t offset, size_t bytes, 708d879cb83SAl Viro struct iov_iter *i) 709d879cb83SAl Viro { 71059bb69c6SAl Viro if (unlikely(iov_iter_is_pipe(i))) { 71159bb69c6SAl Viro return copy_page_to_iter_pipe(page, offset, bytes, i); 71259bb69c6SAl Viro } else { 713c1d4d6a9SAl Viro void *kaddr = kmap_local_page(page); 714c1d4d6a9SAl Viro size_t wanted = _copy_to_iter(kaddr + offset, bytes, i); 715c1d4d6a9SAl Viro kunmap_local(kaddr); 716d879cb83SAl Viro return wanted; 71728f38db7SAl Viro } 718d879cb83SAl Viro } 71908aa6479SAl Viro 72008aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, 72108aa6479SAl Viro struct iov_iter *i) 72208aa6479SAl Viro { 72308aa6479SAl Viro size_t res = 0; 72408aa6479SAl Viro if (unlikely(!page_copy_sane(page, offset, bytes))) 72508aa6479SAl Viro return 0; 72608aa6479SAl Viro page += offset / PAGE_SIZE; // first subpage 72708aa6479SAl Viro offset %= PAGE_SIZE; 72808aa6479SAl Viro while (1) { 72908aa6479SAl Viro size_t n = __copy_page_to_iter(page, offset, 73008aa6479SAl Viro min(bytes, (size_t)PAGE_SIZE - offset), i); 73108aa6479SAl Viro res += n; 73208aa6479SAl Viro bytes -= n; 73308aa6479SAl Viro if (!bytes || !n) 73408aa6479SAl Viro break; 73508aa6479SAl Viro offset += n; 73608aa6479SAl Viro if (offset == PAGE_SIZE) { 73708aa6479SAl Viro page++; 73808aa6479SAl Viro offset = 0; 73908aa6479SAl Viro } 74008aa6479SAl Viro } 74108aa6479SAl Viro return res; 74208aa6479SAl Viro } 743d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter); 744d879cb83SAl Viro 745d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, 746d879cb83SAl Viro struct iov_iter *i) 747d879cb83SAl Viro { 74859bb69c6SAl Viro if (page_copy_sane(page, offset, bytes)) { 74955ca375cSAl Viro void *kaddr = kmap_local_page(page); 750aa28de27SAl Viro size_t wanted = _copy_from_iter(kaddr + offset, bytes, i); 75155ca375cSAl Viro kunmap_local(kaddr); 752d879cb83SAl Viro return wanted; 75328f38db7SAl Viro } 75428f38db7SAl Viro return 0; 755d879cb83SAl Viro } 756d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter); 757d879cb83SAl Viro 758241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i) 759241699cdSAl Viro { 7608fad7767SAl Viro unsigned int chunk, off; 7618fad7767SAl Viro 7628fad7767SAl Viro if (unlikely(bytes > i->count)) 7638fad7767SAl Viro bytes = i->count; 7648fad7767SAl Viro if (unlikely(!bytes)) 7658fad7767SAl Viro return 0; 766241699cdSAl Viro 767241699cdSAl Viro if (!sanity(i)) 768241699cdSAl Viro return 0; 769241699cdSAl Viro 7708fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 7718fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 7728fad7767SAl Viro char *p; 773241699cdSAl Viro 7748fad7767SAl Viro if (!page) 7758fad7767SAl Viro return bytes - n; 7768fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 7778fad7767SAl Viro p = kmap_local_page(page); 778893839fdSAl Viro memset(p + off, 0, chunk); 779893839fdSAl Viro kunmap_local(p); 7808fad7767SAl Viro } 781241699cdSAl Viro return bytes; 782241699cdSAl Viro } 783241699cdSAl Viro 784d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i) 785d879cb83SAl Viro { 78600e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 787241699cdSAl Viro return pipe_zero(bytes, i); 7887baa5099SAl Viro iterate_and_advance(i, bytes, base, len, count, 7897baa5099SAl Viro clear_user(base, len), 7907baa5099SAl Viro memset(base, 0, len) 791d879cb83SAl Viro ) 792d879cb83SAl Viro 793d879cb83SAl Viro return bytes; 794d879cb83SAl Viro } 795d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero); 796d879cb83SAl Viro 797f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes, 798f0b65f39SAl Viro struct iov_iter *i) 799d879cb83SAl Viro { 800d879cb83SAl Viro char *kaddr = kmap_atomic(page), *p = kaddr + offset; 80172e809edSAl Viro if (unlikely(!page_copy_sane(page, offset, bytes))) { 80272e809edSAl Viro kunmap_atomic(kaddr); 80372e809edSAl Viro return 0; 80472e809edSAl Viro } 8059ea9ce04SDavid Howells if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { 806241699cdSAl Viro kunmap_atomic(kaddr); 807241699cdSAl Viro WARN_ON(1); 808241699cdSAl Viro return 0; 809241699cdSAl Viro } 8107baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 8117baa5099SAl Viro copyin(p + off, base, len), 8127baa5099SAl Viro memcpy(p + off, base, len) 813d879cb83SAl Viro ) 814d879cb83SAl Viro kunmap_atomic(kaddr); 815d879cb83SAl Viro return bytes; 816d879cb83SAl Viro } 817f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic); 818d879cb83SAl Viro 819b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size) 820b9dc6f65SAl Viro { 821b9dc6f65SAl Viro struct pipe_inode_info *pipe = i->pipe; 8222c855de9SAl Viro unsigned int off = i->iov_offset; 8238cefc107SDavid Howells 8242c855de9SAl Viro if (!off && !size) { 8252c855de9SAl Viro pipe_discard_from(pipe, i->start_head); // discard everything 8262c855de9SAl Viro return; 827b9dc6f65SAl Viro } 828b9dc6f65SAl Viro i->count -= size; 8292c855de9SAl Viro while (1) { 8302c855de9SAl Viro struct pipe_buffer *buf = pipe_buf(pipe, i->head); 8312c855de9SAl Viro if (off) /* make it relative to the beginning of buffer */ 8322c855de9SAl Viro size += off - buf->offset; 8332c855de9SAl Viro if (size <= buf->len) { 8342c855de9SAl Viro buf->len = size; 8352c855de9SAl Viro i->iov_offset = buf->offset + size; 8362c855de9SAl Viro break; 8372c855de9SAl Viro } 8382c855de9SAl Viro size -= buf->len; 8392c855de9SAl Viro i->head++; 8402c855de9SAl Viro off = 0; 8412c855de9SAl Viro } 8422c855de9SAl Viro pipe_discard_from(pipe, i->head + 1); // discard everything past this one 843241699cdSAl Viro } 844241699cdSAl Viro 84554c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size) 84654c8195bSPavel Begunkov { 84718fa9af7SAl Viro const struct bio_vec *bvec, *end; 84854c8195bSPavel Begunkov 84918fa9af7SAl Viro if (!i->count) 85018fa9af7SAl Viro return; 85118fa9af7SAl Viro i->count -= size; 85254c8195bSPavel Begunkov 85318fa9af7SAl Viro size += i->iov_offset; 85418fa9af7SAl Viro 85518fa9af7SAl Viro for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) { 85618fa9af7SAl Viro if (likely(size < bvec->bv_len)) 85718fa9af7SAl Viro break; 85818fa9af7SAl Viro size -= bvec->bv_len; 85918fa9af7SAl Viro } 86018fa9af7SAl Viro i->iov_offset = size; 86118fa9af7SAl Viro i->nr_segs -= bvec - i->bvec; 86218fa9af7SAl Viro i->bvec = bvec; 86354c8195bSPavel Begunkov } 86454c8195bSPavel Begunkov 865185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size) 866185ac4d4SAl Viro { 867185ac4d4SAl Viro const struct iovec *iov, *end; 868185ac4d4SAl Viro 869185ac4d4SAl Viro if (!i->count) 870185ac4d4SAl Viro return; 871185ac4d4SAl Viro i->count -= size; 872185ac4d4SAl Viro 873185ac4d4SAl Viro size += i->iov_offset; // from beginning of current segment 874185ac4d4SAl Viro for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) { 875185ac4d4SAl Viro if (likely(size < iov->iov_len)) 876185ac4d4SAl Viro break; 877185ac4d4SAl Viro size -= iov->iov_len; 878185ac4d4SAl Viro } 879185ac4d4SAl Viro i->iov_offset = size; 880185ac4d4SAl Viro i->nr_segs -= iov - i->iov; 881185ac4d4SAl Viro i->iov = iov; 882185ac4d4SAl Viro } 883185ac4d4SAl Viro 884d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size) 885d879cb83SAl Viro { 8863b3fc051SAl Viro if (unlikely(i->count < size)) 8873b3fc051SAl Viro size = i->count; 888fcb14cb1SAl Viro if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) { 889fcb14cb1SAl Viro i->iov_offset += size; 890fcb14cb1SAl Viro i->count -= size; 891fcb14cb1SAl Viro } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) { 892185ac4d4SAl Viro /* iovec and kvec have identical layouts */ 893185ac4d4SAl Viro iov_iter_iovec_advance(i, size); 894185ac4d4SAl Viro } else if (iov_iter_is_bvec(i)) { 895185ac4d4SAl Viro iov_iter_bvec_advance(i, size); 896185ac4d4SAl Viro } else if (iov_iter_is_pipe(i)) { 897241699cdSAl Viro pipe_advance(i, size); 898185ac4d4SAl Viro } else if (iov_iter_is_discard(i)) { 899185ac4d4SAl Viro i->count -= size; 9007ff50620SDavid Howells } 901d879cb83SAl Viro } 902d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance); 903d879cb83SAl Viro 90427c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll) 90527c0e374SAl Viro { 90627c0e374SAl Viro if (!unroll) 90727c0e374SAl Viro return; 9085b47d59aSAl Viro if (WARN_ON(unroll > MAX_RW_COUNT)) 9095b47d59aSAl Viro return; 91027c0e374SAl Viro i->count += unroll; 91100e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 91227c0e374SAl Viro struct pipe_inode_info *pipe = i->pipe; 913*92acdc4fSAl Viro unsigned int head = pipe->head; 914*92acdc4fSAl Viro 915*92acdc4fSAl Viro while (head > i->start_head) { 916*92acdc4fSAl Viro struct pipe_buffer *b = pipe_buf(pipe, --head); 917*92acdc4fSAl Viro if (unroll < b->len) { 918*92acdc4fSAl Viro b->len -= unroll; 919*92acdc4fSAl Viro i->iov_offset = b->offset + b->len; 920*92acdc4fSAl Viro i->head = head; 921*92acdc4fSAl Viro return; 92227c0e374SAl Viro } 923*92acdc4fSAl Viro unroll -= b->len; 924*92acdc4fSAl Viro pipe_buf_release(pipe, b); 925*92acdc4fSAl Viro pipe->head--; 92627c0e374SAl Viro } 927*92acdc4fSAl Viro i->iov_offset = 0; 928*92acdc4fSAl Viro i->head = head; 92927c0e374SAl Viro return; 93027c0e374SAl Viro } 9319ea9ce04SDavid Howells if (unlikely(iov_iter_is_discard(i))) 9329ea9ce04SDavid Howells return; 93327c0e374SAl Viro if (unroll <= i->iov_offset) { 93427c0e374SAl Viro i->iov_offset -= unroll; 93527c0e374SAl Viro return; 93627c0e374SAl Viro } 93727c0e374SAl Viro unroll -= i->iov_offset; 938fcb14cb1SAl Viro if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) { 9397ff50620SDavid Howells BUG(); /* We should never go beyond the start of the specified 9407ff50620SDavid Howells * range since we might then be straying into pages that 9417ff50620SDavid Howells * aren't pinned. 9427ff50620SDavid Howells */ 9437ff50620SDavid Howells } else if (iov_iter_is_bvec(i)) { 94427c0e374SAl Viro const struct bio_vec *bvec = i->bvec; 94527c0e374SAl Viro while (1) { 94627c0e374SAl Viro size_t n = (--bvec)->bv_len; 94727c0e374SAl Viro i->nr_segs++; 94827c0e374SAl Viro if (unroll <= n) { 94927c0e374SAl Viro i->bvec = bvec; 95027c0e374SAl Viro i->iov_offset = n - unroll; 95127c0e374SAl Viro return; 95227c0e374SAl Viro } 95327c0e374SAl Viro unroll -= n; 95427c0e374SAl Viro } 95527c0e374SAl Viro } else { /* same logics for iovec and kvec */ 95627c0e374SAl Viro const struct iovec *iov = i->iov; 95727c0e374SAl Viro while (1) { 95827c0e374SAl Viro size_t n = (--iov)->iov_len; 95927c0e374SAl Viro i->nr_segs++; 96027c0e374SAl Viro if (unroll <= n) { 96127c0e374SAl Viro i->iov = iov; 96227c0e374SAl Viro i->iov_offset = n - unroll; 96327c0e374SAl Viro return; 96427c0e374SAl Viro } 96527c0e374SAl Viro unroll -= n; 96627c0e374SAl Viro } 96727c0e374SAl Viro } 96827c0e374SAl Viro } 96927c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert); 97027c0e374SAl Viro 971d879cb83SAl Viro /* 972d879cb83SAl Viro * Return the count of just the current iov_iter segment. 973d879cb83SAl Viro */ 974d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i) 975d879cb83SAl Viro { 97628f38db7SAl Viro if (i->nr_segs > 1) { 97728f38db7SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 97828f38db7SAl Viro return min(i->count, i->iov->iov_len - i->iov_offset); 9797ff50620SDavid Howells if (iov_iter_is_bvec(i)) 980d879cb83SAl Viro return min(i->count, i->bvec->bv_len - i->iov_offset); 98128f38db7SAl Viro } 98228f38db7SAl Viro return i->count; 983d879cb83SAl Viro } 984d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count); 985d879cb83SAl Viro 986aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction, 987d879cb83SAl Viro const struct kvec *kvec, unsigned long nr_segs, 988d879cb83SAl Viro size_t count) 989d879cb83SAl Viro { 990aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 9918cd54c1cSAl Viro *i = (struct iov_iter){ 9928cd54c1cSAl Viro .iter_type = ITER_KVEC, 9938cd54c1cSAl Viro .data_source = direction, 9948cd54c1cSAl Viro .kvec = kvec, 9958cd54c1cSAl Viro .nr_segs = nr_segs, 9968cd54c1cSAl Viro .iov_offset = 0, 9978cd54c1cSAl Viro .count = count 9988cd54c1cSAl Viro }; 999d879cb83SAl Viro } 1000d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec); 1001d879cb83SAl Viro 1002aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction, 1003d879cb83SAl Viro const struct bio_vec *bvec, unsigned long nr_segs, 1004d879cb83SAl Viro size_t count) 1005d879cb83SAl Viro { 1006aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 10078cd54c1cSAl Viro *i = (struct iov_iter){ 10088cd54c1cSAl Viro .iter_type = ITER_BVEC, 10098cd54c1cSAl Viro .data_source = direction, 10108cd54c1cSAl Viro .bvec = bvec, 10118cd54c1cSAl Viro .nr_segs = nr_segs, 10128cd54c1cSAl Viro .iov_offset = 0, 10138cd54c1cSAl Viro .count = count 10148cd54c1cSAl Viro }; 1015d879cb83SAl Viro } 1016d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec); 1017d879cb83SAl Viro 1018aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction, 1019241699cdSAl Viro struct pipe_inode_info *pipe, 1020241699cdSAl Viro size_t count) 1021241699cdSAl Viro { 1022aa563d7bSDavid Howells BUG_ON(direction != READ); 10238cefc107SDavid Howells WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size)); 10248cd54c1cSAl Viro *i = (struct iov_iter){ 10258cd54c1cSAl Viro .iter_type = ITER_PIPE, 10268cd54c1cSAl Viro .data_source = false, 10278cd54c1cSAl Viro .pipe = pipe, 10288cd54c1cSAl Viro .head = pipe->head, 10298cd54c1cSAl Viro .start_head = pipe->head, 10308cd54c1cSAl Viro .iov_offset = 0, 10318cd54c1cSAl Viro .count = count 10328cd54c1cSAl Viro }; 1033241699cdSAl Viro } 1034241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe); 1035241699cdSAl Viro 10369ea9ce04SDavid Howells /** 10377ff50620SDavid Howells * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray 10387ff50620SDavid Howells * @i: The iterator to initialise. 10397ff50620SDavid Howells * @direction: The direction of the transfer. 10407ff50620SDavid Howells * @xarray: The xarray to access. 10417ff50620SDavid Howells * @start: The start file position. 10427ff50620SDavid Howells * @count: The size of the I/O buffer in bytes. 10437ff50620SDavid Howells * 10447ff50620SDavid Howells * Set up an I/O iterator to either draw data out of the pages attached to an 10457ff50620SDavid Howells * inode or to inject data into those pages. The pages *must* be prevented 10467ff50620SDavid Howells * from evaporation, either by taking a ref on them or locking them by the 10477ff50620SDavid Howells * caller. 10487ff50620SDavid Howells */ 10497ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction, 10507ff50620SDavid Howells struct xarray *xarray, loff_t start, size_t count) 10517ff50620SDavid Howells { 10527ff50620SDavid Howells BUG_ON(direction & ~1); 10538cd54c1cSAl Viro *i = (struct iov_iter) { 10548cd54c1cSAl Viro .iter_type = ITER_XARRAY, 10558cd54c1cSAl Viro .data_source = direction, 10568cd54c1cSAl Viro .xarray = xarray, 10578cd54c1cSAl Viro .xarray_start = start, 10588cd54c1cSAl Viro .count = count, 10598cd54c1cSAl Viro .iov_offset = 0 10608cd54c1cSAl Viro }; 10617ff50620SDavid Howells } 10627ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray); 10637ff50620SDavid Howells 10647ff50620SDavid Howells /** 10659ea9ce04SDavid Howells * iov_iter_discard - Initialise an I/O iterator that discards data 10669ea9ce04SDavid Howells * @i: The iterator to initialise. 10679ea9ce04SDavid Howells * @direction: The direction of the transfer. 10689ea9ce04SDavid Howells * @count: The size of the I/O buffer in bytes. 10699ea9ce04SDavid Howells * 10709ea9ce04SDavid Howells * Set up an I/O iterator that just discards everything that's written to it. 10719ea9ce04SDavid Howells * It's only available as a READ iterator. 10729ea9ce04SDavid Howells */ 10739ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count) 10749ea9ce04SDavid Howells { 10759ea9ce04SDavid Howells BUG_ON(direction != READ); 10768cd54c1cSAl Viro *i = (struct iov_iter){ 10778cd54c1cSAl Viro .iter_type = ITER_DISCARD, 10788cd54c1cSAl Viro .data_source = false, 10798cd54c1cSAl Viro .count = count, 10808cd54c1cSAl Viro .iov_offset = 0 10818cd54c1cSAl Viro }; 10829ea9ce04SDavid Howells } 10839ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard); 10849ea9ce04SDavid Howells 1085cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask, 1086cfa320f7SKeith Busch unsigned len_mask) 1087cfa320f7SKeith Busch { 1088cfa320f7SKeith Busch size_t size = i->count; 1089cfa320f7SKeith Busch size_t skip = i->iov_offset; 1090cfa320f7SKeith Busch unsigned k; 1091cfa320f7SKeith Busch 1092cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1093cfa320f7SKeith Busch size_t len = i->iov[k].iov_len - skip; 1094cfa320f7SKeith Busch 1095cfa320f7SKeith Busch if (len > size) 1096cfa320f7SKeith Busch len = size; 1097cfa320f7SKeith Busch if (len & len_mask) 1098cfa320f7SKeith Busch return false; 1099cfa320f7SKeith Busch if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask) 1100cfa320f7SKeith Busch return false; 1101cfa320f7SKeith Busch 1102cfa320f7SKeith Busch size -= len; 1103cfa320f7SKeith Busch if (!size) 1104cfa320f7SKeith Busch break; 1105cfa320f7SKeith Busch } 1106cfa320f7SKeith Busch return true; 1107cfa320f7SKeith Busch } 1108cfa320f7SKeith Busch 1109cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask, 1110cfa320f7SKeith Busch unsigned len_mask) 1111cfa320f7SKeith Busch { 1112cfa320f7SKeith Busch size_t size = i->count; 1113cfa320f7SKeith Busch unsigned skip = i->iov_offset; 1114cfa320f7SKeith Busch unsigned k; 1115cfa320f7SKeith Busch 1116cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1117cfa320f7SKeith Busch size_t len = i->bvec[k].bv_len - skip; 1118cfa320f7SKeith Busch 1119cfa320f7SKeith Busch if (len > size) 1120cfa320f7SKeith Busch len = size; 1121cfa320f7SKeith Busch if (len & len_mask) 1122cfa320f7SKeith Busch return false; 1123cfa320f7SKeith Busch if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask) 1124cfa320f7SKeith Busch return false; 1125cfa320f7SKeith Busch 1126cfa320f7SKeith Busch size -= len; 1127cfa320f7SKeith Busch if (!size) 1128cfa320f7SKeith Busch break; 1129cfa320f7SKeith Busch } 1130cfa320f7SKeith Busch return true; 1131cfa320f7SKeith Busch } 1132cfa320f7SKeith Busch 1133cfa320f7SKeith Busch /** 1134cfa320f7SKeith Busch * iov_iter_is_aligned() - Check if the addresses and lengths of each segments 1135cfa320f7SKeith Busch * are aligned to the parameters. 1136cfa320f7SKeith Busch * 1137cfa320f7SKeith Busch * @i: &struct iov_iter to restore 1138cfa320f7SKeith Busch * @addr_mask: bit mask to check against the iov element's addresses 1139cfa320f7SKeith Busch * @len_mask: bit mask to check against the iov element's lengths 1140cfa320f7SKeith Busch * 1141cfa320f7SKeith Busch * Return: false if any addresses or lengths intersect with the provided masks 1142cfa320f7SKeith Busch */ 1143cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask, 1144cfa320f7SKeith Busch unsigned len_mask) 1145cfa320f7SKeith Busch { 1146fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1147fcb14cb1SAl Viro if (i->count & len_mask) 1148fcb14cb1SAl Viro return false; 1149fcb14cb1SAl Viro if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask) 1150fcb14cb1SAl Viro return false; 1151fcb14cb1SAl Viro return true; 1152fcb14cb1SAl Viro } 1153fcb14cb1SAl Viro 1154cfa320f7SKeith Busch if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 1155cfa320f7SKeith Busch return iov_iter_aligned_iovec(i, addr_mask, len_mask); 1156cfa320f7SKeith Busch 1157cfa320f7SKeith Busch if (iov_iter_is_bvec(i)) 1158cfa320f7SKeith Busch return iov_iter_aligned_bvec(i, addr_mask, len_mask); 1159cfa320f7SKeith Busch 1160cfa320f7SKeith Busch if (iov_iter_is_pipe(i)) { 1161cfa320f7SKeith Busch unsigned int p_mask = i->pipe->ring_size - 1; 1162cfa320f7SKeith Busch size_t size = i->count; 1163cfa320f7SKeith Busch 1164cfa320f7SKeith Busch if (size & len_mask) 1165cfa320f7SKeith Busch return false; 1166cfa320f7SKeith Busch if (size && allocated(&i->pipe->bufs[i->head & p_mask])) { 1167cfa320f7SKeith Busch if (i->iov_offset & addr_mask) 1168cfa320f7SKeith Busch return false; 1169cfa320f7SKeith Busch } 1170cfa320f7SKeith Busch 1171cfa320f7SKeith Busch return true; 1172cfa320f7SKeith Busch } 1173cfa320f7SKeith Busch 1174cfa320f7SKeith Busch if (iov_iter_is_xarray(i)) { 1175cfa320f7SKeith Busch if (i->count & len_mask) 1176cfa320f7SKeith Busch return false; 1177cfa320f7SKeith Busch if ((i->xarray_start + i->iov_offset) & addr_mask) 1178cfa320f7SKeith Busch return false; 1179cfa320f7SKeith Busch } 1180cfa320f7SKeith Busch 1181cfa320f7SKeith Busch return true; 1182cfa320f7SKeith Busch } 1183cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned); 1184cfa320f7SKeith Busch 11859221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i) 1186d879cb83SAl Viro { 1187d879cb83SAl Viro unsigned long res = 0; 1188d879cb83SAl Viro size_t size = i->count; 11899221d2e3SAl Viro size_t skip = i->iov_offset; 11909221d2e3SAl Viro unsigned k; 1191d879cb83SAl Viro 11929221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 11939221d2e3SAl Viro size_t len = i->iov[k].iov_len - skip; 11949221d2e3SAl Viro if (len) { 11959221d2e3SAl Viro res |= (unsigned long)i->iov[k].iov_base + skip; 11969221d2e3SAl Viro if (len > size) 11979221d2e3SAl Viro len = size; 11989221d2e3SAl Viro res |= len; 11999221d2e3SAl Viro size -= len; 12009221d2e3SAl Viro if (!size) 12019221d2e3SAl Viro break; 12029221d2e3SAl Viro } 12039221d2e3SAl Viro } 12049221d2e3SAl Viro return res; 12059221d2e3SAl Viro } 12069221d2e3SAl Viro 12079221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i) 12089221d2e3SAl Viro { 12099221d2e3SAl Viro unsigned res = 0; 12109221d2e3SAl Viro size_t size = i->count; 12119221d2e3SAl Viro unsigned skip = i->iov_offset; 12129221d2e3SAl Viro unsigned k; 12139221d2e3SAl Viro 12149221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 12159221d2e3SAl Viro size_t len = i->bvec[k].bv_len - skip; 12169221d2e3SAl Viro res |= (unsigned long)i->bvec[k].bv_offset + skip; 12179221d2e3SAl Viro if (len > size) 12189221d2e3SAl Viro len = size; 12199221d2e3SAl Viro res |= len; 12209221d2e3SAl Viro size -= len; 12219221d2e3SAl Viro if (!size) 12229221d2e3SAl Viro break; 12239221d2e3SAl Viro } 12249221d2e3SAl Viro return res; 12259221d2e3SAl Viro } 12269221d2e3SAl Viro 12279221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i) 12289221d2e3SAl Viro { 1229fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1230fcb14cb1SAl Viro size_t size = i->count; 1231fcb14cb1SAl Viro if (size) 1232fcb14cb1SAl Viro return ((unsigned long)i->ubuf + i->iov_offset) | size; 1233fcb14cb1SAl Viro return 0; 1234fcb14cb1SAl Viro } 1235fcb14cb1SAl Viro 12369221d2e3SAl Viro /* iovec and kvec have identical layouts */ 12379221d2e3SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 12389221d2e3SAl Viro return iov_iter_alignment_iovec(i); 12399221d2e3SAl Viro 12409221d2e3SAl Viro if (iov_iter_is_bvec(i)) 12419221d2e3SAl Viro return iov_iter_alignment_bvec(i); 12429221d2e3SAl Viro 12439221d2e3SAl Viro if (iov_iter_is_pipe(i)) { 12449221d2e3SAl Viro size_t size = i->count; 1245e0ff126eSJan Kara 12462dcedb2aSAl Viro if (size && i->iov_offset && allocated(pipe_buf(i->pipe, i->head))) 1247241699cdSAl Viro return size | i->iov_offset; 1248241699cdSAl Viro return size; 1249241699cdSAl Viro } 12509221d2e3SAl Viro 12519221d2e3SAl Viro if (iov_iter_is_xarray(i)) 12523d14ec1fSDavid Howells return (i->xarray_start + i->iov_offset) | i->count; 12539221d2e3SAl Viro 12549221d2e3SAl Viro return 0; 1255d879cb83SAl Viro } 1256d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment); 1257d879cb83SAl Viro 1258357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i) 1259357f435dSAl Viro { 1260357f435dSAl Viro unsigned long res = 0; 1261610c7a71SAl Viro unsigned long v = 0; 1262357f435dSAl Viro size_t size = i->count; 1263610c7a71SAl Viro unsigned k; 1264357f435dSAl Viro 1265fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1266fcb14cb1SAl Viro return 0; 1267fcb14cb1SAl Viro 1268610c7a71SAl Viro if (WARN_ON(!iter_is_iovec(i))) 1269241699cdSAl Viro return ~0U; 1270241699cdSAl Viro 1271610c7a71SAl Viro for (k = 0; k < i->nr_segs; k++) { 1272610c7a71SAl Viro if (i->iov[k].iov_len) { 1273610c7a71SAl Viro unsigned long base = (unsigned long)i->iov[k].iov_base; 1274610c7a71SAl Viro if (v) // if not the first one 1275610c7a71SAl Viro res |= base | v; // this start | previous end 1276610c7a71SAl Viro v = base + i->iov[k].iov_len; 1277610c7a71SAl Viro if (size <= i->iov[k].iov_len) 1278610c7a71SAl Viro break; 1279610c7a71SAl Viro size -= i->iov[k].iov_len; 1280610c7a71SAl Viro } 1281610c7a71SAl Viro } 1282357f435dSAl Viro return res; 1283357f435dSAl Viro } 1284357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment); 1285357f435dSAl Viro 1286e76b6312SIlya Dryomov static inline ssize_t __pipe_get_pages(struct iov_iter *i, 1287241699cdSAl Viro size_t maxsize, 1288241699cdSAl Viro struct page **pages, 1289e3b42964SAl Viro size_t off) 1290241699cdSAl Viro { 1291241699cdSAl Viro struct pipe_inode_info *pipe = i->pipe; 1292e3b42964SAl Viro ssize_t left = maxsize; 1293241699cdSAl Viro 1294e3b42964SAl Viro if (off) { 1295ca591967SAl Viro struct pipe_buffer *buf = pipe_buf(pipe, pipe->head - 1); 1296241699cdSAl Viro 1297e3b42964SAl Viro get_page(*pages++ = buf->page); 1298e3b42964SAl Viro left -= PAGE_SIZE - off; 1299e3b42964SAl Viro if (left <= 0) { 1300e3b42964SAl Viro buf->len += maxsize; 1301241699cdSAl Viro return maxsize; 1302241699cdSAl Viro } 1303e3b42964SAl Viro buf->len = PAGE_SIZE; 1304e3b42964SAl Viro } 1305e3b42964SAl Viro while (!pipe_full(pipe->head, pipe->tail, pipe->max_usage)) { 1306e3b42964SAl Viro struct page *page = push_anon(pipe, 1307e3b42964SAl Viro min_t(ssize_t, left, PAGE_SIZE)); 1308e3b42964SAl Viro if (!page) 1309e3b42964SAl Viro break; 1310e3b42964SAl Viro get_page(*pages++ = page); 1311e3b42964SAl Viro left -= PAGE_SIZE; 1312e3b42964SAl Viro if (left <= 0) 1313e3b42964SAl Viro return maxsize; 1314e3b42964SAl Viro } 1315e3b42964SAl Viro return maxsize - left ? : -EFAULT; 1316e3b42964SAl Viro } 1317241699cdSAl Viro 1318241699cdSAl Viro static ssize_t pipe_get_pages(struct iov_iter *i, 1319241699cdSAl Viro struct page **pages, size_t maxsize, unsigned maxpages, 1320241699cdSAl Viro size_t *start) 1321241699cdSAl Viro { 13228cefc107SDavid Howells unsigned int iter_head, npages; 1323241699cdSAl Viro size_t capacity; 1324241699cdSAl Viro 1325241699cdSAl Viro if (!sanity(i)) 1326241699cdSAl Viro return -EFAULT; 1327241699cdSAl Viro 13288cefc107SDavid Howells data_start(i, &iter_head, start); 13298cefc107SDavid Howells /* Amount of free space: some of this one + all after this one */ 13308cefc107SDavid Howells npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe); 1331241699cdSAl Viro capacity = min(npages, maxpages) * PAGE_SIZE - *start; 1332241699cdSAl Viro 1333ca591967SAl Viro return __pipe_get_pages(i, min(maxsize, capacity), pages, *start); 1334241699cdSAl Viro } 1335241699cdSAl Viro 13367ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa, 13377ff50620SDavid Howells pgoff_t index, unsigned int nr_pages) 13387ff50620SDavid Howells { 13397ff50620SDavid Howells XA_STATE(xas, xa, index); 13407ff50620SDavid Howells struct page *page; 13417ff50620SDavid Howells unsigned int ret = 0; 13427ff50620SDavid Howells 13437ff50620SDavid Howells rcu_read_lock(); 13447ff50620SDavid Howells for (page = xas_load(&xas); page; page = xas_next(&xas)) { 13457ff50620SDavid Howells if (xas_retry(&xas, page)) 13467ff50620SDavid Howells continue; 13477ff50620SDavid Howells 13487ff50620SDavid Howells /* Has the page moved or been split? */ 13497ff50620SDavid Howells if (unlikely(page != xas_reload(&xas))) { 13507ff50620SDavid Howells xas_reset(&xas); 13517ff50620SDavid Howells continue; 13527ff50620SDavid Howells } 13537ff50620SDavid Howells 13547ff50620SDavid Howells pages[ret] = find_subpage(page, xas.xa_index); 13557ff50620SDavid Howells get_page(pages[ret]); 13567ff50620SDavid Howells if (++ret == nr_pages) 13577ff50620SDavid Howells break; 13587ff50620SDavid Howells } 13597ff50620SDavid Howells rcu_read_unlock(); 13607ff50620SDavid Howells return ret; 13617ff50620SDavid Howells } 13627ff50620SDavid Howells 13637ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i, 13647ff50620SDavid Howells struct page **pages, size_t maxsize, 13657ff50620SDavid Howells unsigned maxpages, size_t *_start_offset) 13667ff50620SDavid Howells { 13677ff50620SDavid Howells unsigned nr, offset; 13687ff50620SDavid Howells pgoff_t index, count; 13696c776766SDavid Howells size_t size = maxsize; 13707ff50620SDavid Howells loff_t pos; 13717ff50620SDavid Howells 13727ff50620SDavid Howells if (!size || !maxpages) 13737ff50620SDavid Howells return 0; 13747ff50620SDavid Howells 13757ff50620SDavid Howells pos = i->xarray_start + i->iov_offset; 13767ff50620SDavid Howells index = pos >> PAGE_SHIFT; 13777ff50620SDavid Howells offset = pos & ~PAGE_MASK; 13787ff50620SDavid Howells *_start_offset = offset; 13797ff50620SDavid Howells 13807ff50620SDavid Howells count = 1; 13817ff50620SDavid Howells if (size > PAGE_SIZE - offset) { 13827ff50620SDavid Howells size -= PAGE_SIZE - offset; 13837ff50620SDavid Howells count += size >> PAGE_SHIFT; 13847ff50620SDavid Howells size &= ~PAGE_MASK; 13857ff50620SDavid Howells if (size) 13867ff50620SDavid Howells count++; 13877ff50620SDavid Howells } 13887ff50620SDavid Howells 13897ff50620SDavid Howells if (count > maxpages) 13907ff50620SDavid Howells count = maxpages; 13917ff50620SDavid Howells 13927ff50620SDavid Howells nr = iter_xarray_populate_pages(pages, i->xarray, index, count); 13937ff50620SDavid Howells if (nr == 0) 13947ff50620SDavid Howells return 0; 13957ff50620SDavid Howells 13961c27f1fcSLinus Torvalds return min_t(size_t, nr * PAGE_SIZE - offset, maxsize); 13977ff50620SDavid Howells } 13987ff50620SDavid Howells 1399fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */ 1400dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size) 14013d671ca6SAl Viro { 14023d671ca6SAl Viro size_t skip; 14033d671ca6SAl Viro long k; 14043d671ca6SAl Viro 1405fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1406fcb14cb1SAl Viro return (unsigned long)i->ubuf + i->iov_offset; 1407fcb14cb1SAl Viro 14083d671ca6SAl Viro for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) { 14093d671ca6SAl Viro size_t len = i->iov[k].iov_len - skip; 14103d671ca6SAl Viro 14113d671ca6SAl Viro if (unlikely(!len)) 14123d671ca6SAl Viro continue; 141359dbd7d0SAl Viro if (*size > len) 14143d671ca6SAl Viro *size = len; 1415dd45ab9dSAl Viro return (unsigned long)i->iov[k].iov_base + skip; 14163d671ca6SAl Viro } 14173d671ca6SAl Viro BUG(); // if it had been empty, we wouldn't get called 14183d671ca6SAl Viro } 14193d671ca6SAl Viro 14203d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */ 14213d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i, 142259dbd7d0SAl Viro size_t *size, size_t *start) 14233d671ca6SAl Viro { 14243d671ca6SAl Viro struct page *page; 14253d671ca6SAl Viro size_t skip = i->iov_offset, len; 14263d671ca6SAl Viro 14273d671ca6SAl Viro len = i->bvec->bv_len - skip; 142859dbd7d0SAl Viro if (*size > len) 142959dbd7d0SAl Viro *size = len; 14303d671ca6SAl Viro skip += i->bvec->bv_offset; 14313d671ca6SAl Viro page = i->bvec->bv_page + skip / PAGE_SIZE; 1432dda8e5d1SAl Viro *start = skip % PAGE_SIZE; 14333d671ca6SAl Viro return page; 14343d671ca6SAl Viro } 14353d671ca6SAl Viro 1436d879cb83SAl Viro ssize_t iov_iter_get_pages(struct iov_iter *i, 1437d879cb83SAl Viro struct page **pages, size_t maxsize, unsigned maxpages, 1438d879cb83SAl Viro size_t *start) 1439d879cb83SAl Viro { 14403d671ca6SAl Viro int n, res; 14413d671ca6SAl Viro 1442d879cb83SAl Viro if (maxsize > i->count) 1443d879cb83SAl Viro maxsize = i->count; 14443d671ca6SAl Viro if (!maxsize) 14453d671ca6SAl Viro return 0; 14467392ed17SAl Viro if (maxsize > MAX_RW_COUNT) 14477392ed17SAl Viro maxsize = MAX_RW_COUNT; 1448d879cb83SAl Viro 1449fcb14cb1SAl Viro if (likely(user_backed_iter(i))) { 14503337ab08SAndreas Gruenbacher unsigned int gup_flags = 0; 14513d671ca6SAl Viro unsigned long addr; 14529ea9ce04SDavid Howells 14533337ab08SAndreas Gruenbacher if (iov_iter_rw(i) != WRITE) 14543337ab08SAndreas Gruenbacher gup_flags |= FOLL_WRITE; 14553337ab08SAndreas Gruenbacher if (i->nofault) 14563337ab08SAndreas Gruenbacher gup_flags |= FOLL_NOFAULT; 14573337ab08SAndreas Gruenbacher 1458dd45ab9dSAl Viro addr = first_iovec_segment(i, &maxsize); 1459dd45ab9dSAl Viro *start = addr % PAGE_SIZE; 1460dd45ab9dSAl Viro addr &= PAGE_MASK; 146159dbd7d0SAl Viro n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE); 1462dda8e5d1SAl Viro if (n > maxpages) 1463dda8e5d1SAl Viro n = maxpages; 14643337ab08SAndreas Gruenbacher res = get_user_pages_fast(addr, n, gup_flags, pages); 1465814a6674SAndreas Gruenbacher if (unlikely(res <= 0)) 1466d879cb83SAl Viro return res; 146759dbd7d0SAl Viro return min_t(size_t, maxsize, res * PAGE_SIZE - *start); 14683d671ca6SAl Viro } 14693d671ca6SAl Viro if (iov_iter_is_bvec(i)) { 14703d671ca6SAl Viro struct page *page; 14713d671ca6SAl Viro 147259dbd7d0SAl Viro page = first_bvec_segment(i, &maxsize, start); 147359dbd7d0SAl Viro n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE); 1474dda8e5d1SAl Viro if (n > maxpages) 1475dda8e5d1SAl Viro n = maxpages; 1476dda8e5d1SAl Viro for (int k = 0; k < n; k++) 14773d671ca6SAl Viro get_page(*pages++ = page++); 147859dbd7d0SAl Viro return min_t(size_t, maxsize, n * PAGE_SIZE - *start); 14793d671ca6SAl Viro } 14803d671ca6SAl Viro if (iov_iter_is_pipe(i)) 14813d671ca6SAl Viro return pipe_get_pages(i, pages, maxsize, maxpages, start); 14823d671ca6SAl Viro if (iov_iter_is_xarray(i)) 14833d671ca6SAl Viro return iter_xarray_get_pages(i, pages, maxsize, maxpages, start); 1484d879cb83SAl Viro return -EFAULT; 1485d879cb83SAl Viro } 1486d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_get_pages); 1487d879cb83SAl Viro 1488d879cb83SAl Viro static struct page **get_pages_array(size_t n) 1489d879cb83SAl Viro { 1490752ade68SMichal Hocko return kvmalloc_array(n, sizeof(struct page *), GFP_KERNEL); 1491d879cb83SAl Viro } 1492d879cb83SAl Viro 1493241699cdSAl Viro static ssize_t pipe_get_pages_alloc(struct iov_iter *i, 1494241699cdSAl Viro struct page ***pages, size_t maxsize, 1495241699cdSAl Viro size_t *start) 1496241699cdSAl Viro { 1497241699cdSAl Viro struct page **p; 14988cefc107SDavid Howells unsigned int iter_head, npages; 1499d7760d63SIlya Dryomov ssize_t n; 1500241699cdSAl Viro 1501241699cdSAl Viro if (!sanity(i)) 1502241699cdSAl Viro return -EFAULT; 1503241699cdSAl Viro 15048cefc107SDavid Howells data_start(i, &iter_head, start); 15058cefc107SDavid Howells /* Amount of free space: some of this one + all after this one */ 15068cefc107SDavid Howells npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe); 1507241699cdSAl Viro n = npages * PAGE_SIZE - *start; 1508241699cdSAl Viro if (maxsize > n) 1509241699cdSAl Viro maxsize = n; 1510241699cdSAl Viro else 1511241699cdSAl Viro npages = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE); 1512241699cdSAl Viro p = get_pages_array(npages); 1513241699cdSAl Viro if (!p) 1514241699cdSAl Viro return -ENOMEM; 1515ca591967SAl Viro n = __pipe_get_pages(i, maxsize, p, *start); 1516241699cdSAl Viro if (n > 0) 1517241699cdSAl Viro *pages = p; 1518241699cdSAl Viro else 1519241699cdSAl Viro kvfree(p); 1520241699cdSAl Viro return n; 1521241699cdSAl Viro } 1522241699cdSAl Viro 15237ff50620SDavid Howells static ssize_t iter_xarray_get_pages_alloc(struct iov_iter *i, 15247ff50620SDavid Howells struct page ***pages, size_t maxsize, 15257ff50620SDavid Howells size_t *_start_offset) 15267ff50620SDavid Howells { 15277ff50620SDavid Howells struct page **p; 15287ff50620SDavid Howells unsigned nr, offset; 15297ff50620SDavid Howells pgoff_t index, count; 15306c776766SDavid Howells size_t size = maxsize; 15317ff50620SDavid Howells loff_t pos; 15327ff50620SDavid Howells 15337ff50620SDavid Howells if (!size) 15347ff50620SDavid Howells return 0; 15357ff50620SDavid Howells 15367ff50620SDavid Howells pos = i->xarray_start + i->iov_offset; 15377ff50620SDavid Howells index = pos >> PAGE_SHIFT; 15387ff50620SDavid Howells offset = pos & ~PAGE_MASK; 15397ff50620SDavid Howells *_start_offset = offset; 15407ff50620SDavid Howells 15417ff50620SDavid Howells count = 1; 15427ff50620SDavid Howells if (size > PAGE_SIZE - offset) { 15437ff50620SDavid Howells size -= PAGE_SIZE - offset; 15447ff50620SDavid Howells count += size >> PAGE_SHIFT; 15457ff50620SDavid Howells size &= ~PAGE_MASK; 15467ff50620SDavid Howells if (size) 15477ff50620SDavid Howells count++; 15487ff50620SDavid Howells } 15497ff50620SDavid Howells 15507ff50620SDavid Howells p = get_pages_array(count); 15517ff50620SDavid Howells if (!p) 15527ff50620SDavid Howells return -ENOMEM; 15537ff50620SDavid Howells *pages = p; 15547ff50620SDavid Howells 15557ff50620SDavid Howells nr = iter_xarray_populate_pages(p, i->xarray, index, count); 15567ff50620SDavid Howells if (nr == 0) 15577ff50620SDavid Howells return 0; 15587ff50620SDavid Howells 15591c27f1fcSLinus Torvalds return min_t(size_t, nr * PAGE_SIZE - offset, maxsize); 15607ff50620SDavid Howells } 15617ff50620SDavid Howells 1562d879cb83SAl Viro ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, 1563d879cb83SAl Viro struct page ***pages, size_t maxsize, 1564d879cb83SAl Viro size_t *start) 1565d879cb83SAl Viro { 1566d879cb83SAl Viro struct page **p; 15673d671ca6SAl Viro int n, res; 1568d879cb83SAl Viro 1569d879cb83SAl Viro if (maxsize > i->count) 1570d879cb83SAl Viro maxsize = i->count; 15713d671ca6SAl Viro if (!maxsize) 15723d671ca6SAl Viro return 0; 15737392ed17SAl Viro if (maxsize > MAX_RW_COUNT) 15747392ed17SAl Viro maxsize = MAX_RW_COUNT; 1575d879cb83SAl Viro 1576fcb14cb1SAl Viro if (likely(user_backed_iter(i))) { 15773337ab08SAndreas Gruenbacher unsigned int gup_flags = 0; 15783d671ca6SAl Viro unsigned long addr; 15799ea9ce04SDavid Howells 15803337ab08SAndreas Gruenbacher if (iov_iter_rw(i) != WRITE) 15813337ab08SAndreas Gruenbacher gup_flags |= FOLL_WRITE; 15823337ab08SAndreas Gruenbacher if (i->nofault) 15833337ab08SAndreas Gruenbacher gup_flags |= FOLL_NOFAULT; 15843337ab08SAndreas Gruenbacher 1585dd45ab9dSAl Viro addr = first_iovec_segment(i, &maxsize); 1586dd45ab9dSAl Viro *start = addr % PAGE_SIZE; 1587dd45ab9dSAl Viro addr &= PAGE_MASK; 158859dbd7d0SAl Viro n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE); 1589d879cb83SAl Viro p = get_pages_array(n); 1590d879cb83SAl Viro if (!p) 1591d879cb83SAl Viro return -ENOMEM; 15923337ab08SAndreas Gruenbacher res = get_user_pages_fast(addr, n, gup_flags, p); 1593814a6674SAndreas Gruenbacher if (unlikely(res <= 0)) { 1594d879cb83SAl Viro kvfree(p); 1595814a6674SAndreas Gruenbacher *pages = NULL; 1596d879cb83SAl Viro return res; 1597d879cb83SAl Viro } 1598d879cb83SAl Viro *pages = p; 159959dbd7d0SAl Viro return min_t(size_t, maxsize, res * PAGE_SIZE - *start); 16003d671ca6SAl Viro } 16013d671ca6SAl Viro if (iov_iter_is_bvec(i)) { 16023d671ca6SAl Viro struct page *page; 16033d671ca6SAl Viro 160459dbd7d0SAl Viro page = first_bvec_segment(i, &maxsize, start); 160559dbd7d0SAl Viro n = DIV_ROUND_UP(maxsize + *start, PAGE_SIZE); 16063d671ca6SAl Viro *pages = p = get_pages_array(n); 1607d879cb83SAl Viro if (!p) 1608d879cb83SAl Viro return -ENOMEM; 1609dda8e5d1SAl Viro for (int k = 0; k < n; k++) 16103d671ca6SAl Viro get_page(*p++ = page++); 161159dbd7d0SAl Viro return min_t(size_t, maxsize, n * PAGE_SIZE - *start); 16123d671ca6SAl Viro } 16133d671ca6SAl Viro if (iov_iter_is_pipe(i)) 16143d671ca6SAl Viro return pipe_get_pages_alloc(i, pages, maxsize, start); 16153d671ca6SAl Viro if (iov_iter_is_xarray(i)) 16163d671ca6SAl Viro return iter_xarray_get_pages_alloc(i, pages, maxsize, start); 1617d879cb83SAl Viro return -EFAULT; 1618d879cb83SAl Viro } 1619d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc); 1620d879cb83SAl Viro 1621d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, 1622d879cb83SAl Viro struct iov_iter *i) 1623d879cb83SAl Viro { 1624d879cb83SAl Viro __wsum sum, next; 1625d879cb83SAl Viro sum = *csum; 16269ea9ce04SDavid Howells if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { 1627241699cdSAl Viro WARN_ON(1); 1628241699cdSAl Viro return 0; 1629241699cdSAl Viro } 16307baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, ({ 16317baa5099SAl Viro next = csum_and_copy_from_user(base, addr + off, len); 1632d879cb83SAl Viro sum = csum_block_add(sum, next, off); 16337baa5099SAl Viro next ? 0 : len; 1634d879cb83SAl Viro }), ({ 16357baa5099SAl Viro sum = csum_and_memcpy(addr + off, base, len, sum, off); 1636d879cb83SAl Viro }) 1637d879cb83SAl Viro ) 1638d879cb83SAl Viro *csum = sum; 1639d879cb83SAl Viro return bytes; 1640d879cb83SAl Viro } 1641d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter); 1642d879cb83SAl Viro 164352cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate, 1644d879cb83SAl Viro struct iov_iter *i) 1645d879cb83SAl Viro { 164652cbd23aSWillem de Bruijn struct csum_state *csstate = _csstate; 1647d879cb83SAl Viro __wsum sum, next; 164878e1f386SAl Viro 164978e1f386SAl Viro if (unlikely(iov_iter_is_discard(i))) { 1650241699cdSAl Viro WARN_ON(1); /* for now */ 1651241699cdSAl Viro return 0; 1652241699cdSAl Viro } 16536852df12SAl Viro 16546852df12SAl Viro sum = csum_shift(csstate->csum, csstate->off); 16556852df12SAl Viro if (unlikely(iov_iter_is_pipe(i))) 16566852df12SAl Viro bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum); 16576852df12SAl Viro else iterate_and_advance(i, bytes, base, len, off, ({ 16587baa5099SAl Viro next = csum_and_copy_to_user(addr + off, base, len); 1659d879cb83SAl Viro sum = csum_block_add(sum, next, off); 16607baa5099SAl Viro next ? 0 : len; 1661d879cb83SAl Viro }), ({ 16627baa5099SAl Viro sum = csum_and_memcpy(base, addr + off, len, sum, off); 1663d879cb83SAl Viro }) 1664d879cb83SAl Viro ) 1665594e450bSAl Viro csstate->csum = csum_shift(sum, csstate->off); 1666594e450bSAl Viro csstate->off += bytes; 1667d879cb83SAl Viro return bytes; 1668d879cb83SAl Viro } 1669d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter); 1670d879cb83SAl Viro 1671d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, 1672d05f4435SSagi Grimberg struct iov_iter *i) 1673d05f4435SSagi Grimberg { 16747999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH 1675d05f4435SSagi Grimberg struct ahash_request *hash = hashp; 1676d05f4435SSagi Grimberg struct scatterlist sg; 1677d05f4435SSagi Grimberg size_t copied; 1678d05f4435SSagi Grimberg 1679d05f4435SSagi Grimberg copied = copy_to_iter(addr, bytes, i); 1680d05f4435SSagi Grimberg sg_init_one(&sg, addr, copied); 1681d05f4435SSagi Grimberg ahash_request_set_crypt(hash, &sg, NULL, copied); 1682d05f4435SSagi Grimberg crypto_ahash_update(hash); 1683d05f4435SSagi Grimberg return copied; 168427fad74aSYueHaibing #else 168527fad74aSYueHaibing return 0; 168627fad74aSYueHaibing #endif 1687d05f4435SSagi Grimberg } 1688d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter); 1689d05f4435SSagi Grimberg 169066531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages) 1691d879cb83SAl Viro { 169266531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 169366531c65SAl Viro const struct iovec *p; 1694d879cb83SAl Viro int npages = 0; 1695d879cb83SAl Viro 169666531c65SAl Viro for (p = i->iov; size; skip = 0, p++) { 169766531c65SAl Viro unsigned offs = offset_in_page(p->iov_base + skip); 169866531c65SAl Viro size_t len = min(p->iov_len - skip, size); 1699d879cb83SAl Viro 170066531c65SAl Viro if (len) { 170166531c65SAl Viro size -= len; 170266531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 170366531c65SAl Viro if (unlikely(npages > maxpages)) 170466531c65SAl Viro return maxpages; 170566531c65SAl Viro } 170666531c65SAl Viro } 170766531c65SAl Viro return npages; 170866531c65SAl Viro } 170966531c65SAl Viro 171066531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages) 171166531c65SAl Viro { 171266531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 171366531c65SAl Viro const struct bio_vec *p; 171466531c65SAl Viro int npages = 0; 171566531c65SAl Viro 171666531c65SAl Viro for (p = i->bvec; size; skip = 0, p++) { 171766531c65SAl Viro unsigned offs = (p->bv_offset + skip) % PAGE_SIZE; 171866531c65SAl Viro size_t len = min(p->bv_len - skip, size); 171966531c65SAl Viro 172066531c65SAl Viro size -= len; 172166531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 172266531c65SAl Viro if (unlikely(npages > maxpages)) 172366531c65SAl Viro return maxpages; 172466531c65SAl Viro } 172566531c65SAl Viro return npages; 172666531c65SAl Viro } 172766531c65SAl Viro 172866531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages) 172966531c65SAl Viro { 173066531c65SAl Viro if (unlikely(!i->count)) 173166531c65SAl Viro return 0; 1732fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1733fcb14cb1SAl Viro unsigned offs = offset_in_page(i->ubuf + i->iov_offset); 1734fcb14cb1SAl Viro int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE); 1735fcb14cb1SAl Viro return min(npages, maxpages); 1736fcb14cb1SAl Viro } 173766531c65SAl Viro /* iovec and kvec have identical layouts */ 173866531c65SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 173966531c65SAl Viro return iov_npages(i, maxpages); 174066531c65SAl Viro if (iov_iter_is_bvec(i)) 174166531c65SAl Viro return bvec_npages(i, maxpages); 174266531c65SAl Viro if (iov_iter_is_pipe(i)) { 17438cefc107SDavid Howells unsigned int iter_head; 174466531c65SAl Viro int npages; 1745241699cdSAl Viro size_t off; 1746241699cdSAl Viro 1747241699cdSAl Viro if (!sanity(i)) 1748241699cdSAl Viro return 0; 1749241699cdSAl Viro 17508cefc107SDavid Howells data_start(i, &iter_head, &off); 1751241699cdSAl Viro /* some of this one + all after this one */ 175266531c65SAl Viro npages = pipe_space_for_user(iter_head, i->pipe->tail, i->pipe); 175366531c65SAl Viro return min(npages, maxpages); 175466531c65SAl Viro } 175566531c65SAl Viro if (iov_iter_is_xarray(i)) { 1756e4f8df86SAl Viro unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE; 1757e4f8df86SAl Viro int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE); 175866531c65SAl Viro return min(npages, maxpages); 175966531c65SAl Viro } 176066531c65SAl Viro return 0; 1761d879cb83SAl Viro } 1762d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages); 1763d879cb83SAl Viro 1764d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags) 1765d879cb83SAl Viro { 1766d879cb83SAl Viro *new = *old; 176700e23707SDavid Howells if (unlikely(iov_iter_is_pipe(new))) { 1768241699cdSAl Viro WARN_ON(1); 1769241699cdSAl Viro return NULL; 1770241699cdSAl Viro } 177100e23707SDavid Howells if (iov_iter_is_bvec(new)) 1772d879cb83SAl Viro return new->bvec = kmemdup(new->bvec, 1773d879cb83SAl Viro new->nr_segs * sizeof(struct bio_vec), 1774d879cb83SAl Viro flags); 1775fcb14cb1SAl Viro else if (iov_iter_is_kvec(new) || iter_is_iovec(new)) 1776d879cb83SAl Viro /* iovec and kvec have identical layout */ 1777d879cb83SAl Viro return new->iov = kmemdup(new->iov, 1778d879cb83SAl Viro new->nr_segs * sizeof(struct iovec), 1779d879cb83SAl Viro flags); 1780fcb14cb1SAl Viro return NULL; 1781d879cb83SAl Viro } 1782d879cb83SAl Viro EXPORT_SYMBOL(dup_iter); 1783bc917be8SAl Viro 1784bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov, 1785bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1786bfdc5970SChristoph Hellwig { 1787bfdc5970SChristoph Hellwig const struct compat_iovec __user *uiov = 1788bfdc5970SChristoph Hellwig (const struct compat_iovec __user *)uvec; 1789bfdc5970SChristoph Hellwig int ret = -EFAULT, i; 1790bfdc5970SChristoph Hellwig 1791a959a978SChristoph Hellwig if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) 1792bfdc5970SChristoph Hellwig return -EFAULT; 1793bfdc5970SChristoph Hellwig 1794bfdc5970SChristoph Hellwig for (i = 0; i < nr_segs; i++) { 1795bfdc5970SChristoph Hellwig compat_uptr_t buf; 1796bfdc5970SChristoph Hellwig compat_ssize_t len; 1797bfdc5970SChristoph Hellwig 1798bfdc5970SChristoph Hellwig unsafe_get_user(len, &uiov[i].iov_len, uaccess_end); 1799bfdc5970SChristoph Hellwig unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end); 1800bfdc5970SChristoph Hellwig 1801bfdc5970SChristoph Hellwig /* check for compat_size_t not fitting in compat_ssize_t .. */ 1802bfdc5970SChristoph Hellwig if (len < 0) { 1803bfdc5970SChristoph Hellwig ret = -EINVAL; 1804bfdc5970SChristoph Hellwig goto uaccess_end; 1805bfdc5970SChristoph Hellwig } 1806bfdc5970SChristoph Hellwig iov[i].iov_base = compat_ptr(buf); 1807bfdc5970SChristoph Hellwig iov[i].iov_len = len; 1808bfdc5970SChristoph Hellwig } 1809bfdc5970SChristoph Hellwig 1810bfdc5970SChristoph Hellwig ret = 0; 1811bfdc5970SChristoph Hellwig uaccess_end: 1812bfdc5970SChristoph Hellwig user_access_end(); 1813bfdc5970SChristoph Hellwig return ret; 1814bfdc5970SChristoph Hellwig } 1815bfdc5970SChristoph Hellwig 1816bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov, 1817bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1818fb041b59SDavid Laight { 1819fb041b59SDavid Laight unsigned long seg; 1820bfdc5970SChristoph Hellwig 1821bfdc5970SChristoph Hellwig if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec))) 1822bfdc5970SChristoph Hellwig return -EFAULT; 1823bfdc5970SChristoph Hellwig for (seg = 0; seg < nr_segs; seg++) { 1824bfdc5970SChristoph Hellwig if ((ssize_t)iov[seg].iov_len < 0) 1825bfdc5970SChristoph Hellwig return -EINVAL; 1826bfdc5970SChristoph Hellwig } 1827bfdc5970SChristoph Hellwig 1828bfdc5970SChristoph Hellwig return 0; 1829bfdc5970SChristoph Hellwig } 1830bfdc5970SChristoph Hellwig 1831bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec, 1832bfdc5970SChristoph Hellwig unsigned long nr_segs, unsigned long fast_segs, 1833bfdc5970SChristoph Hellwig struct iovec *fast_iov, bool compat) 1834bfdc5970SChristoph Hellwig { 1835bfdc5970SChristoph Hellwig struct iovec *iov = fast_iov; 1836bfdc5970SChristoph Hellwig int ret; 1837fb041b59SDavid Laight 1838fb041b59SDavid Laight /* 1839bfdc5970SChristoph Hellwig * SuS says "The readv() function *may* fail if the iovcnt argument was 1840bfdc5970SChristoph Hellwig * less than or equal to 0, or greater than {IOV_MAX}. Linux has 1841fb041b59SDavid Laight * traditionally returned zero for zero segments, so... 1842fb041b59SDavid Laight */ 1843bfdc5970SChristoph Hellwig if (nr_segs == 0) 1844bfdc5970SChristoph Hellwig return iov; 1845bfdc5970SChristoph Hellwig if (nr_segs > UIO_MAXIOV) 1846bfdc5970SChristoph Hellwig return ERR_PTR(-EINVAL); 1847fb041b59SDavid Laight if (nr_segs > fast_segs) { 1848fb041b59SDavid Laight iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); 1849bfdc5970SChristoph Hellwig if (!iov) 1850bfdc5970SChristoph Hellwig return ERR_PTR(-ENOMEM); 1851fb041b59SDavid Laight } 1852bfdc5970SChristoph Hellwig 1853bfdc5970SChristoph Hellwig if (compat) 1854bfdc5970SChristoph Hellwig ret = copy_compat_iovec_from_user(iov, uvec, nr_segs); 1855bfdc5970SChristoph Hellwig else 1856bfdc5970SChristoph Hellwig ret = copy_iovec_from_user(iov, uvec, nr_segs); 1857bfdc5970SChristoph Hellwig if (ret) { 1858bfdc5970SChristoph Hellwig if (iov != fast_iov) 1859bfdc5970SChristoph Hellwig kfree(iov); 1860bfdc5970SChristoph Hellwig return ERR_PTR(ret); 1861fb041b59SDavid Laight } 1862bfdc5970SChristoph Hellwig 1863bfdc5970SChristoph Hellwig return iov; 1864bfdc5970SChristoph Hellwig } 1865bfdc5970SChristoph Hellwig 1866bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec, 1867bfdc5970SChristoph Hellwig unsigned nr_segs, unsigned fast_segs, struct iovec **iovp, 1868bfdc5970SChristoph Hellwig struct iov_iter *i, bool compat) 1869bfdc5970SChristoph Hellwig { 1870bfdc5970SChristoph Hellwig ssize_t total_len = 0; 1871bfdc5970SChristoph Hellwig unsigned long seg; 1872bfdc5970SChristoph Hellwig struct iovec *iov; 1873bfdc5970SChristoph Hellwig 1874bfdc5970SChristoph Hellwig iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat); 1875bfdc5970SChristoph Hellwig if (IS_ERR(iov)) { 1876bfdc5970SChristoph Hellwig *iovp = NULL; 1877bfdc5970SChristoph Hellwig return PTR_ERR(iov); 1878fb041b59SDavid Laight } 1879fb041b59SDavid Laight 1880fb041b59SDavid Laight /* 1881bfdc5970SChristoph Hellwig * According to the Single Unix Specification we should return EINVAL if 1882bfdc5970SChristoph Hellwig * an element length is < 0 when cast to ssize_t or if the total length 1883bfdc5970SChristoph Hellwig * would overflow the ssize_t return value of the system call. 1884fb041b59SDavid Laight * 1885fb041b59SDavid Laight * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the 1886fb041b59SDavid Laight * overflow case. 1887fb041b59SDavid Laight */ 1888fb041b59SDavid Laight for (seg = 0; seg < nr_segs; seg++) { 1889fb041b59SDavid Laight ssize_t len = (ssize_t)iov[seg].iov_len; 1890fb041b59SDavid Laight 1891bfdc5970SChristoph Hellwig if (!access_ok(iov[seg].iov_base, len)) { 1892bfdc5970SChristoph Hellwig if (iov != *iovp) 1893bfdc5970SChristoph Hellwig kfree(iov); 1894bfdc5970SChristoph Hellwig *iovp = NULL; 1895bfdc5970SChristoph Hellwig return -EFAULT; 1896fb041b59SDavid Laight } 1897bfdc5970SChristoph Hellwig 1898bfdc5970SChristoph Hellwig if (len > MAX_RW_COUNT - total_len) { 1899bfdc5970SChristoph Hellwig len = MAX_RW_COUNT - total_len; 1900fb041b59SDavid Laight iov[seg].iov_len = len; 1901fb041b59SDavid Laight } 1902bfdc5970SChristoph Hellwig total_len += len; 1903fb041b59SDavid Laight } 1904bfdc5970SChristoph Hellwig 1905bfdc5970SChristoph Hellwig iov_iter_init(i, type, iov, nr_segs, total_len); 1906bfdc5970SChristoph Hellwig if (iov == *iovp) 1907bfdc5970SChristoph Hellwig *iovp = NULL; 1908bfdc5970SChristoph Hellwig else 1909bfdc5970SChristoph Hellwig *iovp = iov; 1910bfdc5970SChristoph Hellwig return total_len; 1911fb041b59SDavid Laight } 1912fb041b59SDavid Laight 1913ffecee4fSVegard Nossum /** 1914ffecee4fSVegard Nossum * import_iovec() - Copy an array of &struct iovec from userspace 1915ffecee4fSVegard Nossum * into the kernel, check that it is valid, and initialize a new 1916ffecee4fSVegard Nossum * &struct iov_iter iterator to access it. 1917ffecee4fSVegard Nossum * 1918ffecee4fSVegard Nossum * @type: One of %READ or %WRITE. 1919bfdc5970SChristoph Hellwig * @uvec: Pointer to the userspace array. 1920ffecee4fSVegard Nossum * @nr_segs: Number of elements in userspace array. 1921ffecee4fSVegard Nossum * @fast_segs: Number of elements in @iov. 1922bfdc5970SChristoph Hellwig * @iovp: (input and output parameter) Pointer to pointer to (usually small 1923ffecee4fSVegard Nossum * on-stack) kernel array. 1924ffecee4fSVegard Nossum * @i: Pointer to iterator that will be initialized on success. 1925ffecee4fSVegard Nossum * 1926ffecee4fSVegard Nossum * If the array pointed to by *@iov is large enough to hold all @nr_segs, 1927ffecee4fSVegard Nossum * then this function places %NULL in *@iov on return. Otherwise, a new 1928ffecee4fSVegard Nossum * array will be allocated and the result placed in *@iov. This means that 1929ffecee4fSVegard Nossum * the caller may call kfree() on *@iov regardless of whether the small 1930ffecee4fSVegard Nossum * on-stack array was used or not (and regardless of whether this function 1931ffecee4fSVegard Nossum * returns an error or not). 1932ffecee4fSVegard Nossum * 193387e5e6daSJens Axboe * Return: Negative error code on error, bytes imported on success 1934ffecee4fSVegard Nossum */ 1935bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec, 1936bc917be8SAl Viro unsigned nr_segs, unsigned fast_segs, 1937bfdc5970SChristoph Hellwig struct iovec **iovp, struct iov_iter *i) 1938bc917be8SAl Viro { 193989cd35c5SChristoph Hellwig return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i, 194089cd35c5SChristoph Hellwig in_compat_syscall()); 1941bc917be8SAl Viro } 1942bc917be8SAl Viro EXPORT_SYMBOL(import_iovec); 1943bc917be8SAl Viro 1944bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len, 1945bc917be8SAl Viro struct iovec *iov, struct iov_iter *i) 1946bc917be8SAl Viro { 1947bc917be8SAl Viro if (len > MAX_RW_COUNT) 1948bc917be8SAl Viro len = MAX_RW_COUNT; 194996d4f267SLinus Torvalds if (unlikely(!access_ok(buf, len))) 1950bc917be8SAl Viro return -EFAULT; 1951bc917be8SAl Viro 1952bc917be8SAl Viro iov->iov_base = buf; 1953bc917be8SAl Viro iov->iov_len = len; 1954bc917be8SAl Viro iov_iter_init(i, rw, iov, 1, len); 1955bc917be8SAl Viro return 0; 1956bc917be8SAl Viro } 1957e1267585SAl Viro EXPORT_SYMBOL(import_single_range); 19588fb0f47aSJens Axboe 19598fb0f47aSJens Axboe /** 19608fb0f47aSJens Axboe * iov_iter_restore() - Restore a &struct iov_iter to the same state as when 19618fb0f47aSJens Axboe * iov_iter_save_state() was called. 19628fb0f47aSJens Axboe * 19638fb0f47aSJens Axboe * @i: &struct iov_iter to restore 19648fb0f47aSJens Axboe * @state: state to restore from 19658fb0f47aSJens Axboe * 19668fb0f47aSJens Axboe * Used after iov_iter_save_state() to bring restore @i, if operations may 19678fb0f47aSJens Axboe * have advanced it. 19688fb0f47aSJens Axboe * 19698fb0f47aSJens Axboe * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC 19708fb0f47aSJens Axboe */ 19718fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) 19728fb0f47aSJens Axboe { 19738fb0f47aSJens Axboe if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) && 1974fcb14cb1SAl Viro !iov_iter_is_kvec(i) && !iter_is_ubuf(i)) 19758fb0f47aSJens Axboe return; 19768fb0f47aSJens Axboe i->iov_offset = state->iov_offset; 19778fb0f47aSJens Axboe i->count = state->count; 1978fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1979fcb14cb1SAl Viro return; 19808fb0f47aSJens Axboe /* 19818fb0f47aSJens Axboe * For the *vec iters, nr_segs + iov is constant - if we increment 19828fb0f47aSJens Axboe * the vec, then we also decrement the nr_segs count. Hence we don't 19838fb0f47aSJens Axboe * need to track both of these, just one is enough and we can deduct 19848fb0f47aSJens Axboe * the other from that. ITER_KVEC and ITER_IOVEC are the same struct 19858fb0f47aSJens Axboe * size, so we can just increment the iov pointer as they are unionzed. 19868fb0f47aSJens Axboe * ITER_BVEC _may_ be the same size on some archs, but on others it is 19878fb0f47aSJens Axboe * not. Be safe and handle it separately. 19888fb0f47aSJens Axboe */ 19898fb0f47aSJens Axboe BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec)); 19908fb0f47aSJens Axboe if (iov_iter_is_bvec(i)) 19918fb0f47aSJens Axboe i->bvec -= state->nr_segs - i->nr_segs; 19928fb0f47aSJens Axboe else 19938fb0f47aSJens Axboe i->iov -= state->nr_segs - i->nr_segs; 19948fb0f47aSJens Axboe i->nr_segs = state->nr_segs; 19958fb0f47aSJens Axboe } 1996