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))) { \ 129de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i); \ 1307baa5099SAl Viro void __user *base; \ 1317baa5099SAl Viro size_t len; \ 1327baa5099SAl Viro iterate_iovec(i, n, base, len, off, \ 133a6e4ec7bSAl Viro iov, (I)) \ 134de4f5fedSJens Axboe i->nr_segs -= iov - iter_iov(i); \ 135de4f5fedSJens Axboe 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 1754f80818bSLorenzo Stoakes static int copyout_nofault(void __user *to, const void *from, size_t n) 1764f80818bSLorenzo Stoakes { 1774f80818bSLorenzo Stoakes long res; 1784f80818bSLorenzo Stoakes 1794f80818bSLorenzo Stoakes if (should_fail_usercopy()) 1804f80818bSLorenzo Stoakes return n; 1814f80818bSLorenzo Stoakes 1824f80818bSLorenzo Stoakes res = copy_to_user_nofault(to, from, n); 1834f80818bSLorenzo Stoakes 1844f80818bSLorenzo Stoakes return res < 0 ? n : res; 1854f80818bSLorenzo Stoakes } 1864f80818bSLorenzo Stoakes 18709fc68dcSAl Viro static int copyin(void *to, const void __user *from, size_t n) 18809fc68dcSAl Viro { 18933b75c1dSAlexander Potapenko size_t res = n; 19033b75c1dSAlexander Potapenko 1914d0e9df5SAlbert van der Linde if (should_fail_usercopy()) 1924d0e9df5SAlbert van der Linde return n; 19396d4f267SLinus Torvalds if (access_ok(from, n)) { 19433b75c1dSAlexander Potapenko instrument_copy_from_user_before(to, from, n); 19533b75c1dSAlexander Potapenko res = raw_copy_from_user(to, from, n); 19633b75c1dSAlexander Potapenko instrument_copy_from_user_after(to, from, n, res); 19709fc68dcSAl Viro } 19833b75c1dSAlexander Potapenko return res; 19909fc68dcSAl Viro } 20009fc68dcSAl Viro 201241699cdSAl Viro #ifdef PIPE_PARANOIA 202241699cdSAl Viro static bool sanity(const struct iov_iter *i) 203241699cdSAl Viro { 204241699cdSAl Viro struct pipe_inode_info *pipe = i->pipe; 2058cefc107SDavid Howells unsigned int p_head = pipe->head; 2068cefc107SDavid Howells unsigned int p_tail = pipe->tail; 2078cefc107SDavid Howells unsigned int p_occupancy = pipe_occupancy(p_head, p_tail); 2088cefc107SDavid Howells unsigned int i_head = i->head; 2098cefc107SDavid Howells unsigned int idx; 2108cefc107SDavid Howells 21110f525a8SAl Viro if (i->last_offset) { 212241699cdSAl Viro struct pipe_buffer *p; 2138cefc107SDavid Howells if (unlikely(p_occupancy == 0)) 214241699cdSAl Viro goto Bad; // pipe must be non-empty 2158cefc107SDavid Howells if (unlikely(i_head != p_head - 1)) 216241699cdSAl Viro goto Bad; // must be at the last buffer... 217241699cdSAl Viro 2182dcedb2aSAl Viro p = pipe_buf(pipe, i_head); 21910f525a8SAl Viro if (unlikely(p->offset + p->len != abs(i->last_offset))) 220241699cdSAl Viro goto Bad; // ... at the end of segment 221241699cdSAl Viro } else { 2228cefc107SDavid Howells if (i_head != p_head) 223241699cdSAl Viro goto Bad; // must be right after the last buffer 224241699cdSAl Viro } 225241699cdSAl Viro return true; 226241699cdSAl Viro Bad: 22710f525a8SAl Viro printk(KERN_ERR "idx = %d, offset = %d\n", i_head, i->last_offset); 2288cefc107SDavid Howells printk(KERN_ERR "head = %d, tail = %d, buffers = %d\n", 2298cefc107SDavid Howells p_head, p_tail, pipe->ring_size); 2308cefc107SDavid Howells for (idx = 0; idx < pipe->ring_size; idx++) 231241699cdSAl Viro printk(KERN_ERR "[%p %p %d %d]\n", 232241699cdSAl Viro pipe->bufs[idx].ops, 233241699cdSAl Viro pipe->bufs[idx].page, 234241699cdSAl Viro pipe->bufs[idx].offset, 235241699cdSAl Viro pipe->bufs[idx].len); 236241699cdSAl Viro WARN_ON(1); 237241699cdSAl Viro return false; 238241699cdSAl Viro } 239241699cdSAl Viro #else 240241699cdSAl Viro #define sanity(i) true 241241699cdSAl Viro #endif 242241699cdSAl Viro 24347b7fcaeSAl Viro static struct page *push_anon(struct pipe_inode_info *pipe, unsigned size) 24447b7fcaeSAl Viro { 24547b7fcaeSAl Viro struct page *page = alloc_page(GFP_USER); 24647b7fcaeSAl Viro if (page) { 24747b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++); 24847b7fcaeSAl Viro *buf = (struct pipe_buffer) { 24947b7fcaeSAl Viro .ops = &default_pipe_buf_ops, 25047b7fcaeSAl Viro .page = page, 25147b7fcaeSAl Viro .offset = 0, 25247b7fcaeSAl Viro .len = size 25347b7fcaeSAl Viro }; 25447b7fcaeSAl Viro } 25547b7fcaeSAl Viro return page; 25647b7fcaeSAl Viro } 25747b7fcaeSAl Viro 25847b7fcaeSAl Viro static void push_page(struct pipe_inode_info *pipe, struct page *page, 25947b7fcaeSAl Viro unsigned int offset, unsigned int size) 26047b7fcaeSAl Viro { 26147b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, pipe->head++); 26247b7fcaeSAl Viro *buf = (struct pipe_buffer) { 26347b7fcaeSAl Viro .ops = &page_cache_pipe_buf_ops, 26447b7fcaeSAl Viro .page = page, 26547b7fcaeSAl Viro .offset = offset, 26647b7fcaeSAl Viro .len = size 26747b7fcaeSAl Viro }; 26847b7fcaeSAl Viro get_page(page); 26947b7fcaeSAl Viro } 27047b7fcaeSAl Viro 27110f525a8SAl Viro static inline int last_offset(const struct pipe_buffer *buf) 2728fad7767SAl Viro { 27310f525a8SAl Viro if (buf->ops == &default_pipe_buf_ops) 27410f525a8SAl Viro return buf->len; // buf->offset is 0 for those 27510f525a8SAl Viro else 27610f525a8SAl Viro return -(buf->offset + buf->len); 2778fad7767SAl Viro } 2788fad7767SAl Viro 2798fad7767SAl Viro static struct page *append_pipe(struct iov_iter *i, size_t size, 2808fad7767SAl Viro unsigned int *off) 2818fad7767SAl Viro { 2828fad7767SAl Viro struct pipe_inode_info *pipe = i->pipe; 28310f525a8SAl Viro int offset = i->last_offset; 2848fad7767SAl Viro struct pipe_buffer *buf; 2858fad7767SAl Viro struct page *page; 2868fad7767SAl Viro 28710f525a8SAl Viro if (offset > 0 && offset < PAGE_SIZE) { 28810f525a8SAl Viro // some space in the last buffer; add to it 2898fad7767SAl Viro buf = pipe_buf(pipe, pipe->head - 1); 2908fad7767SAl Viro size = min_t(size_t, size, PAGE_SIZE - offset); 2918fad7767SAl Viro buf->len += size; 29210f525a8SAl Viro i->last_offset += size; 2938fad7767SAl Viro i->count -= size; 2948fad7767SAl Viro *off = offset; 2958fad7767SAl Viro return buf->page; 2968fad7767SAl Viro } 2978fad7767SAl Viro // OK, we need a new buffer 2988fad7767SAl Viro *off = 0; 2998fad7767SAl Viro size = min_t(size_t, size, PAGE_SIZE); 3008fad7767SAl Viro if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 3018fad7767SAl Viro return NULL; 3028fad7767SAl Viro page = push_anon(pipe, size); 3038fad7767SAl Viro if (!page) 3048fad7767SAl Viro return NULL; 3058fad7767SAl Viro i->head = pipe->head - 1; 30610f525a8SAl Viro i->last_offset = size; 3078fad7767SAl Viro i->count -= size; 3088fad7767SAl Viro return page; 3098fad7767SAl Viro } 3108fad7767SAl Viro 311241699cdSAl Viro static size_t copy_page_to_iter_pipe(struct page *page, size_t offset, size_t bytes, 312241699cdSAl Viro struct iov_iter *i) 313241699cdSAl Viro { 314241699cdSAl Viro struct pipe_inode_info *pipe = i->pipe; 31547b7fcaeSAl Viro unsigned int head = pipe->head; 316241699cdSAl Viro 317241699cdSAl Viro if (unlikely(bytes > i->count)) 318241699cdSAl Viro bytes = i->count; 319241699cdSAl Viro 320241699cdSAl Viro if (unlikely(!bytes)) 321241699cdSAl Viro return 0; 322241699cdSAl Viro 323241699cdSAl Viro if (!sanity(i)) 324241699cdSAl Viro return 0; 325241699cdSAl Viro 32610f525a8SAl Viro if (offset && i->last_offset == -offset) { // could we merge it? 32747b7fcaeSAl Viro struct pipe_buffer *buf = pipe_buf(pipe, head - 1); 32847b7fcaeSAl Viro if (buf->page == page) { 329241699cdSAl Viro buf->len += bytes; 33010f525a8SAl Viro i->last_offset -= bytes; 33147b7fcaeSAl Viro i->count -= bytes; 33247b7fcaeSAl Viro return bytes; 333241699cdSAl Viro } 334241699cdSAl Viro } 33547b7fcaeSAl Viro if (pipe_full(pipe->head, pipe->tail, pipe->max_usage)) 336241699cdSAl Viro return 0; 3378cefc107SDavid Howells 33847b7fcaeSAl Viro push_page(pipe, page, offset, bytes); 33910f525a8SAl Viro i->last_offset = -(offset + bytes); 34047b7fcaeSAl Viro i->head = head; 341241699cdSAl Viro i->count -= bytes; 342241699cdSAl Viro return bytes; 343241699cdSAl Viro } 344241699cdSAl Viro 345d879cb83SAl Viro /* 346a6294593SAndreas Gruenbacher * fault_in_iov_iter_readable - fault in iov iterator for reading 347a6294593SAndreas Gruenbacher * @i: iterator 348a6294593SAndreas Gruenbacher * @size: maximum length 349171a0203SAnton Altaparmakov * 350a6294593SAndreas Gruenbacher * Fault in one or more iovecs of the given iov_iter, to a maximum length of 351a6294593SAndreas Gruenbacher * @size. For each iovec, fault in each page that constitutes the iovec. 352a6294593SAndreas Gruenbacher * 353a6294593SAndreas Gruenbacher * Returns the number of bytes not faulted in (like copy_to_user() and 354a6294593SAndreas Gruenbacher * copy_from_user()). 355a6294593SAndreas Gruenbacher * 356a6294593SAndreas Gruenbacher * Always returns 0 for non-userspace iterators. 357171a0203SAnton Altaparmakov */ 358a6294593SAndreas Gruenbacher size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t size) 359171a0203SAnton Altaparmakov { 360fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 361fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 362fcb14cb1SAl Viro n -= fault_in_readable(i->ubuf + i->iov_offset, n); 363fcb14cb1SAl Viro return size - n; 364fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 365a6294593SAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 3668409a0d2SAl Viro const struct iovec *p; 3678409a0d2SAl Viro size_t skip; 3688409a0d2SAl Viro 369a6294593SAndreas Gruenbacher size -= count; 370de4f5fedSJens Axboe for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) { 371a6294593SAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 372a6294593SAndreas Gruenbacher size_t ret; 3738409a0d2SAl Viro 3748409a0d2SAl Viro if (unlikely(!len)) 3758409a0d2SAl Viro continue; 376a6294593SAndreas Gruenbacher ret = fault_in_readable(p->iov_base + skip, len); 377a6294593SAndreas Gruenbacher count -= len - ret; 378a6294593SAndreas Gruenbacher if (ret) 379a6294593SAndreas Gruenbacher break; 3808409a0d2SAl Viro } 381a6294593SAndreas Gruenbacher return count + size; 382171a0203SAnton Altaparmakov } 383171a0203SAnton Altaparmakov return 0; 384171a0203SAnton Altaparmakov } 385a6294593SAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_readable); 386171a0203SAnton Altaparmakov 387cdd591fcSAndreas Gruenbacher /* 388cdd591fcSAndreas Gruenbacher * fault_in_iov_iter_writeable - fault in iov iterator for writing 389cdd591fcSAndreas Gruenbacher * @i: iterator 390cdd591fcSAndreas Gruenbacher * @size: maximum length 391cdd591fcSAndreas Gruenbacher * 392cdd591fcSAndreas Gruenbacher * Faults in the iterator using get_user_pages(), i.e., without triggering 393cdd591fcSAndreas Gruenbacher * hardware page faults. This is primarily useful when we already know that 394cdd591fcSAndreas Gruenbacher * some or all of the pages in @i aren't in memory. 395cdd591fcSAndreas Gruenbacher * 396cdd591fcSAndreas Gruenbacher * Returns the number of bytes not faulted in, like copy_to_user() and 397cdd591fcSAndreas Gruenbacher * copy_from_user(). 398cdd591fcSAndreas Gruenbacher * 399cdd591fcSAndreas Gruenbacher * Always returns 0 for non-user-space iterators. 400cdd591fcSAndreas Gruenbacher */ 401cdd591fcSAndreas Gruenbacher size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t size) 402cdd591fcSAndreas Gruenbacher { 403fcb14cb1SAl Viro if (iter_is_ubuf(i)) { 404fcb14cb1SAl Viro size_t n = min(size, iov_iter_count(i)); 405fcb14cb1SAl Viro n -= fault_in_safe_writeable(i->ubuf + i->iov_offset, n); 406fcb14cb1SAl Viro return size - n; 407fcb14cb1SAl Viro } else if (iter_is_iovec(i)) { 408cdd591fcSAndreas Gruenbacher size_t count = min(size, iov_iter_count(i)); 409cdd591fcSAndreas Gruenbacher const struct iovec *p; 410cdd591fcSAndreas Gruenbacher size_t skip; 411cdd591fcSAndreas Gruenbacher 412cdd591fcSAndreas Gruenbacher size -= count; 413de4f5fedSJens Axboe for (p = iter_iov(i), skip = i->iov_offset; count; p++, skip = 0) { 414cdd591fcSAndreas Gruenbacher size_t len = min(count, p->iov_len - skip); 415cdd591fcSAndreas Gruenbacher size_t ret; 416cdd591fcSAndreas Gruenbacher 417cdd591fcSAndreas Gruenbacher if (unlikely(!len)) 418cdd591fcSAndreas Gruenbacher continue; 419cdd591fcSAndreas Gruenbacher ret = fault_in_safe_writeable(p->iov_base + skip, len); 420cdd591fcSAndreas Gruenbacher count -= len - ret; 421cdd591fcSAndreas Gruenbacher if (ret) 422cdd591fcSAndreas Gruenbacher break; 423cdd591fcSAndreas Gruenbacher } 424cdd591fcSAndreas Gruenbacher return count + size; 425cdd591fcSAndreas Gruenbacher } 426cdd591fcSAndreas Gruenbacher return 0; 427cdd591fcSAndreas Gruenbacher } 428cdd591fcSAndreas Gruenbacher EXPORT_SYMBOL(fault_in_iov_iter_writeable); 429cdd591fcSAndreas Gruenbacher 430aa563d7bSDavid Howells void iov_iter_init(struct iov_iter *i, unsigned int direction, 431d879cb83SAl Viro const struct iovec *iov, unsigned long nr_segs, 432d879cb83SAl Viro size_t count) 433d879cb83SAl Viro { 434aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 4358cd54c1cSAl Viro *i = (struct iov_iter) { 4368cd54c1cSAl Viro .iter_type = ITER_IOVEC, 437*245f0922SKefeng Wang .copy_mc = false, 4383337ab08SAndreas Gruenbacher .nofault = false, 439fcb14cb1SAl Viro .user_backed = true, 4408cd54c1cSAl Viro .data_source = direction, 441de4f5fedSJens Axboe .__iov = iov, 4428cd54c1cSAl Viro .nr_segs = nr_segs, 4438cd54c1cSAl Viro .iov_offset = 0, 4448cd54c1cSAl Viro .count = count 4458cd54c1cSAl Viro }; 446d879cb83SAl Viro } 447d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_init); 448d879cb83SAl Viro 44912d426abSAl Viro // returns the offset in partial buffer (if any) 45012d426abSAl Viro static inline unsigned int pipe_npages(const struct iov_iter *i, int *npages) 451241699cdSAl Viro { 45212d426abSAl Viro struct pipe_inode_info *pipe = i->pipe; 45312d426abSAl Viro int used = pipe->head - pipe->tail; 45410f525a8SAl Viro int off = i->last_offset; 4558cefc107SDavid Howells 45612d426abSAl Viro *npages = max((int)pipe->max_usage - used, 0); 45712d426abSAl Viro 45810f525a8SAl Viro if (off > 0 && off < PAGE_SIZE) { // anon and not full 45912d426abSAl Viro (*npages)++; 46012d426abSAl Viro return off; 46110f525a8SAl Viro } 46212d426abSAl Viro return 0; 463241699cdSAl Viro } 464241699cdSAl Viro 465241699cdSAl Viro static size_t copy_pipe_to_iter(const void *addr, size_t bytes, 466241699cdSAl Viro struct iov_iter *i) 467241699cdSAl Viro { 4688fad7767SAl Viro unsigned int off, chunk; 4698fad7767SAl Viro 4708fad7767SAl Viro if (unlikely(bytes > i->count)) 4718fad7767SAl Viro bytes = i->count; 4728fad7767SAl Viro if (unlikely(!bytes)) 4738fad7767SAl Viro return 0; 474241699cdSAl Viro 475241699cdSAl Viro if (!sanity(i)) 476241699cdSAl Viro return 0; 477241699cdSAl Viro 4788fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 4798fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 4808fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 4818fad7767SAl Viro if (!page) 4828fad7767SAl Viro return bytes - n; 4838fad7767SAl Viro memcpy_to_page(page, off, addr, chunk); 484241699cdSAl Viro addr += chunk; 4858fad7767SAl Viro } 486241699cdSAl Viro return bytes; 487241699cdSAl Viro } 488241699cdSAl Viro 489f9152895SAl Viro static __wsum csum_and_memcpy(void *to, const void *from, size_t len, 490f9152895SAl Viro __wsum sum, size_t off) 491f9152895SAl Viro { 492cc44c17bSAl Viro __wsum next = csum_partial_copy_nocheck(from, to, len); 493f9152895SAl Viro return csum_block_add(sum, next, off); 494f9152895SAl Viro } 495f9152895SAl Viro 49678e1f386SAl Viro static size_t csum_and_copy_to_pipe_iter(const void *addr, size_t bytes, 4976852df12SAl Viro struct iov_iter *i, __wsum *sump) 49878e1f386SAl Viro { 4996852df12SAl Viro __wsum sum = *sump; 5006852df12SAl Viro size_t off = 0; 5018fad7767SAl Viro unsigned int chunk, r; 5028fad7767SAl Viro 5038fad7767SAl Viro if (unlikely(bytes > i->count)) 5048fad7767SAl Viro bytes = i->count; 5058fad7767SAl Viro if (unlikely(!bytes)) 5068fad7767SAl Viro return 0; 50778e1f386SAl Viro 50878e1f386SAl Viro if (!sanity(i)) 50978e1f386SAl Viro return 0; 51078e1f386SAl Viro 5116852df12SAl Viro while (bytes) { 5128fad7767SAl Viro struct page *page = append_pipe(i, bytes, &r); 5138fad7767SAl Viro char *p; 5148fad7767SAl Viro 5158fad7767SAl Viro if (!page) 5168fad7767SAl Viro break; 5178fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - r); 5188fad7767SAl Viro p = kmap_local_page(page); 5196852df12SAl Viro sum = csum_and_memcpy(p + r, addr + off, chunk, sum, off); 5202495bdccSAl Viro kunmap_local(p); 52178e1f386SAl Viro off += chunk; 5228fad7767SAl Viro bytes -= chunk; 5236852df12SAl Viro } 5246852df12SAl Viro *sump = sum; 5256852df12SAl Viro return off; 52678e1f386SAl Viro } 52778e1f386SAl Viro 528aa28de27SAl Viro size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 529d879cb83SAl Viro { 530a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 531a41dad90SAl Viro return 0; 53200e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 533241699cdSAl Viro return copy_pipe_to_iter(addr, bytes, i); 534fcb14cb1SAl Viro if (user_backed_iter(i)) 53509fc68dcSAl Viro might_fault(); 5367baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 5377baa5099SAl Viro copyout(base, addr + off, len), 5387baa5099SAl Viro memcpy(base, addr + off, len) 539d879cb83SAl Viro ) 540d879cb83SAl Viro 541d879cb83SAl Viro return bytes; 542d879cb83SAl Viro } 543aa28de27SAl Viro EXPORT_SYMBOL(_copy_to_iter); 544d879cb83SAl Viro 545ec6347bbSDan Williams #ifdef CONFIG_ARCH_HAS_COPY_MC 546ec6347bbSDan Williams static int copyout_mc(void __user *to, const void *from, size_t n) 5478780356eSDan Williams { 54896d4f267SLinus Torvalds if (access_ok(to, n)) { 549d0ef4c36SMarco Elver instrument_copy_to_user(to, from, n); 550ec6347bbSDan Williams n = copy_mc_to_user((__force void *) to, from, n); 5518780356eSDan Williams } 5528780356eSDan Williams return n; 5538780356eSDan Williams } 5548780356eSDan Williams 555ec6347bbSDan Williams static size_t copy_mc_pipe_to_iter(const void *addr, size_t bytes, 556ca146f6fSDan Williams struct iov_iter *i) 557ca146f6fSDan Williams { 5588fad7767SAl Viro size_t xfer = 0; 5598fad7767SAl Viro unsigned int off, chunk; 5608fad7767SAl Viro 5618fad7767SAl Viro if (unlikely(bytes > i->count)) 5628fad7767SAl Viro bytes = i->count; 5638fad7767SAl Viro if (unlikely(!bytes)) 5648fad7767SAl Viro return 0; 565ca146f6fSDan Williams 566ca146f6fSDan Williams if (!sanity(i)) 567ca146f6fSDan Williams return 0; 568ca146f6fSDan Williams 5698fad7767SAl Viro while (bytes) { 5708fad7767SAl Viro struct page *page = append_pipe(i, bytes, &off); 571ca146f6fSDan Williams unsigned long rem; 5728fad7767SAl Viro char *p; 5738fad7767SAl Viro 5748fad7767SAl Viro if (!page) 5758fad7767SAl Viro break; 5768fad7767SAl Viro chunk = min_t(size_t, bytes, PAGE_SIZE - off); 5778fad7767SAl Viro p = kmap_local_page(page); 5782a510a74SAl Viro rem = copy_mc_to_kernel(p + off, addr + xfer, chunk); 5792a510a74SAl Viro chunk -= rem; 5802a510a74SAl Viro kunmap_local(p); 5812a510a74SAl Viro xfer += chunk; 5828fad7767SAl Viro bytes -= chunk; 583c3497fd0SAl Viro if (rem) { 5848fad7767SAl Viro iov_iter_revert(i, rem); 585ca146f6fSDan Williams break; 586c3497fd0SAl Viro } 5872a510a74SAl Viro } 588ca146f6fSDan Williams return xfer; 589ca146f6fSDan Williams } 590ca146f6fSDan Williams 591bf3eeb9bSDan Williams /** 592ec6347bbSDan Williams * _copy_mc_to_iter - copy to iter with source memory error exception handling 593bf3eeb9bSDan Williams * @addr: source kernel address 594bf3eeb9bSDan Williams * @bytes: total transfer length 59544e55997SRandy Dunlap * @i: destination iterator 596bf3eeb9bSDan Williams * 597ec6347bbSDan Williams * The pmem driver deploys this for the dax operation 598ec6347bbSDan Williams * (dax_copy_to_iter()) for dax reads (bypass page-cache and the 599ec6347bbSDan Williams * block-layer). Upon #MC read(2) aborts and returns EIO or the bytes 600ec6347bbSDan Williams * successfully copied. 601bf3eeb9bSDan Williams * 602ec6347bbSDan Williams * The main differences between this and typical _copy_to_iter(). 603bf3eeb9bSDan Williams * 604bf3eeb9bSDan Williams * * Typical tail/residue handling after a fault retries the copy 605bf3eeb9bSDan Williams * byte-by-byte until the fault happens again. Re-triggering machine 606bf3eeb9bSDan Williams * checks is potentially fatal so the implementation uses source 607bf3eeb9bSDan Williams * alignment and poison alignment assumptions to avoid re-triggering 608bf3eeb9bSDan Williams * hardware exceptions. 609bf3eeb9bSDan Williams * 610bf3eeb9bSDan Williams * * ITER_KVEC, ITER_PIPE, and ITER_BVEC can return short copies. 611bf3eeb9bSDan Williams * Compare to copy_to_iter() where only ITER_IOVEC attempts might return 612bf3eeb9bSDan Williams * a short copy. 61344e55997SRandy Dunlap * 61444e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 615bf3eeb9bSDan Williams */ 616ec6347bbSDan Williams size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 6178780356eSDan Williams { 618a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 619a41dad90SAl Viro return 0; 62000e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 621ec6347bbSDan Williams return copy_mc_pipe_to_iter(addr, bytes, i); 622fcb14cb1SAl Viro if (user_backed_iter(i)) 6238780356eSDan Williams might_fault(); 6247baa5099SAl Viro __iterate_and_advance(i, bytes, base, len, off, 6257baa5099SAl Viro copyout_mc(base, addr + off, len), 6267baa5099SAl Viro copy_mc_to_kernel(base, addr + off, len) 6278780356eSDan Williams ) 6288780356eSDan Williams 6298780356eSDan Williams return bytes; 6308780356eSDan Williams } 631ec6347bbSDan Williams EXPORT_SYMBOL_GPL(_copy_mc_to_iter); 632ec6347bbSDan Williams #endif /* CONFIG_ARCH_HAS_COPY_MC */ 6338780356eSDan Williams 634*245f0922SKefeng Wang static void *memcpy_from_iter(struct iov_iter *i, void *to, const void *from, 635*245f0922SKefeng Wang size_t size) 636*245f0922SKefeng Wang { 637*245f0922SKefeng Wang if (iov_iter_is_copy_mc(i)) 638*245f0922SKefeng Wang return (void *)copy_mc_to_kernel(to, from, size); 639*245f0922SKefeng Wang return memcpy(to, from, size); 640*245f0922SKefeng Wang } 641*245f0922SKefeng Wang 642aa28de27SAl Viro size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) 643d879cb83SAl Viro { 644a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 645241699cdSAl Viro return 0; 646a41dad90SAl Viro 647fcb14cb1SAl Viro if (user_backed_iter(i)) 64809fc68dcSAl Viro might_fault(); 6497baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6507baa5099SAl Viro copyin(addr + off, base, len), 651*245f0922SKefeng Wang memcpy_from_iter(i, addr + off, base, len) 652d879cb83SAl Viro ) 653d879cb83SAl Viro 654d879cb83SAl Viro return bytes; 655d879cb83SAl Viro } 656aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter); 657d879cb83SAl Viro 658aa28de27SAl Viro size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) 659d879cb83SAl Viro { 660a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 661241699cdSAl Viro return 0; 662a41dad90SAl Viro 6637baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6647baa5099SAl Viro __copy_from_user_inatomic_nocache(addr + off, base, len), 6657baa5099SAl Viro memcpy(addr + off, base, len) 666d879cb83SAl Viro ) 667d879cb83SAl Viro 668d879cb83SAl Viro return bytes; 669d879cb83SAl Viro } 670aa28de27SAl Viro EXPORT_SYMBOL(_copy_from_iter_nocache); 671d879cb83SAl Viro 6720aed55afSDan Williams #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE 673abd08d7dSDan Williams /** 674abd08d7dSDan Williams * _copy_from_iter_flushcache - write destination through cpu cache 675abd08d7dSDan Williams * @addr: destination kernel address 676abd08d7dSDan Williams * @bytes: total transfer length 67744e55997SRandy Dunlap * @i: source iterator 678abd08d7dSDan Williams * 679abd08d7dSDan Williams * The pmem driver arranges for filesystem-dax to use this facility via 680abd08d7dSDan Williams * dax_copy_from_iter() for ensuring that writes to persistent memory 681abd08d7dSDan Williams * are flushed through the CPU cache. It is differentiated from 682abd08d7dSDan Williams * _copy_from_iter_nocache() in that guarantees all data is flushed for 683abd08d7dSDan Williams * all iterator types. The _copy_from_iter_nocache() only attempts to 684abd08d7dSDan Williams * bypass the cache for the ITER_IOVEC case, and on some archs may use 685abd08d7dSDan Williams * instructions that strand dirty-data in the cache. 68644e55997SRandy Dunlap * 68744e55997SRandy Dunlap * Return: number of bytes copied (may be %0) 688abd08d7dSDan Williams */ 6896a37e940SLinus Torvalds size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i) 6900aed55afSDan Williams { 691a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 6920aed55afSDan Williams return 0; 693a41dad90SAl Viro 6947baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 6957baa5099SAl Viro __copy_from_user_flushcache(addr + off, base, len), 6967baa5099SAl Viro memcpy_flushcache(addr + off, base, len) 6970aed55afSDan Williams ) 6980aed55afSDan Williams 6990aed55afSDan Williams return bytes; 7000aed55afSDan Williams } 7016a37e940SLinus Torvalds EXPORT_SYMBOL_GPL(_copy_from_iter_flushcache); 7020aed55afSDan Williams #endif 7030aed55afSDan Williams 70472e809edSAl Viro static inline bool page_copy_sane(struct page *page, size_t offset, size_t n) 70572e809edSAl Viro { 7066daef95bSEric Dumazet struct page *head; 7076daef95bSEric Dumazet size_t v = n + offset; 7086daef95bSEric Dumazet 7096daef95bSEric Dumazet /* 7106daef95bSEric Dumazet * The general case needs to access the page order in order 7116daef95bSEric Dumazet * to compute the page size. 7126daef95bSEric Dumazet * However, we mostly deal with order-0 pages and thus can 7136daef95bSEric Dumazet * avoid a possible cache line miss for requests that fit all 7146daef95bSEric Dumazet * page orders. 7156daef95bSEric Dumazet */ 7166daef95bSEric Dumazet if (n <= v && v <= PAGE_SIZE) 7176daef95bSEric Dumazet return true; 7186daef95bSEric Dumazet 7196daef95bSEric Dumazet head = compound_head(page); 7206daef95bSEric Dumazet v += (page - head) << PAGE_SHIFT; 721a90bcb86SPetar Penkov 72240a86061SAl Viro if (WARN_ON(n > v || v > page_size(head))) 72372e809edSAl Viro return false; 72440a86061SAl Viro return true; 72572e809edSAl Viro } 726cbbd26b8SAl Viro 72708aa6479SAl Viro size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, 72808aa6479SAl Viro struct iov_iter *i) 72908aa6479SAl Viro { 73008aa6479SAl Viro size_t res = 0; 73140a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) 73208aa6479SAl Viro return 0; 733a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 734a41dad90SAl Viro return 0; 735f0f6b614SAl Viro if (unlikely(iov_iter_is_pipe(i))) 736f0f6b614SAl Viro return copy_page_to_iter_pipe(page, offset, bytes, i); 73708aa6479SAl Viro page += offset / PAGE_SIZE; // first subpage 73808aa6479SAl Viro offset %= PAGE_SIZE; 73908aa6479SAl Viro while (1) { 740f0f6b614SAl Viro void *kaddr = kmap_local_page(page); 741f0f6b614SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 742f0f6b614SAl Viro n = _copy_to_iter(kaddr + offset, n, i); 743f0f6b614SAl Viro kunmap_local(kaddr); 74408aa6479SAl Viro res += n; 74508aa6479SAl Viro bytes -= n; 74608aa6479SAl Viro if (!bytes || !n) 74708aa6479SAl Viro break; 74808aa6479SAl Viro offset += n; 74908aa6479SAl Viro if (offset == PAGE_SIZE) { 75008aa6479SAl Viro page++; 75108aa6479SAl Viro offset = 0; 75208aa6479SAl Viro } 75308aa6479SAl Viro } 75408aa6479SAl Viro return res; 75508aa6479SAl Viro } 756d879cb83SAl Viro EXPORT_SYMBOL(copy_page_to_iter); 757d879cb83SAl Viro 7584f80818bSLorenzo Stoakes size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, size_t bytes, 7594f80818bSLorenzo Stoakes struct iov_iter *i) 7604f80818bSLorenzo Stoakes { 7614f80818bSLorenzo Stoakes size_t res = 0; 7624f80818bSLorenzo Stoakes 7634f80818bSLorenzo Stoakes if (!page_copy_sane(page, offset, bytes)) 7644f80818bSLorenzo Stoakes return 0; 7654f80818bSLorenzo Stoakes if (WARN_ON_ONCE(i->data_source)) 7664f80818bSLorenzo Stoakes return 0; 7674f80818bSLorenzo Stoakes if (unlikely(iov_iter_is_pipe(i))) 7684f80818bSLorenzo Stoakes return copy_page_to_iter_pipe(page, offset, bytes, i); 7694f80818bSLorenzo Stoakes page += offset / PAGE_SIZE; // first subpage 7704f80818bSLorenzo Stoakes offset %= PAGE_SIZE; 7714f80818bSLorenzo Stoakes while (1) { 7724f80818bSLorenzo Stoakes void *kaddr = kmap_local_page(page); 7734f80818bSLorenzo Stoakes size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 7744f80818bSLorenzo Stoakes 7754f80818bSLorenzo Stoakes iterate_and_advance(i, n, base, len, off, 7764f80818bSLorenzo Stoakes copyout_nofault(base, kaddr + offset + off, len), 7774f80818bSLorenzo Stoakes memcpy(base, kaddr + offset + off, len) 7784f80818bSLorenzo Stoakes ) 7794f80818bSLorenzo Stoakes kunmap_local(kaddr); 7804f80818bSLorenzo Stoakes res += n; 7814f80818bSLorenzo Stoakes bytes -= n; 7824f80818bSLorenzo Stoakes if (!bytes || !n) 7834f80818bSLorenzo Stoakes break; 7844f80818bSLorenzo Stoakes offset += n; 7854f80818bSLorenzo Stoakes if (offset == PAGE_SIZE) { 7864f80818bSLorenzo Stoakes page++; 7874f80818bSLorenzo Stoakes offset = 0; 7884f80818bSLorenzo Stoakes } 7894f80818bSLorenzo Stoakes } 7904f80818bSLorenzo Stoakes return res; 7914f80818bSLorenzo Stoakes } 7924f80818bSLorenzo Stoakes EXPORT_SYMBOL(copy_page_to_iter_nofault); 7934f80818bSLorenzo Stoakes 794d879cb83SAl Viro size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, 795d879cb83SAl Viro struct iov_iter *i) 796d879cb83SAl Viro { 797c03f05f1SAl Viro size_t res = 0; 798c03f05f1SAl Viro if (!page_copy_sane(page, offset, bytes)) 79928f38db7SAl Viro return 0; 800c03f05f1SAl Viro page += offset / PAGE_SIZE; // first subpage 801c03f05f1SAl Viro offset %= PAGE_SIZE; 802c03f05f1SAl Viro while (1) { 803c03f05f1SAl Viro void *kaddr = kmap_local_page(page); 804c03f05f1SAl Viro size_t n = min(bytes, (size_t)PAGE_SIZE - offset); 805c03f05f1SAl Viro n = _copy_from_iter(kaddr + offset, n, i); 806c03f05f1SAl Viro kunmap_local(kaddr); 807c03f05f1SAl Viro res += n; 808c03f05f1SAl Viro bytes -= n; 809c03f05f1SAl Viro if (!bytes || !n) 810c03f05f1SAl Viro break; 811c03f05f1SAl Viro offset += n; 812c03f05f1SAl Viro if (offset == PAGE_SIZE) { 813c03f05f1SAl Viro page++; 814c03f05f1SAl Viro offset = 0; 815c03f05f1SAl Viro } 816c03f05f1SAl Viro } 817c03f05f1SAl Viro return res; 818d879cb83SAl Viro } 819d879cb83SAl Viro EXPORT_SYMBOL(copy_page_from_iter); 820d879cb83SAl Viro 821241699cdSAl Viro static size_t pipe_zero(size_t bytes, struct iov_iter *i) 822241699cdSAl Viro { 8238fad7767SAl Viro unsigned int chunk, off; 8248fad7767SAl Viro 8258fad7767SAl Viro if (unlikely(bytes > i->count)) 8268fad7767SAl Viro bytes = i->count; 8278fad7767SAl Viro if (unlikely(!bytes)) 8288fad7767SAl Viro return 0; 829241699cdSAl Viro 830241699cdSAl Viro if (!sanity(i)) 831241699cdSAl Viro return 0; 832241699cdSAl Viro 8338fad7767SAl Viro for (size_t n = bytes; n; n -= chunk) { 8348fad7767SAl Viro struct page *page = append_pipe(i, n, &off); 8358fad7767SAl Viro char *p; 836241699cdSAl Viro 8378fad7767SAl Viro if (!page) 8388fad7767SAl Viro return bytes - n; 8398fad7767SAl Viro chunk = min_t(size_t, n, PAGE_SIZE - off); 8408fad7767SAl Viro p = kmap_local_page(page); 841893839fdSAl Viro memset(p + off, 0, chunk); 842893839fdSAl Viro kunmap_local(p); 8438fad7767SAl Viro } 844241699cdSAl Viro return bytes; 845241699cdSAl Viro } 846241699cdSAl Viro 847d879cb83SAl Viro size_t iov_iter_zero(size_t bytes, struct iov_iter *i) 848d879cb83SAl Viro { 84900e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) 850241699cdSAl Viro return pipe_zero(bytes, i); 8517baa5099SAl Viro iterate_and_advance(i, bytes, base, len, count, 8527baa5099SAl Viro clear_user(base, len), 8537baa5099SAl Viro memset(base, 0, len) 854d879cb83SAl Viro ) 855d879cb83SAl Viro 856d879cb83SAl Viro return bytes; 857d879cb83SAl Viro } 858d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_zero); 859d879cb83SAl Viro 860f0b65f39SAl Viro size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t bytes, 861f0b65f39SAl Viro struct iov_iter *i) 862d879cb83SAl Viro { 863d879cb83SAl Viro char *kaddr = kmap_atomic(page), *p = kaddr + offset; 86440a86061SAl Viro if (!page_copy_sane(page, offset, bytes)) { 86572e809edSAl Viro kunmap_atomic(kaddr); 86672e809edSAl Viro return 0; 86772e809edSAl Viro } 868a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) { 869241699cdSAl Viro kunmap_atomic(kaddr); 870241699cdSAl Viro return 0; 871241699cdSAl Viro } 8727baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, 8737baa5099SAl Viro copyin(p + off, base, len), 874*245f0922SKefeng Wang memcpy_from_iter(i, p + off, base, len) 875d879cb83SAl Viro ) 876d879cb83SAl Viro kunmap_atomic(kaddr); 877d879cb83SAl Viro return bytes; 878d879cb83SAl Viro } 879f0b65f39SAl Viro EXPORT_SYMBOL(copy_page_from_iter_atomic); 880d879cb83SAl Viro 881b9dc6f65SAl Viro static void pipe_advance(struct iov_iter *i, size_t size) 882b9dc6f65SAl Viro { 883b9dc6f65SAl Viro struct pipe_inode_info *pipe = i->pipe; 88410f525a8SAl Viro int off = i->last_offset; 8858cefc107SDavid Howells 8862c855de9SAl Viro if (!off && !size) { 8872c855de9SAl Viro pipe_discard_from(pipe, i->start_head); // discard everything 8882c855de9SAl Viro return; 889b9dc6f65SAl Viro } 890b9dc6f65SAl Viro i->count -= size; 8912c855de9SAl Viro while (1) { 8922c855de9SAl Viro struct pipe_buffer *buf = pipe_buf(pipe, i->head); 8932c855de9SAl Viro if (off) /* make it relative to the beginning of buffer */ 89410f525a8SAl Viro size += abs(off) - buf->offset; 8952c855de9SAl Viro if (size <= buf->len) { 8962c855de9SAl Viro buf->len = size; 89710f525a8SAl Viro i->last_offset = last_offset(buf); 8982c855de9SAl Viro break; 8992c855de9SAl Viro } 9002c855de9SAl Viro size -= buf->len; 9012c855de9SAl Viro i->head++; 9022c855de9SAl Viro off = 0; 9032c855de9SAl Viro } 9042c855de9SAl Viro pipe_discard_from(pipe, i->head + 1); // discard everything past this one 905241699cdSAl Viro } 906241699cdSAl Viro 90754c8195bSPavel Begunkov static void iov_iter_bvec_advance(struct iov_iter *i, size_t size) 90854c8195bSPavel Begunkov { 90918fa9af7SAl Viro const struct bio_vec *bvec, *end; 91054c8195bSPavel Begunkov 91118fa9af7SAl Viro if (!i->count) 91218fa9af7SAl Viro return; 91318fa9af7SAl Viro i->count -= size; 91454c8195bSPavel Begunkov 91518fa9af7SAl Viro size += i->iov_offset; 91618fa9af7SAl Viro 91718fa9af7SAl Viro for (bvec = i->bvec, end = bvec + i->nr_segs; bvec < end; bvec++) { 91818fa9af7SAl Viro if (likely(size < bvec->bv_len)) 91918fa9af7SAl Viro break; 92018fa9af7SAl Viro size -= bvec->bv_len; 92118fa9af7SAl Viro } 92218fa9af7SAl Viro i->iov_offset = size; 92318fa9af7SAl Viro i->nr_segs -= bvec - i->bvec; 92418fa9af7SAl Viro i->bvec = bvec; 92554c8195bSPavel Begunkov } 92654c8195bSPavel Begunkov 927185ac4d4SAl Viro static void iov_iter_iovec_advance(struct iov_iter *i, size_t size) 928185ac4d4SAl Viro { 929185ac4d4SAl Viro const struct iovec *iov, *end; 930185ac4d4SAl Viro 931185ac4d4SAl Viro if (!i->count) 932185ac4d4SAl Viro return; 933185ac4d4SAl Viro i->count -= size; 934185ac4d4SAl Viro 935185ac4d4SAl Viro size += i->iov_offset; // from beginning of current segment 936de4f5fedSJens Axboe for (iov = iter_iov(i), end = iov + i->nr_segs; iov < end; iov++) { 937185ac4d4SAl Viro if (likely(size < iov->iov_len)) 938185ac4d4SAl Viro break; 939185ac4d4SAl Viro size -= iov->iov_len; 940185ac4d4SAl Viro } 941185ac4d4SAl Viro i->iov_offset = size; 942de4f5fedSJens Axboe i->nr_segs -= iov - iter_iov(i); 943de4f5fedSJens Axboe i->__iov = iov; 944185ac4d4SAl Viro } 945185ac4d4SAl Viro 946d879cb83SAl Viro void iov_iter_advance(struct iov_iter *i, size_t size) 947d879cb83SAl Viro { 9483b3fc051SAl Viro if (unlikely(i->count < size)) 9493b3fc051SAl Viro size = i->count; 950fcb14cb1SAl Viro if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) { 951fcb14cb1SAl Viro i->iov_offset += size; 952fcb14cb1SAl Viro i->count -= size; 953fcb14cb1SAl Viro } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) { 954185ac4d4SAl Viro /* iovec and kvec have identical layouts */ 955185ac4d4SAl Viro iov_iter_iovec_advance(i, size); 956185ac4d4SAl Viro } else if (iov_iter_is_bvec(i)) { 957185ac4d4SAl Viro iov_iter_bvec_advance(i, size); 958185ac4d4SAl Viro } else if (iov_iter_is_pipe(i)) { 959241699cdSAl Viro pipe_advance(i, size); 960185ac4d4SAl Viro } else if (iov_iter_is_discard(i)) { 961185ac4d4SAl Viro i->count -= size; 9627ff50620SDavid Howells } 963d879cb83SAl Viro } 964d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_advance); 965d879cb83SAl Viro 96627c0e374SAl Viro void iov_iter_revert(struct iov_iter *i, size_t unroll) 96727c0e374SAl Viro { 96827c0e374SAl Viro if (!unroll) 96927c0e374SAl Viro return; 9705b47d59aSAl Viro if (WARN_ON(unroll > MAX_RW_COUNT)) 9715b47d59aSAl Viro return; 97227c0e374SAl Viro i->count += unroll; 97300e23707SDavid Howells if (unlikely(iov_iter_is_pipe(i))) { 97427c0e374SAl Viro struct pipe_inode_info *pipe = i->pipe; 97592acdc4fSAl Viro unsigned int head = pipe->head; 97692acdc4fSAl Viro 97792acdc4fSAl Viro while (head > i->start_head) { 97892acdc4fSAl Viro struct pipe_buffer *b = pipe_buf(pipe, --head); 97992acdc4fSAl Viro if (unroll < b->len) { 98092acdc4fSAl Viro b->len -= unroll; 98110f525a8SAl Viro i->last_offset = last_offset(b); 98292acdc4fSAl Viro i->head = head; 98392acdc4fSAl Viro return; 98427c0e374SAl Viro } 98592acdc4fSAl Viro unroll -= b->len; 98692acdc4fSAl Viro pipe_buf_release(pipe, b); 98792acdc4fSAl Viro pipe->head--; 98827c0e374SAl Viro } 98910f525a8SAl Viro i->last_offset = 0; 99092acdc4fSAl Viro i->head = head; 99127c0e374SAl Viro return; 99227c0e374SAl Viro } 9939ea9ce04SDavid Howells if (unlikely(iov_iter_is_discard(i))) 9949ea9ce04SDavid Howells return; 99527c0e374SAl Viro if (unroll <= i->iov_offset) { 99627c0e374SAl Viro i->iov_offset -= unroll; 99727c0e374SAl Viro return; 99827c0e374SAl Viro } 99927c0e374SAl Viro unroll -= i->iov_offset; 1000fcb14cb1SAl Viro if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) { 10017ff50620SDavid Howells BUG(); /* We should never go beyond the start of the specified 10027ff50620SDavid Howells * range since we might then be straying into pages that 10037ff50620SDavid Howells * aren't pinned. 10047ff50620SDavid Howells */ 10057ff50620SDavid Howells } else if (iov_iter_is_bvec(i)) { 100627c0e374SAl Viro const struct bio_vec *bvec = i->bvec; 100727c0e374SAl Viro while (1) { 100827c0e374SAl Viro size_t n = (--bvec)->bv_len; 100927c0e374SAl Viro i->nr_segs++; 101027c0e374SAl Viro if (unroll <= n) { 101127c0e374SAl Viro i->bvec = bvec; 101227c0e374SAl Viro i->iov_offset = n - unroll; 101327c0e374SAl Viro return; 101427c0e374SAl Viro } 101527c0e374SAl Viro unroll -= n; 101627c0e374SAl Viro } 101727c0e374SAl Viro } else { /* same logics for iovec and kvec */ 1018de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i); 101927c0e374SAl Viro while (1) { 102027c0e374SAl Viro size_t n = (--iov)->iov_len; 102127c0e374SAl Viro i->nr_segs++; 102227c0e374SAl Viro if (unroll <= n) { 1023de4f5fedSJens Axboe i->__iov = iov; 102427c0e374SAl Viro i->iov_offset = n - unroll; 102527c0e374SAl Viro return; 102627c0e374SAl Viro } 102727c0e374SAl Viro unroll -= n; 102827c0e374SAl Viro } 102927c0e374SAl Viro } 103027c0e374SAl Viro } 103127c0e374SAl Viro EXPORT_SYMBOL(iov_iter_revert); 103227c0e374SAl Viro 1033d879cb83SAl Viro /* 1034d879cb83SAl Viro * Return the count of just the current iov_iter segment. 1035d879cb83SAl Viro */ 1036d879cb83SAl Viro size_t iov_iter_single_seg_count(const struct iov_iter *i) 1037d879cb83SAl Viro { 103828f38db7SAl Viro if (i->nr_segs > 1) { 103928f38db7SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 1040de4f5fedSJens Axboe return min(i->count, iter_iov(i)->iov_len - i->iov_offset); 10417ff50620SDavid Howells if (iov_iter_is_bvec(i)) 1042d879cb83SAl Viro return min(i->count, i->bvec->bv_len - i->iov_offset); 104328f38db7SAl Viro } 104428f38db7SAl Viro return i->count; 1045d879cb83SAl Viro } 1046d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_single_seg_count); 1047d879cb83SAl Viro 1048aa563d7bSDavid Howells void iov_iter_kvec(struct iov_iter *i, unsigned int direction, 1049d879cb83SAl Viro const struct kvec *kvec, unsigned long nr_segs, 1050d879cb83SAl Viro size_t count) 1051d879cb83SAl Viro { 1052aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 10538cd54c1cSAl Viro *i = (struct iov_iter){ 10548cd54c1cSAl Viro .iter_type = ITER_KVEC, 1055*245f0922SKefeng Wang .copy_mc = false, 10568cd54c1cSAl Viro .data_source = direction, 10578cd54c1cSAl Viro .kvec = kvec, 10588cd54c1cSAl Viro .nr_segs = nr_segs, 10598cd54c1cSAl Viro .iov_offset = 0, 10608cd54c1cSAl Viro .count = count 10618cd54c1cSAl Viro }; 1062d879cb83SAl Viro } 1063d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_kvec); 1064d879cb83SAl Viro 1065aa563d7bSDavid Howells void iov_iter_bvec(struct iov_iter *i, unsigned int direction, 1066d879cb83SAl Viro const struct bio_vec *bvec, unsigned long nr_segs, 1067d879cb83SAl Viro size_t count) 1068d879cb83SAl Viro { 1069aa563d7bSDavid Howells WARN_ON(direction & ~(READ | WRITE)); 10708cd54c1cSAl Viro *i = (struct iov_iter){ 10718cd54c1cSAl Viro .iter_type = ITER_BVEC, 1072*245f0922SKefeng Wang .copy_mc = false, 10738cd54c1cSAl Viro .data_source = direction, 10748cd54c1cSAl Viro .bvec = bvec, 10758cd54c1cSAl Viro .nr_segs = nr_segs, 10768cd54c1cSAl Viro .iov_offset = 0, 10778cd54c1cSAl Viro .count = count 10788cd54c1cSAl Viro }; 1079d879cb83SAl Viro } 1080d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_bvec); 1081d879cb83SAl Viro 1082aa563d7bSDavid Howells void iov_iter_pipe(struct iov_iter *i, unsigned int direction, 1083241699cdSAl Viro struct pipe_inode_info *pipe, 1084241699cdSAl Viro size_t count) 1085241699cdSAl Viro { 1086aa563d7bSDavid Howells BUG_ON(direction != READ); 10878cefc107SDavid Howells WARN_ON(pipe_full(pipe->head, pipe->tail, pipe->ring_size)); 10888cd54c1cSAl Viro *i = (struct iov_iter){ 10898cd54c1cSAl Viro .iter_type = ITER_PIPE, 10908cd54c1cSAl Viro .data_source = false, 10918cd54c1cSAl Viro .pipe = pipe, 10928cd54c1cSAl Viro .head = pipe->head, 10938cd54c1cSAl Viro .start_head = pipe->head, 109410f525a8SAl Viro .last_offset = 0, 10958cd54c1cSAl Viro .count = count 10968cd54c1cSAl Viro }; 1097241699cdSAl Viro } 1098241699cdSAl Viro EXPORT_SYMBOL(iov_iter_pipe); 1099241699cdSAl Viro 11009ea9ce04SDavid Howells /** 11017ff50620SDavid Howells * iov_iter_xarray - Initialise an I/O iterator to use the pages in an xarray 11027ff50620SDavid Howells * @i: The iterator to initialise. 11037ff50620SDavid Howells * @direction: The direction of the transfer. 11047ff50620SDavid Howells * @xarray: The xarray to access. 11057ff50620SDavid Howells * @start: The start file position. 11067ff50620SDavid Howells * @count: The size of the I/O buffer in bytes. 11077ff50620SDavid Howells * 11087ff50620SDavid Howells * Set up an I/O iterator to either draw data out of the pages attached to an 11097ff50620SDavid Howells * inode or to inject data into those pages. The pages *must* be prevented 11107ff50620SDavid Howells * from evaporation, either by taking a ref on them or locking them by the 11117ff50620SDavid Howells * caller. 11127ff50620SDavid Howells */ 11137ff50620SDavid Howells void iov_iter_xarray(struct iov_iter *i, unsigned int direction, 11147ff50620SDavid Howells struct xarray *xarray, loff_t start, size_t count) 11157ff50620SDavid Howells { 11167ff50620SDavid Howells BUG_ON(direction & ~1); 11178cd54c1cSAl Viro *i = (struct iov_iter) { 11188cd54c1cSAl Viro .iter_type = ITER_XARRAY, 1119*245f0922SKefeng Wang .copy_mc = false, 11208cd54c1cSAl Viro .data_source = direction, 11218cd54c1cSAl Viro .xarray = xarray, 11228cd54c1cSAl Viro .xarray_start = start, 11238cd54c1cSAl Viro .count = count, 11248cd54c1cSAl Viro .iov_offset = 0 11258cd54c1cSAl Viro }; 11267ff50620SDavid Howells } 11277ff50620SDavid Howells EXPORT_SYMBOL(iov_iter_xarray); 11287ff50620SDavid Howells 11297ff50620SDavid Howells /** 11309ea9ce04SDavid Howells * iov_iter_discard - Initialise an I/O iterator that discards data 11319ea9ce04SDavid Howells * @i: The iterator to initialise. 11329ea9ce04SDavid Howells * @direction: The direction of the transfer. 11339ea9ce04SDavid Howells * @count: The size of the I/O buffer in bytes. 11349ea9ce04SDavid Howells * 11359ea9ce04SDavid Howells * Set up an I/O iterator that just discards everything that's written to it. 11369ea9ce04SDavid Howells * It's only available as a READ iterator. 11379ea9ce04SDavid Howells */ 11389ea9ce04SDavid Howells void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count) 11399ea9ce04SDavid Howells { 11409ea9ce04SDavid Howells BUG_ON(direction != READ); 11418cd54c1cSAl Viro *i = (struct iov_iter){ 11428cd54c1cSAl Viro .iter_type = ITER_DISCARD, 1143*245f0922SKefeng Wang .copy_mc = false, 11448cd54c1cSAl Viro .data_source = false, 11458cd54c1cSAl Viro .count = count, 11468cd54c1cSAl Viro .iov_offset = 0 11478cd54c1cSAl Viro }; 11489ea9ce04SDavid Howells } 11499ea9ce04SDavid Howells EXPORT_SYMBOL(iov_iter_discard); 11509ea9ce04SDavid Howells 1151cfa320f7SKeith Busch static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask, 1152cfa320f7SKeith Busch unsigned len_mask) 1153cfa320f7SKeith Busch { 1154cfa320f7SKeith Busch size_t size = i->count; 1155cfa320f7SKeith Busch size_t skip = i->iov_offset; 1156cfa320f7SKeith Busch unsigned k; 1157cfa320f7SKeith Busch 1158cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1159de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i) + k; 1160de4f5fedSJens Axboe size_t len = iov->iov_len - skip; 1161cfa320f7SKeith Busch 1162cfa320f7SKeith Busch if (len > size) 1163cfa320f7SKeith Busch len = size; 1164cfa320f7SKeith Busch if (len & len_mask) 1165cfa320f7SKeith Busch return false; 1166de4f5fedSJens Axboe if ((unsigned long)(iov->iov_base + skip) & addr_mask) 1167cfa320f7SKeith Busch return false; 1168cfa320f7SKeith Busch 1169cfa320f7SKeith Busch size -= len; 1170cfa320f7SKeith Busch if (!size) 1171cfa320f7SKeith Busch break; 1172cfa320f7SKeith Busch } 1173cfa320f7SKeith Busch return true; 1174cfa320f7SKeith Busch } 1175cfa320f7SKeith Busch 1176cfa320f7SKeith Busch static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask, 1177cfa320f7SKeith Busch unsigned len_mask) 1178cfa320f7SKeith Busch { 1179cfa320f7SKeith Busch size_t size = i->count; 1180cfa320f7SKeith Busch unsigned skip = i->iov_offset; 1181cfa320f7SKeith Busch unsigned k; 1182cfa320f7SKeith Busch 1183cfa320f7SKeith Busch for (k = 0; k < i->nr_segs; k++, skip = 0) { 1184cfa320f7SKeith Busch size_t len = i->bvec[k].bv_len - skip; 1185cfa320f7SKeith Busch 1186cfa320f7SKeith Busch if (len > size) 1187cfa320f7SKeith Busch len = size; 1188cfa320f7SKeith Busch if (len & len_mask) 1189cfa320f7SKeith Busch return false; 1190cfa320f7SKeith Busch if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask) 1191cfa320f7SKeith Busch return false; 1192cfa320f7SKeith Busch 1193cfa320f7SKeith Busch size -= len; 1194cfa320f7SKeith Busch if (!size) 1195cfa320f7SKeith Busch break; 1196cfa320f7SKeith Busch } 1197cfa320f7SKeith Busch return true; 1198cfa320f7SKeith Busch } 1199cfa320f7SKeith Busch 1200cfa320f7SKeith Busch /** 1201cfa320f7SKeith Busch * iov_iter_is_aligned() - Check if the addresses and lengths of each segments 1202cfa320f7SKeith Busch * are aligned to the parameters. 1203cfa320f7SKeith Busch * 1204cfa320f7SKeith Busch * @i: &struct iov_iter to restore 1205cfa320f7SKeith Busch * @addr_mask: bit mask to check against the iov element's addresses 1206cfa320f7SKeith Busch * @len_mask: bit mask to check against the iov element's lengths 1207cfa320f7SKeith Busch * 1208cfa320f7SKeith Busch * Return: false if any addresses or lengths intersect with the provided masks 1209cfa320f7SKeith Busch */ 1210cfa320f7SKeith Busch bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask, 1211cfa320f7SKeith Busch unsigned len_mask) 1212cfa320f7SKeith Busch { 1213fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1214fcb14cb1SAl Viro if (i->count & len_mask) 1215fcb14cb1SAl Viro return false; 1216fcb14cb1SAl Viro if ((unsigned long)(i->ubuf + i->iov_offset) & addr_mask) 1217fcb14cb1SAl Viro return false; 1218fcb14cb1SAl Viro return true; 1219fcb14cb1SAl Viro } 1220fcb14cb1SAl Viro 1221cfa320f7SKeith Busch if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 1222cfa320f7SKeith Busch return iov_iter_aligned_iovec(i, addr_mask, len_mask); 1223cfa320f7SKeith Busch 1224cfa320f7SKeith Busch if (iov_iter_is_bvec(i)) 1225cfa320f7SKeith Busch return iov_iter_aligned_bvec(i, addr_mask, len_mask); 1226cfa320f7SKeith Busch 1227cfa320f7SKeith Busch if (iov_iter_is_pipe(i)) { 1228cfa320f7SKeith Busch size_t size = i->count; 1229cfa320f7SKeith Busch 1230cfa320f7SKeith Busch if (size & len_mask) 1231cfa320f7SKeith Busch return false; 123210f525a8SAl Viro if (size && i->last_offset > 0) { 123310f525a8SAl Viro if (i->last_offset & addr_mask) 1234cfa320f7SKeith Busch return false; 1235cfa320f7SKeith Busch } 1236cfa320f7SKeith Busch 1237cfa320f7SKeith Busch return true; 1238cfa320f7SKeith Busch } 1239cfa320f7SKeith Busch 1240cfa320f7SKeith Busch if (iov_iter_is_xarray(i)) { 1241cfa320f7SKeith Busch if (i->count & len_mask) 1242cfa320f7SKeith Busch return false; 1243cfa320f7SKeith Busch if ((i->xarray_start + i->iov_offset) & addr_mask) 1244cfa320f7SKeith Busch return false; 1245cfa320f7SKeith Busch } 1246cfa320f7SKeith Busch 1247cfa320f7SKeith Busch return true; 1248cfa320f7SKeith Busch } 1249cfa320f7SKeith Busch EXPORT_SYMBOL_GPL(iov_iter_is_aligned); 1250cfa320f7SKeith Busch 12519221d2e3SAl Viro static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i) 1252d879cb83SAl Viro { 1253d879cb83SAl Viro unsigned long res = 0; 1254d879cb83SAl Viro size_t size = i->count; 12559221d2e3SAl Viro size_t skip = i->iov_offset; 12569221d2e3SAl Viro unsigned k; 1257d879cb83SAl Viro 12589221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 1259de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i) + k; 1260de4f5fedSJens Axboe size_t len = iov->iov_len - skip; 12619221d2e3SAl Viro if (len) { 1262de4f5fedSJens Axboe res |= (unsigned long)iov->iov_base + skip; 12639221d2e3SAl Viro if (len > size) 12649221d2e3SAl Viro len = size; 12659221d2e3SAl Viro res |= len; 12669221d2e3SAl Viro size -= len; 12679221d2e3SAl Viro if (!size) 12689221d2e3SAl Viro break; 12699221d2e3SAl Viro } 12709221d2e3SAl Viro } 12719221d2e3SAl Viro return res; 12729221d2e3SAl Viro } 12739221d2e3SAl Viro 12749221d2e3SAl Viro static unsigned long iov_iter_alignment_bvec(const struct iov_iter *i) 12759221d2e3SAl Viro { 12769221d2e3SAl Viro unsigned res = 0; 12779221d2e3SAl Viro size_t size = i->count; 12789221d2e3SAl Viro unsigned skip = i->iov_offset; 12799221d2e3SAl Viro unsigned k; 12809221d2e3SAl Viro 12819221d2e3SAl Viro for (k = 0; k < i->nr_segs; k++, skip = 0) { 12829221d2e3SAl Viro size_t len = i->bvec[k].bv_len - skip; 12839221d2e3SAl Viro res |= (unsigned long)i->bvec[k].bv_offset + skip; 12849221d2e3SAl Viro if (len > size) 12859221d2e3SAl Viro len = size; 12869221d2e3SAl Viro res |= len; 12879221d2e3SAl Viro size -= len; 12889221d2e3SAl Viro if (!size) 12899221d2e3SAl Viro break; 12909221d2e3SAl Viro } 12919221d2e3SAl Viro return res; 12929221d2e3SAl Viro } 12939221d2e3SAl Viro 12949221d2e3SAl Viro unsigned long iov_iter_alignment(const struct iov_iter *i) 12959221d2e3SAl Viro { 1296fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1297fcb14cb1SAl Viro size_t size = i->count; 1298fcb14cb1SAl Viro if (size) 1299fcb14cb1SAl Viro return ((unsigned long)i->ubuf + i->iov_offset) | size; 1300fcb14cb1SAl Viro return 0; 1301fcb14cb1SAl Viro } 1302fcb14cb1SAl Viro 13039221d2e3SAl Viro /* iovec and kvec have identical layouts */ 13049221d2e3SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 13059221d2e3SAl Viro return iov_iter_alignment_iovec(i); 13069221d2e3SAl Viro 13079221d2e3SAl Viro if (iov_iter_is_bvec(i)) 13089221d2e3SAl Viro return iov_iter_alignment_bvec(i); 13099221d2e3SAl Viro 13109221d2e3SAl Viro if (iov_iter_is_pipe(i)) { 13119221d2e3SAl Viro size_t size = i->count; 1312e0ff126eSJan Kara 131310f525a8SAl Viro if (size && i->last_offset > 0) 131410f525a8SAl Viro return size | i->last_offset; 1315241699cdSAl Viro return size; 1316241699cdSAl Viro } 13179221d2e3SAl Viro 13189221d2e3SAl Viro if (iov_iter_is_xarray(i)) 13193d14ec1fSDavid Howells return (i->xarray_start + i->iov_offset) | i->count; 13209221d2e3SAl Viro 13219221d2e3SAl Viro return 0; 1322d879cb83SAl Viro } 1323d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_alignment); 1324d879cb83SAl Viro 1325357f435dSAl Viro unsigned long iov_iter_gap_alignment(const struct iov_iter *i) 1326357f435dSAl Viro { 1327357f435dSAl Viro unsigned long res = 0; 1328610c7a71SAl Viro unsigned long v = 0; 1329357f435dSAl Viro size_t size = i->count; 1330610c7a71SAl Viro unsigned k; 1331357f435dSAl Viro 1332fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1333fcb14cb1SAl Viro return 0; 1334fcb14cb1SAl Viro 1335610c7a71SAl Viro if (WARN_ON(!iter_is_iovec(i))) 1336241699cdSAl Viro return ~0U; 1337241699cdSAl Viro 1338610c7a71SAl Viro for (k = 0; k < i->nr_segs; k++) { 1339de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i) + k; 1340de4f5fedSJens Axboe if (iov->iov_len) { 1341de4f5fedSJens Axboe unsigned long base = (unsigned long)iov->iov_base; 1342610c7a71SAl Viro if (v) // if not the first one 1343610c7a71SAl Viro res |= base | v; // this start | previous end 1344de4f5fedSJens Axboe v = base + iov->iov_len; 1345de4f5fedSJens Axboe if (size <= iov->iov_len) 1346610c7a71SAl Viro break; 1347de4f5fedSJens Axboe size -= iov->iov_len; 1348610c7a71SAl Viro } 1349610c7a71SAl Viro } 1350357f435dSAl Viro return res; 1351357f435dSAl Viro } 1352357f435dSAl Viro EXPORT_SYMBOL(iov_iter_gap_alignment); 1353357f435dSAl Viro 13543cf42da3SAl Viro static int want_pages_array(struct page ***res, size_t size, 13553cf42da3SAl Viro size_t start, unsigned int maxpages) 1356acbdeb83SAl Viro { 13573cf42da3SAl Viro unsigned int count = DIV_ROUND_UP(size + start, PAGE_SIZE); 13583cf42da3SAl Viro 13593cf42da3SAl Viro if (count > maxpages) 13603cf42da3SAl Viro count = maxpages; 13613cf42da3SAl Viro WARN_ON(!count); // caller should've prevented that 13623cf42da3SAl Viro if (!*res) { 13633cf42da3SAl Viro *res = kvmalloc_array(count, sizeof(struct page *), GFP_KERNEL); 13643cf42da3SAl Viro if (!*res) 13653cf42da3SAl Viro return 0; 13663cf42da3SAl Viro } 13673cf42da3SAl Viro return count; 1368acbdeb83SAl Viro } 1369acbdeb83SAl Viro 137085200084SAl Viro static ssize_t pipe_get_pages(struct iov_iter *i, 137185200084SAl Viro struct page ***pages, size_t maxsize, unsigned maxpages, 137285200084SAl Viro size_t *start) 1373241699cdSAl Viro { 1374746de1f8SAl Viro unsigned int npages, count, off, chunk; 137585200084SAl Viro struct page **p; 1376746de1f8SAl Viro size_t left; 1377241699cdSAl Viro 137885200084SAl Viro if (!sanity(i)) 137985200084SAl Viro return -EFAULT; 138085200084SAl Viro 138185200084SAl Viro *start = off = pipe_npages(i, &npages); 13823cf42da3SAl Viro if (!npages) 13833cf42da3SAl Viro return -EFAULT; 13843cf42da3SAl Viro count = want_pages_array(pages, maxsize, off, min(npages, maxpages)); 13853cf42da3SAl Viro if (!count) 138685200084SAl Viro return -ENOMEM; 13873cf42da3SAl Viro p = *pages; 1388746de1f8SAl Viro for (npages = 0, left = maxsize ; npages < count; npages++, left -= chunk) { 1389746de1f8SAl Viro struct page *page = append_pipe(i, left, &off); 1390e3b42964SAl Viro if (!page) 1391e3b42964SAl Viro break; 1392746de1f8SAl Viro chunk = min_t(size_t, left, PAGE_SIZE - off); 139385200084SAl Viro get_page(*p++ = page); 1394e3b42964SAl Viro } 139585200084SAl Viro if (!npages) 1396241699cdSAl Viro return -EFAULT; 1397746de1f8SAl Viro return maxsize - left; 1398241699cdSAl Viro } 1399241699cdSAl Viro 14007ff50620SDavid Howells static ssize_t iter_xarray_populate_pages(struct page **pages, struct xarray *xa, 14017ff50620SDavid Howells pgoff_t index, unsigned int nr_pages) 14027ff50620SDavid Howells { 14037ff50620SDavid Howells XA_STATE(xas, xa, index); 14047ff50620SDavid Howells struct page *page; 14057ff50620SDavid Howells unsigned int ret = 0; 14067ff50620SDavid Howells 14077ff50620SDavid Howells rcu_read_lock(); 14087ff50620SDavid Howells for (page = xas_load(&xas); page; page = xas_next(&xas)) { 14097ff50620SDavid Howells if (xas_retry(&xas, page)) 14107ff50620SDavid Howells continue; 14117ff50620SDavid Howells 14127ff50620SDavid Howells /* Has the page moved or been split? */ 14137ff50620SDavid Howells if (unlikely(page != xas_reload(&xas))) { 14147ff50620SDavid Howells xas_reset(&xas); 14157ff50620SDavid Howells continue; 14167ff50620SDavid Howells } 14177ff50620SDavid Howells 14187ff50620SDavid Howells pages[ret] = find_subpage(page, xas.xa_index); 14197ff50620SDavid Howells get_page(pages[ret]); 14207ff50620SDavid Howells if (++ret == nr_pages) 14217ff50620SDavid Howells break; 14227ff50620SDavid Howells } 14237ff50620SDavid Howells rcu_read_unlock(); 14247ff50620SDavid Howells return ret; 14257ff50620SDavid Howells } 14267ff50620SDavid Howells 14277ff50620SDavid Howells static ssize_t iter_xarray_get_pages(struct iov_iter *i, 142868fe506fSAl Viro struct page ***pages, size_t maxsize, 14297ff50620SDavid Howells unsigned maxpages, size_t *_start_offset) 14307ff50620SDavid Howells { 14313cf42da3SAl Viro unsigned nr, offset, count; 14323cf42da3SAl Viro pgoff_t index; 14337ff50620SDavid Howells loff_t pos; 14347ff50620SDavid Howells 14357ff50620SDavid Howells pos = i->xarray_start + i->iov_offset; 14367ff50620SDavid Howells index = pos >> PAGE_SHIFT; 14377ff50620SDavid Howells offset = pos & ~PAGE_MASK; 14387ff50620SDavid Howells *_start_offset = offset; 14397ff50620SDavid Howells 14403cf42da3SAl Viro count = want_pages_array(pages, maxsize, offset, maxpages); 14413cf42da3SAl Viro if (!count) 144268fe506fSAl Viro return -ENOMEM; 144368fe506fSAl Viro nr = iter_xarray_populate_pages(*pages, i->xarray, index, count); 14447ff50620SDavid Howells if (nr == 0) 14457ff50620SDavid Howells return 0; 14467ff50620SDavid Howells 1447eba2d3d7SAl Viro maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize); 1448310d9d5aSAl Viro i->iov_offset += maxsize; 1449310d9d5aSAl Viro i->count -= maxsize; 1450eba2d3d7SAl Viro return maxsize; 14517ff50620SDavid Howells } 14527ff50620SDavid Howells 1453fcb14cb1SAl Viro /* must be done on non-empty ITER_UBUF or ITER_IOVEC one */ 1454dd45ab9dSAl Viro static unsigned long first_iovec_segment(const struct iov_iter *i, size_t *size) 14553d671ca6SAl Viro { 14563d671ca6SAl Viro size_t skip; 14573d671ca6SAl Viro long k; 14583d671ca6SAl Viro 1459fcb14cb1SAl Viro if (iter_is_ubuf(i)) 1460fcb14cb1SAl Viro return (unsigned long)i->ubuf + i->iov_offset; 1461fcb14cb1SAl Viro 14623d671ca6SAl Viro for (k = 0, skip = i->iov_offset; k < i->nr_segs; k++, skip = 0) { 1463de4f5fedSJens Axboe const struct iovec *iov = iter_iov(i) + k; 1464de4f5fedSJens Axboe size_t len = iov->iov_len - skip; 14653d671ca6SAl Viro 14663d671ca6SAl Viro if (unlikely(!len)) 14673d671ca6SAl Viro continue; 146859dbd7d0SAl Viro if (*size > len) 14693d671ca6SAl Viro *size = len; 1470de4f5fedSJens Axboe return (unsigned long)iov->iov_base + skip; 14713d671ca6SAl Viro } 14723d671ca6SAl Viro BUG(); // if it had been empty, we wouldn't get called 14733d671ca6SAl Viro } 14743d671ca6SAl Viro 14753d671ca6SAl Viro /* must be done on non-empty ITER_BVEC one */ 14763d671ca6SAl Viro static struct page *first_bvec_segment(const struct iov_iter *i, 147759dbd7d0SAl Viro size_t *size, size_t *start) 14783d671ca6SAl Viro { 14793d671ca6SAl Viro struct page *page; 14803d671ca6SAl Viro size_t skip = i->iov_offset, len; 14813d671ca6SAl Viro 14823d671ca6SAl Viro len = i->bvec->bv_len - skip; 148359dbd7d0SAl Viro if (*size > len) 148459dbd7d0SAl Viro *size = len; 14853d671ca6SAl Viro skip += i->bvec->bv_offset; 14863d671ca6SAl Viro page = i->bvec->bv_page + skip / PAGE_SIZE; 1487dda8e5d1SAl Viro *start = skip % PAGE_SIZE; 14883d671ca6SAl Viro return page; 14893d671ca6SAl Viro } 14903d671ca6SAl Viro 149191329559SAl Viro static ssize_t __iov_iter_get_pages_alloc(struct iov_iter *i, 1492d879cb83SAl Viro struct page ***pages, size_t maxsize, 1493d8207640SLogan Gunthorpe unsigned int maxpages, size_t *start, 1494f62e52d1SDavid Howells iov_iter_extraction_t extraction_flags) 1495d879cb83SAl Viro { 1496f62e52d1SDavid Howells unsigned int n, gup_flags = 0; 1497d879cb83SAl Viro 1498d879cb83SAl Viro if (maxsize > i->count) 1499d879cb83SAl Viro maxsize = i->count; 15003d671ca6SAl Viro if (!maxsize) 15013d671ca6SAl Viro return 0; 15027392ed17SAl Viro if (maxsize > MAX_RW_COUNT) 15037392ed17SAl Viro maxsize = MAX_RW_COUNT; 1504f62e52d1SDavid Howells if (extraction_flags & ITER_ALLOW_P2PDMA) 1505f62e52d1SDavid Howells gup_flags |= FOLL_PCI_P2PDMA; 1506d879cb83SAl Viro 1507fcb14cb1SAl Viro if (likely(user_backed_iter(i))) { 15083d671ca6SAl Viro unsigned long addr; 15093cf42da3SAl Viro int res; 15109ea9ce04SDavid Howells 15113337ab08SAndreas Gruenbacher if (iov_iter_rw(i) != WRITE) 15123337ab08SAndreas Gruenbacher gup_flags |= FOLL_WRITE; 15133337ab08SAndreas Gruenbacher if (i->nofault) 15143337ab08SAndreas Gruenbacher gup_flags |= FOLL_NOFAULT; 15153337ab08SAndreas Gruenbacher 1516dd45ab9dSAl Viro addr = first_iovec_segment(i, &maxsize); 1517dd45ab9dSAl Viro *start = addr % PAGE_SIZE; 1518dd45ab9dSAl Viro addr &= PAGE_MASK; 15193cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 15203cf42da3SAl Viro if (!n) 1521d879cb83SAl Viro return -ENOMEM; 1522451c0ba9SAl Viro res = get_user_pages_fast(addr, n, gup_flags, *pages); 152391329559SAl Viro if (unlikely(res <= 0)) 1524d879cb83SAl Viro return res; 1525eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - *start); 1526eba2d3d7SAl Viro iov_iter_advance(i, maxsize); 1527eba2d3d7SAl Viro return maxsize; 15283d671ca6SAl Viro } 15293d671ca6SAl Viro if (iov_iter_is_bvec(i)) { 1530451c0ba9SAl Viro struct page **p; 15313d671ca6SAl Viro struct page *page; 15323d671ca6SAl Viro 153359dbd7d0SAl Viro page = first_bvec_segment(i, &maxsize, start); 15343cf42da3SAl Viro n = want_pages_array(pages, maxsize, *start, maxpages); 15353cf42da3SAl Viro if (!n) 1536d879cb83SAl Viro return -ENOMEM; 15373cf42da3SAl Viro p = *pages; 1538dda8e5d1SAl Viro for (int k = 0; k < n; k++) 1539eba2d3d7SAl Viro get_page(p[k] = page + k); 1540eba2d3d7SAl Viro maxsize = min_t(size_t, maxsize, n * PAGE_SIZE - *start); 1541310d9d5aSAl Viro i->count -= maxsize; 1542310d9d5aSAl Viro i->iov_offset += maxsize; 1543310d9d5aSAl Viro if (i->iov_offset == i->bvec->bv_len) { 1544310d9d5aSAl Viro i->iov_offset = 0; 1545310d9d5aSAl Viro i->bvec++; 1546310d9d5aSAl Viro i->nr_segs--; 1547310d9d5aSAl Viro } 1548eba2d3d7SAl Viro return maxsize; 15493d671ca6SAl Viro } 15503d671ca6SAl Viro if (iov_iter_is_pipe(i)) 1551451c0ba9SAl Viro return pipe_get_pages(i, pages, maxsize, maxpages, start); 15523d671ca6SAl Viro if (iov_iter_is_xarray(i)) 1553451c0ba9SAl Viro return iter_xarray_get_pages(i, pages, maxsize, maxpages, start); 1554d879cb83SAl Viro return -EFAULT; 1555d879cb83SAl Viro } 155691329559SAl Viro 1557d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages(struct iov_iter *i, 1558451c0ba9SAl Viro struct page **pages, size_t maxsize, unsigned maxpages, 1559f62e52d1SDavid Howells size_t *start, iov_iter_extraction_t extraction_flags) 1560451c0ba9SAl Viro { 1561451c0ba9SAl Viro if (!maxpages) 1562451c0ba9SAl Viro return 0; 1563451c0ba9SAl Viro BUG_ON(!pages); 1564451c0ba9SAl Viro 1565d8207640SLogan Gunthorpe return __iov_iter_get_pages_alloc(i, &pages, maxsize, maxpages, 1566f62e52d1SDavid Howells start, extraction_flags); 1567d8207640SLogan Gunthorpe } 1568d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages); 1569d8207640SLogan Gunthorpe 1570d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages, 1571d8207640SLogan Gunthorpe size_t maxsize, unsigned maxpages, size_t *start) 1572d8207640SLogan Gunthorpe { 1573d8207640SLogan Gunthorpe return iov_iter_get_pages(i, pages, maxsize, maxpages, start, 0); 1574451c0ba9SAl Viro } 1575eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages2); 1576451c0ba9SAl Viro 1577d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, 157891329559SAl Viro struct page ***pages, size_t maxsize, 1579f62e52d1SDavid Howells size_t *start, iov_iter_extraction_t extraction_flags) 158091329559SAl Viro { 158191329559SAl Viro ssize_t len; 158291329559SAl Viro 158391329559SAl Viro *pages = NULL; 158491329559SAl Viro 1585d8207640SLogan Gunthorpe len = __iov_iter_get_pages_alloc(i, pages, maxsize, ~0U, start, 1586f62e52d1SDavid Howells extraction_flags); 158791329559SAl Viro if (len <= 0) { 158891329559SAl Viro kvfree(*pages); 158991329559SAl Viro *pages = NULL; 159091329559SAl Viro } 159191329559SAl Viro return len; 159291329559SAl Viro } 1593d8207640SLogan Gunthorpe EXPORT_SYMBOL_GPL(iov_iter_get_pages_alloc); 1594d8207640SLogan Gunthorpe 1595d8207640SLogan Gunthorpe ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, 1596d8207640SLogan Gunthorpe struct page ***pages, size_t maxsize, size_t *start) 1597d8207640SLogan Gunthorpe { 1598d8207640SLogan Gunthorpe return iov_iter_get_pages_alloc(i, pages, maxsize, start, 0); 1599d8207640SLogan Gunthorpe } 1600eba2d3d7SAl Viro EXPORT_SYMBOL(iov_iter_get_pages_alloc2); 1601d879cb83SAl Viro 1602d879cb83SAl Viro size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, 1603d879cb83SAl Viro struct iov_iter *i) 1604d879cb83SAl Viro { 1605d879cb83SAl Viro __wsum sum, next; 1606d879cb83SAl Viro sum = *csum; 1607a41dad90SAl Viro if (WARN_ON_ONCE(!i->data_source)) 1608241699cdSAl Viro return 0; 1609a41dad90SAl Viro 16107baa5099SAl Viro iterate_and_advance(i, bytes, base, len, off, ({ 16117baa5099SAl Viro next = csum_and_copy_from_user(base, addr + off, len); 1612d879cb83SAl Viro sum = csum_block_add(sum, next, off); 16137baa5099SAl Viro next ? 0 : len; 1614d879cb83SAl Viro }), ({ 16157baa5099SAl Viro sum = csum_and_memcpy(addr + off, base, len, sum, off); 1616d879cb83SAl Viro }) 1617d879cb83SAl Viro ) 1618d879cb83SAl Viro *csum = sum; 1619d879cb83SAl Viro return bytes; 1620d879cb83SAl Viro } 1621d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_from_iter); 1622d879cb83SAl Viro 162352cbd23aSWillem de Bruijn size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *_csstate, 1624d879cb83SAl Viro struct iov_iter *i) 1625d879cb83SAl Viro { 162652cbd23aSWillem de Bruijn struct csum_state *csstate = _csstate; 1627d879cb83SAl Viro __wsum sum, next; 162878e1f386SAl Viro 1629a41dad90SAl Viro if (WARN_ON_ONCE(i->data_source)) 1630a41dad90SAl Viro return 0; 163178e1f386SAl Viro if (unlikely(iov_iter_is_discard(i))) { 1632c67f1fd2SAl Viro // can't use csum_memcpy() for that one - data is not copied 1633c67f1fd2SAl Viro csstate->csum = csum_block_add(csstate->csum, 1634c67f1fd2SAl Viro csum_partial(addr, bytes, 0), 1635c67f1fd2SAl Viro csstate->off); 1636c67f1fd2SAl Viro csstate->off += bytes; 1637c67f1fd2SAl Viro return bytes; 1638241699cdSAl Viro } 16396852df12SAl Viro 16406852df12SAl Viro sum = csum_shift(csstate->csum, csstate->off); 16416852df12SAl Viro if (unlikely(iov_iter_is_pipe(i))) 16426852df12SAl Viro bytes = csum_and_copy_to_pipe_iter(addr, bytes, i, &sum); 16436852df12SAl Viro else iterate_and_advance(i, bytes, base, len, off, ({ 16447baa5099SAl Viro next = csum_and_copy_to_user(addr + off, base, len); 1645d879cb83SAl Viro sum = csum_block_add(sum, next, off); 16467baa5099SAl Viro next ? 0 : len; 1647d879cb83SAl Viro }), ({ 16487baa5099SAl Viro sum = csum_and_memcpy(base, addr + off, len, sum, off); 1649d879cb83SAl Viro }) 1650d879cb83SAl Viro ) 1651594e450bSAl Viro csstate->csum = csum_shift(sum, csstate->off); 1652594e450bSAl Viro csstate->off += bytes; 1653d879cb83SAl Viro return bytes; 1654d879cb83SAl Viro } 1655d879cb83SAl Viro EXPORT_SYMBOL(csum_and_copy_to_iter); 1656d879cb83SAl Viro 1657d05f4435SSagi Grimberg size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, 1658d05f4435SSagi Grimberg struct iov_iter *i) 1659d05f4435SSagi Grimberg { 16607999096fSHerbert Xu #ifdef CONFIG_CRYPTO_HASH 1661d05f4435SSagi Grimberg struct ahash_request *hash = hashp; 1662d05f4435SSagi Grimberg struct scatterlist sg; 1663d05f4435SSagi Grimberg size_t copied; 1664d05f4435SSagi Grimberg 1665d05f4435SSagi Grimberg copied = copy_to_iter(addr, bytes, i); 1666d05f4435SSagi Grimberg sg_init_one(&sg, addr, copied); 1667d05f4435SSagi Grimberg ahash_request_set_crypt(hash, &sg, NULL, copied); 1668d05f4435SSagi Grimberg crypto_ahash_update(hash); 1669d05f4435SSagi Grimberg return copied; 167027fad74aSYueHaibing #else 167127fad74aSYueHaibing return 0; 167227fad74aSYueHaibing #endif 1673d05f4435SSagi Grimberg } 1674d05f4435SSagi Grimberg EXPORT_SYMBOL(hash_and_copy_to_iter); 1675d05f4435SSagi Grimberg 167666531c65SAl Viro static int iov_npages(const struct iov_iter *i, int maxpages) 1677d879cb83SAl Viro { 167866531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 167966531c65SAl Viro const struct iovec *p; 1680d879cb83SAl Viro int npages = 0; 1681d879cb83SAl Viro 1682de4f5fedSJens Axboe for (p = iter_iov(i); size; skip = 0, p++) { 168366531c65SAl Viro unsigned offs = offset_in_page(p->iov_base + skip); 168466531c65SAl Viro size_t len = min(p->iov_len - skip, size); 1685d879cb83SAl Viro 168666531c65SAl Viro if (len) { 168766531c65SAl Viro size -= len; 168866531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 168966531c65SAl Viro if (unlikely(npages > maxpages)) 169066531c65SAl Viro return maxpages; 169166531c65SAl Viro } 169266531c65SAl Viro } 169366531c65SAl Viro return npages; 169466531c65SAl Viro } 169566531c65SAl Viro 169666531c65SAl Viro static int bvec_npages(const struct iov_iter *i, int maxpages) 169766531c65SAl Viro { 169866531c65SAl Viro size_t skip = i->iov_offset, size = i->count; 169966531c65SAl Viro const struct bio_vec *p; 170066531c65SAl Viro int npages = 0; 170166531c65SAl Viro 170266531c65SAl Viro for (p = i->bvec; size; skip = 0, p++) { 170366531c65SAl Viro unsigned offs = (p->bv_offset + skip) % PAGE_SIZE; 170466531c65SAl Viro size_t len = min(p->bv_len - skip, size); 170566531c65SAl Viro 170666531c65SAl Viro size -= len; 170766531c65SAl Viro npages += DIV_ROUND_UP(offs + len, PAGE_SIZE); 170866531c65SAl Viro if (unlikely(npages > maxpages)) 170966531c65SAl Viro return maxpages; 171066531c65SAl Viro } 171166531c65SAl Viro return npages; 171266531c65SAl Viro } 171366531c65SAl Viro 171466531c65SAl Viro int iov_iter_npages(const struct iov_iter *i, int maxpages) 171566531c65SAl Viro { 171666531c65SAl Viro if (unlikely(!i->count)) 171766531c65SAl Viro return 0; 1718fcb14cb1SAl Viro if (likely(iter_is_ubuf(i))) { 1719fcb14cb1SAl Viro unsigned offs = offset_in_page(i->ubuf + i->iov_offset); 1720fcb14cb1SAl Viro int npages = DIV_ROUND_UP(offs + i->count, PAGE_SIZE); 1721fcb14cb1SAl Viro return min(npages, maxpages); 1722fcb14cb1SAl Viro } 172366531c65SAl Viro /* iovec and kvec have identical layouts */ 172466531c65SAl Viro if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) 172566531c65SAl Viro return iov_npages(i, maxpages); 172666531c65SAl Viro if (iov_iter_is_bvec(i)) 172766531c65SAl Viro return bvec_npages(i, maxpages); 172866531c65SAl Viro if (iov_iter_is_pipe(i)) { 172966531c65SAl Viro int npages; 1730241699cdSAl Viro 1731241699cdSAl Viro if (!sanity(i)) 1732241699cdSAl Viro return 0; 1733241699cdSAl Viro 173412d426abSAl Viro pipe_npages(i, &npages); 173566531c65SAl Viro return min(npages, maxpages); 173666531c65SAl Viro } 173766531c65SAl Viro if (iov_iter_is_xarray(i)) { 1738e4f8df86SAl Viro unsigned offset = (i->xarray_start + i->iov_offset) % PAGE_SIZE; 1739e4f8df86SAl Viro int npages = DIV_ROUND_UP(offset + i->count, PAGE_SIZE); 174066531c65SAl Viro return min(npages, maxpages); 174166531c65SAl Viro } 174266531c65SAl Viro return 0; 1743d879cb83SAl Viro } 1744d879cb83SAl Viro EXPORT_SYMBOL(iov_iter_npages); 1745d879cb83SAl Viro 1746d879cb83SAl Viro const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags) 1747d879cb83SAl Viro { 1748d879cb83SAl Viro *new = *old; 174900e23707SDavid Howells if (unlikely(iov_iter_is_pipe(new))) { 1750241699cdSAl Viro WARN_ON(1); 1751241699cdSAl Viro return NULL; 1752241699cdSAl Viro } 175300e23707SDavid Howells if (iov_iter_is_bvec(new)) 1754d879cb83SAl Viro return new->bvec = kmemdup(new->bvec, 1755d879cb83SAl Viro new->nr_segs * sizeof(struct bio_vec), 1756d879cb83SAl Viro flags); 1757fcb14cb1SAl Viro else if (iov_iter_is_kvec(new) || iter_is_iovec(new)) 1758d879cb83SAl Viro /* iovec and kvec have identical layout */ 1759de4f5fedSJens Axboe return new->__iov = kmemdup(new->__iov, 1760d879cb83SAl Viro new->nr_segs * sizeof(struct iovec), 1761d879cb83SAl Viro flags); 1762fcb14cb1SAl Viro return NULL; 1763d879cb83SAl Viro } 1764d879cb83SAl Viro EXPORT_SYMBOL(dup_iter); 1765bc917be8SAl Viro 176650f9a76eSJosh Poimboeuf static __noclone int copy_compat_iovec_from_user(struct iovec *iov, 1767bfdc5970SChristoph Hellwig const struct iovec __user *uvec, unsigned long nr_segs) 1768bfdc5970SChristoph Hellwig { 1769bfdc5970SChristoph Hellwig const struct compat_iovec __user *uiov = 1770bfdc5970SChristoph Hellwig (const struct compat_iovec __user *)uvec; 1771bfdc5970SChristoph Hellwig int ret = -EFAULT, i; 1772bfdc5970SChristoph Hellwig 1773a959a978SChristoph Hellwig if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) 1774bfdc5970SChristoph Hellwig return -EFAULT; 1775bfdc5970SChristoph Hellwig 1776bfdc5970SChristoph Hellwig for (i = 0; i < nr_segs; i++) { 1777bfdc5970SChristoph Hellwig compat_uptr_t buf; 1778bfdc5970SChristoph Hellwig compat_ssize_t len; 1779bfdc5970SChristoph Hellwig 1780bfdc5970SChristoph Hellwig unsafe_get_user(len, &uiov[i].iov_len, uaccess_end); 1781bfdc5970SChristoph Hellwig unsafe_get_user(buf, &uiov[i].iov_base, uaccess_end); 1782bfdc5970SChristoph Hellwig 1783bfdc5970SChristoph Hellwig /* check for compat_size_t not fitting in compat_ssize_t .. */ 1784bfdc5970SChristoph Hellwig if (len < 0) { 1785bfdc5970SChristoph Hellwig ret = -EINVAL; 1786bfdc5970SChristoph Hellwig goto uaccess_end; 1787bfdc5970SChristoph Hellwig } 1788bfdc5970SChristoph Hellwig iov[i].iov_base = compat_ptr(buf); 1789bfdc5970SChristoph Hellwig iov[i].iov_len = len; 1790bfdc5970SChristoph Hellwig } 1791bfdc5970SChristoph Hellwig 1792bfdc5970SChristoph Hellwig ret = 0; 1793bfdc5970SChristoph Hellwig uaccess_end: 1794bfdc5970SChristoph Hellwig user_access_end(); 1795bfdc5970SChristoph Hellwig return ret; 1796bfdc5970SChristoph Hellwig } 1797bfdc5970SChristoph Hellwig 1798bfdc5970SChristoph Hellwig static int copy_iovec_from_user(struct iovec *iov, 1799487c20b0SLinus Torvalds const struct iovec __user *uiov, unsigned long nr_segs) 1800fb041b59SDavid Laight { 1801487c20b0SLinus Torvalds int ret = -EFAULT; 1802bfdc5970SChristoph Hellwig 1803487c20b0SLinus Torvalds if (!user_access_begin(uiov, nr_segs * sizeof(*uiov))) 1804bfdc5970SChristoph Hellwig return -EFAULT; 1805bfdc5970SChristoph Hellwig 1806487c20b0SLinus Torvalds do { 1807487c20b0SLinus Torvalds void __user *buf; 1808487c20b0SLinus Torvalds ssize_t len; 1809487c20b0SLinus Torvalds 1810487c20b0SLinus Torvalds unsafe_get_user(len, &uiov->iov_len, uaccess_end); 1811487c20b0SLinus Torvalds unsafe_get_user(buf, &uiov->iov_base, uaccess_end); 1812487c20b0SLinus Torvalds 1813487c20b0SLinus Torvalds /* check for size_t not fitting in ssize_t .. */ 1814487c20b0SLinus Torvalds if (unlikely(len < 0)) { 1815487c20b0SLinus Torvalds ret = -EINVAL; 1816487c20b0SLinus Torvalds goto uaccess_end; 1817487c20b0SLinus Torvalds } 1818487c20b0SLinus Torvalds iov->iov_base = buf; 1819487c20b0SLinus Torvalds iov->iov_len = len; 1820487c20b0SLinus Torvalds 1821487c20b0SLinus Torvalds uiov++; iov++; 1822487c20b0SLinus Torvalds } while (--nr_segs); 1823487c20b0SLinus Torvalds 1824487c20b0SLinus Torvalds ret = 0; 1825487c20b0SLinus Torvalds uaccess_end: 1826487c20b0SLinus Torvalds user_access_end(); 1827487c20b0SLinus Torvalds return ret; 1828bfdc5970SChristoph Hellwig } 1829bfdc5970SChristoph Hellwig 1830bfdc5970SChristoph Hellwig struct iovec *iovec_from_user(const struct iovec __user *uvec, 1831bfdc5970SChristoph Hellwig unsigned long nr_segs, unsigned long fast_segs, 1832bfdc5970SChristoph Hellwig struct iovec *fast_iov, bool compat) 1833bfdc5970SChristoph Hellwig { 1834bfdc5970SChristoph Hellwig struct iovec *iov = fast_iov; 1835bfdc5970SChristoph Hellwig int ret; 1836fb041b59SDavid Laight 1837fb041b59SDavid Laight /* 1838bfdc5970SChristoph Hellwig * SuS says "The readv() function *may* fail if the iovcnt argument was 1839bfdc5970SChristoph Hellwig * less than or equal to 0, or greater than {IOV_MAX}. Linux has 1840fb041b59SDavid Laight * traditionally returned zero for zero segments, so... 1841fb041b59SDavid Laight */ 1842bfdc5970SChristoph Hellwig if (nr_segs == 0) 1843bfdc5970SChristoph Hellwig return iov; 1844bfdc5970SChristoph Hellwig if (nr_segs > UIO_MAXIOV) 1845bfdc5970SChristoph Hellwig return ERR_PTR(-EINVAL); 1846fb041b59SDavid Laight if (nr_segs > fast_segs) { 1847fb041b59SDavid Laight iov = kmalloc_array(nr_segs, sizeof(struct iovec), GFP_KERNEL); 1848bfdc5970SChristoph Hellwig if (!iov) 1849bfdc5970SChristoph Hellwig return ERR_PTR(-ENOMEM); 1850fb041b59SDavid Laight } 1851bfdc5970SChristoph Hellwig 1852487c20b0SLinus Torvalds if (unlikely(compat)) 1853bfdc5970SChristoph Hellwig ret = copy_compat_iovec_from_user(iov, uvec, nr_segs); 1854bfdc5970SChristoph Hellwig else 1855bfdc5970SChristoph Hellwig ret = copy_iovec_from_user(iov, uvec, nr_segs); 1856bfdc5970SChristoph Hellwig if (ret) { 1857bfdc5970SChristoph Hellwig if (iov != fast_iov) 1858bfdc5970SChristoph Hellwig kfree(iov); 1859bfdc5970SChristoph Hellwig return ERR_PTR(ret); 1860fb041b59SDavid Laight } 1861bfdc5970SChristoph Hellwig 1862bfdc5970SChristoph Hellwig return iov; 1863bfdc5970SChristoph Hellwig } 1864bfdc5970SChristoph Hellwig 18653b2deb0eSJens Axboe /* 18663b2deb0eSJens Axboe * Single segment iovec supplied by the user, import it as ITER_UBUF. 18673b2deb0eSJens Axboe */ 18683b2deb0eSJens Axboe static ssize_t __import_iovec_ubuf(int type, const struct iovec __user *uvec, 18693b2deb0eSJens Axboe struct iovec **iovp, struct iov_iter *i, 18703b2deb0eSJens Axboe bool compat) 18713b2deb0eSJens Axboe { 18723b2deb0eSJens Axboe struct iovec *iov = *iovp; 18733b2deb0eSJens Axboe ssize_t ret; 18743b2deb0eSJens Axboe 18753b2deb0eSJens Axboe if (compat) 18763b2deb0eSJens Axboe ret = copy_compat_iovec_from_user(iov, uvec, 1); 18773b2deb0eSJens Axboe else 18783b2deb0eSJens Axboe ret = copy_iovec_from_user(iov, uvec, 1); 18793b2deb0eSJens Axboe if (unlikely(ret)) 18803b2deb0eSJens Axboe return ret; 18813b2deb0eSJens Axboe 18823b2deb0eSJens Axboe ret = import_ubuf(type, iov->iov_base, iov->iov_len, i); 18833b2deb0eSJens Axboe if (unlikely(ret)) 18843b2deb0eSJens Axboe return ret; 18853b2deb0eSJens Axboe *iovp = NULL; 18863b2deb0eSJens Axboe return i->count; 18873b2deb0eSJens Axboe } 18883b2deb0eSJens Axboe 1889bfdc5970SChristoph Hellwig ssize_t __import_iovec(int type, const struct iovec __user *uvec, 1890bfdc5970SChristoph Hellwig unsigned nr_segs, unsigned fast_segs, struct iovec **iovp, 1891bfdc5970SChristoph Hellwig struct iov_iter *i, bool compat) 1892bfdc5970SChristoph Hellwig { 1893bfdc5970SChristoph Hellwig ssize_t total_len = 0; 1894bfdc5970SChristoph Hellwig unsigned long seg; 1895bfdc5970SChristoph Hellwig struct iovec *iov; 1896bfdc5970SChristoph Hellwig 18973b2deb0eSJens Axboe if (nr_segs == 1) 18983b2deb0eSJens Axboe return __import_iovec_ubuf(type, uvec, iovp, i, compat); 18993b2deb0eSJens Axboe 1900bfdc5970SChristoph Hellwig iov = iovec_from_user(uvec, nr_segs, fast_segs, *iovp, compat); 1901bfdc5970SChristoph Hellwig if (IS_ERR(iov)) { 1902bfdc5970SChristoph Hellwig *iovp = NULL; 1903bfdc5970SChristoph Hellwig return PTR_ERR(iov); 1904fb041b59SDavid Laight } 1905fb041b59SDavid Laight 1906fb041b59SDavid Laight /* 1907bfdc5970SChristoph Hellwig * According to the Single Unix Specification we should return EINVAL if 1908bfdc5970SChristoph Hellwig * an element length is < 0 when cast to ssize_t or if the total length 1909bfdc5970SChristoph Hellwig * would overflow the ssize_t return value of the system call. 1910fb041b59SDavid Laight * 1911fb041b59SDavid Laight * Linux caps all read/write calls to MAX_RW_COUNT, and avoids the 1912fb041b59SDavid Laight * overflow case. 1913fb041b59SDavid Laight */ 1914fb041b59SDavid Laight for (seg = 0; seg < nr_segs; seg++) { 1915fb041b59SDavid Laight ssize_t len = (ssize_t)iov[seg].iov_len; 1916fb041b59SDavid Laight 1917bfdc5970SChristoph Hellwig if (!access_ok(iov[seg].iov_base, len)) { 1918bfdc5970SChristoph Hellwig if (iov != *iovp) 1919bfdc5970SChristoph Hellwig kfree(iov); 1920bfdc5970SChristoph Hellwig *iovp = NULL; 1921bfdc5970SChristoph Hellwig return -EFAULT; 1922fb041b59SDavid Laight } 1923bfdc5970SChristoph Hellwig 1924bfdc5970SChristoph Hellwig if (len > MAX_RW_COUNT - total_len) { 1925bfdc5970SChristoph Hellwig len = MAX_RW_COUNT - total_len; 1926fb041b59SDavid Laight iov[seg].iov_len = len; 1927fb041b59SDavid Laight } 1928bfdc5970SChristoph Hellwig total_len += len; 1929fb041b59SDavid Laight } 1930bfdc5970SChristoph Hellwig 1931bfdc5970SChristoph Hellwig iov_iter_init(i, type, iov, nr_segs, total_len); 1932bfdc5970SChristoph Hellwig if (iov == *iovp) 1933bfdc5970SChristoph Hellwig *iovp = NULL; 1934bfdc5970SChristoph Hellwig else 1935bfdc5970SChristoph Hellwig *iovp = iov; 1936bfdc5970SChristoph Hellwig return total_len; 1937fb041b59SDavid Laight } 1938fb041b59SDavid Laight 1939ffecee4fSVegard Nossum /** 1940ffecee4fSVegard Nossum * import_iovec() - Copy an array of &struct iovec from userspace 1941ffecee4fSVegard Nossum * into the kernel, check that it is valid, and initialize a new 1942ffecee4fSVegard Nossum * &struct iov_iter iterator to access it. 1943ffecee4fSVegard Nossum * 1944ffecee4fSVegard Nossum * @type: One of %READ or %WRITE. 1945bfdc5970SChristoph Hellwig * @uvec: Pointer to the userspace array. 1946ffecee4fSVegard Nossum * @nr_segs: Number of elements in userspace array. 1947ffecee4fSVegard Nossum * @fast_segs: Number of elements in @iov. 1948bfdc5970SChristoph Hellwig * @iovp: (input and output parameter) Pointer to pointer to (usually small 1949ffecee4fSVegard Nossum * on-stack) kernel array. 1950ffecee4fSVegard Nossum * @i: Pointer to iterator that will be initialized on success. 1951ffecee4fSVegard Nossum * 1952ffecee4fSVegard Nossum * If the array pointed to by *@iov is large enough to hold all @nr_segs, 1953ffecee4fSVegard Nossum * then this function places %NULL in *@iov on return. Otherwise, a new 1954ffecee4fSVegard Nossum * array will be allocated and the result placed in *@iov. This means that 1955ffecee4fSVegard Nossum * the caller may call kfree() on *@iov regardless of whether the small 1956ffecee4fSVegard Nossum * on-stack array was used or not (and regardless of whether this function 1957ffecee4fSVegard Nossum * returns an error or not). 1958ffecee4fSVegard Nossum * 195987e5e6daSJens Axboe * Return: Negative error code on error, bytes imported on success 1960ffecee4fSVegard Nossum */ 1961bfdc5970SChristoph Hellwig ssize_t import_iovec(int type, const struct iovec __user *uvec, 1962bc917be8SAl Viro unsigned nr_segs, unsigned fast_segs, 1963bfdc5970SChristoph Hellwig struct iovec **iovp, struct iov_iter *i) 1964bc917be8SAl Viro { 196589cd35c5SChristoph Hellwig return __import_iovec(type, uvec, nr_segs, fast_segs, iovp, i, 196689cd35c5SChristoph Hellwig in_compat_syscall()); 1967bc917be8SAl Viro } 1968bc917be8SAl Viro EXPORT_SYMBOL(import_iovec); 1969bc917be8SAl Viro 1970bc917be8SAl Viro int import_single_range(int rw, void __user *buf, size_t len, 1971bc917be8SAl Viro struct iovec *iov, struct iov_iter *i) 1972bc917be8SAl Viro { 1973bc917be8SAl Viro if (len > MAX_RW_COUNT) 1974bc917be8SAl Viro len = MAX_RW_COUNT; 197596d4f267SLinus Torvalds if (unlikely(!access_ok(buf, len))) 1976bc917be8SAl Viro return -EFAULT; 1977bc917be8SAl Viro 1978e03ad4eeSJens Axboe iov_iter_ubuf(i, rw, buf, len); 1979bc917be8SAl Viro return 0; 1980bc917be8SAl Viro } 1981e1267585SAl Viro EXPORT_SYMBOL(import_single_range); 19828fb0f47aSJens Axboe 19832ad9bd83SJens Axboe int import_ubuf(int rw, void __user *buf, size_t len, struct iov_iter *i) 19842ad9bd83SJens Axboe { 19852ad9bd83SJens Axboe if (len > MAX_RW_COUNT) 19862ad9bd83SJens Axboe len = MAX_RW_COUNT; 19872ad9bd83SJens Axboe if (unlikely(!access_ok(buf, len))) 19882ad9bd83SJens Axboe return -EFAULT; 19892ad9bd83SJens Axboe 19902ad9bd83SJens Axboe iov_iter_ubuf(i, rw, buf, len); 19912ad9bd83SJens Axboe return 0; 19922ad9bd83SJens Axboe } 19932ad9bd83SJens Axboe 19948fb0f47aSJens Axboe /** 19958fb0f47aSJens Axboe * iov_iter_restore() - Restore a &struct iov_iter to the same state as when 19968fb0f47aSJens Axboe * iov_iter_save_state() was called. 19978fb0f47aSJens Axboe * 19988fb0f47aSJens Axboe * @i: &struct iov_iter to restore 19998fb0f47aSJens Axboe * @state: state to restore from 20008fb0f47aSJens Axboe * 20018fb0f47aSJens Axboe * Used after iov_iter_save_state() to bring restore @i, if operations may 20028fb0f47aSJens Axboe * have advanced it. 20038fb0f47aSJens Axboe * 20048fb0f47aSJens Axboe * Note: only works on ITER_IOVEC, ITER_BVEC, and ITER_KVEC 20058fb0f47aSJens Axboe */ 20068fb0f47aSJens Axboe void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) 20078fb0f47aSJens Axboe { 20084397a17cSKeith Busch if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) && 20094397a17cSKeith Busch !iter_is_ubuf(i)) && !iov_iter_is_kvec(i)) 20108fb0f47aSJens Axboe return; 20118fb0f47aSJens Axboe i->iov_offset = state->iov_offset; 20128fb0f47aSJens Axboe i->count = state->count; 2013fcb14cb1SAl Viro if (iter_is_ubuf(i)) 2014fcb14cb1SAl Viro return; 20158fb0f47aSJens Axboe /* 20168fb0f47aSJens Axboe * For the *vec iters, nr_segs + iov is constant - if we increment 20178fb0f47aSJens Axboe * the vec, then we also decrement the nr_segs count. Hence we don't 20188fb0f47aSJens Axboe * need to track both of these, just one is enough and we can deduct 20198fb0f47aSJens Axboe * the other from that. ITER_KVEC and ITER_IOVEC are the same struct 20208fb0f47aSJens Axboe * size, so we can just increment the iov pointer as they are unionzed. 20218fb0f47aSJens Axboe * ITER_BVEC _may_ be the same size on some archs, but on others it is 20228fb0f47aSJens Axboe * not. Be safe and handle it separately. 20238fb0f47aSJens Axboe */ 20248fb0f47aSJens Axboe BUILD_BUG_ON(sizeof(struct iovec) != sizeof(struct kvec)); 20258fb0f47aSJens Axboe if (iov_iter_is_bvec(i)) 20268fb0f47aSJens Axboe i->bvec -= state->nr_segs - i->nr_segs; 20278fb0f47aSJens Axboe else 2028de4f5fedSJens Axboe i->__iov -= state->nr_segs - i->nr_segs; 20298fb0f47aSJens Axboe i->nr_segs = state->nr_segs; 20308fb0f47aSJens Axboe } 20317d58fe73SDavid Howells 20327d58fe73SDavid Howells /* 20337d58fe73SDavid Howells * Extract a list of contiguous pages from an ITER_XARRAY iterator. This does not 20347d58fe73SDavid Howells * get references on the pages, nor does it get a pin on them. 20357d58fe73SDavid Howells */ 20367d58fe73SDavid Howells static ssize_t iov_iter_extract_xarray_pages(struct iov_iter *i, 20377d58fe73SDavid Howells struct page ***pages, size_t maxsize, 20387d58fe73SDavid Howells unsigned int maxpages, 20397d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 20407d58fe73SDavid Howells size_t *offset0) 20417d58fe73SDavid Howells { 20427d58fe73SDavid Howells struct page *page, **p; 20437d58fe73SDavid Howells unsigned int nr = 0, offset; 20447d58fe73SDavid Howells loff_t pos = i->xarray_start + i->iov_offset; 20457d58fe73SDavid Howells pgoff_t index = pos >> PAGE_SHIFT; 20467d58fe73SDavid Howells XA_STATE(xas, i->xarray, index); 20477d58fe73SDavid Howells 20487d58fe73SDavid Howells offset = pos & ~PAGE_MASK; 20497d58fe73SDavid Howells *offset0 = offset; 20507d58fe73SDavid Howells 20517d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 20527d58fe73SDavid Howells if (!maxpages) 20537d58fe73SDavid Howells return -ENOMEM; 20547d58fe73SDavid Howells p = *pages; 20557d58fe73SDavid Howells 20567d58fe73SDavid Howells rcu_read_lock(); 20577d58fe73SDavid Howells for (page = xas_load(&xas); page; page = xas_next(&xas)) { 20587d58fe73SDavid Howells if (xas_retry(&xas, page)) 20597d58fe73SDavid Howells continue; 20607d58fe73SDavid Howells 20617d58fe73SDavid Howells /* Has the page moved or been split? */ 20627d58fe73SDavid Howells if (unlikely(page != xas_reload(&xas))) { 20637d58fe73SDavid Howells xas_reset(&xas); 20647d58fe73SDavid Howells continue; 20657d58fe73SDavid Howells } 20667d58fe73SDavid Howells 20677d58fe73SDavid Howells p[nr++] = find_subpage(page, xas.xa_index); 20687d58fe73SDavid Howells if (nr == maxpages) 20697d58fe73SDavid Howells break; 20707d58fe73SDavid Howells } 20717d58fe73SDavid Howells rcu_read_unlock(); 20727d58fe73SDavid Howells 20737d58fe73SDavid Howells maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize); 20747d58fe73SDavid Howells iov_iter_advance(i, maxsize); 20757d58fe73SDavid Howells return maxsize; 20767d58fe73SDavid Howells } 20777d58fe73SDavid Howells 20787d58fe73SDavid Howells /* 20797d58fe73SDavid Howells * Extract a list of contiguous pages from an ITER_BVEC iterator. This does 20807d58fe73SDavid Howells * not get references on the pages, nor does it get a pin on them. 20817d58fe73SDavid Howells */ 20827d58fe73SDavid Howells static ssize_t iov_iter_extract_bvec_pages(struct iov_iter *i, 20837d58fe73SDavid Howells struct page ***pages, size_t maxsize, 20847d58fe73SDavid Howells unsigned int maxpages, 20857d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 20867d58fe73SDavid Howells size_t *offset0) 20877d58fe73SDavid Howells { 20887d58fe73SDavid Howells struct page **p, *page; 20897d58fe73SDavid Howells size_t skip = i->iov_offset, offset; 20907d58fe73SDavid Howells int k; 20917d58fe73SDavid Howells 20927d58fe73SDavid Howells for (;;) { 20937d58fe73SDavid Howells if (i->nr_segs == 0) 20947d58fe73SDavid Howells return 0; 20957d58fe73SDavid Howells maxsize = min(maxsize, i->bvec->bv_len - skip); 20967d58fe73SDavid Howells if (maxsize) 20977d58fe73SDavid Howells break; 20987d58fe73SDavid Howells i->iov_offset = 0; 20997d58fe73SDavid Howells i->nr_segs--; 21007d58fe73SDavid Howells i->bvec++; 21017d58fe73SDavid Howells skip = 0; 21027d58fe73SDavid Howells } 21037d58fe73SDavid Howells 21047d58fe73SDavid Howells skip += i->bvec->bv_offset; 21057d58fe73SDavid Howells page = i->bvec->bv_page + skip / PAGE_SIZE; 21067d58fe73SDavid Howells offset = skip % PAGE_SIZE; 21077d58fe73SDavid Howells *offset0 = offset; 21087d58fe73SDavid Howells 21097d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 21107d58fe73SDavid Howells if (!maxpages) 21117d58fe73SDavid Howells return -ENOMEM; 21127d58fe73SDavid Howells p = *pages; 21137d58fe73SDavid Howells for (k = 0; k < maxpages; k++) 21147d58fe73SDavid Howells p[k] = page + k; 21157d58fe73SDavid Howells 21167d58fe73SDavid Howells maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset); 21177d58fe73SDavid Howells iov_iter_advance(i, maxsize); 21187d58fe73SDavid Howells return maxsize; 21197d58fe73SDavid Howells } 21207d58fe73SDavid Howells 21217d58fe73SDavid Howells /* 21227d58fe73SDavid Howells * Extract a list of virtually contiguous pages from an ITER_KVEC iterator. 21237d58fe73SDavid Howells * This does not get references on the pages, nor does it get a pin on them. 21247d58fe73SDavid Howells */ 21257d58fe73SDavid Howells static ssize_t iov_iter_extract_kvec_pages(struct iov_iter *i, 21267d58fe73SDavid Howells struct page ***pages, size_t maxsize, 21277d58fe73SDavid Howells unsigned int maxpages, 21287d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 21297d58fe73SDavid Howells size_t *offset0) 21307d58fe73SDavid Howells { 21317d58fe73SDavid Howells struct page **p, *page; 21327d58fe73SDavid Howells const void *kaddr; 21337d58fe73SDavid Howells size_t skip = i->iov_offset, offset, len; 21347d58fe73SDavid Howells int k; 21357d58fe73SDavid Howells 21367d58fe73SDavid Howells for (;;) { 21377d58fe73SDavid Howells if (i->nr_segs == 0) 21387d58fe73SDavid Howells return 0; 21397d58fe73SDavid Howells maxsize = min(maxsize, i->kvec->iov_len - skip); 21407d58fe73SDavid Howells if (maxsize) 21417d58fe73SDavid Howells break; 21427d58fe73SDavid Howells i->iov_offset = 0; 21437d58fe73SDavid Howells i->nr_segs--; 21447d58fe73SDavid Howells i->kvec++; 21457d58fe73SDavid Howells skip = 0; 21467d58fe73SDavid Howells } 21477d58fe73SDavid Howells 21487d58fe73SDavid Howells kaddr = i->kvec->iov_base + skip; 21497d58fe73SDavid Howells offset = (unsigned long)kaddr & ~PAGE_MASK; 21507d58fe73SDavid Howells *offset0 = offset; 21517d58fe73SDavid Howells 21527d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 21537d58fe73SDavid Howells if (!maxpages) 21547d58fe73SDavid Howells return -ENOMEM; 21557d58fe73SDavid Howells p = *pages; 21567d58fe73SDavid Howells 21577d58fe73SDavid Howells kaddr -= offset; 21587d58fe73SDavid Howells len = offset + maxsize; 21597d58fe73SDavid Howells for (k = 0; k < maxpages; k++) { 21607d58fe73SDavid Howells size_t seg = min_t(size_t, len, PAGE_SIZE); 21617d58fe73SDavid Howells 21627d58fe73SDavid Howells if (is_vmalloc_or_module_addr(kaddr)) 21637d58fe73SDavid Howells page = vmalloc_to_page(kaddr); 21647d58fe73SDavid Howells else 21657d58fe73SDavid Howells page = virt_to_page(kaddr); 21667d58fe73SDavid Howells 21677d58fe73SDavid Howells p[k] = page; 21687d58fe73SDavid Howells len -= seg; 21697d58fe73SDavid Howells kaddr += PAGE_SIZE; 21707d58fe73SDavid Howells } 21717d58fe73SDavid Howells 21727d58fe73SDavid Howells maxsize = min_t(size_t, maxsize, maxpages * PAGE_SIZE - offset); 21737d58fe73SDavid Howells iov_iter_advance(i, maxsize); 21747d58fe73SDavid Howells return maxsize; 21757d58fe73SDavid Howells } 21767d58fe73SDavid Howells 21777d58fe73SDavid Howells /* 21787d58fe73SDavid Howells * Extract a list of contiguous pages from a user iterator and get a pin on 21797d58fe73SDavid Howells * each of them. This should only be used if the iterator is user-backed 21807d58fe73SDavid Howells * (IOBUF/UBUF). 21817d58fe73SDavid Howells * 21827d58fe73SDavid Howells * It does not get refs on the pages, but the pages must be unpinned by the 21837d58fe73SDavid Howells * caller once the transfer is complete. 21847d58fe73SDavid Howells * 21857d58fe73SDavid Howells * This is safe to be used where background IO/DMA *is* going to be modifying 21867d58fe73SDavid Howells * the buffer; using a pin rather than a ref makes forces fork() to give the 21877d58fe73SDavid Howells * child a copy of the page. 21887d58fe73SDavid Howells */ 21897d58fe73SDavid Howells static ssize_t iov_iter_extract_user_pages(struct iov_iter *i, 21907d58fe73SDavid Howells struct page ***pages, 21917d58fe73SDavid Howells size_t maxsize, 21927d58fe73SDavid Howells unsigned int maxpages, 21937d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 21947d58fe73SDavid Howells size_t *offset0) 21957d58fe73SDavid Howells { 21967d58fe73SDavid Howells unsigned long addr; 21977d58fe73SDavid Howells unsigned int gup_flags = 0; 21987d58fe73SDavid Howells size_t offset; 21997d58fe73SDavid Howells int res; 22007d58fe73SDavid Howells 22017d58fe73SDavid Howells if (i->data_source == ITER_DEST) 22027d58fe73SDavid Howells gup_flags |= FOLL_WRITE; 22037d58fe73SDavid Howells if (extraction_flags & ITER_ALLOW_P2PDMA) 22047d58fe73SDavid Howells gup_flags |= FOLL_PCI_P2PDMA; 22057d58fe73SDavid Howells if (i->nofault) 22067d58fe73SDavid Howells gup_flags |= FOLL_NOFAULT; 22077d58fe73SDavid Howells 22087d58fe73SDavid Howells addr = first_iovec_segment(i, &maxsize); 22097d58fe73SDavid Howells *offset0 = offset = addr % PAGE_SIZE; 22107d58fe73SDavid Howells addr &= PAGE_MASK; 22117d58fe73SDavid Howells maxpages = want_pages_array(pages, maxsize, offset, maxpages); 22127d58fe73SDavid Howells if (!maxpages) 22137d58fe73SDavid Howells return -ENOMEM; 22147d58fe73SDavid Howells res = pin_user_pages_fast(addr, maxpages, gup_flags, *pages); 22157d58fe73SDavid Howells if (unlikely(res <= 0)) 22167d58fe73SDavid Howells return res; 22177d58fe73SDavid Howells maxsize = min_t(size_t, maxsize, res * PAGE_SIZE - offset); 22187d58fe73SDavid Howells iov_iter_advance(i, maxsize); 22197d58fe73SDavid Howells return maxsize; 22207d58fe73SDavid Howells } 22217d58fe73SDavid Howells 22227d58fe73SDavid Howells /** 22237d58fe73SDavid Howells * iov_iter_extract_pages - Extract a list of contiguous pages from an iterator 22247d58fe73SDavid Howells * @i: The iterator to extract from 22257d58fe73SDavid Howells * @pages: Where to return the list of pages 22267d58fe73SDavid Howells * @maxsize: The maximum amount of iterator to extract 22277d58fe73SDavid Howells * @maxpages: The maximum size of the list of pages 22287d58fe73SDavid Howells * @extraction_flags: Flags to qualify request 22297d58fe73SDavid Howells * @offset0: Where to return the starting offset into (*@pages)[0] 22307d58fe73SDavid Howells * 22317d58fe73SDavid Howells * Extract a list of contiguous pages from the current point of the iterator, 22327d58fe73SDavid Howells * advancing the iterator. The maximum number of pages and the maximum amount 22337d58fe73SDavid Howells * of page contents can be set. 22347d58fe73SDavid Howells * 22357d58fe73SDavid Howells * If *@pages is NULL, a page list will be allocated to the required size and 22367d58fe73SDavid Howells * *@pages will be set to its base. If *@pages is not NULL, it will be assumed 22377d58fe73SDavid Howells * that the caller allocated a page list at least @maxpages in size and this 22387d58fe73SDavid Howells * will be filled in. 22397d58fe73SDavid Howells * 22407d58fe73SDavid Howells * @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA 22417d58fe73SDavid Howells * be allowed on the pages extracted. 22427d58fe73SDavid Howells * 22437d58fe73SDavid Howells * The iov_iter_extract_will_pin() function can be used to query how cleanup 22447d58fe73SDavid Howells * should be performed. 22457d58fe73SDavid Howells * 22467d58fe73SDavid Howells * Extra refs or pins on the pages may be obtained as follows: 22477d58fe73SDavid Howells * 22487d58fe73SDavid Howells * (*) If the iterator is user-backed (ITER_IOVEC/ITER_UBUF), pins will be 22497d58fe73SDavid Howells * added to the pages, but refs will not be taken. 22507d58fe73SDavid Howells * iov_iter_extract_will_pin() will return true. 22517d58fe73SDavid Howells * 22527d58fe73SDavid Howells * (*) If the iterator is ITER_KVEC, ITER_BVEC or ITER_XARRAY, the pages are 22537d58fe73SDavid Howells * merely listed; no extra refs or pins are obtained. 22547d58fe73SDavid Howells * iov_iter_extract_will_pin() will return 0. 22557d58fe73SDavid Howells * 22567d58fe73SDavid Howells * Note also: 22577d58fe73SDavid Howells * 22587d58fe73SDavid Howells * (*) Use with ITER_DISCARD is not supported as that has no content. 22597d58fe73SDavid Howells * 22607d58fe73SDavid Howells * On success, the function sets *@pages to the new pagelist, if allocated, and 22617d58fe73SDavid Howells * sets *offset0 to the offset into the first page. 22627d58fe73SDavid Howells * 22637d58fe73SDavid Howells * It may also return -ENOMEM and -EFAULT. 22647d58fe73SDavid Howells */ 22657d58fe73SDavid Howells ssize_t iov_iter_extract_pages(struct iov_iter *i, 22667d58fe73SDavid Howells struct page ***pages, 22677d58fe73SDavid Howells size_t maxsize, 22687d58fe73SDavid Howells unsigned int maxpages, 22697d58fe73SDavid Howells iov_iter_extraction_t extraction_flags, 22707d58fe73SDavid Howells size_t *offset0) 22717d58fe73SDavid Howells { 22727d58fe73SDavid Howells maxsize = min_t(size_t, min_t(size_t, maxsize, i->count), MAX_RW_COUNT); 22737d58fe73SDavid Howells if (!maxsize) 22747d58fe73SDavid Howells return 0; 22757d58fe73SDavid Howells 22767d58fe73SDavid Howells if (likely(user_backed_iter(i))) 22777d58fe73SDavid Howells return iov_iter_extract_user_pages(i, pages, maxsize, 22787d58fe73SDavid Howells maxpages, extraction_flags, 22797d58fe73SDavid Howells offset0); 22807d58fe73SDavid Howells if (iov_iter_is_kvec(i)) 22817d58fe73SDavid Howells return iov_iter_extract_kvec_pages(i, pages, maxsize, 22827d58fe73SDavid Howells maxpages, extraction_flags, 22837d58fe73SDavid Howells offset0); 22847d58fe73SDavid Howells if (iov_iter_is_bvec(i)) 22857d58fe73SDavid Howells return iov_iter_extract_bvec_pages(i, pages, maxsize, 22867d58fe73SDavid Howells maxpages, extraction_flags, 22877d58fe73SDavid Howells offset0); 22887d58fe73SDavid Howells if (iov_iter_is_xarray(i)) 22897d58fe73SDavid Howells return iov_iter_extract_xarray_pages(i, pages, maxsize, 22907d58fe73SDavid Howells maxpages, extraction_flags, 22917d58fe73SDavid Howells offset0); 22927d58fe73SDavid Howells return -EFAULT; 22937d58fe73SDavid Howells } 22947d58fe73SDavid Howells EXPORT_SYMBOL_GPL(iov_iter_extract_pages); 2295