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 189241699cdSAl Viro #ifdef PIPE_PARANOIA 190241699cdSAl Viro static bool sanity(const struct iov_iter *i) 191241699cdSAl Viro { 192241699cdSAl Viro struct pipe_inode_info *pipe = i->pipe; 1938cefc107SDavid Howells unsigned int p_head = pipe->head; 1948cefc107SDavid Howells unsigned int p_tail = pipe->tail; 1958cefc107SDavid Howells unsigned int p_occupancy = pipe_occupancy(p_head, p_tail); 1968cefc107SDavid Howells unsigned int i_head = i->head; 1978cefc107SDavid Howells unsigned int idx; 1988cefc107SDavid Howells 19910f525a8SAl Viro if (i->last_offset) { 200241699cdSAl Viro struct pipe_buffer *p; 2018cefc107SDavid Howells if (unlikely(p_occupancy == 0)) 202241699cdSAl Viro goto Bad; // pipe must be non-empty 2038cefc107SDavid Howells if (unlikely(i_head != p_head - 1)) 204241699cdSAl Viro goto Bad; // must be at the last buffer... 205241699cdSAl Viro 2062dcedb2aSAl Viro p = pipe_buf(pipe, i_head); 20710f525a8SAl Viro if (unlikely(p->offset + p->len != abs(i->last_offset))) 208241699cdSAl Viro goto Bad; // ... at the end of segment 209241699cdSAl Viro } else { 2108cefc107SDavid Howells if (i_head != p_head) 211241699cdSAl Viro goto Bad; // must be right after the last buffer 212241699cdSAl Viro } 213241699cdSAl Viro return true; 214241699cdSAl Viro Bad: 21510f525a8SAl Viro printk(KERN_ERR "idx = %d, offset = %d\n", i_head, i->last_offset); 2168cefc107SDavid Howells printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n", 2178cefc107SDavid Howells p_head, p_tail, pipe->ring_size); 2188cefc107SDavid Howells for (idx = 0; idx < pipe->ring_size; idx++) 219241699cdSAl Viro printk(KERN_ERR "[%p %p %d %d]\n", 220241699cdSAl Viro pipe->bufs[idx].ops, 221241699cdSAl Viro pipe->bufs[idx].page, 222241699cdSAl Viro pipe->bufs[idx].offset, 223241699cdSAl Viro pipe->bufs[idx].len); 224241699cdSAl Viro WARN_ON(1); 225241699cdSAl Viro return false; 226241699cdSAl Viro } 227241699cdSAl Viro #else 228241699cdSAl Viro #define sanity(i) true 229241699cdSAl Viro #endif 230241699cdSAl Viro 23147b7fcaeSAl Viro static struct page *push_anon(struct pipe_inode_info *pipe, unsigned size) 23247b7fcaeSAl Viro { 23347b7fcaeSAl Viro struct page *page = alloc_page(GFP_USER); 23447b7fcaeSAl Viro if (page) { 23547b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++); 23647b7fcaeSAl Viro *buf = (struct pipe_buffer) { 23747b7fcaeSAl Viro .ops = &default_pipe_buf_ops, 23847b7fcaeSAl Viro .page = page, 23947b7fcaeSAl Viro .offset = 0, 24047b7fcaeSAl Viro .len = size 24147b7fcaeSAl Viro }; 24247b7fcaeSAl Viro } 24347b7fcaeSAl Viro return page; 24447b7fcaeSAl Viro } 24547b7fcaeSAl Viro 24647b7fcaeSAl Viro static void push_page(struct pipe_inode_info *pipe, struct page *page, 24747b7fcaeSAl Viro unsigned int offset, unsigned int size) 24847b7fcaeSAl Viro { 24947b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++); 25047b7fcaeSAl Viro *buf = (struct pipe_buffer) { 25147b7fcaeSAl Viro .ops = &page_cache_pipe_buf_ops, 25247b7fcaeSAl Viro .page = page, 25347b7fcaeSAl Viro .offset = offset, 25447b7fcaeSAl Viro .len = size 25547b7fcaeSAl Viro }; 25647b7fcaeSAl Viro get_page(page); 25747b7fcaeSAl Viro } 25847b7fcaeSAl Viro 25910f525a8SAl Viro static inline int last_offset(const struct pipe_buffer *buf) 2608fad7767SAl Viro { 26110f525a8SAl Viro if (buf->ops == &default_pipe_buf_ops) 26210f525a8SAl Viro return buf->len; // buf->offset is 0 for those 26310f525a8SAl Viro else 26410f525a8SAl Viro return -(buf->offset + buf->len); 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; 27110f525a8SAl Viro int offset = i->last_offset; 2728fad7767SAl Viro struct pipe_buffer *buf; 2738fad7767SAl Viro struct page *page; 2748fad7767SAl Viro 27510f525a8SAl Viro if (offset > 0 && offset < PAGE_SIZE) { 27610f525a8SAl Viro // some space in the last buffer; add to it 2778fad7767SAl Viro buf = pipe_buf(pipe, pipe->head - 1); 2788fad7767SAl Viro size = min_t(size_t, size, PAGE_SIZE - offset); 2798fad7767SAl Viro buf->len += size; 28010f525a8SAl Viro i->last_offset += size; 2818fad7767SAl Viro i->count -= size; 2828fad7767SAl Viro *off = offset; 2838fad7767SAl Viro return buf->page; 2848fad7767SAl Viro } 2858fad7767SAl Viro // OK, we need a new buffer 2868fad7767SAl Viro *off = 0; 2878fad7767SAl Viro size = min_t(size_t, size, PAGE_SIZE); 2888fad7767SAl Viro if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 2898fad7767SAl Viro return NULL; 2908fad7767SAl Viro page = push_anon(pipe, size); 2918fad7767SAl Viro if (!page) 2928fad7767SAl Viro return NULL; 2938fad7767SAl Viro i->head = pipe->head - 1; 29410f525a8SAl Viro i->last_offset = size; 2958fad7767SAl Viro i->count -= size; 2968fad7767SAl Viro return page; 2978fad7767SAl Viro } 2988fad7767SAl Viro 299241699cdSAl Viro static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes, 300241699cdSAl Viro struct iov_iter *i) 301241699cdSAl Viro { 302241699cdSAl Viro struct pipe_inode_info *pipe = i->pipe; 30347b7fcaeSAl Viro unsigned int head = pipe->head; 304241699cdSAl Viro 305241699cdSAl Viro if (unlikely(bytes > i->count)) 306241699cdSAl Viro bytes = i->count; 307241699cdSAl Viro 308241699cdSAl Viro if (unlikely(!bytes)) 309241699cdSAl Viro return 0; 310241699cdSAl Viro 311241699cdSAl Viro if (!sanity(i)) 312241699cdSAl Viro return 0; 313241699cdSAl Viro 31410f525a8SAl Viro if (offset && i->last_offset == -offset) { // could we merge it? 31547b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, head - 1); 31647b7fcaeSAl Viro if (buf->page == page) { 317241699cdSAl Viro buf->len += bytes; 31810f525a8SAl Viro i->last_offset -= bytes; 31947b7fcaeSAl Viro i->count -= bytes; 32047b7fcaeSAl Viro return bytes; 321241699cdSAl Viro } 322241699cdSAl Viro } 32347b7fcaeSAl Viro if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 324241699cdSAl Viro return 0; 3258cefc107SDavid Howells 32647b7fcaeSAl Viro push_page(pipe, page, offset, bytes); 32710f525a8SAl Viro i->last_offset = -(offset + bytes); 32847b7fcaeSAl Viro i->head = head; 329241699cdSAl Viro i->count -= bytes; 330241699cdSAl Viro return bytes; 331241699cdSAl Viro } 332241699cdSAl Viro 333d879cb83SAl Viro /* 334a6294593SAndreas Gruenbacher * fault_in_iov_iter_readable - fault in iov iterator for reading 335a6294593SAndreas Gruenbacher * @i: iterator 336a6294593SAndreas Gruenbacher * @size: maximum length 337171a0203SAnton Altaparmakov * 338a6294593SAndreas Gruenbacher * Fault in one or more iovecs of the given iov_iter, to a maximum length of 339a6294593SAndreas Gruenbacher * @size. For each iovec, fault in each page that constitutes the iovec. 340a6294593SAndreas Gruenbacher * 341a6294593SAndreas Gruenbacher * Returns the number of bytes not faulted in (like copy_to_user() and 342a6294593SAndreas Gruenbacher * copy_from_user()). 343a6294593SAndreas Gruenbacher * 344a6294593SAndreas Gruenbacher * Always returns 0 for non-userspace iterators. 345171a0203SAnton Altaparmakov */ 346a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size) 347171a0203SAnton Altaparmakov { 348fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 349fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 350fcb14cb1SAl Viro n -= fault_in_readable(i->ubuf + i->iov_offset, n); 351fcb14cb1SAl Viro return size - n; 352fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 353a6294593SAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 3548409a0d2SAl Viro const struct iovec *p; 3558409a0d2SAl Viro size_t skip; 3568409a0d2SAl Viro 357a6294593SAndreas Gruenbacher size -= count; 358a6294593SAndreas Gruenbacher for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) { 359a6294593SAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 360a6294593SAndreas Gruenbacher size_t ret; 3618409a0d2SAl Viro 3628409a0d2SAl Viro if (unlikely(!len)) 3638409a0d2SAl Viro continue; 364a6294593SAndreas Gruenbacher ret = fault_in_readable(p->iov_base + skip, len); 365a6294593SAndreas Gruenbacher count -= len - ret; 366a6294593SAndreas Gruenbacher if (ret) 367a6294593SAndreas Gruenbacher break; 3688409a0d2SAl Viro } 369a6294593SAndreas Gruenbacher return count + size; 370171a0203SAnton Altaparmakov } 371171a0203SAnton Altaparmakov return 0; 372171a0203SAnton Altaparmakov } 373a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable); 374171a0203SAnton Altaparmakov 375cdd591fcSAndreas Gruenbacher /* 376cdd591fcSAndreas Gruenbacher * fault_in_iov_iter_writeable - fault in iov iterator for writing 377cdd591fcSAndreas Gruenbacher * @i: iterator 378cdd591fcSAndreas Gruenbacher * @size: maximum length 379cdd591fcSAndreas Gruenbacher * 380cdd591fcSAndreas Gruenbacher * Faults in the iterator using get_user_pages(), i.e., without triggering 381cdd591fcSAndreas Gruenbacher * hardware page faults. This is primarily useful when we already know that 382cdd591fcSAndreas Gruenbacher * some or all of the pages in @i aren't in memory. 383cdd591fcSAndreas Gruenbacher * 384cdd591fcSAndreas Gruenbacher * Returns the number of bytes not faulted in, like copy_to_user() and 385cdd591fcSAndreas Gruenbacher * copy_from_user(). 386cdd591fcSAndreas Gruenbacher * 387cdd591fcSAndreas Gruenbacher * Always returns 0 for non-user-space iterators. 388cdd591fcSAndreas Gruenbacher */ 389cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size) 390cdd591fcSAndreas Gruenbacher { 391fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 392fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 393fcb14cb1SAl Viro n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n); 394fcb14cb1SAl Viro return size - n; 395fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 396cdd591fcSAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 397cdd591fcSAndreas Gruenbacher const struct iovec *p; 398cdd591fcSAndreas Gruenbacher size_t skip; 399cdd591fcSAndreas Gruenbacher 400cdd591fcSAndreas Gruenbacher size -= count; 401cdd591fcSAndreas Gruenbacher for (p = i->iov, skip = i->iov_offset; count; p++, skip = 0) { 402cdd591fcSAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 403cdd591fcSAndreas Gruenbacher size_t ret; 404cdd591fcSAndreas Gruenbacher 405cdd591fcSAndreas Gruenbacher if (unlikely(!len)) 406cdd591fcSAndreas Gruenbacher continue; 407cdd591fcSAndreas Gruenbacher ret = fault_in_safe_writeable(p->iov_base + skip, len); 408cdd591fcSAndreas Gruenbacher count -= len - ret; 409cdd591fcSAndreas Gruenbacher if (ret) 410cdd591fcSAndreas Gruenbacher break; 411cdd591fcSAndreas Gruenbacher } 412cdd591fcSAndreas Gruenbacher return count + size; 413cdd591fcSAndreas Gruenbacher } 414cdd591fcSAndreas Gruenbacher return 0; 415cdd591fcSAndreas Gruenbacher } 416cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable); 417cdd591fcSAndreas Gruenbacher 418aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction, 419d879cb83SAl Viro const struct iovec *iov, unsigned long nr_segs, 420d879cb83SAl Viro size_t count) 421d879cb83SAl Viro { 422aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 4238cd54c1cSAl Viro *i = (struct iov_iter) { 4248cd54c1cSAl Viro .iter_type = ITER_IOVEC, 4253337ab08SAndreas Gruenbacher .nofault = false, 426fcb14cb1SAl Viro .user_backed = true, 4278cd54c1cSAl Viro .data_source = direction, 4288cd54c1cSAl Viro .iov = iov, 4298cd54c1cSAl Viro .nr_segs = nr_segs, 4308cd54c1cSAl Viro .iov_offset = 0, 4318cd54c1cSAl Viro .count = count 4328cd54c1cSAl Viro }; 433d879cb83SAl Viro } 434d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init); 435d879cb83SAl Viro 43612d426abSAl Viro // returns the offset in partial buffer (if any) 43712d426abSAl Viro static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages) 438241699cdSAl Viro { 43912d426abSAl Viro struct pipe_inode_info *pipe = i->pipe; 44012d426abSAl Viro int used = pipe->head - pipe->tail; 44110f525a8SAl Viro int off = i->last_offset; 4428cefc107SDavid Howells 44312d426abSAl Viro *npages = max((int)pipe->max_usage - used, 0); 44412d426abSAl Viro 44510f525a8SAl Viro if (off > 0 && off < PAGE_SIZE) { // anon and not full 44612d426abSAl Viro (*npages)++; 44712d426abSAl Viro return off; 44810f525a8SAl Viro } 44912d426abSAl Viro return 0; 450241699cdSAl Viro } 451241699cdSAl Viro 452241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes, 453241699cdSAl Viro struct iov_iter *i) 454241699cdSAl Viro { 4558fad7767SAl Viro unsigned int off, chunk; 4568fad7767SAl Viro 4578fad7767SAl Viro if (unlikely(bytes > i->count)) 4588fad7767SAl Viro bytes = i->count; 4598fad7767SAl Viro if (unlikely(!bytes)) 4608fad7767SAl Viro return 0; 461241699cdSAl Viro 462241699cdSAl Viro if (!sanity(i)) 463241699cdSAl Viro return 0; 464241699cdSAl Viro 4658fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 4668fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 4678fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 4688fad7767SAl Viro if (!page) 4698fad7767SAl Viro return bytes - n; 4708fad7767SAl Viro memcpy_to_page(page, off, addr, chunk); 471241699cdSAl Viro addr += chunk; 4728fad7767SAl Viro } 473241699cdSAl Viro return bytes; 474241699cdSAl Viro } 475241699cdSAl Viro 476f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len, 477f9152895SAl Viro __wsum sum, size_t off) 478f9152895SAl Viro { 479cc44c17bSAl Viro __wsum next = csum_partial_copy_nocheck(from, to, len); 480f9152895SAl Viro return csum_block_add(sum, next, off); 481f9152895SAl Viro } 482f9152895SAl Viro 48378e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes, 4846852df12SAl Viro struct iov_iter *i, __wsum *sump) 48578e1f386SAl Viro { 4866852df12SAl Viro __wsum sum = *sump; 4876852df12SAl Viro size_t off = 0; 4888fad7767SAl Viro unsigned int chunk, r; 4898fad7767SAl Viro 4908fad7767SAl Viro if (unlikely(bytes > i->count)) 4918fad7767SAl Viro bytes = i->count; 4928fad7767SAl Viro if (unlikely(!bytes)) 4938fad7767SAl Viro return 0; 49478e1f386SAl Viro 49578e1f386SAl Viro if (!sanity(i)) 49678e1f386SAl Viro return 0; 49778e1f386SAl Viro 4986852df12SAl Viro while (bytes) { 4998fad7767SAl Viro struct page *page = append_pipe(i, bytes, &r); 5008fad7767SAl Viro char *p; 5018fad7767SAl Viro 5028fad7767SAl Viro if (!page) 5038fad7767SAl Viro break; 5048fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - r); 5058fad7767SAl Viro p = kmap_local_page(page); 5066852df12SAl Viro sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off); 5072495bdccSAl Viro kunmap_local(p); 50878e1f386SAl Viro off += chunk; 5098fad7767SAl Viro bytes -= chunk; 5106852df12SAl Viro } 5116852df12SAl Viro *sump = sum; 5126852df12SAl Viro return off; 51378e1f386SAl Viro } 51478e1f386SAl Viro 515aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 516d879cb83SAl Viro { 517a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 518a41dad90SAl Viro return 0; 51900e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 520241699cdSAl Viro return copy_pipe_to_iter(addr, bytes, i); 521fcb14cb1SAl Viro if (user_backed_iter(i)) 52209fc68dcSAl Viro might_fault(); 5237baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 5247baa5099SAl Viro copyout(base, addr + off, len), 5257baa5099SAl Viro memcpy(base, addr + off, len) 526d879cb83SAl Viro ) 527d879cb83SAl Viro 528d879cb83SAl Viro return bytes; 529d879cb83SAl Viro } 530aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter); 531d879cb83SAl Viro 532ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC 533ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n) 5348780356eSDan Williams { 53596d4f267SLinus Torvalds if (access_ok(to, n)) { 536d0ef4c36SMarco Elver instrument_copy_to_user(to, from, n); 537ec6347bbSDan Williams n = copy_mc_to_user((__force void *) to, from, n); 5388780356eSDan Williams } 5398780356eSDan Williams return n; 5408780356eSDan Williams } 5418780356eSDan Williams 542ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes, 543ca146f6fSDan Williams struct iov_iter *i) 544ca146f6fSDan Williams { 5458fad7767SAl Viro size_t xfer = 0; 5468fad7767SAl Viro unsigned int off, chunk; 5478fad7767SAl Viro 5488fad7767SAl Viro if (unlikely(bytes > i->count)) 5498fad7767SAl Viro bytes = i->count; 5508fad7767SAl Viro if (unlikely(!bytes)) 5518fad7767SAl Viro return 0; 552ca146f6fSDan Williams 553ca146f6fSDan Williams if (!sanity(i)) 554ca146f6fSDan Williams return 0; 555ca146f6fSDan Williams 5568fad7767SAl Viro while (bytes) { 5578fad7767SAl Viro struct page *page = append_pipe(i, bytes, &off); 558ca146f6fSDan Williams unsigned long rem; 5598fad7767SAl Viro char *p; 5608fad7767SAl Viro 5618fad7767SAl Viro if (!page) 5628fad7767SAl Viro break; 5638fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - off); 5648fad7767SAl Viro p = kmap_local_page(page); 5652a510a74SAl Viro rem = copy_mc_to_kernel(p + off, addr + xfer, chunk); 5662a510a74SAl Viro chunk -= rem; 5672a510a74SAl Viro kunmap_local(p); 5682a510a74SAl Viro xfer += chunk; 5698fad7767SAl Viro bytes -= chunk; 570c3497fd0SAl Viro if (rem) { 5718fad7767SAl Viro iov_iter_revert(i, rem); 572ca146f6fSDan Williams break; 573c3497fd0SAl Viro } 5742a510a74SAl Viro } 575ca146f6fSDan Williams return xfer; 576ca146f6fSDan Williams } 577ca146f6fSDan Williams 578bf3eeb9bSDan Williams /** 579ec6347bbSDan Williams * _copy_mc_to_iter - copy to iter with source memory error exception handling 580bf3eeb9bSDan Williams * @addr: source kernel address 581bf3eeb9bSDan Williams * @bytes: total transfer length 58244e55997SRandy Dunlap * @i: destination iterator 583bf3eeb9bSDan Williams * 584ec6347bbSDan Williams * The pmem driver deploys this for the dax operation 585ec6347bbSDan Williams * (dax_copy_to_iter()) for dax reads (bypass page-cache and the 586ec6347bbSDan Williams * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes 587ec6347bbSDan Williams * successfully copied. 588bf3eeb9bSDan Williams * 589ec6347bbSDan Williams * The main differences between this and typical _copy_to_iter(). 590bf3eeb9bSDan Williams * 591bf3eeb9bSDan Williams * * Typical tail/residue handling after a fault retries the copy 592bf3eeb9bSDan Williams * byte-by-byte until the fault happens again. Re-triggering machine 593bf3eeb9bSDan Williams * checks is potentially fatal so the implementation uses source 594bf3eeb9bSDan Williams * alignment and poison alignment assumptions to avoid re-triggering 595bf3eeb9bSDan Williams * hardware exceptions. 596bf3eeb9bSDan Williams * 597bf3eeb9bSDan Williams * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies. 598bf3eeb9bSDan Williams * Compare to copy_to_iter() where only ITER_IOVEC attempts might return 599bf3eeb9bSDan Williams * a short copy. 60044e55997SRandy Dunlap * 60144e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 602bf3eeb9bSDan Williams */ 603ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 6048780356eSDan Williams { 605a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 606a41dad90SAl Viro return 0; 60700e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 608ec6347bbSDan Williams return copy_mc_pipe_to_iter(addr, bytes, i); 609fcb14cb1SAl Viro if (user_backed_iter(i)) 6108780356eSDan Williams might_fault(); 6117baa5099SAl Viro __iterate_and_advance(i, bytes, base, len, off, 6127baa5099SAl Viro copyout_mc(base, addr + off, len), 6137baa5099SAl Viro copy_mc_to_kernel(base, addr + off, len) 6148780356eSDan Williams ) 6158780356eSDan Williams 6168780356eSDan Williams return bytes; 6178780356eSDan Williams } 618ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter); 619ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */ 6208780356eSDan Williams 621aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) 622d879cb83SAl Viro { 623a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 624241699cdSAl Viro return 0; 625a41dad90SAl Viro 626fcb14cb1SAl Viro if (user_backed_iter(i)) 62709fc68dcSAl Viro might_fault(); 6287baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6297baa5099SAl Viro copyin(addr + off, base, len), 6307baa5099SAl Viro memcpy(addr + off, base, len) 631d879cb83SAl Viro ) 632d879cb83SAl Viro 633d879cb83SAl Viro return bytes; 634d879cb83SAl Viro } 635aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter); 636d879cb83SAl Viro 637aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) 638d879cb83SAl Viro { 639a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 640241699cdSAl Viro return 0; 641a41dad90SAl Viro 6427baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6437baa5099SAl Viro __copy_from_user_inatomic_nocache(addr + off, base, len), 6447baa5099SAl Viro memcpy(addr + off, base, len) 645d879cb83SAl Viro ) 646d879cb83SAl Viro 647d879cb83SAl Viro return bytes; 648d879cb83SAl Viro } 649aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache); 650d879cb83SAl Viro 6510aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE 652abd08d7dSDan Williams /** 653abd08d7dSDan Williams * _copy_from_iter_flushcache - write destination through cpu cache 654abd08d7dSDan Williams * @addr: destination kernel address 655abd08d7dSDan Williams * @bytes: total transfer length 65644e55997SRandy Dunlap * @i: source iterator 657abd08d7dSDan Williams * 658abd08d7dSDan Williams * The pmem driver arranges for filesystem-dax to use this facility via 659abd08d7dSDan Williams * dax_copy_from_iter() for ensuring that writes to persistent memory 660abd08d7dSDan Williams * are flushed through the CPU cache. It is differentiated from 661abd08d7dSDan Williams * _copy_from_iter_nocache() in that guarantees all data is flushed for 662abd08d7dSDan Williams * all iterator types. The _copy_from_iter_nocache() only attempts to 663abd08d7dSDan Williams * bypass the cache for the ITER_IOVEC case, and on some archs may use 664abd08d7dSDan Williams * instructions that strand dirty-data in the cache. 66544e55997SRandy Dunlap * 66644e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 667abd08d7dSDan Williams */ 6686a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i) 6690aed55afSDan Williams { 670a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 6710aed55afSDan Williams return 0; 672a41dad90SAl Viro 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 70140a86061SAl Viro if (WARN_ON(n > v || v > page_size(head))) 70272e809edSAl Viro return false; 70340a86061SAl Viro return true; 70472e809edSAl Viro } 705cbbd26b8SAl Viro 70608aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, 70708aa6479SAl Viro struct iov_iter *i) 70808aa6479SAl Viro { 70908aa6479SAl Viro size_t res = 0; 71040a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) 71108aa6479SAl Viro return 0; 712a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 713a41dad90SAl Viro return 0; 714f0f6b614SAl Viro if (unlikely(iov_iter_is_pipe(i))) 715f0f6b614SAl Viro return copy_page_to_iter_pipe(page, offset, bytes, i); 71608aa6479SAl Viro page += offset / PAGE_SIZE; // first subpage 71708aa6479SAl Viro offset %= PAGE_SIZE; 71808aa6479SAl Viro while (1) { 719f0f6b614SAl Viro void *kaddr = kmap_local_page(page); 720f0f6b614SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 721f0f6b614SAl Viro n = _copy_to_iter(kaddr + offset, n, i); 722f0f6b614SAl Viro kunmap_local(kaddr); 72308aa6479SAl Viro res += n; 72408aa6479SAl Viro bytes -= n; 72508aa6479SAl Viro if (!bytes || !n) 72608aa6479SAl Viro break; 72708aa6479SAl Viro offset += n; 72808aa6479SAl Viro if (offset == PAGE_SIZE) { 72908aa6479SAl Viro page++; 73008aa6479SAl Viro offset = 0; 73108aa6479SAl Viro } 73208aa6479SAl Viro } 73308aa6479SAl Viro return res; 73408aa6479SAl Viro } 735d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter); 736d879cb83SAl Viro 737d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, 738d879cb83SAl Viro struct iov_iter *i) 739d879cb83SAl Viro { 740c03f05f1SAl Viro size_t res = 0; 741c03f05f1SAl Viro if (!page_copy_sane(page, offset, bytes)) 74228f38db7SAl Viro return 0; 743c03f05f1SAl Viro page += offset / PAGE_SIZE; // first subpage 744c03f05f1SAl Viro offset %= PAGE_SIZE; 745c03f05f1SAl Viro while (1) { 746c03f05f1SAl Viro void *kaddr = kmap_local_page(page); 747c03f05f1SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 748c03f05f1SAl Viro n = _copy_from_iter(kaddr + offset, n, i); 749c03f05f1SAl Viro kunmap_local(kaddr); 750c03f05f1SAl Viro res += n; 751c03f05f1SAl Viro bytes -= n; 752c03f05f1SAl Viro if (!bytes || !n) 753c03f05f1SAl Viro break; 754c03f05f1SAl Viro offset += n; 755c03f05f1SAl Viro if (offset == PAGE_SIZE) { 756c03f05f1SAl Viro page++; 757c03f05f1SAl Viro offset = 0; 758c03f05f1SAl Viro } 759c03f05f1SAl Viro } 760c03f05f1SAl Viro return res; 761d879cb83SAl Viro } 762d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter); 763d879cb83SAl Viro 764241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i) 765241699cdSAl Viro { 7668fad7767SAl Viro unsigned int chunk, off; 7678fad7767SAl Viro 7688fad7767SAl Viro if (unlikely(bytes > i->count)) 7698fad7767SAl Viro bytes = i->count; 7708fad7767SAl Viro if (unlikely(!bytes)) 7718fad7767SAl Viro return 0; 772241699cdSAl Viro 773241699cdSAl Viro if (!sanity(i)) 774241699cdSAl Viro return 0; 775241699cdSAl Viro 7768fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 7778fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 7788fad7767SAl Viro char *p; 779241699cdSAl Viro 7808fad7767SAl Viro if (!page) 7818fad7767SAl Viro return bytes - n; 7828fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 7838fad7767SAl Viro p = kmap_local_page(page); 784893839fdSAl Viro memset(p + off, 0, chunk); 785893839fdSAl Viro kunmap_local(p); 7868fad7767SAl Viro } 787241699cdSAl Viro return bytes; 788241699cdSAl Viro } 789241699cdSAl Viro 790d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i) 791d879cb83SAl Viro { 79200e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 793241699cdSAl Viro return pipe_zero(bytes, i); 7947baa5099SAl Viro iterate_and_advance(i, bytes, base, len, count, 7957baa5099SAl Viro clear_user(base, len), 7967baa5099SAl Viro memset(base, 0, len) 797d879cb83SAl Viro ) 798d879cb83SAl Viro 799d879cb83SAl Viro return bytes; 800d879cb83SAl Viro } 801d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero); 802d879cb83SAl Viro 803f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes, 804f0b65f39SAl Viro struct iov_iter *i) 805d879cb83SAl Viro { 806d879cb83SAl Viro char *kaddr = kmap_atomic(page), *p = kaddr + offset; 80740a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) { 80872e809edSAl Viro kunmap_atomic(kaddr); 80972e809edSAl Viro return 0; 81072e809edSAl Viro } 811a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) { 812241699cdSAl Viro kunmap_atomic(kaddr); 813241699cdSAl Viro return 0; 814241699cdSAl Viro } 8157baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 8167baa5099SAl Viro copyin(p + off, base, len), 8177baa5099SAl Viro memcpy(p + off, base, len) 818d879cb83SAl Viro ) 819d879cb83SAl Viro kunmap_atomic(kaddr); 820d879cb83SAl Viro return bytes; 821d879cb83SAl Viro } 822f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic); 823d879cb83SAl Viro 824b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size) 825b9dc6f65SAl Viro { 826b9dc6f65SAl Viro struct pipe_inode_info *pipe = i->pipe; 82710f525a8SAl Viro int off = i->last_offset; 8288cefc107SDavid Howells 8292c855de9SAl Viro if (!off && !size) { 8302c855de9SAl Viro pipe_discard_from(pipe, i->start_head); // discard everything 8312c855de9SAl Viro return; 832b9dc6f65SAl Viro } 833b9dc6f65SAl Viro i->count -= size; 8342c855de9SAl Viro while (1) { 8352c855de9SAl Viro struct pipe_buffer *buf = pipe_buf(pipe, i->head); 8362c855de9SAl Viro if (off) /* make it relative to the beginning of buffer */ 83710f525a8SAl Viro size += abs(off) - buf->offset; 8382c855de9SAl Viro if (size <= buf->len) { 8392c855de9SAl Viro buf->len = size; 84010f525a8SAl Viro i->last_offset = last_offset(buf); 8412c855de9SAl Viro break; 8422c855de9SAl Viro } 8432c855de9SAl Viro size -= buf->len; 8442c855de9SAl Viro i->head++; 8452c855de9SAl Viro off = 0; 8462c855de9SAl Viro } 8472c855de9SAl Viro pipe_discard_from(pipe, i->head + 1); // discard everything past this one 848241699cdSAl Viro } 849241699cdSAl Viro 85054c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size) 85154c8195bSPavel Begunkov { 85218fa9af7SAl Viro const struct bio_vec *bvec, *end; 85354c8195bSPavel Begunkov 85418fa9af7SAl Viro if (!i->count) 85518fa9af7SAl Viro return; 85618fa9af7SAl Viro i->count -= size; 85754c8195bSPavel Begunkov 85818fa9af7SAl Viro size += i->iov_offset; 85918fa9af7SAl Viro 86018fa9af7SAl Viro for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) { 86118fa9af7SAl Viro if (likely(size < bvec->bv_len)) 86218fa9af7SAl Viro break; 86318fa9af7SAl Viro size -= bvec->bv_len; 86418fa9af7SAl Viro } 86518fa9af7SAl Viro i->iov_offset = size; 86618fa9af7SAl Viro i->nr_segs -= bvec - i->bvec; 86718fa9af7SAl Viro i->bvec = bvec; 86854c8195bSPavel Begunkov } 86954c8195bSPavel Begunkov 870185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size) 871185ac4d4SAl Viro { 872185ac4d4SAl Viro const struct iovec *iov, *end; 873185ac4d4SAl Viro 874185ac4d4SAl Viro if (!i->count) 875185ac4d4SAl Viro return; 876185ac4d4SAl Viro i->count -= size; 877185ac4d4SAl Viro 878185ac4d4SAl Viro size += i->iov_offset; // from beginning of current segment 879185ac4d4SAl Viro for (iov = i->iov, end = iov + i->nr_segs; iov < end; iov++) { 880185ac4d4SAl Viro if (likely(size < iov->iov_len)) 881185ac4d4SAl Viro break; 882185ac4d4SAl Viro size -= iov->iov_len; 883185ac4d4SAl Viro } 884185ac4d4SAl Viro i->iov_offset = size; 885185ac4d4SAl Viro i->nr_segs -= iov - i->iov; 886185ac4d4SAl Viro i->iov = iov; 887185ac4d4SAl Viro } 888185ac4d4SAl Viro 889d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size) 890d879cb83SAl Viro { 8913b3fc051SAl Viro if (unlikely(i->count < size)) 8923b3fc051SAl Viro size = i->count; 893fcb14cb1SAl Viro if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) { 894fcb14cb1SAl Viro i->iov_offset += size; 895fcb14cb1SAl Viro i->count -= size; 896fcb14cb1SAl Viro } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) { 897185ac4d4SAl Viro /* iovec and kvec have identical layouts */ 898185ac4d4SAl Viro iov_iter_iovec_advance(i, size); 899185ac4d4SAl Viro } else if (iov_iter_is_bvec(i)) { 900185ac4d4SAl Viro iov_iter_bvec_advance(i, size); 901185ac4d4SAl Viro } else if (iov_iter_is_pipe(i)) { 902241699cdSAl Viro pipe_advance(i, size); 903185ac4d4SAl Viro } else if (iov_iter_is_discard(i)) { 904185ac4d4SAl Viro i->count -= size; 9057ff50620SDavid Howells } 906d879cb83SAl Viro } 907d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance); 908d879cb83SAl Viro 90927c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll) 91027c0e374SAl Viro { 91127c0e374SAl Viro if (!unroll) 91227c0e374SAl Viro return; 9135b47d59aSAl Viro if (WARN_ON(unroll > MAX_RW_COUNT)) 9145b47d59aSAl Viro return; 91527c0e374SAl Viro i->count += unroll; 91600e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 91727c0e374SAl Viro struct pipe_inode_info *pipe = i->pipe; 91892acdc4fSAl Viro unsigned int head = pipe->head; 91992acdc4fSAl Viro 92092acdc4fSAl Viro while (head > i->start_head) { 92192acdc4fSAl Viro struct pipe_buffer *b = pipe_buf(pipe, --head); 92292acdc4fSAl Viro if (unroll < b->len) { 92392acdc4fSAl Viro b->len -= unroll; 92410f525a8SAl Viro i->last_offset = last_offset(b); 92592acdc4fSAl Viro i->head = head; 92692acdc4fSAl Viro return; 92727c0e374SAl Viro } 92892acdc4fSAl Viro unroll -= b->len; 92992acdc4fSAl Viro pipe_buf_release(pipe, b); 93092acdc4fSAl Viro pipe->head--; 93127c0e374SAl Viro } 93210f525a8SAl Viro i->last_offset = 0; 93392acdc4fSAl Viro i->head = head; 93427c0e374SAl Viro return; 93527c0e374SAl Viro } 9369ea9ce04SDavid Howells if (unlikely(iov_iter_is_discard(i))) 9379ea9ce04SDavid Howells return; 93827c0e374SAl Viro if (unroll <= i->iov_offset) { 93927c0e374SAl Viro i->iov_offset -= unroll; 94027c0e374SAl Viro return; 94127c0e374SAl Viro } 94227c0e374SAl Viro unroll -= i->iov_offset; 943fcb14cb1SAl Viro if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) { 9447ff50620SDavid Howells BUG(); /* We should never go beyond the start of the specified 9457ff50620SDavid Howells * range since we might then be straying into pages that 9467ff50620SDavid Howells * aren't pinned. 9477ff50620SDavid Howells */ 9487ff50620SDavid Howells } else if (iov_iter_is_bvec(i)) { 94927c0e374SAl Viro const struct bio_vec *bvec = i->bvec; 95027c0e374SAl Viro while (1) { 95127c0e374SAl Viro size_t n = (--bvec)->bv_len; 95227c0e374SAl Viro i->nr_segs++; 95327c0e374SAl Viro if (unroll <= n) { 95427c0e374SAl Viro i->bvec = bvec; 95527c0e374SAl Viro i->iov_offset = n - unroll; 95627c0e374SAl Viro return; 95727c0e374SAl Viro } 95827c0e374SAl Viro unroll -= n; 95927c0e374SAl Viro } 96027c0e374SAl Viro } else { /* same logics for iovec and kvec */ 96127c0e374SAl Viro const struct iovec *iov = i->iov; 96227c0e374SAl Viro while (1) { 96327c0e374SAl Viro size_t n = (--iov)->iov_len; 96427c0e374SAl Viro i->nr_segs++; 96527c0e374SAl Viro if (unroll <= n) { 96627c0e374SAl Viro i->iov = iov; 96727c0e374SAl Viro i->iov_offset = n - unroll; 96827c0e374SAl Viro return; 96927c0e374SAl Viro } 97027c0e374SAl Viro unroll -= n; 97127c0e374SAl Viro } 97227c0e374SAl Viro } 97327c0e374SAl Viro } 97427c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert); 97527c0e374SAl Viro 976d879cb83SAl Viro /* 977d879cb83SAl Viro * Return the count of just the current iov_iter segment. 978d879cb83SAl Viro */ 979d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i) 980d879cb83SAl Viro { 98128f38db7SAl Viro if (i->nr_segs > 1) { 98228f38db7SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 98328f38db7SAl Viro return min(i->count, i->iov->iov_len - i->iov_offset); 9847ff50620SDavid Howells if (iov_iter_is_bvec(i)) 985d879cb83SAl Viro return min(i->count, i->bvec->bv_len - i->iov_offset); 98628f38db7SAl Viro } 98728f38db7SAl Viro return i->count; 988d879cb83SAl Viro } 989d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count); 990d879cb83SAl Viro 991aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction, 992d879cb83SAl Viro const struct kvec *kvec, unsigned long nr_segs, 993d879cb83SAl Viro size_t count) 994d879cb83SAl Viro { 995aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 9968cd54c1cSAl Viro *i = (struct iov_iter){ 9978cd54c1cSAl Viro .iter_type = ITER_KVEC, 9988cd54c1cSAl Viro .data_source = direction, 9998cd54c1cSAl Viro .kvec = kvec, 10008cd54c1cSAl Viro .nr_segs = nr_segs, 10018cd54c1cSAl Viro .iov_offset = 0, 10028cd54c1cSAl Viro .count = count 10038cd54c1cSAl Viro }; 1004d879cb83SAl Viro } 1005d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec); 1006d879cb83SAl Viro 1007aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction, 1008d879cb83SAl Viro const struct bio_vec *bvec, unsigned long nr_segs, 1009d879cb83SAl Viro size_t count) 1010d879cb83SAl Viro { 1011aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 10128cd54c1cSAl Viro *i = (struct iov_iter){ 10138cd54c1cSAl Viro .iter_type = ITER_BVEC, 10148cd54c1cSAl Viro .data_source = direction, 10158cd54c1cSAl Viro .bvec = bvec, 10168cd54c1cSAl Viro .nr_segs = nr_segs, 10178cd54c1cSAl Viro .iov_offset = 0, 10188cd54c1cSAl Viro .count = count 10198cd54c1cSAl Viro }; 1020d879cb83SAl Viro } 1021d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec); 1022d879cb83SAl Viro 1023aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction, 1024241699cdSAl Viro struct pipe_inode_info *pipe, 1025241699cdSAl Viro size_t count) 1026241699cdSAl Viro { 1027aa563d7bSDavid Howells BUG_ON(direction != READ); 10288cefc107SDavid Howells WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size)); 10298cd54c1cSAl Viro *i = (struct iov_iter){ 10308cd54c1cSAl Viro .iter_type = ITER_PIPE, 10318cd54c1cSAl Viro .data_source = false, 10328cd54c1cSAl Viro .pipe = pipe, 10338cd54c1cSAl Viro .head = pipe->head, 10348cd54c1cSAl Viro .start_head = pipe->head, 103510f525a8SAl Viro .last_offset = 0, 10368cd54c1cSAl Viro .count = count 10378cd54c1cSAl Viro }; 1038241699cdSAl Viro } 1039241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe); 1040241699cdSAl Viro 10419ea9ce04SDavid Howells /** 10427ff50620SDavid Howells * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray 10437ff50620SDavid Howells * @i: The iterator to initialise. 10447ff50620SDavid Howells * @direction: The direction of the transfer. 10457ff50620SDavid Howells * @xarray: The xarray to access. 10467ff50620SDavid Howells * @start: The start file position. 10477ff50620SDavid Howells * @count: The size of the I/O buffer in bytes. 10487ff50620SDavid Howells * 10497ff50620SDavid Howells * Set up an I/O iterator to either draw data out of the pages attached to an 10507ff50620SDavid Howells * inode or to inject data into those pages. The pages *must* be prevented 10517ff50620SDavid Howells * from evaporation, either by taking a ref on them or locking them by the 10527ff50620SDavid Howells * caller. 10537ff50620SDavid Howells */ 10547ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction, 10557ff50620SDavid Howells struct xarray *xarray, loff_t start, size_t count) 10567ff50620SDavid Howells { 10577ff50620SDavid Howells BUG_ON(direction & ~1); 10588cd54c1cSAl Viro *i = (struct iov_iter) { 10598cd54c1cSAl Viro .iter_type = ITER_XARRAY, 10608cd54c1cSAl Viro .data_source = direction, 10618cd54c1cSAl Viro .xarray = xarray, 10628cd54c1cSAl Viro .xarray_start = start, 10638cd54c1cSAl Viro .count = count, 10648cd54c1cSAl Viro .iov_offset = 0 10658cd54c1cSAl Viro }; 10667ff50620SDavid Howells } 10677ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray); 10687ff50620SDavid Howells 10697ff50620SDavid Howells /** 10709ea9ce04SDavid Howells * iov_iter_discard - Initialise an I/O iterator that discards data 10719ea9ce04SDavid Howells * @i: The iterator to initialise. 10729ea9ce04SDavid Howells * @direction: The direction of the transfer. 10739ea9ce04SDavid Howells * @count: The size of the I/O buffer in bytes. 10749ea9ce04SDavid Howells * 10759ea9ce04SDavid Howells * Set up an I/O iterator that just discards everything that's written to it. 10769ea9ce04SDavid Howells * It's only available as a READ iterator. 10779ea9ce04SDavid Howells */ 10789ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count) 10799ea9ce04SDavid Howells { 10809ea9ce04SDavid Howells BUG_ON(direction != READ); 10818cd54c1cSAl Viro *i = (struct iov_iter){ 10828cd54c1cSAl Viro .iter_type = ITER_DISCARD, 10838cd54c1cSAl Viro .data_source = false, 10848cd54c1cSAl Viro .count = count, 10858cd54c1cSAl Viro .iov_offset = 0 10868cd54c1cSAl Viro }; 10879ea9ce04SDavid Howells } 10889ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard); 10899ea9ce04SDavid Howells 1090cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask, 1091cfa320f7SKeith Busch unsigned len_mask) 1092cfa320f7SKeith Busch { 1093cfa320f7SKeith Busch size_t size = i->count; 1094cfa320f7SKeith Busch size_t skip = i->iov_offset; 1095cfa320f7SKeith Busch unsigned k; 1096cfa320f7SKeith Busch 1097cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1098cfa320f7SKeith Busch size_t len = i->iov[k].iov_len - skip; 1099cfa320f7SKeith Busch 1100cfa320f7SKeith Busch if (len > size) 1101cfa320f7SKeith Busch len = size; 1102cfa320f7SKeith Busch if (len & len_mask) 1103cfa320f7SKeith Busch return false; 1104cfa320f7SKeith Busch if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask) 1105cfa320f7SKeith Busch return false; 1106cfa320f7SKeith Busch 1107cfa320f7SKeith Busch size -= len; 1108cfa320f7SKeith Busch if (!size) 1109cfa320f7SKeith Busch break; 1110cfa320f7SKeith Busch } 1111cfa320f7SKeith Busch return true; 1112cfa320f7SKeith Busch } 1113cfa320f7SKeith Busch 1114cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask, 1115cfa320f7SKeith Busch unsigned len_mask) 1116cfa320f7SKeith Busch { 1117cfa320f7SKeith Busch size_t size = i->count; 1118cfa320f7SKeith Busch unsigned skip = i->iov_offset; 1119cfa320f7SKeith Busch unsigned k; 1120cfa320f7SKeith Busch 1121cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1122cfa320f7SKeith Busch size_t len = i->bvec[k].bv_len - skip; 1123cfa320f7SKeith Busch 1124cfa320f7SKeith Busch if (len > size) 1125cfa320f7SKeith Busch len = size; 1126cfa320f7SKeith Busch if (len & len_mask) 1127cfa320f7SKeith Busch return false; 1128cfa320f7SKeith Busch if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask) 1129cfa320f7SKeith Busch return false; 1130cfa320f7SKeith Busch 1131cfa320f7SKeith Busch size -= len; 1132cfa320f7SKeith Busch if (!size) 1133cfa320f7SKeith Busch break; 1134cfa320f7SKeith Busch } 1135cfa320f7SKeith Busch return true; 1136cfa320f7SKeith Busch } 1137cfa320f7SKeith Busch 1138cfa320f7SKeith Busch /** 1139cfa320f7SKeith Busch * iov_iter_is_aligned() - Check if the addresses and lengths of each segments 1140cfa320f7SKeith Busch * are aligned to the parameters. 1141cfa320f7SKeith Busch * 1142cfa320f7SKeith Busch * @i: &struct iov_iter to restore 1143cfa320f7SKeith Busch * @addr_mask: bit mask to check against the iov element's addresses 1144cfa320f7SKeith Busch * @len_mask: bit mask to check against the iov element's lengths 1145cfa320f7SKeith Busch * 1146cfa320f7SKeith Busch * Return: false if any addresses or lengths intersect with the provided masks 1147cfa320f7SKeith Busch */ 1148cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask, 1149cfa320f7SKeith Busch unsigned len_mask) 1150cfa320f7SKeith Busch { 1151fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1152fcb14cb1SAl Viro if (i->count & len_mask) 1153fcb14cb1SAl Viro return false; 1154fcb14cb1SAl Viro if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask) 1155fcb14cb1SAl Viro return false; 1156fcb14cb1SAl Viro return true; 1157fcb14cb1SAl Viro } 1158fcb14cb1SAl Viro 1159cfa320f7SKeith Busch if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 1160cfa320f7SKeith Busch return iov_iter_aligned_iovec(i, addr_mask, len_mask); 1161cfa320f7SKeith Busch 1162cfa320f7SKeith Busch if (iov_iter_is_bvec(i)) 1163cfa320f7SKeith Busch return iov_iter_aligned_bvec(i, addr_mask, len_mask); 1164cfa320f7SKeith Busch 1165cfa320f7SKeith Busch if (iov_iter_is_pipe(i)) { 1166cfa320f7SKeith Busch size_t size = i->count; 1167cfa320f7SKeith Busch 1168cfa320f7SKeith Busch if (size & len_mask) 1169cfa320f7SKeith Busch return false; 117010f525a8SAl Viro if (size && i->last_offset > 0) { 117110f525a8SAl Viro if (i->last_offset & addr_mask) 1172cfa320f7SKeith Busch return false; 1173cfa320f7SKeith Busch } 1174cfa320f7SKeith Busch 1175cfa320f7SKeith Busch return true; 1176cfa320f7SKeith Busch } 1177cfa320f7SKeith Busch 1178cfa320f7SKeith Busch if (iov_iter_is_xarray(i)) { 1179cfa320f7SKeith Busch if (i->count & len_mask) 1180cfa320f7SKeith Busch return false; 1181cfa320f7SKeith Busch if ((i->xarray_start + i->iov_offset) & addr_mask) 1182cfa320f7SKeith Busch return false; 1183cfa320f7SKeith Busch } 1184cfa320f7SKeith Busch 1185cfa320f7SKeith Busch return true; 1186cfa320f7SKeith Busch } 1187cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned); 1188cfa320f7SKeith Busch 11899221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i) 1190d879cb83SAl Viro { 1191d879cb83SAl Viro unsigned long res = 0; 1192d879cb83SAl Viro size_t size = i->count; 11939221d2e3SAl Viro size_t skip = i->iov_offset; 11949221d2e3SAl Viro unsigned k; 1195d879cb83SAl Viro 11969221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 11979221d2e3SAl Viro size_t len = i->iov[k].iov_len - skip; 11989221d2e3SAl Viro if (len) { 11999221d2e3SAl Viro res |= (unsigned long)i->iov[k].iov_base + skip; 12009221d2e3SAl Viro if (len > size) 12019221d2e3SAl Viro len = size; 12029221d2e3SAl Viro res |= len; 12039221d2e3SAl Viro size -= len; 12049221d2e3SAl Viro if (!size) 12059221d2e3SAl Viro break; 12069221d2e3SAl Viro } 12079221d2e3SAl Viro } 12089221d2e3SAl Viro return res; 12099221d2e3SAl Viro } 12109221d2e3SAl Viro 12119221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i) 12129221d2e3SAl Viro { 12139221d2e3SAl Viro unsigned res = 0; 12149221d2e3SAl Viro size_t size = i->count; 12159221d2e3SAl Viro unsigned skip = i->iov_offset; 12169221d2e3SAl Viro unsigned k; 12179221d2e3SAl Viro 12189221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 12199221d2e3SAl Viro size_t len = i->bvec[k].bv_len - skip; 12209221d2e3SAl Viro res |= (unsigned long)i->bvec[k].bv_offset + skip; 12219221d2e3SAl Viro if (len > size) 12229221d2e3SAl Viro len = size; 12239221d2e3SAl Viro res |= len; 12249221d2e3SAl Viro size -= len; 12259221d2e3SAl Viro if (!size) 12269221d2e3SAl Viro break; 12279221d2e3SAl Viro } 12289221d2e3SAl Viro return res; 12299221d2e3SAl Viro } 12309221d2e3SAl Viro 12319221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i) 12329221d2e3SAl Viro { 1233fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1234fcb14cb1SAl Viro size_t size = i->count; 1235fcb14cb1SAl Viro if (size) 1236fcb14cb1SAl Viro return ((unsigned long)i->ubuf + i->iov_offset) | size; 1237fcb14cb1SAl Viro return 0; 1238fcb14cb1SAl Viro } 1239fcb14cb1SAl Viro 12409221d2e3SAl Viro /* iovec and kvec have identical layouts */ 12419221d2e3SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 12429221d2e3SAl Viro return iov_iter_alignment_iovec(i); 12439221d2e3SAl Viro 12449221d2e3SAl Viro if (iov_iter_is_bvec(i)) 12459221d2e3SAl Viro return iov_iter_alignment_bvec(i); 12469221d2e3SAl Viro 12479221d2e3SAl Viro if (iov_iter_is_pipe(i)) { 12489221d2e3SAl Viro size_t size = i->count; 1249e0ff126eSJan Kara 125010f525a8SAl Viro if (size && i->last_offset > 0) 125110f525a8SAl Viro return size | i->last_offset; 1252241699cdSAl Viro return size; 1253241699cdSAl Viro } 12549221d2e3SAl Viro 12559221d2e3SAl Viro if (iov_iter_is_xarray(i)) 12563d14ec1fSDavid Howells return (i->xarray_start + i->iov_offset) | i->count; 12579221d2e3SAl Viro 12589221d2e3SAl Viro return 0; 1259d879cb83SAl Viro } 1260d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment); 1261d879cb83SAl Viro 1262357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i) 1263357f435dSAl Viro { 1264357f435dSAl Viro unsigned long res = 0; 1265610c7a71SAl Viro unsigned long v = 0; 1266357f435dSAl Viro size_t size = i->count; 1267610c7a71SAl Viro unsigned k; 1268357f435dSAl Viro 1269fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1270fcb14cb1SAl Viro return 0; 1271fcb14cb1SAl Viro 1272610c7a71SAl Viro if (WARN_ON(!iter_is_iovec(i))) 1273241699cdSAl Viro return ~0U; 1274241699cdSAl Viro 1275610c7a71SAl Viro for (k = 0; k < i->nr_segs; k++) { 1276610c7a71SAl Viro if (i->iov[k].iov_len) { 1277610c7a71SAl Viro unsigned long base = (unsigned long)i->iov[k].iov_base; 1278610c7a71SAl Viro if (v) // if not the first one 1279610c7a71SAl Viro res |= base | v; // this start | previous end 1280610c7a71SAl Viro v = base + i->iov[k].iov_len; 1281610c7a71SAl Viro if (size <= i->iov[k].iov_len) 1282610c7a71SAl Viro break; 1283610c7a71SAl Viro size -= i->iov[k].iov_len; 1284610c7a71SAl Viro } 1285610c7a71SAl Viro } 1286357f435dSAl Viro return res; 1287357f435dSAl Viro } 1288357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment); 1289357f435dSAl Viro 12903cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size, 12913cf42da3SAl Viro size_t start, unsigned int maxpages) 1292acbdeb83SAl Viro { 12933cf42da3SAl Viro unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE); 12943cf42da3SAl Viro 12953cf42da3SAl Viro if (count > maxpages) 12963cf42da3SAl Viro count = maxpages; 12973cf42da3SAl Viro WARN_ON(!count); // caller should've prevented that 12983cf42da3SAl Viro if (!*res) { 12993cf42da3SAl Viro *res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL); 13003cf42da3SAl Viro if (!*res) 13013cf42da3SAl Viro return 0; 13023cf42da3SAl Viro } 13033cf42da3SAl Viro return count; 1304acbdeb83SAl Viro } 1305acbdeb83SAl Viro 130685200084SAl Viro static ssize_t pipe_get_pages(struct iov_iter *i, 130785200084SAl Viro struct page ***pages, size_t maxsize, unsigned maxpages, 130885200084SAl Viro size_t *start) 1309241699cdSAl Viro { 1310746de1f8SAl Viro unsigned int npages, count, off, chunk; 131185200084SAl Viro struct page **p; 1312746de1f8SAl Viro size_t left; 1313241699cdSAl Viro 131485200084SAl Viro if (!sanity(i)) 131585200084SAl Viro return -EFAULT; 131685200084SAl Viro 131785200084SAl Viro *start = off = pipe_npages(i, &npages); 13183cf42da3SAl Viro if (!npages) 13193cf42da3SAl Viro return -EFAULT; 13203cf42da3SAl Viro count = want_pages_array(pages, maxsize, off, min(npages, maxpages)); 13213cf42da3SAl Viro if (!count) 132285200084SAl Viro return -ENOMEM; 13233cf42da3SAl Viro p = *pages; 1324746de1f8SAl Viro for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) { 1325746de1f8SAl Viro struct page *page = append_pipe(i, left, &off); 1326e3b42964SAl Viro if (!page) 1327e3b42964SAl Viro break; 1328746de1f8SAl Viro chunk = min_t(size_t, left, PAGE_SIZE - off); 132985200084SAl Viro get_page(*p++ = page); 1330e3b42964SAl Viro } 133185200084SAl Viro if (!npages) 1332241699cdSAl Viro return -EFAULT; 1333746de1f8SAl Viro return maxsize - left; 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, 136468fe506fSAl Viro struct page ***pages, size_t maxsize, 13657ff50620SDavid Howells unsigned maxpages, size_t *_start_offset) 13667ff50620SDavid Howells { 13673cf42da3SAl Viro unsigned nr, offset, count; 13683cf42da3SAl Viro pgoff_t index; 13697ff50620SDavid Howells loff_t pos; 13707ff50620SDavid Howells 13717ff50620SDavid Howells pos = i->xarray_start + i->iov_offset; 13727ff50620SDavid Howells index = pos >> PAGE_SHIFT; 13737ff50620SDavid Howells offset = pos & ~PAGE_MASK; 13747ff50620SDavid Howells *_start_offset = offset; 13757ff50620SDavid Howells 13763cf42da3SAl Viro count = want_pages_array(pages, maxsize, offset, maxpages); 13773cf42da3SAl Viro if (!count) 137868fe506fSAl Viro return -ENOMEM; 137968fe506fSAl Viro nr = iter_xarray_populate_pages(*pages, i->xarray, index, count); 13807ff50620SDavid Howells if (nr == 0) 13817ff50620SDavid Howells return 0; 13827ff50620SDavid Howells 1383eba2d3d7SAl Viro maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize); 1384310d9d5aSAl Viro i->iov_offset += maxsize; 1385310d9d5aSAl Viro i->count -= maxsize; 1386eba2d3d7SAl Viro return maxsize; 13877ff50620SDavid Howells } 13887ff50620SDavid Howells 1389fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */ 1390dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size) 13913d671ca6SAl Viro { 13923d671ca6SAl Viro size_t skip; 13933d671ca6SAl Viro long k; 13943d671ca6SAl Viro 1395fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1396fcb14cb1SAl Viro return (unsigned long)i->ubuf + i->iov_offset; 1397fcb14cb1SAl Viro 13983d671ca6SAl Viro for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) { 13993d671ca6SAl Viro size_t len = i->iov[k].iov_len - skip; 14003d671ca6SAl Viro 14013d671ca6SAl Viro if (unlikely(!len)) 14023d671ca6SAl Viro continue; 140359dbd7d0SAl Viro if (*size > len) 14043d671ca6SAl Viro *size = len; 1405dd45ab9dSAl Viro return (unsigned long)i->iov[k].iov_base + skip; 14063d671ca6SAl Viro } 14073d671ca6SAl Viro BUG(); // if it had been empty, we wouldn't get called 14083d671ca6SAl Viro } 14093d671ca6SAl Viro 14103d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */ 14113d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i, 141259dbd7d0SAl Viro size_t *size, size_t *start) 14133d671ca6SAl Viro { 14143d671ca6SAl Viro struct page *page; 14153d671ca6SAl Viro size_t skip = i->iov_offset, len; 14163d671ca6SAl Viro 14173d671ca6SAl Viro len = i->bvec->bv_len - skip; 141859dbd7d0SAl Viro if (*size > len) 141959dbd7d0SAl Viro *size = len; 14203d671ca6SAl Viro skip += i->bvec->bv_offset; 14213d671ca6SAl Viro page = i->bvec->bv_page + skip / PAGE_SIZE; 1422dda8e5d1SAl Viro *start = skip % PAGE_SIZE; 14233d671ca6SAl Viro return page; 14243d671ca6SAl Viro } 14253d671ca6SAl Viro 142691329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i, 1427d879cb83SAl Viro struct page ***pages, size_t maxsize, 1428d8207640SLogan Gunthorpe unsigned int maxpages, size_t *start, 1429*f62e52d1SDavid Howells iov_iter_extraction_t extraction_flags) 1430d879cb83SAl Viro { 1431*f62e52d1SDavid Howells unsigned int n, gup_flags = 0; 1432d879cb83SAl Viro 1433d879cb83SAl Viro if (maxsize > i->count) 1434d879cb83SAl Viro maxsize = i->count; 14353d671ca6SAl Viro if (!maxsize) 14363d671ca6SAl Viro return 0; 14377392ed17SAl Viro if (maxsize > MAX_RW_COUNT) 14387392ed17SAl Viro maxsize = MAX_RW_COUNT; 1439*f62e52d1SDavid Howells if (extraction_flags & ITER_ALLOW_P2PDMA) 1440*f62e52d1SDavid Howells gup_flags |= FOLL_PCI_P2PDMA; 1441d879cb83SAl Viro 1442fcb14cb1SAl Viro if (likely(user_backed_iter(i))) { 14433d671ca6SAl Viro unsigned long addr; 14443cf42da3SAl Viro int res; 14459ea9ce04SDavid Howells 14463337ab08SAndreas Gruenbacher if (iov_iter_rw(i) != WRITE) 14473337ab08SAndreas Gruenbacher gup_flags |= FOLL_WRITE; 14483337ab08SAndreas Gruenbacher if (i->nofault) 14493337ab08SAndreas Gruenbacher gup_flags |= FOLL_NOFAULT; 14503337ab08SAndreas Gruenbacher 1451dd45ab9dSAl Viro addr = first_iovec_segment(i, &maxsize); 1452dd45ab9dSAl Viro *start = addr % PAGE_SIZE; 1453dd45ab9dSAl Viro addr &= PAGE_MASK; 14543cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 14553cf42da3SAl Viro if (!n) 1456d879cb83SAl Viro return -ENOMEM; 1457451c0ba9SAl Viro res = get_user_pages_fast(addr, n, gup_flags, *pages); 145891329559SAl Viro if (unlikely(res <= 0)) 1459d879cb83SAl Viro return res; 1460eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start); 1461eba2d3d7SAl Viro iov_iter_advance(i, maxsize); 1462eba2d3d7SAl Viro return maxsize; 14633d671ca6SAl Viro } 14643d671ca6SAl Viro if (iov_iter_is_bvec(i)) { 1465451c0ba9SAl Viro struct page **p; 14663d671ca6SAl Viro struct page *page; 14673d671ca6SAl Viro 146859dbd7d0SAl Viro page = first_bvec_segment(i, &maxsize, start); 14693cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 14703cf42da3SAl Viro if (!n) 1471d879cb83SAl Viro return -ENOMEM; 14723cf42da3SAl Viro p = *pages; 1473dda8e5d1SAl Viro for (int k = 0; k < n; k++) 1474eba2d3d7SAl Viro get_page(p[k] = page + k); 1475eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start); 1476310d9d5aSAl Viro i->count -= maxsize; 1477310d9d5aSAl Viro i->iov_offset += maxsize; 1478310d9d5aSAl Viro if (i->iov_offset == i->bvec->bv_len) { 1479310d9d5aSAl Viro i->iov_offset = 0; 1480310d9d5aSAl Viro i->bvec++; 1481310d9d5aSAl Viro i->nr_segs--; 1482310d9d5aSAl Viro } 1483eba2d3d7SAl Viro return maxsize; 14843d671ca6SAl Viro } 14853d671ca6SAl Viro if (iov_iter_is_pipe(i)) 1486451c0ba9SAl Viro return pipe_get_pages(i, pages, maxsize, maxpages, start); 14873d671ca6SAl Viro if (iov_iter_is_xarray(i)) 1488451c0ba9SAl Viro return iter_xarray_get_pages(i, pages, maxsize, maxpages, start); 1489d879cb83SAl Viro return -EFAULT; 1490d879cb83SAl Viro } 149191329559SAl Viro 1492d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages(struct iov_iter *i, 1493451c0ba9SAl Viro struct page **pages, size_t maxsize, unsigned maxpages, 1494*f62e52d1SDavid Howells size_t *start, iov_iter_extraction_t extraction_flags) 1495451c0ba9SAl Viro { 1496451c0ba9SAl Viro if (!maxpages) 1497451c0ba9SAl Viro return 0; 1498451c0ba9SAl Viro BUG_ON(!pages); 1499451c0ba9SAl Viro 1500d8207640SLogan Gunthorpe return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, 1501*f62e52d1SDavid Howells start, extraction_flags); 1502d8207640SLogan Gunthorpe } 1503d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages); 1504d8207640SLogan Gunthorpe 1505d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages, 1506d8207640SLogan Gunthorpe size_t maxsize, unsigned maxpages, size_t *start) 1507d8207640SLogan Gunthorpe { 1508d8207640SLogan Gunthorpe return iov_iter_get_pages(i, pages, maxsize, maxpages, start, 0); 1509451c0ba9SAl Viro } 1510eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2); 1511451c0ba9SAl Viro 1512d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, 151391329559SAl Viro struct page ***pages, size_t maxsize, 1514*f62e52d1SDavid Howells size_t *start, iov_iter_extraction_t extraction_flags) 151591329559SAl Viro { 151691329559SAl Viro ssize_t len; 151791329559SAl Viro 151891329559SAl Viro *pages = NULL; 151991329559SAl Viro 1520d8207640SLogan Gunthorpe len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start, 1521*f62e52d1SDavid Howells extraction_flags); 152291329559SAl Viro if (len <= 0) { 152391329559SAl Viro kvfree(*pages); 152491329559SAl Viro *pages = NULL; 152591329559SAl Viro } 152691329559SAl Viro return len; 152791329559SAl Viro } 1528d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages_alloc); 1529d8207640SLogan Gunthorpe 1530d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, 1531d8207640SLogan Gunthorpe struct page ***pages, size_t maxsize, size_t *start) 1532d8207640SLogan Gunthorpe { 1533d8207640SLogan Gunthorpe return iov_iter_get_pages_alloc(i, pages, maxsize, start, 0); 1534d8207640SLogan Gunthorpe } 1535eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2); 1536d879cb83SAl Viro 1537d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, 1538d879cb83SAl Viro struct iov_iter *i) 1539d879cb83SAl Viro { 1540d879cb83SAl Viro __wsum sum, next; 1541d879cb83SAl Viro sum = *csum; 1542a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 1543241699cdSAl Viro return 0; 1544a41dad90SAl Viro 15457baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, ({ 15467baa5099SAl Viro next = csum_and_copy_from_user(base, addr + off, len); 1547d879cb83SAl Viro sum = csum_block_add(sum, next, off); 15487baa5099SAl Viro next ? 0 : len; 1549d879cb83SAl Viro }), ({ 15507baa5099SAl Viro sum = csum_and_memcpy(addr + off, base, len, sum, off); 1551d879cb83SAl Viro }) 1552d879cb83SAl Viro ) 1553d879cb83SAl Viro *csum = sum; 1554d879cb83SAl Viro return bytes; 1555d879cb83SAl Viro } 1556d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter); 1557d879cb83SAl Viro 155852cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate, 1559d879cb83SAl Viro struct iov_iter *i) 1560d879cb83SAl Viro { 156152cbd23aSWillem de Bruijn struct csum_state *csstate = _csstate; 1562d879cb83SAl Viro __wsum sum, next; 156378e1f386SAl Viro 1564a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 1565a41dad90SAl Viro return 0; 156678e1f386SAl Viro if (unlikely(iov_iter_is_discard(i))) { 1567c67f1fd2SAl Viro // can't use csum_memcpy() for that one - data is not copied 1568c67f1fd2SAl Viro csstate->csum = csum_block_add(csstate->csum, 1569c67f1fd2SAl Viro csum_partial(addr, bytes, 0), 1570c67f1fd2SAl Viro csstate->off); 1571c67f1fd2SAl Viro csstate->off += bytes; 1572c67f1fd2SAl Viro return bytes; 1573241699cdSAl Viro } 15746852df12SAl Viro 15756852df12SAl Viro sum = csum_shift(csstate->csum, csstate->off); 15766852df12SAl Viro if (unlikely(iov_iter_is_pipe(i))) 15776852df12SAl Viro bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum); 15786852df12SAl Viro else iterate_and_advance(i, bytes, base, len, off, ({ 15797baa5099SAl Viro next = csum_and_copy_to_user(addr + off, base, len); 1580d879cb83SAl Viro sum = csum_block_add(sum, next, off); 15817baa5099SAl Viro next ? 0 : len; 1582d879cb83SAl Viro }), ({ 15837baa5099SAl Viro sum = csum_and_memcpy(base, addr + off, len, sum, off); 1584d879cb83SAl Viro }) 1585d879cb83SAl Viro ) 1586594e450bSAl Viro csstate->csum = csum_shift(sum, csstate->off); 1587594e450bSAl Viro csstate->off += bytes; 1588d879cb83SAl Viro return bytes; 1589d879cb83SAl Viro } 1590d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter); 1591d879cb83SAl Viro 1592d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, 1593d05f4435SSagi Grimberg struct iov_iter *i) 1594d05f4435SSagi Grimberg { 15957999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH 1596d05f4435SSagi Grimberg struct ahash_request *hash = hashp; 1597d05f4435SSagi Grimberg struct scatterlist sg; 1598d05f4435SSagi Grimberg size_t copied; 1599d05f4435SSagi Grimberg 1600d05f4435SSagi Grimberg copied = copy_to_iter(addr, bytes, i); 1601d05f4435SSagi Grimberg sg_init_one(&sg, addr, copied); 1602d05f4435SSagi Grimberg ahash_request_set_crypt(hash, &sg, NULL, copied); 1603d05f4435SSagi Grimberg crypto_ahash_update(hash); 1604d05f4435SSagi Grimberg return copied; 160527fad74aSYueHaibing #else 160627fad74aSYueHaibing return 0; 160727fad74aSYueHaibing #endif 1608d05f4435SSagi Grimberg } 1609d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter); 1610d05f4435SSagi Grimberg 161166531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages) 1612d879cb83SAl Viro { 161366531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 161466531c65SAl Viro const struct iovec *p; 1615d879cb83SAl Viro int npages = 0; 1616d879cb83SAl Viro 161766531c65SAl Viro for (p = i->iov; size; skip = 0, p++) { 161866531c65SAl Viro unsigned offs = offset_in_page(p->iov_base + skip); 161966531c65SAl Viro size_t len = min(p->iov_len - skip, size); 1620d879cb83SAl Viro 162166531c65SAl Viro if (len) { 162266531c65SAl Viro size -= len; 162366531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 162466531c65SAl Viro if (unlikely(npages > maxpages)) 162566531c65SAl Viro return maxpages; 162666531c65SAl Viro } 162766531c65SAl Viro } 162866531c65SAl Viro return npages; 162966531c65SAl Viro } 163066531c65SAl Viro 163166531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages) 163266531c65SAl Viro { 163366531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 163466531c65SAl Viro const struct bio_vec *p; 163566531c65SAl Viro int npages = 0; 163666531c65SAl Viro 163766531c65SAl Viro for (p = i->bvec; size; skip = 0, p++) { 163866531c65SAl Viro unsigned offs = (p->bv_offset + skip) % PAGE_SIZE; 163966531c65SAl Viro size_t len = min(p->bv_len - skip, size); 164066531c65SAl Viro 164166531c65SAl Viro size -= len; 164266531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 164366531c65SAl Viro if (unlikely(npages > maxpages)) 164466531c65SAl Viro return maxpages; 164566531c65SAl Viro } 164666531c65SAl Viro return npages; 164766531c65SAl Viro } 164866531c65SAl Viro 164966531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages) 165066531c65SAl Viro { 165166531c65SAl Viro if (unlikely(!i->count)) 165266531c65SAl Viro return 0; 1653fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1654fcb14cb1SAl Viro unsigned offs = offset_in_page(i->ubuf + i->iov_offset); 1655fcb14cb1SAl Viro int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE); 1656fcb14cb1SAl Viro return min(npages, maxpages); 1657fcb14cb1SAl Viro } 165866531c65SAl Viro /* iovec and kvec have identical layouts */ 165966531c65SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 166066531c65SAl Viro return iov_npages(i, maxpages); 166166531c65SAl Viro if (iov_iter_is_bvec(i)) 166266531c65SAl Viro return bvec_npages(i, maxpages); 166366531c65SAl Viro if (iov_iter_is_pipe(i)) { 166466531c65SAl Viro int npages; 1665241699cdSAl Viro 1666241699cdSAl Viro if (!sanity(i)) 1667241699cdSAl Viro return 0; 1668241699cdSAl Viro 166912d426abSAl Viro pipe_npages(i, &npages); 167066531c65SAl Viro return min(npages, maxpages); 167166531c65SAl Viro } 167266531c65SAl Viro if (iov_iter_is_xarray(i)) { 1673e4f8df86SAl Viro unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE; 1674e4f8df86SAl Viro int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE); 167566531c65SAl Viro return min(npages, maxpages); 167666531c65SAl Viro } 167766531c65SAl Viro return 0; 1678d879cb83SAl Viro } 1679d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages); 1680d879cb83SAl Viro 1681d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags) 1682d879cb83SAl Viro { 1683d879cb83SAl Viro *new = *old; 168400e23707SDavid Howells if (unlikely(iov_iter_is_pipe(new))) { 1685241699cdSAl Viro WARN_ON(1); 1686241699cdSAl Viro return NULL; 1687241699cdSAl Viro } 168800e23707SDavid Howells if (iov_iter_is_bvec(new)) 1689d879cb83SAl Viro return new->bvec = kmemdup(new->bvec, 1690d879cb83SAl Viro new->nr_segs * sizeof(struct bio_vec), 1691d879cb83SAl Viro flags); 1692fcb14cb1SAl Viro else if (iov_iter_is_kvec(new) || iter_is_iovec(new)) 1693d879cb83SAl Viro /* iovec and kvec have identical layout */ 1694d879cb83SAl Viro return new->iov = kmemdup(new->iov, 1695d879cb83SAl Viro new->nr_segs * sizeof(struct iovec), 1696d879cb83SAl Viro flags); 1697fcb14cb1SAl Viro return NULL; 1698d879cb83SAl Viro } 1699d879cb83SAl Viro EXPORT_SYMBOL(dup_iter); 1700bc917be8SAl Viro 1701bfdc5970SChristoph Hellwig static int copy_compat_iovec_from_user(struct iovec *iov, 1702bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1703bfdc5970SChristoph Hellwig { 1704bfdc5970SChristoph Hellwig const struct compat_iovec __user *uiov = 1705bfdc5970SChristoph Hellwig (const struct compat_iovec __user *)uvec; 1706bfdc5970SChristoph Hellwig int ret = -EFAULT, i; 1707bfdc5970SChristoph Hellwig 1708a959a978SChristoph Hellwig if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) 1709bfdc5970SChristoph Hellwig return -EFAULT; 1710bfdc5970SChristoph Hellwig 1711bfdc5970SChristoph Hellwig for (i = 0; i < nr_segs; i++) { 1712bfdc5970SChristoph Hellwig compat_uptr_t buf; 1713bfdc5970SChristoph Hellwig compat_ssize_t len; 1714bfdc5970SChristoph Hellwig 1715bfdc5970SChristoph Hellwig unsafe_get_user(len, &uiov[i].iov_len, uaccess_end); 1716bfdc5970SChristoph Hellwig unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end); 1717bfdc5970SChristoph Hellwig 1718bfdc5970SChristoph Hellwig /* check for compat_size_t not fitting in compat_ssize_t .. */ 1719bfdc5970SChristoph Hellwig if (len < 0) { 1720bfdc5970SChristoph Hellwig ret = -EINVAL; 1721bfdc5970SChristoph Hellwig goto uaccess_end; 1722bfdc5970SChristoph Hellwig } 1723bfdc5970SChristoph Hellwig iov[i].iov_base = compat_ptr(buf); 1724bfdc5970SChristoph Hellwig iov[i].iov_len = len; 1725bfdc5970SChristoph Hellwig } 1726bfdc5970SChristoph Hellwig 1727bfdc5970SChristoph Hellwig ret = 0; 1728bfdc5970SChristoph Hellwig uaccess_end: 1729bfdc5970SChristoph Hellwig user_access_end(); 1730bfdc5970SChristoph Hellwig return ret; 1731bfdc5970SChristoph Hellwig } 1732bfdc5970SChristoph Hellwig 1733bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov, 1734bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1735fb041b59SDavid Laight { 1736fb041b59SDavid Laight unsigned long seg; 1737bfdc5970SChristoph Hellwig 1738bfdc5970SChristoph Hellwig if (copy_from_user(iov, uvec, nr_segs * sizeof(*uvec))) 1739bfdc5970SChristoph Hellwig return -EFAULT; 1740bfdc5970SChristoph Hellwig for (seg = 0; seg < nr_segs; seg++) { 1741bfdc5970SChristoph Hellwig if ((ssize_t)iov[seg].iov_len < 0) 1742bfdc5970SChristoph Hellwig return -EINVAL; 1743bfdc5970SChristoph Hellwig } 1744bfdc5970SChristoph Hellwig 1745bfdc5970SChristoph Hellwig return 0; 1746bfdc5970SChristoph Hellwig } 1747bfdc5970SChristoph Hellwig 1748bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec, 1749bfdc5970SChristoph Hellwig unsigned long nr_segs, unsigned long fast_segs, 1750bfdc5970SChristoph Hellwig struct iovec *fast_iov, bool compat) 1751bfdc5970SChristoph Hellwig { 1752bfdc5970SChristoph Hellwig struct iovec *iov = fast_iov; 1753bfdc5970SChristoph Hellwig int ret; 1754fb041b59SDavid Laight 1755fb041b59SDavid Laight /* 1756bfdc5970SChristoph Hellwig * SuS says "The readv() function *may* fail if the iovcnt argument was 1757bfdc5970SChristoph Hellwig * less than or equal to 0, or greater than {IOV_MAX}. Linux has 1758fb041b59SDavid Laight * traditionally returned zero for zero segments, so... 1759fb041b59SDavid Laight */ 1760bfdc5970SChristoph Hellwig if (nr_segs == 0) 1761bfdc5970SChristoph Hellwig return iov; 1762bfdc5970SChristoph Hellwig if (nr_segs > UIO_MAXIOV) 1763bfdc5970SChristoph Hellwig return ERR_PTR(-EINVAL); 1764fb041b59SDavid Laight if (nr_segs > fast_segs) { 1765fb041b59SDavid Laight iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); 1766bfdc5970SChristoph Hellwig if (!iov) 1767bfdc5970SChristoph Hellwig return ERR_PTR(-ENOMEM); 1768fb041b59SDavid Laight } 1769bfdc5970SChristoph Hellwig 1770bfdc5970SChristoph Hellwig if (compat) 1771bfdc5970SChristoph Hellwig ret = copy_compat_iovec_from_user(iov, uvec, nr_segs); 1772bfdc5970SChristoph Hellwig else 1773bfdc5970SChristoph Hellwig ret = copy_iovec_from_user(iov, uvec, nr_segs); 1774bfdc5970SChristoph Hellwig if (ret) { 1775bfdc5970SChristoph Hellwig if (iov != fast_iov) 1776bfdc5970SChristoph Hellwig kfree(iov); 1777bfdc5970SChristoph Hellwig return ERR_PTR(ret); 1778fb041b59SDavid Laight } 1779bfdc5970SChristoph Hellwig 1780bfdc5970SChristoph Hellwig return iov; 1781bfdc5970SChristoph Hellwig } 1782bfdc5970SChristoph Hellwig 1783bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec, 1784bfdc5970SChristoph Hellwig unsigned nr_segs, unsigned fast_segs, struct iovec **iovp, 1785bfdc5970SChristoph Hellwig struct iov_iter *i, bool compat) 1786bfdc5970SChristoph Hellwig { 1787bfdc5970SChristoph Hellwig ssize_t total_len = 0; 1788bfdc5970SChristoph Hellwig unsigned long seg; 1789bfdc5970SChristoph Hellwig struct iovec *iov; 1790bfdc5970SChristoph Hellwig 1791bfdc5970SChristoph Hellwig iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat); 1792bfdc5970SChristoph Hellwig if (IS_ERR(iov)) { 1793bfdc5970SChristoph Hellwig *iovp = NULL; 1794bfdc5970SChristoph Hellwig return PTR_ERR(iov); 1795fb041b59SDavid Laight } 1796fb041b59SDavid Laight 1797fb041b59SDavid Laight /* 1798bfdc5970SChristoph Hellwig * According to the Single Unix Specification we should return EINVAL if 1799bfdc5970SChristoph Hellwig * an element length is < 0 when cast to ssize_t or if the total length 1800bfdc5970SChristoph Hellwig * would overflow the ssize_t return value of the system call. 1801fb041b59SDavid Laight * 1802fb041b59SDavid Laight * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the 1803fb041b59SDavid Laight * overflow case. 1804fb041b59SDavid Laight */ 1805fb041b59SDavid Laight for (seg = 0; seg < nr_segs; seg++) { 1806fb041b59SDavid Laight ssize_t len = (ssize_t)iov[seg].iov_len; 1807fb041b59SDavid Laight 1808bfdc5970SChristoph Hellwig if (!access_ok(iov[seg].iov_base, len)) { 1809bfdc5970SChristoph Hellwig if (iov != *iovp) 1810bfdc5970SChristoph Hellwig kfree(iov); 1811bfdc5970SChristoph Hellwig *iovp = NULL; 1812bfdc5970SChristoph Hellwig return -EFAULT; 1813fb041b59SDavid Laight } 1814bfdc5970SChristoph Hellwig 1815bfdc5970SChristoph Hellwig if (len > MAX_RW_COUNT - total_len) { 1816bfdc5970SChristoph Hellwig len = MAX_RW_COUNT - total_len; 1817fb041b59SDavid Laight iov[seg].iov_len = len; 1818fb041b59SDavid Laight } 1819bfdc5970SChristoph Hellwig total_len += len; 1820fb041b59SDavid Laight } 1821bfdc5970SChristoph Hellwig 1822bfdc5970SChristoph Hellwig iov_iter_init(i, type, iov, nr_segs, total_len); 1823bfdc5970SChristoph Hellwig if (iov == *iovp) 1824bfdc5970SChristoph Hellwig *iovp = NULL; 1825bfdc5970SChristoph Hellwig else 1826bfdc5970SChristoph Hellwig *iovp = iov; 1827bfdc5970SChristoph Hellwig return total_len; 1828fb041b59SDavid Laight } 1829fb041b59SDavid Laight 1830ffecee4fSVegard Nossum /** 1831ffecee4fSVegard Nossum * import_iovec() - Copy an array of &struct iovec from userspace 1832ffecee4fSVegard Nossum * into the kernel, check that it is valid, and initialize a new 1833ffecee4fSVegard Nossum * &struct iov_iter iterator to access it. 1834ffecee4fSVegard Nossum * 1835ffecee4fSVegard Nossum * @type: One of %READ or %WRITE. 1836bfdc5970SChristoph Hellwig * @uvec: Pointer to the userspace array. 1837ffecee4fSVegard Nossum * @nr_segs: Number of elements in userspace array. 1838ffecee4fSVegard Nossum * @fast_segs: Number of elements in @iov. 1839bfdc5970SChristoph Hellwig * @iovp: (input and output parameter) Pointer to pointer to (usually small 1840ffecee4fSVegard Nossum * on-stack) kernel array. 1841ffecee4fSVegard Nossum * @i: Pointer to iterator that will be initialized on success. 1842ffecee4fSVegard Nossum * 1843ffecee4fSVegard Nossum * If the array pointed to by *@iov is large enough to hold all @nr_segs, 1844ffecee4fSVegard Nossum * then this function places %NULL in *@iov on return. Otherwise, a new 1845ffecee4fSVegard Nossum * array will be allocated and the result placed in *@iov. This means that 1846ffecee4fSVegard Nossum * the caller may call kfree() on *@iov regardless of whether the small 1847ffecee4fSVegard Nossum * on-stack array was used or not (and regardless of whether this function 1848ffecee4fSVegard Nossum * returns an error or not). 1849ffecee4fSVegard Nossum * 185087e5e6daSJens Axboe * Return: Negative error code on error, bytes imported on success 1851ffecee4fSVegard Nossum */ 1852bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec, 1853bc917be8SAl Viro unsigned nr_segs, unsigned fast_segs, 1854bfdc5970SChristoph Hellwig struct iovec **iovp, struct iov_iter *i) 1855bc917be8SAl Viro { 185689cd35c5SChristoph Hellwig return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i, 185789cd35c5SChristoph Hellwig in_compat_syscall()); 1858bc917be8SAl Viro } 1859bc917be8SAl Viro EXPORT_SYMBOL(import_iovec); 1860bc917be8SAl Viro 1861bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len, 1862bc917be8SAl Viro struct iovec *iov, struct iov_iter *i) 1863bc917be8SAl Viro { 1864bc917be8SAl Viro if (len > MAX_RW_COUNT) 1865bc917be8SAl Viro len = MAX_RW_COUNT; 186696d4f267SLinus Torvalds if (unlikely(!access_ok(buf, len))) 1867bc917be8SAl Viro return -EFAULT; 1868bc917be8SAl Viro 1869bc917be8SAl Viro iov->iov_base = buf; 1870bc917be8SAl Viro iov->iov_len = len; 1871bc917be8SAl Viro iov_iter_init(i, rw, iov, 1, len); 1872bc917be8SAl Viro return 0; 1873bc917be8SAl Viro } 1874e1267585SAl Viro EXPORT_SYMBOL(import_single_range); 18758fb0f47aSJens Axboe 18768fb0f47aSJens Axboe /** 18778fb0f47aSJens Axboe * iov_iter_restore() - Restore a &struct iov_iter to the same state as when 18788fb0f47aSJens Axboe * iov_iter_save_state() was called. 18798fb0f47aSJens Axboe * 18808fb0f47aSJens Axboe * @i: &struct iov_iter to restore 18818fb0f47aSJens Axboe * @state: state to restore from 18828fb0f47aSJens Axboe * 18838fb0f47aSJens Axboe * Used after iov_iter_save_state() to bring restore @i, if operations may 18848fb0f47aSJens Axboe * have advanced it. 18858fb0f47aSJens Axboe * 18868fb0f47aSJens Axboe * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC 18878fb0f47aSJens Axboe */ 18888fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) 18898fb0f47aSJens Axboe { 18908fb0f47aSJens Axboe if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i)) && 1891fcb14cb1SAl Viro !iov_iter_is_kvec(i) && !iter_is_ubuf(i)) 18928fb0f47aSJens Axboe return; 18938fb0f47aSJens Axboe i->iov_offset = state->iov_offset; 18948fb0f47aSJens Axboe i->count = state->count; 1895fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1896fcb14cb1SAl Viro return; 18978fb0f47aSJens Axboe /* 18988fb0f47aSJens Axboe * For the *vec iters, nr_segs + iov is constant - if we increment 18998fb0f47aSJens Axboe * the vec, then we also decrement the nr_segs count. Hence we don't 19008fb0f47aSJens Axboe * need to track both of these, just one is enough and we can deduct 19018fb0f47aSJens Axboe * the other from that. ITER_KVEC and ITER_IOVEC are the same struct 19028fb0f47aSJens Axboe * size, so we can just increment the iov pointer as they are unionzed. 19038fb0f47aSJens Axboe * ITER_BVEC _may_ be the same size on some archs, but on others it is 19048fb0f47aSJens Axboe * not. Be safe and handle it separately. 19058fb0f47aSJens Axboe */ 19068fb0f47aSJens Axboe BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec)); 19078fb0f47aSJens Axboe if (iov_iter_is_bvec(i)) 19088fb0f47aSJens Axboe i->bvec -= state->nr_segs - i->nr_segs; 19098fb0f47aSJens Axboe else 19108fb0f47aSJens Axboe i->iov -= state->nr_segs - i->nr_segs; 19118fb0f47aSJens Axboe i->nr_segs = state->nr_segs; 19128fb0f47aSJens Axboe } 1913