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 { 17733b75c1dSAlexander Potapenko size_t res = n; 17833b75c1dSAlexander Potapenko 1794d0e9df5SAlbert van der Linde if (should_fail_usercopy()) 1804d0e9df5SAlbert van der Linde return n; 18196d4f267SLinus Torvalds if (access_ok(from, n)) { 18233b75c1dSAlexander Potapenko instrument_copy_from_user_before(to, from, n); 18333b75c1dSAlexander Potapenko res = raw_copy_from_user(to, from, n); 18433b75c1dSAlexander Potapenko instrument_copy_from_user_after(to, from, n, res); 18509fc68dcSAl Viro } 18633b75c1dSAlexander Potapenko return res; 18709fc68dcSAl Viro } 18809fc68dcSAl Viro 1892dcedb2aSAl Viro static inline struct pipe_buffer *pipe_buf(const struct pipe_inode_info *pipe, 1902dcedb2aSAl Viro unsigned int slot) 1912dcedb2aSAl Viro { 1922dcedb2aSAl Viro return &pipe->bufs[slot & (pipe->ring_size - 1)]; 1932dcedb2aSAl Viro } 1942dcedb2aSAl Viro 195241699cdSAl Viro #ifdef PIPE_PARANOIA 196241699cdSAl Viro static bool sanity(const struct iov_iter *i) 197241699cdSAl Viro { 198241699cdSAl Viro struct pipe_inode_info *pipe = i->pipe; 1998cefc107SDavid Howells unsigned int p_head = pipe->head; 2008cefc107SDavid Howells unsigned int p_tail = pipe->tail; 2018cefc107SDavid Howells unsigned int p_occupancy = pipe_occupancy(p_head, p_tail); 2028cefc107SDavid Howells unsigned int i_head = i->head; 2038cefc107SDavid Howells unsigned int idx; 2048cefc107SDavid Howells 20510f525a8SAl Viro if (i->last_offset) { 206241699cdSAl Viro struct pipe_buffer *p; 2078cefc107SDavid Howells if (unlikely(p_occupancy == 0)) 208241699cdSAl Viro goto Bad; // pipe must be non-empty 2098cefc107SDavid Howells if (unlikely(i_head != p_head - 1)) 210241699cdSAl Viro goto Bad; // must be at the last buffer... 211241699cdSAl Viro 2122dcedb2aSAl Viro p = pipe_buf(pipe, i_head); 21310f525a8SAl Viro if (unlikely(p->offset + p->len != abs(i->last_offset))) 214241699cdSAl Viro goto Bad; // ... at the end of segment 215241699cdSAl Viro } else { 2168cefc107SDavid Howells if (i_head != p_head) 217241699cdSAl Viro goto Bad; // must be right after the last buffer 218241699cdSAl Viro } 219241699cdSAl Viro return true; 220241699cdSAl Viro Bad: 22110f525a8SAl Viro printk(KERN_ERR "idx = %d, offset = %d\n", i_head, i->last_offset); 2228cefc107SDavid Howells printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n", 2238cefc107SDavid Howells p_head, p_tail, pipe->ring_size); 2248cefc107SDavid Howells for (idx = 0; idx < pipe->ring_size; idx++) 225241699cdSAl Viro printk(KERN_ERR "[%p %p %d %d]\n", 226241699cdSAl Viro pipe->bufs[idx].ops, 227241699cdSAl Viro pipe->bufs[idx].page, 228241699cdSAl Viro pipe->bufs[idx].offset, 229241699cdSAl Viro pipe->bufs[idx].len); 230241699cdSAl Viro WARN_ON(1); 231241699cdSAl Viro return false; 232241699cdSAl Viro } 233241699cdSAl Viro #else 234241699cdSAl Viro #define sanity(i) true 235241699cdSAl Viro #endif 236241699cdSAl Viro 23747b7fcaeSAl Viro static struct page *push_anon(struct pipe_inode_info *pipe, unsigned size) 23847b7fcaeSAl Viro { 23947b7fcaeSAl Viro struct page *page = alloc_page(GFP_USER); 24047b7fcaeSAl Viro if (page) { 24147b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++); 24247b7fcaeSAl Viro *buf = (struct pipe_buffer) { 24347b7fcaeSAl Viro .ops = &default_pipe_buf_ops, 24447b7fcaeSAl Viro .page = page, 24547b7fcaeSAl Viro .offset = 0, 24647b7fcaeSAl Viro .len = size 24747b7fcaeSAl Viro }; 24847b7fcaeSAl Viro } 24947b7fcaeSAl Viro return page; 25047b7fcaeSAl Viro } 25147b7fcaeSAl Viro 25247b7fcaeSAl Viro static void push_page(struct pipe_inode_info *pipe, struct page *page, 25347b7fcaeSAl Viro unsigned int offset, unsigned int size) 25447b7fcaeSAl Viro { 25547b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++); 25647b7fcaeSAl Viro *buf = (struct pipe_buffer) { 25747b7fcaeSAl Viro .ops = &page_cache_pipe_buf_ops, 25847b7fcaeSAl Viro .page = page, 25947b7fcaeSAl Viro .offset = offset, 26047b7fcaeSAl Viro .len = size 26147b7fcaeSAl Viro }; 26247b7fcaeSAl Viro get_page(page); 26347b7fcaeSAl Viro } 26447b7fcaeSAl Viro 26510f525a8SAl Viro static inline int last_offset(const struct pipe_buffer *buf) 2668fad7767SAl Viro { 26710f525a8SAl Viro if (buf->ops == &default_pipe_buf_ops) 26810f525a8SAl Viro return buf->len; // buf->offset is 0 for those 26910f525a8SAl Viro else 27010f525a8SAl Viro return -(buf->offset + buf->len); 2718fad7767SAl Viro } 2728fad7767SAl Viro 2738fad7767SAl Viro static struct page *append_pipe(struct iov_iter *i, size_t size, 2748fad7767SAl Viro unsigned int *off) 2758fad7767SAl Viro { 2768fad7767SAl Viro struct pipe_inode_info *pipe = i->pipe; 27710f525a8SAl Viro int offset = i->last_offset; 2788fad7767SAl Viro struct pipe_buffer *buf; 2798fad7767SAl Viro struct page *page; 2808fad7767SAl Viro 28110f525a8SAl Viro if (offset > 0 && offset < PAGE_SIZE) { 28210f525a8SAl Viro // some space in the last buffer; add to it 2838fad7767SAl Viro buf = pipe_buf(pipe, pipe->head - 1); 2848fad7767SAl Viro size = min_t(size_t, size, PAGE_SIZE - offset); 2858fad7767SAl Viro buf->len += size; 28610f525a8SAl Viro i->last_offset += size; 2878fad7767SAl Viro i->count -= size; 2888fad7767SAl Viro *off = offset; 2898fad7767SAl Viro return buf->page; 2908fad7767SAl Viro } 2918fad7767SAl Viro // OK, we need a new buffer 2928fad7767SAl Viro *off = 0; 2938fad7767SAl Viro size = min_t(size_t, size, PAGE_SIZE); 2948fad7767SAl Viro if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 2958fad7767SAl Viro return NULL; 2968fad7767SAl Viro page = push_anon(pipe, size); 2978fad7767SAl Viro if (!page) 2988fad7767SAl Viro return NULL; 2998fad7767SAl Viro i->head = pipe->head - 1; 30010f525a8SAl Viro i->last_offset = size; 3018fad7767SAl Viro i->count -= size; 3028fad7767SAl Viro return page; 3038fad7767SAl Viro } 3048fad7767SAl Viro 305241699cdSAl Viro static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes, 306241699cdSAl Viro struct iov_iter *i) 307241699cdSAl Viro { 308241699cdSAl Viro struct pipe_inode_info *pipe = i->pipe; 30947b7fcaeSAl Viro unsigned int head = pipe->head; 310241699cdSAl Viro 311241699cdSAl Viro if (unlikely(bytes > i->count)) 312241699cdSAl Viro bytes = i->count; 313241699cdSAl Viro 314241699cdSAl Viro if (unlikely(!bytes)) 315241699cdSAl Viro return 0; 316241699cdSAl Viro 317241699cdSAl Viro if (!sanity(i)) 318241699cdSAl Viro return 0; 319241699cdSAl Viro 32010f525a8SAl Viro if (offset && i->last_offset == -offset) { // could we merge it? 32147b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, head - 1); 32247b7fcaeSAl Viro if (buf->page == page) { 323241699cdSAl Viro buf->len += bytes; 32410f525a8SAl Viro i->last_offset -= bytes; 32547b7fcaeSAl Viro i->count -= bytes; 32647b7fcaeSAl Viro return bytes; 327241699cdSAl Viro } 328241699cdSAl Viro } 32947b7fcaeSAl Viro if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 330241699cdSAl Viro return 0; 3318cefc107SDavid Howells 33247b7fcaeSAl Viro push_page(pipe, page, offset, bytes); 33310f525a8SAl Viro i->last_offset = -(offset + bytes); 33447b7fcaeSAl Viro i->head = head; 335241699cdSAl Viro i->count -= bytes; 336241699cdSAl Viro return bytes; 337241699cdSAl Viro } 338241699cdSAl Viro 339d879cb83SAl Viro /* 340a6294593SAndreas Gruenbacher * fault_in_iov_iter_readable - fault in iov iterator for reading 341a6294593SAndreas Gruenbacher * @i: iterator 342a6294593SAndreas Gruenbacher * @size: maximum length 343171a0203SAnton Altaparmakov * 344a6294593SAndreas Gruenbacher * Fault in one or more iovecs of the given iov_iter, to a maximum length of 345a6294593SAndreas Gruenbacher * @size. For each iovec, fault in each page that constitutes the iovec. 346a6294593SAndreas Gruenbacher * 347a6294593SAndreas Gruenbacher * Returns the number of bytes not faulted in (like copy_to_user() and 348a6294593SAndreas Gruenbacher * copy_from_user()). 349a6294593SAndreas Gruenbacher * 350a6294593SAndreas Gruenbacher * Always returns 0 for non-userspace iterators. 351171a0203SAnton Altaparmakov */ 352a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size) 353171a0203SAnton Altaparmakov { 354fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 355fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 356fcb14cb1SAl Viro n -= fault_in_readable(i->ubuf + i->iov_offset, n); 357fcb14cb1SAl Viro return size - n; 358fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 359a6294593SAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 3608409a0d2SAl Viro const struct iovec *p; 3618409a0d2SAl Viro size_t skip; 3628409a0d2SAl Viro 363a6294593SAndreas Gruenbacher size -= count; 364a6294593SAndreas Gruenbacher for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) { 365a6294593SAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 366a6294593SAndreas Gruenbacher size_t ret; 3678409a0d2SAl Viro 3688409a0d2SAl Viro if (unlikely(!len)) 3698409a0d2SAl Viro continue; 370a6294593SAndreas Gruenbacher ret = fault_in_readable(p->iov_base + skip, len); 371a6294593SAndreas Gruenbacher count -= len - ret; 372a6294593SAndreas Gruenbacher if (ret) 373a6294593SAndreas Gruenbacher break; 3748409a0d2SAl Viro } 375a6294593SAndreas Gruenbacher return count + size; 376171a0203SAnton Altaparmakov } 377171a0203SAnton Altaparmakov return 0; 378171a0203SAnton Altaparmakov } 379a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable); 380171a0203SAnton Altaparmakov 381cdd591fcSAndreas Gruenbacher /* 382cdd591fcSAndreas Gruenbacher * fault_in_iov_iter_writeable - fault in iov iterator for writing 383cdd591fcSAndreas Gruenbacher * @i: iterator 384cdd591fcSAndreas Gruenbacher * @size: maximum length 385cdd591fcSAndreas Gruenbacher * 386cdd591fcSAndreas Gruenbacher * Faults in the iterator using get_user_pages(), i.e., without triggering 387cdd591fcSAndreas Gruenbacher * hardware page faults. This is primarily useful when we already know that 388cdd591fcSAndreas Gruenbacher * some or all of the pages in @i aren't in memory. 389cdd591fcSAndreas Gruenbacher * 390cdd591fcSAndreas Gruenbacher * Returns the number of bytes not faulted in, like copy_to_user() and 391cdd591fcSAndreas Gruenbacher * copy_from_user(). 392cdd591fcSAndreas Gruenbacher * 393cdd591fcSAndreas Gruenbacher * Always returns 0 for non-user-space iterators. 394cdd591fcSAndreas Gruenbacher */ 395cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size) 396cdd591fcSAndreas Gruenbacher { 397fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 398fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 399fcb14cb1SAl Viro n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n); 400fcb14cb1SAl Viro return size - n; 401fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 402cdd591fcSAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 403cdd591fcSAndreas Gruenbacher const struct iovec *p; 404cdd591fcSAndreas Gruenbacher size_t skip; 405cdd591fcSAndreas Gruenbacher 406cdd591fcSAndreas Gruenbacher size -= count; 407cdd591fcSAndreas Gruenbacher for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) { 408cdd591fcSAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 409cdd591fcSAndreas Gruenbacher size_t ret; 410cdd591fcSAndreas Gruenbacher 411cdd591fcSAndreas Gruenbacher if (unlikely(!len)) 412cdd591fcSAndreas Gruenbacher continue; 413cdd591fcSAndreas Gruenbacher ret = fault_in_safe_writeable(p->iov_base + skip, len); 414cdd591fcSAndreas Gruenbacher count -= len - ret; 415cdd591fcSAndreas Gruenbacher if (ret) 416cdd591fcSAndreas Gruenbacher break; 417cdd591fcSAndreas Gruenbacher } 418cdd591fcSAndreas Gruenbacher return count + size; 419cdd591fcSAndreas Gruenbacher } 420cdd591fcSAndreas Gruenbacher return 0; 421cdd591fcSAndreas Gruenbacher } 422cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable); 423cdd591fcSAndreas Gruenbacher 424aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction, 425d879cb83SAl Viro const struct iovec *iov, unsigned long nr_segs, 426d879cb83SAl Viro size_t count) 427d879cb83SAl Viro { 428aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 4298cd54c1cSAl Viro *i = (struct iov_iter) { 4308cd54c1cSAl Viro .iter_type = ITER_IOVEC, 4313337ab08SAndreas Gruenbacher .nofault = false, 432fcb14cb1SAl Viro .user_backed = true, 4338cd54c1cSAl Viro .data_source = direction, 4348cd54c1cSAl Viro .iov = iov, 4358cd54c1cSAl Viro .nr_segs = nr_segs, 4368cd54c1cSAl Viro .iov_offset = 0, 4378cd54c1cSAl Viro .count = count 4388cd54c1cSAl Viro }; 439d879cb83SAl Viro } 440d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init); 441d879cb83SAl Viro 44212d426abSAl Viro // returns the offset in partial buffer (if any) 44312d426abSAl Viro static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages) 444241699cdSAl Viro { 44512d426abSAl Viro struct pipe_inode_info *pipe = i->pipe; 44612d426abSAl Viro int used = pipe->head - pipe->tail; 44710f525a8SAl Viro int off = i->last_offset; 4488cefc107SDavid Howells 44912d426abSAl Viro *npages = max((int)pipe->max_usage - used, 0); 45012d426abSAl Viro 45110f525a8SAl Viro if (off > 0 && off < PAGE_SIZE) { // anon and not full 45212d426abSAl Viro (*npages)++; 45312d426abSAl Viro return off; 45410f525a8SAl Viro } 45512d426abSAl Viro return 0; 456241699cdSAl Viro } 457241699cdSAl Viro 458241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes, 459241699cdSAl Viro struct iov_iter *i) 460241699cdSAl Viro { 4618fad7767SAl Viro unsigned int off, chunk; 4628fad7767SAl Viro 4638fad7767SAl Viro if (unlikely(bytes > i->count)) 4648fad7767SAl Viro bytes = i->count; 4658fad7767SAl Viro if (unlikely(!bytes)) 4668fad7767SAl Viro return 0; 467241699cdSAl Viro 468241699cdSAl Viro if (!sanity(i)) 469241699cdSAl Viro return 0; 470241699cdSAl Viro 4718fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 4728fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 4738fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 4748fad7767SAl Viro if (!page) 4758fad7767SAl Viro return bytes - n; 4768fad7767SAl Viro memcpy_to_page(page, off, addr, chunk); 477241699cdSAl Viro addr += chunk; 4788fad7767SAl Viro } 479241699cdSAl Viro return bytes; 480241699cdSAl Viro } 481241699cdSAl Viro 482f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len, 483f9152895SAl Viro __wsum sum, size_t off) 484f9152895SAl Viro { 485cc44c17bSAl Viro __wsum next = csum_partial_copy_nocheck(from, to, len); 486f9152895SAl Viro return csum_block_add(sum, next, off); 487f9152895SAl Viro } 488f9152895SAl Viro 48978e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes, 4906852df12SAl Viro struct iov_iter *i, __wsum *sump) 49178e1f386SAl Viro { 4926852df12SAl Viro __wsum sum = *sump; 4936852df12SAl Viro size_t off = 0; 4948fad7767SAl Viro unsigned int chunk, r; 4958fad7767SAl Viro 4968fad7767SAl Viro if (unlikely(bytes > i->count)) 4978fad7767SAl Viro bytes = i->count; 4988fad7767SAl Viro if (unlikely(!bytes)) 4998fad7767SAl Viro return 0; 50078e1f386SAl Viro 50178e1f386SAl Viro if (!sanity(i)) 50278e1f386SAl Viro return 0; 50378e1f386SAl Viro 5046852df12SAl Viro while (bytes) { 5058fad7767SAl Viro struct page *page = append_pipe(i, bytes, &r); 5068fad7767SAl Viro char *p; 5078fad7767SAl Viro 5088fad7767SAl Viro if (!page) 5098fad7767SAl Viro break; 5108fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - r); 5118fad7767SAl Viro p = kmap_local_page(page); 5126852df12SAl Viro sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off); 5132495bdccSAl Viro kunmap_local(p); 51478e1f386SAl Viro off += chunk; 5158fad7767SAl Viro bytes -= chunk; 5166852df12SAl Viro } 5176852df12SAl Viro *sump = sum; 5186852df12SAl Viro return off; 51978e1f386SAl Viro } 52078e1f386SAl Viro 521aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 522d879cb83SAl Viro { 52300e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 524241699cdSAl Viro return copy_pipe_to_iter(addr, bytes, i); 525fcb14cb1SAl Viro if (user_backed_iter(i)) 52609fc68dcSAl Viro might_fault(); 5277baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 5287baa5099SAl Viro copyout(base, addr + off, len), 5297baa5099SAl Viro memcpy(base, addr + off, len) 530d879cb83SAl Viro ) 531d879cb83SAl Viro 532d879cb83SAl Viro return bytes; 533d879cb83SAl Viro } 534aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter); 535d879cb83SAl Viro 536ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC 537ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n) 5388780356eSDan Williams { 53996d4f267SLinus Torvalds if (access_ok(to, n)) { 540d0ef4c36SMarco Elver instrument_copy_to_user(to, from, n); 541ec6347bbSDan Williams n = copy_mc_to_user((__force void *) to, from, n); 5428780356eSDan Williams } 5438780356eSDan Williams return n; 5448780356eSDan Williams } 5458780356eSDan Williams 546ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes, 547ca146f6fSDan Williams struct iov_iter *i) 548ca146f6fSDan Williams { 5498fad7767SAl Viro size_t xfer = 0; 5508fad7767SAl Viro unsigned int off, chunk; 5518fad7767SAl Viro 5528fad7767SAl Viro if (unlikely(bytes > i->count)) 5538fad7767SAl Viro bytes = i->count; 5548fad7767SAl Viro if (unlikely(!bytes)) 5558fad7767SAl Viro return 0; 556ca146f6fSDan Williams 557ca146f6fSDan Williams if (!sanity(i)) 558ca146f6fSDan Williams return 0; 559ca146f6fSDan Williams 5608fad7767SAl Viro while (bytes) { 5618fad7767SAl Viro struct page *page = append_pipe(i, bytes, &off); 562ca146f6fSDan Williams unsigned long rem; 5638fad7767SAl Viro char *p; 5648fad7767SAl Viro 5658fad7767SAl Viro if (!page) 5668fad7767SAl Viro break; 5678fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - off); 5688fad7767SAl Viro p = kmap_local_page(page); 5692a510a74SAl Viro rem = copy_mc_to_kernel(p + off, addr + xfer, chunk); 5702a510a74SAl Viro chunk -= rem; 5712a510a74SAl Viro kunmap_local(p); 5722a510a74SAl Viro xfer += chunk; 5738fad7767SAl Viro bytes -= chunk; 574c3497fd0SAl Viro if (rem) { 5758fad7767SAl Viro iov_iter_revert(i, rem); 576ca146f6fSDan Williams break; 577c3497fd0SAl Viro } 5782a510a74SAl Viro } 579ca146f6fSDan Williams return xfer; 580ca146f6fSDan Williams } 581ca146f6fSDan Williams 582bf3eeb9bSDan Williams /** 583ec6347bbSDan Williams * _copy_mc_to_iter - copy to iter with source memory error exception handling 584bf3eeb9bSDan Williams * @addr: source kernel address 585bf3eeb9bSDan Williams * @bytes: total transfer length 58644e55997SRandy Dunlap * @i: destination iterator 587bf3eeb9bSDan Williams * 588ec6347bbSDan Williams * The pmem driver deploys this for the dax operation 589ec6347bbSDan Williams * (dax_copy_to_iter()) for dax reads (bypass page-cache and the 590ec6347bbSDan Williams * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes 591ec6347bbSDan Williams * successfully copied. 592bf3eeb9bSDan Williams * 593ec6347bbSDan Williams * The main differences between this and typical _copy_to_iter(). 594bf3eeb9bSDan Williams * 595bf3eeb9bSDan Williams * * Typical tail/residue handling after a fault retries the copy 596bf3eeb9bSDan Williams * byte-by-byte until the fault happens again. Re-triggering machine 597bf3eeb9bSDan Williams * checks is potentially fatal so the implementation uses source 598bf3eeb9bSDan Williams * alignment and poison alignment assumptions to avoid re-triggering 599bf3eeb9bSDan Williams * hardware exceptions. 600bf3eeb9bSDan Williams * 601bf3eeb9bSDan Williams * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies. 602bf3eeb9bSDan Williams * Compare to copy_to_iter() where only ITER_IOVEC attempts might return 603bf3eeb9bSDan Williams * a short copy. 60444e55997SRandy Dunlap * 60544e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 606bf3eeb9bSDan Williams */ 607ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 6088780356eSDan Williams { 60900e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 610ec6347bbSDan Williams return copy_mc_pipe_to_iter(addr, bytes, i); 611fcb14cb1SAl Viro if (user_backed_iter(i)) 6128780356eSDan Williams might_fault(); 6137baa5099SAl Viro __iterate_and_advance(i, bytes, base, len, off, 6147baa5099SAl Viro copyout_mc(base, addr + off, len), 6157baa5099SAl Viro copy_mc_to_kernel(base, addr + off, len) 6168780356eSDan Williams ) 6178780356eSDan Williams 6188780356eSDan Williams return bytes; 6198780356eSDan Williams } 620ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter); 621ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */ 6228780356eSDan Williams 623aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) 624d879cb83SAl Viro { 62500e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 626241699cdSAl Viro WARN_ON(1); 627241699cdSAl Viro return 0; 628241699cdSAl Viro } 629fcb14cb1SAl Viro if (user_backed_iter(i)) 63009fc68dcSAl Viro might_fault(); 6317baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6327baa5099SAl Viro copyin(addr + off, base, len), 6337baa5099SAl Viro memcpy(addr + off, base, len) 634d879cb83SAl Viro ) 635d879cb83SAl Viro 636d879cb83SAl Viro return bytes; 637d879cb83SAl Viro } 638aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter); 639d879cb83SAl Viro 640aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) 641d879cb83SAl Viro { 64200e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 643241699cdSAl Viro WARN_ON(1); 644241699cdSAl Viro return 0; 645241699cdSAl Viro } 6467baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6477baa5099SAl Viro __copy_from_user_inatomic_nocache(addr + off, base, len), 6487baa5099SAl Viro memcpy(addr + off, base, len) 649d879cb83SAl Viro ) 650d879cb83SAl Viro 651d879cb83SAl Viro return bytes; 652d879cb83SAl Viro } 653aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache); 654d879cb83SAl Viro 6550aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE 656abd08d7dSDan Williams /** 657abd08d7dSDan Williams * _copy_from_iter_flushcache - write destination through cpu cache 658abd08d7dSDan Williams * @addr: destination kernel address 659abd08d7dSDan Williams * @bytes: total transfer length 66044e55997SRandy Dunlap * @i: source iterator 661abd08d7dSDan Williams * 662abd08d7dSDan Williams * The pmem driver arranges for filesystem-dax to use this facility via 663abd08d7dSDan Williams * dax_copy_from_iter() for ensuring that writes to persistent memory 664abd08d7dSDan Williams * are flushed through the CPU cache. It is differentiated from 665abd08d7dSDan Williams * _copy_from_iter_nocache() in that guarantees all data is flushed for 666abd08d7dSDan Williams * all iterator types. The _copy_from_iter_nocache() only attempts to 667abd08d7dSDan Williams * bypass the cache for the ITER_IOVEC case, and on some archs may use 668abd08d7dSDan Williams * instructions that strand dirty-data in the cache. 66944e55997SRandy Dunlap * 67044e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 671abd08d7dSDan Williams */ 6726a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i) 6730aed55afSDan Williams { 67400e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 6750aed55afSDan Williams WARN_ON(1); 6760aed55afSDan Williams return 0; 6770aed55afSDan Williams } 6787baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6797baa5099SAl Viro __copy_from_user_flushcache(addr + off, base, len), 6807baa5099SAl Viro memcpy_flushcache(addr + off, base, len) 6810aed55afSDan Williams ) 6820aed55afSDan Williams 6830aed55afSDan Williams return bytes; 6840aed55afSDan Williams } 6856a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache); 6860aed55afSDan Williams #endif 6870aed55afSDan Williams 68872e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n) 68972e809edSAl Viro { 6906daef95bSEric Dumazet struct page *head; 6916daef95bSEric Dumazet size_t v = n + offset; 6926daef95bSEric Dumazet 6936daef95bSEric Dumazet /* 6946daef95bSEric Dumazet * The general case needs to access the page order in order 6956daef95bSEric Dumazet * to compute the page size. 6966daef95bSEric Dumazet * However, we mostly deal with order-0 pages and thus can 6976daef95bSEric Dumazet * avoid a possible cache line miss for requests that fit all 6986daef95bSEric Dumazet * page orders. 6996daef95bSEric Dumazet */ 7006daef95bSEric Dumazet if (n <= v && v <= PAGE_SIZE) 7016daef95bSEric Dumazet return true; 7026daef95bSEric Dumazet 7036daef95bSEric Dumazet head = compound_head(page); 7046daef95bSEric Dumazet v += (page - head) << PAGE_SHIFT; 705a90bcb86SPetar Penkov 70640a86061SAl Viro if (WARN_ON(n > v || v > page_size(head))) 70772e809edSAl Viro return false; 70840a86061SAl Viro return true; 70972e809edSAl Viro } 710cbbd26b8SAl Viro 71108aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, 71208aa6479SAl Viro struct iov_iter *i) 71308aa6479SAl Viro { 71408aa6479SAl Viro size_t res = 0; 71540a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) 71608aa6479SAl Viro return 0; 717f0f6b614SAl Viro if (unlikely(iov_iter_is_pipe(i))) 718f0f6b614SAl Viro return copy_page_to_iter_pipe(page, offset, bytes, i); 71908aa6479SAl Viro page += offset / PAGE_SIZE; // first subpage 72008aa6479SAl Viro offset %= PAGE_SIZE; 72108aa6479SAl Viro while (1) { 722f0f6b614SAl Viro void *kaddr = kmap_local_page(page); 723f0f6b614SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 724f0f6b614SAl Viro n = _copy_to_iter(kaddr + offset, n, i); 725f0f6b614SAl Viro kunmap_local(kaddr); 72608aa6479SAl Viro res += n; 72708aa6479SAl Viro bytes -= n; 72808aa6479SAl Viro if (!bytes || !n) 72908aa6479SAl Viro break; 73008aa6479SAl Viro offset += n; 73108aa6479SAl Viro if (offset == PAGE_SIZE) { 73208aa6479SAl Viro page++; 73308aa6479SAl Viro offset = 0; 73408aa6479SAl Viro } 73508aa6479SAl Viro } 73608aa6479SAl Viro return res; 73708aa6479SAl Viro } 738d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter); 739d879cb83SAl Viro 740d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, 741d879cb83SAl Viro struct iov_iter *i) 742d879cb83SAl Viro { 743c03f05f1SAl Viro size_t res = 0; 744c03f05f1SAl Viro if (!page_copy_sane(page, offset, bytes)) 74528f38db7SAl Viro return 0; 746c03f05f1SAl Viro page += offset / PAGE_SIZE; // first subpage 747c03f05f1SAl Viro offset %= PAGE_SIZE; 748c03f05f1SAl Viro while (1) { 749c03f05f1SAl Viro void *kaddr = kmap_local_page(page); 750c03f05f1SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 751c03f05f1SAl Viro n = _copy_from_iter(kaddr + offset, n, i); 752c03f05f1SAl Viro kunmap_local(kaddr); 753c03f05f1SAl Viro res += n; 754c03f05f1SAl Viro bytes -= n; 755c03f05f1SAl Viro if (!bytes || !n) 756c03f05f1SAl Viro break; 757c03f05f1SAl Viro offset += n; 758c03f05f1SAl Viro if (offset == PAGE_SIZE) { 759c03f05f1SAl Viro page++; 760c03f05f1SAl Viro offset = 0; 761c03f05f1SAl Viro } 762c03f05f1SAl Viro } 763c03f05f1SAl Viro return res; 764d879cb83SAl Viro } 765d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter); 766d879cb83SAl Viro 767241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i) 768241699cdSAl Viro { 7698fad7767SAl Viro unsigned int chunk, off; 7708fad7767SAl Viro 7718fad7767SAl Viro if (unlikely(bytes > i->count)) 7728fad7767SAl Viro bytes = i->count; 7738fad7767SAl Viro if (unlikely(!bytes)) 7748fad7767SAl Viro return 0; 775241699cdSAl Viro 776241699cdSAl Viro if (!sanity(i)) 777241699cdSAl Viro return 0; 778241699cdSAl Viro 7798fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 7808fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 7818fad7767SAl Viro char *p; 782241699cdSAl Viro 7838fad7767SAl Viro if (!page) 7848fad7767SAl Viro return bytes - n; 7858fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 7868fad7767SAl Viro p = kmap_local_page(page); 787893839fdSAl Viro memset(p + off, 0, chunk); 788893839fdSAl Viro kunmap_local(p); 7898fad7767SAl Viro } 790241699cdSAl Viro return bytes; 791241699cdSAl Viro } 792241699cdSAl Viro 793d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i) 794d879cb83SAl Viro { 79500e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 796241699cdSAl Viro return pipe_zero(bytes, i); 7977baa5099SAl Viro iterate_and_advance(i, bytes, base, len, count, 7987baa5099SAl Viro clear_user(base, len), 7997baa5099SAl Viro memset(base, 0, len) 800d879cb83SAl Viro ) 801d879cb83SAl Viro 802d879cb83SAl Viro return bytes; 803d879cb83SAl Viro } 804d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero); 805d879cb83SAl Viro 806f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes, 807f0b65f39SAl Viro struct iov_iter *i) 808d879cb83SAl Viro { 809d879cb83SAl Viro char *kaddr = kmap_atomic(page), *p = kaddr + offset; 81040a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) { 81172e809edSAl Viro kunmap_atomic(kaddr); 81272e809edSAl Viro return 0; 81372e809edSAl Viro } 8149ea9ce04SDavid Howells if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { 815241699cdSAl Viro kunmap_atomic(kaddr); 816241699cdSAl Viro WARN_ON(1); 817241699cdSAl Viro return 0; 818241699cdSAl Viro } 8197baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 8207baa5099SAl Viro copyin(p + off, base, len), 8217baa5099SAl Viro memcpy(p + off, base, len) 822d879cb83SAl Viro ) 823d879cb83SAl Viro kunmap_atomic(kaddr); 824d879cb83SAl Viro return bytes; 825d879cb83SAl Viro } 826f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic); 827d879cb83SAl Viro 828b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size) 829b9dc6f65SAl Viro { 830b9dc6f65SAl Viro struct pipe_inode_info *pipe = i->pipe; 83110f525a8SAl Viro int off = i->last_offset; 8328cefc107SDavid Howells 8332c855de9SAl Viro if (!off && !size) { 8342c855de9SAl Viro pipe_discard_from(pipe, i->start_head); // discard everything 8352c855de9SAl Viro return; 836b9dc6f65SAl Viro } 837b9dc6f65SAl Viro i->count -= size; 8382c855de9SAl Viro while (1) { 8392c855de9SAl Viro struct pipe_buffer *buf = pipe_buf(pipe, i->head); 8402c855de9SAl Viro if (off) /* make it relative to the beginning of buffer */ 84110f525a8SAl Viro size += abs(off) - buf->offset; 8422c855de9SAl Viro if (size <= buf->len) { 8432c855de9SAl Viro buf->len = size; 84410f525a8SAl Viro i->last_offset = last_offset(buf); 8452c855de9SAl Viro break; 8462c855de9SAl Viro } 8472c855de9SAl Viro size -= buf->len; 8482c855de9SAl Viro i->head++; 8492c855de9SAl Viro off = 0; 8502c855de9SAl Viro } 8512c855de9SAl Viro pipe_discard_from(pipe, i->head + 1); // discard everything past this one 852241699cdSAl Viro } 853241699cdSAl Viro 85454c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size) 85554c8195bSPavel Begunkov { 85618fa9af7SAl Viro const struct bio_vec *bvec, *end; 85754c8195bSPavel Begunkov 85818fa9af7SAl Viro if (!i->count) 85918fa9af7SAl Viro return; 86018fa9af7SAl Viro i->count -= size; 86154c8195bSPavel Begunkov 86218fa9af7SAl Viro size += i->iov_offset; 86318fa9af7SAl Viro 86418fa9af7SAl Viro for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) { 86518fa9af7SAl Viro if (likely(size < bvec->bv_len)) 86618fa9af7SAl Viro break; 86718fa9af7SAl Viro size -= bvec->bv_len; 86818fa9af7SAl Viro } 86918fa9af7SAl Viro i->iov_offset = size; 87018fa9af7SAl Viro i->nr_segs -= bvec - i->bvec; 87118fa9af7SAl Viro i->bvec = bvec; 87254c8195bSPavel Begunkov } 87354c8195bSPavel Begunkov 874185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size) 875185ac4d4SAl Viro { 876185ac4d4SAl Viro const struct iovec *iov, *end; 877185ac4d4SAl Viro 878185ac4d4SAl Viro if (!i->count) 879185ac4d4SAl Viro return; 880185ac4d4SAl Viro i->count -= size; 881185ac4d4SAl Viro 882185ac4d4SAl Viro size += i->iov_offset; // from beginning of current segment 883185ac4d4SAl Viro for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) { 884185ac4d4SAl Viro if (likely(size < iov->iov_len)) 885185ac4d4SAl Viro break; 886185ac4d4SAl Viro size -= iov->iov_len; 887185ac4d4SAl Viro } 888185ac4d4SAl Viro i->iov_offset = size; 889185ac4d4SAl Viro i->nr_segs -= iov - i->iov; 890185ac4d4SAl Viro i->iov = iov; 891185ac4d4SAl Viro } 892185ac4d4SAl Viro 893d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size) 894d879cb83SAl Viro { 8953b3fc051SAl Viro if (unlikely(i->count < size)) 8963b3fc051SAl Viro size = i->count; 897fcb14cb1SAl Viro if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) { 898fcb14cb1SAl Viro i->iov_offset += size; 899fcb14cb1SAl Viro i->count -= size; 900fcb14cb1SAl Viro } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) { 901185ac4d4SAl Viro /* iovec and kvec have identical layouts */ 902185ac4d4SAl Viro iov_iter_iovec_advance(i, size); 903185ac4d4SAl Viro } else if (iov_iter_is_bvec(i)) { 904185ac4d4SAl Viro iov_iter_bvec_advance(i, size); 905185ac4d4SAl Viro } else if (iov_iter_is_pipe(i)) { 906241699cdSAl Viro pipe_advance(i, size); 907185ac4d4SAl Viro } else if (iov_iter_is_discard(i)) { 908185ac4d4SAl Viro i->count -= size; 9097ff50620SDavid Howells } 910d879cb83SAl Viro } 911d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance); 912d879cb83SAl Viro 91327c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll) 91427c0e374SAl Viro { 91527c0e374SAl Viro if (!unroll) 91627c0e374SAl Viro return; 9175b47d59aSAl Viro if (WARN_ON(unroll > MAX_RW_COUNT)) 9185b47d59aSAl Viro return; 91927c0e374SAl Viro i->count += unroll; 92000e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 92127c0e374SAl Viro struct pipe_inode_info *pipe = i->pipe; 92292acdc4fSAl Viro unsigned int head = pipe->head; 92392acdc4fSAl Viro 92492acdc4fSAl Viro while (head > i->start_head) { 92592acdc4fSAl Viro struct pipe_buffer *b = pipe_buf(pipe, --head); 92692acdc4fSAl Viro if (unroll < b->len) { 92792acdc4fSAl Viro b->len -= unroll; 92810f525a8SAl Viro i->last_offset = last_offset(b); 92992acdc4fSAl Viro i->head = head; 93092acdc4fSAl Viro return; 93127c0e374SAl Viro } 93292acdc4fSAl Viro unroll -= b->len; 93392acdc4fSAl Viro pipe_buf_release(pipe, b); 93492acdc4fSAl Viro pipe->head--; 93527c0e374SAl Viro } 93610f525a8SAl Viro i->last_offset = 0; 93792acdc4fSAl Viro i->head = head; 93827c0e374SAl Viro return; 93927c0e374SAl Viro } 9409ea9ce04SDavid Howells if (unlikely(iov_iter_is_discard(i))) 9419ea9ce04SDavid Howells return; 94227c0e374SAl Viro if (unroll <= i->iov_offset) { 94327c0e374SAl Viro i->iov_offset -= unroll; 94427c0e374SAl Viro return; 94527c0e374SAl Viro } 94627c0e374SAl Viro unroll -= i->iov_offset; 947fcb14cb1SAl Viro if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) { 9487ff50620SDavid Howells BUG(); /* We should never go beyond the start of the specified 9497ff50620SDavid Howells * range since we might then be straying into pages that 9507ff50620SDavid Howells * aren't pinned. 9517ff50620SDavid Howells */ 9527ff50620SDavid Howells } else if (iov_iter_is_bvec(i)) { 95327c0e374SAl Viro const struct bio_vec *bvec = i->bvec; 95427c0e374SAl Viro while (1) { 95527c0e374SAl Viro size_t n = (--bvec)->bv_len; 95627c0e374SAl Viro i->nr_segs++; 95727c0e374SAl Viro if (unroll <= n) { 95827c0e374SAl Viro i->bvec = bvec; 95927c0e374SAl Viro i->iov_offset = n - unroll; 96027c0e374SAl Viro return; 96127c0e374SAl Viro } 96227c0e374SAl Viro unroll -= n; 96327c0e374SAl Viro } 96427c0e374SAl Viro } else { /* same logics for iovec and kvec */ 96527c0e374SAl Viro const struct iovec *iov = i->iov; 96627c0e374SAl Viro while (1) { 96727c0e374SAl Viro size_t n = (--iov)->iov_len; 96827c0e374SAl Viro i->nr_segs++; 96927c0e374SAl Viro if (unroll <= n) { 97027c0e374SAl Viro i->iov = iov; 97127c0e374SAl Viro i->iov_offset = n - unroll; 97227c0e374SAl Viro return; 97327c0e374SAl Viro } 97427c0e374SAl Viro unroll -= n; 97527c0e374SAl Viro } 97627c0e374SAl Viro } 97727c0e374SAl Viro } 97827c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert); 97927c0e374SAl Viro 980d879cb83SAl Viro /* 981d879cb83SAl Viro * Return the count of just the current iov_iter segment. 982d879cb83SAl Viro */ 983d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i) 984d879cb83SAl Viro { 98528f38db7SAl Viro if (i->nr_segs > 1) { 98628f38db7SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 98728f38db7SAl Viro return min(i->count, i->iov->iov_len - i->iov_offset); 9887ff50620SDavid Howells if (iov_iter_is_bvec(i)) 989d879cb83SAl Viro return min(i->count, i->bvec->bv_len - i->iov_offset); 99028f38db7SAl Viro } 99128f38db7SAl Viro return i->count; 992d879cb83SAl Viro } 993d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count); 994d879cb83SAl Viro 995aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction, 996d879cb83SAl Viro const struct kvec *kvec, unsigned long nr_segs, 997d879cb83SAl Viro size_t count) 998d879cb83SAl Viro { 999aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 10008cd54c1cSAl Viro *i = (struct iov_iter){ 10018cd54c1cSAl Viro .iter_type = ITER_KVEC, 10028cd54c1cSAl Viro .data_source = direction, 10038cd54c1cSAl Viro .kvec = kvec, 10048cd54c1cSAl Viro .nr_segs = nr_segs, 10058cd54c1cSAl Viro .iov_offset = 0, 10068cd54c1cSAl Viro .count = count 10078cd54c1cSAl Viro }; 1008d879cb83SAl Viro } 1009d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec); 1010d879cb83SAl Viro 1011aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction, 1012d879cb83SAl Viro const struct bio_vec *bvec, unsigned long nr_segs, 1013d879cb83SAl Viro size_t count) 1014d879cb83SAl Viro { 1015aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 10168cd54c1cSAl Viro *i = (struct iov_iter){ 10178cd54c1cSAl Viro .iter_type = ITER_BVEC, 10188cd54c1cSAl Viro .data_source = direction, 10198cd54c1cSAl Viro .bvec = bvec, 10208cd54c1cSAl Viro .nr_segs = nr_segs, 10218cd54c1cSAl Viro .iov_offset = 0, 10228cd54c1cSAl Viro .count = count 10238cd54c1cSAl Viro }; 1024d879cb83SAl Viro } 1025d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec); 1026d879cb83SAl Viro 1027aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction, 1028241699cdSAl Viro struct pipe_inode_info *pipe, 1029241699cdSAl Viro size_t count) 1030241699cdSAl Viro { 1031aa563d7bSDavid Howells BUG_ON(direction != READ); 10328cefc107SDavid Howells WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size)); 10338cd54c1cSAl Viro *i = (struct iov_iter){ 10348cd54c1cSAl Viro .iter_type = ITER_PIPE, 10358cd54c1cSAl Viro .data_source = false, 10368cd54c1cSAl Viro .pipe = pipe, 10378cd54c1cSAl Viro .head = pipe->head, 10388cd54c1cSAl Viro .start_head = pipe->head, 103910f525a8SAl Viro .last_offset = 0, 10408cd54c1cSAl Viro .count = count 10418cd54c1cSAl Viro }; 1042241699cdSAl Viro } 1043241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe); 1044241699cdSAl Viro 10459ea9ce04SDavid Howells /** 10467ff50620SDavid Howells * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray 10477ff50620SDavid Howells * @i: The iterator to initialise. 10487ff50620SDavid Howells * @direction: The direction of the transfer. 10497ff50620SDavid Howells * @xarray: The xarray to access. 10507ff50620SDavid Howells * @start: The start file position. 10517ff50620SDavid Howells * @count: The size of the I/O buffer in bytes. 10527ff50620SDavid Howells * 10537ff50620SDavid Howells * Set up an I/O iterator to either draw data out of the pages attached to an 10547ff50620SDavid Howells * inode or to inject data into those pages. The pages *must* be prevented 10557ff50620SDavid Howells * from evaporation, either by taking a ref on them or locking them by the 10567ff50620SDavid Howells * caller. 10577ff50620SDavid Howells */ 10587ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction, 10597ff50620SDavid Howells struct xarray *xarray, loff_t start, size_t count) 10607ff50620SDavid Howells { 10617ff50620SDavid Howells BUG_ON(direction & ~1); 10628cd54c1cSAl Viro *i = (struct iov_iter) { 10638cd54c1cSAl Viro .iter_type = ITER_XARRAY, 10648cd54c1cSAl Viro .data_source = direction, 10658cd54c1cSAl Viro .xarray = xarray, 10668cd54c1cSAl Viro .xarray_start = start, 10678cd54c1cSAl Viro .count = count, 10688cd54c1cSAl Viro .iov_offset = 0 10698cd54c1cSAl Viro }; 10707ff50620SDavid Howells } 10717ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray); 10727ff50620SDavid Howells 10737ff50620SDavid Howells /** 10749ea9ce04SDavid Howells * iov_iter_discard - Initialise an I/O iterator that discards data 10759ea9ce04SDavid Howells * @i: The iterator to initialise. 10769ea9ce04SDavid Howells * @direction: The direction of the transfer. 10779ea9ce04SDavid Howells * @count: The size of the I/O buffer in bytes. 10789ea9ce04SDavid Howells * 10799ea9ce04SDavid Howells * Set up an I/O iterator that just discards everything that's written to it. 10809ea9ce04SDavid Howells * It's only available as a READ iterator. 10819ea9ce04SDavid Howells */ 10829ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count) 10839ea9ce04SDavid Howells { 10849ea9ce04SDavid Howells BUG_ON(direction != READ); 10858cd54c1cSAl Viro *i = (struct iov_iter){ 10868cd54c1cSAl Viro .iter_type = ITER_DISCARD, 10878cd54c1cSAl Viro .data_source = false, 10888cd54c1cSAl Viro .count = count, 10898cd54c1cSAl Viro .iov_offset = 0 10908cd54c1cSAl Viro }; 10919ea9ce04SDavid Howells } 10929ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard); 10939ea9ce04SDavid Howells 1094cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask, 1095cfa320f7SKeith Busch unsigned len_mask) 1096cfa320f7SKeith Busch { 1097cfa320f7SKeith Busch size_t size = i->count; 1098cfa320f7SKeith Busch size_t skip = i->iov_offset; 1099cfa320f7SKeith Busch unsigned k; 1100cfa320f7SKeith Busch 1101cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1102cfa320f7SKeith Busch size_t len = i->iov[k].iov_len - skip; 1103cfa320f7SKeith Busch 1104cfa320f7SKeith Busch if (len > size) 1105cfa320f7SKeith Busch len = size; 1106cfa320f7SKeith Busch if (len & len_mask) 1107cfa320f7SKeith Busch return false; 1108cfa320f7SKeith Busch if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask) 1109cfa320f7SKeith Busch return false; 1110cfa320f7SKeith Busch 1111cfa320f7SKeith Busch size -= len; 1112cfa320f7SKeith Busch if (!size) 1113cfa320f7SKeith Busch break; 1114cfa320f7SKeith Busch } 1115cfa320f7SKeith Busch return true; 1116cfa320f7SKeith Busch } 1117cfa320f7SKeith Busch 1118cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask, 1119cfa320f7SKeith Busch unsigned len_mask) 1120cfa320f7SKeith Busch { 1121cfa320f7SKeith Busch size_t size = i->count; 1122cfa320f7SKeith Busch unsigned skip = i->iov_offset; 1123cfa320f7SKeith Busch unsigned k; 1124cfa320f7SKeith Busch 1125cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1126cfa320f7SKeith Busch size_t len = i->bvec[k].bv_len - skip; 1127cfa320f7SKeith Busch 1128cfa320f7SKeith Busch if (len > size) 1129cfa320f7SKeith Busch len = size; 1130cfa320f7SKeith Busch if (len & len_mask) 1131cfa320f7SKeith Busch return false; 1132cfa320f7SKeith Busch if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask) 1133cfa320f7SKeith Busch return false; 1134cfa320f7SKeith Busch 1135cfa320f7SKeith Busch size -= len; 1136cfa320f7SKeith Busch if (!size) 1137cfa320f7SKeith Busch break; 1138cfa320f7SKeith Busch } 1139cfa320f7SKeith Busch return true; 1140cfa320f7SKeith Busch } 1141cfa320f7SKeith Busch 1142cfa320f7SKeith Busch /** 1143cfa320f7SKeith Busch * iov_iter_is_aligned() - Check if the addresses and lengths of each segments 1144cfa320f7SKeith Busch * are aligned to the parameters. 1145cfa320f7SKeith Busch * 1146cfa320f7SKeith Busch * @i: &struct iov_iter to restore 1147cfa320f7SKeith Busch * @addr_mask: bit mask to check against the iov element's addresses 1148cfa320f7SKeith Busch * @len_mask: bit mask to check against the iov element's lengths 1149cfa320f7SKeith Busch * 1150cfa320f7SKeith Busch * Return: false if any addresses or lengths intersect with the provided masks 1151cfa320f7SKeith Busch */ 1152cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask, 1153cfa320f7SKeith Busch unsigned len_mask) 1154cfa320f7SKeith Busch { 1155fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1156fcb14cb1SAl Viro if (i->count & len_mask) 1157fcb14cb1SAl Viro return false; 1158fcb14cb1SAl Viro if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask) 1159fcb14cb1SAl Viro return false; 1160fcb14cb1SAl Viro return true; 1161fcb14cb1SAl Viro } 1162fcb14cb1SAl Viro 1163cfa320f7SKeith Busch if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 1164cfa320f7SKeith Busch return iov_iter_aligned_iovec(i, addr_mask, len_mask); 1165cfa320f7SKeith Busch 1166cfa320f7SKeith Busch if (iov_iter_is_bvec(i)) 1167cfa320f7SKeith Busch return iov_iter_aligned_bvec(i, addr_mask, len_mask); 1168cfa320f7SKeith Busch 1169cfa320f7SKeith Busch if (iov_iter_is_pipe(i)) { 1170cfa320f7SKeith Busch size_t size = i->count; 1171cfa320f7SKeith Busch 1172cfa320f7SKeith Busch if (size & len_mask) 1173cfa320f7SKeith Busch return false; 117410f525a8SAl Viro if (size && i->last_offset > 0) { 117510f525a8SAl Viro if (i->last_offset & addr_mask) 1176cfa320f7SKeith Busch return false; 1177cfa320f7SKeith Busch } 1178cfa320f7SKeith Busch 1179cfa320f7SKeith Busch return true; 1180cfa320f7SKeith Busch } 1181cfa320f7SKeith Busch 1182cfa320f7SKeith Busch if (iov_iter_is_xarray(i)) { 1183cfa320f7SKeith Busch if (i->count & len_mask) 1184cfa320f7SKeith Busch return false; 1185cfa320f7SKeith Busch if ((i->xarray_start + i->iov_offset) & addr_mask) 1186cfa320f7SKeith Busch return false; 1187cfa320f7SKeith Busch } 1188cfa320f7SKeith Busch 1189cfa320f7SKeith Busch return true; 1190cfa320f7SKeith Busch } 1191cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned); 1192cfa320f7SKeith Busch 11939221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i) 1194d879cb83SAl Viro { 1195d879cb83SAl Viro unsigned long res = 0; 1196d879cb83SAl Viro size_t size = i->count; 11979221d2e3SAl Viro size_t skip = i->iov_offset; 11989221d2e3SAl Viro unsigned k; 1199d879cb83SAl Viro 12009221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 12019221d2e3SAl Viro size_t len = i->iov[k].iov_len - skip; 12029221d2e3SAl Viro if (len) { 12039221d2e3SAl Viro res |= (unsigned long)i->iov[k].iov_base + skip; 12049221d2e3SAl Viro if (len > size) 12059221d2e3SAl Viro len = size; 12069221d2e3SAl Viro res |= len; 12079221d2e3SAl Viro size -= len; 12089221d2e3SAl Viro if (!size) 12099221d2e3SAl Viro break; 12109221d2e3SAl Viro } 12119221d2e3SAl Viro } 12129221d2e3SAl Viro return res; 12139221d2e3SAl Viro } 12149221d2e3SAl Viro 12159221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i) 12169221d2e3SAl Viro { 12179221d2e3SAl Viro unsigned res = 0; 12189221d2e3SAl Viro size_t size = i->count; 12199221d2e3SAl Viro unsigned skip = i->iov_offset; 12209221d2e3SAl Viro unsigned k; 12219221d2e3SAl Viro 12229221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 12239221d2e3SAl Viro size_t len = i->bvec[k].bv_len - skip; 12249221d2e3SAl Viro res |= (unsigned long)i->bvec[k].bv_offset + skip; 12259221d2e3SAl Viro if (len > size) 12269221d2e3SAl Viro len = size; 12279221d2e3SAl Viro res |= len; 12289221d2e3SAl Viro size -= len; 12299221d2e3SAl Viro if (!size) 12309221d2e3SAl Viro break; 12319221d2e3SAl Viro } 12329221d2e3SAl Viro return res; 12339221d2e3SAl Viro } 12349221d2e3SAl Viro 12359221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i) 12369221d2e3SAl Viro { 1237fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1238fcb14cb1SAl Viro size_t size = i->count; 1239fcb14cb1SAl Viro if (size) 1240fcb14cb1SAl Viro return ((unsigned long)i->ubuf + i->iov_offset) | size; 1241fcb14cb1SAl Viro return 0; 1242fcb14cb1SAl Viro } 1243fcb14cb1SAl Viro 12449221d2e3SAl Viro /* iovec and kvec have identical layouts */ 12459221d2e3SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 12469221d2e3SAl Viro return iov_iter_alignment_iovec(i); 12479221d2e3SAl Viro 12489221d2e3SAl Viro if (iov_iter_is_bvec(i)) 12499221d2e3SAl Viro return iov_iter_alignment_bvec(i); 12509221d2e3SAl Viro 12519221d2e3SAl Viro if (iov_iter_is_pipe(i)) { 12529221d2e3SAl Viro size_t size = i->count; 1253e0ff126eSJan Kara 125410f525a8SAl Viro if (size && i->last_offset > 0) 125510f525a8SAl Viro return size | i->last_offset; 1256241699cdSAl Viro return size; 1257241699cdSAl Viro } 12589221d2e3SAl Viro 12599221d2e3SAl Viro if (iov_iter_is_xarray(i)) 12603d14ec1fSDavid Howells return (i->xarray_start + i->iov_offset) | i->count; 12619221d2e3SAl Viro 12629221d2e3SAl Viro return 0; 1263d879cb83SAl Viro } 1264d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment); 1265d879cb83SAl Viro 1266357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i) 1267357f435dSAl Viro { 1268357f435dSAl Viro unsigned long res = 0; 1269610c7a71SAl Viro unsigned long v = 0; 1270357f435dSAl Viro size_t size = i->count; 1271610c7a71SAl Viro unsigned k; 1272357f435dSAl Viro 1273fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1274fcb14cb1SAl Viro return 0; 1275fcb14cb1SAl Viro 1276610c7a71SAl Viro if (WARN_ON(!iter_is_iovec(i))) 1277241699cdSAl Viro return ~0U; 1278241699cdSAl Viro 1279610c7a71SAl Viro for (k = 0; k < i->nr_segs; k++) { 1280610c7a71SAl Viro if (i->iov[k].iov_len) { 1281610c7a71SAl Viro unsigned long base = (unsigned long)i->iov[k].iov_base; 1282610c7a71SAl Viro if (v) // if not the first one 1283610c7a71SAl Viro res |= base | v; // this start | previous end 1284610c7a71SAl Viro v = base + i->iov[k].iov_len; 1285610c7a71SAl Viro if (size <= i->iov[k].iov_len) 1286610c7a71SAl Viro break; 1287610c7a71SAl Viro size -= i->iov[k].iov_len; 1288610c7a71SAl Viro } 1289610c7a71SAl Viro } 1290357f435dSAl Viro return res; 1291357f435dSAl Viro } 1292357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment); 1293357f435dSAl Viro 12943cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size, 12953cf42da3SAl Viro size_t start, unsigned int maxpages) 1296acbdeb83SAl Viro { 12973cf42da3SAl Viro unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE); 12983cf42da3SAl Viro 12993cf42da3SAl Viro if (count > maxpages) 13003cf42da3SAl Viro count = maxpages; 13013cf42da3SAl Viro WARN_ON(!count); // caller should've prevented that 13023cf42da3SAl Viro if (!*res) { 13033cf42da3SAl Viro *res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL); 13043cf42da3SAl Viro if (!*res) 13053cf42da3SAl Viro return 0; 13063cf42da3SAl Viro } 13073cf42da3SAl Viro return count; 1308acbdeb83SAl Viro } 1309acbdeb83SAl Viro 131085200084SAl Viro static ssize_t pipe_get_pages(struct iov_iter *i, 131185200084SAl Viro struct page ***pages, size_t maxsize, unsigned maxpages, 131285200084SAl Viro size_t *start) 1313241699cdSAl Viro { 1314746de1f8SAl Viro unsigned int npages, count, off, chunk; 131585200084SAl Viro struct page **p; 1316746de1f8SAl Viro size_t left; 1317241699cdSAl Viro 131885200084SAl Viro if (!sanity(i)) 131985200084SAl Viro return -EFAULT; 132085200084SAl Viro 132185200084SAl Viro *start = off = pipe_npages(i, &npages); 13223cf42da3SAl Viro if (!npages) 13233cf42da3SAl Viro return -EFAULT; 13243cf42da3SAl Viro count = want_pages_array(pages, maxsize, off, min(npages, maxpages)); 13253cf42da3SAl Viro if (!count) 132685200084SAl Viro return -ENOMEM; 13273cf42da3SAl Viro p = *pages; 1328746de1f8SAl Viro for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) { 1329746de1f8SAl Viro struct page *page = append_pipe(i, left, &off); 1330e3b42964SAl Viro if (!page) 1331e3b42964SAl Viro break; 1332746de1f8SAl Viro chunk = min_t(size_t, left, PAGE_SIZE - off); 133385200084SAl Viro get_page(*p++ = page); 1334e3b42964SAl Viro } 133585200084SAl Viro if (!npages) 1336241699cdSAl Viro return -EFAULT; 1337746de1f8SAl Viro return maxsize - left; 1338241699cdSAl Viro } 1339241699cdSAl Viro 13407ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa, 13417ff50620SDavid Howells pgoff_t index, unsigned int nr_pages) 13427ff50620SDavid Howells { 13437ff50620SDavid Howells XA_STATE(xas, xa, index); 13447ff50620SDavid Howells struct page *page; 13457ff50620SDavid Howells unsigned int ret = 0; 13467ff50620SDavid Howells 13477ff50620SDavid Howells rcu_read_lock(); 13487ff50620SDavid Howells for (page = xas_load(&xas); page; page = xas_next(&xas)) { 13497ff50620SDavid Howells if (xas_retry(&xas, page)) 13507ff50620SDavid Howells continue; 13517ff50620SDavid Howells 13527ff50620SDavid Howells /* Has the page moved or been split? */ 13537ff50620SDavid Howells if (unlikely(page != xas_reload(&xas))) { 13547ff50620SDavid Howells xas_reset(&xas); 13557ff50620SDavid Howells continue; 13567ff50620SDavid Howells } 13577ff50620SDavid Howells 13587ff50620SDavid Howells pages[ret] = find_subpage(page, xas.xa_index); 13597ff50620SDavid Howells get_page(pages[ret]); 13607ff50620SDavid Howells if (++ret == nr_pages) 13617ff50620SDavid Howells break; 13627ff50620SDavid Howells } 13637ff50620SDavid Howells rcu_read_unlock(); 13647ff50620SDavid Howells return ret; 13657ff50620SDavid Howells } 13667ff50620SDavid Howells 13677ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i, 136868fe506fSAl Viro struct page ***pages, size_t maxsize, 13697ff50620SDavid Howells unsigned maxpages, size_t *_start_offset) 13707ff50620SDavid Howells { 13713cf42da3SAl Viro unsigned nr, offset, count; 13723cf42da3SAl Viro pgoff_t index; 13737ff50620SDavid Howells loff_t pos; 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 13803cf42da3SAl Viro count = want_pages_array(pages, maxsize, offset, maxpages); 13813cf42da3SAl Viro if (!count) 138268fe506fSAl Viro return -ENOMEM; 138368fe506fSAl Viro nr = iter_xarray_populate_pages(*pages, i->xarray, index, count); 13847ff50620SDavid Howells if (nr == 0) 13857ff50620SDavid Howells return 0; 13867ff50620SDavid Howells 1387eba2d3d7SAl Viro maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize); 1388310d9d5aSAl Viro i->iov_offset += maxsize; 1389310d9d5aSAl Viro i->count -= maxsize; 1390eba2d3d7SAl Viro return maxsize; 13917ff50620SDavid Howells } 13927ff50620SDavid Howells 1393fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */ 1394dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size) 13953d671ca6SAl Viro { 13963d671ca6SAl Viro size_t skip; 13973d671ca6SAl Viro long k; 13983d671ca6SAl Viro 1399fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1400fcb14cb1SAl Viro return (unsigned long)i->ubuf + i->iov_offset; 1401fcb14cb1SAl Viro 14023d671ca6SAl Viro for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) { 14033d671ca6SAl Viro size_t len = i->iov[k].iov_len - skip; 14043d671ca6SAl Viro 14053d671ca6SAl Viro if (unlikely(!len)) 14063d671ca6SAl Viro continue; 140759dbd7d0SAl Viro if (*size > len) 14083d671ca6SAl Viro *size = len; 1409dd45ab9dSAl Viro return (unsigned long)i->iov[k].iov_base + skip; 14103d671ca6SAl Viro } 14113d671ca6SAl Viro BUG(); // if it had been empty, we wouldn't get called 14123d671ca6SAl Viro } 14133d671ca6SAl Viro 14143d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */ 14153d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i, 141659dbd7d0SAl Viro size_t *size, size_t *start) 14173d671ca6SAl Viro { 14183d671ca6SAl Viro struct page *page; 14193d671ca6SAl Viro size_t skip = i->iov_offset, len; 14203d671ca6SAl Viro 14213d671ca6SAl Viro len = i->bvec->bv_len - skip; 142259dbd7d0SAl Viro if (*size > len) 142359dbd7d0SAl Viro *size = len; 14243d671ca6SAl Viro skip += i->bvec->bv_offset; 14253d671ca6SAl Viro page = i->bvec->bv_page + skip / PAGE_SIZE; 1426dda8e5d1SAl Viro *start = skip % PAGE_SIZE; 14273d671ca6SAl Viro return page; 14283d671ca6SAl Viro } 14293d671ca6SAl Viro 143091329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i, 1431d879cb83SAl Viro struct page ***pages, size_t maxsize, 1432451c0ba9SAl Viro unsigned int maxpages, size_t *start) 1433d879cb83SAl Viro { 14343cf42da3SAl Viro unsigned int n; 1435d879cb83SAl Viro 1436d879cb83SAl Viro if (maxsize > i->count) 1437d879cb83SAl Viro maxsize = i->count; 14383d671ca6SAl Viro if (!maxsize) 14393d671ca6SAl Viro return 0; 14407392ed17SAl Viro if (maxsize > MAX_RW_COUNT) 14417392ed17SAl Viro maxsize = MAX_RW_COUNT; 1442d879cb83SAl Viro 1443fcb14cb1SAl Viro if (likely(user_backed_iter(i))) { 14443337ab08SAndreas Gruenbacher unsigned int gup_flags = 0; 14453d671ca6SAl Viro unsigned long addr; 14463cf42da3SAl Viro int res; 14479ea9ce04SDavid Howells 14483337ab08SAndreas Gruenbacher if (iov_iter_rw(i) != WRITE) 14493337ab08SAndreas Gruenbacher gup_flags |= FOLL_WRITE; 14503337ab08SAndreas Gruenbacher if (i->nofault) 14513337ab08SAndreas Gruenbacher gup_flags |= FOLL_NOFAULT; 14523337ab08SAndreas Gruenbacher 1453dd45ab9dSAl Viro addr = first_iovec_segment(i, &maxsize); 1454dd45ab9dSAl Viro *start = addr % PAGE_SIZE; 1455dd45ab9dSAl Viro addr &= PAGE_MASK; 14563cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 14573cf42da3SAl Viro if (!n) 1458d879cb83SAl Viro return -ENOMEM; 1459451c0ba9SAl Viro res = get_user_pages_fast(addr, n, gup_flags, *pages); 146091329559SAl Viro if (unlikely(res <= 0)) 1461d879cb83SAl Viro return res; 1462eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start); 1463eba2d3d7SAl Viro iov_iter_advance(i, maxsize); 1464eba2d3d7SAl Viro return maxsize; 14653d671ca6SAl Viro } 14663d671ca6SAl Viro if (iov_iter_is_bvec(i)) { 1467451c0ba9SAl Viro struct page **p; 14683d671ca6SAl Viro struct page *page; 14693d671ca6SAl Viro 147059dbd7d0SAl Viro page = first_bvec_segment(i, &maxsize, start); 14713cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 14723cf42da3SAl Viro if (!n) 1473d879cb83SAl Viro return -ENOMEM; 14743cf42da3SAl Viro p = *pages; 1475dda8e5d1SAl Viro for (int k = 0; k < n; k++) 1476eba2d3d7SAl Viro get_page(p[k] = page + k); 1477eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start); 1478310d9d5aSAl Viro i->count -= maxsize; 1479310d9d5aSAl Viro i->iov_offset += maxsize; 1480310d9d5aSAl Viro if (i->iov_offset == i->bvec->bv_len) { 1481310d9d5aSAl Viro i->iov_offset = 0; 1482310d9d5aSAl Viro i->bvec++; 1483310d9d5aSAl Viro i->nr_segs--; 1484310d9d5aSAl Viro } 1485eba2d3d7SAl Viro return maxsize; 14863d671ca6SAl Viro } 14873d671ca6SAl Viro if (iov_iter_is_pipe(i)) 1488451c0ba9SAl Viro return pipe_get_pages(i, pages, maxsize, maxpages, start); 14893d671ca6SAl Viro if (iov_iter_is_xarray(i)) 1490451c0ba9SAl Viro return iter_xarray_get_pages(i, pages, maxsize, maxpages, start); 1491d879cb83SAl Viro return -EFAULT; 1492d879cb83SAl Viro } 149391329559SAl Viro 1494eba2d3d7SAl Viro ssize_t iov_iter_get_pages2(struct iov_iter *i, 1495451c0ba9SAl Viro struct page **pages, size_t maxsize, unsigned maxpages, 1496451c0ba9SAl Viro size_t *start) 1497451c0ba9SAl Viro { 1498451c0ba9SAl Viro if (!maxpages) 1499451c0ba9SAl Viro return 0; 1500451c0ba9SAl Viro BUG_ON(!pages); 1501451c0ba9SAl Viro 1502451c0ba9SAl Viro return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, start); 1503451c0ba9SAl Viro } 1504eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2); 1505451c0ba9SAl Viro 1506eba2d3d7SAl Viro ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, 150791329559SAl Viro struct page ***pages, size_t maxsize, 150891329559SAl Viro size_t *start) 150991329559SAl Viro { 151091329559SAl Viro ssize_t len; 151191329559SAl Viro 151291329559SAl Viro *pages = NULL; 151391329559SAl Viro 1514451c0ba9SAl Viro len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start); 151591329559SAl Viro if (len <= 0) { 151691329559SAl Viro kvfree(*pages); 151791329559SAl Viro *pages = NULL; 151891329559SAl Viro } 151991329559SAl Viro return len; 152091329559SAl Viro } 1521eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2); 1522d879cb83SAl Viro 1523d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, 1524d879cb83SAl Viro struct iov_iter *i) 1525d879cb83SAl Viro { 1526d879cb83SAl Viro __wsum sum, next; 1527d879cb83SAl Viro sum = *csum; 15289ea9ce04SDavid Howells if (unlikely(iov_iter_is_pipe(i) || iov_iter_is_discard(i))) { 1529241699cdSAl Viro WARN_ON(1); 1530241699cdSAl Viro return 0; 1531241699cdSAl Viro } 15327baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, ({ 15337baa5099SAl Viro next = csum_and_copy_from_user(base, addr + off, len); 1534d879cb83SAl Viro sum = csum_block_add(sum, next, off); 15357baa5099SAl Viro next ? 0 : len; 1536d879cb83SAl Viro }), ({ 15377baa5099SAl Viro sum = csum_and_memcpy(addr + off, base, len, sum, off); 1538d879cb83SAl Viro }) 1539d879cb83SAl Viro ) 1540d879cb83SAl Viro *csum = sum; 1541d879cb83SAl Viro return bytes; 1542d879cb83SAl Viro } 1543d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter); 1544d879cb83SAl Viro 154552cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate, 1546d879cb83SAl Viro struct iov_iter *i) 1547d879cb83SAl Viro { 154852cbd23aSWillem de Bruijn struct csum_state *csstate = _csstate; 1549d879cb83SAl Viro __wsum sum, next; 155078e1f386SAl Viro 155178e1f386SAl Viro if (unlikely(iov_iter_is_discard(i))) { 1552*c67f1fd2SAl Viro // can't use csum_memcpy() for that one - data is not copied 1553*c67f1fd2SAl Viro csstate->csum = csum_block_add(csstate->csum, 1554*c67f1fd2SAl Viro csum_partial(addr, bytes, 0), 1555*c67f1fd2SAl Viro csstate->off); 1556*c67f1fd2SAl Viro csstate->off += bytes; 1557*c67f1fd2SAl Viro return bytes; 1558241699cdSAl Viro } 15596852df12SAl Viro 15606852df12SAl Viro sum = csum_shift(csstate->csum, csstate->off); 15616852df12SAl Viro if (unlikely(iov_iter_is_pipe(i))) 15626852df12SAl Viro bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum); 15636852df12SAl Viro else iterate_and_advance(i, bytes, base, len, off, ({ 15647baa5099SAl Viro next = csum_and_copy_to_user(addr + off, base, len); 1565d879cb83SAl Viro sum = csum_block_add(sum, next, off); 15667baa5099SAl Viro next ? 0 : len; 1567d879cb83SAl Viro }), ({ 15687baa5099SAl Viro sum = csum_and_memcpy(base, addr + off, len, sum, off); 1569d879cb83SAl Viro }) 1570d879cb83SAl Viro ) 1571594e450bSAl Viro csstate->csum = csum_shift(sum, csstate->off); 1572594e450bSAl Viro csstate->off += bytes; 1573d879cb83SAl Viro return bytes; 1574d879cb83SAl Viro } 1575d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter); 1576d879cb83SAl Viro 1577d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, 1578d05f4435SSagi Grimberg struct iov_iter *i) 1579d05f4435SSagi Grimberg { 15807999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH 1581d05f4435SSagi Grimberg struct ahash_request *hash = hashp; 1582d05f4435SSagi Grimberg struct scatterlist sg; 1583d05f4435SSagi Grimberg size_t copied; 1584d05f4435SSagi Grimberg 1585d05f4435SSagi Grimberg copied = copy_to_iter(addr, bytes, i); 1586d05f4435SSagi Grimberg sg_init_one(&sg, addr, copied); 1587d05f4435SSagi Grimberg ahash_request_set_crypt(hash, &sg, NULL, copied); 1588d05f4435SSagi Grimberg crypto_ahash_update(hash); 1589d05f4435SSagi Grimberg return copied; 159027fad74aSYueHaibing #else 159127fad74aSYueHaibing return 0; 159227fad74aSYueHaibing #endif 1593d05f4435SSagi Grimberg } 1594d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter); 1595d05f4435SSagi Grimberg 159666531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages) 1597d879cb83SAl Viro { 159866531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 159966531c65SAl Viro const struct iovec *p; 1600d879cb83SAl Viro int npages = 0; 1601d879cb83SAl Viro 160266531c65SAl Viro for (p = i->iov; size; skip = 0, p++) { 160366531c65SAl Viro unsigned offs = offset_in_page(p->iov_base + skip); 160466531c65SAl Viro size_t len = min(p->iov_len - skip, size); 1605d879cb83SAl Viro 160666531c65SAl Viro if (len) { 160766531c65SAl Viro size -= len; 160866531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 160966531c65SAl Viro if (unlikely(npages > maxpages)) 161066531c65SAl Viro return maxpages; 161166531c65SAl Viro } 161266531c65SAl Viro } 161366531c65SAl Viro return npages; 161466531c65SAl Viro } 161566531c65SAl Viro 161666531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages) 161766531c65SAl Viro { 161866531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 161966531c65SAl Viro const struct bio_vec *p; 162066531c65SAl Viro int npages = 0; 162166531c65SAl Viro 162266531c65SAl Viro for (p = i->bvec; size; skip = 0, p++) { 162366531c65SAl Viro unsigned offs = (p->bv_offset + skip) % PAGE_SIZE; 162466531c65SAl Viro size_t len = min(p->bv_len - skip, size); 162566531c65SAl Viro 162666531c65SAl Viro size -= len; 162766531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 162866531c65SAl Viro if (unlikely(npages > maxpages)) 162966531c65SAl Viro return maxpages; 163066531c65SAl Viro } 163166531c65SAl Viro return npages; 163266531c65SAl Viro } 163366531c65SAl Viro 163466531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages) 163566531c65SAl Viro { 163666531c65SAl Viro if (unlikely(!i->count)) 163766531c65SAl Viro return 0; 1638fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1639fcb14cb1SAl Viro unsigned offs = offset_in_page(i->ubuf + i->iov_offset); 1640fcb14cb1SAl Viro int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE); 1641fcb14cb1SAl Viro return min(npages, maxpages); 1642fcb14cb1SAl Viro } 164366531c65SAl Viro /* iovec and kvec have identical layouts */ 164466531c65SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 164566531c65SAl Viro return iov_npages(i, maxpages); 164666531c65SAl Viro if (iov_iter_is_bvec(i)) 164766531c65SAl Viro return bvec_npages(i, maxpages); 164866531c65SAl Viro if (iov_iter_is_pipe(i)) { 164966531c65SAl Viro int npages; 1650241699cdSAl Viro 1651241699cdSAl Viro if (!sanity(i)) 1652241699cdSAl Viro return 0; 1653241699cdSAl Viro 165412d426abSAl Viro pipe_npages(i, &npages); 165566531c65SAl Viro return min(npages, maxpages); 165666531c65SAl Viro } 165766531c65SAl Viro if (iov_iter_is_xarray(i)) { 1658e4f8df86SAl Viro unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE; 1659e4f8df86SAl Viro int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE); 166066531c65SAl Viro return min(npages, maxpages); 166166531c65SAl Viro } 166266531c65SAl Viro return 0; 1663d879cb83SAl Viro } 1664d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages); 1665d879cb83SAl Viro 1666d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags) 1667d879cb83SAl Viro { 1668d879cb83SAl Viro *new = *old; 166900e23707SDavid Howells if (unlikely(iov_iter_is_pipe(new))) { 1670241699cdSAl Viro WARN_ON(1); 1671241699cdSAl Viro return NULL; 1672241699cdSAl Viro } 167300e23707SDavid Howells if (iov_iter_is_bvec(new)) 1674d879cb83SAl Viro return new->bvec = kmemdup(new->bvec, 1675d879cb83SAl Viro new->nr_segs * sizeof(struct bio_vec), 1676d879cb83SAl Viro flags); 1677fcb14cb1SAl Viro else if (iov_iter_is_kvec(new) || iter_is_iovec(new)) 1678d879cb83SAl Viro /* iovec and kvec have identical layout */ 1679d879cb83SAl Viro return new->iov = kmemdup(new->iov, 1680d879cb83SAl Viro new->nr_segs * sizeof(struct iovec), 1681d879cb83SAl Viro flags); 1682fcb14cb1SAl Viro return NULL; 1683d879cb83SAl Viro } 1684d879cb83SAl Viro EXPORT_SYMBOL(dup_iter); 1685bc917be8SAl Viro 1686bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov, 1687bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1688bfdc5970SChristoph Hellwig { 1689bfdc5970SChristoph Hellwig const struct compat_iovec __user *uiov = 1690bfdc5970SChristoph Hellwig (const struct compat_iovec __user *)uvec; 1691bfdc5970SChristoph Hellwig int ret = -EFAULT, i; 1692bfdc5970SChristoph Hellwig 1693a959a978SChristoph Hellwig if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) 1694bfdc5970SChristoph Hellwig return -EFAULT; 1695bfdc5970SChristoph Hellwig 1696bfdc5970SChristoph Hellwig for (i = 0; i < nr_segs; i++) { 1697bfdc5970SChristoph Hellwig compat_uptr_t buf; 1698bfdc5970SChristoph Hellwig compat_ssize_t len; 1699bfdc5970SChristoph Hellwig 1700bfdc5970SChristoph Hellwig unsafe_get_user(len, &uiov[i].iov_len, uaccess_end); 1701bfdc5970SChristoph Hellwig unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end); 1702bfdc5970SChristoph Hellwig 1703bfdc5970SChristoph Hellwig /* check for compat_size_t not fitting in compat_ssize_t .. */ 1704bfdc5970SChristoph Hellwig if (len < 0) { 1705bfdc5970SChristoph Hellwig ret = -EINVAL; 1706bfdc5970SChristoph Hellwig goto uaccess_end; 1707bfdc5970SChristoph Hellwig } 1708bfdc5970SChristoph Hellwig iov[i].iov_base = compat_ptr(buf); 1709bfdc5970SChristoph Hellwig iov[i].iov_len = len; 1710bfdc5970SChristoph Hellwig } 1711bfdc5970SChristoph Hellwig 1712bfdc5970SChristoph Hellwig ret = 0; 1713bfdc5970SChristoph Hellwig uaccess_end: 1714bfdc5970SChristoph Hellwig user_access_end(); 1715bfdc5970SChristoph Hellwig return ret; 1716bfdc5970SChristoph Hellwig } 1717bfdc5970SChristoph Hellwig 1718bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov, 1719bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1720fb041b59SDavid Laight { 1721fb041b59SDavid Laight unsigned long seg; 1722bfdc5970SChristoph Hellwig 1723bfdc5970SChristoph Hellwig if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec))) 1724bfdc5970SChristoph Hellwig return -EFAULT; 1725bfdc5970SChristoph Hellwig for (seg = 0; seg < nr_segs; seg++) { 1726bfdc5970SChristoph Hellwig if ((ssize_t)iov[seg].iov_len < 0) 1727bfdc5970SChristoph Hellwig return -EINVAL; 1728bfdc5970SChristoph Hellwig } 1729bfdc5970SChristoph Hellwig 1730bfdc5970SChristoph Hellwig return 0; 1731bfdc5970SChristoph Hellwig } 1732bfdc5970SChristoph Hellwig 1733bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec, 1734bfdc5970SChristoph Hellwig unsigned long nr_segs, unsigned long fast_segs, 1735bfdc5970SChristoph Hellwig struct iovec *fast_iov, bool compat) 1736bfdc5970SChristoph Hellwig { 1737bfdc5970SChristoph Hellwig struct iovec *iov = fast_iov; 1738bfdc5970SChristoph Hellwig int ret; 1739fb041b59SDavid Laight 1740fb041b59SDavid Laight /* 1741bfdc5970SChristoph Hellwig * SuS says "The readv() function *may* fail if the iovcnt argument was 1742bfdc5970SChristoph Hellwig * less than or equal to 0, or greater than {IOV_MAX}. Linux has 1743fb041b59SDavid Laight * traditionally returned zero for zero segments, so... 1744fb041b59SDavid Laight */ 1745bfdc5970SChristoph Hellwig if (nr_segs == 0) 1746bfdc5970SChristoph Hellwig return iov; 1747bfdc5970SChristoph Hellwig if (nr_segs > UIO_MAXIOV) 1748bfdc5970SChristoph Hellwig return ERR_PTR(-EINVAL); 1749fb041b59SDavid Laight if (nr_segs > fast_segs) { 1750fb041b59SDavid Laight iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); 1751bfdc5970SChristoph Hellwig if (!iov) 1752bfdc5970SChristoph Hellwig return ERR_PTR(-ENOMEM); 1753fb041b59SDavid Laight } 1754bfdc5970SChristoph Hellwig 1755bfdc5970SChristoph Hellwig if (compat) 1756bfdc5970SChristoph Hellwig ret = copy_compat_iovec_from_user(iov, uvec, nr_segs); 1757bfdc5970SChristoph Hellwig else 1758bfdc5970SChristoph Hellwig ret = copy_iovec_from_user(iov, uvec, nr_segs); 1759bfdc5970SChristoph Hellwig if (ret) { 1760bfdc5970SChristoph Hellwig if (iov != fast_iov) 1761bfdc5970SChristoph Hellwig kfree(iov); 1762bfdc5970SChristoph Hellwig return ERR_PTR(ret); 1763fb041b59SDavid Laight } 1764bfdc5970SChristoph Hellwig 1765bfdc5970SChristoph Hellwig return iov; 1766bfdc5970SChristoph Hellwig } 1767bfdc5970SChristoph Hellwig 1768bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec, 1769bfdc5970SChristoph Hellwig unsigned nr_segs, unsigned fast_segs, struct iovec **iovp, 1770bfdc5970SChristoph Hellwig struct iov_iter *i, bool compat) 1771bfdc5970SChristoph Hellwig { 1772bfdc5970SChristoph Hellwig ssize_t total_len = 0; 1773bfdc5970SChristoph Hellwig unsigned long seg; 1774bfdc5970SChristoph Hellwig struct iovec *iov; 1775bfdc5970SChristoph Hellwig 1776bfdc5970SChristoph Hellwig iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat); 1777bfdc5970SChristoph Hellwig if (IS_ERR(iov)) { 1778bfdc5970SChristoph Hellwig *iovp = NULL; 1779bfdc5970SChristoph Hellwig return PTR_ERR(iov); 1780fb041b59SDavid Laight } 1781fb041b59SDavid Laight 1782fb041b59SDavid Laight /* 1783bfdc5970SChristoph Hellwig * According to the Single Unix Specification we should return EINVAL if 1784bfdc5970SChristoph Hellwig * an element length is < 0 when cast to ssize_t or if the total length 1785bfdc5970SChristoph Hellwig * would overflow the ssize_t return value of the system call. 1786fb041b59SDavid Laight * 1787fb041b59SDavid Laight * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the 1788fb041b59SDavid Laight * overflow case. 1789fb041b59SDavid Laight */ 1790fb041b59SDavid Laight for (seg = 0; seg < nr_segs; seg++) { 1791fb041b59SDavid Laight ssize_t len = (ssize_t)iov[seg].iov_len; 1792fb041b59SDavid Laight 1793bfdc5970SChristoph Hellwig if (!access_ok(iov[seg].iov_base, len)) { 1794bfdc5970SChristoph Hellwig if (iov != *iovp) 1795bfdc5970SChristoph Hellwig kfree(iov); 1796bfdc5970SChristoph Hellwig *iovp = NULL; 1797bfdc5970SChristoph Hellwig return -EFAULT; 1798fb041b59SDavid Laight } 1799bfdc5970SChristoph Hellwig 1800bfdc5970SChristoph Hellwig if (len > MAX_RW_COUNT - total_len) { 1801bfdc5970SChristoph Hellwig len = MAX_RW_COUNT - total_len; 1802fb041b59SDavid Laight iov[seg].iov_len = len; 1803fb041b59SDavid Laight } 1804bfdc5970SChristoph Hellwig total_len += len; 1805fb041b59SDavid Laight } 1806bfdc5970SChristoph Hellwig 1807bfdc5970SChristoph Hellwig iov_iter_init(i, type, iov, nr_segs, total_len); 1808bfdc5970SChristoph Hellwig if (iov == *iovp) 1809bfdc5970SChristoph Hellwig *iovp = NULL; 1810bfdc5970SChristoph Hellwig else 1811bfdc5970SChristoph Hellwig *iovp = iov; 1812bfdc5970SChristoph Hellwig return total_len; 1813fb041b59SDavid Laight } 1814fb041b59SDavid Laight 1815ffecee4fSVegard Nossum /** 1816ffecee4fSVegard Nossum * import_iovec() - Copy an array of &struct iovec from userspace 1817ffecee4fSVegard Nossum * into the kernel, check that it is valid, and initialize a new 1818ffecee4fSVegard Nossum * &struct iov_iter iterator to access it. 1819ffecee4fSVegard Nossum * 1820ffecee4fSVegard Nossum * @type: One of %READ or %WRITE. 1821bfdc5970SChristoph Hellwig * @uvec: Pointer to the userspace array. 1822ffecee4fSVegard Nossum * @nr_segs: Number of elements in userspace array. 1823ffecee4fSVegard Nossum * @fast_segs: Number of elements in @iov. 1824bfdc5970SChristoph Hellwig * @iovp: (input and output parameter) Pointer to pointer to (usually small 1825ffecee4fSVegard Nossum * on-stack) kernel array. 1826ffecee4fSVegard Nossum * @i: Pointer to iterator that will be initialized on success. 1827ffecee4fSVegard Nossum * 1828ffecee4fSVegard Nossum * If the array pointed to by *@iov is large enough to hold all @nr_segs, 1829ffecee4fSVegard Nossum * then this function places %NULL in *@iov on return. Otherwise, a new 1830ffecee4fSVegard Nossum * array will be allocated and the result placed in *@iov. This means that 1831ffecee4fSVegard Nossum * the caller may call kfree() on *@iov regardless of whether the small 1832ffecee4fSVegard Nossum * on-stack array was used or not (and regardless of whether this function 1833ffecee4fSVegard Nossum * returns an error or not). 1834ffecee4fSVegard Nossum * 183587e5e6daSJens Axboe * Return: Negative error code on error, bytes imported on success 1836ffecee4fSVegard Nossum */ 1837bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec, 1838bc917be8SAl Viro unsigned nr_segs, unsigned fast_segs, 1839bfdc5970SChristoph Hellwig struct iovec **iovp, struct iov_iter *i) 1840bc917be8SAl Viro { 184189cd35c5SChristoph Hellwig return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i, 184289cd35c5SChristoph Hellwig in_compat_syscall()); 1843bc917be8SAl Viro } 1844bc917be8SAl Viro EXPORT_SYMBOL(import_iovec); 1845bc917be8SAl Viro 1846bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len, 1847bc917be8SAl Viro struct iovec *iov, struct iov_iter *i) 1848bc917be8SAl Viro { 1849bc917be8SAl Viro if (len > MAX_RW_COUNT) 1850bc917be8SAl Viro len = MAX_RW_COUNT; 185196d4f267SLinus Torvalds if (unlikely(!access_ok(buf, len))) 1852bc917be8SAl Viro return -EFAULT; 1853bc917be8SAl Viro 1854bc917be8SAl Viro iov->iov_base = buf; 1855bc917be8SAl Viro iov->iov_len = len; 1856bc917be8SAl Viro iov_iter_init(i, rw, iov, 1, len); 1857bc917be8SAl Viro return 0; 1858bc917be8SAl Viro } 1859e1267585SAl Viro EXPORT_SYMBOL(import_single_range); 18608fb0f47aSJens Axboe 18618fb0f47aSJens Axboe /** 18628fb0f47aSJens Axboe * iov_iter_restore() - Restore a &struct iov_iter to the same state as when 18638fb0f47aSJens Axboe * iov_iter_save_state() was called. 18648fb0f47aSJens Axboe * 18658fb0f47aSJens Axboe * @i: &struct iov_iter to restore 18668fb0f47aSJens Axboe * @state: state to restore from 18678fb0f47aSJens Axboe * 18688fb0f47aSJens Axboe * Used after iov_iter_save_state() to bring restore @i, if operations may 18698fb0f47aSJens Axboe * have advanced it. 18708fb0f47aSJens Axboe * 18718fb0f47aSJens Axboe * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC 18728fb0f47aSJens Axboe */ 18738fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) 18748fb0f47aSJens Axboe { 18758fb0f47aSJens Axboe if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) && 1876fcb14cb1SAl Viro !iov_iter_is_kvec(i) && !iter_is_ubuf(i)) 18778fb0f47aSJens Axboe return; 18788fb0f47aSJens Axboe i->iov_offset = state->iov_offset; 18798fb0f47aSJens Axboe i->count = state->count; 1880fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1881fcb14cb1SAl Viro return; 18828fb0f47aSJens Axboe /* 18838fb0f47aSJens Axboe * For the *vec iters, nr_segs + iov is constant - if we increment 18848fb0f47aSJens Axboe * the vec, then we also decrement the nr_segs count. Hence we don't 18858fb0f47aSJens Axboe * need to track both of these, just one is enough and we can deduct 18868fb0f47aSJens Axboe * the other from that. ITER_KVEC and ITER_IOVEC are the same struct 18878fb0f47aSJens Axboe * size, so we can just increment the iov pointer as they are unionzed. 18888fb0f47aSJens Axboe * ITER_BVEC _may_ be the same size on some archs, but on others it is 18898fb0f47aSJens Axboe * not. Be safe and handle it separately. 18908fb0f47aSJens Axboe */ 18918fb0f47aSJens Axboe BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec)); 18928fb0f47aSJens Axboe if (iov_iter_is_bvec(i)) 18938fb0f47aSJens Axboe i->bvec -= state->nr_segs - i->nr_segs; 18948fb0f47aSJens Axboe else 18958fb0f47aSJens Axboe i->iov -= state->nr_segs - i->nr_segs; 18968fb0f47aSJens Axboe i->nr_segs = state->nr_segs; 18978fb0f47aSJens Axboe } 1898